1 diff --git a/third_party/zlib/deflate.c b/third_party/zlib/deflate.c 2 index 5c4022f..88b2ec0 100644 3 --- a/third_party/zlib/deflate.c 4 +++ b/third_party/zlib/deflate.c 5 @@ -70,14 +70,15 @@ typedef enum { 6 finish_done /* finish done, accept no more input or output */ 7 } block_state; 8 9 -typedef block_state (*compress_func) OF((deflate_state *s, int flush)); 10 +typedef block_state (*compress_func) OF((deflate_state *s, int flush, 11 + int clas)); 12 /* Compression function. Returns the block state after the call. */ 13 14 local void fill_window OF((deflate_state *s)); 15 -local block_state deflate_stored OF((deflate_state *s, int flush)); 16 -local block_state deflate_fast OF((deflate_state *s, int flush)); 17 +local block_state deflate_stored OF((deflate_state *s, int flush, int clas)); 18 +local block_state deflate_fast OF((deflate_state *s, int flush, int clas)); 19 #ifndef FASTEST 20 -local block_state deflate_slow OF((deflate_state *s, int flush)); 21 +local block_state deflate_slow OF((deflate_state *s, int flush, int clas)); 22 #endif 23 local block_state deflate_rle OF((deflate_state *s, int flush)); 24 local block_state deflate_huff OF((deflate_state *s, int flush)); 25 @@ -87,9 +88,9 @@ local void flush_pending OF((z_streamp strm)); 26 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); 27 #ifdef ASMV 28 void match_init OF((void)); /* asm code initialization */ 29 - uInt longest_match OF((deflate_state *s, IPos cur_match)); 30 + uInt longest_match OF((deflate_state *s, IPos cur_match, int clas)); 31 #else 32 -local uInt longest_match OF((deflate_state *s, IPos cur_match)); 33 +local uInt longest_match OF((deflate_state *s, IPos cur_match, int clas)); 34 #endif 35 36 #ifdef DEBUG 37 @@ -281,6 +282,9 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 38 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 39 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 40 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 41 + s->class_bitmap = NULL; 42 + zmemzero(&s->cookie_locations, sizeof(s->cookie_locations)); 43 + strm->clas = 0; 44 45 s->high_water = 0; /* nothing written to s->window yet */ 46 47 @@ -367,6 +371,8 @@ int ZEXPORT deflateReset (strm) 48 s = (deflate_state *)strm->state; 49 s->pending = 0; 50 s->pending_out = s->pending_buf; 51 + TRY_FREE(strm, s->class_bitmap); 52 + s->class_bitmap = NULL; 53 54 if (s->wrap < 0) { 55 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ 56 @@ -817,9 +823,26 @@ int ZEXPORT deflate (strm, flush) 57 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { 58 block_state bstate; 59 60 - bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : 61 - (s->strategy == Z_RLE ? deflate_rle(s, flush) : 62 - (*(configuration_table[s->level].func))(s, flush)); 63 + if (strm->clas && s->class_bitmap == NULL) { 64 + /* This is the first time that we have seen alternative class 65 + * data. All data up till this point has been standard class. */ 66 + s->class_bitmap = (Bytef*) ZALLOC(strm, s->w_size/4, sizeof(Byte)); 67 + zmemzero(s->class_bitmap, s->w_size/4); 68 + } 69 + 70 + if (strm->clas && s->strategy == Z_RLE) { 71 + /* We haven't patched deflate_rle. */ 72 + ERR_RETURN(strm, Z_BUF_ERROR); 73 + } 74 + 75 + if (s->strategy == Z_HUFFMAN_ONLY) { 76 + bstate = deflate_huff(s, flush); 77 + } else if (s->strategy == Z_RLE) { 78 + bstate = deflate_rle(s, flush); 79 + } else { 80 + bstate = (*(configuration_table[s->level].func)) 81 + (s, flush, strm->clas); 82 + } 83 84 if (bstate == finish_started || bstate == finish_done) { 85 s->status = FINISH_STATE; 86 @@ -915,6 +938,7 @@ int ZEXPORT deflateEnd (strm) 87 TRY_FREE(strm, strm->state->head); 88 TRY_FREE(strm, strm->state->prev); 89 TRY_FREE(strm, strm->state->window); 90 + TRY_FREE(strm, strm->state->class_bitmap); 91 92 ZFREE(strm, strm->state); 93 strm->state = Z_NULL; 94 @@ -1046,6 +1070,57 @@ local void lm_init (s) 95 #endif 96 } 97 98 +/* class_set sets bits [offset,offset+len) in s->class_bitmap to either 1 (if 99 + * class != 0) or 0 (otherwise). */ 100 +local void class_set(s, offset, len, clas) 101 + deflate_state *s; 102 + IPos offset; 103 + uInt len; 104 + int clas; 105 +{ 106 + IPos byte = offset >> 3; 107 + IPos bit = offset & 7; 108 + Bytef class_byte_value = clas ? 0xff : 0x00; 109 + Bytef class_bit_value = clas ? 1 : 0; 110 + static const Bytef mask[8] = {0xfe, 0xfd, 0xfb, 0xf7, 111 + 0xef, 0xdf, 0xbf, 0x7f}; 112 + 113 + if (bit) { 114 + while (len) { 115 + s->class_bitmap[byte] &= mask[bit]; 116 + s->class_bitmap[byte] |= class_bit_value << bit; 117 + bit++; 118 + len--; 119 + if (bit == 8) { 120 + bit = 0; 121 + byte++; 122 + break; 123 + } 124 + } 125 + } 126 + 127 + while (len >= 8) { 128 + s->class_bitmap[byte++] = class_byte_value; 129 + len -= 8; 130 + } 131 + 132 + while (len) { 133 + s->class_bitmap[byte] &= mask[bit]; 134 + s->class_bitmap[byte] |= class_bit_value << bit; 135 + bit++; 136 + len--; 137 + } 138 +} 139 + 140 +local int class_at(s, window_offset) 141 + deflate_state *s; 142 + IPos window_offset; 143 +{ 144 + IPos byte = window_offset >> 3; 145 + IPos bit = window_offset & 7; 146 + return (s->class_bitmap[byte] >> bit) & 1; 147 +} 148 + 149 #ifndef FASTEST 150 /* =========================================================================== 151 * Set match_start to the longest match starting at the given string and 152 @@ -1060,9 +1135,10 @@ local void lm_init (s) 153 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or 154 * match.S. The code will be functionally equivalent. 155 */ 156 -local uInt longest_match(s, cur_match) 157 +local uInt longest_match(s, cur_match, clas) 158 deflate_state *s; 159 IPos cur_match; /* current match */ 160 + int clas; 161 { 162 unsigned chain_length = s->max_chain_length;/* max hash chain length */ 163 register Bytef *scan = s->window + s->strstart; /* current string */ 164 @@ -1110,6 +1186,9 @@ local uInt longest_match(s, cur_match) 165 do { 166 Assert(cur_match < s->strstart, "no future"); 167 match = s->window + cur_match; 168 + /* If the matched data is in the wrong class, skip it. */ 169 + if (s->class_bitmap && class_at(s, cur_match) != clas) 170 + continue; 171 172 /* Skip to next match if the match length cannot increase 173 * or if the match length is less than 2. Note that the checks below 174 @@ -1152,6 +1231,8 @@ local uInt longest_match(s, cur_match) 175 len = (MAX_MATCH - 1) - (int)(strend-scan); 176 scan = strend - (MAX_MATCH-1); 177 178 +#error "UNALIGNED_OK hasn't been patched." 179 + 180 #else /* UNALIGNED_OK */ 181 182 if (match[best_len] != scan_end || 183 @@ -1168,15 +1249,23 @@ local uInt longest_match(s, cur_match) 184 scan += 2, match++; 185 Assert(*scan == *match, "match[2]?"); 186 187 - /* We check for insufficient lookahead only every 8th comparison; 188 - * the 256th check will be made at strstart+258. 189 - */ 190 - do { 191 - } while (*++scan == *++match && *++scan == *++match && 192 - *++scan == *++match && *++scan == *++match && 193 - *++scan == *++match && *++scan == *++match && 194 - *++scan == *++match && *++scan == *++match && 195 - scan < strend); 196 + if (!s->class_bitmap) { 197 + /* We check for insufficient lookahead only every 8th comparison; 198 + * the 256th check will be made at strstart+258. 199 + */ 200 + do { 201 + } while (*++scan == *++match && *++scan == *++match && 202 + *++scan == *++match && *++scan == *++match && 203 + *++scan == *++match && *++scan == *++match && 204 + *++scan == *++match && *++scan == *++match && 205 + scan < strend); 206 + } else { 207 + /* We have to be mindful of the class of the data and not stray. */ 208 + do { 209 + } while (*++scan == *++match && 210 + class_at(s, match - s->window) == clas && 211 + scan < strend); 212 + } 213 214 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 215 216 @@ -1204,20 +1293,74 @@ local uInt longest_match(s, cur_match) 217 } 218 #endif /* ASMV */ 219 220 +/* cookie_match is a replacement for longest_match in the case of cookie data. 221 + * Here we only wish to match the entire value so trying the partial matches in 222 + * longest_match is both wasteful and often fails to find the correct match. 223 + * 224 + * So we take the djb2 hash of the cookie and look up the last position for a 225 + * match in a special hash table. */ 226 +local uInt cookie_match(s, start, len) 227 + deflate_state *s; 228 + IPos start; 229 + unsigned len; 230 +{ 231 + unsigned hash = 5381; 232 + Bytef *str = s->window + start; 233 + unsigned i; 234 + IPos cookie_location; 235 + 236 + if (len >= MAX_MATCH || len == 0) 237 + return 0; 238 + 239 + for (i = 0; i < len; i++) 240 + hash = ((hash << 5) + hash) + str[i]; 241 + 242 + hash &= Z_COOKIE_HASH_MASK; 243 + cookie_location = s->cookie_locations[hash]; 244 + s->cookie_locations[hash] = start; 245 + s->match_start = 0; 246 + if (cookie_location && 247 + (start - cookie_location) > len && 248 + (start - cookie_location) < MAX_DIST(s) && 249 + len <= s->lookahead) { 250 + for (i = 0; i < len; i++) { 251 + if (s->window[start+i] != s->window[cookie_location+i] || 252 + class_at(s, cookie_location+i) != 1) { 253 + return 0; 254 + } 255 + } 256 + /* Check that we aren't matching a prefix of another cookie by ensuring 257 + * that the final byte is either a semicolon (which cannot appear in a 258 + * cookie value), or non-cookie data. */ 259 + if (s->window[cookie_location+len-1] != ';' && 260 + class_at(s, cookie_location+len) != 0) { 261 + return 0; 262 + } 263 + s->match_start = cookie_location; 264 + return len; 265 + } 266 + 267 + return 0; 268 +} 269 + 270 + 271 #else /* FASTEST */ 272 273 /* --------------------------------------------------------------------------- 274 * Optimized version for FASTEST only 275 */ 276 -local uInt longest_match(s, cur_match) 277 +local uInt longest_match(s, cur_match, clas) 278 deflate_state *s; 279 IPos cur_match; /* current match */ 280 + int clas; 281 { 282 register Bytef *scan = s->window + s->strstart; /* current string */ 283 register Bytef *match; /* matched string */ 284 register int len; /* length of current match */ 285 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 286 287 +#error "This code not patched" 288 + 289 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 290 * It is easy to get rid of this optimization if necessary. 291 */ 292 @@ -1360,6 +1503,21 @@ local void fill_window(s) 293 */ 294 } while (--n); 295 #endif 296 + 297 + for (n = 0; n < Z_COOKIE_HASH_SIZE; n++) { 298 + if (s->cookie_locations[n] > wsize) { 299 + s->cookie_locations[n] -= wsize; 300 + } else { 301 + s->cookie_locations[n] = 0; 302 + } 303 + } 304 + 305 + if (s->class_bitmap) { 306 + zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8, 307 + s->w_size/8); 308 + zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8); 309 + } 310 + 311 more += wsize; 312 } 313 if (s->strm->avail_in == 0) return; 314 @@ -1378,6 +1536,9 @@ local void fill_window(s) 315 Assert(more >= 2, "more < 2"); 316 317 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 318 + if (s->class_bitmap != NULL) { 319 + class_set(s, s->strstart + s->lookahead, n, s->strm->clas); 320 + } 321 s->lookahead += n; 322 323 /* Initialize the hash value now that we have some input: */ 324 @@ -1459,9 +1620,10 @@ local void fill_window(s) 325 * NOTE: this function should be optimized to avoid extra copying from 326 * window to pending_buf. 327 */ 328 -local block_state deflate_stored(s, flush) 329 +local block_state deflate_stored(s, flush, clas) 330 deflate_state *s; 331 int flush; 332 + int clas; 333 { 334 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited 335 * to pending_buf_size, and each stored block has a 5 byte header: 336 @@ -1517,13 +1679,19 @@ local block_state deflate_stored(s, flush) 337 * new strings in the dictionary only for unmatched strings or for short 338 * matches. It is used only for the fast compression options. 339 */ 340 -local block_state deflate_fast(s, flush) 341 +local block_state deflate_fast(s, flush, clas) 342 deflate_state *s; 343 int flush; 344 + int clas; 345 { 346 IPos hash_head; /* head of the hash chain */ 347 int bflush; /* set if current block must be flushed */ 348 349 + if (clas != 0) { 350 + /* We haven't patched this code for alternative class data. */ 351 + return Z_BUF_ERROR; 352 + } 353 + 354 for (;;) { 355 /* Make sure that we always have enough lookahead, except 356 * at the end of the input file. We need MAX_MATCH bytes 357 @@ -1554,7 +1722,7 @@ local block_state deflate_fast(s, flush) 358 * of window index 0 (in particular we have to avoid a match 359 * of the string with itself at the start of the input file). 360 */ 361 - s->match_length = longest_match (s, hash_head); 362 + s->match_length = longest_match (s, hash_head, clas); 363 /* longest_match() sets match_start */ 364 } 365 if (s->match_length >= MIN_MATCH) { 366 @@ -1613,12 +1781,25 @@ local block_state deflate_fast(s, flush) 367 * evaluation for matches: a match is finally adopted only if there is 368 * no better match at the next window position. 369 */ 370 -local block_state deflate_slow(s, flush) 371 +local block_state deflate_slow(s, flush, clas) 372 deflate_state *s; 373 int flush; 374 + int clas; 375 { 376 IPos hash_head; /* head of hash chain */ 377 int bflush; /* set if current block must be flushed */ 378 + uInt input_length ; 379 + int first = 1; /* first says whether this is the first iteration 380 + of the loop, below. */ 381 + 382 + if (clas == Z_CLASS_COOKIE) { 383 + if (s->lookahead) { 384 + /* Alternative class data must always be presented at the beginning 385 + * of a block. */ 386 + return Z_BUF_ERROR; 387 + } 388 + input_length = s->strm->avail_in; 389 + } 390 391 /* Process the input block. */ 392 for (;;) { 393 @@ -1648,13 +1829,18 @@ local block_state deflate_slow(s, flush) 394 s->prev_length = s->match_length, s->prev_match = s->match_start; 395 s->match_length = MIN_MATCH-1; 396 397 - if (hash_head != NIL && s->prev_length < s->max_lazy_match && 398 - s->strstart - hash_head <= MAX_DIST(s)) { 399 + if (clas == Z_CLASS_COOKIE && first) { 400 + s->match_length = cookie_match(s, s->strstart, input_length); 401 + } else if (clas == Z_CLASS_STANDARD && 402 + hash_head != NIL && 403 + s->prev_length < s->max_lazy_match && 404 + s->strstart - hash_head <= MAX_DIST(s)) { 405 /* To simplify the code, we prevent matches with the string 406 * of window index 0 (in particular we have to avoid a match 407 * of the string with itself at the start of the input file). 408 */ 409 - s->match_length = longest_match (s, hash_head); 410 + s->match_length = longest_match (s, hash_head, clas); 411 + 412 /* longest_match() sets match_start */ 413 414 if (s->match_length <= 5 && (s->strategy == Z_FILTERED 415 @@ -1673,7 +1859,20 @@ local block_state deflate_slow(s, flush) 416 /* If there was a match at the previous step and the current 417 * match is not better, output the previous match: 418 */ 419 - if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 420 + first = 0; 421 + if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length && 422 + /* We will only accept an exact match for Z_CLASS_COOKIE data and 423 + * we won't match Z_CLASS_HUFFMAN_ONLY data at all. */ 424 + (clas == Z_CLASS_STANDARD || (clas == Z_CLASS_COOKIE && 425 + s->prev_length == input_length && 426 + s->prev_match > 0 && 427 + /* We require that a Z_CLASS_COOKIE match be 428 + * preceded by either a semicolon (which cannot be 429 + * part of a cookie), or non-cookie data. This is 430 + * to prevent a cookie from being a suffix of 431 + * another. */ 432 + (class_at(s, s->prev_match-1) == Z_CLASS_STANDARD || 433 + *(s->window + s->prev_match-1) == ';')))) { 434 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 435 /* Do not insert strings in hash table beyond this. */ 436 437 diff --git a/third_party/zlib/deflate.h b/third_party/zlib/deflate.h 438 index cbf0d1e..2fe6fd6 100644 439 --- a/third_party/zlib/deflate.h 440 +++ b/third_party/zlib/deflate.h 441 @@ -91,6 +91,9 @@ typedef unsigned IPos; 442 * save space in the various tables. IPos is used only for parameter passing. 443 */ 444 445 +#define Z_COOKIE_HASH_SIZE 256 446 +#define Z_COOKIE_HASH_MASK (Z_COOKIE_HASH_SIZE-1) 447 + 448 typedef struct internal_state { 449 z_streamp strm; /* pointer back to this zlib stream */ 450 int status; /* as the name implies */ 451 @@ -139,6 +142,8 @@ typedef struct internal_state { 452 uInt hash_mask; /* hash_size-1 */ 453 454 uInt hash_shift; 455 + Bytef *class_bitmap; /* bitmap of class for each byte in window */ 456 + IPos cookie_locations[Z_COOKIE_HASH_SIZE]; 457 /* Number of bits by which ins_h must be shifted at each input 458 * step. It must be such that after MIN_MATCH steps, the oldest 459 * byte no longer takes part in the hash key, that is: 460 diff --git a/third_party/zlib/zlib.h b/third_party/zlib/zlib.h 461 index 4d54af9..da7e971 100644 462 --- a/third_party/zlib/zlib.h 463 +++ b/third_party/zlib/zlib.h 464 @@ -101,6 +101,7 @@ typedef struct z_stream_s { 465 int data_type; /* best guess about the data type: binary or text */ 466 uLong adler; /* adler32 value of the uncompressed data */ 467 uLong reserved; /* reserved for future use */ 468 + int clas; 469 } z_stream; 470 471 typedef z_stream FAR *z_streamp; 472 @@ -207,6 +208,10 @@ typedef gz_header FAR *gz_headerp; 473 474 #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ 475 476 +#define Z_CLASS_STANDARD 0 477 +#define Z_CLASS_COOKIE 1 478 +#define Z_CLASS_HUFFMAN_ONLY 2 479 + 480 #define zlib_version zlibVersion() 481 /* for compatibility with versions < 1.0.2 */ 482 483 @@ -1587,6 +1592,13 @@ ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, 484 ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); 485 ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); 486 ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); 487 +# else 488 + ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); 489 + ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); 490 + ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); 491 + ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); 492 + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); 493 + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); 494 # endif 495 #else 496 ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); 497