ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitCommon/OptIO/src/rzipunzip.c
Revision: 1.6
Committed: Wed Mar 4 07:24:50 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, Mit_009, Mit_008pre2
Changes since 1.5: +4 -1 lines
Log Message:
Check if preload is activated

File Contents

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