[9298] was b0rked
[openwrt.git] / target / linux / rdc / 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/lzma_misc.c linux-lzma/arch/i386/boot/compressed/lzma_misc.c
696 --- linux-old/arch/i386/boot/compressed/lzma_misc.c 1969-12-31 19:00:00.000000000 -0500
697 +++ linux-lzma/arch/i386/boot/compressed/lzma_misc.c 2005-06-04 21:33:48.000000000 -0400
698 @@ -0,0 +1,281 @@
699 +/*
700 + * lzma_misc.c
701 + *
702 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
703 + * puts by Nick Holloway 1993, better puts by Martin Mares 1995
704 + * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
705 + *
706 + * Decompress LZMA compressed vmlinuz
707 + * Version 0.9 Copyright (c) Ming-Ching Tiew mctiew@yahoo.com
708 + * Program adapted from misc.c for 2.6 kernel
709 + * Forward ported to latest 2.6 version of misc.c by
710 + * Felix Fietkau <nbd@openwrt.org>
711 + */
712 +
713 +#undef CONFIG_PARAVIRT
714 +#include <linux/linkage.h>
715 +#include <linux/vmalloc.h>
716 +#include <linux/screen_info.h>
717 +#include <asm/io.h>
718 +#include <asm/page.h>
719 +#include <asm/boot.h>
720 +
721 +/* WARNING!!
722 + * This code is compiled with -fPIC and it is relocated dynamically
723 + * at run time, but no relocation processing is performed.
724 + * This means that it is not safe to place pointers in static structures.
725 + */
726 +
727 +/*
728 + * Getting to provable safe in place decompression is hard.
729 + * Worst case behaviours need to be analized.
730 + * Background information:
731 + *
732 + * The file layout is:
733 + * magic[2]
734 + * method[1]
735 + * flags[1]
736 + * timestamp[4]
737 + * extraflags[1]
738 + * os[1]
739 + * compressed data blocks[N]
740 + * crc[4] orig_len[4]
741 + *
742 + * resulting in 18 bytes of non compressed data overhead.
743 + *
744 + * Files divided into blocks
745 + * 1 bit (last block flag)
746 + * 2 bits (block type)
747 + *
748 + * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
749 + * The smallest block type encoding is always used.
750 + *
751 + * stored:
752 + * 32 bits length in bytes.
753 + *
754 + * fixed:
755 + * magic fixed tree.
756 + * symbols.
757 + *
758 + * dynamic:
759 + * dynamic tree encoding.
760 + * symbols.
761 + *
762 + *
763 + * The buffer for decompression in place is the length of the
764 + * uncompressed data, plus a small amount extra to keep the algorithm safe.
765 + * The compressed data is placed at the end of the buffer. The output
766 + * pointer is placed at the start of the buffer and the input pointer
767 + * is placed where the compressed data starts. Problems will occur
768 + * when the output pointer overruns the input pointer.
769 + *
770 + * The output pointer can only overrun the input pointer if the input
771 + * pointer is moving faster than the output pointer. A condition only
772 + * triggered by data whose compressed form is larger than the uncompressed
773 + * form.
774 + *
775 + * The worst case at the block level is a growth of the compressed data
776 + * of 5 bytes per 32767 bytes.
777 + *
778 + * The worst case internal to a compressed block is very hard to figure.
779 + * The worst case can at least be boundined by having one bit that represents
780 + * 32764 bytes and then all of the rest of the bytes representing the very
781 + * very last byte.
782 + *
783 + * All of which is enough to compute an amount of extra data that is required
784 + * to be safe. To avoid problems at the block level allocating 5 extra bytes
785 + * per 32767 bytes of data is sufficient. To avoind problems internal to a block
786 + * adding an extra 32767 bytes (the worst case uncompressed block size) is
787 + * sufficient, to ensure that in the worst case the decompressed data for
788 + * block will stop the byte before the compressed data for a block begins.
789 + * To avoid problems with the compressed data's meta information an extra 18
790 + * bytes are needed. Leading to the formula:
791 + *
792 + * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
793 + *
794 + * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
795 + * Adding 32768 instead of 32767 just makes for round numbers.
796 + * Adding the decompressor_size is necessary as it musht live after all
797 + * of the data as well. Last I measured the decompressor is about 14K.
798 + * 10K of actuall data and 4K of bss.
799 + *
800 + */
801 +
802 +/*
803 + * gzip declarations
804 + */
805 +
806 +#define OF(args) args
807 +#define STATIC static
808 +
809 +#undef memcpy
810 +
811 +typedef unsigned char uch;
812 +typedef unsigned short ush;
813 +typedef unsigned long ulg;
814 +
815 +#define WSIZE 0x80000000 /* Window size must be at least 32k,
816 + * and a power of two
817 + * We don't actually have a window just
818 + * a huge output buffer so I report
819 + * a 2G windows size, as that should
820 + * always be larger than our output buffer.
821 + */
822 +
823 +static uch *inbuf; /* input buffer */
824 +static uch *window; /* Sliding window buffer, (and final output buffer) */
825 +
826 +static unsigned insize; /* valid bytes in inbuf */
827 +static unsigned inptr; /* index of next byte to be processed in inbuf */
828 +static unsigned long workspace;
829 +
830 +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
831 +
832 +/* Diagnostic functions */
833 +#ifdef DEBUG
834 +# define Assert(cond,msg) {if(!(cond)) error(msg);}
835 +# define Trace(x) fprintf x
836 +# define Tracev(x) {if (verbose) fprintf x ;}
837 +# define Tracevv(x) {if (verbose>1) fprintf x ;}
838 +# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
839 +# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
840 +#else
841 +# define Assert(cond,msg)
842 +# define Trace(x)
843 +# define Tracev(x)
844 +# define Tracevv(x)
845 +# define Tracec(c,x)
846 +# define Tracecv(c,x)
847 +#endif
848 +
849 +static int fill_inbuf(void);
850 +
851 +/*
852 + * This is set up by the setup-routine at boot-time
853 + */
854 +static unsigned char *real_mode; /* Pointer to real-mode data */
855 +extern unsigned char input_data[];
856 +extern int input_len;
857 +
858 +static void error(char *x);
859 +static void *memcpy(void *dest, const void *src, unsigned n);
860 +
861 +#ifdef CONFIG_X86_NUMAQ
862 +void *xquad_portio;
863 +#endif
864 +
865 +static void* memcpy(void* dest, const void* src, unsigned n)
866 +{
867 + int i;
868 + char *d = (char *)dest, *s = (char *)src;
869 +
870 + for (i=0;i<n;i++) d[i] = s[i];
871 + return dest;
872 +}
873 +
874 +/* ===========================================================================
875 + * Fill the input buffer. This is called only when the buffer is empty
876 + * and at least one byte is really needed.
877 + */
878 +static int fill_inbuf(void)
879 +{
880 + error("ran out of input data");
881 + return 0;
882 +}
883 +
884 +
885 +// When using LZMA in callback, the compressed length is not needed.
886 +// Otherwise you need a special version of lzma compression program
887 +// which will pad the compressed length in the header.
888 +#define _LZMA_IN_CB
889 +#include "LzmaDecode.h"
890 +#include "LzmaDecode.c"
891 +
892 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize);
893 +
894 +
895 +/*
896 + * Do the lzma decompression
897 + * When using LZMA in callback, the end of input stream is automatically determined
898 + */
899 +static int lzma_unzip(void)
900 +{
901 +
902 + unsigned int i; /* temp value */
903 + unsigned int lc; /* literal context bits */
904 + unsigned int lp; /* literal pos state bits */
905 + unsigned int pb; /* pos state bits */
906 + unsigned int uncompressedSize = 0;
907 + unsigned char* p;
908 +
909 + ILzmaInCallback callback;
910 + callback.Read = read_byte;
911 +
912 + /* lzma args */
913 + i = get_byte();
914 + lc = i % 9, i = i / 9;
915 + lp = i % 5, pb = i / 5;
916 +
917 + /* skip dictionary size */
918 + for (i = 0; i < 4; i++)
919 + get_byte();
920 + // get uncompressedSize
921 + p= (char*)&uncompressedSize;
922 + for (i = 0; i < 4; i++)
923 + *p++ = get_byte();
924 +
925 + //get compressedSize
926 + for (i = 0; i < 4; i++)
927 + get_byte();
928 +
929 + // point it beyond uncompresedSize
930 + //workspace = window + uncompressedSize;
931 +
932 + /* decompress kernel */
933 + if (LzmaDecode((unsigned char*)workspace, ~0, lc, lp, pb, &callback,
934 + (unsigned char*)window, uncompressedSize, &i) == LZMA_RESULT_OK)
935 + return 0;
936 + else
937 + return 1;
938 +}
939 +
940 +
941 +#ifdef _LZMA_IN_CB
942 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
943 +{
944 + static unsigned int i = 0;
945 + static unsigned char val;
946 + *bufferSize = 1;
947 + val = get_byte();
948 + *buffer = &val;
949 + return LZMA_RESULT_OK;
950 +}
951 +#endif
952 +
953 +static void error(char *x)
954 +{
955 + while(1); /* Halt */
956 +}
957 +
958 +asmlinkage void decompress_kernel(void *rmode, unsigned long end,
959 + uch *input_data, unsigned long input_len, uch *output)
960 +{
961 + real_mode = rmode;
962 +
963 + window = output;
964 + inbuf = input_data; /* Input buffer */
965 + insize = input_len;
966 + inptr = 0;
967 +
968 + if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
969 + error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
970 + if ((workspace = end) > ((-__PAGE_OFFSET-(512 <<20)-1) & 0x7fffffff))
971 + error("Destination address too large");
972 +#ifndef CONFIG_RELOCATABLE
973 + if ((u32)output != LOAD_PHYSICAL_ADDR)
974 + error("Wrong destination address");
975 +#endif
976 +
977 + lzma_unzip();
978 + return;
979 +}
980 diff -Naur linux-old/arch/i386/boot/compressed/Makefile linux-lzma/arch/i386/boot/compressed/Makefile
981 --- linux-old/arch/i386/boot/compressed/Makefile 2005-06-04 21:53:40.000000000 -0400
982 +++ linux-lzma/arch/i386/boot/compressed/Makefile 2005-06-05 00:25:23.000000000 -0400
983 @@ -4,7 +4,7 @@
984 # create a compressed vmlinux image from the original vmlinux
985 #
986
987 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head.o misc.o piggy.o \
988 +targets := vmlinux vmlinux.bin vmlinux.bin.lzma head.o lzma_misc.o piggy.o \
989 vmlinux.bin.all vmlinux.relocs
990 EXTRA_AFLAGS := -traditional
991
992 @@ -17,7 +17,7 @@
993 $(call cc-option,-fno-stack-protector)
994 LDFLAGS := -m elf_i386
995
996 -$(obj)/vmlinux: $(src)/vmlinux.lds $(obj)/head.o $(obj)/misc.o $(obj)/piggy.o FORCE
997 +$(obj)/vmlinux: $(src)/vmlinux.lds $(obj)/head.o $(obj)/lzma_misc.o $(obj)/piggy.o FORCE
998 $(call if_changed,ld)
999 @:
1000
1001 @@ -37,14 +37,14 @@
1002 $(call if_changed,relocbin)
1003
1004 ifdef CONFIG_RELOCATABLE
1005 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
1006 - $(call if_changed,gzip)
1007 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
1008 + $(call if_changed,lzma)
1009 else
1010 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
1011 - $(call if_changed,gzip)
1012 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
1013 + $(call if_changed,lzma)
1014 endif
1015
1016 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
1017
1018 -$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
1019 +$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.lzma FORCE
1020 $(call if_changed,ld)
1021 diff -urN linux-2.6.19.2/scripts/Makefile.lib linux-2.6.19.2.new/scripts/Makefile.lib
1022 --- linux-2.6.19.2/scripts/Makefile.lib 2007-01-10 20:10:37.000000000 +0100
1023 +++ linux-2.6.19.2.new/scripts/Makefile.lib 2007-04-15 23:51:54.000000000 +0200
1024 @@ -162,4 +162,9 @@
1025 quiet_cmd_gzip = GZIP $@
1026 cmd_gzip = gzip -f -9 < $< > $@
1027
1028 -
1029 +# LZMA
1030 +#
1031 +quiet_cmd_lzma = LZMA $@
1032 +cmd_lzma = bash -e scripts/lzma_kern $< $@ -lc7 -lp0 -pb0
1033 +# to use lzmacomp,
1034 +# cmd_lzma = lzmacomp $< 700 > $@
1035 diff -u linux/scripts/lzma_kern linux/scripts/lzma_kern
1036 --- linux/scripts/lzma_kern 2007-07-27 20:18:17.013014750 -0700
1037 +++ linux/scripts/lzma_kern 2007-07-27 20:18:17.013014750 -0700
1038 @@ -0,0 +1,4 @@
1039 +get-size() { echo "$5" ;}
1040 +printf -v len '%.8x' "$(get-size $(ls -l "$1"))"
1041 +lzma e "$@"
1042 +echo -ne "\x$(echo $len | cut -c 7,8)\x$(echo $len | cut -c 5,6)\x$(echo $len | cut -c 3,4)\x$(echo $len | cut -c 1,2)" >> "$2"
This page took 0.099778 seconds and 5 git commands to generate.