]>
Commit | Line | Data |
---|---|---|
1 | #define PERL_NO_GET_CONTEXT | |
2 | #include "EXTERN.h" | |
3 | #include "perl.h" | |
4 | #include "XSUB.h" | |
5 | ||
6 | #include "ppport.h" | |
7 | ||
8 | #include <dec/decode.h> | |
9 | #include <enc/encode.h> | |
10 | #include <common/dictionary.h> | |
11 | ||
12 | #define BUFFER_SIZE 1048576 | |
13 | ||
14 | MODULE = IO::Compress::Brotli PACKAGE = IO::Uncompress::Brotli | |
15 | PROTOTYPES: ENABLE | |
16 | ||
17 | SV* unbro(buffer) | |
18 | SV* buffer | |
19 | PREINIT: | |
20 | size_t decoded_size; | |
21 | STRLEN encoded_size; | |
22 | uint8_t *encoded_buffer, *decoded_buffer; | |
23 | CODE: | |
24 | encoded_buffer = (uint8_t*) SvPV(buffer, encoded_size); | |
25 | if(!BrotliDecompressedSize(encoded_size, encoded_buffer, &decoded_size)){ | |
26 | croak("Error in BrotliDecompressedSize"); | |
27 | } | |
28 | Newx(decoded_buffer, decoded_size+1, uint8_t); | |
29 | decoded_buffer[decoded_size]=0; | |
30 | if(!BrotliDecoderDecompress(encoded_size, encoded_buffer, &decoded_size, decoded_buffer)){ | |
31 | croak("Error in BrotliDecoderDecompress"); | |
32 | } | |
33 | RETVAL = newSV(0); | |
34 | sv_usepvn_flags(RETVAL, decoded_buffer, decoded_size, SV_HAS_TRAILING_NUL); | |
35 | OUTPUT: | |
36 | RETVAL | |
37 | ||
38 | SV* BrotliDecoderCreateInstance() | |
39 | CODE: | |
40 | RETVAL = newSViv((IV)BrotliDecoderCreateInstance(NULL, NULL, NULL)); | |
41 | OUTPUT: | |
42 | RETVAL | |
43 | ||
44 | void BrotliDecoderDestroyInstance(state) | |
45 | SV* state | |
46 | CODE: | |
47 | BrotliDecoderDestroyInstance((BrotliDecoderState*)SvIV(state)); | |
48 | ||
49 | SV* BrotliDecoderDecompressStream(state, in) | |
50 | SV* state | |
51 | SV* in | |
52 | PREINIT: | |
53 | uint8_t *next_in, *next_out, *buffer; | |
54 | size_t available_in, available_out; | |
55 | BrotliDecoderResult result; | |
56 | CODE: | |
57 | next_in = (uint8_t*) SvPV(in, available_in); | |
58 | Newx(buffer, BUFFER_SIZE, uint8_t); | |
59 | RETVAL = newSVpv("", 0); | |
60 | result = BROTLI_RESULT_NEEDS_MORE_OUTPUT; | |
61 | while(result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) { | |
62 | next_out = buffer; | |
63 | available_out=BUFFER_SIZE; | |
64 | result = BrotliDecoderDecompressStream( (BrotliDecoderState*) SvIV(state), | |
65 | &available_in, | |
66 | (const uint8_t**) &next_in, | |
67 | &available_out, | |
68 | &next_out, | |
69 | NULL ); | |
70 | if(!result){ | |
71 | Safefree(buffer); | |
72 | croak("Error in BrotliDecoderDecompressStream"); | |
73 | } | |
74 | sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out); | |
75 | } | |
76 | Safefree(buffer); | |
77 | OUTPUT: | |
78 | RETVAL | |
79 | ||
80 | void BrotliDecoderSetCustomDictionary(state, dict) | |
81 | SV* state | |
82 | SV* dict | |
83 | PREINIT: | |
84 | size_t size; | |
85 | uint8_t *data; | |
86 | CODE: | |
87 | data = SvPV(dict, size); | |
88 | BrotliDecoderSetCustomDictionary((BrotliDecoderState*) SvIV(state), size, data); | |
89 | ||
90 | ||
91 | MODULE = IO::Compress::Brotli PACKAGE = IO::Compress::Brotli | |
92 | PROTOTYPES: ENABLE | |
93 | ||
94 | SV* bro(buffer, quality=BROTLI_DEFAULT_QUALITY, lgwin=BROTLI_DEFAULT_WINDOW) | |
95 | SV* buffer | |
96 | U32 quality | |
97 | U32 lgwin | |
98 | PREINIT: | |
99 | size_t encoded_size; | |
100 | STRLEN decoded_size; | |
101 | uint8_t *encoded_buffer, *decoded_buffer; | |
102 | BROTLI_BOOL result; | |
103 | CODE: | |
104 | if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) { | |
105 | croak("Invalid quality value"); | |
106 | } | |
107 | decoded_buffer = (uint8_t*) SvPV(buffer, decoded_size); | |
108 | encoded_size = BrotliEncoderMaxCompressedSize(decoded_size); | |
109 | if(!encoded_size){ | |
110 | croak("Compressed size overflow"); | |
111 | } | |
112 | Newx(encoded_buffer, encoded_size+1, uint8_t); | |
113 | result = BrotliEncoderCompress( quality, | |
114 | lgwin, | |
115 | BROTLI_DEFAULT_MODE, | |
116 | decoded_size, | |
117 | decoded_buffer, | |
118 | &encoded_size, | |
119 | encoded_buffer ); | |
120 | if(!result){ | |
121 | croak("Error in BrotliEncoderCompress"); | |
122 | } | |
123 | encoded_buffer[encoded_size]=0; | |
124 | RETVAL = newSV(0); | |
125 | sv_usepvn_flags(RETVAL, encoded_buffer, encoded_size, SV_SMAGIC | SV_HAS_TRAILING_NUL); | |
126 | OUTPUT: | |
127 | RETVAL | |
128 | ||
129 | SV* BrotliEncoderCreateInstance() | |
130 | CODE: | |
131 | RETVAL = newSViv((IV)BrotliEncoderCreateInstance(NULL, NULL, NULL)); | |
132 | OUTPUT: | |
133 | RETVAL | |
134 | ||
135 | SV* BrotliEncoderSetWindow(state, window) | |
136 | SV* state | |
137 | U32 window | |
138 | CODE: | |
139 | if( BrotliEncoderSetParameter((BrotliEncoderState*) SvIV(state), BROTLI_PARAM_LGWIN, window) ) | |
140 | RETVAL = newSVuv(1); | |
141 | else | |
142 | RETVAL = newSVuv(0); | |
143 | OUTPUT: | |
144 | RETVAL | |
145 | ||
146 | SV* BrotliEncoderSetQuality(state, quality) | |
147 | SV* state | |
148 | U32 quality | |
149 | CODE: | |
150 | if( quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY ) { | |
151 | croak("Invalid quality value"); | |
152 | } | |
153 | if( BrotliEncoderSetParameter((BrotliEncoderState*) SvIV(state), BROTLI_PARAM_QUALITY, quality) ) | |
154 | RETVAL = newSVuv(1); | |
155 | else | |
156 | RETVAL = newSVuv(0); | |
157 | OUTPUT: | |
158 | RETVAL | |
159 | ||
160 | SV* BrotliEncoderSetMode(state, mode) | |
161 | SV* state | |
162 | U32 mode | |
163 | CODE: | |
164 | if( BrotliEncoderSetParameter((BrotliEncoderState*) SvIV(state), BROTLI_PARAM_MODE, mode) ) | |
165 | RETVAL = newSVuv(1); | |
166 | else | |
167 | RETVAL = newSVuv(0); | |
168 | OUTPUT: | |
169 | RETVAL | |
170 | ||
171 | SV* BrotliEncoderCompressStream(state, in, op) | |
172 | SV* state | |
173 | SV* in | |
174 | U8 op | |
175 | PREINIT: | |
176 | uint8_t *next_in, *next_out, *buffer; | |
177 | size_t available_in, available_out; | |
178 | BROTLI_BOOL result; | |
179 | CODE: | |
180 | next_in = (uint8_t*) SvPV(in, available_in); | |
181 | Newx(buffer, BUFFER_SIZE, uint8_t); | |
182 | RETVAL = newSVpv("", 0); | |
183 | while(1) { | |
184 | next_out = buffer; | |
185 | available_out = BUFFER_SIZE; | |
186 | result = BrotliEncoderCompressStream( (BrotliEncoderState*) SvIV(state), | |
187 | (BrotliEncoderOperation) op, | |
188 | &available_in, | |
189 | (const uint8_t**) &next_in, | |
190 | &available_out, | |
191 | &next_out, | |
192 | NULL ); | |
193 | if(!result) { | |
194 | Safefree(buffer); | |
195 | croak("Error in BrotliEncoderCompressStream"); | |
196 | } | |
197 | ||
198 | if( available_out != BUFFER_SIZE ) { | |
199 | sv_catpvn(RETVAL, (const char*)buffer, BUFFER_SIZE-available_out); | |
200 | } | |
201 | ||
202 | if( | |
203 | BrotliEncoderIsFinished((BrotliEncoderState*) SvIV(state)) || | |
204 | (!available_in && !BrotliEncoderHasMoreOutput((BrotliEncoderState*) SvIV(state))) | |
205 | ) break; | |
206 | } | |
207 | Safefree(buffer); | |
208 | OUTPUT: | |
209 | RETVAL | |
210 | ||
211 | void BrotliEncoderDestroyInstance(state) | |
212 | SV* state | |
213 | CODE: | |
214 | BrotliEncoderDestroyInstance((BrotliEncoderState*)SvIV(state)); | |
215 | ||
216 | void BrotliEncoderSetCustomDictionary(state, dict) | |
217 | SV* state | |
218 | SV* dict | |
219 | PREINIT: | |
220 | size_t size; | |
221 | uint8_t *data; | |
222 | CODE: | |
223 | data = SvPV(dict, size); | |
224 | BrotliEncoderSetCustomDictionary((BrotliEncoderState*) SvIV(state), size, data); |