Change soft-fail to use the config, rather than env
[rbx.git] / shotgun / lib / compress.c
blob45b2fb84bde5079d63cdc3877a834cc8fd75569c
1 #include <bstrlib.h>
2 #include <zlib.h>
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;
13 int status, size;
14 OBJECT out;
16 z_stream zs;
17 zs.zfree = Z_NULL;
18 zs.zalloc = Z_NULL;
19 zs.opaque = Z_NULL;
20 zs.avail_in = strlen((char*)in) + 1; // termination is added by bstring.
21 zs.next_in = in;
23 status = inflateInit(&zs); // Returns zlib error code
24 if(status != Z_OK) {
25 inflateEnd(&zs);
26 return Qnil;
29 output = bfromcstralloc(zs.avail_in, "");
30 do {
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.
35 switch(status) {
36 case Z_OK:
37 // Fall through
38 case Z_STREAM_END:
39 bcatblk(output, out_buffer, size);
40 break;
41 default: // Punt on any other return value.
42 inflateEnd(&zs);
43 bdestroy(output);
44 return Qnil;
46 } while(zs.avail_out == 0);
48 inflateEnd(&zs);
50 if(status != Z_STREAM_END && status != Z_OK) {
51 bdestroy(output);
52 return Qnil;
54 out = string_newfrombstr(state, output);
55 bdestroy(output);
56 return out;
59 OBJECT compress_deflate(STATE, unsigned char* in) {
60 unsigned char out_buffer[ZLIB_CHUNK_SIZE];
61 bstring output = NULL;
62 int size, status;
63 OBJECT out;
65 z_stream zs;
66 zs.zfree = Z_NULL;
67 zs.zalloc = Z_NULL;
68 zs.opaque = Z_NULL;
70 status = deflateInit(&zs, Z_DEFAULT_COMPRESSION); // Returns zlib error code
71 if (status != Z_OK) {
72 deflateEnd(&zs);
73 return Qnil;
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]
77 zs.next_in = in;
78 output = bfromcstralloc(zs.avail_in, "");
79 do {
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.
84 switch (status) {
85 case Z_OK:
86 // Fall through
87 case Z_STREAM_END:
88 bcatblk(output, out_buffer, size);
89 break;
90 default: // Punt on any other return value.
91 deflateEnd(&zs);
92 bdestroy(output);
93 return Qnil;
95 } while(zs.avail_out == 0);
97 deflateEnd(&zs);
99 if(status != Z_STREAM_END && status != Z_OK) {
100 bdestroy(output);
101 return 0;
103 out = string_newfrombstr(state, output);
104 bdestroy(output);
105 return out;