~ubuntu-branches/ubuntu/lucid/perl-tk/lucid

« back to all changes in this revision

Viewing changes to PNG/zlib/deflate.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Tuckley
  • Date: 2008-02-15 13:56:59 UTC
  • mfrom: (1.1.3 upstream) (4.1.1 hardy)
  • Revision ID: james.westby@ubuntu.com-20080215135659-ru2oqlykuju20fav
Tags: 1:804.028-1
* New Upstream Release (Closes: #463080).
* Update to Debhelper v5.
* Build with XFT=1 (Closes: #411129).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* deflate.c -- compress data using the deflation algorithm
2
 
 * Copyright (C) 1995-2002 Jean-loup Gailly.
3
 
 * For conditions of distribution and use, see copyright notice in zlib.h 
 
2
 * Copyright (C) 1995-2005 Jean-loup Gailly.
 
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
4
 */
5
5
 
6
6
/*
37
37
 *  REFERENCES
38
38
 *
39
39
 *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40
 
 *      Available in ftp://ds.internic.net/rfc/rfc1951.txt
 
40
 *      Available in http://www.ietf.org/rfc/rfc1951.txt
41
41
 *
42
42
 *      A description of the Rabin and Karp algorithm is given in the book
43
43
 *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
52
52
#include "deflate.h"
53
53
 
54
54
const char deflate_copyright[] =
55
 
   " deflate 1.1.4 Copyright 1995-2002 Jean-loup Gailly ";
 
55
   " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
56
56
/*
57
57
  If you use the zlib library in a product, an acknowledgment is welcome
58
58
  in the documentation of your product. If for some reason you cannot
76
76
local void fill_window    OF((deflate_state *s));
77
77
local block_state deflate_stored OF((deflate_state *s, int flush));
78
78
local block_state deflate_fast   OF((deflate_state *s, int flush));
 
79
#ifndef FASTEST
79
80
local block_state deflate_slow   OF((deflate_state *s, int flush));
 
81
#endif
80
82
local void lm_init        OF((deflate_state *s));
81
83
local void putShortMSB    OF((deflate_state *s, uInt b));
82
84
local void flush_pending  OF((z_streamp strm));
83
85
local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
 
86
#ifndef FASTEST
84
87
#ifdef ASMV
85
88
      void match_init OF((void)); /* asm code initialization */
86
89
      uInt longest_match  OF((deflate_state *s, IPos cur_match));
87
90
#else
88
91
local uInt longest_match  OF((deflate_state *s, IPos cur_match));
89
92
#endif
 
93
#endif
 
94
local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
90
95
 
91
96
#ifdef DEBUG
92
97
local  void check_match OF((deflate_state *s, IPos start, IPos match,
123
128
   compress_func func;
124
129
} config;
125
130
 
 
131
#ifdef FASTEST
 
132
local const config configuration_table[2] = {
 
133
/*      good lazy nice chain */
 
134
/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
 
135
/* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
 
136
#else
126
137
local const config configuration_table[10] = {
127
138
/*      good lazy nice chain */
128
139
/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
129
 
/* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches */
 
140
/* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
130
141
/* 2 */ {4,    5, 16,    8, deflate_fast},
131
142
/* 3 */ {4,    6, 32,   32, deflate_fast},
132
143
 
135
146
/* 6 */ {8,   16, 128, 128, deflate_slow},
136
147
/* 7 */ {8,   32, 128, 256, deflate_slow},
137
148
/* 8 */ {32, 128, 258, 1024, deflate_slow},
138
 
/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
 
149
/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
 
150
#endif
139
151
 
140
152
/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
141
153
 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
145
157
#define EQUAL 0
146
158
/* result of memcmp for equal strings */
147
159
 
 
160
#ifndef NO_DUMMY_DECL
148
161
struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
 
162
#endif
149
163
 
150
164
/* ===========================================================================
151
165
 * Update a hash value with the given input byte
174
188
#else
175
189
#define INSERT_STRING(s, str, match_head) \
176
190
   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
177
 
    s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
 
191
    match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
178
192
    s->head[s->ins_h] = (Pos)(str))
179
193
#endif
180
194
 
194
208
    int stream_size;
195
209
{
196
210
    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
197
 
                         Z_DEFAULT_STRATEGY, version, stream_size);
 
211
                         Z_DEFAULT_STRATEGY, version, stream_size);
198
212
    /* To do: ignore strm->next_in if we use it as window */
199
213
}
200
214
 
201
215
/* ========================================================================= */
202
216
int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
203
 
                  version, stream_size)
 
217
                  version, stream_size)
204
218
    z_streamp strm;
205
219
    int  level;
206
220
    int  method;
211
225
    int stream_size;
212
226
{
213
227
    deflate_state *s;
214
 
    int noheader = 0;
215
 
    static const char* my_version = ZLIB_VERSION;
 
228
    int wrap = 1;
 
229
    static const char my_version[] = ZLIB_VERSION;
216
230
 
217
231
    ushf *overlay;
218
232
    /* We overlay pending_buf and d_buf+l_buf. This works since the average
221
235
 
222
236
    if (version == Z_NULL || version[0] != my_version[0] ||
223
237
        stream_size != sizeof(z_stream)) {
224
 
        return Z_VERSION_ERROR;
 
238
        return Z_VERSION_ERROR;
225
239
    }
226
240
    if (strm == Z_NULL) return Z_STREAM_ERROR;
227
241
 
228
242
    strm->msg = Z_NULL;
229
 
    if (strm->zalloc == Z_NULL) {
230
 
        strm->zalloc = zcalloc;
231
 
        strm->opaque = (voidpf)0;
 
243
    if (strm->zalloc == (alloc_func)0) {
 
244
        strm->zalloc = zcalloc;
 
245
        strm->opaque = (voidpf)0;
232
246
    }
233
 
    if (strm->zfree == Z_NULL) strm->zfree = zcfree;
 
247
    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
234
248
 
 
249
#ifdef FASTEST
 
250
    if (level != 0) level = 1;
 
251
#else
235
252
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
236
 
#ifdef FASTEST
237
 
    level = 1;
238
253
#endif
239
254
 
240
 
    if (windowBits < 0) { /* undocumented feature: suppress zlib header */
241
 
        noheader = 1;
 
255
    if (windowBits < 0) { /* suppress zlib wrapper */
 
256
        wrap = 0;
242
257
        windowBits = -windowBits;
243
258
    }
 
259
#ifdef GZIP
 
260
    else if (windowBits > 15) {
 
261
        wrap = 2;       /* write gzip wrapper instead */
 
262
        windowBits -= 16;
 
263
    }
 
264
#endif
244
265
    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
245
 
        windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
246
 
        strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
 
266
        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
 
267
        strategy < 0 || strategy > Z_FIXED) {
247
268
        return Z_STREAM_ERROR;
248
269
    }
 
270
    if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
249
271
    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
250
272
    if (s == Z_NULL) return Z_MEM_ERROR;
251
273
    strm->state = (struct internal_state FAR *)s;
252
274
    s->strm = strm;
253
275
 
254
 
    s->noheader = noheader;
 
276
    s->wrap = wrap;
 
277
    s->gzhead = Z_NULL;
255
278
    s->w_bits = windowBits;
256
279
    s->w_size = 1 << s->w_bits;
257
280
    s->w_mask = s->w_size - 1;
273
296
 
274
297
    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
275
298
        s->pending_buf == Z_NULL) {
 
299
        s->status = FINISH_STATE;
276
300
        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
277
301
        deflateEnd (strm);
278
302
        return Z_MEM_ERROR;
299
323
    IPos hash_head = 0;
300
324
 
301
325
    if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
302
 
        strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
 
326
        strm->state->wrap == 2 ||
 
327
        (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
 
328
        return Z_STREAM_ERROR;
303
329
 
304
330
    s = strm->state;
305
 
    strm->adler = adler32(strm->adler, dictionary, dictLength);
 
331
    if (s->wrap)
 
332
        strm->adler = adler32(strm->adler, dictionary, dictLength);
306
333
 
307
334
    if (length < MIN_MATCH) return Z_OK;
308
335
    if (length > MAX_DIST(s)) {
309
 
        length = MAX_DIST(s);
310
 
#ifndef USE_DICT_HEAD
311
 
        dictionary += dictLength - length; /* use the tail of the dictionary */
312
 
#endif
 
336
        length = MAX_DIST(s);
 
337
        dictionary += dictLength - length; /* use the tail of the dictionary */
313
338
    }
314
339
    zmemcpy(s->window, dictionary, length);
315
340
    s->strstart = length;
322
347
    s->ins_h = s->window[0];
323
348
    UPDATE_HASH(s, s->ins_h, s->window[1]);
324
349
    for (n = 0; n <= length - MIN_MATCH; n++) {
325
 
        INSERT_STRING(s, n, hash_head);
 
350
        INSERT_STRING(s, n, hash_head);
326
351
    }
327
352
    if (hash_head) hash_head = 0;  /* to make compiler happy */
328
353
    return Z_OK;
333
358
    z_streamp strm;
334
359
{
335
360
    deflate_state *s;
336
 
    
 
361
 
337
362
    if (strm == Z_NULL || strm->state == Z_NULL ||
338
 
        strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
 
363
        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
 
364
        return Z_STREAM_ERROR;
 
365
    }
339
366
 
340
367
    strm->total_in = strm->total_out = 0;
341
368
    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
345
372
    s->pending = 0;
346
373
    s->pending_out = s->pending_buf;
347
374
 
348
 
    if (s->noheader < 0) {
349
 
        s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
 
375
    if (s->wrap < 0) {
 
376
        s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
350
377
    }
351
 
    s->status = s->noheader ? BUSY_STATE : INIT_STATE;
352
 
    strm->adler = 1;
 
378
    s->status = s->wrap ? INIT_STATE : BUSY_STATE;
 
379
    strm->adler =
 
380
#ifdef GZIP
 
381
        s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
 
382
#endif
 
383
        adler32(0L, Z_NULL, 0);
353
384
    s->last_flush = Z_NO_FLUSH;
354
385
 
355
386
    _tr_init(s);
359
390
}
360
391
 
361
392
/* ========================================================================= */
 
393
int ZEXPORT deflateSetHeader (strm, head)
 
394
    z_streamp strm;
 
395
    gz_headerp head;
 
396
{
 
397
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 
398
    if (strm->state->wrap != 2) return Z_STREAM_ERROR;
 
399
    strm->state->gzhead = head;
 
400
    return Z_OK;
 
401
}
 
402
 
 
403
/* ========================================================================= */
 
404
int ZEXPORT deflatePrime (strm, bits, value)
 
405
    z_streamp strm;
 
406
    int bits;
 
407
    int value;
 
408
{
 
409
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 
410
    strm->state->bi_valid = bits;
 
411
    strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
 
412
    return Z_OK;
 
413
}
 
414
 
 
415
/* ========================================================================= */
362
416
int ZEXPORT deflateParams(strm, level, strategy)
363
417
    z_streamp strm;
364
418
    int level;
371
425
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
372
426
    s = strm->state;
373
427
 
374
 
    if (level == Z_DEFAULT_COMPRESSION) {
375
 
        level = 6;
376
 
    }
377
 
    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
378
 
        return Z_STREAM_ERROR;
 
428
#ifdef FASTEST
 
429
    if (level != 0) level = 1;
 
430
#else
 
431
    if (level == Z_DEFAULT_COMPRESSION) level = 6;
 
432
#endif
 
433
    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
 
434
        return Z_STREAM_ERROR;
379
435
    }
380
436
    func = configuration_table[s->level].func;
381
437
 
382
438
    if (func != configuration_table[level].func && strm->total_in != 0) {
383
 
        /* Flush the last buffer: */
384
 
        err = deflate(strm, Z_PARTIAL_FLUSH);
 
439
        /* Flush the last buffer: */
 
440
        err = deflate(strm, Z_PARTIAL_FLUSH);
385
441
    }
386
442
    if (s->level != level) {
387
 
        s->level = level;
388
 
        s->max_lazy_match   = configuration_table[level].max_lazy;
389
 
        s->good_match       = configuration_table[level].good_length;
390
 
        s->nice_match       = configuration_table[level].nice_length;
391
 
        s->max_chain_length = configuration_table[level].max_chain;
 
443
        s->level = level;
 
444
        s->max_lazy_match   = configuration_table[level].max_lazy;
 
445
        s->good_match       = configuration_table[level].good_length;
 
446
        s->nice_match       = configuration_table[level].nice_length;
 
447
        s->max_chain_length = configuration_table[level].max_chain;
392
448
    }
393
449
    s->strategy = strategy;
394
450
    return err;
395
451
}
396
452
 
 
453
/* ========================================================================= */
 
454
int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
 
455
    z_streamp strm;
 
456
    int good_length;
 
457
    int max_lazy;
 
458
    int nice_length;
 
459
    int max_chain;
 
460
{
 
461
    deflate_state *s;
 
462
 
 
463
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 
464
    s = strm->state;
 
465
    s->good_match = good_length;
 
466
    s->max_lazy_match = max_lazy;
 
467
    s->nice_match = nice_length;
 
468
    s->max_chain_length = max_chain;
 
469
    return Z_OK;
 
470
}
 
471
 
 
472
/* =========================================================================
 
473
 * For the default windowBits of 15 and memLevel of 8, this function returns
 
474
 * a close to exact, as well as small, upper bound on the compressed size.
 
475
 * They are coded as constants here for a reason--if the #define's are
 
476
 * changed, then this function needs to be changed as well.  The return
 
477
 * value for 15 and 8 only works for those exact settings.
 
478
 *
 
479
 * For any setting other than those defaults for windowBits and memLevel,
 
480
 * the value returned is a conservative worst case for the maximum expansion
 
481
 * resulting from using fixed blocks instead of stored blocks, which deflate
 
482
 * can emit on compressed data for some combinations of the parameters.
 
483
 *
 
484
 * This function could be more sophisticated to provide closer upper bounds
 
485
 * for every combination of windowBits and memLevel, as well as wrap.
 
486
 * But even the conservative upper bound of about 14% expansion does not
 
487
 * seem onerous for output buffer allocation.
 
488
 */
 
489
uLong ZEXPORT deflateBound(strm, sourceLen)
 
490
    z_streamp strm;
 
491
    uLong sourceLen;
 
492
{
 
493
    deflate_state *s;
 
494
    uLong destLen;
 
495
 
 
496
    /* conservative upper bound */
 
497
    destLen = sourceLen +
 
498
              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
 
499
 
 
500
    /* if can't get parameters, return conservative bound */
 
501
    if (strm == Z_NULL || strm->state == Z_NULL)
 
502
        return destLen;
 
503
 
 
504
    /* if not default parameters, return conservative bound */
 
505
    s = strm->state;
 
506
    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
 
507
        return destLen;
 
508
 
 
509
    /* default settings: return tight bound for that case */
 
510
    return compressBound(sourceLen);
 
511
}
 
512
 
397
513
/* =========================================================================
398
514
 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
399
515
 * IN assertion: the stream state is correct and there is enough room in
405
521
{
406
522
    put_byte(s, (Byte)(b >> 8));
407
523
    put_byte(s, (Byte)(b & 0xff));
408
 
}   
 
524
}
409
525
 
410
526
/* =========================================================================
411
527
 * Flush as much pending output as possible. All deflate() output goes
441
557
    deflate_state *s;
442
558
 
443
559
    if (strm == Z_NULL || strm->state == Z_NULL ||
444
 
        flush > Z_FINISH || flush < 0) {
 
560
        flush > Z_FINISH || flush < 0) {
445
561
        return Z_STREAM_ERROR;
446
562
    }
447
563
    s = strm->state;
448
564
 
449
565
    if (strm->next_out == Z_NULL ||
450
566
        (strm->next_in == Z_NULL && strm->avail_in != 0) ||
451
 
        (s->status == FINISH_STATE && flush != Z_FINISH)) {
 
567
        (s->status == FINISH_STATE && flush != Z_FINISH)) {
452
568
        ERR_RETURN(strm, Z_STREAM_ERROR);
453
569
    }
454
570
    if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
457
573
    old_flush = s->last_flush;
458
574
    s->last_flush = flush;
459
575
 
460
 
    /* Write the zlib header */
 
576
    /* Write the header */
461
577
    if (s->status == INIT_STATE) {
462
 
 
463
 
        uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
464
 
        uInt level_flags = (s->level-1) >> 1;
465
 
 
466
 
        if (level_flags > 3) level_flags = 3;
467
 
        header |= (level_flags << 6);
468
 
        if (s->strstart != 0) header |= PRESET_DICT;
469
 
        header += 31 - (header % 31);
470
 
 
471
 
        s->status = BUSY_STATE;
472
 
        putShortMSB(s, header);
473
 
 
474
 
        /* Save the adler32 of the preset dictionary: */
475
 
        if (s->strstart != 0) {
476
 
            putShortMSB(s, (uInt)(strm->adler >> 16));
477
 
            putShortMSB(s, (uInt)(strm->adler & 0xffff));
478
 
        }
479
 
        strm->adler = 1L;
480
 
    }
 
578
#ifdef GZIP
 
579
        if (s->wrap == 2) {
 
580
            strm->adler = crc32(0L, Z_NULL, 0);
 
581
            put_byte(s, 31);
 
582
            put_byte(s, 139);
 
583
            put_byte(s, 8);
 
584
            if (s->gzhead == NULL) {
 
585
                put_byte(s, 0);
 
586
                put_byte(s, 0);
 
587
                put_byte(s, 0);
 
588
                put_byte(s, 0);
 
589
                put_byte(s, 0);
 
590
                put_byte(s, s->level == 9 ? 2 :
 
591
                            (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
 
592
                             4 : 0));
 
593
                put_byte(s, OS_CODE);
 
594
                s->status = BUSY_STATE;
 
595
            }
 
596
            else {
 
597
                put_byte(s, (s->gzhead->text ? 1 : 0) +
 
598
                            (s->gzhead->hcrc ? 2 : 0) +
 
599
                            (s->gzhead->extra == Z_NULL ? 0 : 4) +
 
600
                            (s->gzhead->name == Z_NULL ? 0 : 8) +
 
601
                            (s->gzhead->comment == Z_NULL ? 0 : 16)
 
602
                        );
 
603
                put_byte(s, (Byte)(s->gzhead->time & 0xff));
 
604
                put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
 
605
                put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
 
606
                put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
 
607
                put_byte(s, s->level == 9 ? 2 :
 
608
                            (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
 
609
                             4 : 0));
 
610
                put_byte(s, s->gzhead->os & 0xff);
 
611
                if (s->gzhead->extra != NULL) {
 
612
                    put_byte(s, s->gzhead->extra_len & 0xff);
 
613
                    put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
 
614
                }
 
615
                if (s->gzhead->hcrc)
 
616
                    strm->adler = crc32(strm->adler, s->pending_buf,
 
617
                                        s->pending);
 
618
                s->gzindex = 0;
 
619
                s->status = EXTRA_STATE;
 
620
            }
 
621
        }
 
622
        else
 
623
#endif
 
624
        {
 
625
            uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
 
626
            uInt level_flags;
 
627
 
 
628
            if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
 
629
                level_flags = 0;
 
630
            else if (s->level < 6)
 
631
                level_flags = 1;
 
632
            else if (s->level == 6)
 
633
                level_flags = 2;
 
634
            else
 
635
                level_flags = 3;
 
636
            header |= (level_flags << 6);
 
637
            if (s->strstart != 0) header |= PRESET_DICT;
 
638
            header += 31 - (header % 31);
 
639
 
 
640
            s->status = BUSY_STATE;
 
641
            putShortMSB(s, header);
 
642
 
 
643
            /* Save the adler32 of the preset dictionary: */
 
644
            if (s->strstart != 0) {
 
645
                putShortMSB(s, (uInt)(strm->adler >> 16));
 
646
                putShortMSB(s, (uInt)(strm->adler & 0xffff));
 
647
            }
 
648
            strm->adler = adler32(0L, Z_NULL, 0);
 
649
        }
 
650
    }
 
