sl@0: /* inflate_util.c -- data and routines common to blocks and codes
sl@0:  * Copyright (C) 1995-1998 Mark Adler
sl@0:  * For conditions of distribution and use, see copyright notice in zlib.h 
sl@0:  */
sl@0: 
sl@0: #include "zutil.h"
sl@0: #include "infblock.h"
sl@0: #include "inftrees.h"
sl@0: #include "infcodes.h"
sl@0: #include "infutil.h"
sl@0: 
sl@0: struct inflate_codes_state {int dummy;}; /* for buggy compilers */
sl@0: 
sl@0: /* And'ing with mask[n] masks the lower n bits */
sl@0: const uInt inflate_mask[17] = {
sl@0:     0x0000,
sl@0:     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
sl@0:     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
sl@0: };
sl@0: 
sl@0: 
sl@0: /* copy as much as possible from the sliding window to the output area */
sl@0: int inflate_flush(
sl@0: inflate_blocks_statef *s,
sl@0: z_streamp z,
sl@0: int r)
sl@0: {
sl@0:   uInt n;
sl@0:   Bytef *p;
sl@0:   Bytef *q;
sl@0: 
sl@0:   /* local copies of source and destination pointers */
sl@0:   p = z->next_out;
sl@0:   q = s->read;
sl@0: 
sl@0:   /* compute number of bytes to copy as far as end of window */
sl@0:   n = (uInt)((q <= s->write ? s->write : s->end) - q);
sl@0:   if (n > z->avail_out) n = z->avail_out;
sl@0:   if (n && r == Z_BUF_ERROR) r = Z_OK;
sl@0: 
sl@0:   /* update counters */
sl@0:   z->avail_out -= n;
sl@0:   z->total_out += n;
sl@0: 
sl@0:   /* update check information */
sl@0:   if (s->checkfn != Z_NULL)
sl@0:     z->adler = s->check = (*s->checkfn)(s->check, q, n);
sl@0: 
sl@0:   /* copy as far as end of window */
sl@0:   zmemcpy(p, q, n);
sl@0:   p += n;
sl@0:   q += n;
sl@0: 
sl@0:   /* see if more to copy at beginning of window */
sl@0:   if (q == s->end)
sl@0:   {
sl@0:     /* wrap pointers */
sl@0:     q = s->window;
sl@0:     if (s->write == s->end)
sl@0:       s->write = s->window;
sl@0: 
sl@0:     /* compute bytes to copy */
sl@0:     n = (uInt)(s->write - q);
sl@0:     if (n > z->avail_out) n = z->avail_out;
sl@0:     if (n && r == Z_BUF_ERROR) r = Z_OK;
sl@0: 
sl@0:     /* update counters */
sl@0:     z->avail_out -= n;
sl@0:     z->total_out += n;
sl@0: 
sl@0:     /* update check information */
sl@0:     if (s->checkfn != Z_NULL)
sl@0:       z->adler = s->check = (*s->checkfn)(s->check, q, n);
sl@0: 
sl@0:     /* copy */
sl@0:     zmemcpy(p, q, n);
sl@0:     p += n;
sl@0:     q += n;
sl@0:   }
sl@0: 
sl@0:   /* update pointers */
sl@0:   z->next_out = p;
sl@0:   s->read = q;
sl@0: 
sl@0:   /* done */
sl@0:   return r;
sl@0: }