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
25 * @defgroup msgpack_zbuffer Compressed buffer
26 * @ingroup msgpack_buffer
30 typedef struct msgpack_zbuffer
{
36 #ifndef MSGPACK_ZBUFFER_INIT_SIZE
37 #define MSGPACK_ZBUFFER_INIT_SIZE 8192
40 static inline bool msgpack_zbuffer_init(
41 msgpack_zbuffer
* zbuf
, int level
, size_t init_size
);
42 static inline void msgpack_zbuffer_destroy(msgpack_zbuffer
* zbuf
);
44 static inline msgpack_zbuffer
* msgpack_zbuffer_new(int level
, size_t init_size
);
45 static inline void msgpack_zbuffer_free(msgpack_zbuffer
* zbuf
);
47 static inline char* msgpack_zbuffer_flush(msgpack_zbuffer
* zbuf
);
49 static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer
* zbuf
);
50 static inline size_t msgpack_zbuffer_size(const msgpack_zbuffer
* zbuf
);
52 static inline bool msgpack_zbuffer_reset(msgpack_zbuffer
* zbuf
);
53 static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer
* zbuf
);
54 static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer
* zbuf
);
57 #ifndef MSGPACK_ZBUFFER_RESERVE_SIZE
58 #define MSGPACK_ZBUFFER_RESERVE_SIZE 512
61 static inline int msgpack_zbuffer_write(void* data
, const char* buf
, size_t len
);
63 static inline bool msgpack_zbuffer_expand(msgpack_zbuffer
* zbuf
);
66 static inline bool msgpack_zbuffer_init(msgpack_zbuffer
* zbuf
,
67 int level
, size_t init_size
)
69 memset(zbuf
, 0, sizeof(msgpack_zbuffer
));
70 zbuf
->init_size
= init_size
;
71 if(deflateInit(&zbuf
->stream
, level
) != Z_OK
) {
78 static inline void msgpack_zbuffer_destroy(msgpack_zbuffer
* zbuf
)
80 deflateEnd(&zbuf
->stream
);
84 static inline msgpack_zbuffer
* msgpack_zbuffer_new(int level
, size_t init_size
)
86 msgpack_zbuffer
* zbuf
= (msgpack_zbuffer
*)malloc(sizeof(msgpack_zbuffer
));
87 if (zbuf
== NULL
) return NULL
;
88 if(!msgpack_zbuffer_init(zbuf
, level
, init_size
)) {
95 static inline void msgpack_zbuffer_free(msgpack_zbuffer
* zbuf
)
97 if(zbuf
== NULL
) { return; }
98 msgpack_zbuffer_destroy(zbuf
);
102 static inline bool msgpack_zbuffer_expand(msgpack_zbuffer
* zbuf
)
104 size_t used
= (size_t)((char *)(zbuf
->stream
.next_out
) - zbuf
->data
);
105 size_t csize
= used
+ zbuf
->stream
.avail_out
;
107 size_t nsize
= (csize
== 0) ? zbuf
->init_size
: csize
* 2;
109 char* tmp
= (char*)realloc(zbuf
->data
, nsize
);
115 zbuf
->stream
.next_out
= (Bytef
*)(tmp
+ used
);
116 zbuf
->stream
.avail_out
= (uInt
)(nsize
- used
);
121 static inline int msgpack_zbuffer_write(void* data
, const char* buf
, size_t len
)
123 msgpack_zbuffer
* zbuf
= (msgpack_zbuffer
*)data
;
125 assert(buf
|| len
== 0);
128 zbuf
->stream
.next_in
= (Bytef
*)buf
;
129 zbuf
->stream
.avail_in
= (uInt
)len
;
131 while(zbuf
->stream
.avail_in
> 0) {
132 if(zbuf
->stream
.avail_out
< MSGPACK_ZBUFFER_RESERVE_SIZE
) {
133 if(!msgpack_zbuffer_expand(zbuf
)) {
138 if(deflate(&zbuf
->stream
, Z_NO_FLUSH
) != Z_OK
) {
146 static inline char* msgpack_zbuffer_flush(msgpack_zbuffer
* zbuf
)
149 switch(deflate(&zbuf
->stream
, Z_FINISH
)) {
154 if(!msgpack_zbuffer_expand(zbuf
)) {
164 static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer
* zbuf
)
169 static inline size_t msgpack_zbuffer_size(const msgpack_zbuffer
* zbuf
)
171 return (size_t)((char *)(zbuf
->stream
.next_out
) - zbuf
->data
);
174 static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer
* zbuf
)
176 zbuf
->stream
.avail_out
+= (uInt
)((char*)zbuf
->stream
.next_out
- zbuf
->data
);
177 zbuf
->stream
.next_out
= (Bytef
*)zbuf
->data
;
180 static inline bool msgpack_zbuffer_reset(msgpack_zbuffer
* zbuf
)
182 if(deflateReset(&zbuf
->stream
) != Z_OK
) {
185 msgpack_zbuffer_reset_buffer(zbuf
);
189 static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer
* zbuf
)
191 char* tmp
= zbuf
->data
;
193 zbuf
->stream
.next_out
= NULL
;
194 zbuf
->stream
.avail_out
= 0;
205 #endif /* msgpack/zbuffer.h */