annotate zlib/infutil.c @ 3:5a977ccbc7a9 default tip

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