Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: third_party/lzma_sdk/Bcj2.c

Issue 10152012: Second attempt to update lzma_sdk to 9.20 (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/lzma_sdk/Bcj2.h ('k') | third_party/lzma_sdk/Bra.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // BranchX86_2.c 1 /* Bcj2.c -- Converter for x86 code (BCJ2)
2 2008-10-04 : Igor Pavlov : Public domain */
2 3
3 #include "BranchX86_2.h" 4 #include "Bcj2.h"
4
5 #include "../../Alloc.h"
6 5
7 #ifdef _LZMA_PROB32 6 #ifdef _LZMA_PROB32
8 #define CProb UInt32 7 #define CProb UInt32
9 #else 8 #else
10 #define CProb UInt16 9 #define CProb UInt16
11 #endif 10 #endif
12 11
13 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80) 12 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
14 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1)) 13 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
15 14
16 #define kNumTopBits 24 15 #define kNumTopBits 24
17 #define kTopValue ((UInt32)1 << kNumTopBits) 16 #define kTopValue ((UInt32)1 << kNumTopBits)
18 17
19 #define kNumBitModelTotalBits 11 18 #define kNumBitModelTotalBits 11
20 #define kBitModelTotal (1 << kNumBitModelTotalBits) 19 #define kBitModelTotal (1 << kNumBitModelTotalBits)
21 #define kNumMoveBits 5 20 #define kNumMoveBits 5
22 21
23 #define RC_READ_BYTE (*Buffer++) 22 #define RC_READ_BYTE (*buffer++)
23 #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
24 #define RC_INIT2 code = 0; range = 0xFFFFFFFF; \
25 { int i; for (i = 0; i < 5; i++) { RC_TEST; code = (code << 8) | RC_READ_BYTE; }}
24 26
25 #define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \ 27 #define NORMALIZE if (range < kTopValue) { RC_TEST; range <<= 8; code = (code << 8) | RC_READ_BYTE; }
26 { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
27 28
28 #define RC_TEST { if (Buffer == BufferLim) return BCJ2_RESULT_DATA_ERROR; } 29 #define IF_BIT_0(p) ttt = *(p); bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
30 #define UPDATE_0(p) range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
31 #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
29 32
30 #define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + buffer Size; RC_INIT2 33 int Bcj2_Decode(
31 34 const Byte *buf0, SizeT size0,
32 #define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; } 35 const Byte *buf1, SizeT size1,
33 36 const Byte *buf2, SizeT size2,
34 #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound) 37 const Byte *buf3, SizeT size3,
35 #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMove Bits;
36 #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveB its;
37 // #define UpdateBit0(p) Range = bound; *(p) = (CProb)(*(p) + ((kBitModelTotal - *(p)) >> kNumMoveBits));
38 // #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) = (CProb)(*(p) - (* (p) >> kNumMoveBits));
39
40 int x86_2_Decode(
41 const Byte *buf0, SizeT size0,
42 const Byte *buf1, SizeT size1,
43 const Byte *buf2, SizeT size2,
44 const Byte *buf3, SizeT size3,
45 Byte *outBuf, SizeT outSize) 38 Byte *outBuf, SizeT outSize)
46 { 39 {
47 CProb p[256 + 2]; 40 CProb p[256 + 2];
48 SizeT inPos = 0, outPos = 0; 41 SizeT inPos = 0, outPos = 0;
49 42
50 const Byte *Buffer, *BufferLim; 43 const Byte *buffer, *bufferLim;
51 UInt32 Range, Code; 44 UInt32 range, code;
52 Byte prevByte = 0; 45 Byte prevByte = 0;
53 46
54 unsigned int i; 47 unsigned int i;
55 for (i = 0; i < sizeof(p) / sizeof(p[0]); i++) 48 for (i = 0; i < sizeof(p) / sizeof(p[0]); i++)
56 p[i] = kBitModelTotal >> 1; 49 p[i] = kBitModelTotal >> 1;
57 RC_INIT(buf3, size3); 50
51 buffer = buf3;
52 bufferLim = buffer + size3;
53 RC_INIT2
58 54
59 if (outSize == 0) 55 if (outSize == 0)
60 return BCJ2_RESULT_OK; 56 return SZ_OK;
61 57
62 for (;;) 58 for (;;)
63 { 59 {
64 Byte b; 60 Byte b;
65 CProb *prob; 61 CProb *prob;
66 UInt32 bound; 62 UInt32 bound;
63 UInt32 ttt;
67 64
68 SizeT limit = size0 - inPos; 65 SizeT limit = size0 - inPos;
69 if (outSize - outPos < limit) 66 if (outSize - outPos < limit)
70 limit = outSize - outPos; 67 limit = outSize - outPos;
71 while (limit != 0) 68 while (limit != 0)
72 { 69 {
73 Byte b = buf0[inPos]; 70 Byte b = buf0[inPos];
74 outBuf[outPos++] = b; 71 outBuf[outPos++] = b;
75 if (IsJ(prevByte, b)) 72 if (IsJ(prevByte, b))
76 break; 73 break;
77 inPos++; 74 inPos++;
78 prevByte = b; 75 prevByte = b;
79 limit--; 76 limit--;
80 } 77 }
81 78
82 if (limit == 0 || outPos == outSize) 79 if (limit == 0 || outPos == outSize)
83 break; 80 break;
84 81
85 b = buf0[inPos++]; 82 b = buf0[inPos++];
86 83
87 if (b == 0xE8) 84 if (b == 0xE8)
88 prob = p + prevByte; 85 prob = p + prevByte;
89 else if (b == 0xE9) 86 else if (b == 0xE9)
90 prob = p + 256; 87 prob = p + 256;
91 else 88 else
92 prob = p + 257; 89 prob = p + 257;
93 90
94 IfBit0(prob) 91 IF_BIT_0(prob)
95 { 92 {
96 UpdateBit0(prob) 93 UPDATE_0(prob)
97 prevByte = b; 94 prevByte = b;
98 } 95 }
99 else 96 else
100 { 97 {
101 UInt32 dest; 98 UInt32 dest;
102 const Byte *v; 99 const Byte *v;
103 UpdateBit1(prob) 100 UPDATE_1(prob)
104 if (b == 0xE8) 101 if (b == 0xE8)
105 { 102 {
106 v = buf1; 103 v = buf1;
107 if (size1 < 4) 104 if (size1 < 4)
108 return BCJ2_RESULT_DATA_ERROR; 105 return SZ_ERROR_DATA;
109 buf1 += 4; 106 buf1 += 4;
110 size1 -= 4; 107 size1 -= 4;
111 } 108 }
112 else 109 else
113 { 110 {
114 v = buf2; 111 v = buf2;
115 if (size2 < 4) 112 if (size2 < 4)
116 return BCJ2_RESULT_DATA_ERROR; 113 return SZ_ERROR_DATA;
117 buf2 += 4; 114 buf2 += 4;
118 size2 -= 4; 115 size2 -= 4;
119 } 116 }
120 dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) | 117 dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) |
121 ((UInt32)v[2] << 8) | ((UInt32)v[3])) - ((UInt32)outPos + 4); 118 ((UInt32)v[2] << 8) | ((UInt32)v[3])) - ((UInt32)outPos + 4);
122 outBuf[outPos++] = (Byte)dest; 119 outBuf[outPos++] = (Byte)dest;
123 if (outPos == outSize) 120 if (outPos == outSize)
124 break; 121 break;
125 outBuf[outPos++] = (Byte)(dest >> 8); 122 outBuf[outPos++] = (Byte)(dest >> 8);
126 if (outPos == outSize) 123 if (outPos == outSize)
127 break; 124 break;
128 outBuf[outPos++] = (Byte)(dest >> 16); 125 outBuf[outPos++] = (Byte)(dest >> 16);
129 if (outPos == outSize) 126 if (outPos == outSize)
130 break; 127 break;
131 outBuf[outPos++] = prevByte = (Byte)(dest >> 24); 128 outBuf[outPos++] = prevByte = (Byte)(dest >> 24);
132 } 129 }
133 } 130 }
134 return (outPos == outSize) ? BCJ2_RESULT_OK : BCJ2_RESULT_DATA_ERROR; 131 return (outPos == outSize) ? SZ_OK : SZ_ERROR_DATA;
135 } 132 }
OLDNEW
« no previous file with comments | « third_party/lzma_sdk/Bcj2.h ('k') | third_party/lzma_sdk/Bra.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698