fix x86-2.6 compile
[openwrt.git] / target / linux / generic-2.6 / patches / 600-x86_lzma.patch
1 diff -Nur linux-2.6.21.1/arch/i386/boot/compressed/LzmaDecode.c linux-2.6.21.1-owrt/arch/i386/boot/compressed/LzmaDecode.c
2 --- linux-2.6.21.1/arch/i386/boot/compressed/LzmaDecode.c       1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1-owrt/arch/i386/boot/compressed/LzmaDecode.c  2007-05-14 11:55:38.000000000 +0200
4 @@ -0,0 +1,586 @@
5 +/*
6 +  LzmaDecode.c
7 +  LZMA Decoder (optimized for Speed version)
8 +  
9 +  LZMA SDK 4.17 Copyright (c) 1999-2005 Igor Pavlov (2005-04-05)
10 +  http://www.7-zip.org/
11 +
12 +  LZMA SDK is licensed under two licenses:
13 +  1) GNU Lesser General Public License (GNU LGPL)
14 +  2) Common Public License (CPL)
15 +  It means that you can select one of these two licenses and 
16 +  follow rules of that license.
17 +
18 +  SPECIAL EXCEPTION:
19 +  Igor Pavlov, as the author of this Code, expressly permits you to 
20 +  statically or dynamically link your Code (or bind by name) to the 
21 +  interfaces of this file without subjecting your linked Code to the 
22 +  terms of the CPL or GNU LGPL. Any modifications or additions 
23 +  to this file, however, are subject to the LGPL or CPL terms.
24 +*/
25 +
26 +#include "LzmaDecode.h"
27 +
28 +#ifndef Byte
29 +#define Byte unsigned char
30 +#endif
31 +
32 +#define kNumTopBits 24
33 +#define kTopValue ((UInt32)1 << kNumTopBits)
34 +
35 +#define kNumBitModelTotalBits 11
36 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
37 +#define kNumMoveBits 5
38 +
39 +#define RC_READ_BYTE (*Buffer++)
40 +
41 +#define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
42 +  { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
43 +
44 +#ifdef _LZMA_IN_CB
45 +
46 +#define RC_TEST { if (Buffer == BufferLim) \
47 +  { UInt32 size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
48 +  BufferLim = Buffer + size; if (size == 0) return LZMA_RESULT_DATA_ERROR; }}
49 +
50 +#define RC_INIT Buffer = BufferLim = 0; RC_INIT2
51 +
52 +#else
53 +
54 +#define RC_TEST { if (Buffer == BufferLim) return LZMA_RESULT_DATA_ERROR; }
55 +
56 +#define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
57
58 +#endif
59 +
60 +#define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
61 +
62 +#define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
63 +#define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
64 +#define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
65 +
66 +#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
67 +  { UpdateBit0(p); mi <<= 1; A0; } else \
68 +  { UpdateBit1(p); mi = (mi + mi) + 1; A1; } 
69 +  
70 +#define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)               
71 +
72 +#define RangeDecoderBitTreeDecode(probs, numLevels, res) \
73 +  { int i = numLevels; res = 1; \
74 +  do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
75 +  res -= (1 << numLevels); }
76 +
77 +
78 +#define kNumPosBitsMax 4
79 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
80 +
81 +#define kLenNumLowBits 3
82 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
83 +#define kLenNumMidBits 3
84 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
85 +#define kLenNumHighBits 8
86 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
87 +
88 +#define LenChoice 0
89 +#define LenChoice2 (LenChoice + 1)
90 +#define LenLow (LenChoice2 + 1)
91 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
92 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
93 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols) 
94 +
95 +
96 +#define kNumStates 12
97 +#define kNumLitStates 7
98 +
99 +#define kStartPosModelIndex 4
100 +#define kEndPosModelIndex 14
101 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
102 +
103 +#define kNumPosSlotBits 6
104 +#define kNumLenToPosStates 4
105 +
106 +#define kNumAlignBits 4
107 +#define kAlignTableSize (1 << kNumAlignBits)
108 +
109 +#define kMatchMinLen 2
110 +
111 +#define IsMatch 0
112 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
113 +#define IsRepG0 (IsRep + kNumStates)
114 +#define IsRepG1 (IsRepG0 + kNumStates)
115 +#define IsRepG2 (IsRepG1 + kNumStates)
116 +#define IsRep0Long (IsRepG2 + kNumStates)
117 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
118 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
119 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
120 +#define LenCoder (Align + kAlignTableSize)
121 +#define RepLenCoder (LenCoder + kNumLenProbs)
122 +#define Literal (RepLenCoder + kNumLenProbs)
123 +
124 +#if Literal != LZMA_BASE_SIZE
125 +StopCompilingDueBUG
126 +#endif
127 +
128 +#ifdef _LZMA_OUT_READ
129 +
130 +typedef struct _LzmaVarState
131 +{
132 +  Byte *Buffer;
133 +  Byte *BufferLim;
134 +  UInt32 Range;
135 +  UInt32 Code;
136 +  #ifdef _LZMA_IN_CB
137 +  ILzmaInCallback *InCallback;
138 +  #endif
139 +  Byte *Dictionary;
140 +  UInt32 DictionarySize;
141 +  UInt32 DictionaryPos;
142 +  UInt32 GlobalPos;
143 +  UInt32 Reps[4];
144 +  int lc;
145 +  int lp;
146 +  int pb;
147 +  int State;
148 +  int RemainLen;
149 +  Byte TempDictionary[4];
150 +} LzmaVarState;
151 +
152 +int LzmaDecoderInit(
153 +    unsigned char *buffer, UInt32 bufferSize,
154 +    int lc, int lp, int pb,
155 +    unsigned char *dictionary, UInt32 dictionarySize,
156 +    #ifdef _LZMA_IN_CB
157 +    ILzmaInCallback *InCallback
158 +    #else
159 +    unsigned char *inStream, UInt32 inSize
160 +    #endif
161 +    )
162 +{
163 +  Byte *Buffer;
164 +  Byte *BufferLim;
165 +  UInt32 Range;
166 +  UInt32 Code;
167 +  LzmaVarState *vs = (LzmaVarState *)buffer;
168 +  CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
169 +  UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
170 +  UInt32 i;
171 +  if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
172 +    return LZMA_RESULT_NOT_ENOUGH_MEM;
173 +  vs->Dictionary = dictionary;
174 +  vs->DictionarySize = dictionarySize;
175 +  vs->DictionaryPos = 0;
176 +  vs->GlobalPos = 0;
177 +  vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
178 +  vs->lc = lc;
179 +  vs->lp = lp;
180 +  vs->pb = pb;
181 +  vs->State = 0;
182 +  vs->RemainLen = 0;
183 +  dictionary[dictionarySize - 1] = 0;
184 +  for (i = 0; i < numProbs; i++)
185 +    p[i] = kBitModelTotal >> 1; 
186 +
187 +  #ifdef _LZMA_IN_CB
188 +  RC_INIT;
189 +  #else
190 +  RC_INIT(inStream, inSize);
191 +  #endif
192 +  vs->Buffer = Buffer;
193 +  vs->BufferLim = BufferLim;
194 +  vs->Range = Range;
195 +  vs->Code = Code;
196 +  #ifdef _LZMA_IN_CB
197 +  vs->InCallback = InCallback;
198 +  #endif
199 +
200 +  return LZMA_RESULT_OK;
201 +}
202 +
203 +int LzmaDecode(unsigned char *buffer, 
204 +    unsigned char *outStream, UInt32 outSize,
205 +    UInt32 *outSizeProcessed)
206 +{
207 +  LzmaVarState *vs = (LzmaVarState *)buffer;
208 +  Byte *Buffer = vs->Buffer;
209 +  Byte *BufferLim = vs->BufferLim;
210 +  UInt32 Range = vs->Range;
211 +  UInt32 Code = vs->Code;
212 +  #ifdef _LZMA_IN_CB
213 +  ILzmaInCallback *InCallback = vs->InCallback;
214 +  #endif
215 +  CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
216 +  int state = vs->State;
217 +  Byte previousByte;
218 +  UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
219 +  UInt32 nowPos = 0;
220 +  UInt32 posStateMask = (1 << (vs->pb)) - 1;
221 +  UInt32 literalPosMask = (1 << (vs->lp)) - 1;
222 +  int lc = vs->lc;
223 +  int len = vs->RemainLen;
224 +  UInt32 globalPos = vs->GlobalPos;
225 +
226 +  Byte *dictionary = vs->Dictionary;
227 +  UInt32 dictionarySize = vs->DictionarySize;
228 +  UInt32 dictionaryPos = vs->DictionaryPos;
229 +
230 +  Byte tempDictionary[4];
231 +  if (dictionarySize == 0)
232 +  {
233 +    dictionary = tempDictionary;
234 +    dictionarySize = 1;
235 +    tempDictionary[0] = vs->TempDictionary[0];
236 +  }
237 +
238 +  if (len == -1)
239 +  {
240 +    *outSizeProcessed = 0;
241 +    return LZMA_RESULT_OK;
242 +  }
243 +
244 +  while(len != 0 && nowPos < outSize)
245 +  {
246 +    UInt32 pos = dictionaryPos - rep0;
247 +    if (pos >= dictionarySize)
248 +      pos += dictionarySize;
249 +    outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
250 +    if (++dictionaryPos == dictionarySize)
251 +      dictionaryPos = 0;
252 +    len--;
253 +  }
254 +  if (dictionaryPos == 0)
255 +    previousByte = dictionary[dictionarySize - 1];
256 +  else
257 +    previousByte = dictionary[dictionaryPos - 1];
258 +#else
259 +
260 +int LzmaDecode(
261 +    Byte *buffer, UInt32 bufferSize,
262 +    int lc, int lp, int pb,
263 +    #ifdef _LZMA_IN_CB
264 +    ILzmaInCallback *InCallback,
265 +    #else
266 +    unsigned char *inStream, UInt32 inSize,
267 +    #endif
268 +    unsigned char *outStream, UInt32 outSize,
269 +    UInt32 *outSizeProcessed)
270 +{
271 +  UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
272 +  CProb *p = (CProb *)buffer;
273 +
274 +  UInt32 i;
275 +  int state = 0;
276 +  Byte previousByte = 0;
277 +  UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
278 +  UInt32 nowPos = 0;
279 +  UInt32 posStateMask = (1 << pb) - 1;
280 +  UInt32 literalPosMask = (1 << lp) - 1;
281 +  int len = 0;
282 +  
283 +  Byte *Buffer;
284 +  Byte *BufferLim;
285 +  UInt32 Range;
286 +  UInt32 Code;
287 +  
288 +  if (bufferSize < numProbs * sizeof(CProb))
289 +    return LZMA_RESULT_NOT_ENOUGH_MEM;
290 +  for (i = 0; i < numProbs; i++)
291 +    p[i] = kBitModelTotal >> 1;
292 +  
293 +
294 +  #ifdef _LZMA_IN_CB
295 +  RC_INIT;
296 +  #else
297 +  RC_INIT(inStream, inSize);
298 +  #endif
299 +#endif
300 +
301 +  *outSizeProcessed = 0;
302 +  while(nowPos < outSize)
303 +  {
304 +    CProb *prob;
305 +    UInt32 bound;
306 +    int posState = (int)(
307 +        (nowPos 
308 +        #ifdef _LZMA_OUT_READ
309 +        + globalPos
310 +        #endif
311 +        )
312 +        & posStateMask);
313 +
314 +    prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
315 +    IfBit0(prob)
316 +    {
317 +      int symbol = 1;
318 +      UpdateBit0(prob)
319 +      prob = p + Literal + (LZMA_LIT_SIZE * 
320 +        (((
321 +        (nowPos 
322 +        #ifdef _LZMA_OUT_READ
323 +        + globalPos
324 +        #endif
325 +        )
326 +        & literalPosMask) << lc) + (previousByte >> (8 - lc))));
327 +
328 +      if (state >= kNumLitStates)
329 +      {
330 +        int matchByte;
331 +        #ifdef _LZMA_OUT_READ
332 +        UInt32 pos = dictionaryPos - rep0;
333 +        if (pos >= dictionarySize)
334 +          pos += dictionarySize;
335 +        matchByte = dictionary[pos];
336 +        #else
337 +        matchByte = outStream[nowPos - rep0];
338 +        #endif
339 +        do
340 +        {
341 +          int bit;
342 +          CProb *probLit;
343 +          matchByte <<= 1;
344 +          bit = (matchByte & 0x100);
345 +          probLit = prob + 0x100 + bit + symbol;
346 +          RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
347 +        }
348 +        while (symbol < 0x100);
349 +      }
350 +      while (symbol < 0x100)
351 +      {
352 +        CProb *probLit = prob + symbol;
353 +        RC_GET_BIT(probLit, symbol)
354 +      }
355 +      previousByte = (Byte)symbol;
356 +
357 +      outStream[nowPos++] = previousByte;
358 +      #ifdef _LZMA_OUT_READ
359 +      dictionary[dictionaryPos] = previousByte;
360 +      if (++dictionaryPos == dictionarySize)
361 +        dictionaryPos = 0;
362 +      #endif
363 +      if (state < 4) state = 0;
364 +      else if (state < 10) state -= 3;
365 +      else state -= 6;
366 +    }
367 +    else             
368 +    {
369 +      UpdateBit1(prob);
370 +      prob = p + IsRep + state;
371 +      IfBit0(prob)
372 +      {
373 +        UpdateBit0(prob);
374 +        rep3 = rep2;
375 +        rep2 = rep1;
376 +        rep1 = rep0;
377 +        state = state < kNumLitStates ? 0 : 3;
378 +        prob = p + LenCoder;
379 +      }
380 +      else
381 +      {
382 +        UpdateBit1(prob);
383 +        prob = p + IsRepG0 + state;
384 +        IfBit0(prob)
385 +        {
386 +          UpdateBit0(prob);
387 +          prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
388 +          IfBit0(prob)
389 +          {
390 +            #ifdef _LZMA_OUT_READ
391 +            UInt32 pos;
392 +            #endif
393 +            UpdateBit0(prob);
394 +            if (nowPos 
395 +                #ifdef _LZMA_OUT_READ
396 +                + globalPos
397 +                #endif
398 +                == 0)
399 +              return LZMA_RESULT_DATA_ERROR;
400 +            state = state < kNumLitStates ? 9 : 11;
401 +            #ifdef _LZMA_OUT_READ
402 +            pos = dictionaryPos - rep0;
403 +            if (pos >= dictionarySize)
404 +              pos += dictionarySize;
405 +            previousByte = dictionary[pos];
406 +            dictionary[dictionaryPos] = previousByte;
407 +            if (++dictionaryPos == dictionarySize)
408 +              dictionaryPos = 0;
409 +            #else
410 +            previousByte = outStream[nowPos - rep0];
411 +            #endif
412 +            outStream[nowPos++] = previousByte;
413 +            continue;
414 +          }
415 +          else
416 +          {
417 +            UpdateBit1(prob);
418 +          }
419 +        }
420 +        else
421 +        {
422 +          UInt32 distance;
423 +          UpdateBit1(prob);
424 +          prob = p + IsRepG1 + state;
425 +          IfBit0(prob)
426 +          {
427 +            UpdateBit0(prob);
428 +            distance = rep1;
429 +          }
430 +          else 
431 +          {
432 +            UpdateBit1(prob);
433 +            prob = p + IsRepG2 + state;
434 +            IfBit0(prob)
435 +            {
436 +              UpdateBit0(prob);
437 +              distance = rep2;
438 +            }
439 +            else
440 +            {
441 +              UpdateBit1(prob);
442 +              distance = rep3;
443 +              rep3 = rep2;
444 +            }
445 +            rep2 = rep1;
446 +          }
447 +          rep1 = rep0;
448 +          rep0 = distance;
449 +        }
450 +        state = state < kNumLitStates ? 8 : 11;
451 +        prob = p + RepLenCoder;
452 +      }
453 +      {
454 +        int numBits, offset;
455 +        CProb *probLen = prob + LenChoice;
456 +        IfBit0(probLen)
457 +        {
458 +          UpdateBit0(probLen);
459 +          probLen = prob + LenLow + (posState << kLenNumLowBits);
460 +          offset = 0;
461 +          numBits = kLenNumLowBits;
462 +        }
463 +        else
464 +        {
465 +          UpdateBit1(probLen);
466 +          probLen = prob + LenChoice2;
467 +          IfBit0(probLen)
468 +          {
469 +            UpdateBit0(probLen);
470 +            probLen = prob + LenMid + (posState << kLenNumMidBits);
471 +            offset = kLenNumLowSymbols;
472 +            numBits = kLenNumMidBits;
473 +          }
474 +          else
475 +          {
476 +            UpdateBit1(probLen);
477 +            probLen = prob + LenHigh;
478 +            offset = kLenNumLowSymbols + kLenNumMidSymbols;
479 +            numBits = kLenNumHighBits;
480 +          }
481 +        }
482 +        RangeDecoderBitTreeDecode(probLen, numBits, len);
483 +        len += offset;
484 +      }
485 +
486 +      if (state < 4)
487 +      {
488 +        int posSlot;
489 +        state += kNumLitStates;
490 +        prob = p + PosSlot +
491 +            ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << 
492 +            kNumPosSlotBits);
493 +        RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
494 +        if (posSlot >= kStartPosModelIndex)
495 +        {
496 +          int numDirectBits = ((posSlot >> 1) - 1);
497 +          rep0 = (2 | ((UInt32)posSlot & 1));
498 +          if (posSlot < kEndPosModelIndex)
499 +          {
500 +            rep0 <<= numDirectBits;
501 +            prob = p + SpecPos + rep0 - posSlot - 1;
502 +          }
503 +          else
504 +          {
505 +            numDirectBits -= kNumAlignBits;
506 +            do
507 +            {
508 +              RC_NORMALIZE
509 +              Range >>= 1;
510 +              rep0 <<= 1;
511 +              if (Code >= Range)
512 +              {
513 +                Code -= Range;
514 +                rep0 |= 1;
515 +              }
516 +            }
517 +            while (--numDirectBits != 0);
518 +            prob = p + Align;
519 +            rep0 <<= kNumAlignBits;
520 +            numDirectBits = kNumAlignBits;
521 +          }
522 +          {
523 +            int i = 1;
524 +            int mi = 1;
525 +            do
526 +            {
527 +              CProb *prob3 = prob + mi;
528 +              RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
529 +              i <<= 1;
530 +            }
531 +            while(--numDirectBits != 0);
532 +          }
533 +        }
534 +        else
535 +          rep0 = posSlot;
536 +        if (++rep0 == (UInt32)(0))
537 +        {
538 +          /* it's for stream version */
539 +          len = -1;
540 +          break;
541 +        }
542 +      }
543 +
544 +      len += kMatchMinLen;
545 +      if (rep0 > nowPos 
546 +        #ifdef _LZMA_OUT_READ
547 +        + globalPos || rep0 > dictionarySize
548 +        #endif
549 +        ) 
550 +        return LZMA_RESULT_DATA_ERROR;
551 +      do
552 +      {
553 +        #ifdef _LZMA_OUT_READ
554 +        UInt32 pos = dictionaryPos - rep0;
555 +        if (pos >= dictionarySize)
556 +          pos += dictionarySize;
557 +        previousByte = dictionary[pos];
558 +        dictionary[dictionaryPos] = previousByte;
559 +        if (++dictionaryPos == dictionarySize)
560 +          dictionaryPos = 0;
561 +        #else
562 +        previousByte = outStream[nowPos - rep0];
563 +        #endif
564 +        len--;
565 +        outStream[nowPos++] = previousByte;
566 +      }
567 +      while(len != 0 && nowPos < outSize);
568 +    }
569 +  }
570 +  RC_NORMALIZE;
571 +
572 +  #ifdef _LZMA_OUT_READ
573 +  vs->Buffer = Buffer;
574 +  vs->BufferLim = BufferLim;
575 +  vs->Range = Range;
576 +  vs->Code = Code;
577 +  vs->DictionaryPos = dictionaryPos;
578 +  vs->GlobalPos = globalPos + nowPos;
579 +  vs->Reps[0] = rep0;
580 +  vs->Reps[1] = rep1;
581 +  vs->Reps[2] = rep2;
582 +  vs->Reps[3] = rep3;
583 +  vs->State = state;
584 +  vs->RemainLen = len;
585 +  vs->TempDictionary[0] = tempDictionary[0];
586 +  #endif
587 +
588 +  *outSizeProcessed = nowPos;
589 +  return LZMA_RESULT_OK;
590 +}
591 diff -Nur linux-2.6.21.1/arch/i386/boot/compressed/LzmaDecode.h linux-2.6.21.1-owrt/arch/i386/boot/compressed/LzmaDecode.h
592 --- linux-2.6.21.1/arch/i386/boot/compressed/LzmaDecode.h       1970-01-01 01:00:00.000000000 +0100
593 +++ linux-2.6.21.1-owrt/arch/i386/boot/compressed/LzmaDecode.h  2007-05-14 11:55:38.000000000 +0200
594 @@ -0,0 +1,100 @@
595 +/* 
596 +  LzmaDecode.h
597 +  LZMA Decoder interface
598 +
599 +  LZMA SDK 4.16 Copyright (c) 1999-2005 Igor Pavlov (2005-03-18)
600 +  http://www.7-zip.org/
601 +
602 +  LZMA SDK is licensed under two licenses:
603 +  1) GNU Lesser General Public License (GNU LGPL)
604 +  2) Common Public License (CPL)
605 +  It means that you can select one of these two licenses and 
606 +  follow rules of that license.
607 +
608 +  SPECIAL EXCEPTION:
609 +  Igor Pavlov, as the author of this code, expressly permits you to 
610 +  statically or dynamically link your code (or bind by name) to the 
611 +  interfaces of this file without subjecting your linked code to the 
612 +  terms of the CPL or GNU LGPL. Any modifications or additions 
613 +  to this file, however, are subject to the LGPL or CPL terms.
614 +*/
615 +
616 +#ifndef __LZMADECODE_H
617 +#define __LZMADECODE_H
618 +
619 +/* #define _LZMA_IN_CB */
620 +/* Use callback for input data */
621 +
622 +/* #define _LZMA_OUT_READ */
623 +/* Use read function for output data */
624 +
625 +/* #define _LZMA_PROB32 */
626 +/* It can increase speed on some 32-bit CPUs, 
627 +   but memory usage will be doubled in that case */
628 +
629 +/* #define _LZMA_LOC_OPT */
630 +/* Enable local speed optimizations inside code */
631 +
632 +#ifndef UInt32
633 +#ifdef _LZMA_UINT32_IS_ULONG
634 +#define UInt32 unsigned long
635 +#else
636 +#define UInt32 unsigned int
637 +#endif
638 +#endif
639 +
640 +#ifdef _LZMA_PROB32
641 +#define CProb UInt32
642 +#else
643 +#define CProb unsigned short
644 +#endif
645 +
646 +#define LZMA_RESULT_OK 0
647 +#define LZMA_RESULT_DATA_ERROR 1
648 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
649 +
650 +#ifdef _LZMA_IN_CB
651 +typedef struct _ILzmaInCallback
652 +{
653 +  int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
654 +} ILzmaInCallback;
655 +#endif
656 +
657 +#define LZMA_BASE_SIZE 1846
658 +#define LZMA_LIT_SIZE 768
659 +
660 +/* 
661 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
662 +bufferSize += 100 in case of _LZMA_OUT_READ
663 +by default CProb is unsigned short, 
664 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
665 +*/
666 +
667 +#ifdef _LZMA_OUT_READ
668 +int LzmaDecoderInit(
669 +    unsigned char *buffer, UInt32 bufferSize,
670 +    int lc, int lp, int pb,
671 +    unsigned char *dictionary, UInt32 dictionarySize,
672 +  #ifdef _LZMA_IN_CB
673 +    ILzmaInCallback *inCallback
674 +  #else
675 +    unsigned char *inStream, UInt32 inSize
676 +  #endif
677 +);
678 +#endif
679 +
680 +int LzmaDecode(
681 +    unsigned char *buffer, 
682 +  #ifndef _LZMA_OUT_READ
683 +    UInt32 bufferSize,
684 +    int lc, int lp, int pb,
685 +  #ifdef _LZMA_IN_CB
686 +    ILzmaInCallback *inCallback,
687 +  #else
688 +    unsigned char *inStream, UInt32 inSize,
689 +  #endif
690 +  #endif
691 +    unsigned char *outStream, UInt32 outSize,
692 +    UInt32 *outSizeProcessed);
693 +
694 +#endif
695 diff -Nur linux-2.6.21.1/arch/i386/boot/compressed/lzma_misc.c linux-2.6.21.1-owrt/arch/i386/boot/compressed/lzma_misc.c
696 --- linux-2.6.21.1/arch/i386/boot/compressed/lzma_misc.c        1970-01-01 01:00:00.000000000 +0100
697 +++ linux-2.6.21.1-owrt/arch/i386/boot/compressed/lzma_misc.c   2007-05-14 11:55:38.000000000 +0200
698 @@ -0,0 +1,412 @@
699 +/*
700 + * lzma_misc.c
701 + * 
702 + * Decompress LZMA compressed vmlinuz 
703 + * Version 0.9 Copyright (c) Ming-Ching Tiew mctiew@yahoo.com
704 + * Program adapted from misc.c for 2.6 kernel
705 + * Date: 3 June 2005 
706 + * Source released under GPL
707 + */
708 +
709 +#include <linux/linkage.h>
710 +#include <linux/vmalloc.h>
711 +#include <linux/tty.h>
712 +#include <linux/screen_info.h>
713 +#include <asm/io.h>
714 +
715 +#define OF(args)  args
716 +#define STATIC static
717 +
718 +#undef memset
719 +#undef memcpy
720 +
721 +/*
722 + * Why do we do this? Don't ask me..
723 + *
724 + * Incomprehensible are the ways of bootloaders.
725 + */
726 +static void* memcpy(void *, __const void *, size_t);
727 +
728 +typedef unsigned char  uch;
729 +typedef unsigned short ush;
730 +typedef unsigned long  ulg;
731 +
732 +#define WSIZE 0x8000           /* Window size must be at least 32k, */
733 +                               /* and a power of two */
734 +
735 +static uch *inbuf;          /* input buffer */
736 +
737 +static unsigned insize = 0;  /* valid bytes in inbuf */
738 +static unsigned inptr = 0;   /* index of next byte to be processed in inbuf */
739 +
740 +#define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
741 +               
742 +/* Diagnostic functions */
743 +#ifdef DEBUG
744 +#  define Assert(cond,msg) {if(!(cond)) error(msg);}
745 +#  define Trace(x) fprintf x
746 +#  define Tracev(x) {if (verbose) fprintf x ;}
747 +#  define Tracevv(x) {if (verbose>1) fprintf x ;}
748 +#  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
749 +#  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
750 +#else
751 +#  define Assert(cond,msg)
752 +#  define Trace(x)
753 +#  define Tracev(x)
754 +#  define Tracevv(x)
755 +#  define Tracec(c,x)
756 +#  define Tracecv(c,x)
757 +#endif
758 +
759 +static int  fill_inbuf(void);
760 +static void error(char *m);
761 +  
762 +/*
763 + * This is set up by the setup-routine at boot-time
764 + */
765 +static unsigned char *real_mode; /* Pointer to real-mode data */
766 +
767 +#define RM_EXT_MEM_K   (*(unsigned short *)(real_mode + 0x2))
768 +#ifndef STANDARD_MEMORY_BIOS_CALL
769 +#define RM_ALT_MEM_K   (*(unsigned long *)(real_mode + 0x1e0))
770 +#endif
771 +#define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
772 +
773 +extern char input_data[];
774 +extern int input_len;
775 +
776 +static long bytes_out = 0;
777 +static uch *output_data;
778 +
779 +static void putstr(const char *);
780 +
781 +extern int _end;
782 +static long free_mem_ptr = (long)&_end;
783 +static long free_mem_end_ptr;
784 +
785 +#define INPLACE_MOVE_ROUTINE  0x1000
786 +#define LOW_BUFFER_START      0x2000
787 +#define LOW_BUFFER_MAX       0x90000
788 +#define HEAP_SIZE             0x3000
789 +static unsigned int low_buffer_end, low_buffer_size;
790 +static int high_loaded =0;
791 +static uch *high_buffer_start /* = (uch *)(((ulg)&_end) + HEAP_SIZE)*/;
792 +
793 +static char *vidmem = (char *)0xb8000;
794 +static int vidport;
795 +static int lines, cols;
796 +
797 +static void scroll(void)
798 +{
799 +       int i;
800 +
801 +       memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
802 +       for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
803 +               vidmem[i] = ' ';
804 +}
805 +
806 +static void putstr(const char *s)
807 +{
808 +       int x,y,pos;
809 +       char c;
810 +
811 +       x = RM_SCREEN_INFO.orig_x;
812 +       y = RM_SCREEN_INFO.orig_y;
813 +
814 +       while ( ( c = *s++ ) != '\0' ) {
815 +               if ( c == '\n' ) {
816 +                       x = 0;
817 +                       if ( ++y >= lines ) {
818 +                               scroll();
819 +                               y--;
820 +                       }
821 +               } else {
822 +                       vidmem [ ( x + cols * y ) * 2 ] = c; 
823 +                       if ( ++x >= cols ) {
824 +                               x = 0;
825 +                               if ( ++y >= lines ) {
826 +                                       scroll();
827 +                                       y--;
828 +                               }
829 +                       }
830 +               }
831 +       }
832 +
833 +       RM_SCREEN_INFO.orig_x = x;
834 +       RM_SCREEN_INFO.orig_y = y;
835 +
836 +       pos = (x + cols * y) * 2;       /* Update cursor position */
837 +       outb_p(14, vidport);
838 +       outb_p(0xff & (pos >> 9), vidport+1);
839 +       outb_p(15, vidport);
840 +       outb_p(0xff & (pos >> 1), vidport+1);
841 +}
842 +
843 +static void* memcpy(void* __dest, __const void* __src,
844 +                           size_t __n)
845 +{
846 +       int i;
847 +       char *d = (char *)__dest, *s = (char *)__src;
848 +
849 +       for (i=0;i<__n;i++) d[i] = s[i];
850 +       return __dest;
851 +}
852 +
853 +/* ===========================================================================
854 + * Fill the input buffer. This is called only when the buffer is empty
855 + * and at least one byte is really needed.
856 + */
857 +static int fill_inbuf(void)
858 +{
859 +       if (insize != 0) {
860 +               error("ran out of input data");
861 +       }
862 +
863 +       inbuf = input_data;
864 +       insize = input_len;
865 +       inptr = 1;
866 +       return inbuf[0];
867 +}
868 +
869 +static void error(char *x)
870 +{
871 +       putstr("\n\n");
872 +       putstr(x);
873 +       putstr("\n\n -- System halted");
874 +
875 +       while(1);       /* Halt */
876 +}
877 +
878 +#define STACK_SIZE (4096)
879 +
880 +long user_stack [STACK_SIZE];
881 +
882 +struct {
883 +       long * a;
884 +       short b;
885 +       } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
886 +
887 +static void setup_normal_output_buffer(void)
888 +{
889 +#ifdef STANDARD_MEMORY_BIOS_CALL
890 +       if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
891 +#else
892 +       if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < 1024) error("Less than 2MB of memory");
893 +#endif
894 +       output_data = (char *)0x100000; /* Points to 1M */
895 +       free_mem_end_ptr = (long)real_mode;
896 +}
897 +
898 +struct moveparams {
899 +       uch *low_buffer_start;  int lcount;
900 +       uch *high_buffer_start; int hcount;
901 +};
902 +
903 +static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
904 +{
905 +       high_buffer_start = (uch *)(((ulg)&_end) + HEAP_SIZE);
906 +#ifdef STANDARD_MEMORY_BIOS_CALL
907 +       if (RM_EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
908 +#else
909 +       if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) <
910 +                       (3*1024))
911 +               error("Less than 4MB of memory");
912 +#endif 
913 +       mv->low_buffer_start = output_data = (char *)LOW_BUFFER_START;
914 +       low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
915 +         ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
916 +       low_buffer_size = low_buffer_end - LOW_BUFFER_START;
917 +       high_loaded = 1;
918 +       free_mem_end_ptr = (long)high_buffer_start;
919 +       if ( (0x100000 + low_buffer_size) > ((ulg)high_buffer_start)) {
920 +               high_buffer_start = (uch *)(0x100000 + low_buffer_size);
921 +               mv->hcount = 0; /* say: we need not to move high_buffer */
922 +       }
923 +       else mv->hcount = -1;
924 +       mv->high_buffer_start = high_buffer_start;
925 +}
926 +
927 +static void close_output_buffer_if_we_run_high(struct moveparams *mv)
928 +{
929 +       if (bytes_out > low_buffer_size) {
930 +               mv->lcount = low_buffer_size;
931 +               if (mv->hcount)
932 +                       mv->hcount = bytes_out - low_buffer_size;
933 +       } else {
934 +               mv->lcount = bytes_out;
935 +               mv->hcount = 0;
936 +       }
937 +}
938 +
939 +// When using LZMA in callback, the compressed length is not needed.
940 +// Otherwise you need a special version of lzma compression program
941 +// which will pad the compressed length in the header.
942 +#define _LZMA_IN_CB
943 +#include "LzmaDecode.h"
944 +#include "LzmaDecode.c"
945 +
946 +#ifdef  _LZMA_IN_CB
947 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize);
948 +#endif
949 +
950 +
951 +/*
952 + * Do the lzma decompression
953 + * When using LZMA in callback, the end of input stream is automatically determined
954 + */
955 +static int lzma_unzip(void)
956 +{
957 +
958 +       unsigned int i;  /* temp value */
959 +        unsigned int lc; /* literal context bits */
960 +        unsigned int lp; /* literal pos state bits */
961 +        unsigned int pb; /* pos state bits */
962 +       unsigned char* workspace;
963 +       unsigned int uncompressedSize = 0;
964 +       unsigned char* p;
965 +        
966 +#ifdef  _LZMA_IN_CB
967 +        ILzmaInCallback callback;
968 +        callback.Read = read_byte;
969 +#else  
970 +       unsigned char* inputbuf;
971 +       unsigned int lzma_workspace_size;
972 +       unsigned int compressedSize = 0;
973 +#endif
974 +
975 +       /* lzma args */
976 +       i = get_byte();
977 +       lc = i % 9, i = i / 9;
978 +        lp = i % 5, pb = i / 5;
979 +        
980 +        /* skip dictionary size */
981 +        for (i = 0; i < 4; i++) 
982 +               get_byte();
983 +        // get uncompressedSize        
984 +        p= (char*)&uncompressedSize;   
985 +        for (i = 0; i < 4; i++) 
986 +            *p++ = get_byte();
987 +            
988 +        //get compressedSize 
989 +#ifdef  _LZMA_IN_CB
990 +        for (i = 0; i < 4; i++) 
991 +               get_byte();
992 +#else
993 +        p= (char*)&compressedSize;     
994 +        for (i = 0; i < 4; i++) 
995 +            *p++ = get_byte();
996 +#endif
997 +        
998 +#if 0 
999 +        if ( (lc == 5 ) && (lp == 0 ) && ( pb == 0 ))
1000 +        {
1001 +               putstr("got prop!\n");
1002 +        }
1003 +        
1004 +#ifndef  _LZMA_IN_CB
1005 +        if( compressedSize == 496722 )
1006 +        {
1007 +           putstr( "got the right sizes\n");
1008 +        }
1009 +        else if ( compressedSize > 496722 )
1010 +        {
1011 +           putstr( "greater !\n");
1012 +        }
1013 +        else if ( compressedSize < 496722 )
1014 +           putstr( "smaller!\n");
1015 +       
1016 +#endif
1017 +        if ( uncompressedSize == 1187168 )
1018 +        {
1019 +           putstr( "got the right uncompressed size \n");
1020 +        }else if ( uncompressedSize > 1187168 )
1021 +        {
1022 +           putstr( "uncompressedSize greater!\n");
1023 +        }else
1024 +           putstr( "uncompressedSize smaller!|n");
1025 +#endif   
1026 +
1027 +        // point it beyond uncompresedSize
1028 +        workspace = high_buffer_start + uncompressedSize;
1029 +        //
1030 +#ifndef _LZMA_IN_CB
1031 +        lzma_workspace_size =  (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp))) * sizeof(CProb);
1032 +        inputbuf = high_buffer_start + uncompressedSize + lzma_workspace_size;
1033 +        // read the compressed data
1034 +        for( i=0; i < compressedSize; i++)
1035 +        { 
1036 +            if ( i % ( 1024 * 10 ) == 0 )
1037 +               putstr(".");
1038 +            *inputbuf++ = get_byte();
1039 +        }
1040 +#endif 
1041 +        
1042 +       /* decompress kernel */
1043 +#ifdef  _LZMA_IN_CB
1044 +       if (LzmaDecode(workspace, ~0, lc, lp, pb, 
1045 +          &callback, 
1046 +#else
1047 +       if (LzmaDecode(workspace, lzma_workspace_size, lc, lp, pb, 
1048 +          inputbuf - compressedSize, compressedSize,
1049 +#endif
1050 +          (unsigned char*)high_buffer_start, uncompressedSize, &i) == LZMA_RESULT_OK)
1051 +       {
1052 +               if ( i != uncompressedSize )
1053 +                  error( "kernel corrupted!\n");
1054 +               //copy it back to low_buffer
1055 +               if( uncompressedSize > low_buffer_size )
1056 +               {
1057 +                   memcpy((char*)LOW_BUFFER_START, high_buffer_start, low_buffer_size);
1058 +                   memcpy(high_buffer_start, high_buffer_start+low_buffer_size, 
1059 +                       uncompressedSize-low_buffer_size);      
1060 +               }
1061 +               else            
1062 +                   memcpy((char*)LOW_BUFFER_START, high_buffer_start, uncompressedSize );
1063 +               bytes_out = i;
1064 +               return 0;
1065 +       }
1066 +       return 1;
1067 +}
1068 +
1069 +
1070 +#ifdef  _LZMA_IN_CB
1071 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
1072 +{
1073 +       static unsigned int i = 0;
1074 +       static unsigned char val;
1075 +       *bufferSize = 1;
1076 +       val = get_byte();
1077 +       *buffer = &val;
1078 +        if ( i++ % ( 1024 * 50 ) == 0 )
1079 +               putstr(".");
1080 +       return LZMA_RESULT_OK;
1081 +}      
1082 +#endif
1083 +
1084 +asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
1085 +{
1086 +       real_mode = rmode;
1087 +
1088 +       if (RM_SCREEN_INFO.orig_video_mode == 7) {
1089 +               vidmem = (char *) 0xb0000;
1090 +               vidport = 0x3b4;
1091 +       } else {
1092 +               vidmem = (char *) 0xb8000;
1093 +               vidport = 0x3d4;
1094 +       }
1095 +
1096 +       lines = RM_SCREEN_INFO.orig_video_lines;
1097 +       cols = RM_SCREEN_INFO.orig_video_cols;
1098 +
1099 +       if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
1100 +       else setup_output_buffer_if_we_run_high(mv);
1101 +
1102 +       putstr("LZMA vmlinuz: Ming-Ching Tiew <mctiew@yahoo.com> ...");
1103 +       if( lzma_unzip() != 0 )
1104 +       {
1105 +          error("inflate error\n");
1106 +       }
1107 +       putstr("Ok, booting the kernel.\n");
1108 +       if (high_loaded) close_output_buffer_if_we_run_high(mv);
1109 +       return high_loaded;
1110 +}
1111 diff -Nur linux-2.6.21.1/arch/i386/boot/compressed/Makefile linux-2.6.21.1-owrt/arch/i386/boot/compressed/Makefile
1112 --- linux-2.6.21.1/arch/i386/boot/compressed/Makefile   2007-04-27 23:49:26.000000000 +0200
1113 +++ linux-2.6.21.1-owrt/arch/i386/boot/compressed/Makefile      2007-05-14 12:01:25.000000000 +0200
1114 @@ -4,7 +4,7 @@
1115  # create a compressed vmlinux image from the original vmlinux
1116  #
1117  
1118 -targets                := vmlinux vmlinux.bin vmlinux.bin.gz head.o misc.o piggy.o \
1119 +targets                := vmlinux vmlinux.bin vmlinux.bin.lzma head.o lzma_misc.o piggy.o \
1120                         vmlinux.bin.all vmlinux.relocs
1121  EXTRA_AFLAGS   := -traditional
1122  
1123 @@ -12,7 +12,7 @@
1124  CFLAGS_misc.o += -fPIC
1125  hostprogs-y    := relocs
1126  
1127 -$(obj)/vmlinux: $(src)/vmlinux.lds $(obj)/head.o $(obj)/misc.o $(obj)/piggy.o FORCE
1128 +$(obj)/vmlinux: $(src)/vmlinux.lds $(obj)/head.o $(obj)/lzma_misc.o $(obj)/piggy.o FORCE
1129         $(call if_changed,ld)
1130         @:
1131  
1132 @@ -32,14 +32,14 @@
1133         $(call if_changed,relocbin)
1134  
1135  ifdef CONFIG_RELOCATABLE
1136 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
1137 -       $(call if_changed,gzip)
1138 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
1139 +       $(call if_changed,lzma)
1140  else
1141 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
1142 -       $(call if_changed,gzip)
1143 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
1144 +       $(call if_changed,lzma)
1145  endif
1146  
1147  LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
1148  
1149 -$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
1150 +$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.lzma FORCE
1151         $(call if_changed,ld)
1152 diff -Nur linux-2.6.21.1/scripts/Makefile.lib linux-2.6.21.1-owrt/scripts/Makefile.lib
1153 --- linux-2.6.21.1/scripts/Makefile.lib 2007-04-27 23:49:26.000000000 +0200
1154 +++ linux-2.6.21.1-owrt/scripts/Makefile.lib    2007-05-14 11:55:38.000000000 +0200
1155 @@ -162,4 +162,9 @@
1156  quiet_cmd_gzip = GZIP    $@
1157  cmd_gzip = gzip -f -9 < $< > $@
1158  
1159 -
1160 +# LZMA
1161 +#
1162 +quiet_cmd_lzma = LZMA $@
1163 +cmd_lzma = lzma e $< $@ -lc7 -lp0 -pb0
1164 +# to use lzmacomp,
1165 +# cmd_lzma = lzmacomp $< 700 > $@