4 #include "shotgun/lib/shotgun.h"
5 #include "shotgun/lib/string.h"
6 #include "shotgun/lib/compress.h"
8 #define ZLIB_CHUNK_SIZE 512
10 OBJECT
compress_inflate(STATE
, unsigned char* in
) {
11 unsigned char out_buffer
[ZLIB_CHUNK_SIZE
];
12 bstring output
= NULL
;
20 zs
.avail_in
= strlen((char*)in
) + 1; // termination is added by bstring.
23 status
= inflateInit(&zs
); // Returns zlib error code
29 output
= bfromcstralloc(zs
.avail_in
, "");
31 zs
.avail_out
= ZLIB_CHUNK_SIZE
;
32 zs
.next_out
= out_buffer
;
33 status
= inflate(&zs
, Z_SYNC_FLUSH
);
34 size
= ZLIB_CHUNK_SIZE
- zs
.avail_out
; // How much we got.
39 bcatblk(output
, out_buffer
, size
);
41 default: // Punt on any other return value.
46 } while(zs
.avail_out
== 0);
50 if(status
!= Z_STREAM_END
&& status
!= Z_OK
) {
54 out
= string_newfrombstr(state
, output
);
59 OBJECT
compress_deflate(STATE
, unsigned char* in
) {
60 unsigned char out_buffer
[ZLIB_CHUNK_SIZE
];
61 bstring output
= NULL
;
70 status
= deflateInit(&zs
, Z_DEFAULT_COMPRESSION
); // Returns zlib error code
76 zs
.avail_in
= strlen((char *)in
); // Lower than for zlib_inflate, so that we don't consume the zero-terminator. [and appease emacs' syntax parser]
78 output
= bfromcstralloc(zs
.avail_in
, "");
80 zs
.avail_out
= ZLIB_CHUNK_SIZE
;
81 zs
.next_out
= out_buffer
;
82 status
= deflate(&zs
, Z_FINISH
);
83 size
= ZLIB_CHUNK_SIZE
- zs
.avail_out
; // How much we got.
88 bcatblk(output
, out_buffer
, size
);
90 default: // Punt on any other return value.
95 } while(zs
.avail_out
== 0);
99 if(status
!= Z_STREAM_END
&& status
!= Z_OK
) {
103 out
= string_newfrombstr(state
, output
);