651
#ifdef GZIP
 
652
    if (s->status == EXTRA_STATE) {
 
653
        if (s->gzhead->extra != NULL) {
 
654
            uInt beg = s->pending;  /* start of bytes to update crc */
 
655
 
 
656
            while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
 
657
                if (s->pending == s->pending_buf_size) {
 
658
                    if (s->gzhead->hcrc && s->pending > beg)
 
659
                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
 
660
                                            s->pending - beg);
 
661
                    flush_pending(strm);
 
662
                    beg = s->pending;
 
663
                    if (s->pending == s->pending_buf_size)
 
664
                        break;
 
665
                }
 
666
                put_byte(s, s->gzhead->extra[s->gzindex]);
 
667
                s->gzindex++;
 
668
            }
 
669
            if (s->gzhead->hcrc && s->pending > beg)
 
670
                strm->adler = crc32(strm->adler, s->pending_buf + beg,
 
671
                                    s->pending - beg);
 
672
            if (s->gzindex == s->gzhead->extra_len) {
 
673
                s->gzindex = 0;
 
674
                s->status = NAME_STATE;
 
675
            }
 
676
        }
 
677
        else
 
678
            s->status = NAME_STATE;
 
679
    }
 
680
    if (s->status == NAME_STATE) {
 
681
        if (s->gzhead->name != NULL) {
 
682
            uInt beg = s->pending;  /* start of bytes to update crc */
 
683
            int val;
 
684
 
 
685
            do {
 
686
                if (s->pending == s->pending_buf_size) {
 
687
                    if (s->gzhead->hcrc && s->pending > beg)
 
688
                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
 
689
                                            s->pending - beg);
 
690
                    flush_pending(strm);
 
691
                    beg = s->pending;
 
692
                    if (s->pending == s->pending_buf_size) {
 
693
                        val = 1;
 
694
                        break;
 
695
                    }
 
696
                }
 
697
                val = s->gzhead->name[s->gzindex++];
 
698
                put_byte(s, val);
 
699
            } while (val != 0);
 
700
            if (s->gzhead->hcrc && s->pending > beg)
 
701
                strm->adler = crc32(strm->adler, s->pending_buf + beg,
 
702
                                    s->pending - beg);
 
703
            if (val == 0) {
 
704
                s->gzindex = 0;
 
705
                s->status = COMMENT_STATE;
 
706
            }
 
707
        }
 
