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