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