708
        else
 
709
            s->status = COMMENT_STATE;
 
710
    }
 
711
    if (s->status == COMMENT_STATE) {
 
712
        if (s->gzhead->comment != NULL) {
 
713
            uInt beg = s->pending;  /* start of bytes to update crc */
 
714
            int val;
 
715
 
 
716
            do {
 
717
                if (s->pending == s->pending_buf_size) {
 
718
                    if (s->gzhead->hcrc && s->pending > beg)
 
719
                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
 
720
                                            s->pending - beg);
 
721
                    flush_pending(strm);
 
722
                    beg = s->pending;
 
723
                    if (s->pending == s->pending_buf_size) {
 
724
                        val = 1;
 
725
                        break;
 
726
                    }
 
727
                }
 
728
                val = s->gzhead->comment[s->gzindex++];
 
729
                put_byte(s, val);
 
730
            } while (val != 0);
 
731
            if (s->gzhead->hcrc && s->pending > beg)
 
732
                strm->adler = crc32(strm->adler, s->pending_buf + beg,
 
733
                                    s->pending - beg);
 
734
            if (val == 0)
 
735
                s->status = HCRC_STATE;
 
736
        }
 
737
        else
 
738
            s->status = HCRC_STATE;
 
739
    }
 
740
    if (s->status == HCRC_STATE) {
 
741
        if (s->gzhead->hcrc) {
 
742
            if (s->pending + 2 > s->pending_buf_size)
 
743
                flush_pending(strm);
 
744
            if (s->pending + 2 <= s->pending_buf_size) {
 
745
                put_byte(s, (Byte)(strm->adler & 0xff));
 
746
                put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
 
747
                strm->adler = crc32(0L, Z_NULL, 0);
 
748
                s->status = BUSY_STATE;
 
749
            }
 
750
        }
 
751
        else
 
752
            s->status = BUSY_STATE;
 
753
    }
 
