-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqzlib.cpp
111 lines (82 loc) · 2.65 KB
/
qzlib.cpp
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
/*
qgz - gzip extension for kdb+/q
Copyright (C) 2016 Lucas Martin-King
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <cstdint>
#include <arpa/inet.h> // for ntohl, etc
#include "string.h"
#include "zlib.h"
#define KXVER 3
#include "k.h"
#define BUFLEN 65536
int gzDecompress(Byte *zdata, uLong nzdata, Byte *data, uLong *ndata)
{
int err = 0;
z_stream d_stream = {0}; /* decompression stream */
static char dummy_head[2] = {
0x8 + 0x7 * 0x10,
(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
};
d_stream.zalloc = NULL;
d_stream.zfree = NULL;
d_stream.opaque = NULL;
d_stream.next_in = zdata;
d_stream.avail_in = 0;
d_stream.next_out = data;
if (inflateInit2(&d_stream, -MAX_WBITS) != Z_OK) {
return -1;
}
// if(inflateInit2(&d_stream, 47) != Z_OK) return -1;
while (d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
if((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END)
break;
if (err != Z_OK) {
if (err == Z_DATA_ERROR) {
d_stream.next_in = (Bytef*) dummy_head;
d_stream.avail_in = sizeof(dummy_head);
if((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK) {
return -1;
}
} else {
return -1;
}
}
}
if (inflateEnd(&d_stream)!= Z_OK)
return -1;
*ndata = d_stream.total_out;
return 0;
}
extern "C" K gunzip(K x)
{
if (x->t != KG)
{
return krr("expecting a byte");
}
int len = x->n;
const Byte * buf = (Byte *)&kG(x)[0];
len = len+1;
unsigned char data[BUFLEN] = {0};
uLong datalen = sizeof(data) +1;
gzDecompress((Byte *) buf, len, (Byte *) data, &datalen);
// printf("\t data: %s\n", data);
// printf("\t datalen: %ld\n", datalen);
K uc = kp((char *)data);
if (! uc)
{
return krr("ktn uc");
}
return uc;
}