ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitCommon/OptIO/src/rzipunzip.c
Revision: 1.7
Committed: Mon Sep 21 19:30:00 2009 UTC (15 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.6: +52 -23 lines
Log Message:
Provided independent access to compress/decompress functions.

File Contents

# User Rev Content
1 loizides 1.7 // $Id: rzipunzip.c,v 1.6 2009/03/04 07:24:50 loizides Exp $
2 loizides 1.1
3     #include "MitCommon/OptIO/src/rzipunzip.h"
4     #include "MitCommon/OptIO/src/zlib.h"
5     #include "MitCommon/OptIO/src/bzlib.h"
6     #include "MitCommon/OptIO/src/lzo/lzo1x.h"
7     #include "MitCommon/OptIO/src/rle.h"
8 loizides 1.2 #include "MitCommon/OptIO/src/LzmaEnc.h"
9     #include "MitCommon/OptIO/src/LzmaDec.h"
10 loizides 1.4 #include "MitCommon/OptIO/src/fpc.h"
11 loizides 1.1
12     #include <stdio.h>
13     #include <stdlib.h>
14     #include <string.h>
15    
16     #ifndef HDRSIZE
17     #define HDRSIZE 9
18     #else
19     #error "HDRSIZE already defined."
20     #endif
21    
22     #ifndef MYSIZE
23     #define MYSIZE 5*1024*1024
24     #else
25     #error "MYSIZE already defined."
26     #endif
27    
28     typedef unsigned char uch; /* code assumes unsigned bytes; these type- */
29    
30     extern int R__ZipMode;
31     char mymem[MYSIZE];
32     char *myptr = mymem;
33    
34 loizides 1.6 // the following will be set to one if code was activated
35     int activated = 0;
36    
37 loizides 1.1 // the following can be changed using the OptInt interface
38     double lzipfrac = 1;
39     double gzipfrac = 1;
40     double bzipfrac = 1;
41 loizides 1.2 double lzmafrac = 1;
42 loizides 1.1 int myverbose = 0;
43     int mystaticm = 0;
44    
45     //--------------------------------------------------------------------------------------------------
46     void *mymalloc(size_t size)
47     {
48     if(!mystaticm || size>MYSIZE)
49     return malloc(size);
50    
51     if(myptr-mymem>MYSIZE-size) {
52     myptr=mymem;
53     }
54    
55     void *v=(void*)myptr;
56     myptr+=size;
57     return v;
58     }
59    
60     //--------------------------------------------------------------------------------------------------
61 loizides 1.2 void mymfree(void *ptr)
62 loizides 1.1 {
63     if (mystaticm && (char*)ptr>=mymem && (char*)ptr<mymem+MYSIZE)
64     return;
65     free(ptr);
66     }
67    
68 loizides 1.7 //--------------------------------------------------------------------------------------------------
69 loizides 1.2 static void *SzAlloc(void *p, size_t size) { p = p; return mymalloc(size); }
70 loizides 1.7
71     //--------------------------------------------------------------------------------------------------
72 loizides 1.2 static void SzFree(void *p, void *address) { p = p; mymfree(address); }
73 loizides 1.7
74     //--------------------------------------------------------------------------------------------------
75 loizides 1.2 static ISzAlloc g_Alloc = { SzAlloc, SzFree };
76 loizides 1.7
77     //--------------------------------------------------------------------------------------------------
78 loizides 1.3 static void* my_balloc (void* opaque, int items, int size)
79     {
80     opaque = opaque;
81     return mymalloc(items*size);
82     }
83 loizides 1.7
84     //--------------------------------------------------------------------------------------------------
85 loizides 1.3 static void my_bfree (void* opaque, void* addr)
86     {
87     opaque = opaque;
88     mymfree(addr);
89     }
90 loizides 1.7
91     //--------------------------------------------------------------------------------------------------
92 loizides 1.3 static voidpf my_zalloc OF((voidpf opaque, unsigned items, unsigned size))
93     {
94     opaque = opaque;
95     return mymalloc(items*size);
96     }
97 loizides 1.7
98     //--------------------------------------------------------------------------------------------------
99 loizides 1.3 static void my_zfree OF((voidpf opaque, voidpf ptr))
100     {
101     opaque = opaque;
102     mymfree(ptr);
103     }
104    
105     //--------------------------------------------------------------------------------------------------
106     void delta_encode(char *buffer, int length)
107     {
108     char t = 0;
109     char original;
110     int i;
111     for (i=0; i < length; ++i) {
112     original = buffer[i];
113     buffer[i] -= t;
114     t = original;
115     }
116     }
117    
118 loizides 1.7 //--------------------------------------------------------------------------------------------------
119 loizides 1.3 void delta_decode(char *buffer, int length)
120     {
121     char t = 0;
122     int i;
123     for (i=0; i < length; ++i) {
124     buffer[i] += t;
125     t = buffer[i];
126     }
127     }
128    
129 loizides 1.1 //--------------------------------------------------------------------------------------------------
130 loizides 1.7 void R__myzip(int cxlevel, int *srcsize, char *src, int *tgtsize, char *tgt, int *irep)
131 loizides 1.1 /* int cxlevel; compression level */
132     /* int *srcsize, *tgtsize, *irep; source and target sizes, replay */
133     /* char *tgt, *src; source and target buffers */
134     {
135     int err = 0;
136     int method = 0;
137     unsigned int in_size = 0;
138     unsigned int out_size = 0;
139    
140     if (*tgtsize <= HDRSIZE) {
141     R__error("target buffer too small");
142     return;
143     }
144     if (*srcsize > 0xffffff) {
145     R__error("source buffer too big");
146     return;
147     }
148    
149     *irep = 0;
150     in_size = (unsigned)(*srcsize); /* decompressed size */
151     char *tgtptr = tgt+HDRSIZE; /* compress data */
152    
153 loizides 1.2 if (R__ZipMode == 99) { /*determine best of all methods*/
154 loizides 1.4 int cont = 1;
155     int msize = -1;
156     int zmode = -1;
157     int tgtlen = 2*in_size+64*1024;
158 loizides 1.1 char *mptr1 = mymalloc(tgtlen);
159     char *mptr2 = mymalloc(tgtlen);
160     char *mptr = 0;
161     char *mdum = 0;
162    
163     if (cont) {
164     zmode = 4;
165     msize = RLE_Compress(src, mptr1, in_size);
166     mptr = mptr1;
167     mdum = mptr2;
168     }
169    
170     if (cont && lzipfrac>0) {
171     err = lzo_init();
172     if ( err == LZO_E_OK) {
173     lzo_uint outlen = tgtlen;
174     char *lwmem = mymalloc(LZO1X_999_MEM_COMPRESS);
175     err = lzo1x_999_compress(src,in_size,mdum,&outlen,lwmem);
176     if (err == LZO_E_OK) {
177     if (outlen<lzipfrac*msize) {
178     msize = outlen;
179     zmode = 3;
180     char *tmp = mptr;
181     mptr = mdum;
182     mdum = tmp;
183     }
184     }
185 loizides 1.2 mymfree(lwmem);
186 loizides 1.1 }
187     }
188    
189     if (cont && gzipfrac>0) {
190 loizides 1.2 int outlen = *tgtsize;
191 loizides 1.1 z_stream stream;
192     stream.next_in = (Bytef*)src;
193     stream.avail_in = (uInt)(in_size);
194     stream.next_out = (Bytef*)(mdum);
195 loizides 1.2 stream.avail_out = (uInt)(outlen);
196 loizides 1.1 stream.zalloc = (alloc_func)0;
197     stream.zfree = (free_func)0;
198     stream.opaque = (voidpf)0;
199    
200     err = deflateInit(&stream, cxlevel);
201     if (err == Z_OK) {
202     err = deflate(&stream, Z_FINISH);
203     if (err == Z_STREAM_END) {
204     err = deflateEnd(&stream);
205     int outlen = stream.total_out;
206     if (outlen<gzipfrac*msize) {
207     msize = outlen;
208     zmode = 1;
209     char *tmp = mptr;
210     mptr = mdum;
211     mdum = tmp;
212     }
213     }
214     }
215     }
216    
217     if (cont && bzipfrac>0) {
218 loizides 1.2 int outlen = *tgtsize;
219 loizides 1.1 bz_stream stream;
220     stream.next_in = src;
221     stream.avail_in = (uInt)(in_size);
222     stream.next_out = mdum;
223 loizides 1.2 stream.avail_out = (uInt)(outlen);
224 loizides 1.1 stream.bzalloc = 0;
225     stream.bzfree = 0;
226     stream.opaque = 0;
227    
228     err = BZ2_bzCompressInit(&stream, 9, 0, 1);
229     if (err == BZ_OK) {
230     err = BZ2_bzCompress(&stream, BZ_FINISH);
231     if (err == BZ_STREAM_END) {
232     BZ2_bzCompressEnd(&stream);
233     }
234     int outlen = stream.total_out_lo32;
235     if (outlen<bzipfrac*msize) {
236     msize = outlen;
237     zmode = 2;
238     char *tmp = mptr;
239     mptr = mdum;
240     mdum = tmp;
241     }
242     }
243     }
244 loizides 1.2 if (cont && lzmafrac>0) {
245     CLzmaEncHandle enc = LzmaEnc_Create(&g_Alloc);
246     if (enc) {
247     CLzmaEncProps props;
248     LzmaEncProps_Init(&props);
249     props.level = cxlevel;
250 bendavid 1.5 props.dictSize = (1<<24);
251     props.lc = 0;
252     props.lp = 2;
253 loizides 1.2 SRes res = LzmaEnc_SetProps(enc, &props);
254     if (res == SZ_OK) {
255     SizeT outlen = *tgtsize;
256 loizides 1.3 res = LzmaEnc_MemEncode(enc, mdum, &outlen, src, in_size,
257 loizides 1.2 0, NULL, &g_Alloc, &g_Alloc);
258     if (res == SZ_OK) {
259     if (outlen<lzmafrac*msize) {
260     msize = outlen;
261     zmode = 5;
262     char *tmp = mptr;
263     mptr = mdum;
264     mdum = tmp;
265     }
266     }
267     }
268     }
269 loizides 1.3 LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
270 loizides 1.2 }
271 loizides 1.1
272 loizides 1.2 // determine best candidate
273 loizides 1.1 if (msize>=in_size) {
274     out_size = in_size;
275     memcpy(tgtptr,src,out_size);
276     tgt[0] = 'X'; /* signature xx */
277     tgt[1] = 'X';
278     method = 0;
279     } else {
280     switch (zmode) {
281     case 1:
282     tgt[0] = 'Z'; /* signature zlib */
283     tgt[1] = 'L';
284     method = Z_DEFLATED; //==8
285     break;
286     case 2:
287     tgt[0] = 'B'; /* signature bzlib */
288     tgt[1] = 'Z';
289     method = 2;
290     break;
291     case 3:
292     tgt[0] = 'L'; /* signature lzolib */
293     tgt[1] = 'O';
294     method = 19;
295     break;
296     case 4:
297     tgt[0] = 'R'; /* signature rlelib */
298     tgt[1] = 'E';
299     method = 4;
300 loizides 1.3 break;
301 loizides 1.2 case 5:
302     tgt[0] = 'L'; /* signature lzma */
303     tgt[1] = 'M';
304     method = 3;
305 loizides 1.1 break;
306     }
307     out_size = msize;
308     memcpy(tgtptr,mptr,out_size);
309     }
310 loizides 1.2 mymfree(mptr1);
311     mymfree(mptr2);
312     } else if (R__ZipMode == 5) { /*lzma*/
313     CLzmaEncHandle enc = LzmaEnc_Create(&g_Alloc);
314     if (enc == 0) {
315     printf("error %d - LzmaEnc_Create()\n", SZ_ERROR_MEM);
316     return;
317     }
318     CLzmaEncProps props;
319     LzmaEncProps_Init(&props);
320     props.level = cxlevel;
321 bendavid 1.5 props.dictSize = (1<<24);
322     props.lc = 0;
323     props.lp = 2;
324 loizides 1.2 SRes res = LzmaEnc_SetProps(enc, &props);
325     if (res != SZ_OK) {
326     printf("error %d - LzmaEnc_SetProps()\n", res);
327 loizides 1.3 LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
328 loizides 1.2 return;
329     }
330     res = LzmaEnc_MemEncode(enc, tgtptr, tgtsize, src, in_size,
331     0, NULL, &g_Alloc, &g_Alloc);
332     if (res != SZ_OK) {
333     printf("error %d - LzmaEnc_MemEncode", res);
334 loizides 1.3 LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
335 loizides 1.2 return;
336     }
337     LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
338     out_size = *tgtsize; /* compressed size */
339     if (out_size>=lzmafrac*in_size) {
340     out_size = in_size;
341     memcpy(tgtptr,src,out_size);
342     tgt[0] = 'X'; /* signature xx */
343     tgt[1] = 'X';
344     method = 0;
345     } else {
346     tgt[0] = 'L'; /* signature lzma */
347     tgt[1] = 'M';
348     method = 3;
349     }
350 loizides 1.1 } else if (R__ZipMode == 4) { /*rle*/
351     int tgtlen = 2*in_size+1;
352     char *lmem1 = mymalloc(tgtlen);
353    
354     out_size = RLE_Compress(src, lmem1, in_size);
355     if (out_size>=in_size) {
356     out_size = in_size;
357     memcpy(tgtptr,src,out_size);
358     tgt[0] = 'X'; /* signature xx */
359     tgt[1] = 'X';
360     method = 0;
361     } else {
362     memcpy(tgtptr,lmem1,out_size);
363     tgt[0] = 'R'; /* signature rlelib */
364     tgt[1] = 'E';
365     method = 4;
366     }
367 loizides 1.2 mymfree(lmem1);
368 loizides 1.1 } else if (R__ZipMode == 3) { /*lzo*/
369     err = lzo_init();
370     if ( err != LZO_E_OK) {
371     printf("error %d - lzo_init()\n", err);
372 loizides 1.2 return;
373 loizides 1.1 }
374    
375     lzo_uint tgtlen = 2*in_size+64*1024;
376     char *lmem1 = mymalloc(tgtlen);
377     char *lmem2 = 0;
378     if (cxlevel<=1) {
379     lmem2 = mymalloc(LZO1X_1_11_MEM_COMPRESS);
380     err = lzo1x_1_11_compress(src,in_size,lmem1,&tgtlen,lmem2);
381     method = 11;
382     } else if (cxlevel==2) {
383     lmem2 = mymalloc(LZO1X_1_12_MEM_COMPRESS);
384     err = lzo1x_1_12_compress(src,in_size,lmem1,&tgtlen,lmem2);
385     method = 12;
386     } else if (cxlevel<=5) {
387     lmem2 = mymalloc(LZO1X_1_15_MEM_COMPRESS);
388     err = lzo1x_1_15_compress(src,in_size,lmem1,&tgtlen,lmem2);
389     method = 15;
390     } else {
391     lmem2 = mymalloc(LZO1X_999_MEM_COMPRESS);
392     err = lzo1x_999_compress(src,in_size,lmem1,&tgtlen,lmem2);
393     method = 19;
394     }
395     if (err != LZO_E_OK) {
396 loizides 1.2 mymfree(lmem1);
397     mymfree(lmem2);
398 loizides 1.1 return;
399     }
400    
401     if (tgtlen>=lzipfrac*in_size) {
402     out_size = in_size;
403     memcpy(tgtptr,src,out_size);
404     tgt[0] = 'X'; /* signature xx */
405     tgt[1] = 'X';
406     method = 0;
407     } else {
408     out_size = tgtlen; /* compressed size */
409     memcpy(tgtptr,lmem1,out_size);
410     tgt[0] = 'L'; /* signature lzolib */
411     tgt[1] = 'O';
412     }
413 loizides 1.2 mymfree(lmem1);
414     mymfree(lmem2);
415 loizides 1.1 } else if (R__ZipMode == 2) { /*bzip*/
416     bz_stream stream;
417     stream.next_in = src;
418     stream.avail_in = (uInt)(in_size);
419     stream.next_out = tgtptr;
420     stream.avail_out = (uInt)(*tgtsize);
421     stream.bzalloc = 0;
422     stream.bzfree = 0;
423     stream.opaque = 0;
424    
425     err = BZ2_bzCompressInit(&stream, 9, 0, 1+(9-cxlevel)*25);
426     if (err != BZ_OK) {
427     printf("error %d in BZ2_bzCompressInit (bzlib)\n",err);
428     return;
429     }
430     err = BZ2_bzCompress(&stream, BZ_FINISH);
431     if (err != BZ_STREAM_END) {
432     BZ2_bzCompressEnd(&stream);
433     //printf("error %d in BZ2_bzCompress (bzlib) is not = %d\n",err,BZ_STREAM_END);
434     return;
435     }
436     err = BZ2_bzCompressEnd(&stream);
437    
438     out_size = stream.total_out_lo32; /* compressed size */
439     if (out_size>=bzipfrac*in_size) {
440     out_size = in_size;
441     memcpy(tgtptr,src,out_size);
442     tgt[0] = 'X'; /* signature xx */
443     tgt[1] = 'X';
444     method = 0;
445     } else {
446     tgt[0] = 'B'; /* signature bzlib */
447     tgt[1] = 'Z';
448     method = 2;
449     }
450     } else if (R__ZipMode == 1) { /*zip*/
451     z_stream stream;
452     stream.next_in = (Bytef*)src;
453     stream.avail_in = (uInt)(in_size);
454     stream.next_out = (Bytef*)(tgtptr);
455     stream.avail_out = (uInt)(*tgtsize);
456     stream.zalloc = (alloc_func)0;
457     stream.zfree = (free_func)0;
458     stream.opaque = (voidpf)0;
459    
460     err = deflateInit(&stream, cxlevel);
461     if (err != Z_OK) {
462     printf("error %d in deflateInit (zlib)\n",err);
463     return;
464     }
465     err = deflate(&stream, Z_FINISH);
466     if (err != Z_STREAM_END) {
467     deflateEnd(&stream);
468     //printf("error %d in deflate (zlib) is not = %d\n",err,Z_STREAM_END);
469     return;
470     }
471     err = deflateEnd(&stream);
472    
473     out_size = stream.total_out; /* compressed size */
474     if (out_size>=gzipfrac*in_size) {
475     out_size = in_size;
476     memcpy(tgtptr,src,out_size);
477     tgt[0] = 'X'; /* signature xx */
478     tgt[1] = 'X';
479     method = 0;
480     } else {
481     tgt[0] = 'Z'; /* signature zlib */
482     tgt[1] = 'L';
483     method = Z_DEFLATED;
484     }
485     } else if (R__ZipMode == 0) {
486     printf("error: Old zip method not supported in this patch.");
487     return;
488     }
489    
490     // fill rest of header
491     tgt[2] = (char)method;
492     tgt[3] = (char)(out_size & 0xff);
493     tgt[4] = (char)((out_size >> 8) & 0xff);
494     tgt[5] = (char)((out_size >> 16) & 0xff);
495     tgt[6] = (char)(in_size & 0xff);
496     tgt[7] = (char)((in_size >> 8) & 0xff);
497     tgt[8] = (char)((in_size >> 16) & 0xff);
498     *irep = out_size + HDRSIZE;
499    
500     if (myverbose==1||myverbose>9) {
501 loizides 1.7 printf("R__myzip:: zm=%d m=%d cl=%d: %c%c compressed %lu bytes into %lu bytes -> %.3f%%\n",
502 loizides 1.1 R__ZipMode, method, cxlevel, tgt[0], tgt[1],
503     (unsigned long)in_size, (unsigned long)out_size, (double)out_size/in_size*100.);
504     }
505     }
506    
507     //--------------------------------------------------------------------------------------------------
508 loizides 1.7 void R__zip(int cxlevel, int *srcsize, char *src, int *tgtsize, char *tgt, int *irep)
509     {
510     // Overwrite ROOT R__zip.
511    
512     R__myzip(cxlevel, srcsize, src, tgtsize, tgt, irep);
513     }
514    
515     //--------------------------------------------------------------------------------------------------
516     void R__myunzip(int *srcsize, char *src, int *tgtsize, char *tgt, int *irep)
517 loizides 1.1 /* Input: scrsize - size of input buffer */
518     /* src - input buffer */
519     /* tgtsize - size of target buffer */
520     /* Output: tgt - target buffer (decompressed) */
521     /* irep - size of decompressed data */
522     /* 0 - if error */
523     {
524     unsigned int osize = 0;
525     unsigned int ibufcnt = 0, obufcnt = 0;
526 loizides 1.7 char *ibufptr = 0, *obufptr = 0;
527 loizides 1.1 *irep = 0L;
528    
529     /* C H E C K H E A D E R */
530     if (*srcsize < HDRSIZE) {
531 loizides 1.7 fprintf(stderr,"R__myunzip: too small source\n");
532 loizides 1.1 return;
533     }
534    
535     char method = src[2];
536 loizides 1.2 if (method!=0 && method!= 2 && method!= 3 && method!= 4 && method!=Z_DEFLATED &&
537     method!=11 && method!=12 && method!=15 && method!=19) {
538 loizides 1.7 fprintf(stderr,"R__myunzip: error in header -> unknown method %d\n", method);
539 loizides 1.1 return;
540     }
541    
542     if ((method==0 && (src[0] != 'X' || src[1] != 'X')) ||
543     (method==2 && (src[0] != 'B' || src[1] != 'Z')) ||
544 loizides 1.2 (method==3 && (src[0] != 'L' || src[1] != 'M')) ||
545 loizides 1.1 (method==4 && (src[0] != 'R' || src[1] != 'E')) ||
546     (method==Z_DEFLATED && ((src[0] != 'C' || src[1] != 'S') &&
547     (src[0] != 'Z' || src[1] != 'L'))) ||
548     ((method==11 || method==12 || method==15 || method==19) &&
549     (src[0] != 'L' || src[1] != 'O'))) {
550 loizides 1.7 fprintf(stderr,"R__myunzip: error in header -> m=%d with %c%c\n",
551 loizides 1.1 method, src[0], src[1]);
552     return;
553     }
554    
555     ibufptr = src + HDRSIZE;
556     ibufcnt = (unsigned int)src[3] | ((unsigned int)src[4] << 8) | ((unsigned int)src[5] << 16);
557     osize = (unsigned int)src[6] | ((unsigned int)src[7] << 8) | ((unsigned int)src[8] << 16);
558     obufptr = tgt;
559     obufcnt = *tgtsize;
560    
561     if (obufcnt < osize) {
562 loizides 1.7 fprintf(stderr,"R__myunzip: too small target\n");
563 loizides 1.1 return;
564     }
565    
566     if (ibufcnt + HDRSIZE != *srcsize) {
567 loizides 1.7 fprintf(stderr,"R__myunzip: discrepancy in source length\n");
568 loizides 1.1 return;
569     }
570    
571     if (myverbose==2 || myverbose>9) {
572 loizides 1.7 printf("R__myunzip:: zm=%d m=%d: %c%c uncompressed %lu bytes from %lu bytes (%.3f%%)\n",
573 loizides 1.1 R__ZipMode, method, src[0], src[1], osize, ibufcnt, (double)ibufcnt/osize*100.);
574     }
575    
576     if (method==0) { // apparently this is not reached since underlying ROOT code catches this
577     if (ibufcnt!=osize) {
578 loizides 1.7 fprintf(stderr,"R__myunzip: error in header -> input should be output %d!=%d\n",
579 loizides 1.1 ibufcnt, osize);
580     return;
581     }
582     memcpy(obufptr,ibufptr,osize);
583     *irep = obufcnt;
584     return;
585     }
586    
587     /* D E C O M P R E S S D A T A */
588 loizides 1.2
589     if (src[0] == 'L' && src[1] == 'M') { /* lzma format */
590     CLzmaEncHandle enc = LzmaEnc_Create(&g_Alloc);
591     if (enc == 0) {
592 loizides 1.4 printf("error %d - LzmaEnc_Create (lzma)\n", SZ_ERROR_MEM);
593 loizides 1.2 return;
594     }
595     CLzmaEncProps props;
596     LzmaEncProps_Init(&props);
597 bendavid 1.5 props.dictSize = (1<<24);
598     props.lc = 0;
599     props.lp = 2;
600 loizides 1.2 SRes res = LzmaEnc_SetProps(enc, &props);
601     if (res != SZ_OK) {
602 loizides 1.4 printf("error %d - LzmaEnc_SetProps (lzma)\n", res);
603 loizides 1.3 LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
604 loizides 1.2 return;
605     }
606     size_t hsize = LZMA_PROPS_SIZE;
607     char *hptr = mymalloc(hsize);
608     res = LzmaEnc_WriteProperties(enc, hptr, &hsize);
609     if (res != SZ_OK) {
610 loizides 1.4 printf("error %d - LzmaEnc_WriteProperties (lzma)\n", res);
611 loizides 1.3 mymfree(hptr);
612     LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
613 loizides 1.2 return;
614     }
615    
616     SizeT new_len = obufcnt;
617     ELzmaStatus status;
618     res = LzmaDecode(obufptr, &obufcnt, ibufptr, &new_len, hptr, hsize,
619     LZMA_FINISH_END, &status, &g_Alloc);
620 loizides 1.4 if (res!=SZ_OK) {
621 loizides 1.7 fprintf(stderr,"R__myunzip: error %d in LzmaDecode (lzolib)\n", res);
622 loizides 1.4 mymfree(hptr);
623     return;
624     }
625 loizides 1.2 mymfree(hptr);
626 loizides 1.3 LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
627 loizides 1.2 *irep = obufcnt;
628     } else if (src[0] == 'R' && src[1] == 'E') { /* rle format */
629 loizides 1.1 RLE_Uncompress(ibufptr, obufptr, ibufcnt);
630     *irep = obufcnt;
631     } else if (src[0] == 'L' && src[1] == 'O') { /* lolib format */
632    
633     int err = lzo_init();
634     if ( err != LZO_E_OK) {
635 loizides 1.7 fprintf(stderr,"R__myunzip: error %d in lzo_init (lzolib)\n",err);
636 loizides 1.1 return;
637     }
638     lzo_uint new_len = obufcnt;
639     err = lzo1x_decompress(ibufptr,ibufcnt,obufptr,&new_len, NULL);
640     if ((err != LZO_E_OK) || (new_len!=osize)) {
641 loizides 1.7 fprintf(stderr,"R__myunzip: error %d (%d,%d) in lzo1x_decompress (lzolib)\n",
642 loizides 1.1 err, new_len, osize);
643     return;
644     }
645     *irep = obufcnt;
646     } else if (src[0] == 'B' && src[1] == 'Z') { /* bzlib format */
647     bz_stream stream; /* decompression stream */
648     stream.next_in = ibufptr;
649     stream.avail_in = ibufcnt;
650     stream.next_out = obufptr;
651     stream.avail_out = obufcnt;
652 loizides 1.4 stream.bzalloc = my_balloc;
653     stream.bzfree = my_bfree;
654 loizides 1.1 stream.opaque = 0;
655    
656     int err = BZ2_bzDecompressInit(&stream,0,0);
657     if (err != BZ_OK) {
658 loizides 1.7 fprintf(stderr,"R__myunzip: error %d in BZ2_bzDecompressInit (bzlib)\n",err);
659 loizides 1.1 return;
660     }
661     err = BZ2_bzDecompress(&stream);
662     if (err != BZ_STREAM_END) {
663 loizides 1.7 fprintf(stderr,"R__myunzip: error %d inBZ2_bzDecompress (bzlib)\n",err);
664 loizides 1.1 BZ2_bzDecompressEnd(&stream);
665     return;
666     }
667     BZ2_bzDecompressEnd(&stream);
668     *irep = stream.total_out_lo32;
669     } else if (src[0] == 'Z' && src[1] == 'L') { /* zlib format */
670     z_stream stream; /* decompression stream */
671     stream.next_in = (Bytef*)(&src[HDRSIZE]);
672     stream.avail_in = (uInt)(*srcsize);
673     stream.next_out = (Bytef*)tgt;
674     stream.avail_out = (uInt)(*tgtsize);
675 loizides 1.4 stream.zalloc = my_zalloc;
676     stream.zfree = my_zfree;
677 loizides 1.1 stream.opaque = (voidpf)0;
678    
679     int err = inflateInit(&stream);
680     if (err != Z_OK) {
681 loizides 1.7 fprintf(stderr,"R__myunzip: error %d in inflateInit (zlib)\n",err);
682 loizides 1.1 return;
683     }
684     err = inflate(&stream, Z_FINISH);
685     if (err != Z_STREAM_END) {
686     inflateEnd(&stream);
687 loizides 1.7 fprintf(stderr,"R__myunzip: error %d in inflate (zlib)\n",err);
688 loizides 1.1 return;
689     }
690     inflateEnd(&stream);
691     *irep = stream.total_out;
692     } else if (src[0] == 'C' && src[1] == 'S') { /* old zlib format */
693     if (R__Inflate(&ibufptr, &ibufcnt, &obufptr, &obufcnt)) {
694 loizides 1.7 fprintf(stderr,"R__myunzip: error during decompression\n");
695 loizides 1.1 return;
696     }
697    
698     /* if (obufptr - tgt != osize) {
699     There are some rare cases when a few more bytes are required */
700     if (obufptr - tgt > *tgtsize) {
701 loizides 1.7 fprintf(stderr,"R__myunzip: discrepancy (%ld) with initial size: %ld, tgtsize=%d\n",
702 loizides 1.1 (long)(obufptr - tgt),osize,*tgtsize);
703     *irep = obufptr - tgt;
704     return;
705     }
706     *irep = osize;
707 loizides 1.3 return;
708 loizides 1.1 } else {
709 loizides 1.7 fprintf(stderr,"R__myunzip: Format not supported -> m=%d with %d%d", method, src[0], src[1]);
710 loizides 1.1 return;
711     }
712     }
713 loizides 1.7
714     //--------------------------------------------------------------------------------------------------
715     void R__unzip(int *srcsize, uch *src, int *tgtsize, uch *tgt, int *irep)
716     {
717     // Overwrite ROOT R__unzip.
718    
719     R__myunzip(srcsize, (char*)src, tgtsize, (char*)tgt, irep);
720     }