754
#endif
481
755
 
482
756
    /* Flush as much pending output as possible */
483
757
    if (s->pending != 0) {
484
758
        flush_pending(strm);
485
759
        if (strm->avail_out == 0) {
486
 
            /* Since avail_out is 0, deflate will be called again with
487
 
             * more output space, but possibly with both pending and
488
 
             * avail_in equal to zero. There won't be anything to do,
489
 
             * but this is not an error situation so make sure we
490
 
             * return OK instead of BUF_ERROR at next call of deflate:
 
760
            /* Since avail_out is 0, deflate will be called again with
 
761
             * more output space, but possibly with both pending and
 
762
             * avail_in equal to zero. There won't be anything to do,
 
763
             * but this is not an error situation so make sure we
 
764
             * return OK instead of BUF_ERROR at next call of deflate:
491
765
             */
492
 
            s->last_flush = -1;
493
 
            return Z_OK;
494
 
        }
 
766
            s->last_flush = -1;
 
767
            return Z_OK;
 
768
        }
495
769
 
496
770
    /* Make sure there is something to do and avoid duplicate consecutive
497
771
     * flushes. For repeated and useless calls with Z_FINISH, we keep
498
 
     * returning Z_STREAM_END instead of Z_BUFF_ERROR.
 
772
     * returning Z_STREAM_END instead of Z_BUF_ERROR.
499
773
     */
