From 602971408ba8c2c1490bd87d2987ab65900a5297 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 30 May 2010 03:02:40 +0900 Subject: cpp: move source files into src/ directory --- cpp/src/msgpack/zbuffer.h | 180 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 cpp/src/msgpack/zbuffer.h (limited to 'cpp/src/msgpack/zbuffer.h') diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h new file mode 100644 index 0000000..2a32206 --- /dev/null +++ b/cpp/src/msgpack/zbuffer.h @@ -0,0 +1,180 @@ +/* + * MessagePack for C deflate buffer implementation + * + * Copyright (C) 2010 FURUHASHI Sadayuki + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MSGPACK_ZBUFFER_H__ +#define MSGPACK_ZBUFFER_H__ + +#include "msgpack/sysdep.h" +#include +#include +#include + +#ifndef MSGPACK_ZBUFFER_INIT_SIZE +#define MSGPACK_ZBUFFER_INIT_SIZE 8192 +#endif + +#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE +#define MSGPACK_ZBUFFER_RESERVE_SIZE 512 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + +typedef struct msgpack_zbuffer { + z_stream stream; + char* data; + size_t init_size; +} msgpack_zbuffer; + + +static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf, + int level, size_t init_size); +static inline void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf); + +static inline char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf); + +static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf); +static inline size_t msgpack_zbuffer_size(const msgpack_zbuffer* zbuf); + +static inline bool msgpack_zbuffer_reset(msgpack_zbuffer* zbuf); +static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf); +static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf); + + +static inline int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len); + +static inline bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf); + + +bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf, + int level, size_t init_size) +{ + memset(zbuf, 0, sizeof(msgpack_zbuffer)); + zbuf->init_size = init_size; + if(deflateInit(&zbuf->stream, level) != Z_OK) { + free(zbuf->data); + return false; + } + return true; +} + +void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf) +{ + deflateEnd(&zbuf->stream); + free(zbuf->data); +} + +bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf) +{ + size_t used = (char*)zbuf->stream.next_out - zbuf->data; + size_t csize = used + zbuf->stream.avail_out; + size_t nsize = (csize == 0) ? zbuf->init_size : csize * 2; + + char* tmp = (char*)realloc(zbuf->data, nsize); + if(tmp == NULL) { + return false; + } + + zbuf->data = tmp; + zbuf->stream.next_out = (Bytef*)(tmp + used); + zbuf->stream.avail_out = nsize - used; + + return true; +} + +int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len) +{ + msgpack_zbuffer* zbuf = (msgpack_zbuffer*)data; + + zbuf->stream.next_in = (Bytef*)buf; + zbuf->stream.avail_in = len; + + do { + if(zbuf->stream.avail_out < MSGPACK_ZBUFFER_RESERVE_SIZE) { + if(!msgpack_zbuffer_expand(zbuf)) { + return -1; + } + } + + if(deflate(&zbuf->stream, Z_NO_FLUSH) != Z_OK) { + return -1; + } + } while(zbuf->stream.avail_in > 0); + + return 0; +} + +char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf) +{ + while(true) { + switch(deflate(&zbuf->stream, Z_FINISH)) { + case Z_STREAM_END: + return zbuf->data; + case Z_OK: + if(!msgpack_zbuffer_expand(zbuf)) { + return NULL; + } + break; + default: + return NULL; + } + } +} + +const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf) +{ + return zbuf->data; +} + +size_t msgpack_zbuffer_size(const msgpack_zbuffer* zbuf) +{ + return (char*)zbuf->stream.next_out - zbuf->data; +} + +void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf) +{ + zbuf->stream.avail_out += (char*)zbuf->stream.next_out - zbuf->data; + zbuf->stream.next_out = (Bytef*)zbuf->data; +} + +bool msgpack_zbuffer_reset(msgpack_zbuffer* zbuf) +{ + if(deflateReset(&zbuf->stream) != Z_OK) { + return false; + } + msgpack_zbuffer_reset_buffer(zbuf); + return true; +} + +char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf) +{ + char* tmp = zbuf->data; + zbuf->data = NULL; + zbuf->stream.next_out = NULL; + zbuf->stream.avail_out = 0; + return tmp; +} + + +#ifdef __cplusplus +} +#endif + +#endif /* msgpack/zbuffer.h */ + -- cgit v1.2.1 From 103b14ea3c8a3f72213295d975328dc2399725eb Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 1 Jun 2010 07:10:39 +0900 Subject: cpp: adds msgpack_zbuffer_new and msgpack_zbuffer_free --- cpp/src/msgpack/zbuffer.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'cpp/src/msgpack/zbuffer.h') diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h index 2a32206..0ff9675 100644 --- a/cpp/src/msgpack/zbuffer.h +++ b/cpp/src/msgpack/zbuffer.h @@ -47,6 +47,9 @@ static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf, int level, size_t init_size); static inline void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf); +static inline msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size); +static inline void msgpack_zbuffer_free(msgpack_zbuffer* zbuf); + static inline char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf); static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf); @@ -80,6 +83,23 @@ void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf) free(zbuf->data); } +msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size) +{ + msgpack_zbuffer* zbuf = (msgpack_zbuffer*)malloc(sizeof(msgpack_zbuffer)); + if(!msgpack_zbuffer_init(zbuf, level, init_size)) { + free(zbuf); + return NULL; + } + return zbuf; +} + +void msgpack_zbuffer_free(msgpack_zbuffer* zbuf) +{ + if(zbuf == NULL) { return; } + msgpack_zbuffer_destroy(zbuf); + free(zbuf); +} + bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf) { size_t used = (char*)zbuf->stream.next_out - zbuf->data; -- cgit v1.2.1 From 3d3af3284e3a5fd03395b6694fd745491509aa64 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 1 Jun 2010 08:43:30 +0900 Subject: cpp: adds Doxyfile --- cpp/src/msgpack/zbuffer.h | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'cpp/src/msgpack/zbuffer.h') diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h index 0ff9675..abb9c50 100644 --- a/cpp/src/msgpack/zbuffer.h +++ b/cpp/src/msgpack/zbuffer.h @@ -23,25 +23,26 @@ #include #include -#ifndef MSGPACK_ZBUFFER_INIT_SIZE -#define MSGPACK_ZBUFFER_INIT_SIZE 8192 -#endif - -#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE -#define MSGPACK_ZBUFFER_RESERVE_SIZE 512 -#endif - #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_zbuffer Compressed buffer + * @ingroup msgpack_buffer + * @{ + */ + typedef struct msgpack_zbuffer { z_stream stream; char* data; size_t init_size; } msgpack_zbuffer; +#ifndef MSGPACK_ZBUFFER_INIT_SIZE +#define MSGPACK_ZBUFFER_INIT_SIZE 8192 +#endif static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf, int level, size_t init_size); @@ -60,6 +61,10 @@ static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf); static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf); +#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE +#define MSGPACK_ZBUFFER_RESERVE_SIZE 512 +#endif + static inline int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len); static inline bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf); @@ -191,6 +196,8 @@ char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf) return tmp; } +/** @} */ + #ifdef __cplusplus } -- cgit v1.2.1 From 59ba8dec4ee082e8777047e6ae72e8b6998cdc79 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Fri, 27 Aug 2010 16:45:48 +0900 Subject: cpp: fixes include paths --- cpp/src/msgpack/zbuffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cpp/src/msgpack/zbuffer.h') diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h index abb9c50..efdd304 100644 --- a/cpp/src/msgpack/zbuffer.h +++ b/cpp/src/msgpack/zbuffer.h @@ -18,7 +18,7 @@ #ifndef MSGPACK_ZBUFFER_H__ #define MSGPACK_ZBUFFER_H__ -#include "msgpack/sysdep.h" +#include "sysdep.h" #include #include #include -- cgit v1.2.1