lua: Fixed some cross-platform issues for PPC (and probably other architectures)
[openwrt.git] / target / linux / rdc / patches-2.6.25 / 600-x86_lzma.patch
1 --- /dev/null
2 +++ b/arch/x86/boot/compressed/LzmaDecode.c
3 @@ -0,0 +1,586 @@
4 +/*
5 + LzmaDecode.c
6 + LZMA Decoder (optimized for Speed version)
7 +
8 + LZMA SDK 4.17 Copyright (c) 1999-2005 Igor Pavlov (2005-04-05)
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 + { UInt32 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 +#ifdef _LZMA_OUT_READ
128 +
129 +typedef struct _LzmaVarState
130 +{
131 + Byte *Buffer;
132 + Byte *BufferLim;
133 + UInt32 Range;
134 + UInt32 Code;
135 + #ifdef _LZMA_IN_CB
136 + ILzmaInCallback *InCallback;
137 + #endif
138 + Byte *Dictionary;
139 + UInt32 DictionarySize;
140 + UInt32 DictionaryPos;
141 + UInt32 GlobalPos;
142 + UInt32 Reps[4];
143 + int lc;
144 + int lp;
145 + int pb;
146 + int State;
147 + int RemainLen;
148 + Byte TempDictionary[4];
149 +} LzmaVarState;
150 +
151 +int LzmaDecoderInit(
152 + unsigned char *buffer, UInt32 bufferSize,
153 + int lc, int lp, int pb,
154 + unsigned char *dictionary, UInt32 dictionarySize,
155 + #ifdef _LZMA_IN_CB
156 + ILzmaInCallback *InCallback
157 + #else
158 + unsigned char *inStream, UInt32 inSize
159 + #endif
160 + )
161 +{
162 + Byte *Buffer;
163 + Byte *BufferLim;
164 + UInt32 Range;
165 + UInt32 Code;
166 + LzmaVarState *vs = (LzmaVarState *)buffer;
167 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
168 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
169 + UInt32 i;
170 + if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
171 + return LZMA_RESULT_NOT_ENOUGH_MEM;
172 + vs->Dictionary = dictionary;
173 + vs->DictionarySize = dictionarySize;
174 + vs->DictionaryPos = 0;
175 + vs->GlobalPos = 0;
176 + vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
177 + vs->lc = lc;
178 + vs->lp = lp;
179 + vs->pb = pb;
180 + vs->State = 0;
181 + vs->RemainLen = 0;
182 + dictionary[dictionarySize - 1] = 0;
183 + for (i = 0; i < numProbs; i++)
184 + p[i] = kBitModelTotal >> 1;
185 +
186 + #ifdef _LZMA_IN_CB
187 + RC_INIT;
188 + #else
189 + RC_INIT(inStream, inSize);
190 + #endif
191 + vs->Buffer = Buffer;
192 + vs->BufferLim = BufferLim;
193 + vs->Range = Range;
194 + vs->Code = Code;
195 + #ifdef _LZMA_IN_CB
196 + vs->InCallback = InCallback;
197 + #endif
198 +
199 + return LZMA_RESULT_OK;
200 +}
201 +
202 +int LzmaDecode(unsigned char *buffer,
203 + unsigned char *outStream, UInt32 outSize,
204 + UInt32 *outSizeProcessed)
205 +{
206 + LzmaVarState *vs = (LzmaVarState *)buffer;
207 + Byte *Buffer = vs->Buffer;
208 + Byte *BufferLim = vs->BufferLim;
209 + UInt32 Range = vs->Range;
210 + UInt32 Code = vs->Code;
211 + #ifdef _LZMA_IN_CB
212 + ILzmaInCallback *InCallback = vs->InCallback;
213 + #endif
214 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
215 + int state = vs->State;
216 + Byte previousByte;
217 + UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
218 + UInt32 nowPos = 0;
219 + UInt32 posStateMask = (1 << (vs->pb)) - 1;
220 + UInt32 literalPosMask = (1 << (vs->lp)) - 1;
221 + int lc = vs->lc;
222 + int len = vs->RemainLen;
223 + UInt32 globalPos = vs->GlobalPos;
224 +
225 + Byte *dictionary = vs->Dictionary;
226 + UInt32 dictionarySize = vs->DictionarySize;
227 + UInt32 dictionaryPos = vs->DictionaryPos;
228 +
229 + Byte tempDictionary[4];
230 + if (dictionarySize == 0)
231 + {
232 + dictionary = tempDictionary;
233 + dictionarySize = 1;
234 + tempDictionary[0] = vs->TempDictionary[0];
235 + }
236 +
237 + if (len == -1)
238 + {
239 + *outSizeProcessed = 0;
240 + return LZMA_RESULT_OK;
241 + }
242 +
243 + while(len != 0 && nowPos < outSize)
244 + {
245 + UInt32 pos = dictionaryPos - rep0;
246 + if (pos >= dictionarySize)
247 + pos += dictionarySize;
248 + outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
249 + if (++dictionaryPos == dictionarySize)
250 + dictionaryPos = 0;
251 + len--;
252 + }
253 + if (dictionaryPos == 0)
254 + previousByte = dictionary[dictionarySize - 1];
255 + else
256 + previousByte = dictionary[dictionaryPos - 1];
257 +#else
258 +
259 +int LzmaDecode(
260 + Byte *buffer, UInt32 bufferSize,
261 + int lc, int lp, int pb,
262 + #ifdef _LZMA_IN_CB
263 + ILzmaInCallback *InCallback,
264 + #else
265 + unsigned char *inStream, UInt32 inSize,
266 + #endif
267 + unsigned char *outStream, UInt32 outSize,
268 + UInt32 *outSizeProcessed)
269 +{
270 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
271 + CProb *p = (CProb *)buffer;
272 +
273 + UInt32 i;
274 + int state = 0;
275 + Byte previousByte = 0;
276 + UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
277 + UInt32 nowPos = 0;
278 + UInt32 posStateMask = (1 << pb) - 1;
279 + UInt32 literalPosMask = (1 << lp) - 1;
280 + int len = 0;
281 +
282 + Byte *Buffer;
283 + Byte *BufferLim;
284 + UInt32 Range;
285 + UInt32 Code;
286 +
287 + if (bufferSize < numProbs * sizeof(CProb))
288 + return LZMA_RESULT_NOT_ENOUGH_MEM;
289 + for (i = 0; i < numProbs; i++)
290 + p[i] = kBitModelTotal >> 1;
291 +
292 +
293 + #ifdef _LZMA_IN_CB
294 + RC_INIT;
295 + #else
296 + RC_INIT(inStream, inSize);
297 + #endif
298 +#endif
299 +
300 + *outSizeProcessed = 0;
301 + while(nowPos < outSize)
302 + {
303 + CProb *prob;
304 + UInt32 bound;
305 + int posState = (int)(
306 + (nowPos
307 + #ifdef _LZMA_OUT_READ
308 + + globalPos
309 + #endif
310 + )
311 + & posStateMask);
312 +
313 + prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
314 + IfBit0(prob)
315 + {
316 + int symbol = 1;
317 + UpdateBit0(prob)
318 + prob = p + Literal + (LZMA_LIT_SIZE *
319 + (((
320 + (nowPos
321 + #ifdef _LZMA_OUT_READ
322 + + globalPos
323 + #endif
324 + )
325 + & literalPosMask) << lc) + (previousByte >> (8 - lc))));
326 +
327 + if (state >= kNumLitStates)
328 + {
329 + int matchByte;
330 + #ifdef _LZMA_OUT_READ
331 + UInt32 pos = dictionaryPos - rep0;
332 + if (pos >= dictionarySize)
333 + pos += dictionarySize;
334 + matchByte = dictionary[pos];
335 + #else
336 + matchByte = outStream[nowPos - rep0];
337 + #endif
338 + do
339 + {
340 + int bit;
341 + CProb *probLit;
342 + matchByte <<= 1;
343 + bit = (matchByte & 0x100);
344 + probLit = prob + 0x100 + bit + symbol;
345 + RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
346 + }
347 + while (symbol < 0x100);
348 + }
349 + while (symbol < 0x100)
350 + {
351 + CProb *probLit = prob + symbol;
352 + RC_GET_BIT(probLit, symbol)
353 + }
354 + previousByte = (Byte)symbol;
355 +
356 + outStream[nowPos++] = previousByte;
357 + #ifdef _LZMA_OUT_READ
358 + dictionary[dictionaryPos] = previousByte;
359 + if (++dictionaryPos == dictionarySize)
360 + dictionaryPos = 0;
361 + #endif
362 + if (state < 4) state = 0;
363 + else if (state < 10) state -= 3;
364 + else state -= 6;
365 + }
366 + else
367 + {
368 + UpdateBit1(prob);
369 + prob = p + IsRep + state;
370 + IfBit0(prob)
371 + {
372 + UpdateBit0(prob);
373 + rep3 = rep2;
374 + rep2 = rep1;
375 + rep1 = rep0;
376 + state = state < kNumLitStates ? 0 : 3;
377 + prob = p + LenCoder;
378 + }
379 + else
380 + {
381 + UpdateBit1(prob);
382 + prob = p + IsRepG0 + state;
383 + IfBit0(prob)
384 + {
385 + UpdateBit0(prob);
386 + prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
387 + IfBit0(prob)
388 + {
389 + #ifdef _LZMA_OUT_READ
390 + UInt32 pos;
391 + #endif
392 + UpdateBit0(prob);
393 + if (nowPos
394 + #ifdef _LZMA_OUT_READ
395 + + globalPos
396 + #endif
397 + == 0)
398 + return LZMA_RESULT_DATA_ERROR;
399 + state = state < kNumLitStates ? 9 : 11;
400 + #ifdef _LZMA_OUT_READ
401 + pos = dictionaryPos - rep0;
402 + if (pos >= dictionarySize)
403 + pos += dictionarySize;
404 + previousByte = dictionary[pos];
405 + dictionary[dictionaryPos] = previousByte;
406 + if (++dictionaryPos == dictionarySize)
407 + dictionaryPos = 0;
408 + #else
409 + previousByte = outStream[nowPos - rep0];
410 + #endif
411 + outStream[nowPos++] = previousByte;
412 + continue;
413 + }
414 + else
415 + {
416 + UpdateBit1(prob);
417 + }
418 + }
419 + else
420 + {
421 + UInt32 distance;
422 + UpdateBit1(prob);
423 + prob = p + IsRepG1 + state;
424 + IfBit0(prob)
425 + {
426 + UpdateBit0(prob);
427 + distance = rep1;
428 + }
429 + else
430 + {
431 + UpdateBit1(prob);
432 + prob = p + IsRepG2 + state;
433 + IfBit0(prob)
434 + {
435 + UpdateBit0(prob);
436 + distance = rep2;
437 + }
438 + else
439 + {
440 + UpdateBit1(prob);
441 + distance = rep3;
442 + rep3 = rep2;
443 + }
444 + rep2 = rep1;
445 + }
446 + rep1 = rep0;
447 + rep0 = distance;
448 + }
449 + state = state < kNumLitStates ? 8 : 11;
450 + prob = p + RepLenCoder;
451 + }
452 + {
453 + int numBits, offset;
454 + CProb *probLen = prob + LenChoice;
455 + IfBit0(probLen)
456 + {
457 + UpdateBit0(probLen);
458 + probLen = prob + LenLow + (posState << kLenNumLowBits);
459 + offset = 0;
460 + numBits = kLenNumLowBits;
461 + }
462 + else
463 + {
464 + UpdateBit1(probLen);
465 + probLen = prob + LenChoice2;
466 + IfBit0(probLen)
467 + {
468 + UpdateBit0(probLen);
469 + probLen = prob + LenMid + (posState << kLenNumMidBits);
470 + offset = kLenNumLowSymbols;
471 + numBits = kLenNumMidBits;
472 + }
473 + else
474 + {
475 + UpdateBit1(probLen);
476 + probLen = prob + LenHigh;
477 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
478 + numBits = kLenNumHighBits;
479 + }
480 + }
481 + RangeDecoderBitTreeDecode(probLen, numBits, len);
482 + len += offset;
483 + }
484 +
485 + if (state < 4)
486 + {
487 + int posSlot;
488 + state += kNumLitStates;
489 + prob = p + PosSlot +
490 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
491 + kNumPosSlotBits);
492 + RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
493 + if (posSlot >= kStartPosModelIndex)
494 + {
495 + int numDirectBits = ((posSlot >> 1) - 1);
496 + rep0 = (2 | ((UInt32)posSlot & 1));
497 + if (posSlot < kEndPosModelIndex)
498 + {
499 + rep0 <<= numDirectBits;
500 + prob = p + SpecPos + rep0 - posSlot - 1;
501 + }
502 + else
503 + {
504 + numDirectBits -= kNumAlignBits;
505 + do
506 + {
507 + RC_NORMALIZE
508 + Range >>= 1;
509 + rep0 <<= 1;
510 + if (Code >= Range)
511 + {
512 + Code -= Range;
513 + rep0 |= 1;
514 + }
515 + }
516 + while (--numDirectBits != 0);
517 + prob = p + Align;
518 + rep0 <<= kNumAlignBits;
519 + numDirectBits = kNumAlignBits;
520 + }
521 + {
522 + int i = 1;
523 + int mi = 1;
524 + do
525 + {
526 + CProb *prob3 = prob + mi;
527 + RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
528 + i <<= 1;
529 + }
530 + while(--numDirectBits != 0);
531 + }
532 + }
533 + else
534 + rep0 = posSlot;
535 + if (++rep0 == (UInt32)(0))
536 + {
537 + /* it's for stream version */
538 + len = -1;
539 + break;
540 + }
541 + }
542 +
543 + len += kMatchMinLen;
544 + if (rep0 > nowPos
545 + #ifdef _LZMA_OUT_READ
546 + + globalPos || rep0 > dictionarySize
547 + #endif
548 + )
549 + return LZMA_RESULT_DATA_ERROR;
550 + do
551 + {
552 + #ifdef _LZMA_OUT_READ
553 + UInt32 pos = dictionaryPos - rep0;
554 + if (pos >= dictionarySize)
555 + pos += dictionarySize;
556 + previousByte = dictionary[pos];
557 + dictionary[dictionaryPos] = previousByte;
558 + if (++dictionaryPos == dictionarySize)
559 + dictionaryPos = 0;
560 + #else
561 + previousByte = outStream[nowPos - rep0];
562 + #endif
563 + len--;
564 + outStream[nowPos++] = previousByte;
565 + }
566 + while(len != 0 && nowPos < outSize);
567 + }
568 + }
569 + RC_NORMALIZE;
570 +
571 + #ifdef _LZMA_OUT_READ
572 + vs->Buffer = Buffer;
573 + vs->BufferLim = BufferLim;
574 + vs->Range = Range;
575 + vs->Code = Code;
576 + vs->DictionaryPos = dictionaryPos;
577 + vs->GlobalPos = globalPos + nowPos;
578 + vs->Reps[0] = rep0;
579 + vs->Reps[1] = rep1;
580 + vs->Reps[2] = rep2;
581 + vs->Reps[3] = rep3;
582 + vs->State = state;
583 + vs->RemainLen = len;
584 + vs->TempDictionary[0] = tempDictionary[0];
585 + #endif
586 +
587 + *outSizeProcessed = nowPos;
588 + return LZMA_RESULT_OK;
589 +}
590 --- /dev/null
591 +++ b/arch/x86/boot/compressed/LzmaDecode.h
592 @@ -0,0 +1,100 @@
593 +/*
594 + LzmaDecode.h
595 + LZMA Decoder interface
596 +
597 + LZMA SDK 4.16 Copyright (c) 1999-2005 Igor Pavlov (2005-03-18)
598 + http://www.7-zip.org/
599 +
600 + LZMA SDK is licensed under two licenses:
601 + 1) GNU Lesser General Public License (GNU LGPL)
602 + 2) Common Public License (CPL)
603 + It means that you can select one of these two licenses and
604 + follow rules of that license.
605 +
606 + SPECIAL EXCEPTION:
607 + Igor Pavlov, as the author of this code, expressly permits you to
608 + statically or dynamically link your code (or bind by name) to the
609 + interfaces of this file without subjecting your linked code to the
610 + terms of the CPL or GNU LGPL. Any modifications or additions
611 + to this file, however, are subject to the LGPL or CPL terms.
612 +*/
613 +
614 +#ifndef __LZMADECODE_H
615 +#define __LZMADECODE_H
616 +
617 +/* #define _LZMA_IN_CB */
618 +/* Use callback for input data */
619 +
620 +/* #define _LZMA_OUT_READ */
621 +/* Use read function for output data */
622 +
623 +/* #define _LZMA_PROB32 */
624 +/* It can increase speed on some 32-bit CPUs,
625 + but memory usage will be doubled in that case */
626 +
627 +/* #define _LZMA_LOC_OPT */
628 +/* Enable local speed optimizations inside code */
629 +
630 +#ifndef UInt32
631 +#ifdef _LZMA_UINT32_IS_ULONG
632 +#define UInt32 unsigned long
633 +#else
634 +#define UInt32 unsigned int
635 +#endif
636 +#endif
637 +
638 +#ifdef _LZMA_PROB32
639 +#define CProb UInt32
640 +#else
641 +#define CProb unsigned short
642 +#endif
643 +
644 +#define LZMA_RESULT_OK 0
645 +#define LZMA_RESULT_DATA_ERROR 1
646 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
647 +
648 +#ifdef _LZMA_IN_CB
649 +typedef struct _ILzmaInCallback
650 +{
651 + int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
652 +} ILzmaInCallback;
653 +#endif
654 +
655 +#define LZMA_BASE_SIZE 1846
656 +#define LZMA_LIT_SIZE 768
657 +
658 +/*
659 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
660 +bufferSize += 100 in case of _LZMA_OUT_READ
661 +by default CProb is unsigned short,
662 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
663 +*/
664 +
665 +#ifdef _LZMA_OUT_READ
666 +int LzmaDecoderInit(
667 + unsigned char *buffer, UInt32 bufferSize,
668 + int lc, int lp, int pb,
669 + unsigned char *dictionary, UInt32 dictionarySize,
670 + #ifdef _LZMA_IN_CB
671 + ILzmaInCallback *inCallback
672 + #else
673 + unsigned char *inStream, UInt32 inSize
674 + #endif
675 +);
676 +#endif
677 +
678 +int LzmaDecode(
679 + unsigned char *buffer,
680 + #ifndef _LZMA_OUT_READ
681 + UInt32 bufferSize,
682 + int lc, int lp, int pb,
683 + #ifdef _LZMA_IN_CB
684 + ILzmaInCallback *inCallback,
685 + #else
686 + unsigned char *inStream, UInt32 inSize,
687 + #endif
688 + #endif
689 + unsigned char *outStream, UInt32 outSize,
690 + UInt32 *outSizeProcessed);
691 +
692 +#endif
693 --- /dev/null
694 +++ b/arch/x86/boot/compressed/lzma_misc.c
695 @@ -0,0 +1,281 @@
696 +/*
697 + * lzma_misc.c
698 + *
699 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
700 + * puts by Nick Holloway 1993, better puts by Martin Mares 1995
701 + * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
702 + *
703 + * Decompress LZMA compressed vmlinuz
704 + * Version 0.9 Copyright (c) Ming-Ching Tiew mctiew@yahoo.com
705 + * Program adapted from misc.c for 2.6 kernel
706 + * Forward ported to latest 2.6 version of misc.c by
707 + * Felix Fietkau <nbd@openwrt.org>
708 + */
709 +
710 +#undef CONFIG_PARAVIRT
711 +#include <linux/linkage.h>
712 +#include <linux/vmalloc.h>
713 +#include <linux/screen_info.h>
714 +#include <asm/io.h>
715 +#include <asm/page.h>
716 +#include <asm/boot.h>
717 +
718 +/* WARNING!!
719 + * This code is compiled with -fPIC and it is relocated dynamically
720 + * at run time, but no relocation processing is performed.
721 + * This means that it is not safe to place pointers in static structures.
722 + */
723 +
724 +/*
725 + * Getting to provable safe in place decompression is hard.
726 + * Worst case behaviours need to be analized.
727 + * Background information:
728 + *
729 + * The file layout is:
730 + * magic[2]
731 + * method[1]
732 + * flags[1]
733 + * timestamp[4]
734 + * extraflags[1]
735 + * os[1]
736 + * compressed data blocks[N]
737 + * crc[4] orig_len[4]
738 + *
739 + * resulting in 18 bytes of non compressed data overhead.
740 + *
741 + * Files divided into blocks
742 + * 1 bit (last block flag)
743 + * 2 bits (block type)
744 + *
745 + * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
746 + * The smallest block type encoding is always used.
747 + *
748 + * stored:
749 + * 32 bits length in bytes.
750 + *
751 + * fixed:
752 + * magic fixed tree.
753 + * symbols.
754 + *
755 + * dynamic:
756 + * dynamic tree encoding.
757 + * symbols.
758 + *
759 + *
760 + * The buffer for decompression in place is the length of the
761 + * uncompressed data, plus a small amount extra to keep the algorithm safe.
762 + * The compressed data is placed at the end of the buffer. The output
763 + * pointer is placed at the start of the buffer and the input pointer
764 + * is placed where the compressed data starts. Problems will occur
765 + * when the output pointer overruns the input pointer.
766 + *
767 + * The output pointer can only overrun the input pointer if the input
768 + * pointer is moving faster than the output pointer. A condition only
769 + * triggered by data whose compressed form is larger than the uncompressed
770 + * form.
771 + *
772 + * The worst case at the block level is a growth of the compressed data
773 + * of 5 bytes per 32767 bytes.
774 + *
775 + * The worst case internal to a compressed block is very hard to figure.
776 + * The worst case can at least be boundined by having one bit that represents
777 + * 32764 bytes and then all of the rest of the bytes representing the very
778 + * very last byte.
779 + *
780 + * All of which is enough to compute an amount of extra data that is required
781 + * to be safe. To avoid problems at the block level allocating 5 extra bytes
782 + * per 32767 bytes of data is sufficient. To avoind problems internal to a block
783 + * adding an extra 32767 bytes (the worst case uncompressed block size) is
784 + * sufficient, to ensure that in the worst case the decompressed data for
785 + * block will stop the byte before the compressed data for a block begins.
786 + * To avoid problems with the compressed data's meta information an extra 18
787 + * bytes are needed. Leading to the formula:
788 + *
789 + * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
790 + *
791 + * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
792 + * Adding 32768 instead of 32767 just makes for round numbers.
793 + * Adding the decompressor_size is necessary as it musht live after all
794 + * of the data as well. Last I measured the decompressor is about 14K.
795 + * 10K of actuall data and 4K of bss.
796 + *
797 + */
798 +
799 +/*
800 + * gzip declarations
801 + */
802 +
803 +#define OF(args) args
804 +#define STATIC static
805 +
806 +#undef memcpy
807 +
808 +typedef unsigned char uch;
809 +typedef unsigned short ush;
810 +typedef unsigned long ulg;
811 +
812 +#define WSIZE 0x80000000 /* Window size must be at least 32k,
813 + * and a power of two
814 + * We don't actually have a window just
815 + * a huge output buffer so I report
816 + * a 2G windows size, as that should
817 + * always be larger than our output buffer.
818 + */
819 +
820 +static uch *inbuf; /* input buffer */
821 +static uch *window; /* Sliding window buffer, (and final output buffer) */
822 +
823 +static unsigned insize; /* valid bytes in inbuf */
824 +static unsigned inptr; /* index of next byte to be processed in inbuf */
825 +static unsigned long workspace;
826 +
827 +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
828 +
829 +/* Diagnostic functions */
830 +#ifdef DEBUG
831 +# define Assert(cond,msg) {if(!(cond)) error(msg);}
832 +# define Trace(x) fprintf x
833 +# define Tracev(x) {if (verbose) fprintf x ;}
834 +# define Tracevv(x) {if (verbose>1) fprintf x ;}
835 +# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
836 +# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
837 +#else
838 +# define Assert(cond,msg)
839 +# define Trace(x)
840 +# define Tracev(x)
841 +# define Tracevv(x)
842 +# define Tracec(c,x)
843 +# define Tracecv(c,x)
844 +#endif
845 +
846 +static int fill_inbuf(void);
847 +
848 +/*
849 + * This is set up by the setup-routine at boot-time
850 + */
851 +static unsigned char *real_mode; /* Pointer to real-mode data */
852 +extern unsigned char input_data[];
853 +extern int input_len;
854 +
855 +static void error(char *x);
856 +static void *memcpy(void *dest, const void *src, unsigned n);
857 +
858 +#ifdef CONFIG_X86_NUMAQ
859 +void *xquad_portio;
860 +#endif
861 +
862 +static void* memcpy(void* dest, const void* src, unsigned n)
863 +{
864 + int i;
865 + char *d = (char *)dest, *s = (char *)src;
866 +
867 + for (i=0;i<n;i++) d[i] = s[i];
868 + return dest;
869 +}
870 +
871 +/* ===========================================================================
872 + * Fill the input buffer. This is called only when the buffer is empty
873 + * and at least one byte is really needed.
874 + */
875 +static int fill_inbuf(void)
876 +{
877 + error("ran out of input data");
878 + return 0;
879 +}
880 +
881 +
882 +// When using LZMA in callback, the compressed length is not needed.
883 +// Otherwise you need a special version of lzma compression program
884 +// which will pad the compressed length in the header.
885 +#define _LZMA_IN_CB
886 +#include "LzmaDecode.h"
887 +#include "LzmaDecode.c"
888 +
889 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize);
890 +
891 +
892 +/*
893 + * Do the lzma decompression
894 + * When using LZMA in callback, the end of input stream is automatically determined
895 + */
896 +static int lzma_unzip(void)
897 +{
898 +
899 + unsigned int i; /* temp value */
900 + unsigned int lc; /* literal context bits */
901 + unsigned int lp; /* literal pos state bits */
902 + unsigned int pb; /* pos state bits */
903 + unsigned int uncompressedSize = 0;
904 + unsigned char* p;
905 +
906 + ILzmaInCallback callback;
907 + callback.Read = read_byte;
908 +
909 + /* lzma args */
910 + i = get_byte();
911 + lc = i % 9, i = i / 9;
912 + lp = i % 5, pb = i / 5;
913 +
914 + /* skip dictionary size */
915 + for (i = 0; i < 4; i++)
916 + get_byte();
917 + // get uncompressedSize
918 + p= (char*)&uncompressedSize;
919 + for (i = 0; i < 4; i++)
920 + *p++ = get_byte();
921 +
922 + //get compressedSize
923 + for (i = 0; i < 4; i++)
924 + get_byte();
925 +
926 + // point it beyond uncompresedSize
927 + //workspace = window + uncompressedSize;
928 +
929 + /* decompress kernel */
930 + if (LzmaDecode((unsigned char*)workspace, ~0, lc, lp, pb, &callback,
931 + (unsigned char*)window, uncompressedSize, &i) == LZMA_RESULT_OK)
932 + return 0;
933 + else
934 + return 1;
935 +}
936 +
937 +
938 +#ifdef _LZMA_IN_CB
939 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
940 +{
941 + static unsigned int i = 0;
942 + static unsigned char val;
943 + *bufferSize = 1;
944 + val = get_byte();
945 + *buffer = &val;
946 + return LZMA_RESULT_OK;
947 +}
948 +#endif
949 +
950 +static void error(char *x)
951 +{
952 + while(1); /* Halt */
953 +}
954 +
955 +asmlinkage void decompress_kernel(void *rmode, unsigned long end,
956 + uch *input_data, unsigned long input_len, uch *output)
957 +{
958 + real_mode = rmode;
959 +
960 + window = output;
961 + inbuf = input_data; /* Input buffer */
962 + insize = input_len;
963 + inptr = 0;
964 +
965 + if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
966 + error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
967 + if ((workspace = end) > ((-__PAGE_OFFSET-(512 <<20)-1) & 0x7fffffff))
968 + error("Destination address too large");
969 +#ifndef CONFIG_RELOCATABLE
970 + if ((u32)output != LOAD_PHYSICAL_ADDR)
971 + error("Wrong destination address");
972 +#endif
973 +
974 + lzma_unzip();
975 + return;
976 +}
977 --- a/scripts/Makefile.lib
978 +++ b/scripts/Makefile.lib
979 @@ -172,4 +172,9 @@
980 quiet_cmd_gzip = GZIP $@
981 cmd_gzip = gzip -f -9 < $< > $@
982
983 -
984 +# LZMA
985 +#
986 +quiet_cmd_lzma = LZMA $@
987 +cmd_lzma = bash -e scripts/lzma_kern $< $@ -lc7 -lp0 -pb0
988 +# to use lzmacomp,
989 +# cmd_lzma = lzmacomp $< 700 > $@
990 --- /dev/null
991 +++ b/scripts/lzma_kern
992 @@ -0,0 +1,4 @@
993 +get-size() { echo "$5" ;}
994 +printf -v len '%.8x' "$(get-size $(ls -l "$1"))"
995 +lzma e "$@"
996 +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"
997 --- a/arch/x86/boot/compressed/Makefile
998 +++ b/arch/x86/boot/compressed/Makefile
999 @@ -4,7 +4,7 @@
1000 # create a compressed vmlinux image from the original vmlinux
1001 #
1002
1003 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_$(BITS).o misc.o piggy.o
1004 +targets := vmlinux vmlinux.bin vmlinux.bin.lzma head_$(BITS).o lzma_misc.o piggy.o
1005
1006 KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2
1007 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC
1008 @@ -18,7 +18,7 @@
1009 LDFLAGS := -m elf_$(UTS_MACHINE)
1010 LDFLAGS_vmlinux := -T
1011
1012 -$(obj)/vmlinux: $(src)/vmlinux_$(BITS).lds $(obj)/head_$(BITS).o $(obj)/misc.o $(obj)/piggy.o FORCE
1013 +$(obj)/vmlinux: $(src)/vmlinux_$(BITS).lds $(obj)/head_$(BITS).o $(obj)/lzma_misc.o $(obj)/piggy.o FORCE
1014 $(call if_changed,ld)
1015 @:
1016
1017 @@ -44,11 +44,11 @@
1018 $(call if_changed,relocbin)
1019
1020 ifdef CONFIG_RELOCATABLE
1021 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
1022 - $(call if_changed,gzip)
1023 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
1024 + $(call if_changed,lzma)
1025 else
1026 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
1027 - $(call if_changed,gzip)
1028 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
1029 + $(call if_changed,lzma)
1030 endif
1031 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
1032
1033 @@ -60,5 +60,5 @@
1034 endif
1035
1036
1037 -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
1038 +$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.lzma FORCE
1039 $(call if_changed,ld)
This page took 0.097271 seconds and 5 git commands to generate.