500
774
    } else if (strm->avail_in == 0 && flush <= old_flush &&
501
 
               flush != Z_FINISH) {
 
775
               flush != Z_FINISH) {
502
776
        ERR_RETURN(strm, Z_BUF_ERROR);
503
777
    }
504
778
 
513
787
        (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
514
788
        block_state bstate;
515
789
 
516
 
        bstate = (*(configuration_table[s->level].func))(s, flush);
 
790
        bstate = (*(configuration_table[s->level].func))(s, flush);
517
791
 
518
792
        if (bstate == finish_started || bstate == finish_done) {
519
793
            s->status = FINISH_STATE;
520
794
        }
521
795
        if (bstate == need_more || bstate == finish_started) {
522
 
            if (strm->avail_out == 0) {
523
 
                s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
524
 
            }
525
 
            return Z_OK;
526
 
            /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
527
 
             * of deflate should use the same flush parameter to make sure
528
 
             * that the flush is complete. So we don't have to output an
529
 
             * empty block here, this will be done at next call. This also
530
 
             * ensures that for a very small output buffer, we emit at most
531
 
             * one empty block.
532
 
             */
533
 
        }
 
796
            if (strm->avail_out == 0) {
 
797
                s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
 
798
            }
 
799
            return Z_OK;
 
800
            /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
 
801
             * of deflate should use the same flush parameter to make sure
 
802
             * that the flush is complete. So we don't have to output an
 
803
             * empty block here, this will be done at next call. This also
 
804
             * ensures that for a very small output buffer, we emit at most
 
805
             * one empty block.
 
806
             */
 
807
        }
534
808
        if (bstate == block_done) {
535
809
            if (flush == Z_PARTIAL_FLUSH) {
536
810
                _tr_align(s);
544
818
                }
545
819
            }
546
820
            flush_pending(strm);
547
 
            if (strm->avail_out == 0) {
548
 
              s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
549
 
              return Z_OK;
550
 
            }
 
821
            if (strm->avail_out == 0) {
 
822
              s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
 
823
              return Z_OK;
 
824
            }
551
825
        }
552
826
    }
553
827
    Assert(strm->avail_out > 0, "bug2");
554
828
 
555
829
    if (flush != Z_FINISH) return Z_OK;
556
 
    if (s->noheader) return Z_STREAM_END;
 
830
    if (s->wrap <= 0) return Z_STREAM_END;
557
831
 
558
 
    /* Write the zlib trailer (adler32) */
559
 
    putShortMSB(s, (uInt)(strm->adler >> 16));
560
 
    putShortMSB(s, (uInt)(strm->adler & 0xffff));
 
832
    /* Write the trailer */
 
833
#ifdef GZIP
 
834
    if (s->wrap == 2) {
 
835
        put_byte(s, (Byte)(strm->adler & 0xff));
 
836
        put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
 
837
        put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
 
838
        put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
 
839
        put_byte(s, (Byte)(strm->total_in & 0xff));
 
840
        put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
 
841
        put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
 
842
        put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
 
843
    }
 
844
    else
 
845
#endif
 
846
    {
 
847
        putShortMSB(s, (uInt)(strm->adler >> 16));
 
848
        putShortMSB(s, (uInt)(strm->adler & 0xffff));
 
849
    }
561
850
    flush_pending(strm);
562
851
    /* If avail_out is zero, the application will call deflate again
563
852
     * to flush the rest.
564
853
     */
565
 
    s->noheader = -1; /* write the trailer only once! */
 
854
    if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
566
855
    return s->pending != 0 ? Z_OK : Z_STREAM_END;
567
856
}
568
857
 
575
864
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
576
865
 
577
866
    status = strm->state->status;
578
 
    if (status != INIT_STATE && status != BUSY_STATE &&
579
 
        status != FINISH_STATE) {
 
867
    if (status != INIT_STATE &&
 
868
        status != EXTRA_STATE &&
 
869
        status != NAME_STATE &&
 
870
        status != COMMENT_STATE &&
 
871
        status != HCRC_STATE &&
 
872
        status != BUSY_STATE &&
 
873
        status != FINISH_STATE) {
580
874
      return Z_STREAM_ERROR;
581
875
    }
582
876
 
615
909
 
616
910
    ss = source->state;
617
911
 
618
 
    *dest = *source;
 
912
    zmemcpy(dest, source, sizeof(z_stream));
619
913
 
620
914
    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
621
915
    if (ds == Z_NULL) return Z_MEM_ERROR;
622
916
    dest->state = (struct internal_state FAR *) ds;
623
 
    *ds = *ss;
 
917
    zmemcpy(ds, ss, sizeof(deflate_state));
624
918
    ds->strm = dest;
625
919
 
626
920
    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
649
943
    ds->bl_desc.dyn_tree = ds->bl_tree;
650
944
 
651
945
    return Z_OK;
652
 
#endif
 
946
#endif /* MAXSEG_64K */
653
947
}
654
948
 
655
949
/* ===========================================================================
671
965
 
672
966
    strm->avail_in  -= len;
673
967
 
674
 
    if (!strm->state->noheader) {
 
968
    if (strm->state->wrap == 1) {
675
969
        strm->adler = adler32(strm->adler, strm->next_in, len);
676
970
    }
 
971
#ifdef GZIP
 
972
    else if (strm->state->wrap == 2) {
 
973
        strm->adler = crc32(strm->adler, strm->next_in, len);
 
974
    }
 
975
#endif
677
976
    zmemcpy(buf, strm->next_in, len);
678
977
    strm->next_in  += len;
679
978
    strm->total_in += len;
704
1003
    s->match_length = s->prev_length = MIN_MATCH-1;
705
1004
    s->match_available = 0;
706
1005
    s->ins_h = 0;
 
1006
#ifndef FASTEST
707
1007
#ifdef ASMV
708
1008
    match_init(); /* initialize the asm code */
709
1009
#endif
 
1010
#endif
710
1011
}
711
1012
 
 
1013
#ifndef FASTEST
712
1014
/* ===========================================================================
713
1015
 * Set match_start to the longest match starting at the given string and
714
1016
 * return its length. Matches shorter or equal to prev_length are discarded,
722
1024
/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
723
1025
 * match.S. The code will be functionally equivalent.
724
1026
 */
725
 
#ifndef FASTEST
726
1027
local uInt longest_match(s, cur_match)
727
1028
    deflate_state *s;
728
1029
    IPos cur_match;                             /* current match */
775
1076
        match = s->window + cur_match;
776
1077
 
777
1078
        /* Skip to next match if the match length cannot increase
778
 
         * or if the match length is less than 2:
 
1079
         * or if the match length is less than 2.  Note that the checks below
 
1080
         * for insufficient lookahead only occur occasionally for performance
 
1081
         * reasons.  Therefore uninitialized memory will be accessed, and
 
1082
         * conditional jumps will be made that depend on those values.
 
1083
         * However the length of the match is limited to the lookahead, so
 
1084
         * the output of deflate is not affected by the uninitialized values.
779
1085
         */
780
1086
#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
781
1087
        /* This code assumes sizeof(unsigned short) == 2. Do not use
860
1166
    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
861
1167
    return s->lookahead;
862
1168
}
 
1169
#endif /* ASMV */
 
1170
#endif /* FASTEST */
863
1171
 
864
 
#else /* FASTEST */
865
1172
/* ---------------------------------------------------------------------------
866
 
 * Optimized version for level == 1 only
 
1173
 * Optimized version for level == 1 or strategy == Z_RLE only
867
1174
 */
868
 
local uInt longest_match(s, cur_match)
 
1175
local uInt longest_match_fast(s, cur_match)
869
1176
    deflate_state *s;
870
1177
    IPos cur_match;                             /* current match */
871
1178
{
903
1210
     */
904
1211
    do {
905
1212
    } while (*++scan == *++match && *++scan == *++match &&
906
 
             *++scan == *++match && *++scan == *++match &&
907
 
             *++scan == *++match && *++scan == *++match &&
908
 
             *++scan == *++match && *++scan == *++match &&
909
 
             scan < strend);
 
1213
             *++scan == *++match && *++scan == *++match &&
 
1214
             *++scan == *++match && *++scan == *++match &&
 
1215
             *++scan == *++match && *++scan == *++match &&
 
1216
             scan < strend);
910
1217
 
911
1218
    Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
912
1219
 
915
1222
    if (len < MIN_MATCH) return MIN_MATCH - 1;
916
1223
 
917
1224
    s->match_start = cur_match;
918
 
    return len <= s->lookahead ? len : s->lookahead;
 
1225
    return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
919
1226
}
920
 
#endif /* FASTEST */
921
 
#endif /* ASMV */
922
1227
 
923
1228
#ifdef DEBUG
924
1229
/* ===========================================================================
933
1238
    if (zmemcmp(s->window + match,
934
1239
                s->window + start, length) != EQUAL) {
935
1240
        fprintf(stderr, " start %u, match %u, length %d\n",
936
 
                start, match, length);
 
1241
                start, match, length);
937
1242
        do {
938
 
            fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
939
 
        } while (--length != 0);
 
1243
            fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
 
1244
        } while (--length != 0);
940
1245
        z_error("invalid match");
941
1246
    }
942
1247
    if (z_verbose > 1) {
946
1251
}
947
1252
#else
948
1253
#  define check_match(s, start, match, length)
949
 
#endif
 
1254
#endif /* DEBUG */
950
1255
 
951
1256
/* ===========================================================================
952
1257
 * Fill the window when the lookahead becomes insufficient.
970
1275
        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
971
1276
 
972
1277
        /* Deal with !@#$% 64K limit: */
973
 
        if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
974
 
            more = wsize;
 
1278
        if (sizeof(int) <= 2) {
 
1279
            if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
 
1280
                more = wsize;
975
1281
 
976
 
        } else if (more == (unsigned)(-1)) {
977
 
            /* Very unlikely, but possible on 16 bit machine if strstart == 0
978
 
             * and lookahead == 1 (input done one byte at time)
979
 
             */
980
 
            more--;
 
1282
            } else if (more == (unsigned)(-1)) {
 
1283
                /* Very unlikely, but possible on 16 bit machine if
 
1284
                 * strstart == 0 && lookahead == 1 (input done a byte at time)
 
1285
                 */
 
1286
                more--;
 
1287
            }
 
1288
        }
981
1289
 
982
1290
        /* If the window is almost full and there is insufficient lookahead,
983
1291
         * move the upper half to the lower one to make room in the upper half.
984
1292
         */
985
 
        } else if (s->strstart >= wsize+MAX_DIST(s)) {
 
1293
        if (s->strstart >= wsize+MAX_DIST(s)) {
986
1294
 
987
1295
            zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
988
1296
            s->match_start -= wsize;
995
1303
               later. (Using level 0 permanently is not an optimal usage of
996
1304
               zlib, so we don't care about this pathological case.)
997
1305
             */
998
 
            n = s->hash_size;
999
 
            p = &s->head[n];
1000
 
            do {
1001
 
                m = *--p;
1002
 
                *p = (Pos)(m >= wsize ? m-wsize : NIL);
1003
 
            } while (--n);
 
1306
            /* %%% avoid this when Z_RLE */
 
1307
            n = s->hash_size;
 
1308
            p = &s->head[n];
 
1309
            do {
 
1310
                m = *--p;
 
1311
                *p = (Pos)(m >= wsize ? m-wsize : NIL);
 
1312
            } while (--n);
1004
1313
 
1005
 
            n = wsize;
 
1314
            n = wsize;
1006
1315
#ifndef FASTEST
1007
 
            p = &s->prev[n];
1008
 
            do {
1009
 
                m = *--p;
1010
 
                *p = (Pos)(m >= wsize ? m-wsize : NIL);
1011
 
                /* If n is not on any hash chain, prev[n] is garbage but
1012
 
                 * its value will never be used.
1013
 
                 */
1014
 
            } while (--n);
 
1316
            p = &s->prev[n];
 
1317
            do {
 
1318
                m = *--p;
 
1319
                *p = (Pos)(m >= wsize ? m-wsize : NIL);
 
1320
                /* If n is not on any hash chain, prev[n] is garbage but
 
1321
                 * its value will never be used.
 
1322
                 */
 
1323
            } while (--n);
1015
1324
#endif
1016
1325
            more += wsize;
1017
1326
        }
1056
1365
   _tr_flush_block(s, (s->block_start >= 0L ? \
1057
1366
                   (charf *)&s->window[(unsigned)s->block_start] : \
1058
1367
                   (charf *)Z_NULL), \
1059
 
                (ulg)((long)s->strstart - s->block_start), \
1060
 
                (eof)); \
 
1368
                (ulg)((long)s->strstart - s->block_start), \
 
1369
                (eof)); \
1061
1370
   s->block_start = s->strstart; \
1062
1371
   flush_pending(s->strm); \
1063
1372
   Tracev((stderr,"[FLUSH]")); \
1098
1407
        if (s->lookahead <= 1) {
1099
1408
 
1100
1409
            Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1101
 
                   s->block_start >= (long)s->w_size, "slide too late");
 
1410
                   s->block_start >= (long)s->w_size, "slide too late");
1102
1411
 
1103
1412
            fill_window(s);
1104
1413
            if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1105
1414
 
1106
1415
            if (s->lookahead == 0) break; /* flush the current block */
1107
1416
        }
1108
 
        Assert(s->block_start >= 0L, "block gone");
1109
 
 
1110
 
        s->strstart += s->lookahead;
1111
 
        s->lookahead = 0;
1112
 
 
1113
 
        /* Emit a stored block if pending_buf will be full: */
1114
 
        max_start = s->block_start + max_block_size;
 
1417
        Assert(s->block_start >= 0L, "block gone");
 
1418
 
 
1419
        s->strstart += s->lookahead;
 
1420
        s->lookahead = 0;
 
1421
 
 
1422
        /* Emit a stored block if pending_buf will be full: */
 
1423
        max_start = s->block_start + max_block_size;
1115
1424
        if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1116
 
            /* strstart == 0 is possible when wraparound on 16-bit machine */
1117
 
            s->lookahead = (uInt)(s->strstart - max_start);
1118
 
            s->strstart = (uInt)max_start;
 
1425
            /* strstart == 0 is possible when wraparound on 16-bit machine */
 
1426
            s->lookahead = (uInt)(s->strstart - max_start);
 
1427
            s->strstart = (uInt)max_start;
1119
1428
            FLUSH_BLOCK(s, 0);
1120
 
        }
1121
 
        /* Flush if we may have to slide, otherwise block_start may become
 
1429
        }
 
1430
        /* Flush if we may have to slide, otherwise block_start may become
1122
1431
         * negative and the data will be gone:
1123
1432
         */
1124
1433
        if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1125
1434
            FLUSH_BLOCK(s, 0);
1126
 
        }
 
1435
        }
1127
1436
    }
1128
1437
    FLUSH_BLOCK(s, flush == Z_FINISH);
1129
1438
    return flush == Z_FINISH ? finish_done : block_done;
1152
1461
        if (s->lookahead < MIN_LOOKAHEAD) {
1153
1462
            fill_window(s);
1154
1463
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1155
 
                return need_more;
1156
 
            }
 
1464
                return need_more;
 
1465
            }
1157
1466
            if (s->lookahead == 0) break; /* flush the current block */
1158
1467
        }
1159
1468
 
1172
1481
             * of window index 0 (in particular we have to avoid a match
1173
1482
             * of the string with itself at the start of the input file).
1174
1483
             */
1175
 
            if (s->strategy != Z_HUFFMAN_ONLY) {
 
1484
#ifdef FASTEST
 
1485
            if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
 
1486
                (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
 
1487
                s->match_length = longest_match_fast (s, hash_head);
 
1488
            }
 
1489
#else
 
1490
            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1176
1491
                s->match_length = longest_match (s, hash_head);
 
1492
            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
 
1493
                s->match_length = longest_match_fast (s, hash_head);
1177
1494
            }
1178
 
            /* longest_match() sets match_start */
 
1495
#endif
 
1496
            /* longest_match() or longest_match_fast() sets match_start */
1179
1497
        }
