"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 2ab9403 | 2017-10-05 15:33:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * uncompress for busybox -- (c) 2002 Robert Griebl |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * based on the original compress42.c source |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 6 | * (see disclaimer below) |
| 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 ...] |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 24 | */ |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 26 | #include "bb_archive.h" |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 27 | |
| 28 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 29 | /* Default input buffer size */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [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 */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 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 | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 73 | IF_DESKTOP(long long) int FAST_FUNC |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 74 | unpack_Z_stream(transformer_state_t *xstate) |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 75 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 76 | IF_DESKTOP(long long total_written = 0;) |
| 77 | IF_DESKTOP(long long) int retval = -1; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 78 | unsigned char *stackp; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 79 | int finchar; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 80 | long oldcode; |
| 81 | long incode; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 82 | int inbits; |
| 83 | int posbits; |
| 84 | int outpos; |
| 85 | int insize; |
| 86 | int bitmask; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 87 | long free_ent; |
| 88 | long maxcode; |
| 89 | long maxmaxcode; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 90 | int n_bits; |
| 91 | int rsize = 0; |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 92 | unsigned char *inbuf; /* were eating insane amounts of stack - */ |
| 93 | unsigned char *outbuf; /* bad for some embedded targets */ |
| 94 | unsigned char *htab; |
| 95 | unsigned short *codetab; |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 96 | |
| 97 | /* Hmm, these were statics - why?! */ |
| 98 | /* user settable max # bits/code */ |
| 99 | int maxbits; /* = BITS; */ |
| 100 | /* block compress mode -C compatible with 2.0 */ |
| 101 | int block_mode; /* = BLOCK_MODE; */ |
| 102 | |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 103 | if (check_signature16(xstate, COMPRESS_MAGIC)) |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 104 | return -1; |
| 105 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 106 | inbuf = xzalloc(IBUFSIZ + 64); |
| 107 | outbuf = xzalloc(OBUFSIZ + 2048); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 108 | htab = xzalloc(HSIZE); /* wasn't zeroed out before, maybe can xmalloc? */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 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) */ |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 115 | if (full_read(xstate->src_fd, inbuf, 1) != 1) { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | c71547c | 2012-12-05 01:04:54 +0100 | [diff] [blame] | 143 | { |
| 144 | int i; |
| 145 | for (i = 255; i >= 0; --i) |
| 146 | tab_suffixof(i) = (unsigned char) i; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 147 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 148 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 149 | do { |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 150 | resetbuf: |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 151 | { |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 152 | int i; |
| 153 | int e; |
| 154 | int o; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 156 | o = posbits >> 3; |
| 157 | e = insize - o; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 158 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 159 | for (i = 0; i < e; ++i) |
| 160 | inbuf[i] = inbuf[i + o]; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 161 | |
| 162 | insize = e; |
| 163 | posbits = 0; |
| 164 | } |
| 165 | |
Bernhard Reutner-Fischer | 5d72546 | 2006-09-06 15:28:32 +0000 | [diff] [blame] | 166 | if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 167 | rsize = safe_read(xstate->src_fd, inbuf + insize, IBUFSIZ); |
Denys Vlasenko | 251fc70 | 2011-08-18 14:29:41 +0200 | [diff] [blame] | 168 | if (rsize < 0) |
Denys Vlasenko | a04e4c2 | 2011-08-22 04:59:41 +0200 | [diff] [blame] | 169 | bb_error_msg_and_die(bb_msg_read_error); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 170 | insize += rsize; |
| 171 | } |
| 172 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 173 | inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 : |
| 174 | (insize << 3) - (n_bits - 1)); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 175 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 176 | while (inbits > posbits) { |
Denys Vlasenko | c71547c | 2012-12-05 01:04:54 +0100 | [diff] [blame] | 177 | long code; |
| 178 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 179 | if (free_ent > maxcode) { |
| 180 | posbits = |
| 181 | ((posbits - 1) + |
| 182 | ((n_bits << 3) - |
| 183 | (posbits - 1 + (n_bits << 3)) % (n_bits << 3))); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 184 | ++n_bits; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 185 | if (n_bits == maxbits) { |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 186 | maxcode = maxmaxcode; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 187 | } else { |
| 188 | maxcode = MAXCODE(n_bits) - 1; |
| 189 | } |
| 190 | bitmask = (1 << n_bits) - 1; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 191 | goto resetbuf; |
| 192 | } |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 193 | { |
| 194 | unsigned char *p = &inbuf[posbits >> 3]; |
Denys Vlasenko | c71547c | 2012-12-05 01:04:54 +0100 | [diff] [blame] | 195 | code = ((p[0] |
| 196 | | ((long) (p[1]) << 8) |
| 197 | | ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 198 | } |
| 199 | posbits += n_bits; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 200 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 201 | if (oldcode == -1) { |
Denys Vlasenko | 251fc70 | 2011-08-18 14:29:41 +0200 | [diff] [blame] | 202 | if (code >= 256) |
| 203 | bb_error_msg_and_die("corrupted data"); /* %ld", code); */ |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 204 | oldcode = code; |
| 205 | finchar = (int) oldcode; |
| 206 | outbuf[outpos++] = (unsigned char) finchar; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 207 | continue; |
| 208 | } |
| 209 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 210 | if (code == CLEAR && block_mode) { |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 211 | clear_tab_prefixof(); |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 212 | free_ent = FIRST - 1; |
| 213 | posbits = |
| 214 | ((posbits - 1) + |
| 215 | ((n_bits << 3) - |
| 216 | (posbits - 1 + (n_bits << 3)) % (n_bits << 3))); |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 217 | n_bits = INIT_BITS; |
| 218 | maxcode = MAXCODE(INIT_BITS) - 1; |
| 219 | bitmask = (1 << INIT_BITS) - 1; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 220 | goto resetbuf; |
| 221 | } |
| 222 | |
| 223 | incode = code; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 224 | stackp = de_stack; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 225 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 226 | /* Special case for KwKwK string. */ |
| 227 | if (code >= free_ent) { |
| 228 | if (code > free_ent) { |
Denys Vlasenko | 4d5955e | 2012-12-05 11:08:30 +0100 | [diff] [blame] | 229 | /* |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 230 | unsigned char *p; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 231 | |
| 232 | posbits -= n_bits; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 233 | p = &inbuf[posbits >> 3]; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 234 | bb_error_msg |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 235 | ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 236 | insize, posbits, p[-1], p[0], p[1], p[2], p[3], |
| 237 | (posbits & 07)); |
Denys Vlasenko | a846117 | 2012-12-05 01:06:05 +0100 | [diff] [blame] | 238 | */ |
Denys Vlasenko | ccb88a6 | 2010-05-27 02:22:54 +0200 | [diff] [blame] | 239 | bb_error_msg("corrupted data"); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 240 | goto err; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 243 | *--stackp = (unsigned char) finchar; |
| 244 | code = oldcode; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 247 | /* Generate output characters in reverse order */ |
Denys Vlasenko | c71547c | 2012-12-05 01:04:54 +0100 | [diff] [blame] | 248 | while (code >= 256) { |
Denys Vlasenko | 251fc70 | 2011-08-18 14:29:41 +0200 | [diff] [blame] | 249 | if (stackp <= &htabof(0)) |
| 250 | bb_error_msg_and_die("corrupted data"); |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 251 | *--stackp = tab_suffixof(code); |
| 252 | code = tab_prefixof(code); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 255 | finchar = tab_suffixof(code); |
| 256 | *--stackp = (unsigned char) finchar; |
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 | /* And put them out in forward order */ |
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 | int i; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 261 | |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 262 | i = de_stack - stackp; |
| 263 | if (outpos + i >= OBUFSIZ) { |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 264 | do { |
| 265 | if (i > OBUFSIZ - outpos) { |
| 266 | i = OBUFSIZ - outpos; |
| 267 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 268 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 269 | if (i > 0) { |
| 270 | memcpy(outbuf + outpos, stackp, i); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 271 | outpos += i; |
| 272 | } |
| 273 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 274 | if (outpos >= OBUFSIZ) { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 275 | xtransformer_write(xstate, outbuf, outpos); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 276 | IF_DESKTOP(total_written += outpos;) |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 277 | outpos = 0; |
| 278 | } |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 279 | stackp += i; |
Denis Vlasenko | 447b543 | 2007-01-05 19:49:02 +0000 | [diff] [blame] | 280 | i = de_stack - stackp; |
| 281 | } while (i > 0); |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 282 | } else { |
| 283 | memcpy(outbuf + outpos, stackp, i); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 284 | outpos += i; |
| 285 | } |
| 286 | } |
| 287 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 288 | /* Generate the new entry. */ |
Denys Vlasenko | c71547c | 2012-12-05 01:04:54 +0100 | [diff] [blame] | 289 | if (free_ent < maxmaxcode) { |
| 290 | tab_prefixof(free_ent) = (unsigned short) oldcode; |
| 291 | tab_suffixof(free_ent) = (unsigned char) finchar; |
| 292 | free_ent++; |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 293 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 294 | |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 295 | /* Remember previous code. */ |
| 296 | oldcode = incode; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 297 | } |
Glenn L McGrath | fedbfe4 | 2002-11-28 09:09:47 +0000 | [diff] [blame] | 298 | } while (rsize > 0); |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 299 | |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 300 | if (outpos > 0) { |
Denys Vlasenko | b4c11c1 | 2014-12-07 00:44:00 +0100 | [diff] [blame] | 301 | xtransformer_write(xstate, outbuf, outpos); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 302 | IF_DESKTOP(total_written += outpos;) |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 303 | } |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 304 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 305 | retval = IF_DESKTOP(total_written) + 0; |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 306 | err: |
| 307 | free(inbuf); |
| 308 | free(outbuf); |
| 309 | free(htab); |
| 310 | free(codetab); |
| 311 | return retval; |
Robert Griebl | 94a6a95 | 2002-05-19 19:00:14 +0000 | [diff] [blame] | 312 | } |