compressionlibs/ziplib/test/oldezlib/Zlib/infutil.c
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /* inflate_util.c -- data and routines common to blocks and codes
       
     2  * Copyright (C) 1995-1998 Mark Adler
       
     3  * For conditions of distribution and use, see copyright notice in zlib.h 
       
     4  */
       
     5 
       
     6 #include "zutil.h"
       
     7 #include "infblock.h"
       
     8 #include "inftrees.h"
       
     9 #include "infcodes.h"
       
    10 #include "infutil.h"
       
    11 
       
    12 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
       
    13 
       
    14 /* And'ing with mask[n] masks the lower n bits */
       
    15 uInt inflate_mask[17] = {
       
    16     0x0000,
       
    17     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
       
    18     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
       
    19 };
       
    20 
       
    21 
       
    22 /* copy as much as possible from the sliding window to the output area */
       
    23 int inflate_flush(inflate_blocks_statef *s, z_streamp z, int r)
       
    24 {
       
    25   uInt n;
       
    26   Bytef *p;
       
    27   Bytef *q;
       
    28 
       
    29   /* local copies of source and destination pointers */
       
    30   p = z->next_out;
       
    31   q = s->read;
       
    32 
       
    33   /* compute number of bytes to copy as far as end of window */
       
    34   n = (uInt)((q <= s->write ? s->write : s->end) - q);
       
    35   if (n > z->avail_out) n = z->avail_out;
       
    36   if (n && r == Z_BUF_ERROR) r = Z_OK;
       
    37 
       
    38   /* update counters */
       
    39   z->avail_out -= n;
       
    40   z->total_out += n;
       
    41 
       
    42   /* update check information */
       
    43   if (s->checkfn != Z_NULL)
       
    44     z->adler = s->check = (*s->checkfn)(s->check, q, n);
       
    45 
       
    46   /* copy as far as end of window */
       
    47   zmemcpy(p, q, n);
       
    48   p += n;
       
    49   q += n;
       
    50 
       
    51   /* see if more to copy at beginning of window */
       
    52   if (q == s->end)
       
    53   {
       
    54     /* wrap pointers */
       
    55     q = s->window;
       
    56     if (s->write == s->end)
       
    57       s->write = s->window;
       
    58 
       
    59     /* compute bytes to copy */
       
    60     n = (uInt)(s->write - q);
       
    61     if (n > z->avail_out) n = z->avail_out;
       
    62     if (n && r == Z_BUF_ERROR) r = Z_OK;
       
    63 
       
    64     /* update counters */
       
    65     z->avail_out -= n;
       
    66     z->total_out += n;
       
    67 
       
    68     /* update check information */
       
    69     if (s->checkfn != Z_NULL)
       
    70       z->adler = s->check = (*s->checkfn)(s->check, q, n);
       
    71 
       
    72     /* copy */
       
    73     zmemcpy(p, q, n);
       
    74     p += n;
       
    75     q += n;
       
    76   }
       
    77 
       
    78   /* update pointers */
       
    79   z->next_out = p;
       
    80   s->read = q;
       
    81 
       
    82   /* done */
       
    83   return r;
       
    84 }