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