1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <apr_strings.h>
20 /* This conditional isn't defined anywhere yet. */
26 #include "serf_bucket_util.h"
29 static char deflate_magic[2] = { '\037', '\213' };
30 #define DEFLATE_MAGIC_SIZE 10
31 #define DEFLATE_VERIFY_SIZE 8
32 #define DEFLATE_BUFFER_SIZE 8096
34 static const int DEFLATE_WINDOW_SIZE = -15;
35 static const int DEFLATE_MEMLEVEL = 9;
38 serf_bucket_t *stream;
39 serf_bucket_t *inflate_stream;
41 int format; /* Are we 'deflate' or 'gzip'? */
44 STATE_READING_HEADER, /* reading the gzip header */
45 STATE_HEADER, /* read the gzip header */
46 STATE_INIT, /* init'ing zlib functions */
47 STATE_INFLATE, /* inflating the content now */
48 STATE_READING_VERIFY, /* reading the final gzip CRC */
49 STATE_VERIFY, /* verifying the final gzip CRC */
50 STATE_FINISH, /* clean up after reading body */
51 STATE_DONE, /* body is done; we'll return EOF here */
55 char hdr_buffer[DEFLATE_MAGIC_SIZE];
56 unsigned char buffer[DEFLATE_BUFFER_SIZE];
62 /* How much of the chunk, or the terminator, do we have left to read? */
63 apr_size_t stream_left;
65 /* How much are we supposed to read? */
66 apr_size_t stream_size;
68 int stream_status; /* What was the last status we read? */
72 /* Inputs a string and returns a long. */
73 static unsigned long getLong(unsigned char *string)
75 return ((unsigned long)string[0])
76 | (((unsigned long)string[1]) << 8)
77 | (((unsigned long)string[2]) << 16)
78 | (((unsigned long)string[3]) << 24);
81 serf_bucket_t *serf_bucket_deflate_create(
82 serf_bucket_t *stream,
83 serf_bucket_alloc_t *allocator,
86 deflate_context_t *ctx;
88 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
90 ctx->stream_status = APR_SUCCESS;
91 ctx->inflate_stream = serf_bucket_aggregate_create(allocator);
94 /* zstream must be NULL'd out. */
95 memset(&ctx->zstream, 0, sizeof(ctx->zstream));
97 switch (ctx->format) {
98 case SERF_DEFLATE_GZIP:
99 ctx->state = STATE_READING_HEADER;
101 case SERF_DEFLATE_DEFLATE:
102 /* deflate doesn't have a header. */
103 ctx->state = STATE_INIT;
110 /* Initial size of gzip header. */
111 ctx->stream_left = ctx->stream_size = DEFLATE_MAGIC_SIZE;
113 ctx->windowSize = DEFLATE_WINDOW_SIZE;
114 ctx->memLevel = DEFLATE_MEMLEVEL;
115 ctx->bufferSize = DEFLATE_BUFFER_SIZE;
117 return serf_bucket_create(&serf_bucket_type_deflate, allocator, ctx);
120 static void serf_deflate_destroy_and_data(serf_bucket_t *bucket)
122 deflate_context_t *ctx = bucket->data;
124 if (ctx->state > STATE_INIT &&
125 ctx->state <= STATE_FINISH)
126 inflateEnd(&ctx->zstream);
128 /* We may have appended inflate_stream into the stream bucket.
129 * If so, avoid free'ing it twice.
131 if (ctx->inflate_stream) {
132 serf_bucket_destroy(ctx->inflate_stream);
134 serf_bucket_destroy(ctx->stream);
136 serf_default_destroy_and_data(bucket);
139 static apr_status_t serf_deflate_read(serf_bucket_t *bucket,
140 apr_size_t requested,
141 const char **data, apr_size_t *len)
143 deflate_context_t *ctx = bucket->data;
144 unsigned long compCRC, compLen;
146 const char *private_data;
147 apr_size_t private_len;
151 switch (ctx->state) {
152 case STATE_READING_HEADER:
153 case STATE_READING_VERIFY:
154 status = serf_bucket_read(ctx->stream, ctx->stream_left,
155 &private_data, &private_len);
157 if (SERF_BUCKET_READ_ERROR(status)) {
161 memcpy(ctx->hdr_buffer + (ctx->stream_size - ctx->stream_left),
162 private_data, private_len);
164 ctx->stream_left -= private_len;
166 if (ctx->stream_left == 0) {
168 if (APR_STATUS_IS_EAGAIN(status)) {
179 if (ctx->hdr_buffer[0] != deflate_magic[0] ||
180 ctx->hdr_buffer[1] != deflate_magic[1]) {
181 return SERF_ERROR_DECOMPRESSION_FAILED;
183 if (ctx->hdr_buffer[3] != 0) {
184 return SERF_ERROR_DECOMPRESSION_FAILED;
189 /* Do the checksum computation. */
190 compCRC = getLong((unsigned char*)ctx->hdr_buffer);
191 if (ctx->crc != compCRC) {
192 return SERF_ERROR_DECOMPRESSION_FAILED;
194 compLen = getLong((unsigned char*)ctx->hdr_buffer + 4);
195 if (ctx->zstream.total_out != compLen) {
196 return SERF_ERROR_DECOMPRESSION_FAILED;
201 zRC = inflateInit2(&ctx->zstream, ctx->windowSize);
203 return SERF_ERROR_DECOMPRESSION_FAILED;
205 ctx->zstream.next_out = ctx->buffer;
206 ctx->zstream.avail_out = ctx->bufferSize;
210 inflateEnd(&ctx->zstream);
211 serf_bucket_aggregate_prepend(ctx->stream, ctx->inflate_stream);
212 ctx->inflate_stream = 0;
216 /* Do we have anything already uncompressed to read? */
217 status = serf_bucket_read(ctx->inflate_stream, requested, data,
219 if (SERF_BUCKET_READ_ERROR(status)) {
223 if (APR_STATUS_IS_EOF(status)) {
224 status = ctx->stream_status;
225 if (APR_STATUS_IS_EOF(status)) {
226 /* We've read all of the data from our stream, but we
227 * need to continue to iterate until we flush
228 * out the zlib buffer.
230 status = APR_SUCCESS;
237 /* We tried; but we have nothing buffered. Fetch more. */
239 /* It is possible that we maxed out avail_out before
240 * exhausting avail_in; therefore, continue using the
241 * previous buffer. Otherwise, fetch more data from
244 if (ctx->zstream.avail_in == 0) {
245 /* When we empty our inflated stream, we'll return this
246 * status - this allow us to eventually pass up EAGAINs.
248 ctx->stream_status = serf_bucket_read(ctx->stream,
253 if (SERF_BUCKET_READ_ERROR(ctx->stream_status)) {
254 return ctx->stream_status;
257 if (!private_len && APR_STATUS_IS_EAGAIN(ctx->stream_status)) {
259 status = ctx->stream_status;
260 ctx->stream_status = APR_SUCCESS;
264 ctx->zstream.next_in = (unsigned char*)private_data;
265 ctx->zstream.avail_in = private_len;
268 while (ctx->zstream.avail_in != 0) {
269 /* We're full, clear out our buffer, reset, and return. */
270 if (ctx->zstream.avail_out == 0) {
272 ctx->zstream.next_out = ctx->buffer;
273 private_len = ctx->bufferSize - ctx->zstream.avail_out;
275 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer,
278 /* FIXME: There probably needs to be a free func. */
279 tmp = SERF_BUCKET_SIMPLE_STRING_LEN((char *)ctx->buffer,
282 serf_bucket_aggregate_append(ctx->inflate_stream, tmp);
283 ctx->zstream.avail_out = ctx->bufferSize;
286 zRC = inflate(&ctx->zstream, Z_NO_FLUSH);
288 if (zRC == Z_STREAM_END) {
291 private_len = ctx->bufferSize - ctx->zstream.avail_out;
292 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer,
294 /* FIXME: There probably needs to be a free func. */
295 tmp = SERF_BUCKET_SIMPLE_STRING_LEN((char *)ctx->buffer,
298 serf_bucket_aggregate_append(ctx->inflate_stream, tmp);
300 ctx->zstream.avail_out = ctx->bufferSize;
302 /* Push back the remaining data to be read. */
303 tmp = serf_bucket_aggregate_create(bucket->allocator);
304 serf_bucket_aggregate_prepend(tmp, ctx->stream);
307 /* We now need to take the remaining avail_in and
308 * throw it in ctx->stream so our next read picks it up.
310 tmp = SERF_BUCKET_SIMPLE_STRING_LEN(
311 (const char*)ctx->zstream.next_in,
312 ctx->zstream.avail_in,
314 serf_bucket_aggregate_prepend(ctx->stream, tmp);
316 switch (ctx->format) {
317 case SERF_DEFLATE_GZIP:
318 ctx->stream_left = ctx->stream_size =
322 case SERF_DEFLATE_DEFLATE:
323 /* Deflate does not have a verify footer. */
324 ctx->state = STATE_FINISH;
334 return SERF_ERROR_DECOMPRESSION_FAILED;
337 /* Okay, we've inflated. Try to read. */
338 status = serf_bucket_read(ctx->inflate_stream, requested, data,
341 if (APR_STATUS_IS_EOF(status)) {
342 status = ctx->stream_status;
343 /* If our stream is finished too, return SUCCESS so
344 * we'll iterate one more time.
346 if (APR_STATUS_IS_EOF(status)) {
347 /* No more data to read from the stream, and everything
348 inflated. If all data was received correctly, state
349 should have been advanced to STATE_READING_VERIFY or
350 STATE_FINISH. If not, then the data was incomplete
351 and we have an error. */
352 if (ctx->state != STATE_INFLATE)
355 return SERF_ERROR_DECOMPRESSION_FAILED;
360 /* We're done inflating. Use our finished buffer. */
361 return serf_bucket_read(ctx->stream, requested, data, len);
371 /* ### need to implement */
372 #define serf_deflate_readline NULL
373 #define serf_deflate_peek NULL
375 const serf_bucket_type_t serf_bucket_type_deflate = {
378 serf_deflate_readline,
379 serf_default_read_iovec,
380 serf_default_read_for_sendfile,
381 serf_default_read_bucket,
383 serf_deflate_destroy_and_data,