-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.cpp
More file actions
181 lines (147 loc) · 4.4 KB
/
zip.cpp
File metadata and controls
181 lines (147 loc) · 4.4 KB
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
/*
* Written in 2009 by Lloyd Hilaiel
*
* License
*
* All the cruft you find here is public domain. You don't have to credit
* anyone to use this code, but my personal request is that you mention
* Igor Pavlov for his hard, high quality work.
*
* simple.c - a wrapper around easylzma to compress/decompress to memory
*/
#include "zip.hpp"
#include <string.h>
#include <assert.h>
#include "threads.hpp"
struct dataStream
{
const unsigned char * inData;
size_t inLen;
unsigned char * outData;
size_t outLen;
};
int
inputCallback(void *ctx, void *buf, size_t * size)
{
size_t rd = 0;
struct dataStream * ds = (struct dataStream *) ctx;
assert(ds != NULL);
rd = (ds->inLen < *size) ? ds->inLen : *size;
if (rd > 0) {
memcpy(buf, (void *) ds->inData, rd);
ds->inData += rd;
ds->inLen -= rd;
}
*size = rd;
return 0;
}
size_t
outputCallback(void *ctx, const void *buf, size_t size)
{
struct dataStream * ds = (struct dataStream *) ctx;
assert(ds != NULL);
if (size > 0) {
ds->outData =(unsigned char*) realloc(ds->outData, ds->outLen + size);
memcpy((void *) (ds->outData + ds->outLen), buf, size);
ds->outLen += size;
}
return size;
}
int
simpleCompress(elzma_file_format format, const unsigned char * inData,
size_t inLen, unsigned char ** outData,
size_t * outLen)
{
int rc;
elzma_compress_handle hand;
/* allocate compression handle */
hand = elzma_compress_alloc();
assert(hand != NULL);
rc = elzma_compress_config(hand, ELZMA_LC_DEFAULT,
ELZMA_LP_DEFAULT, ELZMA_PB_DEFAULT,
5, (1 << 20) /* 1mb */,
format, inLen);
if (rc != ELZMA_E_OK) {
elzma_compress_free(&hand);
return rc;
}
/* now run the compression */
{
struct dataStream ds;
ds.inData = inData;
ds.inLen = inLen;
ds.outData = NULL;
ds.outLen = 0;
rc = elzma_compress_run(hand, inputCallback, (void *) &ds,
outputCallback, (void *) &ds,
NULL, NULL);
if (rc != ELZMA_E_OK) {
if (ds.outData != NULL) free(ds.outData);
elzma_compress_free(&hand);
return rc;
}
*outData = ds.outData;
*outLen = ds.outLen;
}
return rc;
}
int simpleDecompress(elzma_file_format format, const unsigned char * inData,
size_t inLen, unsigned char ** outData,
size_t * outLen)
{
int rc;
elzma_decompress_handle hand;
//mtxlock(8);
hand = elzma_decompress_alloc();
/* now run the compression */
{
struct dataStream ds;
ds.inData = inData;
ds.inLen = inLen;
ds.outData = NULL;
ds.outLen = 0;
//try{
rc = elzma_decompress_run(hand, inputCallback, (void *) &ds, outputCallback, (void *) &ds, format);
//dbgv(rc,inLen);getchar();
//}catch(...){rc= ELZMA_E_OK-1;}
if (rc != ELZMA_E_OK) {
if (ds.outData != NULL) free(ds.outData);
//if (ds.inData != NULL) free(ds.inData);
elzma_decompress_free(&hand);
//mtxunlock(8);
return rc;
}
elzma_decompress_free(&hand);
*outData = ds.outData;
*outLen = ds.outLen;
}
//mtxunlock(8);
return rc;
}
string string_compresslzma(string &data,int comp1dec0){
unsigned char * hs = NULL;
size_t sz = 0;
int rc;
if(comp1dec0==0)
rc= simpleDecompress(ELZMA_lzma,(const unsigned char*)data.c_str(),data.size(), &hs, &sz);
if(comp1dec0==1)
rc= simpleCompress(ELZMA_lzma,(const unsigned char*)data.c_str(),data.size(), &hs, &sz);
dbgv(rc);
string str(reinterpret_cast<char*>(hs),sz);
return str;
}
///I have wrote little snipet that works for strings using easylzma lib, and I need for a vector float, since string as I know is some kind of vector, is this possible?
//void vfloat_compresslzma(vfloat &vdata, string &data,int comp1dec0){
// unsigned char * hs = NULL;
// size_t sz = 0;
// int rc;
// if(comp1dec0==0)
// rc= simpleDecompress(ELZMA_lzma,(const unsigned char*)data.c_str(),data.size(), &hs, &sz);
//
// if(comp1dec0==1)
// rc= simpleCompress(ELZMA_lzma,(const unsigned char*)data.c_str(),data.size(), &hs, &sz);
//dbgv(rc);
// string str(reinterpret_cast<char*>(hs),sz);
// return str;
//
//}