1 1.1 mrg /* $FreeBSD: head/usr.bin/gzip/unpack.c 194579 2009-06-21 09:39:43Z delphij $ */ 2 1.4 simonb /* $NetBSD: unpack.c,v 1.4 2023/06/10 04:45:25 simonb Exp $ */ 3 1.1 mrg 4 1.1 mrg /*- 5 1.1 mrg * Copyright (c) 2009 Xin LI <delphij (at) FreeBSD.org> 6 1.1 mrg * All rights reserved. 7 1.1 mrg * 8 1.1 mrg * Redistribution and use in source and binary forms, with or without 9 1.1 mrg * modification, are permitted provided that the following conditions 10 1.1 mrg * are met: 11 1.1 mrg * 1. Redistributions of source code must retain the above copyright 12 1.1 mrg * notice, this list of conditions and the following disclaimer. 13 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 mrg * notice, this list of conditions and the following disclaimer in the 15 1.1 mrg * documentation and/or other materials provided with the distribution. 16 1.1 mrg * 17 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 1.1 mrg * SUCH DAMAGE. 28 1.1 mrg */ 29 1.1 mrg 30 1.1 mrg /* This file is #included by gzip.c */ 31 1.1 mrg 32 1.1 mrg /* 33 1.1 mrg * pack(1) file format: 34 1.1 mrg * 35 1.1 mrg * The first 7 bytes is the header: 36 1.1 mrg * 00, 01 - Signature (US, RS), we already validated it earlier. 37 1.1 mrg * 02..05 - Uncompressed size 38 1.1 mrg * 06 - Level for the huffman tree (<=24) 39 1.1 mrg * 40 1.1 mrg * pack(1) will then store symbols (leaf) nodes count in each huffman 41 1.1 mrg * tree levels, each level would consume 1 byte (See [1]). 42 1.1 mrg * 43 1.1 mrg * After the symbol count table, there is the symbol table, storing 44 1.2 mrg * symbols represented by corresponding leaf node. EOB is not being 45 1.1 mrg * explicitly transmitted (not necessary anyway) in the symbol table. 46 1.1 mrg * 47 1.1 mrg * Compressed data goes after the symbol table. 48 1.1 mrg * 49 1.1 mrg * NOTES 50 1.1 mrg * 51 1.1 mrg * [1] If we count EOB into the symbols, that would mean that we will 52 1.1 mrg * have at most 256 symbols in the huffman tree. pack(1) rejects empty 53 1.1 mrg * file and files that just repeats one character, which means that we 54 1.1 mrg * will have at least 2 symbols. Therefore, pack(1) would reduce the 55 1.1 mrg * last level symbol count by 2 which makes it a number in 56 1.1 mrg * range [0..254], so all levels' symbol count would fit into 1 byte. 57 1.1 mrg */ 58 1.1 mrg 59 1.1 mrg #define PACK_HEADER_LENGTH 7 60 1.1 mrg #define HTREE_MAXLEVEL 24 61 1.1 mrg 62 1.1 mrg /* 63 1.1 mrg * unpack descriptor 64 1.1 mrg * 65 1.2 mrg * Represent the huffman tree in a similar way that pack(1) would 66 1.1 mrg * store in a packed file. We store all symbols in a linear table, 67 1.1 mrg * and store pointers to each level's first symbol. In addition to 68 1.1 mrg * that, maintain two counts for each level: inner nodes count and 69 1.1 mrg * leaf nodes count. 70 1.1 mrg */ 71 1.1 mrg typedef struct { 72 1.1 mrg int symbol_size; /* Size of the symbol table */ 73 1.1 mrg int treelevels; /* Levels for the huffman tree */ 74 1.1 mrg 75 1.1 mrg int *symbolsin; /* Table of leaf symbols count in 76 1.1 mrg each level */ 77 1.1 mrg int *inodesin; /* Table of internal nodes count in 78 1.1 mrg each level */ 79 1.1 mrg 80 1.1 mrg char *symbol; /* The symbol table */ 81 1.1 mrg char *symbol_eob; /* Pointer to the EOB symbol */ 82 1.1 mrg char **tree; /* Decoding huffman tree (pointers to 83 1.1 mrg first symbol of each tree level */ 84 1.1 mrg 85 1.1 mrg off_t uncompressed_size; /* Uncompressed size */ 86 1.1 mrg FILE *fpIn; /* Input stream */ 87 1.1 mrg FILE *fpOut; /* Output stream */ 88 1.1 mrg } unpack_descriptor_t; 89 1.1 mrg 90 1.1 mrg /* 91 1.1 mrg * Release resource allocated to an unpack descriptor. 92 1.1 mrg * 93 1.1 mrg * Caller is responsible to make sure that all of these pointers are 94 1.1 mrg * initialized (in our case, they all point to valid memory block). 95 1.1 mrg * We don't zero out pointers here because nobody else would ever 96 1.2 mrg * reference the memory block without scrubbing them. 97 1.1 mrg */ 98 1.1 mrg static void 99 1.1 mrg unpack_descriptor_fini(unpack_descriptor_t *unpackd) 100 1.1 mrg { 101 1.1 mrg 102 1.1 mrg free(unpackd->symbolsin); 103 1.1 mrg free(unpackd->inodesin); 104 1.1 mrg free(unpackd->symbol); 105 1.1 mrg free(unpackd->tree); 106 1.1 mrg 107 1.1 mrg fclose(unpackd->fpIn); 108 1.1 mrg fclose(unpackd->fpOut); 109 1.1 mrg } 110 1.1 mrg 111 1.1 mrg /* 112 1.1 mrg * Recursively fill the internal node count table 113 1.1 mrg */ 114 1.1 mrg static void 115 1.1 mrg unpackd_fill_inodesin(const unpack_descriptor_t *unpackd, int level) 116 1.1 mrg { 117 1.1 mrg 118 1.1 mrg /* 119 1.1 mrg * The internal nodes would be 1/2 of total internal nodes and 120 1.1 mrg * leaf nodes in the next level. For the last level there 121 1.2 mrg * would be no internal node by definition. 122 1.1 mrg */ 123 1.1 mrg if (level < unpackd->treelevels) { 124 1.1 mrg unpackd_fill_inodesin(unpackd, level + 1); 125 1.1 mrg unpackd->inodesin[level] = (unpackd->inodesin[level + 1] + 126 1.1 mrg unpackd->symbolsin[level + 1]) / 2; 127 1.1 mrg } else 128 1.1 mrg unpackd->inodesin[level] = 0; 129 1.1 mrg } 130 1.1 mrg 131 1.1 mrg /* 132 1.1 mrg * Update counter for accepted bytes 133 1.1 mrg */ 134 1.1 mrg static void 135 1.1 mrg accepted_bytes(off_t *bytes_in, off_t newbytes) 136 1.1 mrg { 137 1.1 mrg 138 1.1 mrg if (bytes_in != NULL) 139 1.1 mrg (*bytes_in) += newbytes; 140 1.1 mrg } 141 1.1 mrg 142 1.1 mrg /* 143 1.1 mrg * Read file header and construct the tree. Also, prepare the buffered I/O 144 1.2 mrg * for decode routine. 145 1.1 mrg * 146 1.1 mrg * Return value is uncompressed size. 147 1.1 mrg */ 148 1.1 mrg static void 149 1.1 mrg unpack_parse_header(int in, int out, char *pre, size_t prelen, off_t *bytes_in, 150 1.1 mrg unpack_descriptor_t *unpackd) 151 1.1 mrg { 152 1.1 mrg unsigned char hdr[PACK_HEADER_LENGTH]; /* buffer for header */ 153 1.1 mrg ssize_t bytesread; /* Bytes read from the file */ 154 1.1 mrg int i, j, thisbyte; 155 1.1 mrg 156 1.1 mrg /* Prepend the header buffer if we already read some data */ 157 1.1 mrg if (prelen != 0) 158 1.1 mrg memcpy(hdr, pre, prelen); 159 1.1 mrg 160 1.1 mrg /* Read in and fill the rest bytes of header */ 161 1.1 mrg bytesread = read(in, hdr + prelen, PACK_HEADER_LENGTH - prelen); 162 1.1 mrg if (bytesread < 0) 163 1.1 mrg maybe_err("Error reading pack header"); 164 1.3 mrg infile_newdata(bytesread); 165 1.1 mrg 166 1.1 mrg accepted_bytes(bytes_in, PACK_HEADER_LENGTH); 167 1.1 mrg 168 1.1 mrg /* Obtain uncompressed length (bytes 2,3,4,5)*/ 169 1.1 mrg unpackd->uncompressed_size = 0; 170 1.1 mrg for (i = 2; i <= 5; i++) { 171 1.1 mrg unpackd->uncompressed_size <<= 8; 172 1.1 mrg unpackd->uncompressed_size |= hdr[i]; 173 1.1 mrg } 174 1.1 mrg 175 1.1 mrg /* Get the levels of the tree */ 176 1.1 mrg unpackd->treelevels = hdr[6]; 177 1.1 mrg if (unpackd->treelevels > HTREE_MAXLEVEL || unpackd->treelevels < 1) 178 1.1 mrg maybe_errx("Huffman tree has insane levels"); 179 1.1 mrg 180 1.1 mrg /* Let libc take care for buffering from now on */ 181 1.1 mrg if ((unpackd->fpIn = fdopen(in, "r")) == NULL) 182 1.1 mrg maybe_err("Can not fdopen() input stream"); 183 1.1 mrg if ((unpackd->fpOut = fdopen(out, "w")) == NULL) 184 1.1 mrg maybe_err("Can not fdopen() output stream"); 185 1.1 mrg 186 1.1 mrg /* Allocate for the tables of bounds and the tree itself */ 187 1.1 mrg unpackd->inodesin = 188 1.1 mrg calloc(unpackd->treelevels, sizeof(*(unpackd->inodesin))); 189 1.1 mrg unpackd->symbolsin = 190 1.1 mrg calloc(unpackd->treelevels, sizeof(*(unpackd->symbolsin))); 191 1.1 mrg unpackd->tree = 192 1.1 mrg calloc(unpackd->treelevels, (sizeof (*(unpackd->tree)))); 193 1.1 mrg if (unpackd->inodesin == NULL || unpackd->symbolsin == NULL || 194 1.1 mrg unpackd->tree == NULL) 195 1.1 mrg maybe_err("calloc"); 196 1.1 mrg 197 1.1 mrg /* We count from 0 so adjust to match array upper bound */ 198 1.1 mrg unpackd->treelevels--; 199 1.1 mrg 200 1.2 mrg /* Read the levels symbol count table and calculate total */ 201 1.1 mrg unpackd->symbol_size = 1; /* EOB */ 202 1.1 mrg for (i = 0; i <= unpackd->treelevels; i++) { 203 1.1 mrg if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) 204 1.1 mrg maybe_err("File appears to be truncated"); 205 1.1 mrg unpackd->symbolsin[i] = (unsigned char)thisbyte; 206 1.1 mrg unpackd->symbol_size += unpackd->symbolsin[i]; 207 1.1 mrg } 208 1.1 mrg accepted_bytes(bytes_in, unpackd->treelevels); 209 1.1 mrg if (unpackd->symbol_size > 256) 210 1.1 mrg maybe_errx("Bad symbol table"); 211 1.3 mrg infile_newdata(unpackd->treelevels); 212 1.1 mrg 213 1.1 mrg /* Allocate for the symbol table, point symbol_eob at the beginning */ 214 1.1 mrg unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size); 215 1.1 mrg if (unpackd->symbol == NULL) 216 1.1 mrg maybe_err("calloc"); 217 1.1 mrg 218 1.1 mrg /* 219 1.1 mrg * Read in the symbol table, which contain [2, 256] symbols. 220 1.1 mrg * In order to fit the count in one byte, pack(1) would offset 221 1.1 mrg * it by reducing 2 from the actual number from the last level. 222 1.1 mrg * 223 1.1 mrg * We adjust the last level's symbol count by 1 here, because 224 1.1 mrg * the EOB symbol is not being transmitted explicitly. Another 225 1.1 mrg * adjustment would be done later afterward. 226 1.1 mrg */ 227 1.1 mrg unpackd->symbolsin[unpackd->treelevels]++; 228 1.1 mrg for (i = 0; i <= unpackd->treelevels; i++) { 229 1.1 mrg unpackd->tree[i] = unpackd->symbol_eob; 230 1.1 mrg for (j = 0; j < unpackd->symbolsin[i]; j++) { 231 1.1 mrg if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) 232 1.1 mrg maybe_errx("Symbol table truncated"); 233 1.1 mrg *unpackd->symbol_eob++ = (char)thisbyte; 234 1.1 mrg } 235 1.3 mrg infile_newdata(unpackd->symbolsin[i]); 236 1.1 mrg accepted_bytes(bytes_in, unpackd->symbolsin[i]); 237 1.1 mrg } 238 1.1 mrg 239 1.1 mrg /* Now, take account for the EOB symbol as well */ 240 1.1 mrg unpackd->symbolsin[unpackd->treelevels]++; 241 1.1 mrg 242 1.1 mrg /* 243 1.1 mrg * The symbolsin table has been constructed now. 244 1.2 mrg * Calculate the internal nodes count table based on it. 245 1.1 mrg */ 246 1.1 mrg unpackd_fill_inodesin(unpackd, 0); 247 1.1 mrg } 248 1.1 mrg 249 1.1 mrg /* 250 1.1 mrg * Decode huffman stream, based on the huffman tree. 251 1.1 mrg */ 252 1.1 mrg static void 253 1.1 mrg unpack_decode(const unpack_descriptor_t *unpackd, off_t *bytes_in) 254 1.1 mrg { 255 1.1 mrg int thislevel, thiscode, thisbyte, inlevelindex; 256 1.1 mrg int i; 257 1.1 mrg off_t bytes_out = 0; 258 1.1 mrg const char *thissymbol; /* The symbol pointer decoded from stream */ 259 1.1 mrg 260 1.1 mrg /* 261 1.1 mrg * Decode huffman. Fetch every bytes from the file, get it 262 1.1 mrg * into 'thiscode' bit-by-bit, then output the symbol we got 263 1.1 mrg * when one has been found. 264 1.1 mrg * 265 1.1 mrg * Assumption: sizeof(int) > ((max tree levels + 1) / 8). 266 1.1 mrg * bad things could happen if not. 267 1.1 mrg */ 268 1.1 mrg thislevel = 0; 269 1.1 mrg thiscode = thisbyte = 0; 270 1.1 mrg 271 1.1 mrg while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) { 272 1.1 mrg accepted_bytes(bytes_in, 1); 273 1.3 mrg infile_newdata(1); 274 1.3 mrg check_siginfo(); 275 1.1 mrg 276 1.1 mrg /* 277 1.1 mrg * Split one bit from thisbyte, from highest to lowest, 278 1.1 mrg * feed the bit into thiscode, until we got a symbol from 279 1.1 mrg * the tree. 280 1.1 mrg */ 281 1.1 mrg for (i = 7; i >= 0; i--) { 282 1.1 mrg thiscode = (thiscode << 1) | ((thisbyte >> i) & 1); 283 1.1 mrg 284 1.1 mrg /* Did we got a symbol? (referencing leaf node) */ 285 1.1 mrg if (thiscode >= unpackd->inodesin[thislevel]) { 286 1.1 mrg inlevelindex = 287 1.1 mrg thiscode - unpackd->inodesin[thislevel]; 288 1.1 mrg if (inlevelindex > unpackd->symbolsin[thislevel]) 289 1.1 mrg maybe_errx("File corrupt"); 290 1.1 mrg 291 1.1 mrg thissymbol = 292 1.1 mrg &(unpackd->tree[thislevel][inlevelindex]); 293 1.1 mrg if ((thissymbol == unpackd->symbol_eob) && 294 1.1 mrg (bytes_out == unpackd->uncompressed_size)) 295 1.1 mrg goto finished; 296 1.1 mrg 297 1.1 mrg fputc((*thissymbol), unpackd->fpOut); 298 1.1 mrg bytes_out++; 299 1.1 mrg 300 1.1 mrg /* Prepare for next input */ 301 1.1 mrg thislevel = 0; thiscode = 0; 302 1.1 mrg } else { 303 1.1 mrg thislevel++; 304 1.1 mrg if (thislevel > unpackd->treelevels) 305 1.1 mrg maybe_errx("File corrupt"); 306 1.1 mrg } 307 1.1 mrg } 308 1.1 mrg } 309 1.1 mrg 310 1.1 mrg finished: 311 1.1 mrg if (bytes_out != unpackd->uncompressed_size) 312 1.1 mrg maybe_errx("Premature EOF"); 313 1.1 mrg } 314 1.1 mrg 315 1.1 mrg /* Handler for pack(1)'ed file */ 316 1.1 mrg static off_t 317 1.1 mrg unpack(int in, int out, char *pre, size_t prelen, off_t *bytes_in) 318 1.1 mrg { 319 1.1 mrg unpack_descriptor_t unpackd; 320 1.1 mrg 321 1.1 mrg unpack_parse_header(dup(in), dup(out), pre, prelen, bytes_in, &unpackd); 322 1.1 mrg unpack_decode(&unpackd, bytes_in); 323 1.1 mrg unpack_descriptor_fini(&unpackd); 324 1.1 mrg 325 1.1 mrg /* If we reached here, the unpack was successful */ 326 1.1 mrg return (unpackd.uncompressed_size); 327 1.1 mrg } 328 1.1 mrg 329