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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
#include "LzmaFile.h"
#include "LzmaDec.h"
#include "Alloc.h"
#include "Types.h"
#include "7zFile.h"
//#include <sstream>
//#include <string>
#include <cmath>
#include <iostream>
#include <queue>
#include <cstdlib>
using namespace std;
const char *kCantReadMessage = "Can not read input file";
const char *kCantWriteMessage = "Can not write output file";
const char *kCantAllocateMessage = "Can not allocate memory";
const char *kDataErrorMessage = "Data error";
static void *SzAlloc(void *, size_t size) { return MyAlloc(size); }
static void SzFree(void *, void *address) { MyFree(address); }
static ISzAlloc g_Alloc = {SzAlloc, SzFree};
LzmaFile::LzmaFile() {
// fStorage.reserve(10000);
fStartNumber = false;
fReadSign = true;
fReadMantisseR = true;
fReadMantisseF = false;
fReadExponentSign = false;
fReadExponent = false;
fNegative = false;
fExponentNegative = false;
fMantisseR = 0;
fMantisseF = 0;
fMantisseFcount = 0;
fExponent = 0;
}
SRes LzmaFile::Open(const string &fileName) {
//fStrNumber.str("");
//fStrNumber.clear();
FileSeqInStream_CreateVTable(&inStream);
File_Construct(&inStream.file);
if (InFile_Open(&inStream.file, fileName.c_str()) != 0) {
cout << "Cannot open input file: " << fileName << endl;
cout << "First use: \n\t \'lzma --best " << fileName.substr(0, fileName.rfind(".lzma")) << "\'"
<< " to create it. " << endl;
exit(1);
}
ISeqInStream *stream = &inStream.s;
/* Read and parse header */
/* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */
unsigned char header[LZMA_PROPS_SIZE + 8];
RINOK(SeqInStream_Read(stream, header, sizeof(header)));
unpackSize = 0;
int i = 0;
for (i = 0; i < 8; i++)
unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);
LzmaDec_Construct(&state);
RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc));
LzmaDec_Init(&state);
inPos = 0;
inSize = 0;
outPos = 0;
return SZ_OK;
}
SRes LzmaFile::ReadNextNumber(double &data) {
if (fStorage.empty()) {
const int ret = DecodeBuffer();
if (ret != SZ_OK) {
cout << "Error in ReadNextNumber ret=" << ret << endl;
return SZ_ERROR_DATA;
}
}
data = fStorage.front();
fStorage.pop();
return SZ_OK;
}
SRes LzmaFile::FillArray(double *data, const int length) {
for (int i = 0; i < length; ++i) {
if (fStorage.empty()) {
const int ret = DecodeBuffer();
if (ret != SZ_OK) {
cout << "Error in FillArray i=" << i << " ret=" << ret << endl;
return SZ_ERROR_DATA;
}
}
data[i] = fStorage.front();
fStorage.pop();
}
return SZ_OK;
}
/*
double
LzmaFile::strToDouble(const char& p) {
// init
fR = 0;
fNegative = false;
if (*p == '-') {
neg = true;
++p;
}
while (*p >= '0' && *p <= '9') {
r = (r*10.0) + (*p - '0');
++p;
}
if (*p == '.') {
double f = 0.0;
int n = 0;
++p;
while (*p >= '0' && *p <= '9') {
f = (f*10.0) + (*p - '0');
++p;
++n;
}
r += f / std::pow(10.0, n);
}
if (neg) {
r = -r;
}
return r;
}
*/
SRes LzmaFile::DecodeBuffer() {
ISeqInStream *stream = &inStream.s;
const int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
if (inPos == inSize) {
inSize = IN_BUF_SIZE;
RINOK(stream->Read(stream, inBuf, &inSize));
inPos = 0;
}
SizeT inProcessed = inSize - inPos;
SizeT outProcessed = OUT_BUF_SIZE - outPos;
ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
ELzmaStatus status;
if (thereIsSize && outProcessed > unpackSize) {
outProcessed = (SizeT)unpackSize;
finishMode = LZMA_FINISH_END;
}
SRes res = LzmaDec_DecodeToBuf(&state, outBuf, &outProcessed, inBuf + inPos, &inProcessed, finishMode, &status);
inPos += inProcessed;
unpackSize -= outProcessed;
const char *strBuf = (const char *)outBuf;
int countC = 0;
do {
if (countC >= int(outProcessed)) {
// cout << " countC=" << countC
// << " outProcessed=" << outProcessed
// << endl;
break;
}
const char &C = strBuf[countC];
countC++;
//cout << "\'" << C << "\'" << endl;
if (C == ' ' || C == '\n') { // END OF NUMBER
if (!fStartNumber)
continue;
//istringstream strToNum(fStrNumber.str().c_str());
//double number = atof(fStrNumber.str().c_str());
//strToNum >> number;
const double number = (fNegative ? -1 : 1) * (fMantisseR + fMantisseF / pow(10, fMantisseFcount)) *
pow(10, (fExponentNegative ? -1 : 1) * fExponent);
//cout << " number=" << number << endl;
fStorage.push(number);
fStartNumber = false;
fReadSign = true;
fReadMantisseR = true;
fReadMantisseF = false;
fReadExponentSign = false;
fReadExponent = false;
fNegative = false;
fExponentNegative = false;
fMantisseR = 0;
fMantisseF = 0;
fMantisseFcount = 0;
fExponent = 0;
continue;
}
fStartNumber = true;
const int num = C - '0';
if (num >= 0 && num <= 9) {
if (fReadMantisseR) {
fReadSign = false;
fMantisseR = fMantisseR * 10 + num;
} else if (fReadMantisseF) {
fReadSign = false;
fMantisseF = fMantisseF * 10 + num;
++fMantisseFcount;
} else if (fReadExponent) {
fReadExponentSign = false;
fExponent = fExponent * 10 + num;
}
} else {
switch (C) {
case '-': {
if (fReadSign) {
fNegative = true;
fReadSign = false;
fReadMantisseR = true;
} else if (fReadExponentSign) {
fExponentNegative = true;
fReadExponentSign = false;
fReadExponent = true;
} else {
cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
exit(10);
}
} break;
case '.':
if (!fReadMantisseR) {
cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
exit(10);
}
fReadMantisseR = false;
fReadMantisseF = true;
break;
case 'e':
case 'E':
case 'D':
case 'd':
if (!fReadMantisseR || !fReadMantisseF) {
fReadMantisseR = false;
fReadMantisseF = false;
fReadExponentSign = true;
fReadExponent = true;
}
break;
default:
cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
exit(10);
break;
}
}
} while (true);
//strBuf.str("");
//strBuf.clear();
/*
if (!strNumber.str().empty()) {
cout << "NACHZUEGLER" << endl;
istringstream strToNum(strNumber.str());
double number;
strToNum >> number;
fStorage.push(number);
}
*/
if (res != SZ_OK || (thereIsSize && unpackSize == 0))
return res;
if (inProcessed == 0 && outProcessed == 0) {
if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
return SZ_ERROR_DATA;
return res;
}
return SZ_OK;
}
SRes LzmaFile::DecodeAll() {
ISeqInStream *stream = &inStream.s;
int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
for (;;) {
if (inPos == inSize) {
inSize = IN_BUF_SIZE;
RINOK(stream->Read(stream, inBuf, &inSize));
inPos = 0;
}
SizeT inProcessed = inSize - inPos;
SizeT outProcessed = OUT_BUF_SIZE - outPos;
ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
ELzmaStatus status;
if (thereIsSize && outProcessed > unpackSize) {
outProcessed = (SizeT)unpackSize;
finishMode = LZMA_FINISH_END;
}
SRes res = LzmaDec_DecodeToBuf(&state, outBuf, &outProcessed, inBuf + inPos, &inProcessed, finishMode, &status);
inPos += inProcessed;
unpackSize -= outProcessed;
unsigned int k = 0;
for (k = 0; k < outProcessed; ++k) {
printf("%c", outBuf[k]);
}
if (res != SZ_OK || (thereIsSize && unpackSize == 0))
return res;
if (inProcessed == 0 && outProcessed == 0) {
if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
return SZ_ERROR_DATA;
return res;
}
} // for loop
return 0;
}
SRes LzmaFile::Close() {
LzmaDec_Free(&state, &g_Alloc);
res = File_Close(&inStream.file);
return res;
}
|