-
Notifications
You must be signed in to change notification settings - Fork 10
/
Zlib.xs
2374 lines (1997 loc) · 60 KB
/
Zlib.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Filename: Zlib.xs
* Author : Paul Marquess, <pmqs@cpan.org>
* Created : 22nd January 1996
* Version : 2.000
*
* Copyright (c) 1995-2013 Paul Marquess. All rights reserved.
* This program is free software; you can redistribute it and/or
* modify it under the same terms as Perl itself.
*
*/
/* Parts of this code are based on the files gzio.c and gzappend.c from
* the standard zlib source distribution. Below are the copyright statements
* from each.
*/
/* gzio.c -- IO on .gz files
* Copyright (C) 1995 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* gzappend -- command to append to a gzip file
Copyright (C) 2003 Mark Adler, all rights reserved
version 1.1, 4 Nov 2003
*/
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#if USE_ZLIB_NG
# include "zlib-ng.h"
#else
# include "zlib.h"
#endif
/* zlib prior to 1.06 doesn't know about z_off_t */
#ifndef z_off_t
# define z_off_t long
#endif
#if ! USE_ZLIB_NG && (! defined(ZLIB_VERNUM) || ZLIB_VERNUM < 0x1200)
# define NEED_DUMMY_BYTE_AT_END
#endif
#if USE_ZLIB_NG || (defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1210)
# define MAGIC_APPEND
# define AT_LEAST_ZLIB_1_2_1
#endif
#if USE_ZLIB_NG || (defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221)
# define AT_LEAST_ZLIB_1_2_2_1
#endif
#if USE_ZLIB_NG || (defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1222)
# define AT_LEAST_ZLIB_1_2_2_2
#endif
#if USE_ZLIB_NG || (defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1223)
# define AT_LEAST_ZLIB_1_2_2_3
#endif
#if USE_ZLIB_NG || (defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1230)
# define AT_LEAST_ZLIB_1_2_3
#endif
#if USE_ZLIB_NG || (defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1252)
/*
Use Z_SOLO to build source means need own malloc/free
*/
# define AT_LEAST_ZLIB_1_2_5_2
#endif
/* zlib vs zlib-ng */
#if USE_ZLIB_NG
/* zlibng native */
# define HAVE_ZLIB_NG_NATIVE TRUE
# define HAVE_ZLIB_NG_COMPAT FALSE
# ifndef ZLIBNG_VER_STATUS
# define ZLIBNG_VER_STATUS 0
# endif
# ifndef ZLIBNG_VER_MODIFIED
# define ZLIBNG_VER_MODIFIED 0
# endif
# define CRZ_adlerInitial zng_adler32(0L, Z_NULL, 0)
# define CRZ_crcInitial zng_crc32(0L, Z_NULL, 0)
# define CRZ_ZSTREAM zng_stream
# define CRZ_adler32 zng_adler32
# define CRZ_adler32_combine zng_adler32_combine
# define CRZ_crc32 zng_crc32
# define CRZ_crc32_combine zng_crc32_combine
# define CRZ_deflate zng_deflate
# define CRZ_deflateEnd zng_deflateEnd
# define CRZ_deflateInit zng_deflateInit
# define CRZ_deflateInit2 zng_deflateInit2
# define CRZ_deflateParams zng_deflateParams
# define CRZ_deflatePrime zng_deflatePrime
# define CRZ_deflateReset zng_deflateReset
# define CRZ_deflateSetDictionary zng_deflateSetDictionary
# define CRZ_deflateTune zng_deflateTune
# define CRZ_inflate zng_inflate
# define CRZ_inflateEnd zng_inflateEnd
# define CRZ_inflateInit2 zng_inflateInit2
# define CRZ_inflateReset zng_inflateReset
# define CRZ_inflateSetDictionary zng_inflateSetDictionary
# define CRZ_inflateSync zng_inflateSync
# define CRZ_zlibCompileFlags zng_zlibCompileFlags
/* zlib symbols & functions */
// # define CRZ_ZLIB_VERSION ZLIBNG_VERSION
// # define ZLIB_VERSION ZLIBNG_VERSION
# define CRZ_ZLIB_VERSION ""
# define ZLIB_VERSION ""
// # define CRZ_zlibVersion zlibng_version
// # define CRZ_zlib_version zlibng_version
const char *CRZ_zlibVersion(void) { return ""; }
const char *CRZ_zlib_version(void) { return ""; }
#else /* zlib specific */
# define HAVE_ZLIB_NG_NATIVE FALSE
/* Is this real zlib or zlib-ng in compat mode */
# ifdef ZLIBNG_VERSION
/* zlib-ng in compat mode */
# define HAVE_ZLIB_NG_COMPAT TRUE
# ifndef ZLIBNG_VER_STATUS
# define ZLIBNG_VER_STATUS 0
# endif
# ifndef ZLIBNG_VER_MODIFIED
# define ZLIBNG_VER_MODIFIED 0
# endif
const char *zlibng_version(void) { return ZLIBNG_VERSION ; }
# else
/* zlib native mode */
# define HAVE_ZLIB_NG_COMPAT FALSE
/* zlib doesn't have the ZLIBNG synbols, so create them */
# define ZLIBNG_VERSION ""
# define ZLIBNG_VERNUM 0
# define ZLIBNG_VER_MAJOR 0
# define ZLIBNG_VER_MINOR 0
# define ZLIBNG_VER_REVISION 0
# define ZLIBNG_VER_STATUS 0
# define ZLIBNG_VER_MODIFIED 0
# define ZLIBNG_VERNUM 0
const char *zlibng_version(void) { return ""; }
# endif
# define CRZ_adlerInitial adler32(0L, Z_NULL, 0)
# define CRZ_crcInitial crc32(0L, Z_NULL, 0)
# define CRZ_ZSTREAM z_stream
# define CRZ_adler32 adler32
# define CRZ_adler32_combine adler32_combine
# define CRZ_crc32 crc32
# define CRZ_crc32_combine crc32_combine
# define CRZ_deflate deflate
# define CRZ_deflateEnd deflateEnd
# define CRZ_deflateInit deflateInit
# define CRZ_deflateInit2 deflateInit2
# define CRZ_deflateParams deflateParams
# define CRZ_deflatePrime deflatePrime
# define CRZ_deflateReset deflateReset
# define CRZ_deflateSetDictionary deflateSetDictionary
# define CRZ_deflateTune deflateTune
# define CRZ_inflate inflate
# define CRZ_inflateEnd inflateEnd
# define CRZ_inflateInit2 inflateInit2
# define CRZ_inflateReset inflateReset
# define CRZ_inflateSetDictionary inflateSetDictionary
# define CRZ_inflateSync inflateSync
# define CRZ_zlibCompileFlags zlibCompileFlags
# define CRZ_zlibVersion zlibVersion
# define CRZ_zlib_version zlibVersion
#endif
#ifdef USE_PPPORT_H
# define NEED_sv_2pvbyte
# define NEED_sv_2pv_nolen
# define NEED_sv_pvn_force_flags
# include "ppport.h"
/* Proposed fix for https://github.com/Dual-Life/Devel-PPPort/issues/231 */
# if PERL_VERSION < 18
# ifdef sv_2pv
# undef sv_2pv
# endif
# if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
# define sv_2pv(sv, lp) ({ SV *_sv_2pv = (sv); SvPOKp(_sv_2pv) ? ((*(lp) = SvCUR(_sv_2pv)), SvPVX(_sv_2pv)) : Perl_sv_2pv(aTHX_ _sv_2pv, (lp)); })
# else
# define sv_2pv(sv, lp) (SvPOKp(sv) ? ((*(lp) = SvCUR(sv)), SvPVX(sv)) : Perl_sv_2pv(aTHX_ (sv), (lp)))
# endif
#endif
#endif
#if PERL_REVISION == 5 && PERL_VERSION == 9
/* For Andreas */
# define sv_pvbyte_force(sv,lp) sv_pvbyten_force(sv,lp)
#endif
#if PERL_REVISION == 5 && (PERL_VERSION < 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
# ifdef SvPVbyte_force
# undef SvPVbyte_force
# endif
# define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
#endif
#ifndef SvPVbyte_nolen
# define SvPVbyte_nolen SvPV_nolen
#endif
#if 0
# ifndef SvPVbyte_nolen
# define SvPVbyte_nolen SvPV_nolen
# endif
# ifndef SvPVbyte_force
# define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
# endif
#endif
#if PERL_REVISION == 5 && (PERL_VERSION >= 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
# define UTF8_AVAILABLE
#endif
typedef int DualType ;
typedef int int_undef ;
typedef struct di_stream {
int flags ;
#define FLAG_APPEND 1
#define FLAG_CRC32 2
#define FLAG_ADLER32 4
#define FLAG_CONSUME_INPUT 8
#define FLAG_LIMIT_OUTPUT 16
uLong crc32 ;
uLong adler32 ;
CRZ_ZSTREAM stream;
uLong bufsize;
SV * dictionary ;
uLong dict_adler ;
int last_error ;
bool zip_mode ;
/* #define SETP_BYTE */
#ifdef SETP_BYTE
/* SETP_BYTE only works with zlib up to 1.2.8 */
bool deflateParams_out_valid ;
Bytef deflateParams_out_byte;
#else
#define deflateParams_BUFFER_SIZE 0x40000
uLong deflateParams_out_length;
Bytef* deflateParams_out_buffer;
#endif
int Level;
int Method;
int WindowBits;
int MemLevel;
int Strategy;
uLong bytesInflated ;
uLong compressedBytes ;
uLong uncompressedBytes ;
#ifdef MAGIC_APPEND
#define WINDOW_SIZE 32768U
bool matchedEndBlock;
Bytef* window ;
int window_lastbit, window_left, window_full;
unsigned window_have;
off_t window_lastoff, window_end;
off_t window_endOffset;
uLong lastBlockOffset ;
unsigned char window_lastByte ;
#endif
} di_stream;
typedef di_stream * deflateStream ;
typedef di_stream * Compress__Raw__Zlib__deflateStream ;
typedef di_stream * inflateStream ;
typedef di_stream * Compress__Raw__Zlib__inflateStream ;
typedef di_stream * Compress__Raw__Zlib__inflateScanStream ;
#define ZMALLOC(to, typ) (to = (typ *)safecalloc(sizeof(typ), 1))
/* Figure out the Operating System */
#ifdef MSDOS
# define OS_CODE 0x00
#endif
#if defined(AMIGA) || defined(AMIGAOS) || defined(__amigaos4__)
# define OS_CODE 0x01
#endif
#if defined(VAXC) || defined(VMS)
# define OS_CODE 0x02
#endif
#if 0 /* VM/CMS */
# define OS_CODE 0x04
#endif
#if defined(ATARI) || defined(atarist)
# define OS_CODE 0x05
#endif
#ifdef OS2
# define OS_CODE 0x06
#endif
#if defined(MACOS) || defined(TARGET_OS_MAC)
# define OS_CODE 0x07
#endif
#if 0 /* Z-System */
# define OS_CODE 0x08
#endif
#if 0 /* CP/M */
# define OS_CODE 0x09
#endif
#ifdef TOPS20
# define OS_CODE 0x0a
#endif
#ifdef WIN32 /* Window 95 & Windows NT */
# define OS_CODE 0x0b
#endif
#if 0 /* QDOS */
# define OS_CODE 0x0c
#endif
#if 0 /* Acorn RISCOS */
# define OS_CODE 0x0d
#endif
#if 0 /* ??? */
# define OS_CODE 0x0e
#endif
#ifdef __50SERIES /* Prime/PRIMOS */
# define OS_CODE 0x0F
#endif
/* Default to UNIX */
#ifndef OS_CODE
# define OS_CODE 0x03 /* assume Unix */
#endif
#ifndef GZIP_OS_CODE
# define GZIP_OS_CODE OS_CODE
#endif
/* static const char * const my_z_errmsg[] = { */
static const char my_z_errmsg[][32] = {
"need dictionary", /* Z_NEED_DICT 2 */
"stream end", /* Z_STREAM_END 1 */
"", /* Z_OK 0 */
"file error", /* Z_ERRNO (-1) */
"stream error", /* Z_STREAM_ERROR (-2) */
"data error", /* Z_DATA_ERROR (-3) */
"insufficient memory", /* Z_MEM_ERROR (-4) */
"buffer error", /* Z_BUF_ERROR (-5) */
"incompatible version",/* Z_VERSION_ERROR(-6) */
""};
#define setDUALstatus(var, err) \
sv_setnv(var, (double)err) ; \
sv_setpv(var, ((err) ? GetErrorString(err) : "")) ; \
SvNOK_on(var);
#if defined(__SYMBIAN32__)
# define NO_WRITEABLE_DATA
#endif
/* Set TRACE_DEFAULT to a non-zero value to enable tracing */
#define TRACE_DEFAULT 0
#if defined(NO_WRITEABLE_DATA) || TRACE_DEFAULT == 0
# define trace TRACE_DEFAULT
#else
static int trace = TRACE_DEFAULT ;
#endif
/* Dodge PerlIO hiding of these functions. */
#undef printf
static char *
#ifdef CAN_PROTOTYPE
GetErrorString(int error_no)
#else
GetErrorString(error_no)
int error_no ;
#endif
{
dTHX;
char * errstr ;
if (error_no == Z_ERRNO) {
errstr = Strerror(errno) ;
}
else
/* errstr = gzerror(fil, &error_no) ; */
errstr = (char*) my_z_errmsg[2 - error_no];
return errstr ;
}
#ifdef MAGIC_APPEND
/*
The following two functions are taken almost directly from
examples/gzappend.c. Only cosmetic changes have been made to conform to
the coding style of the rest of the code in this file.
*/
/* return the greatest common divisor of a and b using Euclid's algorithm,
modified to be fast when one argument much greater than the other, and
coded to avoid unnecessary swapping */
static unsigned
#ifdef CAN_PROTOTYPE
gcd(unsigned a, unsigned b)
#else
gcd(a, b)
unsigned a;
unsigned b;
#endif
{
unsigned c;
while (a && b)
if (a > b) {
c = b;
while (a - c >= c)
c <<= 1;
a -= c;
}
else {
c = a;
while (b - c >= c)
c <<= 1;
b -= c;
}
return a + b;
}
/* rotate list[0..len-1] left by rot positions, in place */
static void
#ifdef CAN_PROTOTYPE
rotate(unsigned char *list, unsigned len, unsigned rot)
#else
rotate(list, len, rot)
unsigned char *list;
unsigned len ;
unsigned rot;
#endif
{
unsigned char tmp;
unsigned cycles;
unsigned char *start, *last, *to, *from;
/* normalize rot and handle degenerate cases */
if (len < 2) return;
if (rot >= len) rot %= len;
if (rot == 0) return;
/* pointer to last entry in list */
last = list + (len - 1);
/* do simple left shift by one */
if (rot == 1) {
tmp = *list;
memmove(list, list + 1, len - 1);
*last = tmp;
return;
}
/* do simple right shift by one */
if (rot == len - 1) {
tmp = *last;
memmove(list + 1, list, len - 1);
*list = tmp;
return;
}
/* otherwise do rotate as a set of cycles in place */
cycles = gcd(len, rot); /* number of cycles */
do {
start = from = list + cycles; /* start index is arbitrary */
tmp = *from; /* save entry to be overwritten */
for (;;) {
to = from; /* next step in cycle */
from += rot; /* go right rot positions */
if (from > last) from -= len; /* (pointer better not wrap) */
if (from == start) break; /* all but one shifted */
*to = *from; /* shift left */
}
*to = tmp; /* complete the circle */
} while (--cycles);
}
#endif /* MAGIC_APPEND */
static void
#ifdef CAN_PROTOTYPE
DispHex(const void * ptr, int length)
#else
DispHex(ptr, length)
const void * ptr;
int length;
#endif
{
char * p = (char*)ptr;
int i;
for (i = 0; i < length; ++i) {
printf(" %02x", 0xFF & *(p+i));
}
}
static void
#ifdef CAN_PROTOTYPE
DispStream(di_stream * s, const char * message)
#else
DispStream(s, message)
di_stream * s;
const char * message;
#endif
{
#if 0
if (! trace)
return ;
#endif
#define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
printf("DispStream %p", s) ;
if (message)
printf("- %s \n", message) ;
printf("\n") ;
if (!s) {
printf(" stream pointer is NULL\n");
}
else {
printf(" stream %p\n", &(s->stream));
printf(" zalloc %p\n", s->stream.zalloc);
printf(" zfree %p\n", s->stream.zfree);
printf(" opaque %p\n", s->stream.opaque);
printf(" state %p\n", s->stream.state);
if (s->stream.msg)
printf(" msg %s\n", s->stream.msg);
else
printf(" msg \n");
printf(" next_in %p", s->stream.next_in);
if (s->stream.next_in){
printf(" =>");
DispHex(s->stream.next_in, 4);
}
printf("\n");
printf(" next_out %p", s->stream.next_out);
if (s->stream.next_out){
printf(" =>");
DispHex(s->stream.next_out, 4);
}
printf("\n");
printf(" avail_in %lu\n", (unsigned long)s->stream.avail_in);
printf(" avail_out %lu\n", (unsigned long)s->stream.avail_out);
printf(" total_in %ld\n", s->stream.total_in);
printf(" total_out %ld\n", s->stream.total_out);
#if ! USE_ZLIB_NG
printf(" adler %ld\n", s->stream.adler );
#else
printf(" adler %u\n", s->stream.adler );
#endif
printf(" bufsize %ld\n", s->bufsize);
printf(" dictionary %p\n", s->dictionary);
printf(" dict_adler 0x%ld\n",s->dict_adler);
printf(" zip_mode %d\n", s->zip_mode);
printf(" crc32 0x%x\n", (unsigned)s->crc32);
printf(" adler32 0x%x\n", (unsigned)s->adler32);
printf(" flags 0x%x\n", s->flags);
printf(" APPEND %s\n", EnDis(FLAG_APPEND));
printf(" CRC32 %s\n", EnDis(FLAG_CRC32));
printf(" ADLER32 %s\n", EnDis(FLAG_ADLER32));
printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
printf(" LIMIT %s\n", EnDis(FLAG_LIMIT_OUTPUT));
#ifdef MAGIC_APPEND
printf(" window %p\n", s->window);
#endif
printf("\n");
}
}
#ifdef AT_LEAST_ZLIB_1_2_5_2
voidpf my_zcalloc (voidpf opaque, unsigned items, unsigned size)
{
PERL_UNUSED_VAR(opaque);
/* TODO - put back to calloc */
/* return safecalloc(items, size); */
return (voidpf)safemalloc(items* size);
}
void my_zcfree (voidpf opaque, voidpf ptr)
{
PERL_UNUSED_VAR(opaque);
safefree(ptr);
return;
}
#endif
static di_stream *
#ifdef CAN_PROTOTYPE
InitStream(void)
#else
InitStream()
#endif
{
di_stream *s ;
ZMALLOC(s, di_stream) ;
#ifdef AT_LEAST_ZLIB_1_2_5_2
s->stream.zalloc = my_zcalloc;
s->stream.zfree = my_zcfree;
#endif
return s ;
}
static void
#ifdef CAN_PROTOTYPE
PostInitStream(di_stream * s, int flags, int bufsize, int windowBits)
#else
PostInitStream(s, flags, bufsize, windowBits)
di_stream *s ;
int flags ;
int bufsize ;
int windowBits ;
#endif
{
s->bufsize = bufsize ;
s->compressedBytes =
s->uncompressedBytes =
s->last_error = 0 ;
s->flags = flags ;
s->zip_mode = (windowBits < 0) ;
if (flags & FLAG_CRC32)
s->crc32 = CRZ_crcInitial ;
if (flags & FLAG_ADLER32)
s->adler32 = CRZ_adlerInitial ;
}
static SV*
#ifdef CAN_PROTOTYPE
deRef(SV * sv, const char * string)
#else
deRef(sv, string)
SV * sv ;
char * string;
#endif
{
dTHX;
SvGETMAGIC(sv);
if (SvROK(sv)) {
sv = SvRV(sv) ;
SvGETMAGIC(sv);
switch(SvTYPE(sv)) {
case SVt_PVAV:
case SVt_PVHV:
case SVt_PVCV:
croak("%s: buffer parameter is not a SCALAR reference", string);
default:
break;
}
if (SvROK(sv))
croak("%s: buffer parameter is a reference to a reference", string) ;
}
if (!SvOK(sv))
sv = sv_2mortal(newSVpv("", 0));
return sv ;
}
static SV*
#ifdef CAN_PROTOTYPE
deRef_l(SV * sv, const char * string)
#else
deRef_l(sv, string)
SV * sv ;
char * string ;
#endif
{
dTHX;
bool wipe = 0 ;
STRLEN na;
SvGETMAGIC(sv);
wipe = ! SvOK(sv) ;
if (SvROK(sv)) {
sv = SvRV(sv) ;
SvGETMAGIC(sv);
wipe = ! SvOK(sv) ;
switch(SvTYPE(sv)) {
case SVt_PVAV:
case SVt_PVHV:
case SVt_PVCV:
croak("%s: buffer parameter is not a SCALAR reference", string);
default:
break;
}
if (SvROK(sv))
croak("%s: buffer parameter is a reference to a reference", string) ;
}
if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
croak("%s: buffer parameter is read-only", string);
SvUPGRADE(sv, SVt_PV);
if (wipe)
sv_setpv(sv, "") ;
else
(void)SvPVbyte_force(sv, na) ;
return sv ;
}
#if 0
int
flushToBuffer(di_stream* s, int flush)
{
dTHX;
int ret ;
CRZ_ZSTREAM * strm = &s->stream;
Bytef* output = s->deflateParams_out_buffer ;
strm->next_in = NULL;
strm->avail_in = 0;
uLong total_output = 0;
uLong have = 0;
do
{
if (output)
output = (unsigned char *)saferealloc(output, total_output + s->bufsize);
else
output = (unsigned char *)safemalloc(s->bufsize);
strm->next_out = output + total_output;
strm->avail_out = s->bufsize;
ret = deflate(strm, flush); /* no bad return value */
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */
if(ret == Z_STREAM_ERROR)
{
safefree(output);
return ret;
}
have = s->bufsize - strm->avail_out;
total_output += have;
//fprintf(stderr, "FLUSH %s %d, return %d\n", flush_flags[flush], have, ret);
} while (strm->avail_out == 0);
s->deflateParams_out_buffer = output;
s->deflateParams_out_length = total_output;
return Z_OK;
}
#endif
#ifndef SETP_BYTE
int
flushParams(di_stream* s)
{
dTHX;
int ret ;
CRZ_ZSTREAM * strm = &s->stream;
Bytef* output = s->deflateParams_out_buffer ;
uLong total_output = s->deflateParams_out_length;
uLong have = 0;
strm->next_in = NULL;
strm->avail_in = 0;
do
{
if (output)
output = (unsigned char *)saferealloc(output, total_output + s->bufsize);
else
output = (unsigned char *)safemalloc(s->bufsize);
strm->next_out = output + total_output;
strm->avail_out = s->bufsize;
ret = CRZ_deflateParams(&(s->stream), s->Level, s->Strategy);
/* fprintf(stderr, "deflateParams %d %s %lu\n", ret,
GetErrorString(ret), s->bufsize - strm->avail_out); */
if (ret == Z_STREAM_ERROR)
break;
have = s->bufsize - strm->avail_out;
total_output += have;
} while (ret == Z_BUF_ERROR) ;
if(ret == Z_STREAM_ERROR)
safefree(output);
else
{
s->deflateParams_out_buffer = output;
s->deflateParams_out_length = total_output;
}
return ret;
}
#endif /* ! SETP_BYTE */
#include "constants.h"
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
REQUIRE: 1.924
PROTOTYPES: DISABLE
INCLUDE: constants.xs
BOOT:
#if ! USE_ZLIB_NG
/* Check this version of zlib is == 1 */
if (CRZ_zlibVersion()[0] != '1')
croak("Compress::Raw::Zlib needs zlib version 1.x\n") ;
#endif
{
/* Create the $os_code scalar */
SV * os_code_sv = perl_get_sv("Compress::Raw::Zlib::gzip_os_code", GV_ADDMULTI) ;
sv_setiv(os_code_sv, GZIP_OS_CODE) ;
}
{
/* BUILD_ZLIB */
SV * os_code_sv = perl_get_sv("Compress::Raw::Zlib::BUILD_ZLIB", GV_ADDMULTI) ;
sv_setiv(os_code_sv, Perl_crz_BUILD_ZLIB) ;
}
#define Zip_zlib_version() (const char*)CRZ_zlib_version()
const char*
Zip_zlib_version()
const char*
zlibng_version()
#define Zip_is_zlib_native() (! (HAVE_ZLIB_NG_NATIVE || HAVE_ZLIB_NG_COMPAT))
bool
Zip_is_zlib_native()
#define Zip_is_zlibng_native() (bool)HAVE_ZLIB_NG_NATIVE
bool
Zip_is_zlibng_native()
#define Zip_is_zlibng_compat() (bool)HAVE_ZLIB_NG_COMPAT
bool
Zip_is_zlibng_compat()
#define Zip_is_zlibng() (bool)(HAVE_ZLIB_NG_NATIVE || HAVE_ZLIB_NG_COMPAT)
bool
Zip_is_zlibng()
unsigned
ZLIB_VERNUM()
CODE:
#ifdef ZLIB_VERNUM
RETVAL = ZLIB_VERNUM ;
#elif USE_ZLIB_NG
RETVAL = 0 ;
#else
/* 1.1.4 => 0x1140 */
RETVAL = (CRZ_ZLIB_VERSION[0] - '0') << 12 ;
RETVAL += (CRZ_ZLIB_VERSION[2] - '0') << 8 ;
RETVAL += (CRZ_ZLIB_VERSION[4] - '0') << 4 ;
if (strlen(CRZ_ZLIB_VERSION) > 5)
RETVAL += (CRZ_ZLIB_VERSION[6] - '0') ;
#endif
OUTPUT:
RETVAL
#ifndef AT_LEAST_ZLIB_1_2_1
# define Zip_zlibCompileFlags 0
#else
# define Zip_zlibCompileFlags CRZ_zlibCompileFlags
#endif
uLong
Zip_zlibCompileFlags()
const char*
ZLIBNG_VER_STATUS()
CODE:
#ifdef ZLIBNG_VER_STATUS
RETVAL = STRINGIFY(ZLIBNG_VER_STATUS);
#else
RETVAL = "0";
#endif
OUTPUT:
RETVAL
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
#define Zip_adler32(buf, adler) CRZ_adler32(adler, buf, (uInt)len)
uLong
Zip_adler32(buf, adler=CRZ_adlerInitial)
uLong adler = NO_INIT
STRLEN len = NO_INIT
Bytef * buf = NO_INIT
SV * sv = ST(0) ;
INIT:
/* If the buffer is a reference, dereference it */
sv = deRef(sv, "adler32") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
croak("Wide character in Compress::Raw::Zlib::adler32");
#endif
buf = (Byte*)SvPVbyte(sv, len) ;
if (items < 2)