-
Notifications
You must be signed in to change notification settings - Fork 0
/
gunzip.c
259 lines (197 loc) · 5.67 KB
/
gunzip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <stdio.h>
#include "postgres.h"
#include "fmgr.h"
#include "zlib.h"
#include <assert.h>
#include "utils/elog.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
typedef struct
{
size_t alloced;
size_t used;
char *data;
} RAWDATA;
int uncomp(RAWDATA *rd, unsigned char *source, size_t sourceLen);
/**
*Memory handling
* Just a function handling allocation error on malloc
*/
static inline void* st_malloc(size_t len)
{
void *res = malloc(len);
if(res==NULL)
{
fprintf(stderr, "Misslyckad allokering av minne\n");
exit(EXIT_FAILURE);
}
return res;
}
/**
*Memory handling
* Just a function handling allocation error on realloc
*/
static inline void* st_realloc(void *ptr, size_t len)
{
void *res = realloc(ptr, len);
if(res==NULL)
{
free(ptr);
fprintf(stderr, "Misslyckad allokering av minne\n");
exit(EXIT_FAILURE);
}
return res;
}
/**
*Memory handling
* Just a function handling allocation error on calloc
*/
static inline void* st_calloc(int n, size_t s)
{
void *res = calloc(n, s);
if(res==NULL)
{
fprintf(stderr, "Misslyckad allokering av minne\n");
exit(EXIT_FAILURE);
}
return res;
}
static inline RAWDATA* init_data(size_t init_size)
{
RAWDATA *rd = st_malloc(sizeof(RAWDATA));
rd->data = st_calloc(1, init_size);
rd->alloced = init_size;
rd->used = 0;
return rd;
}
static inline void realloc_data(RAWDATA *rd)
{
size_t new_size = rd->alloced * 2;
rd->data = st_realloc(rd->data, new_size);
rd->alloced = new_size;
return;
}
static inline void realloc_data_needed(RAWDATA *rd, size_t needed)
{
size_t new_size = rd->alloced;
while (new_size < needed)
{
new_size*=2;
};
if(new_size>rd->alloced)
{
rd->data = st_realloc(rd->data, new_size);
rd->alloced = new_size;
}
return;
}
static inline void reset_data(RAWDATA *rd)
{
rd->used = 0;
return;
}
static inline void destroy_data(RAWDATA *rd)
{
free(rd->data);
rd->used = 0;
rd->alloced = 0;
free(rd);
rd = NULL;
}
int uncomp(RAWDATA *rd, unsigned char *source, size_t sourceLen)
{
int ret;
z_stream strm;
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
//ret = inflateInit(&strm);
ret = inflateInit2(&strm, 16+MAX_WBITS);
if (ret != Z_OK)
return ret;
strm.avail_out = rd->alloced - rd->used;;
strm.next_out = (unsigned char *) rd->data;
/* decompress until deflate stream ends or end of file */
do
{
strm.avail_in = sourceLen;
if (strm.avail_in == 0)
break;
strm.next_in = (unsigned char *) source;
/* run inflate() on input until output buffer not full */
while(1)
{
strm.avail_out = rd->alloced - rd->used;
//elog(NOTICE, "strm.avail_out = %d, rd->alloced =%ld, rd->used= %ld ",strm.avail_out,rd->alloced, rd->used);
strm.next_out =(unsigned char *) rd->data + rd->used;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
rd->used = rd->alloced - strm.avail_out;
if(strm.avail_out == 0)
realloc_data(rd);
else
break;
}
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
if(strm.avail_out == 0)
realloc_data(rd);
//elog(NOTICE, "slut: strm.avail_out = %d, rd->alloced =%ld, rd->used= %ld ",strm.avail_out,rd->alloced, rd->used);
rd->data[rd->used] = '\0';
rd->used++;
/* clean up and return */
(void)inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
Datum gunzip_text(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(gunzip_text);
Datum gunzip_text(PG_FUNCTION_ARGS) {
bytea *gzipped_input = PG_GETARG_BYTEA_P(0);
unsigned char *src = (unsigned char *) VARDATA(gzipped_input);
text *destination;
size_t srclen = VARSIZE(gzipped_input) - VARHDRSZ;
RAWDATA *rd = init_data(srclen * 2);// Vi börjar med dubbelt så stor output som input så får vi se
// unsigned char *dst_ptr = (unsigned char *) VARDATA(destination);
if(uncomp (rd,src, srclen) != Z_OK)
{
elog(NOTICE,"Failed unzipping\n");
PG_RETURN_NULL();
}
destination = (text *) palloc(VARHDRSZ + rd->used);
strcpy(VARDATA(destination), rd->data);
SET_VARSIZE(destination, VARHDRSZ + strlen(rd->data));
destroy_data(rd);
PG_RETURN_TEXT_P(destination);
}
Datum gunzip_bytea(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(gunzip_bytea);
Datum gunzip_bytea(PG_FUNCTION_ARGS) {
bytea *gzipped_input = PG_GETARG_BYTEA_P(0);
unsigned char *src = (unsigned char *) VARDATA(gzipped_input);
uint8_t *destination;
size_t srclen = VARSIZE(gzipped_input) - VARHDRSZ;
RAWDATA *rd = init_data(srclen * 2);// Vi börjar med dubbelt så stor output som input så får vi se
// unsigned char *dst_ptr = (unsigned char *) VARDATA(destination);
if(uncomp (rd,src, srclen) != Z_OK)
{
elog(NOTICE,"Failed unzipping\n");
PG_RETURN_NULL();
}
destination = (uint8_t *) palloc(VARHDRSZ + rd->used);
memcpy(VARDATA(destination), rd->data, rd->used);
SET_VARSIZE(destination, VARHDRSZ + strlen(rd->data));
destroy_data(rd);
PG_RETURN_BYTEA_P(destination);
}