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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
|
/* Filename: Zlib.xs
* Author : Paul Marquess, <pmqs@cpan.org>
* Created : 22nd January 1996
* Version : 2.000
*
* Copyright (c) 1995-2010 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
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <zlib.h>
/* zlib prior to 1.06 doesn't know about z_off_t */
#ifndef z_off_t
# define z_off_t long
#endif
#if ! defined(ZLIB_VERNUM) || ZLIB_VERNUM < 0x1200
# define NEED_DUMMY_BYTE_AT_END
#endif
#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1210
# define MAGIC_APPEND
#endif
#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
# define AT_LEAST_ZLIB_1_2_2_1
#endif
#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1222
# define AT_LEAST_ZLIB_1_2_2_2
#endif
#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1223
# define AT_LEAST_ZLIB_1_2_2_3
#endif
#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1230
# define AT_LEAST_ZLIB_1_2_3
#endif
#ifdef USE_PPPORT_H
# define NEED_sv_2pvbyte
# define NEED_sv_2pv_nolen
# include "ppport.h"
#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 ;
z_stream stream;
uLong bufsize;
SV * dictionary ;
uLong dict_adler ;
int last_error ;
bool zip_mode ;
#define SETP_BYTE
#ifdef SETP_BYTE
bool deflateParams_out_valid ;
Bytef deflateParams_out_byte;
#else
#define deflateParams_BUFFER_SIZE 0x4000
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 *)safemalloc(sizeof(typ))), \
Zero(to,1,typ))
/* Figure out the Operating System */
#ifdef MSDOS
# define OS_CODE 0x00
#endif
#if defined(AMIGA) || defined(AMIGAOS)
# 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
#define adlerInitial adler32(0L, Z_NULL, 0)
#define crcInitial crc32(0L, Z_NULL, 0)
/* 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
#define TRACE_DEFAULT 0
#ifdef NO_WRITEABLE_DATA
# 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;
memcpy(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(void * ptr, int length)
#else
DispHex(ptr, length)
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, char * message)
#else
DispStream(s, message)
di_stream * s;
char * message;
#endif
{
#if 0
if (! trace)
return ;
#endif
#define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
printf("DispStream 0x%p", s) ;
if (message)
printf("- %s \n", message) ;
printf("\n") ;
if (!s) {
printf(" stream pointer is NULL\n");
}
else {
printf(" stream 0x%p\n", &(s->stream));
printf(" zalloc 0x%p\n", s->stream.zalloc);
printf(" zfree 0x%p\n", s->stream.zfree);
printf(" opaque 0x%p\n", s->stream.opaque);
if (s->stream.msg)
printf(" msg %s\n", s->stream.msg);
else
printf(" msg \n");
printf(" next_in 0x%p", s->stream.next_in);
if (s->stream.next_in){
printf(" =>");
DispHex(s->stream.next_in, 4);
}
printf("\n");
printf(" next_out 0x%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);
printf(" adler %ld\n", s->stream.adler );
printf(" bufsize %ld\n", s->bufsize);
printf(" dictionary 0x%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 0x%p\n", s->window);
#endif
printf("\n");
}
}
static di_stream *
#ifdef CAN_PROTOTYPE
InitStream(void)
#else
InitStream()
#endif
{
di_stream *s ;
ZMALLOC(s, di_stream) ;
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 = crcInitial ;
if (flags & FLAG_ADLER32)
s->adler32 = 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 = 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 ;
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)
SvCUR_set(sv, 0);
SvOOK_off(sv);
SvPOK_only(sv);
return sv ;
}
#include "constants.h"
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
REQUIRE: 1.924
PROTOTYPES: DISABLE
INCLUDE: constants.xs
BOOT:
/* Check this version of zlib is == 1 */
if (zlibVersion()[0] != '1')
croak("Compress::Raw::Zlib needs zlib version 1.x\n") ;
{
/* 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) ;
}
#define Zip_zlib_version() (const char*)zlib_version
const char*
Zip_zlib_version()
unsigned
ZLIB_VERNUM()
CODE:
#ifdef ZLIB_VERNUM
RETVAL = ZLIB_VERNUM ;
#else
/* 1.1.4 => 0x1140 */
RETVAL = (ZLIB_VERSION[0] - '0') << 12 ;
RETVAL += (ZLIB_VERSION[2] - '0') << 8 ;
RETVAL += (ZLIB_VERSION[4] - '0') << 4 ;
#endif
OUTPUT:
RETVAL
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
#define Zip_adler32(buf, adler) adler32(adler, buf, (uInt)len)
uLong
Zip_adler32(buf, adler=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)
adler = adlerInitial;
else if (SvOK(ST(1)))
adler = SvUV(ST(1)) ;
else
adler = adlerInitial;
OUTPUT:
RETVAL
#define Zip_crc32(buf, crc) crc32(crc, buf, (uInt)len)
uLong
Zip_crc32(buf, crc=crcInitial)
uLong crc = 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, "crc32") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
croak("Wide character in Compress::Raw::Zlib::crc32");
#endif
buf = (Byte*)SvPVbyte(sv, len) ;
if (items < 2)
crc = crcInitial;
else if (SvOK(ST(1)))
crc = SvUV(ST(1)) ;
else
crc = crcInitial;
uLong
crc32_combine(crc1, crc2, len2)
uLong crc1
uLong crc2
z_off_t len2
CODE:
#ifndef AT_LEAST_ZLIB_1_2_2_1
crc1 = crc1; crc2 = crc2 ; len2 = len2; /* Silence -Wall */
croak("crc32_combine needs zlib 1.2.3 or better");
#else
RETVAL = crc32_combine(crc1, crc2, len2);
#endif
OUTPUT:
RETVAL
uLong
adler32_combine(adler1, adler2, len2)
uLong adler1
uLong adler2
z_off_t len2
CODE:
#ifndef AT_LEAST_ZLIB_1_2_2_1
adler1 = adler1; adler2 = adler2 ; len2 = len2; /* Silence -Wall */
croak("adler32_combine needs zlib 1.2.3 or better");
#else
RETVAL = adler32_combine(adler1, adler2, len2);
#endif
OUTPUT:
RETVAL
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib
void
_deflateInit(flags,level, method, windowBits, memLevel, strategy, bufsize, dictionary)
int flags
int level
int method
int windowBits
int memLevel
int strategy
uLong bufsize
SV* dictionary
PPCODE:
int err ;
deflateStream s ;
if (trace)
warn("in _deflateInit(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%ld dictionary=%p)\n",
level, method, windowBits, memLevel, strategy, bufsize, dictionary) ;
if ((s = InitStream() )) {
s->Level = level;
s->Method = method;
s->WindowBits = windowBits;
s->MemLevel = memLevel;
s->Strategy = strategy;
err = deflateInit2(&(s->stream), level,
method, windowBits, memLevel, strategy);
/* Check if a dictionary has been specified */
if (err == Z_OK && SvCUR(dictionary)) {
#ifdef UTF8_AVAILABLE
if (DO_UTF8(dictionary) && !sv_utf8_downgrade(dictionary, 1))
croak("Wide character in Compress::Raw::Zlib::Deflate::new dicrionary parameter");
#endif
err = deflateSetDictionary(&(s->stream), (const Bytef*) SvPVbyte_nolen(dictionary),
SvCUR(dictionary)) ;
s->dict_adler = s->stream.adler ;
}
if (err != Z_OK) {
Safefree(s) ;
s = NULL ;
}
else
PostInitStream(s, flags, bufsize, windowBits) ;
}
else
err = Z_MEM_ERROR ;
{
SV* obj = sv_setref_pv(sv_newmortal(),
"Compress::Raw::Zlib::deflateStream", (void*)s);
XPUSHs(obj);
}
if (GIMME == G_ARRAY) {
SV * sv = sv_2mortal(newSViv(err)) ;
setDUALstatus(sv, err);
XPUSHs(sv) ;
}
void
_inflateInit(flags, windowBits, bufsize, dictionary)
int flags
int windowBits
uLong bufsize
SV * dictionary
ALIAS:
_inflateScanInit = 1
PPCODE:
int err = Z_OK ;
inflateStream s ;
#ifndef MAGIC_APPEND
if (ix == 1)
croak("inflateScanInit needs zlib 1.2.1 or better");
#endif
if (trace)
warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
if ((s = InitStream() )) {
s->WindowBits = windowBits;
err = inflateInit2(&(s->stream), windowBits);
if (err != Z_OK) {
Safefree(s) ;
s = NULL ;
}
else if (SvCUR(dictionary)) {
#ifdef AT_LEAST_ZLIB_1_2_2_1
/* Zlib 1.2.2.1 or better allows a dictionary with raw inflate */
if (s->WindowBits < 0) {
err = inflateSetDictionary(&(s->stream),
(const Bytef*)SvPVbyte_nolen(dictionary),
SvCUR(dictionary));
if (err != Z_OK) {
Safefree(s) ;
s = NULL ;
}
}
else
#endif
/* Dictionary specified - take a copy for use in inflate */
s->dictionary = newSVsv(dictionary) ;
}
if (s) {
PostInitStream(s, flags, bufsize, windowBits) ;
#ifdef MAGIC_APPEND
if (ix == 1)
{
s->window = (unsigned char *)safemalloc(WINDOW_SIZE);
}
#endif
}
}
else
err = Z_MEM_ERROR ;
{
SV* obj = sv_setref_pv(sv_newmortal(),
ix == 1
? "Compress::Raw::Zlib::inflateScanStream"
: "Compress::Raw::Zlib::inflateStream",
(void*)s);
XPUSHs(obj);
}
if (GIMME == G_ARRAY) {
SV * sv = sv_2mortal(newSViv(err)) ;
setDUALstatus(sv, err);
XPUSHs(sv) ;
}
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::deflateStream
void
DispStream(s, message=NULL)
Compress::Raw::Zlib::deflateStream s
char * message
DualType
deflateReset(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = deflateReset(&(s->stream)) ;
if (RETVAL == Z_OK) {
PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
}
OUTPUT:
RETVAL
DualType
deflate (s, buf, output)
Compress::Raw::Zlib::deflateStream s
SV * buf
SV * output
uInt cur_length = NO_INIT
uInt increment = NO_INIT
uInt prefix = NO_INIT
int RETVAL = 0;
uLong bufinc = NO_INIT
CODE:
bufinc = s->bufsize;
/* If the input buffer is a reference, dereference it */
buf = deRef(buf, "deflate") ;
/* initialise the input buffer */
#ifdef UTF8_AVAILABLE
if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
croak("Wide character in Compress::Raw::Zlib::Deflate::deflate input parameter");
#endif
s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
s->stream.avail_in = SvCUR(buf) ;
if (s->flags & FLAG_CRC32)
s->crc32 = crc32(s->crc32, s->stream.next_in, s->stream.avail_in) ;
if (s->flags & FLAG_ADLER32)
s->adler32 = adler32(s->adler32, s->stream.next_in, s->stream.avail_in) ;
/* and retrieve the output buffer */
output = deRef_l(output, "deflate") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
croak("Wide character in Compress::Raw::Zlib::Deflate::deflate output parameter");
#endif
if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
SvCUR_set(output, 0);
/* sv_setpvn(output, "", 0); */
}
prefix = cur_length = SvCUR(output) ;
s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
increment = SvLEN(output) - cur_length;
s->stream.avail_out = increment;
#ifdef SETP_BYTE
/* Check for saved output from deflateParams */
if (s->deflateParams_out_valid) {
*(s->stream.next_out) = s->deflateParams_out_byte;
++ s->stream.next_out;
-- s->stream.avail_out ;
s->deflateParams_out_valid = FALSE;
}
#else
/* Check for saved output from deflateParams */
if (s->deflateParams_out_length) {
uLong plen = s->deflateParams_out_length ;
/* printf("Copy %d bytes saved data\n", plen);*/
if (s->stream.avail_out < plen) {
/*printf("GROW from %d to %d\n", s->stream.avail_out,
SvLEN(output) + plen - s->stream.avail_out); */
Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
}
Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
cur_length = cur_length + plen;
SvCUR_set(output, cur_length);
s->stream.next_out += plen ;
s->stream.avail_out = SvLEN(output) - cur_length ;
increment = s->stream.avail_out;
s->deflateParams_out_length = 0;
}
#endif
while (s->stream.avail_in != 0) {
if (s->stream.avail_out == 0) {
/* out of space in the output buffer so make it bigger */
Sv_Grow(output, SvLEN(output) + bufinc) ;
cur_length += increment ;
s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
increment = bufinc ;
s->stream.avail_out = increment;
bufinc *= 2 ;
}
RETVAL = deflate(&(s->stream), Z_NO_FLUSH);
if (RETVAL != Z_OK)
break;
}
s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
s->uncompressedBytes += SvCUR(buf) - s->stream.avail_in ;
s->last_error = RETVAL ;
if (RETVAL == Z_OK) {
SvPOK_only(output);
SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
SvSETMAGIC(output);
}
OUTPUT:
RETVAL
void
DESTROY(s)
Compress::Raw::Zlib::deflateStream s
CODE:
deflateEnd(&s->stream) ;
if (s->dictionary)
SvREFCNT_dec(s->dictionary) ;
#ifndef SETP_BYTE
if (s->deflateParams_out_buffer)
Safefree(s->deflateParams_out_buffer);
#endif
Safefree(s) ;
DualType
flush(s, output, f=Z_FINISH)
Compress::Raw::Zlib::deflateStream s
SV * output
int f
uInt cur_length = NO_INIT
uInt increment = NO_INIT
uInt prefix = NO_INIT
uLong bufinc = NO_INIT
CODE:
bufinc = s->bufsize;
s->stream.avail_in = 0; /* should be zero already anyway */
/* retrieve the output buffer */
output = deRef_l(output, "flush") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
croak("Wide character in Compress::Raw::Zlib::Deflate::flush input parameter");
#endif
if(! s->flags & FLAG_APPEND) {
SvCUR_set(output, 0);
/* sv_setpvn(output, "", 0); */
}
prefix = cur_length = SvCUR(output) ;
s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
increment = SvLEN(output) - cur_length;
s->stream.avail_out = increment;
#ifdef SETP_BYTE
/* Check for saved output from deflateParams */
if (s->deflateParams_out_valid) {
*(s->stream.next_out) = s->deflateParams_out_byte;
++ s->stream.next_out;
-- s->stream.avail_out ;
s->deflateParams_out_valid = FALSE;
}
#else
/* Check for saved output from deflateParams */
if (s->deflateParams_out_length) {
uLong plen = s->deflateParams_out_length ;
/* printf("Copy %d bytes saved data\n", plen); */
if (s->stream.avail_out < plen) {
/* printf("GROW from %d to %d\n", s->stream.avail_out,
SvLEN(output) + plen - s->stream.avail_out); */
Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
}
Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
cur_length = cur_length + plen;
SvCUR_set(output, cur_length);
s->stream.next_out += plen ;
s->stream.avail_out = SvLEN(output) - cur_length ;
increment = s->stream.avail_out;
s->deflateParams_out_length = 0;
}
#endif
for (;;) {
if (s->stream.avail_out == 0) {
/* consumed all the available output, so extend it */
Sv_Grow(output, SvLEN(output) + bufinc) ;
cur_length += increment ;
s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
increment = bufinc ;
s->stream.avail_out = increment;
bufinc *= 2 ;
}
RETVAL = deflate(&(s->stream), f);
/* deflate has finished flushing only when it hasn't used up
* all the available space in the output buffer:
*/
if (s->stream.avail_out != 0 || RETVAL != Z_OK )
break;
}
RETVAL = (RETVAL == Z_STREAM_END ? Z_OK : RETVAL) ;
s->last_error = RETVAL ;
s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
if (RETVAL == Z_OK) {
SvPOK_only(output);
SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
SvSETMAGIC(output);
}
OUTPUT:
RETVAL
DualType
_deflateParams(s, flags, level, strategy, bufsize)
Compress::Raw::Zlib::deflateStream s
int flags
int level
int strategy
uLong bufsize
CODE:
/* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize);
printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
if (flags & 1)
s->Level = level ;
if (flags & 2)
s->Strategy = strategy ;
if (flags & 4) {
s->bufsize = bufsize;
}
/* printf("After -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize);*/
#ifdef SETP_BYTE
s->stream.avail_in = 0;
s->stream.next_out = &(s->deflateParams_out_byte) ;
s->stream.avail_out = 1;
RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
s->deflateParams_out_valid =
(RETVAL == Z_OK && s->stream.avail_out == 0) ;
/* printf("RETVAL %d, avail out %d, byte %c\n", RETVAL, s->stream.avail_out, s->deflateParams_out_byte); */
#else
/* printf("Level %d Strategy %d, Prev Len %d\n",
s->Level, s->Strategy, s->deflateParams_out_length); */
s->stream.avail_in = 0;
if (s->deflateParams_out_buffer == NULL)
s->deflateParams_out_buffer = safemalloc(deflateParams_BUFFER_SIZE);
s->stream.next_out = s->deflateParams_out_buffer ;
s->stream.avail_out = deflateParams_BUFFER_SIZE;
RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
s->deflateParams_out_length = deflateParams_BUFFER_SIZE - s->stream.avail_out;
/* printf("RETVAL %d, length out %d, avail %d\n",
RETVAL, s->deflateParams_out_length, s->stream.avail_out ); */
#endif
OUTPUT:
RETVAL
int
get_Level(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->Level ;
OUTPUT:
RETVAL
int
get_Strategy(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->Strategy ;
OUTPUT:
RETVAL
uLong
get_Bufsize(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->bufsize ;
OUTPUT:
RETVAL
int
status(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->last_error ;
OUTPUT:
RETVAL
uLong
crc32(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->crc32 ;
OUTPUT:
RETVAL
uLong
dict_adler(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->dict_adler ;
OUTPUT:
RETVAL
uLong
adler32(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->adler32 ;
OUTPUT:
RETVAL
uLong
compressedBytes(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->compressedBytes;
OUTPUT:
RETVAL
uLong
uncompressedBytes(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->uncompressedBytes;
OUTPUT:
RETVAL
uLong
total_in(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->stream.total_in ;
OUTPUT:
RETVAL
uLong
total_out(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->stream.total_out ;
OUTPUT:
RETVAL
char*
msg(s)
Compress::Raw::Zlib::deflateStream s
CODE:
RETVAL = s->stream.msg;
OUTPUT:
RETVAL
int
deflateTune(s, good_length, max_lazy, nice_length, max_chain)
Compress::Raw::Zlib::deflateStream s
int good_length
int max_lazy
int nice_length
int max_chain
CODE:
#ifndef AT_LEAST_ZLIB_1_2_2_3
good_length = good_length; max_lazy = max_lazy ; /* Silence -Wall */
nice_length = nice_length; max_chain = max_chain; /* Silence -Wall */
croak("deflateTune needs zlib 1.2.2.3 or better");
#else
RETVAL = deflateTune(&(s->stream), good_length, max_lazy, nice_length, max_chain);
#endif
OUTPUT:
RETVAL
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateStream
void
DispStream(s, message=NULL)
Compress::Raw::Zlib::inflateStream s
char * message
DualType
inflateReset(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = inflateReset(&(s->stream)) ;
if (RETVAL == Z_OK) {
PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
}
OUTPUT:
RETVAL
DualType
inflate (s, buf, output, eof=FALSE)
Compress::Raw::Zlib::inflateStream s
SV * buf
SV * output
bool eof
uInt cur_length = 0;
uInt prefix_length = 0;
int increment = 0;
STRLEN stmp = NO_INIT
uLong bufinc = NO_INIT
PREINIT:
#ifdef UTF8_AVAILABLE
bool out_utf8 = FALSE;
#endif
CODE:
bufinc = s->bufsize;
/* If the buffer is a reference, dereference it */
buf = deRef(buf, "inflate") ;
if (s->flags & FLAG_CONSUME_INPUT && SvREADONLY(buf))
croak("Compress::Raw::Zlib::Inflate::inflate input parameter cannot be read-only when ConsumeInput is specified");
#ifdef UTF8_AVAILABLE
if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
croak("Wide character in Compress::Raw::Zlib::Inflate::inflate input parameter");
#endif
/* initialise the input buffer */
s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
s->stream.avail_in = SvCUR(buf) ;
/* and retrieve the output buffer */
output = deRef_l(output, "inflate") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(output))
out_utf8 = TRUE ;
if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
croak("Wide character in Compress::Raw::Zlib::Inflate::inflate output parameter");
#endif
if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
SvCUR_set(output, 0);
}
/* Assume no output buffer - the code below will update if there is any available */
s->stream.avail_out = 0;
if (SvLEN(output)) {
prefix_length = cur_length = SvCUR(output) ;
if (s->flags & FLAG_LIMIT_OUTPUT && SvLEN(output) - cur_length - 1 < bufinc)
{
Sv_Grow(output, bufinc + cur_length + 1) ;
}
/* Only setup the stream output pointers if there is spare
capacity in the outout SV
*/
if (SvLEN(output) > cur_length + 1)
{
s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
increment = SvLEN(output) - cur_length - 1;
s->stream.avail_out = increment;
}
}
s->bytesInflated = 0;
RETVAL = Z_OK;
while (RETVAL == Z_OK) {
if (s->stream.avail_out == 0) {
/* out of space in the output buffer so make it bigger */
Sv_Grow(output, SvLEN(output) + bufinc +1) ;
cur_length += increment ;
s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
increment = bufinc ;
s->stream.avail_out = increment;
bufinc *= 2 ;
}
/* printf("INFLATE Availl In %d, Out %d\n", s->stream.avail_in,
s->stream.avail_out);
DispStream(s, "BEFORE");
Perl_sv_dump(output); */
RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
/* printf("INFLATE returned %d %s, avail in %d, out %d\n", RETVAL,
GetErrorString(RETVAL), s->stream.avail_in, s->stream.avail_out); */
if (RETVAL == Z_NEED_DICT && s->dictionary) {
s->dict_adler = s->stream.adler ;
RETVAL = inflateSetDictionary(&(s->stream),
(const Bytef*)SvPVbyte_nolen(s->dictionary),
SvCUR(s->dictionary));
if (RETVAL == Z_OK)
continue;
}
if (s->flags & FLAG_LIMIT_OUTPUT &&
(RETVAL == Z_OK || RETVAL == Z_BUF_ERROR ))
break;
if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
RETVAL == Z_DATA_ERROR || RETVAL == Z_STREAM_END )
break ;
if (RETVAL == Z_BUF_ERROR) {
if (s->stream.avail_out == 0)
continue ;
if (s->stream.avail_in == 0) {
RETVAL = Z_OK ;
break ;
}
}
}
#ifdef NEED_DUMMY_BYTE_AT_END
if (eof && RETVAL == Z_OK && s->flags & FLAG_LIMIT_OUTPUT == 0) {
Bytef* nextIn = s->stream.next_in;
uInt availIn = s->stream.avail_in;
s->stream.next_in = (Bytef*) " ";
s->stream.avail_in = 1;
if (s->stream.avail_out == 0) {
/* out of space in the output buffer so make it bigger */
Sv_Grow(output, SvLEN(output) + bufinc) ;
cur_length += increment ;
s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
increment = bufinc ;
s->stream.avail_out = increment;
bufinc *= 2 ;
}
RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
s->stream.next_in = nextIn ;
s->stream.avail_in = availIn ;
}
#endif
s->last_error = RETVAL ;
if (RETVAL == Z_OK || RETVAL == Z_STREAM_END || RETVAL == Z_BUF_ERROR || RETVAL == Z_DATA_ERROR) {
unsigned in ;
s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
s->uncompressedBytes += s->bytesInflated ;
s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
SvPOK_only(output);
SvCUR_set(output, prefix_length + s->bytesInflated) ;
*SvEND(output) = '\0';
#ifdef UTF8_AVAILABLE
if (out_utf8)
sv_utf8_upgrade(output);
#endif
SvSETMAGIC(output);
if (s->flags & FLAG_CRC32 )
s->crc32 = crc32(s->crc32,
(const Bytef*)SvPVbyte_nolen(output)+prefix_length,
SvCUR(output)-prefix_length) ;
if (s->flags & FLAG_ADLER32)
s->adler32 = adler32(s->adler32,
(const Bytef*)SvPVbyte_nolen(output)+prefix_length,
SvCUR(output)-prefix_length) ;
/* fix the input buffer */
if (s->flags & FLAG_CONSUME_INPUT || s->flags & FLAG_LIMIT_OUTPUT) {
in = s->stream.avail_in ;
SvCUR_set(buf, in) ;
if (in)
Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
*SvEND(buf) = '\0';
SvSETMAGIC(buf);
}
}
OUTPUT:
RETVAL
uLong
inflateCount(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->bytesInflated;
OUTPUT:
RETVAL
uLong
compressedBytes(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->compressedBytes;
OUTPUT:
RETVAL
uLong
uncompressedBytes(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->uncompressedBytes;
OUTPUT:
RETVAL
DualType
inflateSync (s, buf)
Compress::Raw::Zlib::inflateStream s
SV * buf
CODE:
/* If the buffer is a reference, dereference it */
buf = deRef(buf, "inflateSync") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
croak("Wide character in Compress::Raw::Zlib::Inflate::inflateSync");
#endif
/* initialise the input buffer */
s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
s->stream.avail_in = SvCUR(buf) ;
/* inflateSync doesn't create any output */
s->stream.next_out = (Bytef*) NULL;
s->stream.avail_out = 0;
RETVAL = inflateSync(&(s->stream));
s->last_error = RETVAL ;
/* fix the input buffer */
{
unsigned in = s->stream.avail_in ;
SvCUR_set(buf, in) ;
if (in)
Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
*SvEND(buf) = '\0';
SvSETMAGIC(buf);
}
OUTPUT:
RETVAL
void
DESTROY(s)
Compress::Raw::Zlib::inflateStream s
CODE:
inflateEnd(&s->stream) ;
if (s->dictionary)
SvREFCNT_dec(s->dictionary) ;
#ifndef SETP_BYTE
if (s->deflateParams_out_buffer)
Safefree(s->deflateParams_out_buffer);
#endif
#ifdef MAGIC_APPEND
if (s->window)
Safefree(s->window);
#endif
Safefree(s) ;
uLong
status(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->last_error ;
OUTPUT:
RETVAL
uLong
crc32(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->crc32 ;
OUTPUT:
RETVAL
uLong
dict_adler(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->dict_adler ;
OUTPUT:
RETVAL
uLong
total_in(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->stream.total_in ;
OUTPUT:
RETVAL
uLong
adler32(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->adler32 ;
OUTPUT:
RETVAL
uLong
total_out(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->stream.total_out ;
OUTPUT:
RETVAL
char*
msg(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->stream.msg;
OUTPUT:
RETVAL
uLong
get_Bufsize(s)
Compress::Raw::Zlib::inflateStream s
CODE:
RETVAL = s->bufsize ;
OUTPUT:
RETVAL
bool
set_Append(s, mode)
Compress::Raw::Zlib::inflateStream s
bool mode
CODE:
RETVAL = ((s->flags & FLAG_APPEND) == FLAG_APPEND);
if (mode)
s->flags |= FLAG_APPEND ;
else
s->flags &= ~FLAG_APPEND ;
OUTPUT:
RETVAL
MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateScanStream
void
DESTROY(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
inflateEnd(&s->stream) ;
if (s->dictionary)
SvREFCNT_dec(s->dictionary) ;
#ifndef SETP_BYTE
if (s->deflateParams_out_buffer)
Safefree(s->deflateParams_out_buffer);
#endif
#ifdef MAGIC_APPEND
if (s->window)
Safefree(s->window);
#endif
Safefree(s) ;
void
DispStream(s, message=NULL)
Compress::Raw::Zlib::inflateScanStream s
char * message
DualType
inflateReset(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
RETVAL = inflateReset(&(s->stream)) ;
if (RETVAL == Z_OK) {
PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
}
OUTPUT:
RETVAL
DualType
scan(s, buf, out=NULL, eof=FALSE)
Compress::Raw::Zlib::inflateScanStream s
SV * buf
SV * out
bool eof
bool eof_mode = FALSE;
int start_len = NO_INIT
STRLEN stmp = NO_INIT
CODE:
/* If the input buffer is a reference, dereference it */
#ifndef MAGIC_APPEND
buf = buf;
croak("scan needs zlib 1.2.1 or better");
#else
buf = deRef(buf, "inflateScan") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
croak("Wide character in Compress::Raw::Zlib::InflateScan::scan input parameter");
#endif
/* initialise the input buffer */
s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
s->stream.avail_in = SvCUR(buf) ;
start_len = s->stream.avail_in ;
s->bytesInflated = 0 ;
do
{
if (s->stream.avail_in == 0) {
RETVAL = Z_OK ;
break ;
}
/* set up output to next available section of sliding window */
s->stream.avail_out = WINDOW_SIZE - s->window_have;
s->stream.next_out = s->window + s->window_have;
/* DispStream(s, "before inflate\n"); */
/* inflate and check for errors */
RETVAL = inflate(&(s->stream), Z_BLOCK);
if (start_len > 1 && ! eof_mode)
s->window_lastByte = *(s->stream.next_in - 1 ) ;
if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
RETVAL == Z_DATA_ERROR )
break ;
if (s->flags & FLAG_CRC32 )
s->crc32 = crc32(s->crc32, s->window + s->window_have,
WINDOW_SIZE - s->window_have - s->stream.avail_out);
if (s->flags & FLAG_ADLER32)
s->adler32 = adler32(s->adler32, s->window + s->window_have,
WINDOW_SIZE - s->window_have - s->stream.avail_out);
s->uncompressedBytes =
s->bytesInflated += WINDOW_SIZE - s->window_have - s->stream.avail_out;
if (s->stream.avail_out)
s->window_have = WINDOW_SIZE - s->stream.avail_out;
else {
s->window_have = 0;
s->window_full = 1;
}
/* process end of block */
if (s->stream.data_type & 128) {
if (s->stream.data_type & 64) {
s->window_left = s->stream.data_type & 0x1f;
}
else {
s->window_lastbit = s->stream.data_type & 0x1f;
s->lastBlockOffset = s->stream.total_in;
}
}
} while (RETVAL != Z_STREAM_END);
s->last_error = RETVAL ;
s->window_lastoff = s->stream.total_in ;
s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
if (RETVAL == Z_STREAM_END)
{
s->matchedEndBlock = 1 ;
/* save the location of the end of the compressed data */
s->window_end = SvCUR(buf) - s->stream.avail_in - 1 ;
s->window_endOffset = s->stream.total_in ;
if (s->window_left)
{
-- s->window_endOffset ;
}
/* if window wrapped, build dictionary from window by rotating */
if (s->window_full) {
rotate(s->window, WINDOW_SIZE, s->window_have);
s->window_have = WINDOW_SIZE;
}
/* if (s->flags & FLAG_CONSUME_INPUT) { */
if (1) {
unsigned in = s->stream.avail_in ;
SvCUR_set(buf, in) ;
if (in)
Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
*SvEND(buf) = '\0';
SvSETMAGIC(buf);
}
}
#endif
OUTPUT:
RETVAL
uLong
getEndOffset(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
#ifndef MAGIC_APPEND
croak("getEndOffset needs zlib 1.2.1 or better");
#else
RETVAL = s->window_endOffset;
#endif
OUTPUT:
RETVAL
uLong
inflateCount(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
#ifndef MAGIC_APPEND
croak("inflateCount needs zlib 1.2.1 or better");
#else
RETVAL = s->bytesInflated;
#endif
OUTPUT:
RETVAL
uLong
compressedBytes(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
RETVAL = s->compressedBytes;
OUTPUT:
RETVAL
uLong
uncompressedBytes(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
RETVAL = s->uncompressedBytes;
OUTPUT:
RETVAL
uLong
getLastBlockOffset(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
#ifndef MAGIC_APPEND
croak("getLastBlockOffset needs zlib 1.2.1 or better");
#else
RETVAL = s->lastBlockOffset - (s->window_lastbit != 0);
#endif
OUTPUT:
RETVAL
uLong
getLastBufferOffset(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
#ifndef MAGIC_APPEND
croak("getLastBufferOffset needs zlib 1.2.1 or better");
#else
RETVAL = s->window_lastoff;
#endif
OUTPUT:
RETVAL
void
resetLastBlockByte(s, byte)
Compress::Raw::Zlib::inflateScanStream s
unsigned char* byte
CODE:
#ifndef MAGIC_APPEND
croak("resetLastBlockByte needs zlib 1.2.1 or better");
#else
if (byte != NULL)
*byte = *byte ^ (1 << ((8 - s->window_lastbit) & 7));
#endif
void
_createDeflateStream(inf_s, flags,level, method, windowBits, memLevel, strategy, bufsize)
Compress::Raw::Zlib::inflateScanStream inf_s
int flags
int level
int method
int windowBits
int memLevel
int strategy
uLong bufsize
PPCODE:
{
#ifndef MAGIC_APPEND
flags = flags;
level = level ;
method = method;
windowBits = windowBits;
memLevel = memLevel;
strategy = strategy;
bufsize= bufsize;
croak("_createDeflateStream needs zlib 1.2.1 or better");
#else
int err ;
deflateStream s ;
if (trace)
warn("in _createDeflateStream(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%lu\n",
level, method, windowBits, memLevel, strategy, bufsize) ;
if ((s = InitStream() )) {
s->Level = level;
s->Method = method;
s->WindowBits = windowBits;
s->MemLevel = memLevel;
s->Strategy = strategy;
err = deflateInit2(&(s->stream), level,
method, windowBits, memLevel, strategy);
if (err == Z_OK) {
err = deflateSetDictionary(&(s->stream), inf_s->window, inf_s->window_have);
s->dict_adler = s->stream.adler ;
}
if (err != Z_OK) {
Safefree(s) ;
s = NULL ;
}
else {
PostInitStream(s, flags, bufsize, windowBits) ;
s->crc32 = inf_s->crc32;
s->adler32 = inf_s->adler32;
s->stream.adler = inf_s->stream.adler ;
/* s->stream.total_out = inf_s->bytesInflated ; */
s->stream.total_in = inf_s->stream.total_out ;
if (inf_s->window_left) {
/* printf("** window_left %d, window_lastByte %d\n", inf_s->window_left, inf_s->window_lastByte); */
deflatePrime(&(s->stream), 8 - inf_s->window_left, inf_s->window_lastByte);
}
}
}
else
err = Z_MEM_ERROR ;
XPUSHs(sv_setref_pv(sv_newmortal(),
"Compress::Raw::Zlib::deflateStream", (void*)s));
if (GIMME == G_ARRAY) {
SV * sv = sv_2mortal(newSViv(err)) ;
setDUALstatus(sv, err);
XPUSHs(sv) ;
}
#endif
}
DualType
status(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
RETVAL = s->last_error ;
OUTPUT:
RETVAL
uLong
crc32(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
RETVAL = s->crc32 ;
OUTPUT:
RETVAL
uLong
adler32(s)
Compress::Raw::Zlib::inflateScanStream s
CODE:
RETVAL = s->adler32 ;
OUTPUT:
RETVAL
|