2 * MessagePack for C deflate buffer implementation
4 * Copyright (C) 2010 FURUHASHI Sadayuki
6 * Distributed under the Boost Software License, Version 1.0.
7 * (See accompanying file LICENSE_1_0.txt or copy at
8 * http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef MSGPACK_ZBUFFER_H
11 #define MSGPACK_ZBUFFER_H
24 * @defgroup msgpack_zbuffer Compressed buffer
25 * @ingroup msgpack_buffer
29 typedef struct msgpack_zbuffer
{
35 #ifndef MSGPACK_ZBUFFER_INIT_SIZE
36 #define MSGPACK_ZBUFFER_INIT_SIZE 8192
39 static inline bool msgpack_zbuffer_init(
40 msgpack_zbuffer
* zbuf
, int level
, size_t init_size
);
41 static inline void msgpack_zbuffer_destroy(msgpack_zbuffer
* zbuf
);
43 static inline msgpack_zbuffer
* msgpack_zbuffer_new(int level
, size_t init_size
);
44 static inline void msgpack_zbuffer_free(msgpack_zbuffer
* zbuf
);
46 static inline char* msgpack_zbuffer_flush(msgpack_zbuffer
* zbuf
);
48 static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer
* zbuf
);
49 static inline size_t msgpack_zbuffer_size(const msgpack_zbuffer
* zbuf
);
51 static inline bool msgpack_zbuffer_reset(msgpack_zbuffer
* zbuf
);
52 static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer
* zbuf
);
53 static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer
* zbuf
);
56 #ifndef MSGPACK_ZBUFFER_RESERVE_SIZE
57 #define MSGPACK_ZBUFFER_RESERVE_SIZE 512
60 static inline int msgpack_zbuffer_write(void* data
, const char* buf
, size_t len
);
62 static inline bool msgpack_zbuffer_expand(msgpack_zbuffer
* zbuf
);
65 static inline bool msgpack_zbuffer_init(msgpack_zbuffer
* zbuf
,
66 int level
, size_t init_size
)
68 memset(zbuf
, 0, sizeof(msgpack_zbuffer
));
69 zbuf
->init_size
= init_size
;
70 if(deflateInit(&zbuf
->stream
, level
) != Z_OK
) {
77 static inline void msgpack_zbuffer_destroy(msgpack_zbuffer
* zbuf
)
79 deflateEnd(&zbuf
->stream
);
83 static inline msgpack_zbuffer
* msgpack_zbuffer_new(int level
, size_t init_size
)
85 msgpack_zbuffer
* zbuf
= (msgpack_zbuffer
*)malloc(sizeof(msgpack_zbuffer
));
86 if (zbuf
== NULL
) return NULL
;
87 if(!msgpack_zbuffer_init(zbuf
, level
, init_size
)) {
94 static inline void msgpack_zbuffer_free(msgpack_zbuffer
* zbuf
)
96 if(zbuf
== NULL
) { return; }
97 msgpack_zbuffer_destroy(zbuf
);
101 static inline bool msgpack_zbuffer_expand(msgpack_zbuffer
* zbuf
)
103 size_t used
= (size_t)((char *)(zbuf
->stream
.next_out
) - zbuf
->data
);
104 size_t csize
= used
+ zbuf
->stream
.avail_out
;
106 size_t nsize
= (csize
== 0) ? zbuf
->init_size
: csize
* 2;
108 char* tmp
= (char*)realloc(zbuf
->data
, nsize
);
114 zbuf
->stream
.next_out
= (Bytef
*)(tmp
+ used
);
115 zbuf
->stream
.avail_out
= (uInt
)(nsize
- used
);
120 static inline int msgpack_zbuffer_write(void* data
, const char* buf
, size_t len
)
122 msgpack_zbuffer
* zbuf
= (msgpack_zbuffer
*)data
;
124 zbuf
->stream
.next_in
= (Bytef
*)buf
;
125 zbuf
->stream
.avail_in
= (uInt
)len
;
127 while(zbuf
->stream
.avail_in
> 0) {
128 if(zbuf
->stream
.avail_out
< MSGPACK_ZBUFFER_RESERVE_SIZE
) {
129 if(!msgpack_zbuffer_expand(zbuf
)) {
134 if(deflate(&zbuf
->stream
, Z_NO_FLUSH
) != Z_OK
) {
142 static inline char* msgpack_zbuffer_flush(msgpack_zbuffer
* zbuf
)
145 switch(deflate(&zbuf
->stream
, Z_FINISH
)) {
150 if(!msgpack_zbuffer_expand(zbuf
)) {
160 static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer
* zbuf
)
165 static inline size_t msgpack_zbuffer_size(const msgpack_zbuffer
* zbuf
)
167 return (size_t)((char *)(zbuf
->stream
.next_out
) - zbuf
->data
);
170 static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer
* zbuf
)
172 zbuf
->stream
.avail_out
+= (uInt
)((char*)zbuf
->stream
.next_out
- zbuf
->data
);
173 zbuf
->stream
.next_out
= (Bytef
*)zbuf
->data
;
176 static inline bool msgpack_zbuffer_reset(msgpack_zbuffer
* zbuf
)
178 if(deflateReset(&zbuf
->stream
) != Z_OK
) {
181 msgpack_zbuffer_reset_buffer(zbuf
);
185 static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer
* zbuf
)
187 char* tmp
= zbuf
->data
;
189 zbuf
->stream
.next_out
= NULL
;
190 zbuf
->stream
.avail_out
= 0;
201 #endif /* msgpack/zbuffer.h */