"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 2 | #include "libbb.h" |
| 3 | |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 4 | /* uncompress for busybox -- (c) 2002 Robert Griebl |
| 5 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * based on the original compress42.c source |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 7 | * (see disclaimer below) |
| 8 | */ |
| 9 | |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 10 | /* (N)compress42.c - File compression ala IEEE Computer, Mar 1992. |
| 11 | * |
| 12 | * Authors: |
| 13 | * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) |
| 14 | * Jim McKie (decvax!mcvax!jim) |
| 15 | * Steve Davies (decvax!vax135!petsd!peora!srd) |
| 16 | * Ken Turkowski (decvax!decwrl!turtlevax!ken) |
| 17 | * James A. Woods (decvax!ihnp4!ames!jaw) |
| 18 | * Joe Orost (decvax!vax135!petsd!joe) |
| 19 | * Dave Mack (csu@alembic.acs.com) |
| 20 | * Peter Jannesen, Network Communication Systems |
| 21 | * (peter@ncs.nl) |
| 22 | * |
| 23 | * marc@suse.de : a small security fix for a buffer overflow |
| 24 | * |
| 25 | * [... History snipped ...] |
| 26 | * |
| 27 | */ |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 28 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 29 | /* Default input buffer size */ |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 30 | #define IBUFSIZ 2048 |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 31 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 32 | /* Default output buffer size */ |
| 33 | #define OBUFSIZ 2048 |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 34 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 35 | /* Defines for third byte of header */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 36 | #define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */ |
| 37 | /* Masks 0x20 and 0x40 are free. */ |
| 38 | /* I think 0x20 should mean that there is */ |
| 39 | /* a fourth header byte (for expansion). */ |
| 40 | #define BLOCK_MODE 0x80 /* Block compression if table is full and */ |
| 41 | /* compression rate is dropping flush tables */ |
| 42 | /* the next two codes should not be changed lightly, as they must not */ |
| 43 | /* lie within the contiguous general code space. */ |
| 44 | #define FIRST 257 /* first free entry */ |
| 45 | #define CLEAR 256 /* table clear output code */ |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 47 | #define INIT_BITS 9 /* initial number of bits/code */ |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 48 | |
| 49 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 50 | /* machine variants which require cc -Dmachine: pdp11, z8000, DOS */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 51 | #define HBITS 17 /* 50% occupancy */ |
| 52 | #define HSIZE (1<<HBITS) |
| 53 | #define HMASK (HSIZE-1) /* unused */ |
| 54 | #define HPRIME 9941 /* unused */ |
| 55 | #define BITS 16 |
| 56 | #define BITS_STR "16" |
| 57 | #undef MAXSEG_64K /* unused */ |
| 58 | #define MAXCODE(n) (1L << (n)) |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 60 | #define htabof(i) htab[i] |
| 61 | #define codetabof(i) codetab[i] |
| 62 | #define tab_prefixof(i) codetabof(i) |
| 63 | #define tab_suffixof(i) ((unsigned char *)(htab))[i] |
| 64 | #define de_stack ((unsigned char *)&(htab[HSIZE-1])) |
| 65 | #define clear_tab_prefixof() memset(codetab, 0, 256) |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 66 | |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Decompress stdin to stdout. This routine adapts to the codes in the |
| 69 | * file building the "string" table on-the-fly; requiring no table to |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 70 | * be stored in the compressed file. |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 71 | */ |
| 72 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 73 | USE_DESKTOP(long long) int |
| 74 | uncompress(int fd_in, int fd_out) |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 75 | { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 76 | USE_DESKTOP(long long total_written = 0;) |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 77 | USE_DESKTOP(long long) int retval = -1; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 78 | unsigned char *stackp; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 79 | long code; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 80 | int finchar; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 81 | long oldcode; |
| 82 | long incode; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 83 | int inbits; |
| 84 | int posbits; |
| 85 | int outpos; |
| 86 | int insize; |
| 87 | int bitmask; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 88 | long free_ent; |
| 89 | long maxcode; |
| 90 | long maxmaxcode; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 91 | int n_bits; |
| 92 | int rsize = 0; |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 93 | unsigned char *inbuf; /* were eating insane amounts of stack - */ |
| 94 | unsigned char *outbuf; /* bad for some embedded targets */ |
| 95 | unsigned char *htab; |
| 96 | unsigned short *codetab; |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 97 | |
| 98 | /* Hmm, these were statics - why?! */ |
| 99 | /* user settable max # bits/code */ |
| 100 | int maxbits; /* = BITS; */ |
| 101 | /* block compress mode -C compatible with 2.0 */ |
| 102 | int block_mode; /* = BLOCK_MODE; */ |
| 103 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 104 | inbuf = xzalloc(IBUFSIZ + 64); |
| 105 | outbuf = xzalloc(OBUFSIZ + 2048); |
| 106 | htab = xzalloc(HSIZE); /* wsn't zeroed out before, maybe can xmalloc? */ |
| 107 | codetab = xzalloc(HSIZE * sizeof(codetab[0])); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 108 | |
| 109 | insize = 0; |
| 110 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 111 | /* xread isn't good here, we have to return - caller may want |
| 112 | * to do some cleanup (e.g. delete incomplete unpacked file etc) */ |
| 113 | if (full_read(fd_in, inbuf, 1) != 1) { |
| 114 | bb_error_msg("short read"); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 115 | goto err; |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 116 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 117 | |
| 118 | maxbits = inbuf[0] & BIT_MASK; |
| 119 | block_mode = inbuf[0] & BLOCK_MODE; |
| 120 | maxmaxcode = MAXCODE(maxbits); |
| 121 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 122 | if (maxbits > BITS) { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 123 | bb_error_msg("compressed with %d bits, can only handle " |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 124 | BITS_STR" bits", maxbits); |
| 125 | goto err; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 128 | n_bits = INIT_BITS; |
| 129 | maxcode = MAXCODE(INIT_BITS) - 1; |
| 130 | bitmask = (1 << INIT_BITS) - 1; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 131 | oldcode = -1; |
| 132 | finchar = 0; |
| 133 | outpos = 0; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 134 | posbits = 0 << 3; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 135 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 136 | free_ent = ((block_mode) ? FIRST : 256); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 137 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 138 | /* As above, initialize the first 256 entries in the table. */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 139 | /*clear_tab_prefixof(); - done by xzalloc */ |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 140 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 141 | for (code = 255; code >= 0; --code) { |
| 142 | tab_suffixof(code) = (unsigned char) code; |
| 143 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 144 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 145 | do { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 146 | resetbuf: |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 147 | { |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 148 | int i; |
| 149 | int e; |
| 150 | int o; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 151 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 152 | o = posbits >> 3; |
| 153 | e = insize - o; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 154 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 155 | for (i = 0; i < e; ++i) |
| 156 | inbuf[i] = inbuf[i + o]; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 157 | |
| 158 | insize = e; |
| 159 | posbits = 0; |
| 160 | } |
| 161 | |
Bernhard Reutner-Fischer | 5d72546 | 2006-09-06 15:28:32 +0000 | [diff] [blame] | 162 | if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) { |
Glenn L McGrath | 1a2d75f | 2003-11-21 22:17:28 +0000 | [diff] [blame] | 163 | rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ); |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 164 | //error check?? |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 165 | insize += rsize; |
| 166 | } |
| 167 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 168 | inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 : |
| 169 | (insize << 3) - (n_bits - 1)); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 170 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 171 | while (inbits > posbits) { |
| 172 | if (free_ent > maxcode) { |
| 173 | posbits = |
| 174 | ((posbits - 1) + |
| 175 | ((n_bits << 3) - |
| 176 | (posbits - 1 + (n_bits << 3)) % (n_bits << 3))); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 177 | ++n_bits; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 178 | if (n_bits == maxbits) { |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 179 | maxcode = maxmaxcode; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 180 | } else { |
| 181 | maxcode = MAXCODE(n_bits) - 1; |
| 182 | } |
| 183 | bitmask = (1 << n_bits) - 1; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 184 | goto resetbuf; |
| 185 | } |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 186 | { |
| 187 | unsigned char *p = &inbuf[posbits >> 3]; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 188 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 189 | code = ((((long) (p[0])) | ((long) (p[1]) << 8) | |
| 190 | ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 191 | } |
| 192 | posbits += n_bits; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 193 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 194 | |
| 195 | if (oldcode == -1) { |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 196 | oldcode = code; |
| 197 | finchar = (int) oldcode; |
| 198 | outbuf[outpos++] = (unsigned char) finchar; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 199 | continue; |
| 200 | } |
| 201 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 202 | if (code == CLEAR && block_mode) { |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 203 | clear_tab_prefixof(); |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 204 | free_ent = FIRST - 1; |
| 205 | posbits = |
| 206 | ((posbits - 1) + |
| 207 | ((n_bits << 3) - |
| 208 | (posbits - 1 + (n_bits << 3)) % (n_bits << 3))); |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 209 | n_bits = INIT_BITS; |
| 210 | maxcode = MAXCODE(INIT_BITS) - 1; |
| 211 | bitmask = (1 << INIT_BITS) - 1; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 212 | goto resetbuf; |
| 213 | } |
| 214 | |
| 215 | incode = code; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 216 | stackp = de_stack; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 217 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 218 | /* Special case for KwKwK string. */ |
| 219 | if (code >= free_ent) { |
| 220 | if (code > free_ent) { |
| 221 | unsigned char *p; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 222 | |
| 223 | posbits -= n_bits; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 224 | p = &inbuf[posbits >> 3]; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 225 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | bb_error_msg |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 227 | ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)", |
| 228 | insize, posbits, p[-1], p[0], p[1], p[2], p[3], |
| 229 | (posbits & 07)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 230 | bb_error_msg("uncompress: corrupt input"); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 231 | goto err; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 234 | *--stackp = (unsigned char) finchar; |
| 235 | code = oldcode; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 238 | /* Generate output characters in reverse order */ |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 239 | while ((long) code >= (long) 256) { |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 240 | *--stackp = tab_suffixof(code); |
| 241 | code = tab_prefixof(code); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 244 | finchar = tab_suffixof(code); |
| 245 | *--stackp = (unsigned char) finchar; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 246 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 247 | /* And put them out in forward order */ |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 248 | { |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 249 | int i; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 250 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 251 | i = de_stack - stackp; |
| 252 | if (outpos + i >= OBUFSIZ) { |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 253 | do { |
| 254 | if (i > OBUFSIZ - outpos) { |
| 255 | i = OBUFSIZ - outpos; |
| 256 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 257 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 258 | if (i > 0) { |
| 259 | memcpy(outbuf + outpos, stackp, i); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 260 | outpos += i; |
| 261 | } |
| 262 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 263 | if (outpos >= OBUFSIZ) { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 264 | full_write(fd_out, outbuf, outpos); |
| 265 | //error check?? |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 266 | USE_DESKTOP(total_written += outpos;) |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 267 | outpos = 0; |
| 268 | } |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 269 | stackp += i; |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 270 | i = de_stack - stackp; |
| 271 | } while (i > 0); |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 272 | } else { |
| 273 | memcpy(outbuf + outpos, stackp, i); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 274 | outpos += i; |
| 275 | } |
| 276 | } |
| 277 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 278 | /* Generate the new entry. */ |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 279 | code = free_ent; |
| 280 | if (code < maxmaxcode) { |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 281 | tab_prefixof(code) = (unsigned short) oldcode; |
| 282 | tab_suffixof(code) = (unsigned char) finchar; |
| 283 | free_ent = code + 1; |
| 284 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 285 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 286 | /* Remember previous code. */ |
| 287 | oldcode = incode; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 290 | } while (rsize > 0); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 291 | |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 292 | if (outpos > 0) { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 293 | full_write(fd_out, outbuf, outpos); |
| 294 | //error check?? |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 295 | USE_DESKTOP(total_written += outpos;) |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 296 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 297 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 298 | retval = USE_DESKTOP(total_written) + 0; |
| 299 | err: |
| 300 | free(inbuf); |
| 301 | free(outbuf); |
| 302 | free(htab); |
| 303 | free(codetab); |
| 304 | return retval; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 305 | } |