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  int countNum = 0;
174  do {
175  if (countC >= int(outProcessed)) {
176  // cout << " countC=" << countC
177  // << " outProcessed=" << outProcessed
178  // << endl;
179  break;
180  }
181 
182  const char &C = strBuf[countC];
183  countC++;
184 
185  //cout << "\'" << C << "\'" << endl;
186 
187  if (C == ' ' || C == '\n') { // END OF NUMBER
188 
189  if (!fStartNumber)
190  continue;
191 
192  //istringstream strToNum(fStrNumber.str().c_str());
193  //double number = atof(fStrNumber.str().c_str());
194  //strToNum >> number;
195 
196  const double number = (fNegative ? -1 : 1) * (fMantisseR + fMantisseF / pow(10, fMantisseFcount)) *
197  pow(10, (fExponentNegative ? -1 : 1) * fExponent);
198  //cout << " number=" << number << endl;
199 
200  fStorage.push(number);
201  countNum++;
202 
203  fStartNumber = false;
204 
205  fReadSign = true;
206  fReadMantisseR = true;
207  fReadMantisseF = false;
208  fReadExponentSign = false;
209  fReadExponent = false;
210 
211  fNegative = false;
212  fExponentNegative = false;
213 
214  fMantisseR = 0;
215  fMantisseF = 0;
216  fMantisseFcount = 0;
217  fExponent = 0;
218 
219  continue;
220  }
221 
222  fStartNumber = true;
223  const int num = C - '0';
224  if (num >= 0 && num <= 9) {
225  if (fReadMantisseR) {
226  fReadSign = false;
227  fMantisseR = fMantisseR * 10 + num;
228  } else if (fReadMantisseF) {
229  fReadSign = false;
230  fMantisseF = fMantisseF * 10 + num;
231  ++fMantisseFcount;
232  } else if (fReadExponent) {
233  fReadExponentSign = false;
234  fExponent = fExponent * 10 + num;
235  }
236 
237  } else {
238  switch (C) {
239  case '-': {
240  if (fReadSign) {
241  fNegative = true;
242  fReadSign = false;
243  fReadMantisseR = true;
244  } else if (fReadExponentSign) {
245  fExponentNegative = true;
246  fReadExponentSign = false;
247  fReadExponent = true;
248  } else {
249  cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
250  exit(10);
251  }
252  } break;
253  case '.':
254  if (!fReadMantisseR) {
255  cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
256  exit(10);
257  }
258  fReadMantisseR = false;
259  fReadMantisseF = true;
260  break;
261  case 'e':
262  case 'E':
263  case 'D':
264  case 'd':
265  if (!fReadMantisseR || !fReadMantisseF) {
266  fReadMantisseR = false;
267  fReadMantisseF = false;
268  fReadExponentSign = true;
269  fReadExponent = true;
270  }
271  break;
272  default:
273  cout << "LzmaFile: found \'" << C << "\' at wrong position. " << endl;
274  exit(10);
275  break;
276  }
277  }
278 
279  } while (true);
280 
281  //strBuf.str("");
282  //strBuf.clear();
283 
284  /*
285  if (!strNumber.str().empty()) {
286  cout << "NACHZUEGLER" << endl;
287  istringstream strToNum(strNumber.str());
288  double number;
289  strToNum >> number;
290  fStorage.push(number);
291  }
292  */
293 
294  if (res != SZ_OK || (thereIsSize && unpackSize == 0))
295  return res;
296 
297  if (inProcessed == 0 && outProcessed == 0) {
298  if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
299  return SZ_ERROR_DATA;
300  return res;
301  }
302 
303  return SZ_OK;
304 }
305 
307  ISeqInStream *stream = &inStream.s;
308 
309  int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
310 
311  for (;;) {
312  if (inPos == inSize) {
313  inSize = IN_BUF_SIZE;
314  RINOK(stream->Read(stream, inBuf, &inSize));
315  inPos = 0;
316  }
317 
318  SizeT inProcessed = inSize - inPos;
319  SizeT outProcessed = OUT_BUF_SIZE - outPos;
320  ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
322 
323  if (thereIsSize && outProcessed > unpackSize) {
324  outProcessed = (SizeT)unpackSize;
325  finishMode = LZMA_FINISH_END;
326  }
327 
328  SRes res = LzmaDec_DecodeToBuf(&state, outBuf, &outProcessed, inBuf + inPos, &inProcessed, finishMode, &status);
329  inPos += inProcessed;
330  unpackSize -= outProcessed;
331 
332  unsigned int k = 0;
333  for (k = 0; k < outProcessed; ++k) {
334  printf("%c", outBuf[k]);
335  }
336 
337  if (res != SZ_OK || (thereIsSize && unpackSize == 0))
338  return res;
339 
340  if (inProcessed == 0 && outProcessed == 0) {
341  if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
342  return SZ_ERROR_DATA;
343  return res;
344  }
345 
346  } // for loop
347 
348  return 0;
349 }
350 
353  res = File_Close(&inStream.file);
354  return res;
355 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:373
kCantReadMessage
const char * kCantReadMessage
Definition: LzmaFile.cc:16
mps_fire.i
i
Definition: mps_fire.py:428
LzmaFile::LzmaFile
LzmaFile()
Definition: LzmaFile.cc:25
LzmaFile::Close
SRes Close()
Definition: LzmaFile.cc:351
LzmaDec_Allocate
SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
Definition: LzmaDec.cc:887
SzAlloc
static void * SzAlloc(void *, size_t size)
Definition: LzmaFile.cc:21
LZMA_FINISH_ANY
Definition: LzmaDec.h:80
mps_update.status
status
Definition: mps_update.py:69
LzmaDec.h
cms::cuda::stream
cudaStream_t stream
Definition: HistoContainer.h:57
gather_cfg.cout
cout
Definition: gather_cfg.py:144
UInt64
unsigned long long int UInt64
Definition: Types.h:88
7zFile.h
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
OUT_BUF_SIZE
#define OUT_BUF_SIZE
Definition: LzmaFile.h:22
ELzmaStatus
ELzmaStatus
Definition: LzmaDec.h:99
File_Construct
void File_Construct(CSzFile *p)
Definition: 7zFile.cc:28
LzmaFile::ReadNextNumber
SRes ReadNextNumber(double &data)
Definition: LzmaFile.cc:80
SZ_OK
#define SZ_OK
Definition: Types.h:25
LzmaDec_DecodeToBuf
SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
Definition: LzmaDec.cc:786
contentValuesFiles.number
number
Definition: contentValuesFiles.py:53
LzmaDec_Init
void LzmaDec_Init(CLzmaDec *p)
Definition: LzmaDec.cc:667
SzFree
static void SzFree(void *, void *address)
Definition: LzmaFile.cc:22
MyAlloc
void * MyAlloc(size_t size)
Definition: Alloc.cc:23
LzmaFile.h
kDataErrorMessage
const char * kDataErrorMessage
Definition: LzmaFile.cc:19
g_Alloc
static ISzAlloc g_Alloc
Definition: LzmaFile.cc:23
ISzAlloc
Definition: Types.h:215
dqmdumpme.k
k
Definition: dqmdumpme.py:60
ISeqInStream
Definition: Types.h:138
LzmaFile::Open
SRes Open(const std::string &fileName)
Definition: LzmaFile.cc:44
LzmaFile::DecodeAll
SRes DecodeAll()
Definition: LzmaFile.cc:306
SizeT
size_t SizeT
Definition: Types.h:97
File_Close
WRes File_Close(CSzFile *p)
Definition: 7zFile.cc:77
Int64
long long int Int64
Definition: Types.h:87
IN_BUF_SIZE
#define IN_BUF_SIZE
Definition: LzmaFile.h:21
LzmaDec_Free
void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
Definition: LzmaDec.cc:837
SeqInStream_Read
SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size)
Definition: 7zStream.cc:20
Alloc.h
SRes
int SRes
Definition: Types.h:43
LZMA_FINISH_END
Definition: LzmaDec.h:81
MyFree
void MyFree(void *address)
Definition: Alloc.cc:37
res
Definition: Electron.h:6
EgammaValidation_cff.num
num
Definition: EgammaValidation_cff.py:34
LZMA_STATUS_FINISHED_WITH_MARK
Definition: LzmaDec.h:101
LzmaDec_Construct
#define LzmaDec_Construct(p)
Definition: LzmaDec.h:67
kCantWriteMessage
const char * kCantWriteMessage
Definition: LzmaFile.cc:17
std
Definition: JetResolutionObject.h:76
RunInfoPI::state
state
Definition: RunInfoPayloadInspectoHelper.h:16
gen::C
C
Definition: PomwigHadronizer.cc:78
LzmaFile::DecodeBuffer
SRes DecodeBuffer()
Definition: LzmaFile.cc:145
InFile_Open
WRes InFile_Open(CSzFile *p, const char *name)
Definition: 7zFile.cc:58
FileSeqInStream_CreateVTable
void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
Definition: 7zFile.cc:243
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:292
funct::pow
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
Types.h
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53
kCantAllocateMessage
const char * kCantAllocateMessage
Definition: LzmaFile.cc:18
LZMA_PROPS_SIZE
#define LZMA_PROPS_SIZE
Definition: LzmaDec.h:25
LzmaFile::FillArray
SRes FillArray(double *data, const int length)
Definition: LzmaFile.cc:94
SZ_ERROR_DATA
#define SZ_ERROR_DATA
Definition: Types.h:27
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
RINOK
#define RINOK(x)
Definition: Types.h:52
ELzmaFinishMode
ELzmaFinishMode
Definition: LzmaDec.h:79