Mercurial > repos > blastem
annotate zlib/gzwrite.c @ 2014:9ca255be938d
Reorder system settings so that more common options are near the top and none of the dropdowns get cut off
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 02 Nov 2020 20:03:47 -0800 |
parents | 00d788dac91a |
children |
rev | line source |
---|---|
1530
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 /* gzwrite.c -- zlib functions for writing gzip files |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 * Copyright (C) 2004-2017 Mark Adler |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 * For conditions of distribution and use, see copyright notice in zlib.h |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 #include "gzguts.h" |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 /* Local functions */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 local int gz_init OF((gz_statep)); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 local int gz_comp OF((gz_statep, int)); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 local int gz_zero OF((gz_statep, z_off64_t)); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 local z_size_t gz_write OF((gz_statep, voidpc, z_size_t)); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 /* Initialize state for writing a gzip file. Mark initialization by setting |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 state->size to non-zero. Return -1 on a memory allocation failure, or 0 on |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 success. */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 local int gz_init(state) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 int ret; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 z_streamp strm = &(state->strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 /* allocate input buffer (double size for gzprintf) */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 state->in = (unsigned char *)malloc(state->want << 1); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 if (state->in == NULL) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 gz_error(state, Z_MEM_ERROR, "out of memory"); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 /* only need output buffer and deflate state if compressing */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 if (!state->direct) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 /* allocate output buffer */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 state->out = (unsigned char *)malloc(state->want); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 if (state->out == NULL) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 free(state->in); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 gz_error(state, Z_MEM_ERROR, "out of memory"); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 /* allocate deflate memory, set up for gzip compression */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 strm->zalloc = Z_NULL; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 strm->zfree = Z_NULL; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 strm->opaque = Z_NULL; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 ret = deflateInit2(strm, state->level, Z_DEFLATED, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 if (ret != Z_OK) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 free(state->out); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 free(state->in); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 gz_error(state, Z_MEM_ERROR, "out of memory"); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 strm->next_in = NULL; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 /* mark state as initialized */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 state->size = state->want; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 /* initialize write buffer if compressing */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 if (!state->direct) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 strm->avail_out = state->size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 strm->next_out = state->out; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 state->x.next = strm->next_out; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 /* Compress whatever is at avail_in and next_in and write to the output file. |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 Return -1 if there is an error writing to the output file or if gz_init() |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 fails to allocate memory, otherwise 0. flush is assumed to be a valid |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 deflate() flush value. If flush is Z_FINISH, then the deflate() state is |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 reset to start a new gzip stream. If gz->direct is true, then simply write |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 to the output file without compressing, and ignore flush. */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 local int gz_comp(state, flush) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 int flush; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 int ret, writ; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 unsigned have, put, max = ((unsigned)-1 >> 2) + 1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 z_streamp strm = &(state->strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 /* allocate memory if this is the first time through */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 if (state->size == 0 && gz_init(state) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 /* write directly if requested */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 if (state->direct) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 while (strm->avail_in) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 put = strm->avail_in > max ? max : strm->avail_in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 writ = write(state->fd, strm->next_in, put); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 if (writ < 0) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 gz_error(state, Z_ERRNO, zstrerror()); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 strm->avail_in -= (unsigned)writ; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 strm->next_in += writ; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 /* run deflate() on provided input until it produces no more output */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 ret = Z_OK; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 do { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 /* write out current buffer contents if full, or if flushing, but if |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 doing Z_FINISH then don't write until we get to Z_STREAM_END */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 (flush != Z_FINISH || ret == Z_STREAM_END))) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 while (strm->next_out > state->x.next) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 put = strm->next_out - state->x.next > (int)max ? max : |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 (unsigned)(strm->next_out - state->x.next); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 writ = write(state->fd, state->x.next, put); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 if (writ < 0) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 gz_error(state, Z_ERRNO, zstrerror()); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 state->x.next += writ; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
116 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 if (strm->avail_out == 0) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 strm->avail_out = state->size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 strm->next_out = state->out; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 state->x.next = state->out; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
121 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 /* compress */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
125 have = strm->avail_out; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
126 ret = deflate(strm, flush); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 if (ret == Z_STREAM_ERROR) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 gz_error(state, Z_STREAM_ERROR, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 "internal error: deflate stream corrupt"); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
131 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 have -= strm->avail_out; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 } while (have); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 /* if that completed a deflate stream, allow another to start */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 if (flush == Z_FINISH) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 deflateReset(strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 /* all done, no errors */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 /* Compress len zeros to output. Return -1 on a write error or memory |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 allocation failure by gz_comp(), or 0 on success. */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 local int gz_zero(state, len) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 z_off64_t len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 int first; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
150 unsigned n; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
151 z_streamp strm = &(state->strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 /* consume whatever's left in the input buffer */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 /* compress len zeros (len guaranteed > 0) */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 first = 1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 while (len) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 n = GT_OFF(state->size) || (z_off64_t)state->size > len ? |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 (unsigned)len : state->size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 if (first) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 memset(state->in, 0, n); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 first = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 strm->avail_in = n; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 strm->next_in = state->in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 state->x.pos += n; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 if (gz_comp(state, Z_NO_FLUSH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 len -= n; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
174 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 /* Write len bytes from buf to file. Return the number of bytes written. If |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 the returned value is less than len, then there was an error. */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 local z_size_t gz_write(state, buf, len) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
179 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
180 voidpc buf; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 z_size_t len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 z_size_t put = len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 /* if len is zero, avoid unnecessary operations */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 if (len == 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 /* allocate memory if this is the first time through */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 if (state->size == 0 && gz_init(state) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 /* check for seek request */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 if (state->seek) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 state->seek = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 if (gz_zero(state, state->skip) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 /* for small len, copy to input buffer, otherwise compress directly */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 if (len < state->size) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 /* copy to input buffer, compress when full */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
203 do { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 unsigned have, copy; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
206 if (state->strm.avail_in == 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 state->strm.next_in = state->in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
208 have = (unsigned)((state->strm.next_in + state->strm.avail_in) - |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
209 state->in); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
210 copy = state->size - have; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
211 if (copy > len) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
212 copy = len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
213 memcpy(state->in + have, buf, copy); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
214 state->strm.avail_in += copy; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
215 state->x.pos += copy; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
216 buf = (const char *)buf + copy; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
217 len -= copy; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
218 if (len && gz_comp(state, Z_NO_FLUSH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
219 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
220 } while (len); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
221 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
222 else { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 /* consume whatever's left in the input buffer */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
224 if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
225 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
226 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
227 /* directly compress user buffer to file */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
228 state->strm.next_in = (z_const Bytef *)buf; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
229 do { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
230 unsigned n = (unsigned)-1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
231 if (n > len) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
232 n = len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
233 state->strm.avail_in = n; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
234 state->x.pos += n; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
235 if (gz_comp(state, Z_NO_FLUSH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
236 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
237 len -= n; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
238 } while (len); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
239 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
240 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
241 /* input was all buffered or compressed */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
242 return put; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
243 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
244 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
245 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
246 int ZEXPORT gzwrite(file, buf, len) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
247 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
248 voidpc buf; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
249 unsigned len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
250 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
251 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
252 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
253 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
254 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
255 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
256 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
257 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
258 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
259 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
260 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
261 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
262 /* since an int is returned, make sure len fits in one, otherwise return |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
263 with an error (this avoids a flaw in the interface) */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
264 if ((int)len < 0) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
265 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
266 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
267 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
268 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
269 /* write len bytes from buf (the return value will fit in an int) */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
270 return (int)gz_write(state, buf, len); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
271 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
272 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
273 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
274 z_size_t ZEXPORT gzfwrite(buf, size, nitems, file) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
275 voidpc buf; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
276 z_size_t size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
277 z_size_t nitems; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
278 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
279 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
280 z_size_t len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
281 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
282 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
283 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
284 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
285 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
286 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
287 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
288 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
289 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
290 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
291 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
292 /* compute bytes to read -- error on overflow */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
293 len = nitems * size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
294 if (size && len / size != nitems) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
295 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
296 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
297 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
298 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
299 /* write len bytes to buf, return the number of full items written */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
300 return len ? gz_write(state, buf, len) / size : 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
301 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
302 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
303 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
304 int ZEXPORT gzputc(file, c) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
305 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
306 int c; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
307 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
308 unsigned have; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
309 unsigned char buf[1]; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
310 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
311 z_streamp strm; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
312 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
313 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
314 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
315 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
316 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
317 strm = &(state->strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
318 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
319 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
320 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
321 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
322 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
323 /* check for seek request */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
324 if (state->seek) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
325 state->seek = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
326 if (gz_zero(state, state->skip) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
327 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
328 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
329 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
330 /* try writing to input buffer for speed (state->size == 0 if buffer not |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
331 initialized) */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
332 if (state->size) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
333 if (strm->avail_in == 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
334 strm->next_in = state->in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
335 have = (unsigned)((strm->next_in + strm->avail_in) - state->in); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
336 if (have < state->size) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
337 state->in[have] = (unsigned char)c; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
338 strm->avail_in++; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
339 state->x.pos++; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
340 return c & 0xff; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
341 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
342 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
343 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
344 /* no room in buffer or not initialized, use gz_write() */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
345 buf[0] = (unsigned char)c; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
346 if (gz_write(state, buf, 1) != 1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
347 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
348 return c & 0xff; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
349 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
350 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
351 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
352 int ZEXPORT gzputs(file, str) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
353 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
354 const char *str; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
355 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
356 int ret; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
357 z_size_t len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
358 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
359 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
360 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
361 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
362 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
363 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
364 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
365 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
366 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
367 return -1; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
368 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
369 /* write string */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
370 len = strlen(str); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
371 ret = gz_write(state, str, len); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
372 return ret == 0 && len != 0 ? -1 : ret; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
373 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
374 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
375 #if defined(STDC) || defined(Z_HAVE_STDARG_H) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
376 #include <stdarg.h> |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
377 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
378 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
379 int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
380 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
381 int len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
382 unsigned left; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
383 char *next; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
384 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
385 z_streamp strm; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
386 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
387 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
388 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
389 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
390 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
391 strm = &(state->strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
392 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
393 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
394 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
395 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
396 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
397 /* make sure we have some buffer space */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
398 if (state->size == 0 && gz_init(state) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
399 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
400 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
401 /* check for seek request */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
402 if (state->seek) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
403 state->seek = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
404 if (gz_zero(state, state->skip) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
405 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
406 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
407 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
408 /* do the printf() into the input buffer, put length in len -- the input |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
409 buffer is double-sized just for this function, so there is guaranteed to |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
410 be state->size bytes available after the current contents */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
411 if (strm->avail_in == 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
412 strm->next_in = state->in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
413 next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
414 next[state->size - 1] = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
415 #ifdef NO_vsnprintf |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
416 # ifdef HAS_vsprintf_void |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
417 (void)vsprintf(next, format, va); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
418 for (len = 0; len < state->size; len++) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
419 if (next[len] == 0) break; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
420 # else |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
421 len = vsprintf(next, format, va); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
422 # endif |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
423 #else |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
424 # ifdef HAS_vsnprintf_void |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
425 (void)vsnprintf(next, state->size, format, va); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
426 len = strlen(next); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
427 # else |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
428 len = vsnprintf(next, state->size, format, va); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
429 # endif |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
430 #endif |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
431 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
432 /* check that printf() results fit in buffer */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
433 if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
434 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
435 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
436 /* update buffer and position, compress first half if past that */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
437 strm->avail_in += (unsigned)len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
438 state->x.pos += len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
439 if (strm->avail_in >= state->size) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
440 left = strm->avail_in - state->size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
441 strm->avail_in = state->size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
442 if (gz_comp(state, Z_NO_FLUSH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
443 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
444 memcpy(state->in, state->in + state->size, left); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
445 strm->next_in = state->in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
446 strm->avail_in = left; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
447 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
448 return len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
449 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
450 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
451 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
452 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
453 va_list va; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
454 int ret; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
455 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
456 va_start(va, format); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
457 ret = gzvprintf(file, format, va); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
458 va_end(va); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
459 return ret; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
460 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
461 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
462 #else /* !STDC && !Z_HAVE_STDARG_H */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
463 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
464 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
465 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
466 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
467 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
468 const char *format; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
469 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
470 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
471 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
472 unsigned len, left; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
473 char *next; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
474 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
475 z_streamp strm; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
476 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
477 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
478 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
479 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
480 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
481 strm = &(state->strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
482 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
483 /* check that can really pass pointer in ints */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
484 if (sizeof(int) != sizeof(void *)) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
485 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
486 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
487 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
488 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
489 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
490 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
491 /* make sure we have some buffer space */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
492 if (state->size == 0 && gz_init(state) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
493 return state->error; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
494 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
495 /* check for seek request */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
496 if (state->seek) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
497 state->seek = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
498 if (gz_zero(state, state->skip) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
499 return state->error; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
500 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
501 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
502 /* do the printf() into the input buffer, put length in len -- the input |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
503 buffer is double-sized just for this function, so there is guaranteed to |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
504 be state->size bytes available after the current contents */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
505 if (strm->avail_in == 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
506 strm->next_in = state->in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
507 next = (char *)(strm->next_in + strm->avail_in); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
508 next[state->size - 1] = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
509 #ifdef NO_snprintf |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
510 # ifdef HAS_sprintf_void |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
511 sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
512 a13, a14, a15, a16, a17, a18, a19, a20); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
513 for (len = 0; len < size; len++) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
514 if (next[len] == 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
515 break; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
516 # else |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
517 len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
518 a12, a13, a14, a15, a16, a17, a18, a19, a20); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
519 # endif |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
520 #else |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
521 # ifdef HAS_snprintf_void |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
522 snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
523 a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
524 len = strlen(next); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
525 # else |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
526 len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
527 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
528 # endif |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
529 #endif |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
530 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
531 /* check that printf() results fit in buffer */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
532 if (len == 0 || len >= state->size || next[state->size - 1] != 0) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
533 return 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
534 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
535 /* update buffer and position, compress first half if past that */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
536 strm->avail_in += len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
537 state->x.pos += len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
538 if (strm->avail_in >= state->size) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
539 left = strm->avail_in - state->size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
540 strm->avail_in = state->size; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
541 if (gz_comp(state, Z_NO_FLUSH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
542 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
543 memcpy(state->in, state->in + state->size, left); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
544 strm->next_in = state->in; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
545 strm->avail_in = left; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
546 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
547 return (int)len; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
548 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
549 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
550 #endif |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
551 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
552 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
553 int ZEXPORT gzflush(file, flush) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
554 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
555 int flush; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
556 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
557 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
558 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
559 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
560 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
561 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
562 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
563 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
564 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
565 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
566 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
567 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
568 /* check flush parameter */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
569 if (flush < 0 || flush > Z_FINISH) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
570 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
571 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
572 /* check for seek request */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
573 if (state->seek) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
574 state->seek = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
575 if (gz_zero(state, state->skip) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
576 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
577 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
578 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
579 /* compress remaining data with requested flush */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
580 (void)gz_comp(state, flush); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
581 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
582 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
583 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
584 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
585 int ZEXPORT gzsetparams(file, level, strategy) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
586 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
587 int level; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
588 int strategy; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
589 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
590 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
591 z_streamp strm; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
592 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
593 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
594 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
595 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
596 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
597 strm = &(state->strm); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
598 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
599 /* check that we're writing and that there's no error */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
600 if (state->mode != GZ_WRITE || state->err != Z_OK) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
601 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
602 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
603 /* if no change is requested, then do nothing */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
604 if (level == state->level && strategy == state->strategy) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
605 return Z_OK; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
606 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
607 /* check for seek request */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
608 if (state->seek) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
609 state->seek = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
610 if (gz_zero(state, state->skip) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
611 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
612 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
613 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
614 /* change compression parameters for subsequent input */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
615 if (state->size) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
616 /* flush previous input with previous parameters before changing */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
617 if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
618 return state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
619 deflateParams(strm, level, strategy); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
620 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
621 state->level = level; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
622 state->strategy = strategy; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
623 return Z_OK; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
624 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
625 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
626 /* -- see zlib.h -- */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
627 int ZEXPORT gzclose_w(file) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
628 gzFile file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
629 { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
630 int ret = Z_OK; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
631 gz_statep state; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
632 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
633 /* get internal structure */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
634 if (file == NULL) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
635 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
636 state = (gz_statep)file; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
637 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
638 /* check that we're writing */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
639 if (state->mode != GZ_WRITE) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
640 return Z_STREAM_ERROR; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
641 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
642 /* check for seek request */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
643 if (state->seek) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
644 state->seek = 0; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
645 if (gz_zero(state, state->skip) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
646 ret = state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
647 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
648 |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
649 /* flush, free memory, and close file */ |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
650 if (gz_comp(state, Z_FINISH) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
651 ret = state->err; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
652 if (state->size) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
653 if (!state->direct) { |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
654 (void)deflateEnd(&(state->strm)); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
655 free(state->out); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
656 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
657 free(state->in); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
658 } |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
659 gz_error(state, Z_OK, NULL); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
660 free(state->path); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
661 if (close(state->fd) == -1) |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
662 ret = Z_ERRNO; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
663 free(state); |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
664 return ret; |
00d788dac91a
Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
665 } |