rev |
line source |
pascal@3214
|
1 --- busybox-1.12.0/archival/libunarchive/decompress_unlzma.c
|
pascal@3214
|
2 +++ busybox-1.12.0/archival/libunarchive/decompress_unlzma.c
|
pascal@3214
|
3 @@ -14,8 +14,10 @@
|
pascal@3214
|
4
|
pascal@3214
|
5 #if ENABLE_FEATURE_LZMA_FAST
|
pascal@3214
|
6 # define speed_inline ALWAYS_INLINE
|
pascal@3214
|
7 +# define size_inline
|
pascal@3214
|
8 #else
|
pascal@3214
|
9 # define speed_inline
|
pascal@3214
|
10 +# define size_inline ALWAYS_INLINE
|
pascal@3214
|
11 #endif
|
pascal@3214
|
12
|
pascal@3214
|
13
|
pascal@3214
|
14 @@ -44,8 +46,8 @@
|
pascal@3214
|
15 #define RC_MODEL_TOTAL_BITS 11
|
pascal@3214
|
16
|
pascal@3214
|
17
|
pascal@3214
|
18 -/* Called twice: once at startup and once in rc_normalize() */
|
pascal@3214
|
19 -static void rc_read(rc_t *rc)
|
pascal@3214
|
20 +/* Called twice: once at startup (LZMA_FAST only) and once in rc_normalize() */
|
pascal@3214
|
21 +static size_inline void rc_read(rc_t *rc)
|
pascal@3214
|
22 {
|
pascal@3214
|
23 int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
|
pascal@3214
|
24 if (buffer_size <= 0)
|
pascal@3214
|
25 @@ -54,8 +56,17 @@
|
pascal@3214
|
26 rc->buffer_end = RC_BUFFER + buffer_size;
|
pascal@3214
|
27 }
|
pascal@3214
|
28
|
pascal@3214
|
29 +/* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
|
pascal@3214
|
30 +static void rc_do_normalize(rc_t *rc)
|
pascal@3214
|
31 +{
|
pascal@3214
|
32 + if (rc->ptr >= rc->buffer_end)
|
pascal@3214
|
33 + rc_read(rc);
|
pascal@3214
|
34 + rc->range <<= 8;
|
pascal@3214
|
35 + rc->code = (rc->code << 8) | *rc->ptr++;
|
pascal@3214
|
36 +}
|
pascal@3214
|
37 +
|
pascal@3214
|
38 /* Called once */
|
pascal@3214
|
39 -static rc_t* rc_init(int fd) /*, int buffer_size) */
|
pascal@3214
|
40 +static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
|
pascal@3214
|
41 {
|
pascal@3214
|
42 int i;
|
pascal@3214
|
43 rc_t *rc;
|
pascal@3214
|
44 @@ -63,17 +74,18 @@
|
pascal@3214
|
45 rc = xmalloc(sizeof(*rc) + RC_BUFFER_SIZE);
|
pascal@3214
|
46
|
pascal@3214
|
47 rc->fd = fd;
|
pascal@3214
|
48 - /* rc->buffer_size = buffer_size; */
|
pascal@3214
|
49 - rc->buffer_end = RC_BUFFER + RC_BUFFER_SIZE;
|
pascal@3214
|
50 rc->ptr = rc->buffer_end;
|
pascal@3214
|
51
|
pascal@3214
|
52 - rc->code = 0;
|
pascal@3214
|
53 - rc->range = 0xFFFFFFFF;
|
pascal@3214
|
54 for (i = 0; i < 5; i++) {
|
pascal@3214
|
55 +#if ENABLE_FEATURE_LZMA_FAST
|
pascal@3214
|
56 if (rc->ptr >= rc->buffer_end)
|
pascal@3214
|
57 rc_read(rc);
|
pascal@3214
|
58 rc->code = (rc->code << 8) | *rc->ptr++;
|
pascal@3214
|
59 +#else
|
pascal@3214
|
60 + rc_do_normalize(rc);
|
pascal@3214
|
61 +#endif
|
pascal@3214
|
62 }
|
pascal@3214
|
63 + rc->range = 0xFFFFFFFF;
|
pascal@3214
|
64 return rc;
|
pascal@3214
|
65 }
|
pascal@3214
|
66
|
pascal@3214
|
67 @@ -83,14 +95,6 @@
|
pascal@3214
|
68 free(rc);
|
pascal@3214
|
69 }
|
pascal@3214
|
70
|
pascal@3214
|
71 -/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
|
pascal@3214
|
72 -static void rc_do_normalize(rc_t *rc)
|
pascal@3214
|
73 -{
|
pascal@3214
|
74 - if (rc->ptr >= rc->buffer_end)
|
pascal@3214
|
75 - rc_read(rc);
|
pascal@3214
|
76 - rc->range <<= 8;
|
pascal@3214
|
77 - rc->code = (rc->code << 8) | *rc->ptr++;
|
pascal@3214
|
78 -}
|
pascal@3214
|
79 static ALWAYS_INLINE void rc_normalize(rc_t *rc)
|
pascal@3214
|
80 {
|
pascal@3214
|
81 if (rc->range < (1 << RC_TOP_BITS)) {
|
pascal@3214
|
82 @@ -98,49 +102,30 @@
|
pascal@3214
|
83 }
|
pascal@3214
|
84 }
|
pascal@3214
|
85
|
pascal@3214
|
86 -/* rc_is_bit_0 is called 9 times */
|
pascal@3214
|
87 -/* Why rc_is_bit_0_helper exists?
|
pascal@3214
|
88 - * Because we want to always expose (rc->code < rc->bound) to optimizer.
|
pascal@3214
|
89 - * Thus rc_is_bit_0 is always inlined, and rc_is_bit_0_helper is inlined
|
pascal@3214
|
90 - * only if we compile for speed.
|
pascal@3214
|
91 - */
|
pascal@3214
|
92 -static speed_inline uint32_t rc_is_bit_0_helper(rc_t *rc, uint16_t *p)
|
pascal@3214
|
93 +/* rc_is_bit_1 is called 9 times */
|
pascal@3214
|
94 +static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
|
pascal@3214
|
95 {
|
pascal@3214
|
96 rc_normalize(rc);
|
pascal@3214
|
97 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
|
pascal@3214
|
98 - return rc->bound;
|
pascal@3214
|
99 + if (rc->code < rc->bound) {
|
pascal@3214
|
100 + rc->range = rc->bound;
|
pascal@3214
|
101 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
|
pascal@3214
|
102 + return 0;
|
pascal@3214
|
103 + }
|
pascal@3214
|
104 + else {
|
pascal@3214
|
105 + rc->range -= rc->bound;
|
pascal@3214
|
106 + rc->code -= rc->bound;
|
pascal@3214
|
107 + *p -= *p >> RC_MOVE_BITS;
|
pascal@3214
|
108 + return 1;
|
pascal@3214
|
109 + }
|
pascal@3214
|
110 }
|
pascal@3214
|
111 -static ALWAYS_INLINE int rc_is_bit_0(rc_t *rc, uint16_t *p)
|
pascal@3214
|
112 -{
|
pascal@3214
|
113 - uint32_t t = rc_is_bit_0_helper(rc, p);
|
pascal@3214
|
114 - return rc->code < t;
|
pascal@3214
|
115 -}
|
pascal@3214
|
116
|
pascal@3214
|
117 -/* Called ~10 times, but very small, thus inlined */
|
pascal@3214
|
118 -static speed_inline void rc_update_bit_0(rc_t *rc, uint16_t *p)
|
pascal@3214
|
119 -{
|
pascal@3214
|
120 - rc->range = rc->bound;
|
pascal@3214
|
121 - *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
|
pascal@3214
|
122 -}
|
pascal@3214
|
123 -static speed_inline void rc_update_bit_1(rc_t *rc, uint16_t *p)
|
pascal@3214
|
124 -{
|
pascal@3214
|
125 - rc->range -= rc->bound;
|
pascal@3214
|
126 - rc->code -= rc->bound;
|
pascal@3214
|
127 - *p -= *p >> RC_MOVE_BITS;
|
pascal@3214
|
128 -}
|
pascal@3214
|
129 -
|
pascal@3214
|
130 /* Called 4 times in unlzma loop */
|
pascal@3214
|
131 -static int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
|
pascal@3214
|
132 +static speed_inline int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
|
pascal@3214
|
133 {
|
pascal@3214
|
134 - if (rc_is_bit_0(rc, p)) {
|
pascal@3214
|
135 - rc_update_bit_0(rc, p);
|
pascal@3214
|
136 - *symbol *= 2;
|
pascal@3214
|
137 - return 0;
|
pascal@3214
|
138 - } else {
|
pascal@3214
|
139 - rc_update_bit_1(rc, p);
|
pascal@3214
|
140 - *symbol = *symbol * 2 + 1;
|
pascal@3214
|
141 - return 1;
|
pascal@3214
|
142 - }
|
pascal@3214
|
143 + int ret = rc_is_bit_1(rc, p);
|
pascal@3214
|
144 + *symbol = *symbol * 2 + ret;
|
pascal@3214
|
145 + return ret;
|
pascal@3214
|
146 }
|
pascal@3214
|
147
|
pascal@3214
|
148 /* Called once */
|
pascal@3214
|
149 @@ -266,13 +251,13 @@
|
pascal@3214
|
150 header.dst_size = SWAP_LE64(header.dst_size);
|
pascal@3214
|
151
|
pascal@3214
|
152 if (header.dict_size == 0)
|
pascal@3214
|
153 - header.dict_size = 1;
|
pascal@3214
|
154 + header.dict_size++;
|
pascal@3214
|
155
|
pascal@3214
|
156 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
|
pascal@3214
|
157
|
pascal@3214
|
158 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
|
pascal@3214
|
159 p = xmalloc(num_probs * sizeof(*p));
|
pascal@3214
|
160 - num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
|
pascal@3214
|
161 + num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
|
pascal@3214
|
162 for (i = 0; i < num_probs; i++)
|
pascal@3214
|
163 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
|
pascal@3214
|
164
|
pascal@3214
|
165 @@ -282,9 +267,8 @@
|
pascal@3214
|
166 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
|
pascal@3214
|
167
|
pascal@3214
|
168 prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
|
pascal@3214
|
169 - if (rc_is_bit_0(rc, prob)) {
|
pascal@3214
|
170 + if (!rc_is_bit_1(rc, prob)) {
|
pascal@3214
|
171 mi = 1;
|
pascal@3214
|
172 - rc_update_bit_0(rc, prob);
|
pascal@3214
|
173 prob = (p + LZMA_LITERAL
|
pascal@3214
|
174 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
|
pascal@3214
|
175 + (previous_byte >> (8 - lc))
|
pascal@3214
|
176 @@ -340,26 +324,21 @@
|
pascal@3214
|
177 int offset;
|
pascal@3214
|
178 uint16_t *prob_len;
|
pascal@3214
|
179
|
pascal@3214
|
180 - rc_update_bit_1(rc, prob);
|
pascal@3214
|
181 prob = p + LZMA_IS_REP + state;
|
pascal@3214
|
182 - if (rc_is_bit_0(rc, prob)) {
|
pascal@3214
|
183 - rc_update_bit_0(rc, prob);
|
pascal@3214
|
184 + if (!rc_is_bit_1(rc, prob)) {
|
pascal@3214
|
185 rep3 = rep2;
|
pascal@3214
|
186 rep2 = rep1;
|
pascal@3214
|
187 rep1 = rep0;
|
pascal@3214
|
188 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
|
pascal@3214
|
189 prob = p + LZMA_LEN_CODER;
|
pascal@3214
|
190 } else {
|
pascal@3214
|
191 - rc_update_bit_1(rc, prob);
|
pascal@3214
|
192 - prob = p + LZMA_IS_REP_G0 + state;
|
pascal@3214
|
193 - if (rc_is_bit_0(rc, prob)) {
|
pascal@3214
|
194 - rc_update_bit_0(rc, prob);
|
pascal@3214
|
195 + prob += LZMA_IS_REP_G0 - LZMA_IS_REP;
|
pascal@3214
|
196 + if (!rc_is_bit_1(rc, prob)) {
|
pascal@3214
|
197 prob = (p + LZMA_IS_REP_0_LONG
|
pascal@3214
|
198 + (state << LZMA_NUM_POS_BITS_MAX)
|
pascal@3214
|
199 + pos_state
|
pascal@3214
|
200 );
|
pascal@3214
|
201 - if (rc_is_bit_0(rc, prob)) {
|
pascal@3214
|
202 - rc_update_bit_0(rc, prob);
|
pascal@3214
|
203 + if (!rc_is_bit_1(rc, prob)) {
|
pascal@3214
|
204
|
pascal@3214
|
205 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
|
pascal@3214
|
206 #if ENABLE_FEATURE_LZMA_FAST
|
pascal@3214
|
207 @@ -372,25 +351,16 @@
|
pascal@3214
|
208 len = 1;
|
pascal@3214
|
209 goto string;
|
pascal@3214
|
210 #endif
|
pascal@3214
|
211 - } else {
|
pascal@3214
|
212 - rc_update_bit_1(rc, prob);
|
pascal@3214
|
213 }
|
pascal@3214
|
214 } else {
|
pascal@3214
|
215 uint32_t distance;
|
pascal@3214
|
216
|
pascal@3214
|
217 - rc_update_bit_1(rc, prob);
|
pascal@3214
|
218 - prob = p + LZMA_IS_REP_G1 + state;
|
pascal@3214
|
219 - if (rc_is_bit_0(rc, prob)) {
|
pascal@3214
|
220 - rc_update_bit_0(rc, prob);
|
pascal@3214
|
221 - distance = rep1;
|
pascal@3214
|
222 - } else {
|
pascal@3214
|
223 - rc_update_bit_1(rc, prob);
|
pascal@3214
|
224 - prob = p + LZMA_IS_REP_G2 + state;
|
pascal@3214
|
225 - if (rc_is_bit_0(rc, prob)) {
|
pascal@3214
|
226 - rc_update_bit_0(rc, prob);
|
pascal@3214
|
227 - distance = rep2;
|
pascal@3214
|
228 - } else {
|
pascal@3214
|
229 - rc_update_bit_1(rc, prob);
|
pascal@3214
|
230 + prob += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
|
pascal@3214
|
231 + distance = rep1;
|
pascal@3214
|
232 + if (rc_is_bit_1(rc, prob)) {
|
pascal@3214
|
233 + prob += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
|
pascal@3214
|
234 + distance = rep2;
|
pascal@3214
|
235 + if (rc_is_bit_1(rc, prob)) {
|
pascal@3214
|
236 distance = rep3;
|
pascal@3214
|
237 rep3 = rep2;
|
pascal@3214
|
238 }
|
pascal@3214
|
239 @@ -404,24 +374,20 @@
|
pascal@3214
|
240 }
|
pascal@3214
|
241
|
pascal@3214
|
242 prob_len = prob + LZMA_LEN_CHOICE;
|
pascal@3214
|
243 - if (rc_is_bit_0(rc, prob_len)) {
|
pascal@3214
|
244 - rc_update_bit_0(rc, prob_len);
|
pascal@3214
|
245 - prob_len = (prob + LZMA_LEN_LOW
|
pascal@3214
|
246 - + (pos_state << LZMA_LEN_NUM_LOW_BITS));
|
pascal@3214
|
247 + if (!rc_is_bit_1(rc, prob_len)) {
|
pascal@3214
|
248 + prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
|
pascal@3214
|
249 + + (pos_state << LZMA_LEN_NUM_LOW_BITS);
|
pascal@3214
|
250 offset = 0;
|
pascal@3214
|
251 num_bits = LZMA_LEN_NUM_LOW_BITS;
|
pascal@3214
|
252 } else {
|
pascal@3214
|
253 - rc_update_bit_1(rc, prob_len);
|
pascal@3214
|
254 - prob_len = prob + LZMA_LEN_CHOICE_2;
|
pascal@3214
|
255 - if (rc_is_bit_0(rc, prob_len)) {
|
pascal@3214
|
256 - rc_update_bit_0(rc, prob_len);
|
pascal@3214
|
257 - prob_len = (prob + LZMA_LEN_MID
|
pascal@3214
|
258 - + (pos_state << LZMA_LEN_NUM_MID_BITS));
|
pascal@3214
|
259 + prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
|
pascal@3214
|
260 + if (!rc_is_bit_1(rc, prob_len)) {
|
pascal@3214
|
261 + prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
|
pascal@3214
|
262 + + (pos_state << LZMA_LEN_NUM_MID_BITS);
|
pascal@3214
|
263 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
|
pascal@3214
|
264 num_bits = LZMA_LEN_NUM_MID_BITS;
|
pascal@3214
|
265 } else {
|
pascal@3214
|
266 - rc_update_bit_1(rc, prob_len);
|
pascal@3214
|
267 - prob_len = prob + LZMA_LEN_HIGH;
|
pascal@3214
|
268 + prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
|
pascal@3214
|
269 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
|
pascal@3214
|
270 + (1 << LZMA_LEN_NUM_MID_BITS));
|
pascal@3214
|
271 num_bits = LZMA_LEN_NUM_HIGH_BITS;
|
pascal@3214
|
272 @@ -440,17 +406,18 @@
|
pascal@3214
|
273 << LZMA_NUM_POS_SLOT_BITS);
|
pascal@3214
|
274 rc_bit_tree_decode(rc, prob, LZMA_NUM_POS_SLOT_BITS,
|
pascal@3214
|
275 &pos_slot);
|
pascal@3214
|
276 + rep0 = pos_slot;
|
pascal@3214
|
277 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
|
pascal@3214
|
278 num_bits = (pos_slot >> 1) - 1;
|
pascal@3214
|
279 rep0 = 2 | (pos_slot & 1);
|
pascal@3214
|
280 + prob = p + LZMA_ALIGN;
|
pascal@3214
|
281 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
|
pascal@3214
|
282 rep0 <<= num_bits;
|
pascal@3214
|
283 - prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
|
pascal@3214
|
284 + prob += LZMA_SPEC_POS - LZMA_ALIGN - 1 + rep0 - pos_slot ;
|
pascal@3214
|
285 } else {
|
pascal@3214
|
286 num_bits -= LZMA_NUM_ALIGN_BITS;
|
pascal@3214
|
287 while (num_bits--)
|
pascal@3214
|
288 rep0 = (rep0 << 1) | rc_direct_bit(rc);
|
pascal@3214
|
289 - prob = p + LZMA_ALIGN;
|
pascal@3214
|
290 rep0 <<= LZMA_NUM_ALIGN_BITS;
|
pascal@3214
|
291 num_bits = LZMA_NUM_ALIGN_BITS;
|
pascal@3214
|
292 }
|
pascal@3214
|
293 @@ -461,8 +428,7 @@
|
pascal@3214
|
294 rep0 |= i;
|
pascal@3214
|
295 i <<= 1;
|
pascal@3214
|
296 }
|
pascal@3214
|
297 - } else
|
pascal@3214
|
298 - rep0 = pos_slot;
|
pascal@3214
|
299 + }
|
pascal@3214
|
300 if (++rep0 == 0)
|
pascal@3214
|
301 break;
|
pascal@3214
|
302 }
|