diff options
author | Rob Landley <rob@landley.net> | 2006-06-20 22:38:00 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-06-20 22:38:00 +0000 |
commit | b13fee4cbb98a20ae761da46b02b58cb47f289ab (patch) | |
tree | f7909ad87f1bc06d9d02c6589ae646a013d35558 | |
parent | 5483de1cb71b9cac095e4f6b65196b5390c06560 (diff) | |
download | busybox-w32-b13fee4cbb98a20ae761da46b02b58cb47f289ab.tar.gz busybox-w32-b13fee4cbb98a20ae761da46b02b58cb47f289ab.tar.bz2 busybox-w32-b13fee4cbb98a20ae761da46b02b58cb47f289ab.zip |
Since rangecoder is just a bunch of C functions, move it into the one user
(decompress_unlzma.c). Also a slight #include cleanup, and I've been meaning
to put #include <unistd.h> into libbb.h since it's so darn common...
-rw-r--r-- | archival/libunarchive/decompress_unlzma.c | 164 | ||||
-rw-r--r-- | archival/libunarchive/rangecoder.h | 147 | ||||
-rw-r--r-- | include/libbb.h | 1 |
3 files changed, 145 insertions, 167 deletions
diff --git a/archival/libunarchive/decompress_unlzma.c b/archival/libunarchive/decompress_unlzma.c index fa7b37c37..10883581c 100644 --- a/archival/libunarchive/decompress_unlzma.c +++ b/archival/libunarchive/decompress_unlzma.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* vi:set ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Small lzma deflate implementation. | 3 | * Small lzma deflate implementation. |
3 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> | 4 | * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
@@ -5,31 +6,154 @@ | |||
5 | * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) | 6 | * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
6 | * Copyright (C) 1999-2005 Igor Pavlov | 7 | * Copyright (C) 1999-2005 Igor Pavlov |
7 | * | 8 | * |
8 | * This program is free software; you can redistribute it and/or | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with this library; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | 10 | */ |
22 | 11 | ||
23 | #include <stdint.h> | ||
24 | #include <unistd.h> | ||
25 | #include <stdio.h> | ||
26 | #include <byteswap.h> | ||
27 | |||
28 | #include "libbb.h" | 12 | #include "libbb.h" |
29 | |||
30 | #include "unarchive.h" | 13 | #include "unarchive.h" |
31 | 14 | ||
32 | #include "rangecoder.h" | 15 | #ifdef CONFIG_FEATURE_LZMA_FAST |
16 | # define speed_inline ATTRIBUTE_ALWAYS_INLINE | ||
17 | #else | ||
18 | # define speed_inline | ||
19 | #endif | ||
20 | |||
21 | |||
22 | typedef struct { | ||
23 | int fd; | ||
24 | uint8_t *ptr; | ||
25 | uint8_t *buffer; | ||
26 | uint8_t *buffer_end; | ||
27 | int buffer_size; | ||
28 | uint32_t code; | ||
29 | uint32_t range; | ||
30 | uint32_t bound; | ||
31 | } rc_t; | ||
32 | |||
33 | |||
34 | #define RC_TOP_BITS 24 | ||
35 | #define RC_MOVE_BITS 5 | ||
36 | #define RC_MODEL_TOTAL_BITS 11 | ||
37 | |||
38 | |||
39 | /* Called twice: once at startup and once in rc_normalize() */ | ||
40 | static void rc_read(rc_t * rc) | ||
41 | { | ||
42 | rc->buffer_size = read(rc->fd, rc->buffer, rc->buffer_size); | ||
43 | if (rc->buffer_size <= 0) | ||
44 | bb_error_msg_and_die("unexpected EOF"); | ||
45 | rc->ptr = rc->buffer; | ||
46 | rc->buffer_end = rc->buffer + rc->buffer_size; | ||
47 | } | ||
48 | |||
49 | /* Called once */ | ||
50 | static void rc_init(rc_t * rc, int fd, int buffer_size) | ||
51 | { | ||
52 | int i; | ||
53 | |||
54 | rc->fd = fd; | ||
55 | rc->buffer = xmalloc(buffer_size); | ||
56 | rc->buffer_size = buffer_size; | ||
57 | rc->buffer_end = rc->buffer + rc->buffer_size; | ||
58 | rc->ptr = rc->buffer_end; | ||
59 | |||
60 | rc->code = 0; | ||
61 | rc->range = 0xFFFFFFFF; | ||
62 | for (i = 0; i < 5; i++) { | ||
63 | if (rc->ptr >= rc->buffer_end) | ||
64 | rc_read(rc); | ||
65 | rc->code = (rc->code << 8) | *rc->ptr++; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | /* Called once. TODO: bb_maybe_free() */ | ||
70 | static ATTRIBUTE_ALWAYS_INLINE void rc_free(rc_t * rc) | ||
71 | { | ||
72 | if (ENABLE_FEATURE_CLEAN_UP) | ||
73 | free(rc->buffer); | ||
74 | } | ||
75 | |||
76 | /* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */ | ||
77 | static void rc_do_normalize(rc_t * rc) | ||
78 | { | ||
79 | if (rc->ptr >= rc->buffer_end) | ||
80 | rc_read(rc); | ||
81 | rc->range <<= 8; | ||
82 | rc->code = (rc->code << 8) | *rc->ptr++; | ||
83 | } | ||
84 | static ATTRIBUTE_ALWAYS_INLINE void rc_normalize(rc_t * rc) | ||
85 | { | ||
86 | if (rc->range < (1 << RC_TOP_BITS)) { | ||
87 | rc_do_normalize(rc); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | /* Called 9 times */ | ||
92 | /* Why rc_is_bit_0_helper exists? | ||
93 | * Because we want to always expose (rc->code < rc->bound) to optimizer | ||
94 | */ | ||
95 | static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p) | ||
96 | { | ||
97 | rc_normalize(rc); | ||
98 | rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS); | ||
99 | return rc->bound; | ||
100 | } | ||
101 | static ATTRIBUTE_ALWAYS_INLINE int rc_is_bit_0(rc_t * rc, uint16_t * p) | ||
102 | { | ||
103 | uint32_t t = rc_is_bit_0_helper(rc, p); | ||
104 | return rc->code < t; | ||
105 | } | ||
106 | |||
107 | /* Called ~10 times, but very small, thus inlined */ | ||
108 | static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p) | ||
109 | { | ||
110 | rc->range = rc->bound; | ||
111 | *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS; | ||
112 | } | ||
113 | static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p) | ||
114 | { | ||
115 | rc->range -= rc->bound; | ||
116 | rc->code -= rc->bound; | ||
117 | *p -= *p >> RC_MOVE_BITS; | ||
118 | } | ||
119 | |||
120 | /* Called 4 times in unlzma loop */ | ||
121 | static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol) | ||
122 | { | ||
123 | if (rc_is_bit_0(rc, p)) { | ||
124 | rc_update_bit_0(rc, p); | ||
125 | *symbol *= 2; | ||
126 | return 0; | ||
127 | } else { | ||
128 | rc_update_bit_1(rc, p); | ||
129 | *symbol = *symbol * 2 + 1; | ||
130 | return 1; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | /* Called once */ | ||
135 | static ATTRIBUTE_ALWAYS_INLINE int rc_direct_bit(rc_t * rc) | ||
136 | { | ||
137 | rc_normalize(rc); | ||
138 | rc->range >>= 1; | ||
139 | if (rc->code >= rc->range) { | ||
140 | rc->code -= rc->range; | ||
141 | return 1; | ||
142 | } | ||
143 | return 0; | ||
144 | } | ||
145 | |||
146 | /* Called twice */ | ||
147 | static speed_inline void | ||
148 | rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol) | ||
149 | { | ||
150 | int i = num_levels; | ||
151 | |||
152 | *symbol = 1; | ||
153 | while (i--) | ||
154 | rc_get_bit(rc, p + *symbol, symbol); | ||
155 | *symbol -= 1 << num_levels; | ||
156 | } | ||
33 | 157 | ||
34 | 158 | ||
35 | typedef struct { | 159 | typedef struct { |
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 | |||
11 | typedef 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() */ | ||
29 | static 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 */ | ||
39 | static 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() */ | ||
59 | static 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() */ | ||
66 | static 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 | } | ||
73 | static 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 | */ | ||
84 | static 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 | } | ||
90 | static 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 */ | ||
97 | static 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 | } | ||
102 | static 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 */ | ||
110 | static 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 */ | ||
124 | static 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 */ | ||
136 | static speed_inline void | ||
137 | rc_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: */ | ||
diff --git a/include/libbb.h b/include/libbb.h index bc886dab9..998bcc6f3 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <sys/time.h> | 24 | #include <sys/time.h> |
25 | #include <sys/types.h> | 25 | #include <sys/types.h> |
26 | #include <termios.h> | 26 | #include <termios.h> |
27 | #include <unistd.h> | ||
27 | 28 | ||
28 | #ifdef CONFIG_SELINUX | 29 | #ifdef CONFIG_SELINUX |
29 | #include <selinux/selinux.h> | 30 | #include <selinux/selinux.h> |