move the lzma decompressor out of squashfs-lzma patch and into a new patch (i want...
[openwrt.git] / target / linux / generic-2.6 / patches / 600-x86_lzma.patch
1 diff -Naur linux-old/arch/i386/boot/compressed/LzmaDecode.c linux-lzma/arch/i386/boot/compressed/LzmaDecode.c
2 --- linux-old/arch/i386/boot/compressed/LzmaDecode.c    1969-12-31 19:00:00.000000000 -0500
3 +++ linux-lzma/arch/i386/boot/compressed/LzmaDecode.c   2005-06-05 00:07:38.000000000 -0400
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 -Naur linux-old/arch/i386/boot/compressed/LzmaDecode.h linux-lzma/arch/i386/boot/compressed/LzmaDecode.h
592 --- linux-old/arch/i386/boot/compressed/LzmaDecode.h    1969-12-31 19:00:00.000000000 -0500
593 +++ linux-lzma/arch/i386/boot/compressed/LzmaDecode.h   2005-06-05 00:07:39.000000000 -0400
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 -Naur linux-old/arch/i386/boot/compressed/Makefile linux-lzma/arch/i386/boot/compressed/Makefile
696 --- linux-old/arch/i386/boot/compressed/Makefile        2005-06-04 21:53:40.000000000 -0400
697 +++ linux-lzma/arch/i386/boot/compressed/Makefile       2005-06-05 00:25:23.000000000 -0400
698 @@ -2,24 +2,33 @@
699  # linux/arch/i386/boot/compressed/Makefile
700  #
701  # create a compressed vmlinux image from the original vmlinux
702 +# patched by Ming-Ching Tiew <mctiew@yahoo.com> for kernel 2.6
703 +# requires program 'lzma' from LZMA SDK ( http://www.7-zip.org/ ) to work
704 +#    $ mkdir lzma
705 +#    $ cd lzma
706 +#    $ tar tvjf ../lzma417.tar.bz2
707 +#    $ cd SRC/7zip/Compress/LZMA_Alone
708 +#    $ dos2unix makefile
709 +#    $ make
710 +#    $ su
711 +#    # cp lzma /usr/bin
712  #
713 -
714 -targets                := vmlinux vmlinux.bin vmlinux.bin.gz head.o misc.o piggy.o
715 +targets                := vmlinux vmlinux.bin vmlinux.bin.lzma head.o lzma_misc.o piggy.o
716  EXTRA_AFLAGS   := -traditional
717  
718  LDFLAGS_vmlinux := -Ttext $(IMAGE_OFFSET) -e startup_32
719  
720 -$(obj)/vmlinux: $(obj)/head.o $(obj)/misc.o $(obj)/piggy.o FORCE
721 +$(obj)/vmlinux: $(obj)/head.o $(obj)/lzma_misc.o $(obj)/piggy.o FORCE
722         $(call if_changed,ld)
723         @:
724  
725  $(obj)/vmlinux.bin: vmlinux FORCE
726         $(call if_changed,objcopy)
727  
728 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
729 -       $(call if_changed,gzip)
730 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
731 +       $(call if_changed,lzma)
732  
733  LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
734  
735 -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
736 +$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.lzma FORCE
737         $(call if_changed,ld)
738 diff -Naur linux-old/arch/i386/boot/compressed/lzma_misc.c linux-lzma/arch/i386/boot/compressed/lzma_misc.c
739 --- linux-old/arch/i386/boot/compressed/lzma_misc.c     1969-12-31 19:00:00.000000000 -0500
740 +++ linux-lzma/arch/i386/boot/compressed/lzma_misc.c    2005-06-04 21:33:48.000000000 -0400
741 @@ -0,0 +1,412 @@
742 +/*
743 + * lzma_misc.c
744 + * 
745 + * Decompress LZMA compressed vmlinuz 
746 + * Version 0.9 Copyright (c) Ming-Ching Tiew mctiew@yahoo.com
747 + * Program adapted from misc.c for 2.6 kernel
748 + * Date: 3 June 2005 
749 + * Source released under GPL
750 + */
751 +
752 +#include <linux/linkage.h>
753 +#include <linux/vmalloc.h>
754 +#include <linux/tty.h>
755 +#include <linux/screen_info.h>
756 +#include <asm/io.h>
757 +
758 +#define OF(args)  args
759 +#define STATIC static
760 +
761 +#undef memset
762 +#undef memcpy
763 +
764 +/*
765 + * Why do we do this? Don't ask me..
766 + *
767 + * Incomprehensible are the ways of bootloaders.
768 + */
769 +static void* memcpy(void *, __const void *, size_t);
770 +
771 +typedef unsigned char  uch;
772 +typedef unsigned short ush;
773 +typedef unsigned long  ulg;
774 +
775 +#define WSIZE 0x8000           /* Window size must be at least 32k, */
776 +                               /* and a power of two */
777 +
778 +static uch *inbuf;          /* input buffer */
779 +
780 +static unsigned insize = 0;  /* valid bytes in inbuf */
781 +static unsigned inptr = 0;   /* index of next byte to be processed in inbuf */
782 +
783 +#define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
784 +               
785 +/* Diagnostic functions */
786 +#ifdef DEBUG
787 +#  define Assert(cond,msg) {if(!(cond)) error(msg);}
788 +#  define Trace(x) fprintf x
789 +#  define Tracev(x) {if (verbose) fprintf x ;}
790 +#  define Tracevv(x) {if (verbose>1) fprintf x ;}
791 +#  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
792 +#  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
793 +#else
794 +#  define Assert(cond,msg)
795 +#  define Trace(x)
796 +#  define Tracev(x)
797 +#  define Tracevv(x)
798 +#  define Tracec(c,x)
799 +#  define Tracecv(c,x)
800 +#endif
801 +
802 +static int  fill_inbuf(void);
803 +static void error(char *m);
804 +  
805 +/*
806 + * This is set up by the setup-routine at boot-time
807 + */
808 +static unsigned char *real_mode; /* Pointer to real-mode data */
809 +
810 +#define RM_EXT_MEM_K   (*(unsigned short *)(real_mode + 0x2))
811 +#ifndef STANDARD_MEMORY_BIOS_CALL
812 +#define RM_ALT_MEM_K   (*(unsigned long *)(real_mode + 0x1e0))
813 +#endif
814 +#define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0))
815 +
816 +extern char input_data[];
817 +extern int input_len;
818 +
819 +static long bytes_out = 0;
820 +static uch *output_data;
821 +
822 +static void putstr(const char *);
823 +
824 +extern int end;
825 +static long free_mem_ptr = (long)&end;
826 +static long free_mem_end_ptr;
827 +
828 +#define INPLACE_MOVE_ROUTINE  0x1000
829 +#define LOW_BUFFER_START      0x2000
830 +#define LOW_BUFFER_MAX       0x90000
831 +#define HEAP_SIZE             0x3000
832 +static unsigned int low_buffer_end, low_buffer_size;
833 +static int high_loaded =0;
834 +static uch *high_buffer_start /* = (uch *)(((ulg)&end) + HEAP_SIZE)*/;
835 +
836 +static char *vidmem = (char *)0xb8000;
837 +static int vidport;
838 +static int lines, cols;
839 +
840 +static void scroll(void)
841 +{
842 +       int i;
843 +
844 +       memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
845 +       for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
846 +               vidmem[i] = ' ';
847 +}
848 +
849 +static void putstr(const char *s)
850 +{
851 +       int x,y,pos;
852 +       char c;
853 +
854 +       x = RM_SCREEN_INFO.orig_x;
855 +       y = RM_SCREEN_INFO.orig_y;
856 +
857 +       while ( ( c = *s++ ) != '\0' ) {
858 +               if ( c == '\n' ) {
859 +                       x = 0;
860 +                       if ( ++y >= lines ) {
861 +                               scroll();
862 +                               y--;
863 +                       }
864 +               } else {
865 +                       vidmem [ ( x + cols * y ) * 2 ] = c; 
866 +                       if ( ++x >= cols ) {
867 +                               x = 0;
868 +                               if ( ++y >= lines ) {
869 +                                       scroll();
870 +                                       y--;
871 +                               }
872 +                       }
873 +               }
874 +       }
875 +
876 +       RM_SCREEN_INFO.orig_x = x;
877 +       RM_SCREEN_INFO.orig_y = y;
878 +
879 +       pos = (x + cols * y) * 2;       /* Update cursor position */
880 +       outb_p(14, vidport);
881 +       outb_p(0xff & (pos >> 9), vidport+1);
882 +       outb_p(15, vidport);
883 +       outb_p(0xff & (pos >> 1), vidport+1);
884 +}
885 +
886 +static void* memcpy(void* __dest, __const void* __src,
887 +                           size_t __n)
888 +{
889 +       int i;
890 +       char *d = (char *)__dest, *s = (char *)__src;
891 +
892 +       for (i=0;i<__n;i++) d[i] = s[i];
893 +       return __dest;
894 +}
895 +
896 +/* ===========================================================================
897 + * Fill the input buffer. This is called only when the buffer is empty
898 + * and at least one byte is really needed.
899 + */
900 +static int fill_inbuf(void)
901 +{
902 +       if (insize != 0) {
903 +               error("ran out of input data");
904 +       }
905 +
906 +       inbuf = input_data;
907 +       insize = input_len;
908 +       inptr = 1;
909 +       return inbuf[0];
910 +}
911 +
912 +static void error(char *x)
913 +{
914 +       putstr("\n\n");
915 +       putstr(x);
916 +       putstr("\n\n -- System halted");
917 +
918 +       while(1);       /* Halt */
919 +}
920 +
921 +#define STACK_SIZE (4096)
922 +
923 +long user_stack [STACK_SIZE];
924 +
925 +struct {
926 +       long * a;
927 +       short b;
928 +       } stack_start = { & user_stack [STACK_SIZE] , __BOOT_DS };
929 +
930 +static void setup_normal_output_buffer(void)
931 +{
932 +#ifdef STANDARD_MEMORY_BIOS_CALL
933 +       if (RM_EXT_MEM_K < 1024) error("Less than 2MB of memory");
934 +#else
935 +       if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) < 1024) error("Less than 2MB of memory");
936 +#endif
937 +       output_data = (char *)0x100000; /* Points to 1M */
938 +       free_mem_end_ptr = (long)real_mode;
939 +}
940 +
941 +struct moveparams {
942 +       uch *low_buffer_start;  int lcount;
943 +       uch *high_buffer_start; int hcount;
944 +};
945 +
946 +static void setup_output_buffer_if_we_run_high(struct moveparams *mv)
947 +{
948 +       high_buffer_start = (uch *)(((ulg)&end) + HEAP_SIZE);
949 +#ifdef STANDARD_MEMORY_BIOS_CALL
950 +       if (RM_EXT_MEM_K < (3*1024)) error("Less than 4MB of memory");
951 +#else
952 +       if ((RM_ALT_MEM_K > RM_EXT_MEM_K ? RM_ALT_MEM_K : RM_EXT_MEM_K) <
953 +                       (3*1024))
954 +               error("Less than 4MB of memory");
955 +#endif 
956 +       mv->low_buffer_start = output_data = (char *)LOW_BUFFER_START;
957 +       low_buffer_end = ((unsigned int)real_mode > LOW_BUFFER_MAX
958 +         ? LOW_BUFFER_MAX : (unsigned int)real_mode) & ~0xfff;
959 +       low_buffer_size = low_buffer_end - LOW_BUFFER_START;
960 +       high_loaded = 1;
961 +       free_mem_end_ptr = (long)high_buffer_start;
962 +       if ( (0x100000 + low_buffer_size) > ((ulg)high_buffer_start)) {
963 +               high_buffer_start = (uch *)(0x100000 + low_buffer_size);
964 +               mv->hcount = 0; /* say: we need not to move high_buffer */
965 +       }
966 +       else mv->hcount = -1;
967 +       mv->high_buffer_start = high_buffer_start;
968 +}
969 +
970 +static void close_output_buffer_if_we_run_high(struct moveparams *mv)
971 +{
972 +       if (bytes_out > low_buffer_size) {
973 +               mv->lcount = low_buffer_size;
974 +               if (mv->hcount)
975 +                       mv->hcount = bytes_out - low_buffer_size;
976 +       } else {
977 +               mv->lcount = bytes_out;
978 +               mv->hcount = 0;
979 +       }
980 +}
981 +
982 +// When using LZMA in callback, the compressed length is not needed.
983 +// Otherwise you need a special version of lzma compression program
984 +// which will pad the compressed length in the header.
985 +#define _LZMA_IN_CB
986 +#include "LzmaDecode.h"
987 +#include "LzmaDecode.c"
988 +
989 +#ifdef  _LZMA_IN_CB
990 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize);
991 +#endif
992 +
993 +
994 +/*
995 + * Do the lzma decompression
996 + * When using LZMA in callback, the end of input stream is automatically determined
997 + */
998 +static int lzma_unzip(void)
999 +{
1000 +
1001 +       unsigned int i;  /* temp value */
1002 +        unsigned int lc; /* literal context bits */
1003 +        unsigned int lp; /* literal pos state bits */
1004 +        unsigned int pb; /* pos state bits */
1005 +       unsigned char* workspace;
1006 +       unsigned int uncompressedSize = 0;
1007 +       unsigned char* p;
1008 +        
1009 +#ifdef  _LZMA_IN_CB
1010 +        ILzmaInCallback callback;
1011 +        callback.Read = read_byte;
1012 +#else  
1013 +       unsigned char* inputbuf;
1014 +       unsigned int lzma_workspace_size;
1015 +       unsigned int compressedSize = 0;
1016 +#endif
1017 +
1018 +       /* lzma args */
1019 +       i = get_byte();
1020 +       lc = i % 9, i = i / 9;
1021 +        lp = i % 5, pb = i / 5;
1022 +        
1023 +        /* skip dictionary size */
1024 +        for (i = 0; i < 4; i++) 
1025 +               get_byte();
1026 +        // get uncompressedSize        
1027 +        p= (char*)&uncompressedSize;   
1028 +        for (i = 0; i < 4; i++) 
1029 +            *p++ = get_byte();
1030 +            
1031 +        //get compressedSize 
1032 +#ifdef  _LZMA_IN_CB
1033 +        for (i = 0; i < 4; i++) 
1034 +               get_byte();
1035 +#else
1036 +        p= (char*)&compressedSize;     
1037 +        for (i = 0; i < 4; i++) 
1038 +            *p++ = get_byte();
1039 +#endif
1040 +        
1041 +#if 0 
1042 +        if ( (lc == 5 ) && (lp == 0 ) && ( pb == 0 ))
1043 +        {
1044 +               putstr("got prop!\n");
1045 +        }
1046 +        
1047 +#ifndef  _LZMA_IN_CB
1048 +        if( compressedSize == 496722 )
1049 +        {
1050 +           putstr( "got the right sizes\n");
1051 +        }
1052 +        else if ( compressedSize > 496722 )
1053 +        {
1054 +           putstr( "greater !\n");
1055 +        }
1056 +        else if ( compressedSize < 496722 )
1057 +           putstr( "smaller!\n");
1058 +       
1059 +#endif
1060 +        if ( uncompressedSize == 1187168 )
1061 +        {
1062 +           putstr( "got the right uncompressed size \n");
1063 +        }else if ( uncompressedSize > 1187168 )
1064 +        {
1065 +           putstr( "uncompressedSize greater!\n");
1066 +        }else
1067 +           putstr( "uncompressedSize smaller!|n");
1068 +#endif   
1069 +
1070 +        // point it beyond uncompresedSize
1071 +        workspace = high_buffer_start + uncompressedSize;
1072 +        //
1073 +#ifndef _LZMA_IN_CB
1074 +        lzma_workspace_size =  (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp))) * sizeof(CProb);
1075 +        inputbuf = high_buffer_start + uncompressedSize + lzma_workspace_size;
1076 +        // read the compressed data
1077 +        for( i=0; i < compressedSize; i++)
1078 +        { 
1079 +            if ( i % ( 1024 * 10 ) == 0 )
1080 +               putstr(".");
1081 +            *inputbuf++ = get_byte();
1082 +        }
1083 +#endif 
1084 +        
1085 +       /* decompress kernel */
1086 +#ifdef  _LZMA_IN_CB
1087 +       if (LzmaDecode(workspace, ~0, lc, lp, pb, 
1088 +          &callback, 
1089 +#else
1090 +       if (LzmaDecode(workspace, lzma_workspace_size, lc, lp, pb, 
1091 +          inputbuf - compressedSize, compressedSize,
1092 +#endif
1093 +          (unsigned char*)high_buffer_start, uncompressedSize, &i) == LZMA_RESULT_OK)
1094 +       {
1095 +               if ( i != uncompressedSize )
1096 +                  error( "kernel corrupted!\n");
1097 +               //copy it back to low_buffer
1098 +               if( uncompressedSize > low_buffer_size )
1099 +               {
1100 +                   memcpy((char*)LOW_BUFFER_START, high_buffer_start, low_buffer_size);
1101 +                   memcpy(high_buffer_start, high_buffer_start+low_buffer_size, 
1102 +                       uncompressedSize-low_buffer_size);      
1103 +               }
1104 +               else            
1105 +                   memcpy((char*)LOW_BUFFER_START, high_buffer_start, uncompressedSize );
1106 +               bytes_out = i;
1107 +               return 0;
1108 +       }
1109 +       return 1;
1110 +}
1111 +
1112 +
1113 +#ifdef  _LZMA_IN_CB
1114 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
1115 +{
1116 +       static unsigned int i = 0;
1117 +       static unsigned char val;
1118 +       *bufferSize = 1;
1119 +       val = get_byte();
1120 +       *buffer = &val;
1121 +        if ( i++ % ( 1024 * 50 ) == 0 )
1122 +               putstr(".");
1123 +       return LZMA_RESULT_OK;
1124 +}      
1125 +#endif
1126 +
1127 +asmlinkage int decompress_kernel(struct moveparams *mv, void *rmode)
1128 +{
1129 +       real_mode = rmode;
1130 +
1131 +       if (RM_SCREEN_INFO.orig_video_mode == 7) {
1132 +               vidmem = (char *) 0xb0000;
1133 +               vidport = 0x3b4;
1134 +       } else {
1135 +               vidmem = (char *) 0xb8000;
1136 +               vidport = 0x3d4;
1137 +       }
1138 +
1139 +       lines = RM_SCREEN_INFO.orig_video_lines;
1140 +       cols = RM_SCREEN_INFO.orig_video_cols;
1141 +
1142 +       if (free_mem_ptr < 0x100000) setup_normal_output_buffer();
1143 +       else setup_output_buffer_if_we_run_high(mv);
1144 +
1145 +       putstr("LZMA vmlinuz: Ming-Ching Tiew <mctiew@yahoo.com> ...");
1146 +       if( lzma_unzip() != 0 )
1147 +       {
1148 +          error("inflate error\n");
1149 +       }
1150 +       putstr("Ok, booting the kernel.\n");
1151 +       if (high_loaded) close_output_buffer_if_we_run_high(mv);
1152 +       return high_loaded;
1153 +}
1154 diff -urN linux-2.6.19.2/scripts/Makefile.lib linux-2.6.19.2.new/scripts/Makefile.lib
1155 --- linux-2.6.19.2/scripts/Makefile.lib 2007-01-10 20:10:37.000000000 +0100
1156 +++ linux-2.6.19.2.new/scripts/Makefile.lib     2007-04-15 23:51:54.000000000 +0200
1157 @@ -162,4 +162,9 @@
1158  quiet_cmd_gzip = GZIP    $@
1159  cmd_gzip = gzip -f -9 < $< > $@
1160  
1161 -
1162 +# LZMA
1163 +#
1164 +quiet_cmd_lzma = LZMA $@
1165 +cmd_lzma = lzma e $< $@ -lc7 -lp0 -pb0
1166 +# to use lzmacomp,
1167 +# cmd_lzma = lzmacomp $< 700 > $@