Change the translation of the "Help" menu item to "?", so that the menu can be displa...
[reactos.git] / rosapps / smartpdf / fitz / stream / filt_flate.c
1 #include "fitz-base.h"
2 #include "fitz-stream.h"
3
4 #include <zlib.h>
5
6 typedef struct fz_flate_s fz_flate;
7
8 struct fz_flate_s
9 {
10 fz_filter super;
11 z_stream z;
12 };
13
14 static void *
15 zmalloc(void *opaque, unsigned int items, unsigned int size)
16 {
17 fz_memorycontext *mctx = (fz_memorycontext*)opaque;
18 return mctx->malloc(mctx, items * size);
19 }
20
21 fz_error *
22 fz_newflated(fz_filter **fp, fz_obj *params)
23 {
24 fz_error *eo;
25 fz_obj *obj;
26 int zipfmt;
27 int ei;
28
29 FZ_NEWFILTER(fz_flate, f, flated);
30
31 f->z.zalloc = zmalloc;
32 f->z.zfree = (void(*)(void*,void*))fz_currentmemorycontext()->free;
33 f->z.opaque = fz_currentmemorycontext();
34 f->z.next_in = nil;
35 f->z.avail_in = 0;
36
37 zipfmt = 0;
38
39 if (params)
40 {
41 obj = fz_dictgets(params, "ZIP");
42 if (obj) zipfmt = fz_tobool(obj);
43 }
44
45 if (zipfmt)
46 {
47 /* if windowbits is negative the zlib header is skipped */
48 ei = inflateInit2(&f->z, -15);
49 }
50 else
51 ei = inflateInit(&f->z);
52
53 if (ei != Z_OK)
54 {
55 eo = fz_throw("ioerror: inflateInit: %s", f->z.msg);
56 fz_free(f);
57 return eo;
58 }
59
60 return nil;
61 }
62
63 void
64 fz_dropflated(fz_filter *f)
65 {
66 z_streamp zp = &((fz_flate*)f)->z;
67 int err;
68
69 err = inflateEnd(zp);
70 if (err != Z_OK)
71 fprintf(stderr, "inflateEnd: %s", zp->msg);
72 }
73
74 fz_error *
75 fz_processflated(fz_filter *f, fz_buffer *in, fz_buffer *out)
76 {
77 z_streamp zp = &((fz_flate*)f)->z;
78 int err;
79
80 if (in->rp == in->wp && !in->eof)
81 return fz_ioneedin;
82 if (out->wp == out->ep)
83 return fz_ioneedout;
84
85 zp->next_in = in->rp;
86 zp->avail_in = in->wp - in->rp;
87
88 zp->next_out = out->wp;
89 zp->avail_out = out->ep - out->wp;
90
91 err = inflate(zp, Z_NO_FLUSH);
92
93 in->rp = in->wp - zp->avail_in;
94 out->wp = out->ep - zp->avail_out;
95
96 if (err == Z_STREAM_END)
97 {
98 out->eof = 1;
99 return fz_iodone;
100 }
101 else if (err == Z_OK)
102 {
103 if (in->rp == in->wp && !in->eof)
104 return fz_ioneedin;
105 if (out->wp == out->ep)
106 return fz_ioneedout;
107 return fz_ioneedin; /* hmm, what's going on here? */
108 }
109 else
110 {
111 return fz_throw("ioerror: inflate: %s", zp->msg);
112 }
113 }
114
115 fz_error *
116 fz_newflatee(fz_filter **fp, fz_obj *params)
117 {
118 fz_obj *obj;
119 fz_error *eo;
120 int effort;
121 int zipfmt;
122 int ei;
123
124 FZ_NEWFILTER(fz_flate, f, flatee);
125
126 effort = -1;
127 zipfmt = 0;
128
129 if (params)
130 {
131 obj = fz_dictgets(params, "Effort");
132 if (obj) effort = fz_toint(obj);
133 obj = fz_dictgets(params, "ZIP");
134 if (obj) effort = fz_tobool(obj);
135 }
136
137 f->z.zalloc = zmalloc;
138 f->z.zfree = (void(*)(void*,void*))fz_currentmemorycontext()->free;
139 f->z.opaque = fz_currentmemorycontext();
140 f->z.next_in = nil;
141 f->z.avail_in = 0;
142
143 if (zipfmt)
144 ei = deflateInit2(&f->z, effort, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
145 else
146 ei = deflateInit(&f->z, effort);
147
148 if (ei != Z_OK)
149 {
150 eo = fz_throw("ioerror: deflateInit: %s", f->z.msg);
151 fz_free(f);
152 return eo;
153 }
154
155 return nil;
156 }
157
158 void
159 fz_dropflatee(fz_filter *f)
160 {
161 z_streamp zp = &((fz_flate*)f)->z;
162 int err;
163
164 err = deflateEnd(zp);
165 if (err != Z_OK)
166 fprintf(stderr, "deflateEnd: %s", zp->msg);
167
168 fz_free(f);
169 }
170
171 fz_error *
172 fz_processflatee(fz_filter *f, fz_buffer *in, fz_buffer *out)
173 {
174 z_streamp zp = &((fz_flate*)f)->z;
175 int err;
176
177 if (in->rp == in->wp && !in->eof)
178 return fz_ioneedin;
179 if (out->wp == out->ep)
180 return fz_ioneedout;
181
182 zp->next_in = in->rp;
183 zp->avail_in = in->wp - in->rp;
184
185 zp->next_out = out->wp;
186 zp->avail_out = out->ep - out->wp;
187
188 err = deflate(zp, in->eof ? Z_FINISH : Z_NO_FLUSH);
189
190 in->rp = in->wp - zp->avail_in;
191 out->wp = out->ep - zp->avail_out;
192
193 if (err == Z_STREAM_END)
194 {
195 out->eof = 1;
196 return fz_iodone;
197 }
198 else if (err == Z_OK)
199 {
200 if (in->rp == in->wp && !in->eof)
201 return fz_ioneedin;
202 if (out->wp == out->ep)
203 return fz_ioneedout;
204 return fz_ioneedin; /* hmm? */
205 }
206 else
207 {
208 return fz_throw("ioerror: deflate: %s", zp->msg);
209 }
210 }
211