Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: third_party/zlib/deflate.c

Issue 10874087: Merge 151720 - net: workaround compression leaks (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1180/src/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/zlib/deflate.h ('k') | third_party/zlib/mixed-source.patch » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* deflate.c -- compress data using the deflation algorithm 1 /* deflate.c -- compress data using the deflation algorithm
2 * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler 2 * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6 /* 6 /*
7 * ALGORITHM 7 * ALGORITHM
8 * 8 *
9 * The "deflation" process depends on being able to identify portions 9 * The "deflation" process depends on being able to identify portions
10 * of the input text which are identical to earlier input (within a 10 * of the input text which are identical to earlier input (within a
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 /* =========================================================================== 63 /* ===========================================================================
64 * Function prototypes. 64 * Function prototypes.
65 */ 65 */
66 typedef enum { 66 typedef enum {
67 need_more, /* block not completed, need more input or more output */ 67 need_more, /* block not completed, need more input or more output */
68 block_done, /* block flush performed */ 68 block_done, /* block flush performed */
69 finish_started, /* finish started, need only more output at next deflate */ 69 finish_started, /* finish started, need only more output at next deflate */
70 finish_done /* finish done, accept no more input or output */ 70 finish_done /* finish done, accept no more input or output */
71 } block_state; 71 } block_state;
72 72
73 typedef block_state (*compress_func) OF((deflate_state *s, int flush)); 73 typedef block_state (*compress_func) OF((deflate_state *s, int flush,
74 int clas));
74 /* Compression function. Returns the block state after the call. */ 75 /* Compression function. Returns the block state after the call. */
75 76
76 local void fill_window OF((deflate_state *s)); 77 local void fill_window OF((deflate_state *s));
77 local block_state deflate_stored OF((deflate_state *s, int flush)); 78 local block_state deflate_stored OF((deflate_state *s, int flush, int clas));
78 local block_state deflate_fast OF((deflate_state *s, int flush)); 79 local block_state deflate_fast OF((deflate_state *s, int flush, int clas));
79 #ifndef FASTEST 80 #ifndef FASTEST
80 local block_state deflate_slow OF((deflate_state *s, int flush)); 81 local block_state deflate_slow OF((deflate_state *s, int flush, int clas));
81 #endif 82 #endif
82 local block_state deflate_rle OF((deflate_state *s, int flush)); 83 local block_state deflate_rle OF((deflate_state *s, int flush));
83 local block_state deflate_huff OF((deflate_state *s, int flush)); 84 local block_state deflate_huff OF((deflate_state *s, int flush));
84 local void lm_init OF((deflate_state *s)); 85 local void lm_init OF((deflate_state *s));
85 local void putShortMSB OF((deflate_state *s, uInt b)); 86 local void putShortMSB OF((deflate_state *s, uInt b));
86 local void flush_pending OF((z_streamp strm)); 87 local void flush_pending OF((z_streamp strm));
87 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); 88 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
88 #ifdef ASMV 89 #ifdef ASMV
89 void match_init OF((void)); /* asm code initialization */ 90 void match_init OF((void)); /* asm code initialization */
90 uInt longest_match OF((deflate_state *s, IPos cur_match)); 91 uInt longest_match OF((deflate_state *s, IPos cur_match, int clas));
91 #else 92 #else
92 local uInt longest_match OF((deflate_state *s, IPos cur_match)); 93 local uInt longest_match OF((deflate_state *s, IPos cur_match, int clas));
93 #endif 94 #endif
94 95
95 #ifdef DEBUG 96 #ifdef DEBUG
96 local void check_match OF((deflate_state *s, IPos start, IPos match, 97 local void check_match OF((deflate_state *s, IPos start, IPos match,
97 int length)); 98 int length));
98 #endif 99 #endif
99 100
100 /* =========================================================================== 101 /* ===========================================================================
101 * Local data 102 * Local data
102 */ 103 */
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 s->w_mask = s->w_size - 1; 275 s->w_mask = s->w_size - 1;
275 276
276 s->hash_bits = memLevel + 7; 277 s->hash_bits = memLevel + 7;
277 s->hash_size = 1 << s->hash_bits; 278 s->hash_size = 1 << s->hash_bits;
278 s->hash_mask = s->hash_size - 1; 279 s->hash_mask = s->hash_size - 1;
279 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); 280 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
280 281
281 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); 282 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
282 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 283 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
283 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 284 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
285 s->class_bitmap = NULL;
286 zmemzero(&s->cookie_locations, sizeof(s->cookie_locations));
287 strm->clas = 0;
284 288
285 s->high_water = 0; /* nothing written to s->window yet */ 289 s->high_water = 0; /* nothing written to s->window yet */
286 290
287 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ 291 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
288 292
289 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); 293 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
290 s->pending_buf = (uchf *) overlay; 294 s->pending_buf = (uchf *) overlay;
291 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); 295 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
292 296
293 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || 297 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 return Z_STREAM_ERROR; 364 return Z_STREAM_ERROR;
361 } 365 }
362 366
363 strm->total_in = strm->total_out = 0; 367 strm->total_in = strm->total_out = 0;
364 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ 368 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
365 strm->data_type = Z_UNKNOWN; 369 strm->data_type = Z_UNKNOWN;
366 370
367 s = (deflate_state *)strm->state; 371 s = (deflate_state *)strm->state;
368 s->pending = 0; 372 s->pending = 0;
369 s->pending_out = s->pending_buf; 373 s->pending_out = s->pending_buf;
374 TRY_FREE(strm, s->class_bitmap);
375 s->class_bitmap = NULL;
370 376
371 if (s->wrap < 0) { 377 if (s->wrap < 0) {
372 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ 378 s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
373 } 379 }
374 s->status = s->wrap ? INIT_STATE : BUSY_STATE; 380 s->status = s->wrap ? INIT_STATE : BUSY_STATE;
375 strm->adler = 381 strm->adler =
376 #ifdef GZIP 382 #ifdef GZIP
377 s->wrap == 2 ? crc32(0L, Z_NULL, 0) : 383 s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
378 #endif 384 #endif
379 adler32(0L, Z_NULL, 0); 385 adler32(0L, Z_NULL, 0);
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 if (s->status == FINISH_STATE && strm->avail_in != 0) { 816 if (s->status == FINISH_STATE && strm->avail_in != 0) {
811 ERR_RETURN(strm, Z_BUF_ERROR); 817 ERR_RETURN(strm, Z_BUF_ERROR);
812 } 818 }
813 819
814 /* Start a new block or continue the current one. 820 /* Start a new block or continue the current one.
815 */ 821 */
816 if (strm->avail_in != 0 || s->lookahead != 0 || 822 if (strm->avail_in != 0 || s->lookahead != 0 ||
817 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { 823 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
818 block_state bstate; 824 block_state bstate;
819 825
820 bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : 826 if (strm->clas && s->class_bitmap == NULL) {
821 (s->strategy == Z_RLE ? deflate_rle(s, flush) : 827 /* This is the first time that we have seen alternative class
822 (*(configuration_table[s->level].func))(s, flush)); 828 * data. All data up till this point has been standard class. */
829 s->class_bitmap = (Bytef*) ZALLOC(strm, s->w_size/4, sizeof(Byte));
830 zmemzero(s->class_bitmap, s->w_size/4);
831 }
832
833 if (strm->clas && s->strategy == Z_RLE) {
834 /* We haven't patched deflate_rle. */
835 ERR_RETURN(strm, Z_BUF_ERROR);
836 }
837
838 if (s->strategy == Z_HUFFMAN_ONLY) {
839 bstate = deflate_huff(s, flush);
840 } else if (s->strategy == Z_RLE) {
841 bstate = deflate_rle(s, flush);
842 } else {
843 bstate = (*(configuration_table[s->level].func))
844 (s, flush, strm->clas);
845 }
823 846
824 if (bstate == finish_started || bstate == finish_done) { 847 if (bstate == finish_started || bstate == finish_done) {
825 s->status = FINISH_STATE; 848 s->status = FINISH_STATE;
826 } 849 }
827 if (bstate == need_more || bstate == finish_started) { 850 if (bstate == need_more || bstate == finish_started) {
828 if (strm->avail_out == 0) { 851 if (strm->avail_out == 0) {
829 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ 852 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
830 } 853 }
831 return Z_OK; 854 return Z_OK;
832 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 855 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
908 status != BUSY_STATE && 931 status != BUSY_STATE &&
909 status != FINISH_STATE) { 932 status != FINISH_STATE) {
910 return Z_STREAM_ERROR; 933 return Z_STREAM_ERROR;
911 } 934 }
912 935
913 /* Deallocate in reverse order of allocations: */ 936 /* Deallocate in reverse order of allocations: */
914 TRY_FREE(strm, strm->state->pending_buf); 937 TRY_FREE(strm, strm->state->pending_buf);
915 TRY_FREE(strm, strm->state->head); 938 TRY_FREE(strm, strm->state->head);
916 TRY_FREE(strm, strm->state->prev); 939 TRY_FREE(strm, strm->state->prev);
917 TRY_FREE(strm, strm->state->window); 940 TRY_FREE(strm, strm->state->window);
941 TRY_FREE(strm, strm->state->class_bitmap);
918 942
919 ZFREE(strm, strm->state); 943 ZFREE(strm, strm->state);
920 strm->state = Z_NULL; 944 strm->state = Z_NULL;
921 945
922 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; 946 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
923 } 947 }
924 948
925 /* ========================================================================= 949 /* =========================================================================
926 * Copy the source state to the destination state. 950 * Copy the source state to the destination state.
927 * To simplify the source, this is not supported for 16-bit MSDOS (which 951 * To simplify the source, this is not supported for 16-bit MSDOS (which
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 s->match_length = s->prev_length = MIN_MATCH-1; 1063 s->match_length = s->prev_length = MIN_MATCH-1;
1040 s->match_available = 0; 1064 s->match_available = 0;
1041 s->ins_h = 0; 1065 s->ins_h = 0;
1042 #ifndef FASTEST 1066 #ifndef FASTEST
1043 #ifdef ASMV 1067 #ifdef ASMV
1044 match_init(); /* initialize the asm code */ 1068 match_init(); /* initialize the asm code */
1045 #endif 1069 #endif
1046 #endif 1070 #endif
1047 } 1071 }
1048 1072
1073 /* class_set sets bits [offset,offset+len) in s->class_bitmap to either 1 (if
1074 * class != 0) or 0 (otherwise). */
1075 local void class_set(s, offset, len, clas)
1076 deflate_state *s;
1077 IPos offset;
1078 uInt len;
1079 int clas;
1080 {
1081 IPos byte = offset >> 3;
1082 IPos bit = offset & 7;
1083 Bytef class_byte_value = clas ? 0xff : 0x00;
1084 Bytef class_bit_value = clas ? 1 : 0;
1085 static const Bytef mask[8] = {0xfe, 0xfd, 0xfb, 0xf7,
1086 0xef, 0xdf, 0xbf, 0x7f};
1087
1088 if (bit) {
1089 while (len) {
1090 s->class_bitmap[byte] &= mask[bit];
1091 s->class_bitmap[byte] |= class_bit_value << bit;
1092 bit++;
1093 len--;
1094 if (bit == 8) {
1095 bit = 0;
1096 byte++;
1097 break;
1098 }
1099 }
1100 }
1101
1102 while (len >= 8) {
1103 s->class_bitmap[byte++] = class_byte_value;
1104 len -= 8;
1105 }
1106
1107 while (len) {
1108 s->class_bitmap[byte] &= mask[bit];
1109 s->class_bitmap[byte] |= class_bit_value << bit;
1110 bit++;
1111 len--;
1112 }
1113 }
1114
1115 local int class_at(s, window_offset)
1116 deflate_state *s;
1117 IPos window_offset;
1118 {
1119 IPos byte = window_offset >> 3;
1120 IPos bit = window_offset & 7;
1121 return (s->class_bitmap[byte] >> bit) & 1;
1122 }
1123
1049 #ifndef FASTEST 1124 #ifndef FASTEST
1050 /* =========================================================================== 1125 /* ===========================================================================
1051 * Set match_start to the longest match starting at the given string and 1126 * Set match_start to the longest match starting at the given string and
1052 * return its length. Matches shorter or equal to prev_length are discarded, 1127 * return its length. Matches shorter or equal to prev_length are discarded,
1053 * in which case the result is equal to prev_length and match_start is 1128 * in which case the result is equal to prev_length and match_start is
1054 * garbage. 1129 * garbage.
1055 * IN assertions: cur_match is the head of the hash chain for the current 1130 * IN assertions: cur_match is the head of the hash chain for the current
1056 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 1131 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1057 * OUT assertion: the match length is not greater than s->lookahead. 1132 * OUT assertion: the match length is not greater than s->lookahead.
1058 */ 1133 */
1059 #ifndef ASMV 1134 #ifndef ASMV
1060 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or 1135 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
1061 * match.S. The code will be functionally equivalent. 1136 * match.S. The code will be functionally equivalent.
1062 */ 1137 */
1063 local uInt longest_match(s, cur_match) 1138 local uInt longest_match(s, cur_match, clas)
1064 deflate_state *s; 1139 deflate_state *s;
1065 IPos cur_match; /* current match */ 1140 IPos cur_match; /* current match */
1141 int clas;
1066 { 1142 {
1067 unsigned chain_length = s->max_chain_length;/* max hash chain length */ 1143 unsigned chain_length = s->max_chain_length;/* max hash chain length */
1068 register Bytef *scan = s->window + s->strstart; /* current string */ 1144 register Bytef *scan = s->window + s->strstart; /* current string */
1069 register Bytef *match; /* matched string */ 1145 register Bytef *match; /* matched string */
1070 register int len; /* length of current match */ 1146 register int len; /* length of current match */
1071 int best_len = s->prev_length; /* best match length so far */ 1147 int best_len = s->prev_length; /* best match length so far */
1072 int nice_match = s->nice_match; /* stop if match long enough */ 1148 int nice_match = s->nice_match; /* stop if match long enough */
1073 IPos limit = s->strstart > (IPos)MAX_DIST(s) ? 1149 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1074 s->strstart - (IPos)MAX_DIST(s) : NIL; 1150 s->strstart - (IPos)MAX_DIST(s) : NIL;
1075 /* Stop when cur_match becomes <= limit. To simplify the code, 1151 /* Stop when cur_match becomes <= limit. To simplify the code,
(...skipping 27 matching lines...) Expand all
1103 /* Do not look for matches beyond the end of the input. This is necessary 1179 /* Do not look for matches beyond the end of the input. This is necessary
1104 * to make deflate deterministic. 1180 * to make deflate deterministic.
1105 */ 1181 */
1106 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; 1182 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1107 1183
1108 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1184 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1109 1185
1110 do { 1186 do {
1111 Assert(cur_match < s->strstart, "no future"); 1187 Assert(cur_match < s->strstart, "no future");
1112 match = s->window + cur_match; 1188 match = s->window + cur_match;
1189 /* If the matched data is in the wrong class, skip it. */
1190 if (s->class_bitmap && class_at(s, cur_match) != clas)
1191 continue;
1113 1192
1114 /* Skip to next match if the match length cannot increase 1193 /* Skip to next match if the match length cannot increase
1115 * or if the match length is less than 2. Note that the checks below 1194 * or if the match length is less than 2. Note that the checks below
1116 * for insufficient lookahead only occur occasionally for performance 1195 * for insufficient lookahead only occur occasionally for performance
1117 * reasons. Therefore uninitialized memory will be accessed, and 1196 * reasons. Therefore uninitialized memory will be accessed, and
1118 * conditional jumps will be made that depend on those values. 1197 * conditional jumps will be made that depend on those values.
1119 * However the length of the match is limited to the lookahead, so 1198 * However the length of the match is limited to the lookahead, so
1120 * the output of deflate is not affected by the uninitialized values. 1199 * the output of deflate is not affected by the uninitialized values.
1121 */ 1200 */
1122 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) 1201 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
(...skipping 22 matching lines...) Expand all
1145 scan < strend); 1224 scan < strend);
1146 /* The funny "do {}" generates better code on most compilers */ 1225 /* The funny "do {}" generates better code on most compilers */
1147 1226
1148 /* Here, scan <= window+strstart+257 */ 1227 /* Here, scan <= window+strstart+257 */
1149 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1228 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1150 if (*scan == *match) scan++; 1229 if (*scan == *match) scan++;
1151 1230
1152 len = (MAX_MATCH - 1) - (int)(strend-scan); 1231 len = (MAX_MATCH - 1) - (int)(strend-scan);
1153 scan = strend - (MAX_MATCH-1); 1232 scan = strend - (MAX_MATCH-1);
1154 1233
1234 #error "UNALIGNED_OK hasn't been patched."
1235
1155 #else /* UNALIGNED_OK */ 1236 #else /* UNALIGNED_OK */
1156 1237
1157 if (match[best_len] != scan_end || 1238 if (match[best_len] != scan_end ||
1158 match[best_len-1] != scan_end1 || 1239 match[best_len-1] != scan_end1 ||
1159 *match != *scan || 1240 *match != *scan ||
1160 *++match != scan[1]) continue; 1241 *++match != scan[1]) continue;
1161 1242
1162 /* The check at best_len-1 can be removed because it will be made 1243 /* The check at best_len-1 can be removed because it will be made
1163 * again later. (This heuristic is not always a win.) 1244 * again later. (This heuristic is not always a win.)
1164 * It is not necessary to compare scan[2] and match[2] since they 1245 * It is not necessary to compare scan[2] and match[2] since they
1165 * are always equal when the other bytes match, given that 1246 * are always equal when the other bytes match, given that
1166 * the hash keys are equal and that HASH_BITS >= 8. 1247 * the hash keys are equal and that HASH_BITS >= 8.
1167 */ 1248 */
1168 scan += 2, match++; 1249 scan += 2, match++;
1169 Assert(*scan == *match, "match[2]?"); 1250 Assert(*scan == *match, "match[2]?");
1170 1251
1171 /* We check for insufficient lookahead only every 8th comparison; 1252 if (!s->class_bitmap) {
1172 * the 256th check will be made at strstart+258. 1253 /* We check for insufficient lookahead only every 8th comparison;
1173 */ 1254 * the 256th check will be made at strstart+258.
1174 do { 1255 */
1175 } while (*++scan == *++match && *++scan == *++match && 1256 do {
1176 *++scan == *++match && *++scan == *++match && 1257 } while (*++scan == *++match && *++scan == *++match &&
1177 *++scan == *++match && *++scan == *++match && 1258 *++scan == *++match && *++scan == *++match &&
1178 *++scan == *++match && *++scan == *++match && 1259 *++scan == *++match && *++scan == *++match &&
1179 scan < strend); 1260 *++scan == *++match && *++scan == *++match &&
1261 scan < strend);
1262 } else {
1263 /* We have to be mindful of the class of the data and not stray. */
1264 do {
1265 } while (*++scan == *++match &&
1266 class_at(s, match - s->window) == clas &&
1267 scan < strend);
1268 }
1180 1269
1181 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); 1270 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1182 1271
1183 len = MAX_MATCH - (int)(strend - scan); 1272 len = MAX_MATCH - (int)(strend - scan);
1184 scan = strend - MAX_MATCH; 1273 scan = strend - MAX_MATCH;
1185 1274
1186 #endif /* UNALIGNED_OK */ 1275 #endif /* UNALIGNED_OK */
1187 1276
1188 if (len > best_len) { 1277 if (len > best_len) {
1189 s->match_start = cur_match; 1278 s->match_start = cur_match;
1190 best_len = len; 1279 best_len = len;
1191 if (len >= nice_match) break; 1280 if (len >= nice_match) break;
1192 #ifdef UNALIGNED_OK 1281 #ifdef UNALIGNED_OK
1193 scan_end = *(ushf*)(scan+best_len-1); 1282 scan_end = *(ushf*)(scan+best_len-1);
1194 #else 1283 #else
1195 scan_end1 = scan[best_len-1]; 1284 scan_end1 = scan[best_len-1];
1196 scan_end = scan[best_len]; 1285 scan_end = scan[best_len];
1197 #endif 1286 #endif
1198 } 1287 }
1199 } while ((cur_match = prev[cur_match & wmask]) > limit 1288 } while ((cur_match = prev[cur_match & wmask]) > limit
1200 && --chain_length != 0); 1289 && --chain_length != 0);
1201 1290
1202 if ((uInt)best_len <= s->lookahead) return (uInt)best_len; 1291 if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1203 return s->lookahead; 1292 return s->lookahead;
1204 } 1293 }
1205 #endif /* ASMV */ 1294 #endif /* ASMV */
1206 1295
1296 /* cookie_match is a replacement for longest_match in the case of cookie data.
1297 * Here we only wish to match the entire value so trying the partial matches in
1298 * longest_match is both wasteful and often fails to find the correct match.
1299 *
1300 * So we take the djb2 hash of the cookie and look up the last position for a
1301 * match in a special hash table. */
1302 local uInt cookie_match(s, start, len)
1303 deflate_state *s;
1304 IPos start;
1305 unsigned len;
1306 {
1307 unsigned hash = 5381;
1308 Bytef *str = s->window + start;
1309 unsigned i;
1310 IPos cookie_location;
1311
1312 if (len >= MAX_MATCH)
1313 return 0;
1314
1315 for (i = 0; i < len; i++)
1316 hash = ((hash << 5) + hash) + str[i];
1317
1318 hash &= Z_COOKIE_HASH_MASK;
1319 cookie_location = s->cookie_locations[hash];
1320 s->cookie_locations[hash] = start;
1321 s->match_start = 0;
1322 if (cookie_location &&
1323 (start - cookie_location) > len &&
1324 (start - cookie_location) < MAX_DIST(s) &&
1325 len <= s->lookahead) {
1326 for (i = 0; i < len; i++) {
1327 if (s->window[start+i] != s->window[cookie_location+i] ||
1328 class_at(s, cookie_location+i) != 1) {
1329 return 0;
1330 }
1331 }
1332 s->match_start = cookie_location;
1333 return len;
1334 }
1335
1336 return 0;
1337 }
1338
1339
1207 #else /* FASTEST */ 1340 #else /* FASTEST */
1208 1341
1209 /* --------------------------------------------------------------------------- 1342 /* ---------------------------------------------------------------------------
1210 * Optimized version for FASTEST only 1343 * Optimized version for FASTEST only
1211 */ 1344 */
1212 local uInt longest_match(s, cur_match) 1345 local uInt longest_match(s, cur_match, clas)
1213 deflate_state *s; 1346 deflate_state *s;
1214 IPos cur_match; /* current match */ 1347 IPos cur_match; /* current match */
1348 int clas;
1215 { 1349 {
1216 register Bytef *scan = s->window + s->strstart; /* current string */ 1350 register Bytef *scan = s->window + s->strstart; /* current string */
1217 register Bytef *match; /* matched string */ 1351 register Bytef *match; /* matched string */
1218 register int len; /* length of current match */ 1352 register int len; /* length of current match */
1219 register Bytef *strend = s->window + s->strstart + MAX_MATCH; 1353 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1220 1354
1355 #error "This code not patched"
1356
1221 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1357 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1222 * It is easy to get rid of this optimization if necessary. 1358 * It is easy to get rid of this optimization if necessary.
1223 */ 1359 */
1224 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); 1360 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1225 1361
1226 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); 1362 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1227 1363
1228 Assert(cur_match < s->strstart, "no future"); 1364 Assert(cur_match < s->strstart, "no future");
1229 1365
1230 match = s->window + cur_match; 1366 match = s->window + cur_match;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
1353 #ifndef FASTEST 1489 #ifndef FASTEST
1354 p = &s->prev[n]; 1490 p = &s->prev[n];
1355 do { 1491 do {
1356 m = *--p; 1492 m = *--p;
1357 *p = (Pos)(m >= wsize ? m-wsize : NIL); 1493 *p = (Pos)(m >= wsize ? m-wsize : NIL);
1358 /* If n is not on any hash chain, prev[n] is garbage but 1494 /* If n is not on any hash chain, prev[n] is garbage but
1359 * its value will never be used. 1495 * its value will never be used.
1360 */ 1496 */
1361 } while (--n); 1497 } while (--n);
1362 #endif 1498 #endif
1499
1500 for (n = 0; n < Z_COOKIE_HASH_SIZE; n++) {
1501 if (s->cookie_locations[n] > wsize) {
1502 s->cookie_locations[n] -= wsize;
1503 } else {
1504 s->cookie_locations[n] = 0;
1505 }
1506 }
1507
1508 if (s->class_bitmap) {
1509 zmemcpy(s->class_bitmap, s->class_bitmap + s->w_size/8,
1510 s->w_size/8);
1511 zmemzero(s->class_bitmap + s->w_size/8, s->w_size/8);
1512 }
1513
1363 more += wsize; 1514 more += wsize;
1364 } 1515 }
1365 if (s->strm->avail_in == 0) return; 1516 if (s->strm->avail_in == 0) return;
1366 1517
1367 /* If there was no sliding: 1518 /* If there was no sliding:
1368 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 1519 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1369 * more == window_size - lookahead - strstart 1520 * more == window_size - lookahead - strstart
1370 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 1521 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1371 * => more >= window_size - 2*WSIZE + 2 1522 * => more >= window_size - 2*WSIZE + 2
1372 * In the BIG_MEM or MMAP case (not yet supported), 1523 * In the BIG_MEM or MMAP case (not yet supported),
1373 * window_size == input_size + MIN_LOOKAHEAD && 1524 * window_size == input_size + MIN_LOOKAHEAD &&
1374 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 1525 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1375 * Otherwise, window_size == 2*WSIZE so more >= 2. 1526 * Otherwise, window_size == 2*WSIZE so more >= 2.
1376 * If there was sliding, more >= WSIZE. So in all cases, more >= 2. 1527 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1377 */ 1528 */
1378 Assert(more >= 2, "more < 2"); 1529 Assert(more >= 2, "more < 2");
1379 1530
1380 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); 1531 n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1532 if (s->class_bitmap != NULL) {
1533 class_set(s, s->strstart + s->lookahead, n, s->strm->clas);
1534 }
1381 s->lookahead += n; 1535 s->lookahead += n;
1382 1536
1383 /* Initialize the hash value now that we have some input: */ 1537 /* Initialize the hash value now that we have some input: */
1384 if (s->lookahead >= MIN_MATCH) { 1538 if (s->lookahead >= MIN_MATCH) {
1385 s->ins_h = s->window[s->strstart]; 1539 s->ins_h = s->window[s->strstart];
1386 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 1540 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1387 #if MIN_MATCH != 3 1541 #if MIN_MATCH != 3
1388 Call UPDATE_HASH() MIN_MATCH-3 more times 1542 Call UPDATE_HASH() MIN_MATCH-3 more times
1389 #endif 1543 #endif
1390 } 1544 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1452 1606
1453 /* =========================================================================== 1607 /* ===========================================================================
1454 * Copy without compression as much as possible from the input stream, return 1608 * Copy without compression as much as possible from the input stream, return
1455 * the current block state. 1609 * the current block state.
1456 * This function does not insert new strings in the dictionary since 1610 * This function does not insert new strings in the dictionary since
1457 * uncompressible data is probably not useful. This function is used 1611 * uncompressible data is probably not useful. This function is used
1458 * only for the level=0 compression option. 1612 * only for the level=0 compression option.
1459 * NOTE: this function should be optimized to avoid extra copying from 1613 * NOTE: this function should be optimized to avoid extra copying from
1460 * window to pending_buf. 1614 * window to pending_buf.
1461 */ 1615 */
1462 local block_state deflate_stored(s, flush) 1616 local block_state deflate_stored(s, flush, clas)
1463 deflate_state *s; 1617 deflate_state *s;
1464 int flush; 1618 int flush;
1619 int clas;
1465 { 1620 {
1466 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited 1621 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1467 * to pending_buf_size, and each stored block has a 5 byte header: 1622 * to pending_buf_size, and each stored block has a 5 byte header:
1468 */ 1623 */
1469 ulg max_block_size = 0xffff; 1624 ulg max_block_size = 0xffff;
1470 ulg max_start; 1625 ulg max_start;
1471 1626
1472 if (max_block_size > s->pending_buf_size - 5) { 1627 if (max_block_size > s->pending_buf_size - 5) {
1473 max_block_size = s->pending_buf_size - 5; 1628 max_block_size = s->pending_buf_size - 5;
1474 } 1629 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1510 return flush == Z_FINISH ? finish_done : block_done; 1665 return flush == Z_FINISH ? finish_done : block_done;
1511 } 1666 }
1512 1667
1513 /* =========================================================================== 1668 /* ===========================================================================
1514 * Compress as much as possible from the input stream, return the current 1669 * Compress as much as possible from the input stream, return the current
1515 * block state. 1670 * block state.
1516 * This function does not perform lazy evaluation of matches and inserts 1671 * This function does not perform lazy evaluation of matches and inserts
1517 * new strings in the dictionary only for unmatched strings or for short 1672 * new strings in the dictionary only for unmatched strings or for short
1518 * matches. It is used only for the fast compression options. 1673 * matches. It is used only for the fast compression options.
1519 */ 1674 */
1520 local block_state deflate_fast(s, flush) 1675 local block_state deflate_fast(s, flush, clas)
1521 deflate_state *s; 1676 deflate_state *s;
1522 int flush; 1677 int flush;
1678 int clas;
1523 { 1679 {
1524 IPos hash_head; /* head of the hash chain */ 1680 IPos hash_head; /* head of the hash chain */
1525 int bflush; /* set if current block must be flushed */ 1681 int bflush; /* set if current block must be flushed */
1526 1682
1683 if (clas != 0) {
1684 /* We haven't patched this code for alternative class data. */
1685 return Z_BUF_ERROR;
1686 }
1687
1527 for (;;) { 1688 for (;;) {
1528 /* Make sure that we always have enough lookahead, except 1689 /* Make sure that we always have enough lookahead, except
1529 * at the end of the input file. We need MAX_MATCH bytes 1690 * at the end of the input file. We need MAX_MATCH bytes
1530 * for the next match, plus MIN_MATCH bytes to insert the 1691 * for the next match, plus MIN_MATCH bytes to insert the
1531 * string following the next match. 1692 * string following the next match.
1532 */ 1693 */
1533 if (s->lookahead < MIN_LOOKAHEAD) { 1694 if (s->lookahead < MIN_LOOKAHEAD) {
1534 fill_window(s); 1695 fill_window(s);
1535 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1696 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1536 return need_more; 1697 return need_more;
(...skipping 10 matching lines...) Expand all
1547 } 1708 }
1548 1709
1549 /* Find the longest match, discarding those <= prev_length. 1710 /* Find the longest match, discarding those <= prev_length.
1550 * At this point we have always match_length < MIN_MATCH 1711 * At this point we have always match_length < MIN_MATCH
1551 */ 1712 */
1552 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 1713 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1553 /* To simplify the code, we prevent matches with the string 1714 /* To simplify the code, we prevent matches with the string
1554 * of window index 0 (in particular we have to avoid a match 1715 * of window index 0 (in particular we have to avoid a match
1555 * of the string with itself at the start of the input file). 1716 * of the string with itself at the start of the input file).
1556 */ 1717 */
1557 s->match_length = longest_match (s, hash_head); 1718 s->match_length = longest_match (s, hash_head, clas);
1558 /* longest_match() sets match_start */ 1719 /* longest_match() sets match_start */
1559 } 1720 }
1560 if (s->match_length >= MIN_MATCH) { 1721 if (s->match_length >= MIN_MATCH) {
1561 check_match(s, s->strstart, s->match_start, s->match_length); 1722 check_match(s, s->strstart, s->match_start, s->match_length);
1562 1723
1563 _tr_tally_dist(s, s->strstart - s->match_start, 1724 _tr_tally_dist(s, s->strstart - s->match_start,
1564 s->match_length - MIN_MATCH, bflush); 1725 s->match_length - MIN_MATCH, bflush);
1565 1726
1566 s->lookahead -= s->match_length; 1727 s->lookahead -= s->match_length;
1567 1728
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1606 FLUSH_BLOCK(s, flush == Z_FINISH); 1767 FLUSH_BLOCK(s, flush == Z_FINISH);
1607 return flush == Z_FINISH ? finish_done : block_done; 1768 return flush == Z_FINISH ? finish_done : block_done;
1608 } 1769 }
1609 1770
1610 #ifndef FASTEST 1771 #ifndef FASTEST
1611 /* =========================================================================== 1772 /* ===========================================================================
1612 * Same as above, but achieves better compression. We use a lazy 1773 * Same as above, but achieves better compression. We use a lazy
1613 * evaluation for matches: a match is finally adopted only if there is 1774 * evaluation for matches: a match is finally adopted only if there is
1614 * no better match at the next window position. 1775 * no better match at the next window position.
1615 */ 1776 */
1616 local block_state deflate_slow(s, flush) 1777 local block_state deflate_slow(s, flush, clas)
1617 deflate_state *s; 1778 deflate_state *s;
1618 int flush; 1779 int flush;
1780 int clas;
1619 { 1781 {
1620 IPos hash_head; /* head of hash chain */ 1782 IPos hash_head; /* head of hash chain */
1621 int bflush; /* set if current block must be flushed */ 1783 int bflush; /* set if current block must be flushed */
1784 uInt input_length ;
1785 int first = 1; /* first says whether this is the first iteration
1786 of the loop, below. */
1787
1788 if (clas == Z_CLASS_COOKIE) {
1789 if (s->lookahead) {
1790 /* Alternative class data must always be presented at the beginning
1791 * of a block. */
1792 return Z_BUF_ERROR;
1793 }
1794 input_length = s->strm->avail_in;
1795 }
1622 1796
1623 /* Process the input block. */ 1797 /* Process the input block. */
1624 for (;;) { 1798 for (;;) {
1625 /* Make sure that we always have enough lookahead, except 1799 /* Make sure that we always have enough lookahead, except
1626 * at the end of the input file. We need MAX_MATCH bytes 1800 * at the end of the input file. We need MAX_MATCH bytes
1627 * for the next match, plus MIN_MATCH bytes to insert the 1801 * for the next match, plus MIN_MATCH bytes to insert the
1628 * string following the next match. 1802 * string following the next match.
1629 */ 1803 */
1630 if (s->lookahead < MIN_LOOKAHEAD) { 1804 if (s->lookahead < MIN_LOOKAHEAD) {
1631 fill_window(s); 1805 fill_window(s);
1632 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1806 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1633 return need_more; 1807 return need_more;
1634 } 1808 }
1635 if (s->lookahead == 0) break; /* flush the current block */ 1809 if (s->lookahead == 0) break; /* flush the current block */
1636 } 1810 }
1637 1811
1638 /* Insert the string window[strstart .. strstart+2] in the 1812 /* Insert the string window[strstart .. strstart+2] in the
1639 * dictionary, and set hash_head to the head of the hash chain: 1813 * dictionary, and set hash_head to the head of the hash chain:
1640 */ 1814 */
1641 hash_head = NIL; 1815 hash_head = NIL;
1642 if (s->lookahead >= MIN_MATCH) { 1816 if (s->lookahead >= MIN_MATCH) {
1643 INSERT_STRING(s, s->strstart, hash_head); 1817 INSERT_STRING(s, s->strstart, hash_head);
1644 } 1818 }
1645 1819
1646 /* Find the longest match, discarding those <= prev_length. 1820 /* Find the longest match, discarding those <= prev_length.
1647 */ 1821 */
1648 s->prev_length = s->match_length, s->prev_match = s->match_start; 1822 s->prev_length = s->match_length, s->prev_match = s->match_start;
1649 s->match_length = MIN_MATCH-1; 1823 s->match_length = MIN_MATCH-1;
1650 1824
1651 if (hash_head != NIL && s->prev_length < s->max_lazy_match && 1825 if (clas == Z_CLASS_COOKIE && first) {
1652 s->strstart - hash_head <= MAX_DIST(s)) { 1826 s->match_length = cookie_match(s, s->strstart, input_length);
1827 } else if (clas == Z_CLASS_STANDARD &&
1828 hash_head != NIL &&
1829 s->prev_length < s->max_lazy_match &&
1830 s->strstart - hash_head <= MAX_DIST(s)) {
1653 /* To simplify the code, we prevent matches with the string 1831 /* To simplify the code, we prevent matches with the string
1654 * of window index 0 (in particular we have to avoid a match 1832 * of window index 0 (in particular we have to avoid a match
1655 * of the string with itself at the start of the input file). 1833 * of the string with itself at the start of the input file).
1656 */ 1834 */
1657 s->match_length = longest_match (s, hash_head); 1835 s->match_length = longest_match (s, hash_head, clas);
1836
1658 /* longest_match() sets match_start */ 1837 /* longest_match() sets match_start */
1659 1838
1660 if (s->match_length <= 5 && (s->strategy == Z_FILTERED 1839 if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1661 #if TOO_FAR <= 32767 1840 #if TOO_FAR <= 32767
1662 || (s->match_length == MIN_MATCH && 1841 || (s->match_length == MIN_MATCH &&
1663 s->strstart - s->match_start > TOO_FAR) 1842 s->strstart - s->match_start > TOO_FAR)
1664 #endif 1843 #endif
1665 )) { 1844 )) {
1666 1845
1667 /* If prev_match is also MIN_MATCH, match_start is garbage 1846 /* If prev_match is also MIN_MATCH, match_start is garbage
1668 * but we will ignore the current match anyway. 1847 * but we will ignore the current match anyway.
1669 */ 1848 */
1670 s->match_length = MIN_MATCH-1; 1849 s->match_length = MIN_MATCH-1;
1671 } 1850 }
1672 } 1851 }
1673 /* If there was a match at the previous step and the current 1852 /* If there was a match at the previous step and the current
1674 * match is not better, output the previous match: 1853 * match is not better, output the previous match:
1675 */ 1854 */
1676 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { 1855 first = 0;
1856 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length &&
1857 /* We will only accept an exact match for Z_CLASS_COOKIE data and
1858 * we won't match Z_CLASS_HUFFMAN_ONLY data at all. */
1859 (clas == Z_CLASS_STANDARD || (clas == Z_CLASS_COOKIE &&
1860 s->prev_length == input_length &&
1861 s->prev_match > 0 &&
1862 /* We require that a Z_CLASS_COOKIE match be
1863 * preceded by either a semicolon (which cannot be
1864 * part of a cookie), or non-cookie data. This is
1865 * to prevent
1866 * a cookie from being a prefix of another.
1867 * spdy_framer.cc ensures that cookies are always
1868 * terminated by a semicolon. */
1869 (class_at(s, s->prev_match-1) == Z_CLASS_STANDARD ||
1870 *(s->window + s->prev_match-1) == ';')))) {
1677 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 1871 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1678 /* Do not insert strings in hash table beyond this. */ 1872 /* Do not insert strings in hash table beyond this. */
1679 1873
1680 check_match(s, s->strstart-1, s->prev_match, s->prev_length); 1874 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1681 1875
1682 _tr_tally_dist(s, s->strstart -1 - s->prev_match, 1876 _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1683 s->prev_length - MIN_MATCH, bflush); 1877 s->prev_length - MIN_MATCH, bflush);
1684 1878
1685 /* Insert in hash table all strings up to the end of the match. 1879 /* Insert in hash table all strings up to the end of the match.
1686 * strstart-1 and strstart are already inserted. If there is not 1880 * strstart-1 and strstart are already inserted. If there is not
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 s->match_length = 0; 2019 s->match_length = 0;
1826 Tracevv((stderr,"%c", s->window[s->strstart])); 2020 Tracevv((stderr,"%c", s->window[s->strstart]));
1827 _tr_tally_lit (s, s->window[s->strstart], bflush); 2021 _tr_tally_lit (s, s->window[s->strstart], bflush);
1828 s->lookahead--; 2022 s->lookahead--;
1829 s->strstart++; 2023 s->strstart++;
1830 if (bflush) FLUSH_BLOCK(s, 0); 2024 if (bflush) FLUSH_BLOCK(s, 0);
1831 } 2025 }
1832 FLUSH_BLOCK(s, flush == Z_FINISH); 2026 FLUSH_BLOCK(s, flush == Z_FINISH);
1833 return flush == Z_FINISH ? finish_done : block_done; 2027 return flush == Z_FINISH ? finish_done : block_done;
1834 } 2028 }
OLDNEW
« no previous file with comments | « third_party/zlib/deflate.h ('k') | third_party/zlib/mixed-source.patch » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698