ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitCommon/OptIO/src/rzipunzip.c
Revision: 1.5
Committed: Fri Feb 27 17:39:30 2009 UTC (16 years, 2 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre1
Changes since 1.4: +10 -4 lines
Log Message:
Optimized hardcoded defaults for LZMA

File Contents

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