7995b5e5156510a40573a660652c55708c0dfaea
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.30 / 960-arm_lzma_loader.patch
1 --- /dev/null
2 +++ b/arch/arm/boot/compressed/LzmaDecode.c
3 @@ -0,0 +1,590 @@
4 +/*
5 +  LzmaDecode.c
6 +  LZMA Decoder (optimized for Speed version)
7 +  
8 +  LZMA SDK 4.22 Copyright (c) 1999-2005 Igor Pavlov (2005-06-10)
9 +  http://www.7-zip.org/
10 +
11 +  LZMA SDK is licensed under two licenses:
12 +  1) GNU Lesser General Public License (GNU LGPL)
13 +  2) Common Public License (CPL)
14 +  It means that you can select one of these two licenses and 
15 +  follow rules of that license.
16 +
17 +  SPECIAL EXCEPTION:
18 +  Igor Pavlov, as the author of this Code, expressly permits you to 
19 +  statically or dynamically link your Code (or bind by name) to the 
20 +  interfaces of this file without subjecting your linked Code to the 
21 +  terms of the CPL or GNU LGPL. Any modifications or additions 
22 +  to this file, however, are subject to the LGPL or CPL terms.
23 +*/
24 +
25 +#include "LzmaDecode.h"
26 +
27 +#ifndef Byte
28 +#define Byte unsigned char
29 +#endif
30 +
31 +#define kNumTopBits 24
32 +#define kTopValue ((UInt32)1 << kNumTopBits)
33 +
34 +#define kNumBitModelTotalBits 11
35 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
36 +#define kNumMoveBits 5
37 +
38 +#define RC_READ_BYTE (*Buffer++)
39 +
40 +#define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
41 +  { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
42 +
43 +#ifdef _LZMA_IN_CB
44 +
45 +#define RC_TEST { if (Buffer == BufferLim) \
46 +  { SizeT size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
47 +  BufferLim = Buffer + size; if (size == 0) return LZMA_RESULT_DATA_ERROR; }}
48 +
49 +#define RC_INIT Buffer = BufferLim = 0; RC_INIT2
50 +
51 +#else
52 +
53 +#define RC_TEST { if (Buffer == BufferLim) return LZMA_RESULT_DATA_ERROR; }
54 +
55 +#define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
56
57 +#endif
58 +
59 +#define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
60 +
61 +#define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
62 +#define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
63 +#define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
64 +
65 +#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
66 +  { UpdateBit0(p); mi <<= 1; A0; } else \
67 +  { UpdateBit1(p); mi = (mi + mi) + 1; A1; } 
68 +  
69 +#define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)               
70 +
71 +#define RangeDecoderBitTreeDecode(probs, numLevels, res) \
72 +  { int i = numLevels; res = 1; \
73 +  do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
74 +  res -= (1 << numLevels); }
75 +
76 +
77 +#define kNumPosBitsMax 4
78 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
79 +
80 +#define kLenNumLowBits 3
81 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
82 +#define kLenNumMidBits 3
83 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
84 +#define kLenNumHighBits 8
85 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
86 +
87 +#define LenChoice 0
88 +#define LenChoice2 (LenChoice + 1)
89 +#define LenLow (LenChoice2 + 1)
90 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
91 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
92 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols) 
93 +
94 +
95 +#define kNumStates 12
96 +#define kNumLitStates 7
97 +
98 +#define kStartPosModelIndex 4
99 +#define kEndPosModelIndex 14
100 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
101 +
102 +#define kNumPosSlotBits 6
103 +#define kNumLenToPosStates 4
104 +
105 +#define kNumAlignBits 4
106 +#define kAlignTableSize (1 << kNumAlignBits)
107 +
108 +#define kMatchMinLen 2
109 +
110 +#define IsMatch 0
111 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
112 +#define IsRepG0 (IsRep + kNumStates)
113 +#define IsRepG1 (IsRepG0 + kNumStates)
114 +#define IsRepG2 (IsRepG1 + kNumStates)
115 +#define IsRep0Long (IsRepG2 + kNumStates)
116 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
117 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
118 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
119 +#define LenCoder (Align + kAlignTableSize)
120 +#define RepLenCoder (LenCoder + kNumLenProbs)
121 +#define Literal (RepLenCoder + kNumLenProbs)
122 +
123 +#if Literal != LZMA_BASE_SIZE
124 +StopCompilingDueBUG
125 +#endif
126 +
127 +#if 0
128 +int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
129 +{
130 +  unsigned char prop0;
131 +  if (size < LZMA_PROPERTIES_SIZE)
132 +    return LZMA_RESULT_DATA_ERROR;
133 +  prop0 = propsData[0];
134 +  if (prop0 >= (9 * 5 * 5))
135 +    return LZMA_RESULT_DATA_ERROR;
136 +  {
137 +    for (propsRes->pb = 0; prop0 >= (9 * 5); propsRes->pb++, prop0 -= (9 * 5));
138 +    for (propsRes->lp = 0; prop0 >= 9; propsRes->lp++, prop0 -= 9);
139 +    propsRes->lc = prop0;
140 +    /*
141 +    unsigned char remainder = (unsigned char)(prop0 / 9);
142 +    propsRes->lc = prop0 % 9;
143 +    propsRes->pb = remainder / 5;
144 +    propsRes->lp = remainder % 5;
145 +    */
146 +  }
147 +
148 +  #ifdef _LZMA_OUT_READ
149 +  {
150 +    int i;
151 +    propsRes->DictionarySize = 0;
152 +    for (i = 0; i < 4; i++)
153 +      propsRes->DictionarySize += (UInt32)(propsData[1 + i]) << (i * 8);
154 +    if (propsRes->DictionarySize == 0)
155 +      propsRes->DictionarySize = 1;
156 +  }
157 +  #endif
158 +  return LZMA_RESULT_OK;
159 +}
160 +#endif
161 +
162 +#define kLzmaStreamWasFinishedId (-1)
163 +
164 +int LzmaDecode(CLzmaDecoderState *vs,
165 +    #ifdef _LZMA_IN_CB
166 +    ILzmaInCallback *InCallback,
167 +    #else
168 +    const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
169 +    #endif
170 +    unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed)
171 +{
172 +  CProb *p = vs->Probs;
173 +  SizeT nowPos = 0;
174 +  Byte previousByte = 0;
175 +  UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
176 +  UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
177 +  int lc = vs->Properties.lc;
178 +
179 +  #ifdef _LZMA_OUT_READ
180 +  
181 +  UInt32 Range = vs->Range;
182 +  UInt32 Code = vs->Code;
183 +  #ifdef _LZMA_IN_CB
184 +  const Byte *Buffer = vs->Buffer;
185 +  const Byte *BufferLim = vs->BufferLim;
186 +  #else
187 +  const Byte *Buffer = inStream;
188 +  const Byte *BufferLim = inStream + inSize;
189 +  #endif
190 +  int state = vs->State;
191 +  UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
192 +  int len = vs->RemainLen;
193 +  UInt32 globalPos = vs->GlobalPos;
194 +  UInt32 distanceLimit = vs->DistanceLimit;
195 +
196 +  Byte *dictionary = vs->Dictionary;
197 +  UInt32 dictionarySize = vs->Properties.DictionarySize;
198 +  UInt32 dictionaryPos = vs->DictionaryPos;
199 +
200 +  Byte tempDictionary[4];
201 +
202 +  #ifndef _LZMA_IN_CB
203 +  *inSizeProcessed = 0;
204 +  #endif
205 +  *outSizeProcessed = 0;
206 +  if (len == kLzmaStreamWasFinishedId)
207 +    return LZMA_RESULT_OK;
208 +
209 +  if (dictionarySize == 0)
210 +  {
211 +    dictionary = tempDictionary;
212 +    dictionarySize = 1;
213 +    tempDictionary[0] = vs->TempDictionary[0];
214 +  }
215 +
216 +  if (len == kLzmaNeedInitId)
217 +  {
218 +    {
219 +      UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
220 +      UInt32 i;
221 +      for (i = 0; i < numProbs; i++)
222 +        p[i] = kBitModelTotal >> 1; 
223 +      rep0 = rep1 = rep2 = rep3 = 1;
224 +      state = 0;
225 +      globalPos = 0;
226 +      distanceLimit = 0;
227 +      dictionaryPos = 0;
228 +      dictionary[dictionarySize - 1] = 0;
229 +      #ifdef _LZMA_IN_CB
230 +      RC_INIT;
231 +      #else
232 +      RC_INIT(inStream, inSize);
233 +      #endif
234 +    }
235 +    len = 0;
236 +  }
237 +  while(len != 0 && nowPos < outSize)
238 +  {
239 +    UInt32 pos = dictionaryPos - rep0;
240 +    if (pos >= dictionarySize)
241 +      pos += dictionarySize;
242 +    outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
243 +    if (++dictionaryPos == dictionarySize)
244 +      dictionaryPos = 0;
245 +    len--;
246 +  }
247 +  if (dictionaryPos == 0)
248 +    previousByte = dictionary[dictionarySize - 1];
249 +  else
250 +    previousByte = dictionary[dictionaryPos - 1];
251 +
252 +  #else /* if !_LZMA_OUT_READ */
253 +
254 +  int state = 0;
255 +  UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
256 +  int len = 0;
257 +  const Byte *Buffer;
258 +  const Byte *BufferLim;
259 +  UInt32 Range;
260 +  UInt32 Code;
261 +
262 +  #ifndef _LZMA_IN_CB
263 +  *inSizeProcessed = 0;
264 +  #endif
265 +  *outSizeProcessed = 0;
266 +
267 +  {
268 +    UInt32 i;
269 +    UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
270 +    for (i = 0; i < numProbs; i++)
271 +      p[i] = kBitModelTotal >> 1;
272 +  }
273 +  
274 +  #ifdef _LZMA_IN_CB
275 +  RC_INIT;
276 +  #else
277 +  RC_INIT(inStream, inSize);
278 +  #endif
279 +
280 +  #endif /* _LZMA_OUT_READ */
281 +
282 +  while(nowPos < outSize)
283 +  {
284 +    CProb *prob;
285 +    UInt32 bound;
286 +    int posState = (int)(
287 +        (nowPos 
288 +        #ifdef _LZMA_OUT_READ
289 +        + globalPos
290 +        #endif
291 +        )
292 +        & posStateMask);
293 +
294 +    prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
295 +    IfBit0(prob)
296 +    {
297 +      int symbol = 1;
298 +      UpdateBit0(prob)
299 +      prob = p + Literal + (LZMA_LIT_SIZE * 
300 +        (((
301 +        (nowPos 
302 +        #ifdef _LZMA_OUT_READ
303 +        + globalPos
304 +        #endif
305 +        )
306 +        & literalPosMask) << lc) + (previousByte >> (8 - lc))));
307 +
308 +      if (state >= kNumLitStates)
309 +      {
310 +        int matchByte;
311 +        #ifdef _LZMA_OUT_READ
312 +        UInt32 pos = dictionaryPos - rep0;
313 +        if (pos >= dictionarySize)
314 +          pos += dictionarySize;
315 +        matchByte = dictionary[pos];
316 +        #else
317 +        matchByte = outStream[nowPos - rep0];
318 +        #endif
319 +        do
320 +        {
321 +          int bit;
322 +          CProb *probLit;
323 +          matchByte <<= 1;
324 +          bit = (matchByte & 0x100);
325 +          probLit = prob + 0x100 + bit + symbol;
326 +          RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
327 +        }
328 +        while (symbol < 0x100);
329 +      }
330 +      while (symbol < 0x100)
331 +      {
332 +        CProb *probLit = prob + symbol;
333 +        RC_GET_BIT(probLit, symbol)
334 +      }
335 +      previousByte = (Byte)symbol;
336 +
337 +      outStream[nowPos++] = previousByte;
338 +      #ifdef _LZMA_OUT_READ
339 +      if (distanceLimit < dictionarySize)
340 +        distanceLimit++;
341 +
342 +      dictionary[dictionaryPos] = previousByte;
343 +      if (++dictionaryPos == dictionarySize)
344 +        dictionaryPos = 0;
345 +      #endif
346 +      if (state < 4) state = 0;
347 +      else if (state < 10) state -= 3;
348 +      else state -= 6;
349 +    }
350 +    else             
351 +    {
352 +      UpdateBit1(prob);
353 +      prob = p + IsRep + state;
354 +      IfBit0(prob)
355 +      {
356 +        UpdateBit0(prob);
357 +        rep3 = rep2;
358 +        rep2 = rep1;
359 +        rep1 = rep0;
360 +        state = state < kNumLitStates ? 0 : 3;
361 +        prob = p + LenCoder;
362 +      }
363 +      else
364 +      {
365 +        UpdateBit1(prob);
366 +        prob = p + IsRepG0 + state;
367 +        IfBit0(prob)
368 +        {
369 +          UpdateBit0(prob);
370 +          prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
371 +          IfBit0(prob)
372 +          {
373 +            #ifdef _LZMA_OUT_READ
374 +            UInt32 pos;
375 +            #endif
376 +            UpdateBit0(prob);
377 +            
378 +            #ifdef _LZMA_OUT_READ
379 +            if (distanceLimit == 0)
380 +            #else
381 +            if (nowPos == 0)
382 +            #endif
383 +              return LZMA_RESULT_DATA_ERROR;
384 +            
385 +            state = state < kNumLitStates ? 9 : 11;
386 +            #ifdef _LZMA_OUT_READ
387 +            pos = dictionaryPos - rep0;
388 +            if (pos >= dictionarySize)
389 +              pos += dictionarySize;
390 +            previousByte = dictionary[pos];
391 +            dictionary[dictionaryPos] = previousByte;
392 +            if (++dictionaryPos == dictionarySize)
393 +              dictionaryPos = 0;
394 +            #else
395 +            previousByte = outStream[nowPos - rep0];
396 +            #endif
397 +            outStream[nowPos++] = previousByte;
398 +            #ifdef _LZMA_OUT_READ
399 +            if (distanceLimit < dictionarySize)
400 +              distanceLimit++;
401 +            #endif
402 +
403 +            continue;
404 +          }
405 +          else
406 +          {
407 +            UpdateBit1(prob);
408 +          }
409 +        }
410 +        else
411 +        {
412 +          UInt32 distance;
413 +          UpdateBit1(prob);
414 +          prob = p + IsRepG1 + state;
415 +          IfBit0(prob)
416 +          {
417 +            UpdateBit0(prob);
418 +            distance = rep1;
419 +          }
420 +          else 
421 +          {
422 +            UpdateBit1(prob);
423 +            prob = p + IsRepG2 + state;
424 +            IfBit0(prob)
425 +            {
426 +              UpdateBit0(prob);
427 +              distance = rep2;
428 +            }
429 +            else
430 +            {
431 +              UpdateBit1(prob);
432 +              distance = rep3;
433 +              rep3 = rep2;
434 +            }
435 +            rep2 = rep1;
436 +          }
437 +          rep1 = rep0;
438 +          rep0 = distance;
439 +        }
440 +        state = state < kNumLitStates ? 8 : 11;
441 +        prob = p + RepLenCoder;
442 +      }
443 +      {
444 +        int numBits, offset;
445 +        CProb *probLen = prob + LenChoice;
446 +        IfBit0(probLen)
447 +        {
448 +          UpdateBit0(probLen);
449 +          probLen = prob + LenLow + (posState << kLenNumLowBits);
450 +          offset = 0;
451 +          numBits = kLenNumLowBits;
452 +        }
453 +        else
454 +        {
455 +          UpdateBit1(probLen);
456 +          probLen = prob + LenChoice2;
457 +          IfBit0(probLen)
458 +          {
459 +            UpdateBit0(probLen);
460 +            probLen = prob + LenMid + (posState << kLenNumMidBits);
461 +            offset = kLenNumLowSymbols;
462 +            numBits = kLenNumMidBits;
463 +          }
464 +          else
465 +          {
466 +            UpdateBit1(probLen);
467 +            probLen = prob + LenHigh;
468 +            offset = kLenNumLowSymbols + kLenNumMidSymbols;
469 +            numBits = kLenNumHighBits;
470 +          }
471 +        }
472 +        RangeDecoderBitTreeDecode(probLen, numBits, len);
473 +        len += offset;
474 +      }
475 +
476 +      if (state < 4)
477 +      {
478 +        int posSlot;
479 +        state += kNumLitStates;
480 +        prob = p + PosSlot +
481 +            ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << 
482 +            kNumPosSlotBits);
483 +        RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
484 +        if (posSlot >= kStartPosModelIndex)
485 +        {
486 +          int numDirectBits = ((posSlot >> 1) - 1);
487 +          rep0 = (2 | ((UInt32)posSlot & 1));
488 +          if (posSlot < kEndPosModelIndex)
489 +          {
490 +            rep0 <<= numDirectBits;
491 +            prob = p + SpecPos + rep0 - posSlot - 1;
492 +          }
493 +          else
494 +          {
495 +            numDirectBits -= kNumAlignBits;
496 +            do
497 +            {
498 +              RC_NORMALIZE
499 +              Range >>= 1;
500 +              rep0 <<= 1;
501 +              if (Code >= Range)
502 +              {
503 +                Code -= Range;
504 +                rep0 |= 1;
505 +              }
506 +            }
507 +            while (--numDirectBits != 0);
508 +            prob = p + Align;
509 +            rep0 <<= kNumAlignBits;
510 +            numDirectBits = kNumAlignBits;
511 +          }
512 +          {
513 +            int i = 1;
514 +            int mi = 1;
515 +            do
516 +            {
517 +              CProb *prob3 = prob + mi;
518 +              RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
519 +              i <<= 1;
520 +            }
521 +            while(--numDirectBits != 0);
522 +          }
523 +        }
524 +        else
525 +          rep0 = posSlot;
526 +        if (++rep0 == (UInt32)(0))
527 +        {
528 +          /* it's for stream version */
529 +          len = kLzmaStreamWasFinishedId;
530 +          break;
531 +        }
532 +      }
533 +
534 +      len += kMatchMinLen;
535 +      #ifdef _LZMA_OUT_READ
536 +      if (rep0 > distanceLimit) 
537 +      #else
538 +      if (rep0 > nowPos)
539 +      #endif
540 +        return LZMA_RESULT_DATA_ERROR;
541 +
542 +      #ifdef _LZMA_OUT_READ
543 +      if (dictionarySize - distanceLimit > (UInt32)len)
544 +        distanceLimit += len;
545 +      else
546 +        distanceLimit = dictionarySize;
547 +      #endif
548 +
549 +      do
550 +      {
551 +        #ifdef _LZMA_OUT_READ
552 +        UInt32 pos = dictionaryPos - rep0;
553 +        if (pos >= dictionarySize)
554 +          pos += dictionarySize;
555 +        previousByte = dictionary[pos];
556 +        dictionary[dictionaryPos] = previousByte;
557 +        if (++dictionaryPos == dictionarySize)
558 +          dictionaryPos = 0;
559 +        #else
560 +        previousByte = outStream[nowPos - rep0];
561 +        #endif
562 +        len--;
563 +        outStream[nowPos++] = previousByte;
564 +      }
565 +      while(len != 0 && nowPos < outSize);
566 +    }
567 +  }
568 +  RC_NORMALIZE;
569 +
570 +  #ifdef _LZMA_OUT_READ
571 +  vs->Range = Range;
572 +  vs->Code = Code;
573 +  vs->DictionaryPos = dictionaryPos;
574 +  vs->GlobalPos = globalPos + (UInt32)nowPos;
575 +  vs->DistanceLimit = distanceLimit;
576 +  vs->Reps[0] = rep0;
577 +  vs->Reps[1] = rep1;
578 +  vs->Reps[2] = rep2;
579 +  vs->Reps[3] = rep3;
580 +  vs->State = state;
581 +  vs->RemainLen = len;
582 +  vs->TempDictionary[0] = tempDictionary[0];
583 +  #endif
584 +
585 +  #ifdef _LZMA_IN_CB
586 +  vs->Buffer = Buffer;
587 +  vs->BufferLim = BufferLim;
588 +  #else
589 +  *inSizeProcessed = (SizeT)(Buffer - inStream);
590 +  #endif
591 +  *outSizeProcessed = nowPos;
592 +  return LZMA_RESULT_OK;
593 +}
594 --- /dev/null
595 +++ b/arch/arm/boot/compressed/LzmaDecode.h
596 @@ -0,0 +1,131 @@
597 +/* 
598 +  LzmaDecode.h
599 +  LZMA Decoder interface
600 +
601 +  LZMA SDK 4.21 Copyright (c) 1999-2005 Igor Pavlov (2005-06-08)
602 +  http://www.7-zip.org/
603 +
604 +  LZMA SDK is licensed under two licenses:
605 +  1) GNU Lesser General Public License (GNU LGPL)
606 +  2) Common Public License (CPL)
607 +  It means that you can select one of these two licenses and 
608 +  follow rules of that license.
609 +
610 +  SPECIAL EXCEPTION:
611 +  Igor Pavlov, as the author of this code, expressly permits you to 
612 +  statically or dynamically link your code (or bind by name) to the 
613 +  interfaces of this file without subjecting your linked code to the 
614 +  terms of the CPL or GNU LGPL. Any modifications or additions 
615 +  to this file, however, are subject to the LGPL or CPL terms.
616 +*/
617 +
618 +#ifndef __LZMADECODE_H
619 +#define __LZMADECODE_H
620 +
621 +/* #define _LZMA_IN_CB */
622 +/* Use callback for input data */
623 +
624 +/* #define _LZMA_OUT_READ */
625 +/* Use read function for output data */
626 +
627 +/* #define _LZMA_PROB32 */
628 +/* It can increase speed on some 32-bit CPUs, 
629 +   but memory usage will be doubled in that case */
630 +
631 +/* #define _LZMA_LOC_OPT */
632 +/* Enable local speed optimizations inside code */
633 +
634 +/* #define _LZMA_SYSTEM_SIZE_T */
635 +/* Use system's size_t. You can use it to enable 64-bit sizes supporting*/
636 +
637 +#ifndef UInt32
638 +#ifdef _LZMA_UINT32_IS_ULONG
639 +#define UInt32 unsigned long
640 +#else
641 +#define UInt32 unsigned int
642 +#endif
643 +#endif
644 +
645 +#ifndef SizeT
646 +#ifdef _LZMA_SYSTEM_SIZE_T
647 +#include <stddef.h>
648 +#define SizeT size_t
649 +#else
650 +#define SizeT UInt32
651 +#endif
652 +#endif
653 +
654 +#ifdef _LZMA_PROB32
655 +#define CProb UInt32
656 +#else
657 +#define CProb unsigned short
658 +#endif
659 +
660 +#define LZMA_RESULT_OK 0
661 +#define LZMA_RESULT_DATA_ERROR 1
662 +
663 +#ifdef _LZMA_IN_CB
664 +typedef struct _ILzmaInCallback
665 +{
666 +  int (*Read)(void *object, const unsigned char **buffer, SizeT *bufferSize);
667 +} ILzmaInCallback;
668 +#endif
669 +
670 +#define LZMA_BASE_SIZE 1846
671 +#define LZMA_LIT_SIZE 768
672 +
673 +#define LZMA_PROPERTIES_SIZE 5
674 +
675 +typedef struct _CLzmaProperties
676 +{
677 +  int lc;
678 +  int lp;
679 +  int pb;
680 +  #ifdef _LZMA_OUT_READ
681 +  UInt32 DictionarySize;
682 +  #endif
683 +}CLzmaProperties;
684 +
685 +int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size);
686 +
687 +#define LzmaGetNumProbs(Properties) (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((Properties)->lc + (Properties)->lp)))
688 +
689 +#define kLzmaNeedInitId (-2)
690 +
691 +typedef struct _CLzmaDecoderState
692 +{
693 +  CLzmaProperties Properties;
694 +  CProb *Probs;
695 +
696 +  #ifdef _LZMA_IN_CB
697 +  const unsigned char *Buffer;
698 +  const unsigned char *BufferLim;
699 +  #endif
700 +
701 +  #ifdef _LZMA_OUT_READ
702 +  unsigned char *Dictionary;
703 +  UInt32 Range;
704 +  UInt32 Code;
705 +  UInt32 DictionaryPos;
706 +  UInt32 GlobalPos;
707 +  UInt32 DistanceLimit;
708 +  UInt32 Reps[4];
709 +  int State;
710 +  int RemainLen;
711 +  unsigned char TempDictionary[4];
712 +  #endif
713 +} CLzmaDecoderState;
714 +
715 +#ifdef _LZMA_OUT_READ
716 +#define LzmaDecoderInit(vs) { (vs)->RemainLen = kLzmaNeedInitId; }
717 +#endif
718 +
719 +int LzmaDecode(CLzmaDecoderState *vs,
720 +    #ifdef _LZMA_IN_CB
721 +    ILzmaInCallback *inCallback,
722 +    #else
723 +    const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
724 +    #endif
725 +    unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed);
726 +
727 +#endif
728 --- a/arch/arm/boot/compressed/Makefile
729 +++ b/arch/arm/boot/compressed/Makefile
730 @@ -5,7 +5,7 @@
731  #
732  
733  HEAD   = head.o
734 -OBJS   = misc.o
735 +OBJS   = misc.o ../../lib/lib1funcs.o
736  FONTC  = $(srctree)/drivers/video/console/font_acorn_8x8.c
737  
738  #
739 @@ -63,7 +63,7 @@ endif
740  
741  SEDFLAGS       = s/TEXT_START/$(ZTEXTADDR)/;s/BSS_START/$(ZBSSADDR)/
742  
743 -targets       := vmlinux vmlinux.lds piggy.gz piggy.o font.o font.c \
744 +targets       := vmlinux vmlinux.lds piggy.lzma piggy.o font.o font.c \
745                  head.o misc.o $(OBJS)
746  
747  ifeq ($(CONFIG_FUNCTION_TRACER),y)
748 @@ -96,10 +96,10 @@ $(obj)/vmlinux: $(obj)/vmlinux.lds $(obj
749         $(call if_changed,ld)
750         @:
751  
752 -$(obj)/piggy.gz: $(obj)/../Image FORCE
753 -       $(call if_changed,gzip)
754 +$(obj)/piggy.lzma: $(obj)/../Image FORCE
755 +       $(call if_changed,lzma)
756  
757 -$(obj)/piggy.o:  $(obj)/piggy.gz FORCE
758 +$(obj)/piggy.o:  $(obj)/piggy.lzma FORCE
759  
760  CFLAGS_font.o := -Dstatic=
761  
762 --- a/arch/arm/boot/compressed/misc.c
763 +++ b/arch/arm/boot/compressed/misc.c
764 @@ -202,8 +202,8 @@ typedef unsigned long  ulg;
765  static uch *inbuf;             /* input buffer */
766  static uch window[WSIZE];      /* Sliding window buffer */
767  
768 -static unsigned insize;                /* valid bytes in inbuf */
769 -static unsigned inptr;         /* index of next byte to be processed in inbuf */
770 +static unsigned insize = 0;            /* valid bytes in inbuf */
771 +static unsigned inptr = 0;             /* index of next byte to be processed in inbuf */
772  static unsigned outcnt;                /* bytes in output buffer */
773  
774  /* gzip flag byte */
775 @@ -242,7 +242,7 @@ extern char input_data[];
776  extern char input_data_end[];
777  
778  static uch *output_data;
779 -static ulg output_ptr;
780 +static ulg output_ptr = 0;
781  static ulg bytes_out;
782  
783  static void error(char *m);
784 @@ -259,7 +259,7 @@ static ulg free_mem_end_ptr;
785  
786  #define ARCH_HAS_DECOMP_WDOG
787  
788 -#include "../../../../lib/inflate.c"
789 +/* #include "../../../../lib/inflate.c" */
790  
791  /* ===========================================================================
792   * Fill the input buffer. This is called only when the buffer is empty
793 @@ -277,6 +277,76 @@ int fill_inbuf(void)
794         return inbuf[0];
795  }
796  
797 +#define _LZMA_IN_CB
798 +#include "LzmaDecode.h"
799 +#include "LzmaDecode.c"
800 +
801 +void __div0(void)
802 +{
803 +}
804 +
805 +static int read_byte(void *object, const unsigned char **buffer, SizeT *bufferSize);
806 +
807 +/*
808 + * Do the lzma decompression
809 + */
810 +static int unlzma(void)
811 +{
812 +
813 +       unsigned int i;
814 +       CLzmaDecoderState state;
815 +       unsigned int uncompressedSize = 0;
816 +
817 +       ILzmaInCallback callback;
818 +       callback.Read = read_byte;
819 +
820 +       /* lzma args */
821 +       i = get_byte();
822 +       state.Properties.lc = i % 9, i = i / 9;
823 +       state.Properties.lp = i % 5, state.Properties.pb = i / 5;
824 +
825 +       /* skip dictionary size */
826 +       for (i = 0; i < 4; i++)
827 +               get_byte();
828 +       /* get uncompressed size */
829 +       uncompressedSize = (get_byte()) +
830 +               (get_byte() << 8) +
831 +               (get_byte() << 16) +
832 +               (get_byte() << 24);
833 +
834 +       /* skip high order bytes */
835 +       for (i = 0; i < 4; i++)
836 +               get_byte();
837 +       /* point it beyond uncompresedSize */
838 +       state.Probs = (CProb *) (output_data + uncompressedSize);
839 +
840 +       /* decompress kernel */
841 +       if (LzmaDecode(&state, &callback,
842 +               (unsigned char *)output_data, uncompressedSize, &i) == LZMA_RESULT_OK) {
843 +               if (i != uncompressedSize)
844 +                       error("kernel corrupted!\n");
845 +               /* copy it back to low_buffer */
846 +               bytes_out = i;
847 +               output_ptr = i;
848 +               return 0;
849 +       }
850 +       return 1;
851 +}
852 +
853 +static unsigned int icnt = 0;
854 +
855 +static int read_byte(void *object, const unsigned char **buffer, SizeT *bufferSize)
856 +{
857 +       static unsigned char val;
858 +       *bufferSize = 1;
859 +       val = get_byte();
860 +       *buffer = &val;
861 +       if (icnt++ % (1024 * 10) == 0)
862 +               putstr(".");
863 +       return LZMA_RESULT_OK;
864 +}
865 +
866 +#if 0
867  /* ===========================================================================
868   * Write the output window window[0..outcnt-1] and update crc and bytes_out.
869   * (Used for the decompressed data only.)
870 @@ -299,6 +369,7 @@ void flush_window(void)
871         outcnt = 0;
872         putstr(".");
873  }
874 +#endif
875  
876  #ifndef arch_error
877  #define arch_error(x)
878 @@ -328,9 +399,9 @@ decompress_kernel(ulg output_start, ulg 
879  
880         arch_decomp_setup();
881  
882 -       makecrc();
883 +       /* makecrc(); */
884         putstr("Uncompressing Linux...");
885 -       gunzip();
886 +       unlzma();
887         putstr(" done, booting the kernel.\n");
888         return output_ptr;
889  }
890 @@ -342,9 +413,9 @@ int main()
891  {
892         output_data = output_buffer;
893  
894 -       makecrc();
895 +       /* makecrc(); */
896         putstr("Uncompressing Linux...");
897 -       gunzip();
898 +       unlzma();
899         putstr("done.\n");
900         return 0;
901  }
902 --- a/arch/arm/boot/compressed/piggy.S
903 +++ b/arch/arm/boot/compressed/piggy.S
904 @@ -1,6 +1,6 @@
905         .section .piggydata,#alloc
906         .globl  input_data
907  input_data:
908 -       .incbin "arch/arm/boot/compressed/piggy.gz"
909 +       .incbin "arch/arm/boot/compressed/piggy.lzma"
910         .globl  input_data_end
911  input_data_end: