Changeset 5a7aa10 in rtems


Ignore:
Timestamp:
03/18/11 10:11:21 (13 years ago)
Author:
Ralf Corsepius <ralf.corsepius@…>
Branches:
4.11, 5, master
Children:
d71b3a7
Parents:
f7d9aea2
Message:

Import from zlib-1.2.4

Location:
cpukit/zlib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpukit/zlib/deflate.c

    rf7d9aea2 r5a7aa10  
    11/* deflate.c -- compress data using the deflation algorithm
    2  * Copyright (C) 1995-2005 Jean-loup Gailly.
     2 * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
    33 * For conditions of distribution and use, see copyright notice in zlib.h
    44 */
     
    5353
    5454const char deflate_copyright[] =
    55    " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
     55   " deflate 1.2.4 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
    5656/*
    5757  If you use the zlib library in a product, an acknowledgment is welcome
     
    8080local block_state deflate_slow   OF((deflate_state *s, int flush));
    8181#endif
     82local block_state deflate_rle    OF((deflate_state *s, int flush));
     83local block_state deflate_huff   OF((deflate_state *s, int flush));
    8284local void lm_init        OF((deflate_state *s));
    8385local void putShortMSB    OF((deflate_state *s, uInt b));
    8486local void flush_pending  OF((z_streamp strm));
    8587local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
    86 #ifndef FASTEST
    8788#ifdef ASMV
    8889      void match_init OF((void)); /* asm code initialization */
     
    9192local uInt longest_match  OF((deflate_state *s, IPos cur_match));
    9293#endif
    93 #endif
    94 local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
    9594
    9695#ifdef DEBUG
     
    110109#endif
    111110/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
    112 
    113 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
    114 /* Minimum amount of lookahead, except at the end of the input file.
    115  * See deflate.c for comments about the MIN_MATCH+1.
    116  */
    117111
    118112/* Values for max_lazy_match, good_match and max_chain_length, depending on
     
    289283    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
    290284
     285    s->high_water = 0;      /* nothing written to s->window yet */
     286
    291287    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
    292288
     
    333329
    334330    if (length < MIN_MATCH) return Z_OK;
    335     if (length > MAX_DIST(s)) {
    336         length = MAX_DIST(s);
     331    if (length > s->w_size) {
     332        length = s->w_size;
    337333        dictionary += dictLength - length; /* use the tail of the dictionary */
    338334    }
     
    436432    func = configuration_table[s->level].func;
    437433
    438     if (func != configuration_table[level].func && strm->total_in != 0) {
     434    if ((strategy != s->strategy || func != configuration_table[level].func) &&
     435        strm->total_in != 0) {
    439436        /* Flush the last buffer: */
    440         err = deflate(strm, Z_PARTIAL_FLUSH);
     437        err = deflate(strm, Z_BLOCK);
    441438    }
    442439    if (s->level != level) {
     
    482479 * can emit on compressed data for some combinations of the parameters.
    483480 *
    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.
     481 * This function could be more sophisticated to provide closer upper bounds for
     482 * every combination of windowBits and memLevel.  But even the conservative
     483 * upper bound of about 14% expansion does not seem onerous for output buffer
     484 * allocation.
    488485 */
    489486uLong ZEXPORT deflateBound(strm, sourceLen)
     
    492489{
    493490    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 */
     491    uLong complen, wraplen;
     492    Bytef *str;
     493
     494    /* conservative upper bound for compressed data */
     495    complen = sourceLen +
     496              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
     497
     498    /* if can't get parameters, return conservative bound plus zlib wrapper */
    501499    if (strm == Z_NULL || strm->state == Z_NULL)
    502         return destLen;
     500        return complen + 6;
     501
     502    /* compute wrapper length */
     503    s = strm->state;
     504    switch (s->wrap) {
     505    case 0:                                 /* raw deflate */
     506        wraplen = 0;
     507        break;
     508    case 1:                                 /* zlib wrapper */
     509        wraplen = 6 + (s->strstart ? 4 : 0);
     510        break;
     511    case 2:                                 /* gzip wrapper */
     512        wraplen = 18;
     513        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
     514            if (s->gzhead->extra != Z_NULL)
     515                wraplen += 2 + s->gzhead->extra_len;
     516            str = s->gzhead->name;
     517            if (str != Z_NULL)
     518                do {
     519                    wraplen++;
     520                } while (*str++);
     521            str = s->gzhead->comment;
     522            if (str != Z_NULL)
     523                do {
     524                    wraplen++;
     525                } while (*str++);
     526            if (s->gzhead->hcrc)
     527                wraplen += 2;
     528        }
     529        break;
     530    default:                                /* for compiler happiness */
     531        wraplen = 6;
     532    }
    503533
    504534    /* if not default parameters, return conservative bound */
    505     s = strm->state;
    506535    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
    507         return destLen;
     536        return complen + wraplen;
    508537
    509538    /* default settings: return tight bound for that case */
    510     return compressBound(sourceLen);
     539    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
     540           (sourceLen >> 25) + 13 - 6 + wraplen;
    511541}
    512542
     
    558588
    559589    if (strm == Z_NULL || strm->state == Z_NULL ||
    560         flush > Z_FINISH || flush < 0) {
     590        flush > Z_BLOCK || flush < 0) {
    561591        return Z_STREAM_ERROR;
    562592    }
     
    582612            put_byte(s, 139);
    583613            put_byte(s, 8);
    584             if (s->gzhead == NULL) {
     614            if (s->gzhead == Z_NULL) {
    585615                put_byte(s, 0);
    586616                put_byte(s, 0);
     
    609639                             4 : 0));
    610640                put_byte(s, s->gzhead->os & 0xff);
    611                 if (s->gzhead->extra != NULL) {
     641                if (s->gzhead->extra != Z_NULL) {
    612642                    put_byte(s, s->gzhead->extra_len & 0xff);
    613643                    put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
     
    651681#ifdef GZIP
    652682    if (s->status == EXTRA_STATE) {
    653         if (s->gzhead->extra != NULL) {
     683        if (s->gzhead->extra != Z_NULL) {
    654684            uInt beg = s->pending;  /* start of bytes to update crc */
    655685
     
    679709    }
    680710    if (s->status == NAME_STATE) {
    681         if (s->gzhead->name != NULL) {
     711        if (s->gzhead->name != Z_NULL) {
    682712            uInt beg = s->pending;  /* start of bytes to update crc */
    683713            int val;
     
    710740    }
    711741    if (s->status == COMMENT_STATE) {
    712         if (s->gzhead->comment != NULL) {
     742        if (s->gzhead->comment != Z_NULL) {
    713743            uInt beg = s->pending;  /* start of bytes to update crc */
    714744            int val;
     
    788818        block_state bstate;
    789819
    790         bstate = (*(configuration_table[s->level].func))(s, flush);
     820        bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
     821                    (s->strategy == Z_RLE ? deflate_rle(s, flush) :
     822                        (*(configuration_table[s->level].func))(s, flush));
    791823
    792824        if (bstate == finish_started || bstate == finish_done) {
     
    809841            if (flush == Z_PARTIAL_FLUSH) {
    810842                _tr_align(s);
    811             } else { /* FULL_FLUSH or SYNC_FLUSH */
     843            } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
    812844                _tr_stored_block(s, (char*)0, 0L, 0);
    813845                /* For a full flush, this empty block will be recognized
     
    816848                if (flush == Z_FULL_FLUSH) {
    817849                    CLEAR_HASH(s);             /* forget history */
     850                    if (s->lookahead == 0) {
     851                        s->strstart = 0;
     852                        s->block_start = 0L;
     853                    }
    818854                }
    819855            }
     
    11681204}
    11691205#endif /* ASMV */
    1170 #endif /* FASTEST */
     1206
     1207#else /* FASTEST */
    11711208
    11721209/* ---------------------------------------------------------------------------
    1173  * Optimized version for level == 1 or strategy == Z_RLE only
    1174  */
    1175 local uInt longest_match_fast(s, cur_match)
     1210 * Optimized version for FASTEST only
     1211 */
     1212local uInt longest_match(s, cur_match)
    11761213    deflate_state *s;
    11771214    IPos cur_match;                             /* current match */
     
    12261263}
    12271264
     1265#endif /* FASTEST */
     1266
    12281267#ifdef DEBUG
    12291268/* ===========================================================================
     
    13041343               zlib, so we don't care about this pathological case.)
    13051344             */
    1306             /* %%% avoid this when Z_RLE */
    13071345            n = s->hash_size;
    13081346            p = &s->head[n];
     
    13561394
    13571395    } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
     1396
     1397    /* If the WIN_INIT bytes after the end of the current data have never been
     1398     * written, then zero those bytes in order to avoid memory check reports of
     1399     * the use of uninitialized (or uninitialised as Julian writes) bytes by
     1400     * the longest match routines.  Update the high water mark for the next
     1401     * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
     1402     * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
     1403     */
     1404    if (s->high_water < s->window_size) {
     1405        ulg curr = s->strstart + (ulg)(s->lookahead);
     1406        ulg init;
     1407
     1408        if (s->high_water < curr) {
     1409            /* Previous high water mark below current data -- zero WIN_INIT
     1410             * bytes or up to end of window, whichever is less.
     1411             */
     1412            init = s->window_size - curr;
     1413            if (init > WIN_INIT)
     1414                init = WIN_INIT;
     1415            zmemzero(s->window + curr, (unsigned)init);
     1416            s->high_water = curr + init;
     1417        }
     1418        else if (s->high_water < (ulg)curr + WIN_INIT) {
     1419            /* High water mark at or above current data, but below current data
     1420             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
     1421             * to end of window, whichever is less.
     1422             */
     1423            init = (ulg)curr + WIN_INIT - s->high_water;
     1424            if (init > s->window_size - s->high_water)
     1425                init = s->window_size - s->high_water;
     1426            zmemzero(s->window + s->high_water, (unsigned)init);
     1427            s->high_water += init;
     1428        }
     1429    }
    13581430}
    13591431
     
    13621434 * IN assertion: strstart is set to the end of the current match.
    13631435 */
    1364 #define FLUSH_BLOCK_ONLY(s, eof) { \
     1436#define FLUSH_BLOCK_ONLY(s, last) { \
    13651437   _tr_flush_block(s, (s->block_start >= 0L ? \
    13661438                   (charf *)&s->window[(unsigned)s->block_start] : \
    13671439                   (charf *)Z_NULL), \
    13681440                (ulg)((long)s->strstart - s->block_start), \
    1369                 (eof)); \
     1441                (last)); \
    13701442   s->block_start = s->strstart; \
    13711443   flush_pending(s->strm); \
     
    13741446
    13751447/* Same but force premature exit if necessary. */
    1376 #define FLUSH_BLOCK(s, eof) { \
    1377    FLUSH_BLOCK_ONLY(s, eof); \
    1378    if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
     1448#define FLUSH_BLOCK(s, last) { \
     1449   FLUSH_BLOCK_ONLY(s, last); \
     1450   if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
    13791451}
    13801452
     
    14501522    int flush;
    14511523{
    1452     IPos hash_head = NIL; /* head of the hash chain */
     1524    IPos hash_head;      /* head of the hash chain */
    14531525    int bflush;           /* set if current block must be flushed */
    14541526
     
    14701542         * dictionary, and set hash_head to the head of the hash chain:
    14711543         */
     1544        hash_head = NIL;
    14721545        if (s->lookahead >= MIN_MATCH) {
    14731546            INSERT_STRING(s, s->strstart, hash_head);
     
    14821555             * of the string with itself at the start of the input file).
    14831556             */
    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) {
    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);
    1494             }
    1495 #endif
    1496             /* longest_match() or longest_match_fast() sets match_start */
     1557            s->match_length = longest_match (s, hash_head);
     1558            /* longest_match() sets match_start */
    14971559        }
    14981560        if (s->match_length >= MIN_MATCH) {
     
    15561618    int flush;
    15571619{
    1558     IPos hash_head = NIL;    /* head of hash chain */
     1620    IPos hash_head;          /* head of hash chain */
    15591621    int bflush;              /* set if current block must be flushed */
    15601622
     
    15771639         * dictionary, and set hash_head to the head of the hash chain:
    15781640         */
     1641        hash_head = NIL;
    15791642        if (s->lookahead >= MIN_MATCH) {
    15801643            INSERT_STRING(s, s->strstart, hash_head);
     
    15921655             * of the string with itself at the start of the input file).
    15931656             */
    1594             if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
    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);
    1598             }
    1599             /* longest_match() or longest_match_fast() sets match_start */
     1657            s->match_length = longest_match (s, hash_head);
     1658            /* longest_match() sets match_start */
    16001659
    16011660            if (s->match_length <= 5 && (s->strategy == Z_FILTERED
     
    16751734#endif /* FASTEST */
    16761735
    1677 #if 0
    16781736/* ===========================================================================
    16791737 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
     
    16851743    int flush;
    16861744{
    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 */
     1745    int bflush;             /* set if current block must be flushed */
     1746    uInt prev;              /* byte at distance one to match */
     1747    Bytef *scan, *strend;   /* scan goes up to strend for length of run */
    16921748
    16931749    for (;;) {
     
    17051761
    17061762        /* 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;
     1763        s->match_length = 0;
     1764        if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
    17101765            scan = s->window + s->strstart - 1;
    1711             prev = *scan++;
    1712             do {
    1713                 if (*scan++ != prev)
    1714                     break;
    1715             } while (++run < max);
     1766            prev = *scan;
     1767            if (prev == *++scan && prev == *++scan && prev == *++scan) {
     1768                strend = s->window + s->strstart + MAX_MATCH;
     1769                do {
     1770                } while (prev == *++scan && prev == *++scan &&
     1771                         prev == *++scan && prev == *++scan &&
     1772                         prev == *++scan && prev == *++scan &&
     1773                         prev == *++scan && prev == *++scan &&
     1774                         scan < strend);
     1775                s->match_length = MAX_MATCH - (int)(strend - scan);
     1776                if (s->match_length > s->lookahead)
     1777                    s->match_length = s->lookahead;
     1778            }
    17161779        }
    17171780
    17181781        /* 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;
     1782        if (s->match_length >= MIN_MATCH) {
     1783            check_match(s, s->strstart, s->strstart - 1, s->match_length);
     1784
     1785            _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
     1786
     1787            s->lookahead -= s->match_length;
     1788            s->strstart += s->match_length;
     1789            s->match_length = 0;
    17241790        } else {
    17251791            /* No match, output a literal byte */
     
    17341800    return flush == Z_FINISH ? finish_done : block_done;
    17351801}
    1736 #endif
     1802
     1803/* ===========================================================================
     1804 * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
     1805 * (It will be regenerated if this run of deflate switches away from Huffman.)
     1806 */
     1807local block_state deflate_huff(s, flush)
     1808    deflate_state *s;
     1809    int flush;
     1810{
     1811    int bflush;             /* set if current block must be flushed */
     1812
     1813    for (;;) {
     1814        /* Make sure that we have a literal to write. */
     1815        if (s->lookahead == 0) {
     1816            fill_window(s);
     1817            if (s->lookahead == 0) {
     1818                if (flush == Z_NO_FLUSH)
     1819                    return need_more;
     1820                break;      /* flush the current block */
     1821            }
     1822        }
     1823
     1824        /* Output a literal byte */
     1825        s->match_length = 0;
     1826        Tracevv((stderr,"%c", s->window[s->strstart]));
     1827        _tr_tally_lit (s, s->window[s->strstart], bflush);
     1828        s->lookahead--;
     1829        s->strstart++;
     1830        if (bflush) FLUSH_BLOCK(s, 0);
     1831    }
     1832    FLUSH_BLOCK(s, flush == Z_FINISH);
     1833    return flush == Z_FINISH ? finish_done : block_done;
     1834}
  • cpukit/zlib/deflate.h

    rf7d9aea2 r5a7aa10  
    11/* deflate.h -- internal compression state
    2  * Copyright (C) 1995-2004 Jean-loup Gailly
     2 * Copyright (C) 1995-2009 Jean-loup Gailly
    33 * For conditions of distribution and use, see copyright notice in zlib.h
    44 */
     
    261261     */
    262262
     263    ulg high_water;
     264    /* High water mark offset in window for initialized bytes -- bytes above
     265     * this are set to zero in order to avoid memory check warnings when
     266     * longest match routines access bytes past the input.  This is then
     267     * updated to the new high water mark.
     268     */
     269
    263270} FAR deflate_state;
    264271
     
    278285 * distances are limited to MAX_DIST instead of WSIZE.
    279286 */
     287
     288#define WIN_INIT MAX_MATCH
     289/* Number of bytes after end of data in window to initialize in order to avoid
     290   memory checker errors from longest match routines */
    280291
    281292        /* in trees.c */
     
    283294int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
    284295void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
    285                           int eof));
     296                          int last));
    286297void _tr_align        OF((deflate_state *s));
    287298void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
    288                           int eof));
     299                          int last));
    289300
    290301#define d_code(dist) \
  • cpukit/zlib/trees.c

    rf7d9aea2 r5a7aa10  
    11/* trees.c -- output deflated data using Huffman coding
    2  * Copyright (C) 1995-2005 Jean-loup Gailly
     2 * Copyright (C) 1995-2009 Jean-loup Gailly
     3 * detect_data_type() function provided freely by Cosmin Truta, 2006
    34 * For conditions of distribution and use, see copyright notice in zlib.h
    45 */
     
    153154local void compress_block OF((deflate_state *s, ct_data *ltree,
    154155                              ct_data *dtree));
    155 local void set_data_type OF((deflate_state *s));
     156local int  detect_data_type OF((deflate_state *s));
    156157local unsigned bi_reverse OF((unsigned value, int length));
    157158local void bi_windup      OF((deflate_state *s));
     
    204205     */
    205206    if (s->bi_valid > (int)Buf_size - length) {
    206         s->bi_buf |= (value << s->bi_valid);
     207        s->bi_buf |= (ush)value << s->bi_valid;
    207208        put_short(s, s->bi_buf);
    208209        s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
    209210        s->bi_valid += length - Buf_size;
    210211    } else {
    211         s->bi_buf |= value << s->bi_valid;
     212        s->bi_buf |= (ush)value << s->bi_valid;
    212213        s->bi_valid += length;
    213214    }
     
    219220  if (s->bi_valid > (int)Buf_size - len) {\
    220221    int val = value;\
    221     s->bi_buf |= (val << s->bi_valid);\
     222    s->bi_buf |= (ush)val << s->bi_valid;\
    222223    put_short(s, s->bi_buf);\
    223224    s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
    224225    s->bi_valid += len - Buf_size;\
    225226  } else {\
    226     s->bi_buf |= (value) << s->bi_valid;\
     227    s->bi_buf |= (ush)(value) << s->bi_valid;\
    227228    s->bi_valid += len;\
    228229  }\
     
    251252
    252253    /* For some embedded targets, global variables are not initialized: */
     254#ifdef NO_INIT_GLOBAL_POINTERS
    253255    static_l_desc.static_tree = static_ltree;
    254256    static_l_desc.extra_bits = extra_lbits;
     
    256258    static_d_desc.extra_bits = extra_dbits;
    257259    static_bl_desc.extra_bits = extra_blbits;
     260#endif
    258261
    259262    /* Initialize the mapping length (0..255) -> length code (0..28) */
     
    865868 * Send a stored block
    866869 */
    867 void _tr_stored_block(s, buf, stored_len, eof)
     870void _tr_stored_block(s, buf, stored_len, last)
    868871    deflate_state *s;
    869872    charf *buf;       /* input block */
    870873    ulg stored_len;   /* length of input block */
    871     int eof;          /* true if this is the last block for a file */
    872 {
    873     send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
     874    int last;         /* one if this is the last block for a file */
     875{
     876    send_bits(s, (STORED_BLOCK<<1)+last, 3);    /* send block type */
    874877#ifdef DEBUG
    875878    s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
     
    919922 * trees or store, and output the encoded block to the zip file.
    920923 */
    921 void _tr_flush_block(s, buf, stored_len, eof)
     924void _tr_flush_block(s, buf, stored_len, last)
    922925    deflate_state *s;
    923926    charf *buf;       /* input block, or NULL if too old */
    924927    ulg stored_len;   /* length of input block */
    925     int eof;          /* true if this is the last block for a file */
     928    int last;         /* one if this is the last block for a file */
    926929{
    927930    ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
     
    932935
    933936        /* Check if the file is binary or text */
    934         if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
    935             set_data_type(s);
     937        if (s->strm->data_type == Z_UNKNOWN)
     938            s->strm->data_type = detect_data_type(s);
    936939
    937940        /* Construct the literal and distance trees */
     
    979982         * transform a block into a stored block.
    980983         */
    981         _tr_stored_block(s, buf, stored_len, eof);
     984        _tr_stored_block(s, buf, stored_len, last);
    982985
    983986#ifdef FORCE_STATIC
     
    986989    } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
    987990#endif
    988         send_bits(s, (STATIC_TREES<<1)+eof, 3);
     991        send_bits(s, (STATIC_TREES<<1)+last, 3);
    989992        compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
    990993#ifdef DEBUG
     
    992995#endif
    993996    } else {
    994         send_bits(s, (DYN_TREES<<1)+eof, 3);
     997        send_bits(s, (DYN_TREES<<1)+last, 3);
    995998        send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
    996999                       max_blindex+1);
     
    10061009    init_block(s);
    10071010
    1008     if (eof) {
     1011    if (last) {
    10091012        bi_windup(s);
    10101013#ifdef DEBUG
     
    10131016    }
    10141017    Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
    1015            s->compressed_len-7*eof));
     1018           s->compressed_len-7*last));
    10161019}
    10171020
     
    11191122
    11201123/* ===========================================================================
    1121  * Set the data type to BINARY or TEXT, using a crude approximation:
    1122  * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
    1123  * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
     1124 * Check if the data type is TEXT or BINARY, using the following algorithm:
     1125 * - TEXT if the two conditions below are satisfied:
     1126 *    a) There are no non-portable control characters belonging to the
     1127 *       "black list" (0..6, 14..25, 28..31).
     1128 *    b) There is at least one printable character belonging to the
     1129 *       "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
     1130 * - BINARY otherwise.
     1131 * - The following partially-portable control characters form a
     1132 *   "gray list" that is ignored in this detection algorithm:
     1133 *   (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
    11241134 * IN assertion: the fields Freq of dyn_ltree are set.
    11251135 */
    1126 local void set_data_type(s)
    1127     deflate_state *s;
    1128 {
     1136local int detect_data_type(s)
     1137    deflate_state *s;
     1138{
     1139    /* black_mask is the bit mask of black-listed bytes
     1140     * set bits 0..6, 14..25, and 28..31
     1141     * 0xf3ffc07f = binary 11110011111111111100000001111111
     1142     */
     1143    unsigned long black_mask = 0xf3ffc07fUL;
    11291144    int n;
    11301145
    1131     for (n = 0; n < 9; n++)
     1146    /* Check for non-textual ("black-listed") bytes. */
     1147    for (n = 0; n <= 31; n++, black_mask >>= 1)
     1148        if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
     1149            return Z_BINARY;
     1150
     1151    /* Check for textual ("white-listed") bytes. */
     1152    if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
     1153            || s->dyn_ltree[13].Freq != 0)
     1154        return Z_TEXT;
     1155    for (n = 32; n < LITERALS; n++)
    11321156        if (s->dyn_ltree[n].Freq != 0)
    1133             break;
    1134     if (n == 9)
    1135         for (n = 14; n < 32; n++)
    1136             if (s->dyn_ltree[n].Freq != 0)
    1137                 break;
    1138     s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
     1157            return Z_TEXT;
     1158
     1159    /* There are no "black-listed" or "white-listed" bytes:
     1160     * this stream either is empty or has tolerated ("gray-listed") bytes only.
     1161     */
     1162    return Z_BINARY;
    11391163}
    11401164
Note: See TracChangeset for help on using the changeset viewer.