CMS 3D CMS Logo

LzmaFile.cc
Go to the documentation of this file.
1 #include "LzmaFile.h"
2 
3 #include "LzmaDec.h"
4 #include "Alloc.h"
5 #include "Types.h"
6 #include "7zFile.h"
7 
8 //#include <sstream>
9 //#include <string>
10 #include <cmath>
11 #include <iostream>
12 #include <queue>
13 #include <cstdlib>
14 using namespace std;
15 
16 const char *kCantReadMessage = "Can not read input file";
17 const char *kCantWriteMessage = "Can not write output file";
18 const char *kCantAllocateMessage = "Can not allocate memory";
19 const char *kDataErrorMessage = "Data error";
20 
21 static void *SzAlloc(void *, size_t size) { return MyAlloc(size); }
22 static void SzFree(void *, void *address) { MyFree(address); }
24 
26 {
27  // fStorage.reserve(10000);
28  fStartNumber = false;
29 
30  fReadSign = true;
31  fReadMantisseR = true;
32  fReadMantisseF = false;
33  fReadExponentSign = false;
34  fReadExponent = false;
35 
36  fNegative = false;
37  fExponentNegative = false;
38 
39  fMantisseR = 0;
40  fMantisseF = 0;
41  fMantisseFcount = 0;
42  fExponent = 0;
43 }
44 
45 
46 SRes
47 LzmaFile::Open(const string& fileName)
48 {
49  //fStrNumber.str("");
50  //fStrNumber.clear();
51 
53  File_Construct(&inStream.file);
54 
55  if (InFile_Open(&inStream.file, fileName.c_str()) != 0) {
56  cout << "Cannot open input file: " << fileName << endl;
57  cout << "First use: \n\t \'lzma --best " << fileName.substr(0, fileName.rfind(".lzma")) << "\'"
58  << " to create it. "
59  << endl;
60  exit(1);
61  }
62 
63  ISeqInStream *stream = &inStream.s;
64 
65  /* Read and parse header */
66  /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */
67  unsigned char header[LZMA_PROPS_SIZE + 8];
68  RINOK(SeqInStream_Read(stream, header, sizeof(header)));
69 
70  unpackSize = 0;
71  int i = 0;
72  for (i = 0; i < 8; i++)
73  unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);
74 
75  LzmaDec_Construct(&state);
76  RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc));
77  LzmaDec_Init(&state);
78 
79  inPos = 0;
80  inSize = 0;
81  outPos = 0;
82  return SZ_OK;
83 }
84 
85 SRes
87 {
88  if (fStorage.empty()) {
89  const int ret = DecodeBuffer();
90  if (ret != SZ_OK) {
91  cout << "Error in ReadNextNumber ret=" << ret << endl;
92  return SZ_ERROR_DATA;
93  }
94  }
95 
96  data = fStorage.front();
97  fStorage.pop();
98  return SZ_OK;
99 }
100 
101 
102 
103 SRes
104 LzmaFile::FillArray(double* data, const int length)
105 {
106  for (int i=0; i<length; ++i) {
107 
108  if (fStorage.empty()) {
109  const int ret = DecodeBuffer();
110  if (ret != SZ_OK) {
111  cout << "Error in FillArray i=" << i << " ret=" << ret << endl;
112  return SZ_ERROR_DATA;
113  }
114  }
115 
116  data[i] = fStorage.front();
117  fStorage.pop();
118 
119  }
120 
121  return SZ_OK;
122 
123 }
124 
125 
126 /*
127 double
128 LzmaFile::strToDouble(const char& p) {
129 
130  // init
131  fR = 0;
132  fNegative = false;
133 
134  if (*p == '-') {
135  neg = true;
136  ++p;
137  }
138  while (*p >= '0' && *p <= '9') {
139  r = (r*10.0) + (*p - '0');
140  ++p;
141  }
142  if (*p == '.') {
143  double f = 0.0;
144  int n = 0;
145  ++p;
146  while (*p >= '0' && *p <= '9') {
147  f = (f*10.0) + (*p - '0');
148  ++p;
149  ++n;
150  }
151  r += f / std::pow(10.0, n);
152  }
153  if (neg) {
154  r = -r;
155  }
156  return r;
157 }
158 */
159 
160 
161 SRes
163 {
164  ISeqInStream *stream = &inStream.s;
165 
166  const int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
167 
168 
169  if (inPos == inSize) {
170  inSize = IN_BUF_SIZE;
171  RINOK(stream->Read(stream, inBuf, &inSize));
172  inPos = 0;
173  }
174 
175  SizeT inProcessed = inSize - inPos;
176  SizeT outProcessed = OUT_BUF_SIZE - outPos;
177  ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
179 
180  if (thereIsSize && outProcessed > unpackSize) {
181  outProcessed = (SizeT)unpackSize;
182  finishMode = LZMA_FINISH_END;
183  }
184 
185  SRes res = LzmaDec_DecodeToBuf(&state, outBuf, &outProcessed,
186  inBuf + inPos, &inProcessed, finishMode, &status);
187  inPos += inProcessed;
188  unpackSize -= outProcessed;
189 
190 
191  const char* strBuf = (const char*)outBuf;
192 
193  int countC = 0;
194  int countNum = 0;
195  do {
196 
197  if (countC >= int(outProcessed)) {
198  // cout << " countC=" << countC
199  // << " outProcessed=" << outProcessed
200  // << endl;
201  break;
202  }
203 
204  const char& C = strBuf[countC];
205  countC++;
206 
207  //cout << "\'" << C << "\'" << endl;
208 
209  if (C==' ' || C=='\n') { // END OF NUMBER
210 
211  if (!fStartNumber)
212  continue;
213 
214  //istringstream strToNum(fStrNumber.str().c_str());
215  //double number = atof(fStrNumber.str().c_str());
216  //strToNum >> number;
217 
218  const double number = (fNegative?-1:1) * (fMantisseR + fMantisseF/pow(10, fMantisseFcount)) * pow(10, (fExponentNegative?-1:1) * fExponent);
219  //cout << " number=" << number << endl;
220 
221  fStorage.push(number);
222  countNum++;
223 
224  fStartNumber = false;
225 
226  fReadSign = true;
227  fReadMantisseR = true;
228  fReadMantisseF = false;
229  fReadExponentSign = false;
230  fReadExponent = false;
231 
232  fNegative = false;
233  fExponentNegative = false;
234 
235  fMantisseR = 0;
236  fMantisseF = 0;
237  fMantisseFcount = 0;
238  fExponent = 0;
239 
240  continue;
241  }
242 
243  fStartNumber = true;
244  const int num = C - '0';
245  if (num >= 0 && num <= 9) {
246 
247  if (fReadMantisseR) {
248  fReadSign = false;
249  fMantisseR = fMantisseR*10 + num;
250  } else if (fReadMantisseF) {
251  fReadSign = false;
252  fMantisseF = fMantisseF*10 + num;
253  ++fMantisseFcount;
254  } else if (fReadExponent) {
255  fReadExponentSign = false;
256  fExponent = fExponent*10 + num;
257  }
258 
259  } else {
260 
261  switch(C) {
262  case '-':
263  {
264  if (fReadSign) {
265  fNegative = true;
266  fReadSign = false;
267  fReadMantisseR = true;
268  } else if (fReadExponentSign) {
269  fExponentNegative = true;
270  fReadExponentSign = false;
271  fReadExponent = true;
272  } else {
273  cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
274  exit(10);
275  }
276  }
277  break;
278  case '.':
279  if (!fReadMantisseR) {
280  cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
281  exit(10);
282  }
283  fReadMantisseR = false;
284  fReadMantisseF = true;
285  break;
286  case 'e':
287  case 'E':
288  case 'D':
289  case 'd':
290  if (!fReadMantisseR || !fReadMantisseF) {
291  fReadMantisseR = false;
292  fReadMantisseF = false;
293  fReadExponentSign = true;
294  fReadExponent = true;
295  }
296  break;
297  default:
298  cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
299  exit(10);
300  break;
301  }
302  }
303 
304  } while(true);
305 
306  //strBuf.str("");
307  //strBuf.clear();
308 
309 
310  /*
311  if (!strNumber.str().empty()) {
312  cout << "NACHZUEGLER" << endl;
313  istringstream strToNum(strNumber.str());
314  double number;
315  strToNum >> number;
316  fStorage.push(number);
317  }
318  */
319 
320 
321 
322 
323  if (res != SZ_OK || (thereIsSize && unpackSize == 0))
324  return res;
325 
326  if (inProcessed == 0 && outProcessed == 0) {
327  if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
328  return SZ_ERROR_DATA;
329  return res;
330  }
331 
332  return SZ_OK;
333 }
334 
335 
336 
337 
338 SRes
340 {
341  ISeqInStream *stream = &inStream.s;
342 
343  int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
344 
345  for (;;) {
346 
347  if (inPos == inSize) {
348  inSize = IN_BUF_SIZE;
349  RINOK(stream->Read(stream, inBuf, &inSize));
350  inPos = 0;
351  }
352 
353  SizeT inProcessed = inSize - inPos;
354  SizeT outProcessed = OUT_BUF_SIZE - outPos;
355  ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
357 
358  if (thereIsSize && outProcessed > unpackSize) {
359  outProcessed = (SizeT)unpackSize;
360  finishMode = LZMA_FINISH_END;
361  }
362 
363  SRes res = LzmaDec_DecodeToBuf(&state, outBuf, &outProcessed,
364  inBuf + inPos, &inProcessed, finishMode, &status);
365  inPos += inProcessed;
366  unpackSize -= outProcessed;
367 
368 
369  unsigned int k=0;
370  for (k=0; k<outProcessed; ++k) {
371  printf("%c", outBuf[k]);
372  }
373 
374  if (res != SZ_OK || (thereIsSize && unpackSize == 0))
375  return res;
376 
377  if (inProcessed == 0 && outProcessed == 0) {
378  if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
379  return SZ_ERROR_DATA;
380  return res;
381  }
382 
383  } // for loop
384 
385  return 0;
386 }
387 
388 
389 
390 SRes
392 {
393  LzmaDec_Free(&state, &g_Alloc);
394  res = File_Close(&inStream.file);
395  return res;
396 }
size
Write out results.
#define SZ_ERROR_DATA
Definition: Types.h:27
void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
Definition: LzmaDec.cc:892
void * MyAlloc(size_t size)
Definition: Alloc.cc:23
#define LZMA_PROPS_SIZE
Definition: LzmaDec.h:26
static void * SzAlloc(void *, size_t size)
Definition: LzmaFile.cc:21
unsigned long long int UInt64
Definition: Types.h:83
void MyFree(void *address)
Definition: Alloc.cc:38
ELzmaFinishMode
Definition: LzmaDec.h:79
#define RINOK(x)
Definition: Types.h:52
SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size)
Definition: 7zStream.cc:22
SRes ReadNextNumber(double &data)
Definition: LzmaFile.cc:86
#define OUT_BUF_SIZE
Definition: LzmaFile.h:24
SRes Close()
Definition: LzmaFile.cc:391
ELzmaStatus
Definition: LzmaDec.h:100
static void SzFree(void *, void *address)
Definition: LzmaFile.cc:22
Definition: Electron.h:6
SRes DecodeBuffer()
Definition: LzmaFile.cc:162
void File_Construct(CSzFile *p)
Definition: 7zFile.cc:28
const char * kDataErrorMessage
Definition: LzmaFile.cc:19
const char * kCantReadMessage
Definition: LzmaFile.cc:16
size_t SizeT
Definition: Types.h:92
const char * kCantWriteMessage
Definition: LzmaFile.cc:17
int SRes
Definition: Types.h:43
SRes FillArray(double *data, const int length)
Definition: LzmaFile.cc:104
#define IN_BUF_SIZE
Definition: LzmaFile.h:23
#define SZ_OK
Definition: Types.h:25
const char * kCantAllocateMessage
Definition: LzmaFile.cc:18
SRes DecodeAll()
Definition: LzmaFile.cc:339
long long int Int64
Definition: Types.h:82
#define LzmaDec_Construct(p)
Definition: LzmaDec.h:71
int k[5][pyjets_maxn]
WRes File_Close(CSzFile *p)
Definition: 7zFile.cc:76
static ISzAlloc g_Alloc
Definition: LzmaFile.cc:23
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
Definition: 7zFile.cc:245
void LzmaDec_Init(CLzmaDec *p)
Definition: LzmaDec.cc:701
SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
Definition: LzmaDec.cc:840
SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
Definition: LzmaDec.cc:947
SRes Open(const std::string &fileName)
Definition: LzmaFile.cc:47
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
WRes InFile_Open(CSzFile *p, const char *name)
Definition: 7zFile.cc:58
LzmaFile()
Definition: LzmaFile.cc:25
SRes(* Read)(void *p, void *buf, size_t *size)
Definition: Types.h:139