1180
1498
        if (s->match_length >= MIN_MATCH) {
1181
1499
            check_match(s, s->strstart, s->match_start, s->match_length);
1191
1509
#ifndef FASTEST
1192
1510
            if (s->match_length <= s->max_insert_length &&
1193
1511
                s->lookahead >= MIN_MATCH) {
1194
 
                s->match_length--; /* string at strstart already in hash table */
 
1512
                s->match_length--; /* string at strstart already in table */
1195
1513
                do {
1196
1514
                    s->strstart++;
1197
1515
                    INSERT_STRING(s, s->strstart, hash_head);
1199
1517
                     * always MIN_MATCH bytes ahead.
1200
1518
                     */
1201
1519
                } while (--s->match_length != 0);
1202
 
                s->strstart++; 
 
1520
                s->strstart++;
1203
1521
            } else
1204
1522
#endif
1205
 
            {
 
1523
            {
1206
1524
                s->strstart += s->match_length;
1207
1525
                s->match_length = 0;
1208
1526
                s->ins_h = s->window[s->strstart];
1219
1537
            Tracevv((stderr,"%c", s->window[s->strstart]));
1220
1538
            _tr_tally_lit (s, s->window[s->strstart], bflush);
1221
1539
            s->lookahead--;
1222
 
            s->strstart++; 
 
1540
            s->strstart++;
1223
1541
        }
1224
1542
        if (bflush) FLUSH_BLOCK(s, 0);
1225
1543
    }
1227
1545
    return flush == Z_FINISH ? finish_done : block_done;
1228
1546
}
1229
1547
 
 
1548
#ifndef FASTEST
1230
1549
/* ===========================================================================
1231
1550
 * Same as above, but achieves better compression. We use a lazy
1232
1551
 * evaluation for matches: a match is finally adopted only if there is
1249
1568
        if (s->lookahead < MIN_LOOKAHEAD) {
1250
1569
            fill_window(s);
1251
1570
            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1252
 
                return need_more;
1253
 
            }
 
1571
                return need_more;
 
1572
            }
1254
1573
            if (s->lookahead == 0) break; /* flush the current block */
1255
1574
        }
1256
1575
 
1272
1591
             * of window index 0 (in particular we have to avoid a match
1273
1592
             * of the string with itself at the start of the input file).
1274
1593
             */
1275
 
            if (s->strategy != Z_HUFFMAN_ONLY) {
 
1594
            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1276
1595
                s->match_length = longest_match (s, hash_head);
 
1596
            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
 
1597
                s->match_length = longest_match_fast (s, hash_head);
1277
1598
            }
1278
 
            /* longest_match() sets match_start */
 
1599
            /* longest_match() or longest_match_fast() sets match_start */
1279
1600
 
1280
 
            if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
1281
 
                 (s->match_length == MIN_MATCH &&
1282
 
                  s->strstart - s->match_start > TOO_FAR))) {
 
1601
            if (s->match_length <= 5 && (s->strategy == Z_FILTERED
 
1602
#if TOO_FAR <= 32767
 
1603
                || (s->match_length == MIN_MATCH &&
 
1604
                    s->strstart - s->match_start > TOO_FAR)
 
1605
#endif
 
1606
                )) {
1283
1607
 
1284
1608
                /* If prev_match is also MIN_MATCH, match_start is garbage
1285
1609
                 * but we will ignore the current match anyway.
1297
1621
            check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1298
1622
 
1299
1623
            _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1300
 
                           s->prev_length - MIN_MATCH, bflush);
 
1624
                           s->prev_length - MIN_MATCH, bflush);
1301
1625
 
1302
1626
            /* Insert in hash table all strings up to the end of the match.
1303
1627
             * strstart-1 and strstart are already inserted. If there is not
1323
1647
             * is longer, truncate the previous match to a single literal.
1324
1648
             */
1325
1649
            Tracevv((stderr,"%c", s->window[s->strstart-1]));
1326
 
            _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1327
 
            if (bflush) {
 
1650
            _tr_tally_lit(s, s->window[s->strstart-1], bflush);
 
1651
            if (bflush) {
1328
1652
                FLUSH_BLOCK_ONLY(s, 0);
1329
1653
            }
1330
1654
            s->strstart++;
1348
1672
    FLUSH_BLOCK(s, flush == Z_FINISH);
1349
1673
    return flush == Z_FINISH ? finish_done : block_done;
1350
1674
}
 
1675
#endif /* FASTEST */
 
1676
 
 
1677
#if 0
 
1678
/* ===========================================================================
 
1679
 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
 
1680
 * one.  Do not maintain a hash table.  (It will be regenerated if this run of
 
1681
 * deflate switches away from Z_RLE.)
 
1682
 */
 
1683
local block_state deflate_rle(s, flush)
 
1684
    deflate_state *s;
 
1685
    int flush;
 
1686
{
 
1687
    int bflush;         /* set if current block must be flushed */
 
1688
    uInt run;           /* length of run */
 
1689
    uInt max;           /* maximum length of run */
 
1690
    uInt prev;          /* byte at distance one to match */
 
1691
    Bytef *scan;        /* scan for end of run */
 
1692
 
 
1693
    for (;;) {
 
1694
        /* Make sure that we always have enough lookahead, except
 
1695
         * at the end of the input file. We need MAX_MATCH bytes
 
1696
         * for the longest encodable run.
 
1697
         */
 
1698
        if (s->lookahead < MAX_MATCH) {
 
1699
            fill_window(s);
 
1700
            if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
 
1701
                return need_more;
 
1702
            }
 
1703
            if (s->lookahead == 0) break; /* flush the current block */
 
1704
        }
 
1705
 
 
1706
        /* See how many times the previous byte repeats */
 
1707
        run = 0;
 
1708
        if (s->strstart > 0) {      /* if there is a previous byte, that is */
 
1709
            max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
 
1710
            scan = s->window + s->strstart - 1;
 
1711
            prev = *scan++;
 
1712
            do {
 
1713
                if (*scan++ != prev)
 
1714
                    break;
 
1715
            } while (++run < max);
 
1716
        }
 
1717
 
 
1718
        /* Emit match if have run of MIN_MATCH or longer, else emit literal */
 
1719
        if (run >= MIN_MATCH) {
 
1720
            check_match(s, s->strstart, s->strstart - 1, run);
 
1721
            _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
 
1722
            s->lookahead -= run;
 
1723
            s->strstart += run;
 
1724
        } else {
 
1725
            /* No match, output a literal byte */
 
1726
            Tracevv((stderr,"%c", s->window[s->strstart]));
 
1727
            _tr_tally_lit (s, s->window[s->strstart], bflush);
 
1728
            s->lookahead--;
 
1729
            s->strstart++;
 
1730
        }
 
1731
        if (bflush) FLUSH_BLOCK(s, 0);
 
1732
    }
 
1733
    FLUSH_BLOCK(s, flush == Z_FINISH);
 
1734
    return flush == Z_FINISH ? finish_done : block_done;
 
1735
}
 
1736
#endif