1 | /* inflate_util.c -- data and routines common to blocks and codes |
---|
2 | * Copyright (C) 1995-2002 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(s, z, r) |
---|
24 | inflate_blocks_statef *s; |
---|
25 | z_streamp z; |
---|
26 | int r; |
---|
27 | { |
---|
28 | uInt n; |
---|
29 | Bytef *p; |
---|
30 | Bytef *q; |
---|
31 | |
---|
32 | /* local copies of source and destination pointers */ |
---|
33 | p = z->next_out; |
---|
34 | q = s->read; |
---|
35 | |
---|
36 | /* compute number of bytes to copy as far as end of window */ |
---|
37 | n = (uInt)((q <= s->write ? s->write : s->end) - q); |
---|
38 | if (n > z->avail_out) n = z->avail_out; |
---|
39 | if (n && r == Z_BUF_ERROR) r = Z_OK; |
---|
40 | |
---|
41 | /* update counters */ |
---|
42 | z->avail_out -= n; |
---|
43 | z->total_out += n; |
---|
44 | |
---|
45 | /* update check information */ |
---|
46 | if (s->checkfn != Z_NULL) |
---|
47 | z->adler = s->check = (*s->checkfn)(s->check, q, n); |
---|
48 | |
---|
49 | /* copy as far as end of window */ |
---|
50 | zmemcpy(p, q, n); |
---|
51 | p += n; |
---|
52 | q += n; |
---|
53 | |
---|
54 | /* see if more to copy at beginning of window */ |
---|
55 | if (q == s->end) |
---|
56 | { |
---|
57 | /* wrap pointers */ |
---|
58 | q = s->window; |
---|
59 | if (s->write == s->end) |
---|
60 | s->write = s->window; |
---|
61 | |
---|
62 | /* compute bytes to copy */ |
---|
63 | n = (uInt)(s->write - q); |
---|
64 | if (n > z->avail_out) n = z->avail_out; |
---|
65 | if (n && r == Z_BUF_ERROR) r = Z_OK; |
---|
66 | |
---|
67 | /* update counters */ |
---|
68 | z->avail_out -= n; |
---|
69 | z->total_out += n; |
---|
70 | |
---|
71 | /* update check information */ |
---|
72 | if (s->checkfn != Z_NULL) |
---|
73 | z->adler = s->check = (*s->checkfn)(s->check, q, n); |
---|
74 | |
---|
75 | /* copy */ |
---|
76 | zmemcpy(p, q, n); |
---|
77 | p += n; |
---|
78 | q += n; |
---|
79 | } |
---|
80 | |
---|
81 | /* update pointers */ |
---|
82 | z->next_out = p; |
---|
83 | s->read = q; |
---|
84 | |
---|
85 | /* done */ |
---|
86 | return r; |
---|
87 | } |
---|