Move inflate functions to libspdylay from examples.

This commit is contained in:
Jim Morrison
2012-04-30 12:36:37 -07:00
parent bbac4fea8d
commit 1a384a6000
7 changed files with 180 additions and 99 deletions

View File

@@ -35,12 +35,12 @@ OBJECTS = spdylay_pq.c spdylay_map.c spdylay_queue.c \
spdylay_buffer.c spdylay_frame.c spdylay_zlib.c \
spdylay_session.c spdylay_helper.c spdylay_stream.c spdylay_npn.c \
spdylay_submit.c spdylay_outbound_item.c \
spdylay_client_cert_vector.c
spdylay_client_cert_vector.c spdylay_gzip.c
HFILES = spdylay_pq.h spdylay_int.h spdylay_map.h spdylay_queue.h \
spdylay_buffer.h spdylay_frame.h spdylay_zlib.h \
spdylay_session.h spdylay_helper.h spdylay_stream.h spdylay_int.h \
spdylay_npn.h \
spdylay_npn.h spdylay_gzip.h \
spdylay_submit.h spdylay_outbound_item.h \
spdylay_client_cert_vector.h \
spdylay_net.h

View File

@@ -32,6 +32,7 @@ extern "C" {
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <zlib.h>
#include <spdylay/spdylayver.h>
@@ -1827,6 +1828,51 @@ int spdylay_select_next_protocol(unsigned char **out, unsigned char *outlen,
*/
uint16_t spdylay_npn_get_version(const unsigned char *proto, size_t protolen);
/**
* @function
*
* A helper function to set up a per request zlib stream to inflate data.
*/
z_stream *spdylay_new_inflate_stream();
/**
* @function
* Inflates data from in to out. Returns 0 on success and -1 on error.
* E.g
* void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags,
* int32_t stream_id,
* const uint8_t *data, size_t len,
* void *user_data)
* req = spdylay_session_get_stream_user_data(session, stream_id);
* z_stream *inflater = req->inflater;
* while(len > 0) {
* uint8_t out[MAX_OUTLEN];
* size_t outlen = MAX_OUTLEN;
* size_t tlen = len;
* int rv;
* rv = spdylay_inflate_data(inflater, out, &outlen, data, &tlen);
* if(rv == -1) {
* spdylay_submit_rst_stream(session, stream_id, SPDYLAY_INTERNAL_ERROR);
* break;
* }
* ... Do stuff ...
* data += tlen;
* len -= tlen;
* }
* ....
* }
*/
int spdylay_inflate_data
(z_stream *stream, uint8_t *out, size_t *outlen_ptr,
const uint8_t *in, size_t *inlen_ptr);
/**
* @function
* Frees the inflate stream. inflater may be null.
*/
void spdylay_free_inflate_stream(z_stream* inflater);
#ifdef __cplusplus
}
#endif

87
lib/spdylay_gzip.c Normal file
View File

@@ -0,0 +1,87 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdlib.h>
#include <zlib.h>
#include "spdylay_gzip.h"
z_stream *spdylay_new_inflate_stream()
{
int rv;
z_stream *inflater = malloc(sizeof(z_stream));
if (inflater == NULL) {
return NULL;
}
inflater->next_in = Z_NULL;
inflater->zalloc = Z_NULL;
inflater->zfree = Z_NULL;
inflater->opaque = Z_NULL;
rv = inflateInit2(inflater, 47);
if(rv != Z_OK) {
free(inflater);
return NULL;
}
return inflater;
}
int spdylay_inflate_data
(z_stream *inflater, uint8_t *out, size_t *outlen_ptr,
const uint8_t *in, size_t *inlen_ptr) {
int rv;
assert(inflater);
inflater->avail_in = *inlen_ptr;
inflater->next_in = (unsigned char*)in;
inflater->avail_out = *outlen_ptr;
inflater->next_out = out;
rv = inflate(inflater, Z_NO_FLUSH);
*inlen_ptr -= inflater->avail_in;
*outlen_ptr -= inflater->avail_out;
switch(rv) {
case Z_OK:
case Z_STREAM_END:
case Z_BUF_ERROR:
return 0;
case Z_DATA_ERROR:
case Z_STREAM_ERROR:
case Z_NEED_DICT:
case Z_MEM_ERROR:
return -1;
default:
abort();
}
}
void spdylay_free_inflate_stream(z_stream* stream)
{
if (stream != NULL) {
inflateEnd(stream);
free(stream);
}
}

34
lib/spdylay_gzip.h Normal file
View File

@@ -0,0 +1,34 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
#include <zlib.h>
/* See usage comments in spdylay.h */
z_stream *spdylay_new_inflate_stream();
int spdylay_inflate_data
(z_stream *stream, uint8_t *out, size_t *outlen_ptr,
const uint8_t *in, size_t *inlen_ptr);
void spdylay_free_inflate_stream(z_stream* stream);