forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbz2.c
More file actions
569 lines (499 loc) · 17.4 KB
/
bz2.c
File metadata and controls
569 lines (499 loc) · 17.4 KB
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
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* This implementation emulate CPython's bz2
* see cpython/Modules/_bz2module.c
*/
#define DEBUG 500
// #define BENCHMARK
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <bzlib.h>
typedef char Byte; /* 8 bits */
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#define API_FUNC __declspec(dllexport)
#else
#define API_FUNC
#endif
// Integer.MAX_INT
#define GRAALPYTHON_MAX_SIZE (INT_MAX)
#ifndef NDEBUG
#include <stdio.h>
#include <stdarg.h>
static void debug_log(int level, char *file, int line, char *format, ...) {
if (DEBUG > level) {
return;
}
char *type = "ALL";
switch (level) {
case 1000: type = "SEVERE"; break;
case 900: type = "WARNING"; break;
case 800: type = "INFO"; break;
case 700: type = "CONFIG"; break;
case 500: type = "FINE"; break;
case 400: type = "FINER"; break;
case 300: type = "FINEST"; break;
}
va_list args;
va_start(args, format);
fprintf(stderr, "[%s %s:%d] ", type, file, line);
vfprintf(stderr, format, args);
// fprintf(stderr, "\n");
va_end(args);
}
#define LOG_SEVERE(format, ...) \
debug_log(1000, __FILE__, __LINE__, format, ## __VA_ARGS__);
#define LOG_WARNING(format, ...) \
debug_log(900, __FILE__, __LINE__, format, ## __VA_ARGS__);
#define LOG_INFO(format, ...) \
debug_log(800, __FILE__, __LINE__, format, ## __VA_ARGS__);
#define LOG_CONFIG(format, ...) \
debug_log(700, __FILE__, __LINE__, format, ## __VA_ARGS__);
#define LOG_FINE(format, ...) \
debug_log(500, __FILE__, __LINE__, format, ## __VA_ARGS__);
#define LOG_FINER(format, ...) \
debug_log(400, __FILE__, __LINE__, format, ## __VA_ARGS__);
#define LOG_FINEST(format, ...) \
debug_log(300, __FILE__, __LINE__, format, ## __VA_ARGS__);
#else
#define LOG_SEVERE(format, ...)
#define LOG_WARNING(format, ...)
#define LOG_INFO(format, ...)
#define LOG_CONFIG(format, ...)
#define LOG_FINE(format, ...)
#define LOG_FINER(format, ...)
#define LOG_FINEST(format, ...)
#endif
#ifdef BENCHMARK
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6;
}
#define START_TIME double start_time = get_time();
#define END_TIME(bzst) bzst->timeElapsed += get_time() - start_time;
#else
#define START_TIME
#define END_TIME(bzst)
#endif
// The ref_count is important for `copy` as we
// share off heap storage between native objects.
typedef struct
{
size_t ref_count;
size_t size;
Byte *buf;
} off_heap_buffer;
#define NOT_INITIALIZED 0
#define COMPRESS_TYPE 1
#define DECOMPRESS_TYPE 2
typedef struct
{
bz_stream bzs;
int bzs_type;
off_heap_buffer *output;
size_t output_size;
ssize_t next_in_index;
ssize_t bzs_avail_in_real;
#ifdef BENCHMARK
double timeElapsed;
#endif
} bzst_stream;
static void* BZ2_Malloc(void* ctx, int items, int size)
{
void *m = malloc((size_t)items * (size_t)size);
LOG_FINER("malloc(address: %p, items: %u, size: %u)\n", m, items, size);
return m;
}
static void BZ2_Free(void* ctx, void *ptr)
{
LOG_FINER("free(%p)\n", ptr);
free(ptr);
}
static off_heap_buffer* bz_allocate_buffer(size_t items)
{
size_t size = items * sizeof(Byte);
if (!size) {
LOG_SEVERE("Creating Empty Buffer!!\n");
size = sizeof(Byte);
}
off_heap_buffer *o = (off_heap_buffer*) malloc(sizeof(off_heap_buffer));
if (!o) {
return NULL;
}
Byte *buf = (Byte*) malloc(size);
if (!buf) {
LOG_SEVERE("Memory Error!!\n");
}
o->ref_count = 1;
o->buf = buf;
o->size = items;
LOG_FINER("malloc[off_heap_buffer](address: %p, buf: %p, items: %zu, ref_count: %zu)\n", o, buf, items, o->ref_count);
return o;
}
static void bz_release_buffer(off_heap_buffer *o) {
if (!o) {
return;
}
if (o->ref_count == 0) {
LOG_SEVERE("trying to double free(%p)!!\n", o);
return;
}
LOG_FINEST("off_heap_buffer(address: %p, ref_count: %zu - 1)\n", o, o->ref_count);
o->ref_count--;
if (o->ref_count > 0) {
return;
}
LOG_FINER("free(%p)\n", o);
free(o->buf);
free(o);
}
// nfi_function: name('createStream') map('bzst_stream*', 'POINTER')
API_FUNC bzst_stream *bz_create_bzst_stream() {
bzst_stream *bzst = (bzst_stream *) malloc(sizeof(bzst_stream));
bzst->bzs.opaque = NULL;
bzst->bzs.bzalloc = BZ2_Malloc;
bzst->bzs.bzfree = BZ2_Free;
bzst->bzs_type = NOT_INITIALIZED;
bzst->output = bz_allocate_buffer(1);
bzst->output->size = 0;
bzst->output_size = 0;
bzst->next_in_index = 0;
bzst->bzs_avail_in_real = 0;
#ifdef BENCHMARK
bzst->timeElapsed = 0;
#endif
LOG_INFO("bzst_stream(%p)\n", bzst);
return bzst;
}
// nfi_function: name('getTimeElapsed') map('bzst_stream*', 'POINTER') static(true)
API_FUNC double bz_get_timeElapsed(bzst_stream* zst) {
#ifdef BENCHMARK
double t = bzst->timeElapsed;
LOG_FINEST("time Elapsed: %.2f\n", t);
bzst->timeElapsed = 0;
return t;
#else
return -1.0;
#endif
}
// nfi_function: name('deallocateStream') map('bzst_stream*', 'POINTER')
API_FUNC void bz_free_stream(bzst_stream* bzst) {
if (!bzst) {
return;
}
if(bzst->bzs_type) {
if (bzst->bzs_type == COMPRESS_TYPE) {
BZ2_bzCompressEnd(&bzst->bzs);
} else {
BZ2_bzDecompressEnd(&bzst->bzs);
}
}
bz_release_buffer(bzst->output);
LOG_INFO("free bzst_stream(%p)\n", bzst);
free(bzst);
}
// nfi_function: name('gcReleaseHelper') map('bzst_stream*', 'POINTER') release(true)
API_FUNC void bz_gc_helper(bzst_stream* bzst) {
bz_free_stream(bzst);
}
// nfi_function: name('getNextInIndex') map('bzst_stream*', 'POINTER')
API_FUNC ssize_t bz_get_next_in_index(bzst_stream *bzst) {
return bzst->next_in_index;
}
// nfi_function: name('getBzsAvailInReal') map('bzst_stream*', 'POINTER')
API_FUNC ssize_t bz_get_bzs_avail_in_real(bzst_stream *bzst) {
return bzst->bzs_avail_in_real;
}
// nfi_function: name('setBzsAvailInReal') map('bzst_stream*', 'POINTER')
API_FUNC void bz_set_bzs_avail_in_real(bzst_stream *bzst, ssize_t v) {
bzst->bzs_avail_in_real = v;
}
// nfi_function: name('getOutputBufferSize') map('bzst_stream*', 'POINTER')
API_FUNC size_t bz_get_output_buffer_size(bzst_stream *bzst) {
LOG_INFO("bz_get_output_buffer_size(%p)\n", bzst);
size_t size = bzst->output_size;
if (size > GRAALPYTHON_MAX_SIZE) {
LOG_SEVERE("buffer size is larger than max: %zd > %zd!!\n", size, (ssize_t) GRAALPYTHON_MAX_SIZE);
return GRAALPYTHON_MAX_SIZE;
}
return size;
}
static void clear_output(bzst_stream *bzst) {
bz_release_buffer(bzst->output);
bzst->output = bz_allocate_buffer(1);
bzst->output->size = 0;
bzst->output_size = 0;
}
// nfi_function: name('getOutputBuffer') map('bzst_stream*', 'POINTER')
API_FUNC void bz_get_output_buffer(bzst_stream *bzst, Byte *dest) {
LOG_INFO("bz_get_off_heap_buffer(%p)\n", bzst);
off_heap_buffer *buffer = bzst->output;
size_t size = bzst->output_size;
if (!size) {
return;
}
memcpy(dest, buffer->buf, size);
clear_output(bzst);
}
static int resize_output_buffer(bzst_stream *bzst, ssize_t length) {
LOG_INFO("resize_output_buffer(%p, %zd)\n", bzst, length);
off_heap_buffer *current = bzst->output;
off_heap_buffer *resized = bz_allocate_buffer(length);
if (!resized) {
return -1;
}
memcpy(resized->buf, current->buf, bzst->output->size);
bz_release_buffer(current);
bzst->output = resized;
return 0;
}
static int bz_prepare_output_buffer(bzst_stream *bzst, ssize_t len) {
LOG_INFO("bz_prepare_output_buffer(%p, %zd)\n", bzst, len);
bz_release_buffer(bzst->output);
bzst->output = bz_allocate_buffer(len);
if (!bzst->output) {
return -1;
}
bzst->output_size = 0;
return 0;
}
static int isOK(int bzerror) {
switch(bzerror) {
case BZ_OK:
case BZ_RUN_OK:
case BZ_FLUSH_OK:
case BZ_FINISH_OK:
case BZ_STREAM_END:
return 1;
default:
return 0;
}
}
static int
grow_buffer(bzst_stream *bzst, ssize_t max_length) {
LOG_INFO("grow_buffer(%p, %zd)\n", bzst, max_length);
/* Expand the buffer by an amount proportional to the current size,
giving us amortized linear-time behavior. Use a less-than-double
growth factor to avoid excessive allocation. */
size_t size = bzst->output->size;
size_t new_size = size + (size >> 3) + 6;
if (max_length > 0 && new_size > (size_t) max_length)
new_size = (size_t) max_length;
if (new_size > size) {
return resize_output_buffer(bzst, new_size);
} else { /* overflow */
return -1;
}
}
/************************************************
* Compress Object *
************************************************/
// nfi_function: name('compressInit') map('bzst_stream*', 'POINTER')
API_FUNC int bz_compressor_init(bzst_stream *bzst, int compresslevel) {
LOG_INFO("bz_compressor_init(%p, %d)\n", bzst, compresslevel);
int bzerror = BZ2_bzCompressInit(&bzst->bzs, compresslevel, 0, 0);
if (!isOK(bzerror)) {
return bzerror;
}
bzst->bzs_type = COMPRESS_TYPE;
return BZ_OK;
}
// nfi_function: name('compress') map('bzst_stream*', 'POINTER')
API_FUNC int bz_compress(bzst_stream *bzst, Byte *data, ssize_t len, int action, ssize_t bufsize) {
LOG_INFO("bz_compress(%p, %zd, %d, %zd)\n", bzst, len, action, bufsize);
size_t data_size = 0;
if (bz_prepare_output_buffer(bzst, bufsize) < 0) {
return BZ_MEM_ERROR;
}
if (len == 0) {
bzst->bzs.next_in = NULL;
} else {
bzst->bzs.next_in = data;
}
bzst->bzs.avail_in = 0;
bzst->bzs.next_out = bzst->output->buf;
bzst->bzs.avail_out = bzst->output->size;
for (;;) {
char *this_out;
int bzerror;
/* On a 64-bit system, len might not fit in avail_in (an unsigned int).
Do compression in chunks of no more than UINT_MAX bytes each. */
if (bzst->bzs.avail_in == 0 && len > 0) {
if (len > GRAALPYTHON_MAX_SIZE) {
bzst->bzs.avail_in = GRAALPYTHON_MAX_SIZE;
} else {
bzst->bzs.avail_in = len;
}
len -= bzst->bzs.avail_in;
}
/* In regular compression mode, stop when input data is exhausted. */
if (action == BZ_RUN && bzst->bzs.avail_in == 0)
break;
if (bzst->bzs.avail_out == 0) {
size_t buffer_left = bzst->output->size - data_size;
if (buffer_left == 0) {
if (grow_buffer(bzst, -1) < 0) {
clear_output(bzst);
return BZ_MEM_ERROR;
}
bzst->bzs.next_out = bzst->output->buf + data_size;
buffer_left = bzst->output->size - data_size;
}
if (buffer_left > GRAALPYTHON_MAX_SIZE) {
bzst->bzs.avail_out = GRAALPYTHON_MAX_SIZE;
} else {
bzst->bzs.avail_out = buffer_left;
}
}
this_out = bzst->bzs.next_out;
bzerror = BZ2_bzCompress(&bzst->bzs, action);
data_size += bzst->bzs.next_out - this_out;
if (!isOK(bzerror)) {
clear_output(bzst);
return bzerror;
}
/* In flushing mode, stop when all buffered data has been flushed. */
if (action == BZ_FINISH && bzerror == BZ_STREAM_END)
break;
}
bzst->output_size = data_size;
return BZ_OK;
}
/************************************************
* Decompress Object *
************************************************/
// nfi_function: name('decompressInit') map('bzst_stream*', 'POINTER')
API_FUNC int bz_decompress_init(bzst_stream *bzst) {
LOG_INFO("bz_decompress_init(%p)\n", bzst);
int bzerror = BZ2_bzDecompressInit(&bzst->bzs, 0, 0);
if (!isOK(bzerror)) {
return bzerror;
}
bzst->bzs_type = DECOMPRESS_TYPE;
return BZ_OK;
}
/* Decompress data of length d->bzs_avail_in_real in d->bzs.next_in. The output
buffer is allocated dynamically and returned. At most max_length bytes are
returned, so some of the input may not be consumed. d->bzs.next_in and
d->bzs_avail_in_real are updated to reflect the consumed input. */
// nfi_function: name('decompress') map('bzst_stream*', 'POINTER')
API_FUNC int bz_decompress(bzst_stream *bzst,
Byte *input_buffer, ssize_t offset,
ssize_t max_length,
ssize_t bufsize, ssize_t bzs_avail_in_real) {
LOG_INFO("bz_decompress(%p, %p, %zd, %zd, %zd)\n", bzst, input_buffer, offset, max_length, bufsize);
/* data_size is strictly positive, but because we repeatedly have to
compare against max_length and PyBytes_GET_SIZE we declare it as
signed */
ssize_t data_size = 0;
bz_stream *bzs = &bzst->bzs;
bzst->bzs_avail_in_real = bzs_avail_in_real;
if (offset < 0) {
bzs->next_in = NULL;
} else {
bzs->next_in = input_buffer + offset;
}
int check_alloc;
if (max_length < 0 || max_length >= bufsize) {
check_alloc = bz_prepare_output_buffer(bzst, bufsize);
} else {
check_alloc = bz_prepare_output_buffer(bzst, max_length);
}
if (check_alloc < 0) {
return BZ_MEM_ERROR;
}
bzs->next_out = bzst->output->buf;
int ret = BZ_OK;
for (;;) {
int bzret;
size_t avail;
/* On a 64-bit system, buffer length might not fit in avail_out, so we
do decompression in chunks of no more than UINT_MAX bytes
each. Note that the expression for `avail` is guaranteed to be
positive, so the cast is safe. */
avail = (size_t) (bzst->output->size - data_size);
if (avail > GRAALPYTHON_MAX_SIZE) {
bzs->avail_out = (unsigned int) GRAALPYTHON_MAX_SIZE;
} else {
bzs->avail_out = (unsigned int) avail;
}
if (bzst->bzs_avail_in_real > GRAALPYTHON_MAX_SIZE) {
bzs->avail_in = (unsigned int) GRAALPYTHON_MAX_SIZE;
} else {
bzs->avail_in = (unsigned int) bzst->bzs_avail_in_real;
}
bzst->bzs_avail_in_real -= bzs->avail_in;
bzret = BZ2_bzDecompress(bzs);
data_size = bzs->next_out - bzst->output->buf;
bzst->next_in_index = bzs->next_in - input_buffer;
bzst->bzs_avail_in_real += bzs->avail_in;
if (!isOK(bzret)) {
clear_output(bzst);
return bzret;
}
if (bzret == BZ_STREAM_END) {
// d->eof = 1; // it will be set on the java-side
ret = bzret;
break;
} else if (bzst->bzs_avail_in_real == 0) {
break;
} else if (bzs->avail_out == 0) {
if (data_size == max_length) {
break;
}
if (data_size == bzst->output->size && grow_buffer(bzst, max_length) < 0) {
clear_output(bzst);
return BZ_MEM_ERROR;
}
bzs->next_out = bzst->output->buf + data_size;
}
}
bzst->output_size = data_size;
return ret;
}