infutil.c 1.9 KB
Newer Older
M
Mark Adler 已提交
1
/* inflate_util.c -- data and routines common to blocks and codes
M
Mark Adler 已提交
2
 * Copyright (C) 1995-1996 Mark Adler
M
Mark Adler 已提交
3 4 5 6
 * For conditions of distribution and use, see copyright notice in zlib.h 
 */

#include "zutil.h"
M
Mark Adler 已提交
7
#include "infblock.h"
M
Mark Adler 已提交
8
#include "inftrees.h"
M
Mark Adler 已提交
9
#include "infcodes.h"
M
Mark Adler 已提交
10 11 12 13 14
#include "infutil.h"

struct inflate_codes_state {int dummy;}; /* for buggy compilers */

/* And'ing with mask[n] masks the lower n bits */
M
Mark Adler 已提交
15
uInt inflate_mask[17] = {
M
Mark Adler 已提交
16 17 18 19 20 21 22 23
    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)
M
Mark Adler 已提交
24
inflate_blocks_statef *s;
M
Mark Adler 已提交
25 26 27 28
z_stream *z;
int r;
{
  uInt n;
M
Mark Adler 已提交
29
  Bytef *p, *q;
M
Mark Adler 已提交
30 31 32 33 34 35

  /* 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 */
M
Mark Adler 已提交
36
  n = (uInt)((q <= s->write ? s->write : s->end) - q);
M
Mark Adler 已提交
37 38 39 40 41 42 43 44
  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 */
M
Mark Adler 已提交
45
  if (s->checkfn != Z_NULL)
M
Mark Adler 已提交
46
    z->adler = s->check = (*s->checkfn)(s->check, q, n);
M
Mark Adler 已提交
47 48

  /* copy as far as end of window */
M
Mark Adler 已提交
49 50 51
  zmemcpy(p, q, n);
  p += n;
  q += n;
M
Mark Adler 已提交
52 53 54 55

  /* see if more to copy at beginning of window */
  if (q == s->end)
  {
M
Mark Adler 已提交
56
    /* wrap pointers */
M
Mark Adler 已提交
57
    q = s->window;
M
Mark Adler 已提交
58 59
    if (s->write == s->end)
      s->write = s->window;
M
Mark Adler 已提交
60 61

    /* compute bytes to copy */
M
Mark Adler 已提交
62
    n = (uInt)(s->write - q);
M
Mark Adler 已提交
63 64 65 66 67 68 69 70
    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 */
M
Mark Adler 已提交
71
    if (s->checkfn != Z_NULL)
M
Mark Adler 已提交
72
      z->adler = s->check = (*s->checkfn)(s->check, q, n);
M
Mark Adler 已提交
73 74

    /* copy */
M
Mark Adler 已提交
75 76 77
    zmemcpy(p, q, n);
    p += n;
    q += n;
M
Mark Adler 已提交
78 79 80 81 82 83 84 85 86
  }

  /* update pointers */
  z->next_out = p;
  s->read = q;

  /* done */
  return r;
}