unpack.c (d6d16831c555d9c626ed2b49977241abd0d5f97e) | unpack.c (90f528e8d7868a5d31dc35b24943c673bf67821d) |
---|---|
1/* $FreeBSD$ */ 2/* $NetBSD: unpack.c,v 1.3 2017/08/04 07:27:08 mrg Exp $ */ 3 |
|
1/*- 2 * Copyright (c) 2009 Xin LI <delphij@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 138 unchanged lines hidden (view full) --- 147static void 148unpack_parse_header(int in, int out, char *pre, size_t prelen, off_t *bytes_in, 149 unpack_descriptor_t *unpackd) 150{ 151 unsigned char hdr[PACK_HEADER_LENGTH]; /* buffer for header */ 152 ssize_t bytesread; /* Bytes read from the file */ 153 int i, j, thisbyte; 154 | 4/*- 5 * Copyright (c) 2009 Xin LI <delphij@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright --- 138 unchanged lines hidden (view full) --- 150static void 151unpack_parse_header(int in, int out, char *pre, size_t prelen, off_t *bytes_in, 152 unpack_descriptor_t *unpackd) 153{ 154 unsigned char hdr[PACK_HEADER_LENGTH]; /* buffer for header */ 155 ssize_t bytesread; /* Bytes read from the file */ 156 int i, j, thisbyte; 157 |
158 if (prelen > sizeof hdr) 159 maybe_err("prelen too long"); 160 |
|
155 /* Prepend the header buffer if we already read some data */ 156 if (prelen != 0) 157 memcpy(hdr, pre, prelen); 158 159 /* Read in and fill the rest bytes of header */ 160 bytesread = read(in, hdr + prelen, PACK_HEADER_LENGTH - prelen); 161 if (bytesread < 0) 162 maybe_err("Error reading pack header"); | 161 /* Prepend the header buffer if we already read some data */ 162 if (prelen != 0) 163 memcpy(hdr, pre, prelen); 164 165 /* Read in and fill the rest bytes of header */ 166 bytesread = read(in, hdr + prelen, PACK_HEADER_LENGTH - prelen); 167 if (bytesread < 0) 168 maybe_err("Error reading pack header"); |
169 infile_newdata(bytesread); |
|
163 164 accepted_bytes(bytes_in, PACK_HEADER_LENGTH); 165 166 /* Obtain uncompressed length (bytes 2,3,4,5) */ 167 unpackd->uncompressed_size = 0; 168 for (i = 2; i <= 5; i++) { 169 unpackd->uncompressed_size <<= 8; 170 unpackd->uncompressed_size |= hdr[i]; --- 30 unchanged lines hidden (view full) --- 201 if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) 202 maybe_err("File appears to be truncated"); 203 unpackd->symbolsin[i] = (unsigned char)thisbyte; 204 unpackd->symbol_size += unpackd->symbolsin[i]; 205 } 206 accepted_bytes(bytes_in, unpackd->treelevels); 207 if (unpackd->symbol_size > 256) 208 maybe_errx("Bad symbol table"); | 170 171 accepted_bytes(bytes_in, PACK_HEADER_LENGTH); 172 173 /* Obtain uncompressed length (bytes 2,3,4,5) */ 174 unpackd->uncompressed_size = 0; 175 for (i = 2; i <= 5; i++) { 176 unpackd->uncompressed_size <<= 8; 177 unpackd->uncompressed_size |= hdr[i]; --- 30 unchanged lines hidden (view full) --- 208 if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) 209 maybe_err("File appears to be truncated"); 210 unpackd->symbolsin[i] = (unsigned char)thisbyte; 211 unpackd->symbol_size += unpackd->symbolsin[i]; 212 } 213 accepted_bytes(bytes_in, unpackd->treelevels); 214 if (unpackd->symbol_size > 256) 215 maybe_errx("Bad symbol table"); |
216 infile_newdata(unpackd->treelevels); |
|
209 210 /* Allocate for the symbol table, point symbol_eob at the beginning */ 211 unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size); 212 if (unpackd->symbol == NULL) 213 maybe_err("calloc"); 214 215 /* 216 * Read in the symbol table, which contain [2, 256] symbols. --- 7 unchanged lines hidden (view full) --- 224 unpackd->symbolsin[unpackd->treelevels]++; 225 for (i = 0; i <= unpackd->treelevels; i++) { 226 unpackd->tree[i] = unpackd->symbol_eob; 227 for (j = 0; j < unpackd->symbolsin[i]; j++) { 228 if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) 229 maybe_errx("Symbol table truncated"); 230 *unpackd->symbol_eob++ = (char)thisbyte; 231 } | 217 218 /* Allocate for the symbol table, point symbol_eob at the beginning */ 219 unpackd->symbol_eob = unpackd->symbol = calloc(1, unpackd->symbol_size); 220 if (unpackd->symbol == NULL) 221 maybe_err("calloc"); 222 223 /* 224 * Read in the symbol table, which contain [2, 256] symbols. --- 7 unchanged lines hidden (view full) --- 232 unpackd->symbolsin[unpackd->treelevels]++; 233 for (i = 0; i <= unpackd->treelevels; i++) { 234 unpackd->tree[i] = unpackd->symbol_eob; 235 for (j = 0; j < unpackd->symbolsin[i]; j++) { 236 if ((thisbyte = fgetc(unpackd->fpIn)) == EOF) 237 maybe_errx("Symbol table truncated"); 238 *unpackd->symbol_eob++ = (char)thisbyte; 239 } |
240 infile_newdata(unpackd->symbolsin[i]); |
|
232 accepted_bytes(bytes_in, unpackd->symbolsin[i]); 233 } 234 235 /* Now, take account for the EOB symbol as well */ 236 unpackd->symbolsin[unpackd->treelevels]++; 237 238 /* 239 * The symbolsin table has been constructed now. --- 21 unchanged lines hidden (view full) --- 261 * Assumption: sizeof(int) > ((max tree levels + 1) / 8). 262 * bad things could happen if not. 263 */ 264 thislevel = 0; 265 thiscode = thisbyte = 0; 266 267 while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) { 268 accepted_bytes(bytes_in, 1); | 241 accepted_bytes(bytes_in, unpackd->symbolsin[i]); 242 } 243 244 /* Now, take account for the EOB symbol as well */ 245 unpackd->symbolsin[unpackd->treelevels]++; 246 247 /* 248 * The symbolsin table has been constructed now. --- 21 unchanged lines hidden (view full) --- 270 * Assumption: sizeof(int) > ((max tree levels + 1) / 8). 271 * bad things could happen if not. 272 */ 273 thislevel = 0; 274 thiscode = thisbyte = 0; 275 276 while ((thisbyte = fgetc(unpackd->fpIn)) != EOF) { 277 accepted_bytes(bytes_in, 1); |
278 infile_newdata(1); 279 check_siginfo(); |
|
269 270 /* 271 * Split one bit from thisbyte, from highest to lowest, 272 * feed the bit into thiscode, until we got a symbol from 273 * the tree. 274 */ 275 for (i = 7; i >= 0; i--) { 276 thiscode = (thiscode << 1) | ((thisbyte >> i) & 1); --- 52 unchanged lines hidden --- | 280 281 /* 282 * Split one bit from thisbyte, from highest to lowest, 283 * feed the bit into thiscode, until we got a symbol from 284 * the tree. 285 */ 286 for (i = 7; i >= 0; i--) { 287 thiscode = (thiscode << 1) | ((thisbyte >> i) & 1); --- 52 unchanged lines hidden --- |