-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lzma.xs
1493 lines (1228 loc) · 35.8 KB
/
Lzma.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: Lzma.xs
* Author : Paul Marquess, <[email protected]>
* Created : 14th March 2009
* Version : 2.000
*
* Copyright (c) 2009-2024 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.
*
*/
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "lzma.h"
#define NEED_sv_2pv_nolen
#include "ppport.h"
#if PERL_VERSION < 18
/* Proposed fix for https://github.com/Dual-Life/Devel-PPPort/issues/231 */
# 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
#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 PERL_REVISION == 5 && (PERL_VERSION >= 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
# define UTF8_AVAILABLE
#endif
typedef int DualType ;
typedef int int_undef ;
typedef unsigned long uLong;
typedef unsigned int uInt;
typedef struct di_stream {
int flags ;
#define FLAG_APPEND_OUTPUT 1
#define FLAG_CONSUME_INPUT 8
#define FLAG_LIMIT_OUTPUT 16
//bool is_tainted;
bool forZip;
void* extraAddress ;
lzma_stream stream ;
lzma_filter filters[LZMA_FILTERS_MAX + 1];
SV* sv_filters[LZMA_FILTERS_MAX];
uInt bufsize;
int last_error ;
uint64_t bytesInflated ;
uint64_t compressedBytes ;
uint64_t uncompressedBytes ;
} di_stream;
typedef struct di_filter {
lzma_filter filter;
SV* dict;
} di_filter;
typedef di_stream * deflateStream ;
typedef di_stream * Compress__Raw__Lzma ;
typedef di_stream * Compress__Raw__Lzma__Encoder ;
typedef di_stream * Compress__Raw__Lzma__Decoder ;
typedef di_filter * Lzma__Filter ;
typedef di_filter * Lzma__Filter__Lzma;
typedef di_filter * Lzma__Filter__BCJ ;
typedef di_filter * Lzma__Filter__Delta ;
typedef di_filter * Lzma__Filter__SubBlock ;
typedef di_stream * inflateStream ;
typedef lzma_options_lzma * Compress__Raw__Lzma__Options;
#define COMPRESS_CLASS "Compress::Raw::Lzma::Encoder"
#define UNCOMPRESS_CLASS "Compress::Raw::Lzma::Decoder"
#define ZMALLOC(to, typ) (((to) = (typ *)safemalloc(sizeof(typ))), \
Zero((to),1,typ))
#define setDefaultOptions(options) \
{ \
Zero((options),1,lzma_options_lzma); \
(options)->dict_size = LZMA_DICT_SIZE_DEFAULT; \
(options)->preset_dict = NULL; \
(options)->preset_dict_size = 0; \
(options)->lc = LZMA_LC_DEFAULT; \
(options)->lp = LZMA_LP_DEFAULT; \
(options)->pb = LZMA_PB_DEFAULT; \
(options)->mode = LZMA_MODE_NORMAL; \
(options)->nice_len = 64; \
(options)->mf = LZMA_MF_BT4; \
(options)->depth = 0; \
}
#if 0
#if sizeof(unsigned long) >= 8
#define IN_u64(arg) (unsigned long)SvUV(arg)
#define OUT_u64(arg, var) sv_setuv(arg, (IV)var)
#else
#define IN_u64(arg) if (SvOK($arg))
$var = ($type)SvPVbyte_nolen($arg);
else
$var = NULL ;
#define OUT_u64(arg, var) sv_setpv((SV*)arg, var);
#endif
#endif
/*** Tainting ***/
#define isTainted(sv) ( SvTAINTED(sv) || (SvROK(sv) ? SvTAINTED(SvRV(sv)) : FALSE))
#define rememberTainting(sv) STMT_START { s->is_tainted |= isTainted(sv); } STMT_END
#define setTainted(sv) STMT_START { SvTAINTED_on(sv); } STMT_END
#define taintedStack(x) ( FALSE || items >= x && isTainted(ST(x-1)) )
/* #define taintedStack(items) ( isTainted(ST(items-1)) ) */
#define taintedStack_1 ( taintedStack(1) )
#define taintedStack_2 ( taintedStack_1 || taintedStack(2) )
#define taintedStack_3 ( taintedStack_2 || taintedStack(3) )
#define taintedStack_4 ( taintedStack_3 || taintedStack(4) )
#define taintedStack_5 ( taintedStack_4 || taintedStack(5) )
#define taintedStack_6 ( taintedStack_5 || taintedStack(6) )
#define taintedStack_7 ( taintedStack_6 || taintedStack(7) )
#define taintedStack_8 ( taintedStack_7 || taintedStack(8) )
#define getTaint1 ( s->is_tainted = s->is_tainted || taintedStack_1 )
#define getTaint2 ( s->is_tainted = s->is_tainted || taintedStack_2 )
#define getTaint3 ( s->is_tainted = s->is_tainted || taintedStack_3 )
#define getTaint4 ( s->is_tainted = s->is_tainted || taintedStack_4 )
#define getTaint5 ( s->is_tainted = s->is_tainted || taintedStack_5 )
#define getTaint6 ( s->is_tainted = s->is_tainted || taintedStack_6 )
#define getTaint7 ( s->is_tainted = s->is_tainted || taintedStack_7 )
#define getTaint8 ( s->is_tainted = s->is_tainted || taintedStack_8 )
/*** End Tainting ***/
//static const char * const my_l_errmsg[] = {
static const char my_l_errmsg[][34] = {
"OK", /* LZMA_OK = 0 */
"End of stream", /* LZMA_STREAM_END = 1 */
"No integrity check", /* LZMA_NO_CHECK = 2 */
"Cannot calculate integrity check", /* LZMA_UNSUPPORTED_CHECK = 3 */
"Integrity check type available", /* LZMA_GET_CHECK = 4 */
"Cannot allocate memory", /* LZMA_MEM_ERROR = 5 */
"Memory usage limit was reached", /* LZMA_MEMLIMIT_ERROR = 6 */
"File format not recognized", /* LZMA_FORMAT_ERROR = 7 */
"Invalid or unsupported options", /* LZMA_OPTIONS_ERROR = 8 */
"Data is corrupt", /* LZMA_DATA_ERROR = 9 */
"No progress is possible", /* LZMA_BUF_ERROR = 10 */
"Programming error", /* LZMA_PROG_ERROR = 11 */
""};
#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
#if 1
#define getInnerObject(x) (*av_fetch((AV*)SvRV(x), 0, FALSE))
#else
#define getInnerObject(x) ((SV*)SvRV(sv))
#endif
static char *
#ifdef CAN_PROTOTYPE
GetErrorString(int error_no)
#else
GetErrorString(error_no)
int error_no ;
#endif
{
dTHX;
char * errstr ;
errstr = (char*) my_l_errmsg[error_no];
return errstr ;
}
#if 0
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, SV* sv, const char * message)
#else
DispStream(s, message)
di_stream * s;
const char * message;
#endif
{
dTHX;
#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 (sv) {
sv_dump(sv);
printf("\n") ;
}
if (!s) {
printf(" stream pointer is NULL\n");
}
else {
printf(" stream 0x%p\n", &(s->stream));
printf(" next_in 0x%p", s->stream.next_in);
if (s->stream.next_in){
printf(" =>");
DispHex((void*)s->stream.next_in, 4);
}
printf("\n");
printf(" next_out 0x%p", s->stream.next_out);
if (s->stream.next_out){
printf(" =>");
DispHex((void*)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(" bufsize %lu\n", (unsigned long)s->bufsize);
printf(" flags 0x%x\n", s->flags);
printf(" APPEND %s\n", EnDis(FLAG_APPEND_OUTPUT));
printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
printf(" LIMIT %s\n", EnDis(FLAG_LIMIT_OUTPUT));
printf("\n");
}
}
#endif
void* my_alloc (void* opaque, size_t items, size_t size)
{
PERL_UNUSED_VAR(opaque);
return safemalloc(items * size);
}
void my_free (void* opaque, void* ptr)
{
PERL_UNUSED_VAR(opaque);
safefree(ptr);
return;
}
static di_stream *
#ifdef CAN_PROTOTYPE
InitStream(void)
#else
InitStream()
#endif
{
dTHX;
di_stream *s ;
lzma_allocator * allocator;
ZMALLOC(s, di_stream) ;
/* lzma_memory_usage(lzma_preset_lzma, TRUE); */
ZMALLOC(allocator, lzma_allocator) ;
allocator->alloc = my_alloc;
allocator->free = my_free;
s->stream.allocator = allocator;
return s ;
}
static void
#ifdef CAN_PROTOTYPE
PostInitStream(di_stream * s, int flags, int bufsize)
#else
PostInitStream(s, flags, bufsize)
di_stream *s ;
int flags ;
int bufsize;
#endif
{
s->bufsize = bufsize;
s->last_error = LZMA_OK ;
s->flags = flags ;
}
bool
setupFilters(di_stream* s, AV* filters, const char* properties)
{
dTHX;
int i = 0;
if (properties) {
s->filters[0].id = LZMA_FILTER_LZMA1;
if (lzma_properties_decode(&s->filters[0], s->stream.allocator,
(const uint8_t*)properties, 5) != LZMA_OK)
return FALSE;
s->extraAddress = (void*)s->filters[0].options;
++i;
}
else {
AV* f = filters;
int count = av_len(f) ;
for (i = 0; i <= count; ++i)
{
SV * fptr = (SV*) * av_fetch(f, i, FALSE) ;
IV tmp = SvIV((SV*)SvRV(fptr));
di_filter* filter = INT2PTR(di_filter*, tmp);
/* Keep a reference to the filter so it doesn't get destroyed */
s->sv_filters[i] = newSVsv(fptr) ;
s->filters[i].id = filter->filter.id;
s->filters[i].options = filter->filter.options;
}
}
/* Terminate the filter list */
s->filters[i].id = LZMA_VLI_UNKNOWN ;
return TRUE;
}
void
destroyStream(di_stream * s)
{
dTHX;
if (s)
{
int i;
if (s->extraAddress)
Safefree(s->extraAddress) ;
if (s->stream.allocator)
Safefree(s->stream.allocator);
for (i = 0; i < LZMA_FILTERS_MAX; ++i)
{
if (s->sv_filters[i])
SvREFCNT_dec(s->sv_filters[i]);
}
Safefree(s) ;
}
}
static SV*
#ifdef CAN_PROTOTYPE
deRef(SV * sv, 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, 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
static lzma_filter all_filters[LZMA_FILTERS_MAX + 1];
//static lzma_options_filter lzma->filters[8];
static size_t filters_count = 0;
static size_t preset_number = 6 ;
static size_t opt_memory = 1024 * 1024 * 1000 ;
#endif
#if 0
static void
set_compression_settings(lzma_data * lzma)
{
lzma->preset_default = lzma->filters_count == 0;
size_t i;
if (lzma->preset_default) {
lzma->filters[0].id = LZMA_FILTER_LZMA1;
lzma->filters[0].options = (lzma_options_lzma *)(
lzma_preset_lzma + lzma->preset_number);
lzma->filters_count = 1;
}
// Terminate the filter options array.
lzma->filters[lzma->filters_count].id = UINT64_MAX;
// Optimize the filter chain a little by removing all
// Copy filters.
for (i = 0; lzma->filters[i].id != UINT64_MAX; ++i) {
while (lzma->filters[i].id == LZMA_FILTER_COPY) {
size_t j = i;
do {
lzma->filters[j] = lzma->filters[j + 1];
} while (lzma->filters[++j].id != UINT64_MAX);
}
}
const uint32_t memory_limit = opt_memory / (1024 * 1024) + 1;
uint32_t memory_usage = lzma_memory_usage(lzma->filters, TRUE);
// Don't go over the memory limits when the default
// setting is used.
if (preset_default) {
while (memory_usage > memory_limit) {
if (lzma->preset_number == 0) {
croak("Memory usage limit is too "
"small for any internal "
"filter preset");
exit(-1);
}
--lzma->preset_number;
lzma->filters[0].options = (lzma_options_lzma *)(
lzma_preset_lzma
+ lzma->preset_number);
memory_usage = lzma_memory_usage(lzma->filters,
TRUE);
}
} else {
if (memory_usage > memory_limit) {
croak("Memory usage limit is too small "
"for the given filter setup");
exit(-1);
}
}
// Limit the number of worked threads so that memory usage
// limit isn't exceeded.
// FIXME: Probably should use bytes instead of mebibytes for
// memory_usage and memory_limit.
if (memory_usage == 0)
memory_usage = 1;
#if 0
size_t thread_limit = memory_limit / memory_usage;
if (thread_limit == 0)
thread_limit = 1;
if (opt_threads > thread_limit)
opt_threads = thread_limit;
#endif
return;
}
#endif
lzma_ret
#ifdef CAN_PROTOTYPE
addZipProperties(di_stream* s, SV* output)
#else
addZipProperties(s, output)
di_stream* s;
SV* output ;
#endif
{
dTHX;
uint32_t size;
int cur_length = SvCUR(output) ;
lzma_ret status = lzma_properties_size(&size, &s->filters[0]);
uint8_t *props ;
if (status != LZMA_OK)
return status;
Sv_Grow(output, SvLEN(output) + size + 4) ;
props = (uint8_t*) SvPVbyte_nolen(output) + cur_length;
*props = (uint8_t)LZMA_VERSION_MAJOR ; ++ props;
*props = (uint8_t)LZMA_VERSION_MINOR ; ++ props;
*props = size ; ++ props;
*props = 0 ; ++ props;
status = lzma_properties_encode(&s->filters[0], props);
SvCUR_set(output, cur_length + size + 4);
s->forZip = FALSE ;
return status ;
}
#include "constants.h"
MODULE = Compress::Raw::Lzma PACKAGE = Compress::Raw::Lzma
REQUIRE: 1.924
PROTOTYPES: DISABLE
INCLUDE: constants.xs
BOOT:
{
PERL_UNUSED_VAR(trace);
}
MODULE = Compress::Raw::Lzma PACKAGE = Compress::Raw::Lzma PREFIX = MY_
#define MY_LZMA_VERSION() LZMA_VERSION
uint32_t
MY_LZMA_VERSION()
uint32_t
lzma_version_number()
const char *
lzma_version_string()
#define MY_LZMA_VERSION_STRING() LZMA_VERSION_STRING
const char *
MY_LZMA_VERSION_STRING()
#define MY_LZMA_FILTER_LZMA1() LZMA_FILTER_LZMA1
uint64_t
MY_LZMA_FILTER_LZMA1()
#define MY_LZMA_BACKWARD_SIZE_MAX() LZMA_BACKWARD_SIZE_MAX
uint64_t
MY_LZMA_BACKWARD_SIZE_MAX()
lzma_bool
lzma_mf_is_supported(match_finder)
lzma_match_finder match_finder
lzma_bool
lzma_mode_is_supported(mode)
lzma_mode mode
lzma_bool
lzma_check_is_supported(check)
lzma_check check
uint32_t
lzma_check_size(check)
lzma_check check
size_t
lzma_stream_buffer_bound(uncompressed_size)
size_t uncompressed_size
lzma_bool
lzma_filter_encoder_is_supported(id)
lzma_vli id
lzma_bool
lzma_filter_decoder_is_supported(id)
lzma_vli id
uint64_t
lzma_easy_encoder_memusage(preset)
uint32_t preset
uint64_t
lzma_easy_decoder_memusage(preset)
uint32_t preset
void
lzma_alone_encoder(Class, flags, bufsize, filters)
const char * Class
int flags
uLong bufsize
AV* filters
PPCODE:
{
lzma_ret err = LZMA_OK;
deflateStream s = NULL;
if ((s = InitStream() )) {
setupFilters(s, filters, NULL);
err = lzma_alone_encoder ( &(s->stream), (const lzma_options_lzma*)s->filters[0].options );
if (err != LZMA_OK) {
Safefree(s) ;
s = NULL ;
}
else {
PostInitStream(s, flags, bufsize) ;
//s->is_tainted = is_tainted;
}
}
else {
err = LZMA_MEM_ERROR ;
s = NULL;
}
{
SV* obj = sv_setref_pv(sv_newmortal(), Class, (void*)s);
//if (is_tainted)
//setTainted(obj);
XPUSHs(obj);
}
if (GIMME_V == G_ARRAY) {
SV * sv = sv_2mortal(newSViv(err)) ;
setDUALstatus(sv, err);
//if (is_tainted)
//setTainted(sv);
XPUSHs(sv) ;
}
}
void
lzma_raw_encoder(Class, flags, bufsize, filters, forZip)
const char * Class
int flags
uLong bufsize
AV* filters
bool forZip
PPCODE:
{
lzma_ret err = LZMA_OK;
deflateStream s = NULL;
if ((s = InitStream() )) {
setupFilters(s, filters, NULL);
s->forZip = forZip ;
err = lzma_raw_encoder ( &(s->stream), (const lzma_filter*)&s->filters );
if (err != LZMA_OK) {
Safefree(s) ;
s = NULL ;
}
else {
PostInitStream(s, flags, bufsize) ;
//s->is_tainted = is_tainted;
}
}
else {
err = LZMA_MEM_ERROR ;
s = NULL;
}
{
SV* obj = sv_setref_pv(sv_newmortal(), Class, (void*)s);
//if (is_tainted)
//setTainted(obj);
XPUSHs(obj);
}
if (GIMME_V == G_ARRAY) {
SV * sv = sv_2mortal(newSViv(err)) ;
setDUALstatus(sv, err);
//if (is_tainted)
//setTainted(sv);
XPUSHs(sv) ;
}
}
void
lzma_stream_encoder(Class, flags, bufsize, filters, check=LZMA_CHECK_CRC32)
const char * Class
int flags
uLong bufsize
AV* filters
lzma_check check
PPCODE:
{
lzma_ret err = LZMA_OK;
deflateStream s = NULL;
if ((s = InitStream() )) {
setupFilters(s, filters, NULL);
err = lzma_stream_encoder ( &(s->stream), (const lzma_filter*)&s->filters, check );
if (err != LZMA_OK) {
Safefree(s) ;
s = NULL ;
}
else {
PostInitStream(s, flags, bufsize) ;
//s->is_tainted = is_tainted;
}
}
else {
err = LZMA_MEM_ERROR ;
s = NULL;
}
{
SV* obj = sv_setref_pv(sv_newmortal(), Class, (void*)s);
//if (is_tainted)
//setTainted(obj);
XPUSHs(obj);
}
if (GIMME_V == G_ARRAY) {
SV * sv = sv_2mortal(newSViv(err)) ;
setDUALstatus(sv, err);
//if (is_tainted)
//setTainted(sv);
XPUSHs(sv) ;
}
}
void
lzma_easy_encoder(Class, flags, bufsize, preset=LZMA_PRESET_DEFAULT, check=LZMA_CHECK_CRC32)
const char * Class
int flags
int preset
lzma_check check
uLong bufsize
PPCODE:
{
lzma_ret err = LZMA_OK;
deflateStream s = NULL;
if ((s = InitStream())) {
err = lzma_easy_encoder ( &(s->stream), preset, check);
if (err != LZMA_OK) {
Safefree(s) ;
s = NULL ;
}
else {
PostInitStream(s, flags, bufsize) ;
//s->is_tainted = is_tainted;
}
}
else {
err = LZMA_MEM_ERROR ;
s = NULL;
}
{
SV* obj = sv_setref_pv(sv_newmortal(), Class, (void*)s);
//if (is_tainted)
//setTainted(obj);
XPUSHs(obj);
}
if (GIMME_V == G_ARRAY) {
SV * sv = sv_2mortal(newSViv(err)) ;
setDUALstatus(sv, err);
//if (is_tainted)
//setTainted(sv);
XPUSHs(sv) ;
}
}
MODULE = Compress::Raw::Lzma::Encoder PACKAGE = Compress::Raw::Lzma::Encoder
void
DESTROY(s)
Compress::Raw::Lzma::Encoder s
CODE:
lzma_end(&s->stream) ;
destroyStream(s) ;
DualType
code (s, buf, output)
Compress::Raw::Lzma::Encoder s
SV * buf
SV * output
uInt cur_length = NO_INIT
uInt increment = NO_INIT
lzma_ret RETVAL = LZMA_OK;
uInt bufinc = NO_INIT
//bool is_tainted = getTaint3;
STRLEN origlen = NO_INIT
CODE:
bufinc = s->bufsize;
/* If the input buffer is a reference, dereference it */
buf = deRef(buf, (char*)"code") ;
/* initialise the input buffer */
#ifdef UTF8_AVAILABLE
if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
croak("Wide character in " COMPRESS_CLASS "::code input parameter");
#endif
s->stream.next_in = (uint8_t*)SvPV_nomg(buf, origlen) ;
s->stream.avail_in = origlen;
//if (is_tainted)
//setTainted(output);
/* and retrieve the output buffer */
output = deRef_l(output, (char*)"code") ;
#ifdef UTF8_AVAILABLE
if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
croak("Wide character in " COMPRESS_CLASS "::code output parameter");
#endif
if((s->flags & FLAG_APPEND_OUTPUT) == FLAG_APPEND_OUTPUT) {
SvOOK_off(output);
} else {
SvCUR_set(output, 0);
}
if (s->forZip)
addZipProperties(s, output) ;
cur_length = SvCUR(output) ;
s->stream.next_out = (uint8_t*) SvPVX(output) + cur_length;
increment = SvLEN(output) - cur_length;
s->stream.avail_out = increment;
while (s->stream.avail_in != 0) {
if (s->stream.avail_out == 0) {
/* out of space in the output buffer so make it bigger */
s->stream.next_out = (uint8_t*)Sv_Grow(output, SvLEN(output) + bufinc) ;
cur_length += increment ;
s->stream.next_out += cur_length ;
increment = bufinc ;
s->stream.avail_out = increment;
bufinc *= 2 ;
}
RETVAL = lzma_code(&(s->stream), LZMA_RUN);
if (RETVAL == LZMA_STREAM_END)
break;
if (RETVAL != LZMA_OK)
break;
/* if (RETVAL == LZMA_BUF_ERROR) { */
if (s->stream.avail_out == 0)
continue ;
if (s->stream.avail_in == 0) {
RETVAL = LZMA_OK ;
break ;
}
if (RETVAL != LZMA_OK)
break;
}
s->compressedBytes += cur_length + increment - s->stream.avail_out ;
s->uncompressedBytes += origlen - s->stream.avail_in ;
s->last_error = RETVAL ;
if (RETVAL == LZMA_OK) {
SvPOK_only(output);
SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
SvSETMAGIC(output);
}
OUTPUT:
RETVAL
DualType
flush(s, output, f=LZMA_FINISH)