ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitCommon/OptIO/src/rzipunzip.c
Revision: 1.3
Committed: Wed Feb 25 10:54:40 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.2: +55 -7 lines
Log Message:
Small fixes for lzma

File Contents

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