summaryrefslogtreecommitdiff
path: root/archival/libunarchive/rangecoder.h
diff options
context:
space:
mode:
Diffstat (limited to 'archival/libunarchive/rangecoder.h')
-rw-r--r--archival/libunarchive/rangecoder.h147
1 files changed, 0 insertions, 147 deletions
diff --git a/archival/libunarchive/rangecoder.h b/archival/libunarchive/rangecoder.h
deleted file mode 100644
index b806eebf6..000000000
--- a/archival/libunarchive/rangecoder.h
+++ /dev/null
@@ -1,147 +0,0 @@
1
2#include "libbb.h"
3
4#ifdef CONFIG_FEATURE_LZMA_FAST
5# define speed_inline ATTRIBUTE_ALWAYS_INLINE
6#else
7# define speed_inline
8#endif
9
10
11typedef struct {
12 int fd;
13 uint8_t *ptr;
14 uint8_t *buffer;
15 uint8_t *buffer_end;
16 int buffer_size;
17 uint32_t code;
18 uint32_t range;
19 uint32_t bound;
20} rc_t;
21
22
23#define RC_TOP_BITS 24
24#define RC_MOVE_BITS 5
25#define RC_MODEL_TOTAL_BITS 11
26
27
28/* Called twice: once at startup and once in rc_normalize() */
29static void rc_read(rc_t * rc)
30{
31 rc->buffer_size = read(rc->fd, rc->buffer, rc->buffer_size);
32 if (rc->buffer_size <= 0)
33 bb_error_msg_and_die("unexpected EOF");
34 rc->ptr = rc->buffer;
35 rc->buffer_end = rc->buffer + rc->buffer_size;
36}
37
38/* Called once */
39static void rc_init(rc_t * rc, int fd, int buffer_size)
40{
41 int i;
42
43 rc->fd = fd;
44 rc->buffer = xmalloc(buffer_size);
45 rc->buffer_size = buffer_size;
46 rc->buffer_end = rc->buffer + rc->buffer_size;
47 rc->ptr = rc->buffer_end;
48
49 rc->code = 0;
50 rc->range = 0xFFFFFFFF;
51 for (i = 0; i < 5; i++) {
52 if (rc->ptr >= rc->buffer_end)
53 rc_read(rc);
54 rc->code = (rc->code << 8) | *rc->ptr++;
55 }
56}
57
58/* Called once. TODO: bb_maybe_free() */
59static ATTRIBUTE_ALWAYS_INLINE void rc_free(rc_t * rc)
60{
61 if (ENABLE_FEATURE_CLEAN_UP)
62 free(rc->buffer);
63}
64
65/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
66static void rc_do_normalize(rc_t * rc)
67{
68 if (rc->ptr >= rc->buffer_end)
69 rc_read(rc);
70 rc->range <<= 8;
71 rc->code = (rc->code << 8) | *rc->ptr++;
72}
73static ATTRIBUTE_ALWAYS_INLINE void rc_normalize(rc_t * rc)
74{
75 if (rc->range < (1 << RC_TOP_BITS)) {
76 rc_do_normalize(rc);
77 }
78}
79
80/* Called 9 times */
81/* Why rc_is_bit_0_helper exists?
82 * Because we want to always expose (rc->code < rc->bound) to optimizer
83 */
84static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p)
85{
86 rc_normalize(rc);
87 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
88 return rc->bound;
89}
90static ATTRIBUTE_ALWAYS_INLINE int rc_is_bit_0(rc_t * rc, uint16_t * p)
91{
92 uint32_t t = rc_is_bit_0_helper(rc, p);
93 return rc->code < t;
94}
95
96/* Called ~10 times, but very small, thus inlined */
97static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p)
98{
99 rc->range = rc->bound;
100 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
101}
102static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p)
103{
104 rc->range -= rc->bound;
105 rc->code -= rc->bound;
106 *p -= *p >> RC_MOVE_BITS;
107}
108
109/* Called 4 times in unlzma loop */
110static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol)
111{
112 if (rc_is_bit_0(rc, p)) {
113 rc_update_bit_0(rc, p);
114 *symbol *= 2;
115 return 0;
116 } else {
117 rc_update_bit_1(rc, p);
118 *symbol = *symbol * 2 + 1;
119 return 1;
120 }
121}
122
123/* Called once */
124static ATTRIBUTE_ALWAYS_INLINE int rc_direct_bit(rc_t * rc)
125{
126 rc_normalize(rc);
127 rc->range >>= 1;
128 if (rc->code >= rc->range) {
129 rc->code -= rc->range;
130 return 1;
131 }
132 return 0;
133}
134
135/* Called twice */
136static speed_inline void
137rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol)
138{
139 int i = num_levels;
140
141 *symbol = 1;
142 while (i--)
143 rc_get_bit(rc, p + *symbol, symbol);
144 *symbol -= 1 << num_levels;
145}
146
147/* vi:set ts=4: */