multi_compress 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +51 -0
- data/GET_STARTED.md +5 -3
- data/README.md +2 -2
- data/THIRD_PARTY_NOTICES.md +3 -3
- data/ext/multi_compress/exports.darwin +1 -0
- data/ext/multi_compress/exports.map +4 -0
- data/ext/multi_compress/extconf.rb +20 -0
- data/ext/multi_compress/multi_compress.c +352 -149
- data/ext/multi_compress/vendor/.vendored +2 -2
- data/ext/multi_compress/vendor/brotli/c/common/version.h +2 -2
- data/ext/multi_compress/vendor/brotli/c/dec/decode.c +59 -30
- data/ext/multi_compress/vendor/brotli/c/include/brotli/decode.h +1 -3
- data/ext/multi_compress/vendor/zstd/lib/BUCK +10 -5
- data/ext/multi_compress/vendor/zstd/lib/README.md +25 -5
- data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +6 -2
- data/ext/multi_compress/vendor/zstd/lib/common/bits.h +1 -1
- data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +3 -3
- data/ext/multi_compress/vendor/zstd/lib/common/bmi2.h +40 -0
- data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +37 -11
- data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +7 -6
- data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +4 -3
- data/ext/multi_compress/vendor/zstd/lib/common/pool.c +16 -19
- data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +31 -0
- data/ext/multi_compress/vendor/zstd/lib/common/threading.c +16 -2
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +9 -0
- data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +4 -2
- data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +300 -3
- data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +4 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +4 -1
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +775 -214
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +35 -6
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +9 -5
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +14 -5
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +2 -2
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +2 -2
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +49 -5
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +11 -11
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +11 -0
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +16 -18
- data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +3 -3
- data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +64 -39
- data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +30 -2
- data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +78 -64
- data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +182 -18
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +59 -27
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +11 -0
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +194 -93
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +225 -85
- data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +19 -2
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +136 -91
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +77 -77
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +48 -42
- data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +18 -14
- data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +66 -20
- data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +10 -6
- data/ext/multi_compress/vendor/zstd/lib/install_oses.mk +17 -0
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +10 -8
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +5 -364
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +4 -5
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +7 -3
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +8 -5
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +3 -3
- data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +1 -0
- data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +8 -7
- data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +1 -0
- data/ext/multi_compress/vendor/zstd/lib/module.modulemap +0 -21
- data/ext/multi_compress/vendor/zstd/lib/zstd.h +209 -112
- data/lib/multi_compress/active_record.rb +11 -5
- data/lib/multi_compress/codec.rb +164 -94
- data/lib/multi_compress/database/api.rb +42 -0
- data/lib/multi_compress/database/decoding.rb +42 -0
- data/lib/multi_compress/database/dictionary.rb +72 -0
- data/lib/multi_compress/database/encoding.rb +31 -0
- data/lib/multi_compress/database/header.rb +71 -0
- data/lib/multi_compress/database/operations.rb +73 -0
- data/lib/multi_compress/database/validation.rb +95 -0
- data/lib/multi_compress/database.rb +10 -282
- data/lib/multi_compress/db_deployment/bundle_archiver.rb +70 -0
- data/lib/multi_compress/db_deployment/bundle_metadata.rb +56 -0
- data/lib/multi_compress/db_deployment/bundle_stager.rb +62 -0
- data/lib/multi_compress/db_deployment/commands.rb +309 -0
- data/lib/multi_compress/db_deployment/deployment_bundle.rb +80 -0
- data/lib/multi_compress/db_deployment/deterministic_tar_writer.rb +76 -0
- data/lib/multi_compress/db_deployment/dictionary_registry.rb +244 -0
- data/lib/multi_compress/db_deployment/readable_view.rb +112 -0
- data/lib/multi_compress/db_deployment/readable_view_options.rb +51 -0
- data/lib/multi_compress/db_deployment/sql_identifiers.rb +45 -0
- data/lib/multi_compress/db_deployment.rb +10 -844
- data/lib/multi_compress/reader/buffer_operations.rb +57 -0
- data/lib/multi_compress/reader/read_operations.rb +69 -0
- data/lib/multi_compress/reader.rb +97 -0
- data/lib/multi_compress/stream_io.rb +13 -0
- data/lib/multi_compress/version.rb +1 -1
- data/lib/multi_compress/writer.rb +85 -0
- data/lib/multi_compress.rb +42 -333
- data/multi_compress.gemspec +2 -0
- data/mysql_udf/Makefile +12 -5
- data/mysql_udf/multi_compress_mysql.exports +6 -0
- data/postgres_extension/Makefile +2 -1
- data/postgres_extension/test/run_e2e.sh +28 -10
- metadata +30 -10
- data/ext/multi_compress/vendor/lz4/lib/Makefile +0 -244
- data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +0 -63
- data/ext/multi_compress/vendor/zstd/lib/Makefile +0 -389
- data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +0 -48
|
@@ -8,8 +8,9 @@
|
|
|
8
8
|
#include <brotli/decode.h>
|
|
9
9
|
#include <brotli/encode.h>
|
|
10
10
|
#include <lz4.h>
|
|
11
|
-
#include <
|
|
11
|
+
#include <limits.h>
|
|
12
12
|
#include <lz4frame.h>
|
|
13
|
+
#include <lz4hc.h>
|
|
13
14
|
#include <pthread.h>
|
|
14
15
|
#include <stdio.h>
|
|
15
16
|
#include <stdint.h>
|
|
@@ -183,10 +184,7 @@ static size_t zstd_decompress_cached(void *dst, size_t dst_cap, const void *src,
|
|
|
183
184
|
return 0;
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
|
|
187
|
-
return ZSTD_decompress_usingDDict(dctx, dst, dst_cap, src, src_len, ddict);
|
|
188
|
-
|
|
189
|
-
return ZSTD_decompressDCtx(dctx, dst, dst_cap, src, src_len);
|
|
187
|
+
return ZSTD_decompress_usingDDict(dctx, dst, dst_cap, src, src_len, ddict);
|
|
190
188
|
}
|
|
191
189
|
|
|
192
190
|
_Static_assert(ALGO_BROTLI == MC_NUM_ALGOS - 1,
|
|
@@ -518,30 +516,42 @@ static compress_algo_t detect_algo(const uint8_t *data, size_t len) {
|
|
|
518
516
|
return ALGO_ZSTD;
|
|
519
517
|
}
|
|
520
518
|
|
|
521
|
-
static inline
|
|
522
|
-
|
|
519
|
+
static inline long rb_checked_string_length(size_t len) {
|
|
520
|
+
if ((uintmax_t)len > (uintmax_t)LONG_MAX)
|
|
521
|
+
rb_raise(eDataError, "string size exceeds Ruby's maximum length");
|
|
522
|
+
|
|
523
|
+
return (long)len;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
static inline VALUE rb_binary_str_new(const char *ptr, size_t len) {
|
|
527
|
+
VALUE str = rb_str_new(ptr, rb_checked_string_length(len));
|
|
523
528
|
rb_enc_associate(str, binary_encoding);
|
|
524
529
|
return str;
|
|
525
530
|
}
|
|
526
531
|
|
|
527
|
-
static inline VALUE rb_binary_str_buf_new(
|
|
528
|
-
VALUE str = rb_str_buf_new(capa);
|
|
532
|
+
static inline VALUE rb_binary_str_buf_new(size_t capa) {
|
|
533
|
+
VALUE str = rb_str_buf_new(rb_checked_string_length(capa));
|
|
529
534
|
rb_enc_associate(str, binary_encoding);
|
|
530
535
|
return str;
|
|
531
536
|
}
|
|
532
537
|
|
|
533
|
-
static inline VALUE rb_binary_str_buf_reserve(
|
|
534
|
-
VALUE str = rb_str_buf_new(capa);
|
|
538
|
+
static inline VALUE rb_binary_str_buf_reserve(size_t capa) {
|
|
539
|
+
VALUE str = rb_str_buf_new(rb_checked_string_length(capa));
|
|
535
540
|
rb_enc_associate(str, binary_encoding);
|
|
536
541
|
return str;
|
|
537
542
|
}
|
|
538
543
|
|
|
544
|
+
static inline void rb_binary_str_set_len(VALUE str, size_t len) {
|
|
545
|
+
rb_str_set_len(str, rb_checked_string_length(len));
|
|
546
|
+
}
|
|
547
|
+
|
|
539
548
|
static inline void grow_binary_str(VALUE str, size_t cur_len, size_t new_cap) {
|
|
540
549
|
size_t cur_cap = (size_t)rb_str_capacity(str);
|
|
541
550
|
if (new_cap <= cur_cap)
|
|
542
551
|
return;
|
|
543
|
-
|
|
544
|
-
|
|
552
|
+
(void)rb_checked_string_length(new_cap);
|
|
553
|
+
rb_binary_str_set_len(str, cur_len);
|
|
554
|
+
rb_str_modify_expand(str, rb_checked_string_length(new_cap - cur_len));
|
|
545
555
|
}
|
|
546
556
|
|
|
547
557
|
typedef struct {
|
|
@@ -562,6 +572,8 @@ static void limits_config_apply_parsed(const mc_opts_t *opts, limits_config_t *l
|
|
|
562
572
|
size_t max_output_size = NUM2SIZET(val);
|
|
563
573
|
if (max_output_size == 0)
|
|
564
574
|
rb_raise(rb_eArgError, "max_output_size must be greater than 0");
|
|
575
|
+
if ((uintmax_t)max_output_size > (uintmax_t)LONG_MAX)
|
|
576
|
+
rb_raise(rb_eArgError, "max_output_size exceeds Ruby's maximum string length");
|
|
565
577
|
limits->max_output_size = max_output_size;
|
|
566
578
|
}
|
|
567
579
|
|
|
@@ -1306,7 +1318,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1306
1318
|
rb_raise(eMemError, "zstd: failed to create context");
|
|
1307
1319
|
if (ZSTD_isError(csize))
|
|
1308
1320
|
rb_raise(eError, "zstd compress: %s", ZSTD_getErrorName(csize));
|
|
1309
|
-
|
|
1321
|
+
rb_binary_str_set_len(dst, csize);
|
|
1310
1322
|
RB_GC_GUARD(data);
|
|
1311
1323
|
RB_GC_GUARD(dict_val);
|
|
1312
1324
|
return dst;
|
|
@@ -1343,7 +1355,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1343
1355
|
rb_raise(eError, "zstd compress: %s", ZSTD_getErrorName(fargs.result));
|
|
1344
1356
|
}
|
|
1345
1357
|
|
|
1346
|
-
VALUE result = rb_binary_str_new(out_buf,
|
|
1358
|
+
VALUE result = rb_binary_str_new(out_buf, fargs.result);
|
|
1347
1359
|
free(out_buf);
|
|
1348
1360
|
RB_GC_GUARD(data);
|
|
1349
1361
|
RB_GC_GUARD(dict_val);
|
|
@@ -1370,7 +1382,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1370
1382
|
if (ZSTD_isError(args.result))
|
|
1371
1383
|
rb_raise(eError, "zstd compress: %s", ZSTD_getErrorName(args.result));
|
|
1372
1384
|
|
|
1373
|
-
|
|
1385
|
+
rb_binary_str_set_len(dst, args.result);
|
|
1374
1386
|
RB_GC_GUARD(data);
|
|
1375
1387
|
RB_GC_GUARD(dict_val);
|
|
1376
1388
|
return dst;
|
|
@@ -1383,7 +1395,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1383
1395
|
prefs.frameInfo.blockChecksumFlag = LZ4F_blockChecksumEnabled;
|
|
1384
1396
|
prefs.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;
|
|
1385
1397
|
size_t bound = LZ4F_compressFrameBound(slen, &prefs);
|
|
1386
|
-
VALUE dst = rb_binary_str_buf_reserve(
|
|
1398
|
+
VALUE dst = rb_binary_str_buf_reserve(bound);
|
|
1387
1399
|
lz4frame_compress_args_t args = {
|
|
1388
1400
|
.src = src,
|
|
1389
1401
|
.src_len = slen,
|
|
@@ -1401,7 +1413,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1401
1413
|
if (args.error_code)
|
|
1402
1414
|
rb_raise(eError, "lz4 frame compress failed: %s",
|
|
1403
1415
|
LZ4F_getErrorName(args.error_code));
|
|
1404
|
-
|
|
1416
|
+
rb_binary_str_set_len(dst, args.result);
|
|
1405
1417
|
RB_GC_GUARD(data);
|
|
1406
1418
|
return dst;
|
|
1407
1419
|
}
|
|
@@ -1438,7 +1450,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1438
1450
|
size_t total = 8 + (size_t)csize;
|
|
1439
1451
|
write_le_u32((uint8_t *)out + total, 0);
|
|
1440
1452
|
|
|
1441
|
-
|
|
1453
|
+
rb_binary_str_set_len(dst, total + 4);
|
|
1442
1454
|
RB_GC_GUARD(data);
|
|
1443
1455
|
return dst;
|
|
1444
1456
|
} else {
|
|
@@ -1460,7 +1472,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1460
1472
|
size_t total = 8 + csize;
|
|
1461
1473
|
write_le_u32((uint8_t *)out + total, 0);
|
|
1462
1474
|
|
|
1463
|
-
|
|
1475
|
+
rb_binary_str_set_len(dst, total + 4);
|
|
1464
1476
|
RB_GC_GUARD(data);
|
|
1465
1477
|
return dst;
|
|
1466
1478
|
}
|
|
@@ -1516,7 +1528,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1516
1528
|
if (!ok)
|
|
1517
1529
|
rb_raise(eError, "brotli compress with dict failed");
|
|
1518
1530
|
|
|
1519
|
-
|
|
1531
|
+
rb_binary_str_set_len(dst, initial_out - available_out);
|
|
1520
1532
|
RB_GC_GUARD(data);
|
|
1521
1533
|
RB_GC_GUARD(dict_val);
|
|
1522
1534
|
return dst;
|
|
@@ -1540,7 +1552,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1540
1552
|
if (!args.result)
|
|
1541
1553
|
rb_raise(eError, "brotli compress failed");
|
|
1542
1554
|
|
|
1543
|
-
|
|
1555
|
+
rb_binary_str_set_len(dst, actual_out_len);
|
|
1544
1556
|
RB_GC_GUARD(data);
|
|
1545
1557
|
return dst;
|
|
1546
1558
|
} else {
|
|
@@ -1550,7 +1562,7 @@ static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
|
|
|
1550
1562
|
(const uint8_t *)src, &out_len, (uint8_t *)RSTRING_PTR(dst));
|
|
1551
1563
|
if (!ok)
|
|
1552
1564
|
rb_raise(eError, "brotli compress failed");
|
|
1553
|
-
|
|
1565
|
+
rb_binary_str_set_len(dst, out_len);
|
|
1554
1566
|
RB_GC_GUARD(data);
|
|
1555
1567
|
return dst;
|
|
1556
1568
|
}
|
|
@@ -1603,6 +1615,75 @@ static VALUE zstd_single_frame_info(VALUE self, VALUE data) {
|
|
|
1603
1615
|
return result;
|
|
1604
1616
|
}
|
|
1605
1617
|
|
|
1618
|
+
typedef struct {
|
|
1619
|
+
ZSTD_DCtx *dctx;
|
|
1620
|
+
ZSTD_DDict *ddict;
|
|
1621
|
+
const char *src;
|
|
1622
|
+
size_t src_len;
|
|
1623
|
+
limits_config_t limits;
|
|
1624
|
+
} zstd_unknown_size_args_t;
|
|
1625
|
+
|
|
1626
|
+
static VALUE zstd_decompress_unknown_size_body(VALUE arg) {
|
|
1627
|
+
zstd_unknown_size_args_t *a = (zstd_unknown_size_args_t *)arg;
|
|
1628
|
+
ZSTD_DCtx *dctx = a->dctx;
|
|
1629
|
+
const size_t max_out = a->limits.max_output_size;
|
|
1630
|
+
const size_t hard_cap = max_out == SIZE_MAX ? SIZE_MAX : max_out + 1;
|
|
1631
|
+
|
|
1632
|
+
size_t r = ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters);
|
|
1633
|
+
if (ZSTD_isError(r))
|
|
1634
|
+
rb_raise(eError, "zstd dctx reset: %s", ZSTD_getErrorName(r));
|
|
1635
|
+
if (a->ddict) {
|
|
1636
|
+
r = ZSTD_DCtx_refDDict(dctx, a->ddict);
|
|
1637
|
+
if (ZSTD_isError(r))
|
|
1638
|
+
rb_raise(eError, "zstd dict ref: %s", ZSTD_getErrorName(r));
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
size_t alloc_size = (a->src_len > max_out / 8) ? max_out : a->src_len * 8;
|
|
1642
|
+
if (alloc_size < 4096)
|
|
1643
|
+
alloc_size = 4096;
|
|
1644
|
+
if (alloc_size > hard_cap)
|
|
1645
|
+
alloc_size = hard_cap;
|
|
1646
|
+
|
|
1647
|
+
VALUE dst = rb_binary_str_buf_reserve(alloc_size);
|
|
1648
|
+
size_t total_out = 0;
|
|
1649
|
+
ZSTD_inBuffer input = {a->src, a->src_len, 0};
|
|
1650
|
+
|
|
1651
|
+
for (;;) {
|
|
1652
|
+
if (total_out >= alloc_size) {
|
|
1653
|
+
if (alloc_size >= hard_cap)
|
|
1654
|
+
rb_raise(eDataError, "decompressed output exceeds limit (%zu bytes)", max_out);
|
|
1655
|
+
alloc_size = alloc_size > hard_cap / 2 ? hard_cap : alloc_size * 2;
|
|
1656
|
+
grow_binary_str(dst, total_out, alloc_size);
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
size_t out_cap = alloc_size - total_out;
|
|
1660
|
+
ZSTD_outBuffer output = {RSTRING_PTR(dst) + total_out, out_cap, 0};
|
|
1661
|
+
size_t ret = ZSTD_decompressStream(dctx, &output, &input);
|
|
1662
|
+
if (ZSTD_isError(ret))
|
|
1663
|
+
rb_raise(eDataError, "zstd decompress: %s", ZSTD_getErrorName(ret));
|
|
1664
|
+
total_out = checked_add_size(total_out, output.pos,
|
|
1665
|
+
"decompressed output exceeds representable size");
|
|
1666
|
+
enforce_output_and_ratio_limits(total_out, a->src_len, max_out,
|
|
1667
|
+
a->limits.max_ratio_enabled, a->limits.max_ratio);
|
|
1668
|
+
|
|
1669
|
+
if (ret == 0) {
|
|
1670
|
+
if (input.pos == input.size)
|
|
1671
|
+
break;
|
|
1672
|
+
continue;
|
|
1673
|
+
}
|
|
1674
|
+
if (input.pos == input.size && output.pos < out_cap)
|
|
1675
|
+
rb_raise(eDataError, "zstd decompress: truncated input (incomplete frame)");
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
rb_binary_str_set_len(dst, total_out);
|
|
1679
|
+
return dst;
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
static VALUE zstd_tls_dctx_release(VALUE arg) {
|
|
1683
|
+
ZSTD_DCtx_reset((ZSTD_DCtx *)(uintptr_t)arg, ZSTD_reset_session_and_parameters);
|
|
1684
|
+
return Qnil;
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1606
1687
|
static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
1607
1688
|
VALUE data, opts;
|
|
1608
1689
|
scan_one_required_keywords(argc, argv, &data, &opts);
|
|
@@ -1690,7 +1771,7 @@ static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
|
1690
1771
|
|
|
1691
1772
|
enforce_output_and_ratio_limits(dsize, slen, limits.max_output_size,
|
|
1692
1773
|
limits.max_ratio_enabled, limits.max_ratio);
|
|
1693
|
-
|
|
1774
|
+
rb_binary_str_set_len(dst, dsize);
|
|
1694
1775
|
RB_GC_GUARD(data);
|
|
1695
1776
|
RB_GC_GUARD(dict_val);
|
|
1696
1777
|
return dst;
|
|
@@ -1714,7 +1795,7 @@ static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
|
1714
1795
|
rb_raise(eDataError, "zstd decompress: %s", ZSTD_getErrorName(dsize));
|
|
1715
1796
|
enforce_output_and_ratio_limits(dsize, slen, limits.max_output_size,
|
|
1716
1797
|
limits.max_ratio_enabled, limits.max_ratio);
|
|
1717
|
-
|
|
1798
|
+
rb_binary_str_set_len(dst, dsize);
|
|
1718
1799
|
RB_GC_GUARD(data);
|
|
1719
1800
|
RB_GC_GUARD(dict_val);
|
|
1720
1801
|
return dst;
|
|
@@ -1725,69 +1806,21 @@ static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
|
1725
1806
|
if (!dctx)
|
|
1726
1807
|
rb_raise(eMemError, "zstd: failed to create dctx");
|
|
1727
1808
|
|
|
1728
|
-
{
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1809
|
+
zstd_unknown_size_args_t uargs = {
|
|
1810
|
+
.dctx = dctx,
|
|
1811
|
+
.ddict = NULL,
|
|
1812
|
+
.src = src,
|
|
1813
|
+
.src_len = slen,
|
|
1814
|
+
.limits = limits,
|
|
1815
|
+
};
|
|
1734
1816
|
if (dict) {
|
|
1735
|
-
|
|
1736
|
-
if (ddict)
|
|
1737
|
-
|
|
1738
|
-
if (ZSTD_isError(r)) {
|
|
1739
|
-
rb_raise(eError, "zstd dict ref: %s", ZSTD_getErrorName(r));
|
|
1740
|
-
}
|
|
1741
|
-
}
|
|
1742
|
-
}
|
|
1743
|
-
|
|
1744
|
-
size_t alloc_size = (slen > limits.max_output_size / 8) ? limits.max_output_size : slen * 8;
|
|
1745
|
-
if (alloc_size < 4096)
|
|
1746
|
-
alloc_size = limits.max_output_size < 4096 ? limits.max_output_size : 4096;
|
|
1747
|
-
if (alloc_size == 0)
|
|
1748
|
-
alloc_size = limits.max_output_size;
|
|
1749
|
-
|
|
1750
|
-
VALUE dst = rb_binary_str_buf_reserve(alloc_size);
|
|
1751
|
-
size_t total_out = 0;
|
|
1752
|
-
|
|
1753
|
-
ZSTD_inBuffer input = {src, slen, 0};
|
|
1754
|
-
while (input.pos < input.size) {
|
|
1755
|
-
if (total_out >= alloc_size) {
|
|
1756
|
-
if (alloc_size >= limits.max_output_size) {
|
|
1757
|
-
rb_raise(eDataError, "decompressed output exceeds limit (%zu bytes)",
|
|
1758
|
-
limits.max_output_size);
|
|
1759
|
-
}
|
|
1760
|
-
size_t next_cap = alloc_size * 2;
|
|
1761
|
-
if (next_cap > limits.max_output_size)
|
|
1762
|
-
next_cap = limits.max_output_size;
|
|
1763
|
-
alloc_size = next_cap;
|
|
1764
|
-
grow_binary_str(dst, total_out, alloc_size);
|
|
1765
|
-
}
|
|
1766
|
-
|
|
1767
|
-
size_t remaining_budget = limits.max_output_size - total_out;
|
|
1768
|
-
if (remaining_budget == 0) {
|
|
1769
|
-
rb_raise(eDataError, "decompressed output exceeds limit (%zu bytes)",
|
|
1770
|
-
limits.max_output_size);
|
|
1771
|
-
}
|
|
1772
|
-
|
|
1773
|
-
size_t out_cap = alloc_size - total_out;
|
|
1774
|
-
if (out_cap > remaining_budget)
|
|
1775
|
-
out_cap = remaining_budget;
|
|
1776
|
-
|
|
1777
|
-
ZSTD_outBuffer output = {RSTRING_PTR(dst) + total_out, out_cap, 0};
|
|
1778
|
-
size_t ret = ZSTD_decompressStream(dctx, &output, &input);
|
|
1779
|
-
if (ZSTD_isError(ret)) {
|
|
1780
|
-
rb_raise(eDataError, "zstd decompress: %s", ZSTD_getErrorName(ret));
|
|
1781
|
-
}
|
|
1782
|
-
total_out = checked_add_size(total_out, output.pos,
|
|
1783
|
-
"decompressed output exceeds representable size");
|
|
1784
|
-
enforce_output_and_ratio_limits(total_out, slen, limits.max_output_size,
|
|
1785
|
-
limits.max_ratio_enabled, limits.max_ratio);
|
|
1786
|
-
if (ret == 0)
|
|
1787
|
-
break;
|
|
1817
|
+
uargs.ddict = dict_get_ddict(dict);
|
|
1818
|
+
if (!uargs.ddict)
|
|
1819
|
+
rb_raise(eMemError, "zstd: failed to create ddict");
|
|
1788
1820
|
}
|
|
1789
1821
|
|
|
1790
|
-
|
|
1822
|
+
VALUE dst = rb_ensure(zstd_decompress_unknown_size_body, (VALUE)&uargs,
|
|
1823
|
+
zstd_tls_dctx_release, (VALUE)(uintptr_t)dctx);
|
|
1791
1824
|
RB_GC_GUARD(data);
|
|
1792
1825
|
RB_GC_GUARD(dict_val);
|
|
1793
1826
|
return dst;
|
|
@@ -1800,7 +1833,7 @@ static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
|
1800
1833
|
alloc_size = limits.max_output_size < 4096 ? limits.max_output_size : 4096;
|
|
1801
1834
|
if (alloc_size == 0)
|
|
1802
1835
|
alloc_size = limits.max_output_size;
|
|
1803
|
-
VALUE dst = rb_binary_str_buf_reserve(
|
|
1836
|
+
VALUE dst = rb_binary_str_buf_reserve(alloc_size);
|
|
1804
1837
|
while (1) {
|
|
1805
1838
|
lz4frame_decompress_args_t args = {
|
|
1806
1839
|
.src = src,
|
|
@@ -1835,7 +1868,7 @@ static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
|
1835
1868
|
LZ4F_getErrorName(args.error_code));
|
|
1836
1869
|
enforce_output_and_ratio_limits(args.result, slen, limits.max_output_size,
|
|
1837
1870
|
limits.max_ratio_enabled, limits.max_ratio);
|
|
1838
|
-
|
|
1871
|
+
rb_binary_str_set_len(dst, args.result);
|
|
1839
1872
|
RB_GC_GUARD(data);
|
|
1840
1873
|
return dst;
|
|
1841
1874
|
}
|
|
@@ -1890,7 +1923,7 @@ static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
|
1890
1923
|
|
|
1891
1924
|
enforce_output_and_ratio_limits(args.out_offset, slen, limits.max_output_size,
|
|
1892
1925
|
limits.max_ratio_enabled, limits.max_ratio);
|
|
1893
|
-
|
|
1926
|
+
rb_binary_str_set_len(result, args.out_offset);
|
|
1894
1927
|
RB_GC_GUARD(data);
|
|
1895
1928
|
return result;
|
|
1896
1929
|
}
|
|
@@ -1976,7 +2009,7 @@ static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
|
|
|
1976
2009
|
if (res != BROTLI_DECODER_RESULT_SUCCESS) {
|
|
1977
2010
|
rb_raise(eDataError, "brotli decompress failed");
|
|
1978
2011
|
}
|
|
1979
|
-
|
|
2012
|
+
rb_binary_str_set_len(dst, total_out);
|
|
1980
2013
|
RB_GC_GUARD(data);
|
|
1981
2014
|
RB_GC_GUARD(dict_val);
|
|
1982
2015
|
return dst;
|
|
@@ -2131,11 +2164,45 @@ static VALUE compress_version(VALUE self, VALUE algo_sym) {
|
|
|
2131
2164
|
_Static_assert(LZ4_RING_BUFFER_TOTAL == 2 * LZ4_RING_BUFFER_SIZE,
|
|
2132
2165
|
"ring buffer total must be exactly twice the window size");
|
|
2133
2166
|
|
|
2167
|
+
typedef struct {
|
|
2168
|
+
int *active;
|
|
2169
|
+
VALUE self;
|
|
2170
|
+
VALUE argument;
|
|
2171
|
+
VALUE (*operation)(VALUE, VALUE);
|
|
2172
|
+
} stream_operation_t;
|
|
2173
|
+
|
|
2174
|
+
static VALUE stream_operation_body(VALUE opaque) {
|
|
2175
|
+
stream_operation_t *call = (stream_operation_t *)(uintptr_t)opaque;
|
|
2176
|
+
return call->operation(call->self, call->argument);
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
static VALUE stream_operation_ensure(VALUE opaque) {
|
|
2180
|
+
stream_operation_t *call = (stream_operation_t *)(uintptr_t)opaque;
|
|
2181
|
+
*call->active = 0;
|
|
2182
|
+
return Qnil;
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
static VALUE stream_operation_call(int *active, VALUE self, VALUE argument,
|
|
2186
|
+
VALUE (*operation)(VALUE, VALUE)) {
|
|
2187
|
+
if (*active)
|
|
2188
|
+
rb_raise(eStreamError, "stream operation already in progress");
|
|
2189
|
+
|
|
2190
|
+
stream_operation_t call = {
|
|
2191
|
+
.active = active, .self = self, .argument = argument, .operation = operation};
|
|
2192
|
+
*active = 1;
|
|
2193
|
+
VALUE result = rb_ensure(stream_operation_body, (VALUE)(uintptr_t)&call,
|
|
2194
|
+
stream_operation_ensure, (VALUE)(uintptr_t)&call);
|
|
2195
|
+
RB_GC_GUARD(self);
|
|
2196
|
+
RB_GC_GUARD(argument);
|
|
2197
|
+
return result;
|
|
2198
|
+
}
|
|
2199
|
+
|
|
2134
2200
|
typedef struct {
|
|
2135
2201
|
compress_algo_t algo;
|
|
2136
2202
|
int level;
|
|
2137
2203
|
int closed;
|
|
2138
2204
|
int finished;
|
|
2205
|
+
int operation_active;
|
|
2139
2206
|
|
|
2140
2207
|
union {
|
|
2141
2208
|
ZSTD_CStream *zstd;
|
|
@@ -2179,6 +2246,9 @@ static size_t deflater_memsize(const void *ptr) {
|
|
|
2179
2246
|
return 0;
|
|
2180
2247
|
|
|
2181
2248
|
size_t s = sizeof(deflater_t);
|
|
2249
|
+
if (d->operation_active)
|
|
2250
|
+
return s;
|
|
2251
|
+
|
|
2182
2252
|
switch (d->algo) {
|
|
2183
2253
|
case ALGO_ZSTD:
|
|
2184
2254
|
if (d->ctx.zstd)
|
|
@@ -2325,7 +2395,7 @@ static VALUE lz4_compress_ring_block(deflater_t *d) {
|
|
|
2325
2395
|
|
|
2326
2396
|
write_le_u32((uint8_t *)out + 4, (uint32_t)csize);
|
|
2327
2397
|
|
|
2328
|
-
|
|
2398
|
+
rb_binary_str_set_len(output, 8 + csize);
|
|
2329
2399
|
d->lz4_ring.pending = 0;
|
|
2330
2400
|
|
|
2331
2401
|
if (d->lz4_ring.ring_offset >= LZ4_RING_BUFFER_SIZE) {
|
|
@@ -2335,7 +2405,7 @@ static VALUE lz4_compress_ring_block(deflater_t *d) {
|
|
|
2335
2405
|
return output;
|
|
2336
2406
|
}
|
|
2337
2407
|
|
|
2338
|
-
static VALUE
|
|
2408
|
+
static VALUE deflater_write_impl(VALUE self, VALUE chunk) {
|
|
2339
2409
|
deflater_t *d;
|
|
2340
2410
|
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2341
2411
|
if (d->closed)
|
|
@@ -2405,7 +2475,7 @@ static VALUE deflater_write(VALUE self, VALUE chunk) {
|
|
|
2405
2475
|
}
|
|
2406
2476
|
result_len += output.pos;
|
|
2407
2477
|
}
|
|
2408
|
-
|
|
2478
|
+
rb_binary_str_set_len(result, result_len);
|
|
2409
2479
|
RB_GC_GUARD(chunk);
|
|
2410
2480
|
return result;
|
|
2411
2481
|
}
|
|
@@ -2471,7 +2541,7 @@ static VALUE deflater_write(VALUE self, VALUE chunk) {
|
|
|
2471
2541
|
}
|
|
2472
2542
|
}
|
|
2473
2543
|
}
|
|
2474
|
-
|
|
2544
|
+
rb_binary_str_set_len(result, result_len);
|
|
2475
2545
|
RB_GC_GUARD(chunk);
|
|
2476
2546
|
return result;
|
|
2477
2547
|
}
|
|
@@ -2517,7 +2587,7 @@ static VALUE deflater_write(VALUE self, VALUE chunk) {
|
|
|
2517
2587
|
}
|
|
2518
2588
|
}
|
|
2519
2589
|
}
|
|
2520
|
-
|
|
2590
|
+
rb_binary_str_set_len(result, result_len);
|
|
2521
2591
|
RB_GC_GUARD(chunk);
|
|
2522
2592
|
return result;
|
|
2523
2593
|
}
|
|
@@ -2525,7 +2595,14 @@ static VALUE deflater_write(VALUE self, VALUE chunk) {
|
|
|
2525
2595
|
return rb_binary_str_new("", 0);
|
|
2526
2596
|
}
|
|
2527
2597
|
|
|
2528
|
-
static VALUE
|
|
2598
|
+
static VALUE deflater_write(VALUE self, VALUE chunk) {
|
|
2599
|
+
deflater_t *d;
|
|
2600
|
+
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2601
|
+
return stream_operation_call(&d->operation_active, self, chunk, deflater_write_impl);
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
static VALUE deflater_flush_impl(VALUE self, VALUE unused) {
|
|
2605
|
+
(void)unused;
|
|
2529
2606
|
deflater_t *d;
|
|
2530
2607
|
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2531
2608
|
if (d->closed)
|
|
@@ -2554,7 +2631,7 @@ static VALUE deflater_flush(VALUE self) {
|
|
|
2554
2631
|
result_len += output.pos;
|
|
2555
2632
|
} while (ret > 0);
|
|
2556
2633
|
|
|
2557
|
-
|
|
2634
|
+
rb_binary_str_set_len(result, result_len);
|
|
2558
2635
|
return result;
|
|
2559
2636
|
}
|
|
2560
2637
|
case ALGO_BROTLI: {
|
|
@@ -2586,7 +2663,7 @@ static VALUE deflater_flush(VALUE self) {
|
|
|
2586
2663
|
}
|
|
2587
2664
|
} while (BrotliEncoderHasMoreOutput(d->ctx.brotli));
|
|
2588
2665
|
|
|
2589
|
-
|
|
2666
|
+
rb_binary_str_set_len(result, result_len);
|
|
2590
2667
|
return result;
|
|
2591
2668
|
}
|
|
2592
2669
|
case ALGO_LZ4:
|
|
@@ -2595,7 +2672,14 @@ static VALUE deflater_flush(VALUE self) {
|
|
|
2595
2672
|
return rb_binary_str_new("", 0);
|
|
2596
2673
|
}
|
|
2597
2674
|
|
|
2598
|
-
static VALUE
|
|
2675
|
+
static VALUE deflater_flush(VALUE self) {
|
|
2676
|
+
deflater_t *d;
|
|
2677
|
+
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2678
|
+
return stream_operation_call(&d->operation_active, self, Qnil, deflater_flush_impl);
|
|
2679
|
+
}
|
|
2680
|
+
|
|
2681
|
+
static VALUE deflater_finish_impl(VALUE self, VALUE unused) {
|
|
2682
|
+
(void)unused;
|
|
2599
2683
|
deflater_t *d;
|
|
2600
2684
|
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2601
2685
|
if (d->closed)
|
|
@@ -2625,7 +2709,7 @@ static VALUE deflater_finish(VALUE self) {
|
|
|
2625
2709
|
result_len += output.pos;
|
|
2626
2710
|
} while (ret > 0);
|
|
2627
2711
|
|
|
2628
|
-
|
|
2712
|
+
rb_binary_str_set_len(result, result_len);
|
|
2629
2713
|
return result;
|
|
2630
2714
|
}
|
|
2631
2715
|
case ALGO_BROTLI: {
|
|
@@ -2658,7 +2742,7 @@ static VALUE deflater_finish(VALUE self) {
|
|
|
2658
2742
|
} while (BrotliEncoderHasMoreOutput(d->ctx.brotli) ||
|
|
2659
2743
|
!BrotliEncoderIsFinished(d->ctx.brotli));
|
|
2660
2744
|
|
|
2661
|
-
|
|
2745
|
+
rb_binary_str_set_len(result, result_len);
|
|
2662
2746
|
return result;
|
|
2663
2747
|
}
|
|
2664
2748
|
case ALGO_LZ4: {
|
|
@@ -2689,14 +2773,21 @@ static VALUE deflater_finish(VALUE self) {
|
|
|
2689
2773
|
write_le_u32((uint8_t *)out, 0);
|
|
2690
2774
|
result_len += 4;
|
|
2691
2775
|
|
|
2692
|
-
|
|
2776
|
+
rb_binary_str_set_len(result, result_len);
|
|
2693
2777
|
return result;
|
|
2694
2778
|
}
|
|
2695
2779
|
}
|
|
2696
2780
|
return rb_binary_str_new("", 0);
|
|
2697
2781
|
}
|
|
2698
2782
|
|
|
2699
|
-
static VALUE
|
|
2783
|
+
static VALUE deflater_finish(VALUE self) {
|
|
2784
|
+
deflater_t *d;
|
|
2785
|
+
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2786
|
+
return stream_operation_call(&d->operation_active, self, Qnil, deflater_finish_impl);
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
static VALUE deflater_reset_impl(VALUE self, VALUE unused) {
|
|
2790
|
+
(void)unused;
|
|
2700
2791
|
deflater_t *d;
|
|
2701
2792
|
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2702
2793
|
|
|
@@ -2752,7 +2843,14 @@ static VALUE deflater_reset(VALUE self) {
|
|
|
2752
2843
|
return self;
|
|
2753
2844
|
}
|
|
2754
2845
|
|
|
2755
|
-
static VALUE
|
|
2846
|
+
static VALUE deflater_reset(VALUE self) {
|
|
2847
|
+
deflater_t *d;
|
|
2848
|
+
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2849
|
+
return stream_operation_call(&d->operation_active, self, Qnil, deflater_reset_impl);
|
|
2850
|
+
}
|
|
2851
|
+
|
|
2852
|
+
static VALUE deflater_close_impl(VALUE self, VALUE unused) {
|
|
2853
|
+
(void)unused;
|
|
2756
2854
|
deflater_t *d;
|
|
2757
2855
|
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2758
2856
|
if (d->closed)
|
|
@@ -2782,6 +2880,12 @@ static VALUE deflater_close(VALUE self) {
|
|
|
2782
2880
|
return Qnil;
|
|
2783
2881
|
}
|
|
2784
2882
|
|
|
2883
|
+
static VALUE deflater_close(VALUE self) {
|
|
2884
|
+
deflater_t *d;
|
|
2885
|
+
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
2886
|
+
return stream_operation_call(&d->operation_active, self, Qnil, deflater_close_impl);
|
|
2887
|
+
}
|
|
2888
|
+
|
|
2785
2889
|
static VALUE deflater_closed_p(VALUE self) {
|
|
2786
2890
|
deflater_t *d;
|
|
2787
2891
|
TypedData_Get_Struct(self, deflater_t, &deflater_type, d);
|
|
@@ -2792,6 +2896,9 @@ typedef struct {
|
|
|
2792
2896
|
compress_algo_t algo;
|
|
2793
2897
|
int closed;
|
|
2794
2898
|
int finished;
|
|
2899
|
+
int operation_active;
|
|
2900
|
+
int stream_open;
|
|
2901
|
+
int lz4_ended;
|
|
2795
2902
|
size_t max_output_size;
|
|
2796
2903
|
size_t total_output;
|
|
2797
2904
|
size_t total_input;
|
|
@@ -2838,6 +2945,9 @@ static size_t inflater_memsize(const void *ptr) {
|
|
|
2838
2945
|
return 0;
|
|
2839
2946
|
|
|
2840
2947
|
size_t s = sizeof(inflater_t);
|
|
2948
|
+
if (inf->operation_active)
|
|
2949
|
+
return s;
|
|
2950
|
+
|
|
2841
2951
|
switch (inf->algo) {
|
|
2842
2952
|
case ALGO_ZSTD:
|
|
2843
2953
|
if (inf->ctx.zstd)
|
|
@@ -2932,7 +3042,7 @@ static VALUE inflater_initialize(int argc, VALUE *argv, VALUE self) {
|
|
|
2932
3042
|
return self;
|
|
2933
3043
|
}
|
|
2934
3044
|
|
|
2935
|
-
static VALUE
|
|
3045
|
+
static VALUE inflater_write_impl(VALUE self, VALUE chunk) {
|
|
2936
3046
|
inflater_t *inf;
|
|
2937
3047
|
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
2938
3048
|
if (inf->closed)
|
|
@@ -2952,8 +3062,10 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
2952
3062
|
ZSTD_inBuffer input = {src, slen, 0};
|
|
2953
3063
|
size_t out_cap = ZSTD_DStreamOutSize();
|
|
2954
3064
|
size_t result_cap = out_cap > slen * 2 ? out_cap : slen * 2;
|
|
3065
|
+
size_t hard_limit =
|
|
3066
|
+
inf->max_output_size == SIZE_MAX ? SIZE_MAX : inf->max_output_size + 1;
|
|
2955
3067
|
size_t remaining_total_budget =
|
|
2956
|
-
|
|
3068
|
+
hard_limit > inf->total_output ? hard_limit - inf->total_output : 0;
|
|
2957
3069
|
if (remaining_total_budget == 0)
|
|
2958
3070
|
rb_raise(eDataError, "decompressed output exceeds limit (%zu bytes)",
|
|
2959
3071
|
inf->max_output_size);
|
|
@@ -2962,17 +3074,19 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
2962
3074
|
VALUE result = rb_binary_str_buf_reserve(result_cap);
|
|
2963
3075
|
size_t result_len = 0;
|
|
2964
3076
|
VALUE scheduler = current_fiber_scheduler();
|
|
3077
|
+
size_t ret = 0;
|
|
3078
|
+
int drain = 0;
|
|
2965
3079
|
|
|
2966
|
-
while (input.pos < input.size) {
|
|
2967
|
-
size_t remaining_budget =
|
|
3080
|
+
while (input.pos < input.size || drain) {
|
|
3081
|
+
size_t remaining_budget = remaining_total_budget - result_len;
|
|
2968
3082
|
if (remaining_budget == 0)
|
|
2969
3083
|
rb_raise(eDataError, "decompressed output exceeds limit (%zu bytes)",
|
|
2970
3084
|
inf->max_output_size);
|
|
2971
3085
|
|
|
2972
3086
|
if (result_len + out_cap > result_cap) {
|
|
2973
3087
|
size_t next_cap = result_cap * 2;
|
|
2974
|
-
if (next_cap >
|
|
2975
|
-
next_cap =
|
|
3088
|
+
if (next_cap > remaining_total_budget)
|
|
3089
|
+
next_cap = remaining_total_budget;
|
|
2976
3090
|
result_cap = next_cap;
|
|
2977
3091
|
rb_str_resize(result, result_cap);
|
|
2978
3092
|
}
|
|
@@ -2982,7 +3096,6 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
2982
3096
|
current_out_cap = remaining_budget;
|
|
2983
3097
|
|
|
2984
3098
|
ZSTD_outBuffer output = {RSTRING_PTR(result) + result_len, current_out_cap, 0};
|
|
2985
|
-
size_t ret;
|
|
2986
3099
|
|
|
2987
3100
|
if (select_fiber_or_direct_mode(scheduler, input.size - input.pos,
|
|
2988
3101
|
policy->fiber_stream_threshold) == WORK_EXEC_FIBER) {
|
|
@@ -3010,14 +3123,14 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
3010
3123
|
"compressed input exceeds representable size");
|
|
3011
3124
|
enforce_output_and_ratio_limits(total_output, total_input, inf->max_output_size,
|
|
3012
3125
|
inf->max_ratio_enabled, inf->max_ratio);
|
|
3013
|
-
|
|
3014
|
-
break;
|
|
3126
|
+
drain = ret != 0 && output.pos == current_out_cap;
|
|
3015
3127
|
}
|
|
3128
|
+
inf->stream_open = ret != 0;
|
|
3016
3129
|
inf->total_input = checked_add_size(input_accounted_before, input.pos,
|
|
3017
3130
|
"compressed input exceeds representable size");
|
|
3018
3131
|
inf->total_output = checked_add_size(inf->total_output, result_len,
|
|
3019
3132
|
"decompressed output exceeds representable size");
|
|
3020
|
-
|
|
3133
|
+
rb_binary_str_set_len(result, result_len);
|
|
3021
3134
|
RB_GC_GUARD(chunk);
|
|
3022
3135
|
return result;
|
|
3023
3136
|
}
|
|
@@ -3098,7 +3211,7 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
3098
3211
|
"compressed input exceeds representable size");
|
|
3099
3212
|
inf->total_output = checked_add_size(inf->total_output, result_len,
|
|
3100
3213
|
"decompressed output exceeds representable size");
|
|
3101
|
-
|
|
3214
|
+
rb_binary_str_set_len(result, result_len);
|
|
3102
3215
|
RB_GC_GUARD(chunk);
|
|
3103
3216
|
return result;
|
|
3104
3217
|
}
|
|
@@ -3147,12 +3260,17 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
3147
3260
|
int use_fiber = has_fiber_scheduler();
|
|
3148
3261
|
size_t fiber_counter = 0;
|
|
3149
3262
|
|
|
3263
|
+
if (!inf->lz4_ended)
|
|
3264
|
+
inf->stream_open = 1;
|
|
3265
|
+
|
|
3150
3266
|
size_t pos = inf->lz4_buf.offset;
|
|
3151
3267
|
while (pos + 4 <= inf->lz4_buf.len) {
|
|
3152
3268
|
const uint8_t *p = (const uint8_t *)(inf->lz4_buf.buf + pos);
|
|
3153
3269
|
uint32_t orig_size = read_le_u32(p);
|
|
3154
3270
|
if (orig_size == 0) {
|
|
3155
3271
|
inf->finished = 1;
|
|
3272
|
+
inf->lz4_ended = 1;
|
|
3273
|
+
inf->stream_open = 0;
|
|
3156
3274
|
pos += 4;
|
|
3157
3275
|
break;
|
|
3158
3276
|
}
|
|
@@ -3188,6 +3306,8 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
3188
3306
|
|
|
3189
3307
|
result_len += dsize;
|
|
3190
3308
|
pos += 8 + comp_size;
|
|
3309
|
+
inf->lz4_ended = 0;
|
|
3310
|
+
inf->stream_open = 1;
|
|
3191
3311
|
if (use_fiber) {
|
|
3192
3312
|
int did_yield = 0;
|
|
3193
3313
|
fiber_counter = fiber_maybe_yield(fiber_counter, (size_t)dsize,
|
|
@@ -3201,7 +3321,7 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
3201
3321
|
inf->lz4_buf.offset = pos;
|
|
3202
3322
|
inf->total_output = checked_add_size(inf->total_output, result_len,
|
|
3203
3323
|
"decompressed output exceeds representable size");
|
|
3204
|
-
|
|
3324
|
+
rb_binary_str_set_len(result, result_len);
|
|
3205
3325
|
RB_GC_GUARD(chunk);
|
|
3206
3326
|
return result;
|
|
3207
3327
|
}
|
|
@@ -3209,16 +3329,48 @@ static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
|
3209
3329
|
return rb_binary_str_new("", 0);
|
|
3210
3330
|
}
|
|
3211
3331
|
|
|
3212
|
-
static VALUE
|
|
3332
|
+
static VALUE inflater_write(VALUE self, VALUE chunk) {
|
|
3333
|
+
inflater_t *inf;
|
|
3334
|
+
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
3335
|
+
return stream_operation_call(&inf->operation_active, self, chunk, inflater_write_impl);
|
|
3336
|
+
}
|
|
3337
|
+
|
|
3338
|
+
static void inflater_ensure_complete(inflater_t *inf) {
|
|
3339
|
+
switch (inf->algo) {
|
|
3340
|
+
case ALGO_ZSTD:
|
|
3341
|
+
if (inf->stream_open)
|
|
3342
|
+
rb_raise(eDataError, "zstd stream is truncated (incomplete frame)");
|
|
3343
|
+
break;
|
|
3344
|
+
case ALGO_BROTLI:
|
|
3345
|
+
if (inf->total_input > 0 && !BrotliDecoderIsFinished(inf->ctx.brotli))
|
|
3346
|
+
rb_raise(eDataError, "brotli stream is truncated (incomplete stream)");
|
|
3347
|
+
break;
|
|
3348
|
+
case ALGO_LZ4:
|
|
3349
|
+
if (inf->stream_open)
|
|
3350
|
+
rb_raise(eDataError, "lz4 stream is truncated (missing end marker)");
|
|
3351
|
+
break;
|
|
3352
|
+
}
|
|
3353
|
+
}
|
|
3354
|
+
|
|
3355
|
+
static VALUE inflater_finish_impl(VALUE self, VALUE unused) {
|
|
3356
|
+
(void)unused;
|
|
3213
3357
|
inflater_t *inf;
|
|
3214
3358
|
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
3215
3359
|
if (inf->closed)
|
|
3216
3360
|
rb_raise(eStreamError, "stream is closed");
|
|
3361
|
+
inflater_ensure_complete(inf);
|
|
3217
3362
|
inf->finished = 1;
|
|
3218
3363
|
return rb_binary_str_new("", 0);
|
|
3219
3364
|
}
|
|
3220
3365
|
|
|
3221
|
-
static VALUE
|
|
3366
|
+
static VALUE inflater_finish(VALUE self) {
|
|
3367
|
+
inflater_t *inf;
|
|
3368
|
+
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
3369
|
+
return stream_operation_call(&inf->operation_active, self, Qnil, inflater_finish_impl);
|
|
3370
|
+
}
|
|
3371
|
+
|
|
3372
|
+
static VALUE inflater_reset_impl(VALUE self, VALUE unused) {
|
|
3373
|
+
(void)unused;
|
|
3222
3374
|
inflater_t *inf;
|
|
3223
3375
|
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
3224
3376
|
|
|
@@ -3258,12 +3410,21 @@ static VALUE inflater_reset(VALUE self) {
|
|
|
3258
3410
|
}
|
|
3259
3411
|
inf->closed = 0;
|
|
3260
3412
|
inf->finished = 0;
|
|
3413
|
+
inf->stream_open = 0;
|
|
3414
|
+
inf->lz4_ended = 0;
|
|
3261
3415
|
inf->total_output = 0;
|
|
3262
3416
|
inf->total_input = 0;
|
|
3263
3417
|
return self;
|
|
3264
3418
|
}
|
|
3265
3419
|
|
|
3266
|
-
static VALUE
|
|
3420
|
+
static VALUE inflater_reset(VALUE self) {
|
|
3421
|
+
inflater_t *inf;
|
|
3422
|
+
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
3423
|
+
return stream_operation_call(&inf->operation_active, self, Qnil, inflater_reset_impl);
|
|
3424
|
+
}
|
|
3425
|
+
|
|
3426
|
+
static VALUE inflater_close_impl(VALUE self, VALUE unused) {
|
|
3427
|
+
(void)unused;
|
|
3267
3428
|
inflater_t *inf;
|
|
3268
3429
|
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
3269
3430
|
if (inf->closed)
|
|
@@ -3289,6 +3450,12 @@ static VALUE inflater_close(VALUE self) {
|
|
|
3289
3450
|
return Qnil;
|
|
3290
3451
|
}
|
|
3291
3452
|
|
|
3453
|
+
static VALUE inflater_close(VALUE self) {
|
|
3454
|
+
inflater_t *inf;
|
|
3455
|
+
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
3456
|
+
return stream_operation_call(&inf->operation_active, self, Qnil, inflater_close_impl);
|
|
3457
|
+
}
|
|
3458
|
+
|
|
3292
3459
|
static VALUE inflater_closed_p(VALUE self) {
|
|
3293
3460
|
inflater_t *inf;
|
|
3294
3461
|
TypedData_Get_Struct(self, inflater_t, &inflater_type, inf);
|
|
@@ -3409,6 +3576,59 @@ static VALUE brotli_train_dictionary(int argc, VALUE *argv, VALUE self) {
|
|
|
3409
3576
|
return train_dictionary_internal(samples, parsed_opts.size, ALGO_BROTLI);
|
|
3410
3577
|
}
|
|
3411
3578
|
|
|
3579
|
+
typedef struct {
|
|
3580
|
+
FILE *file;
|
|
3581
|
+
VALUE path_value;
|
|
3582
|
+
const char *path;
|
|
3583
|
+
uint8_t *data;
|
|
3584
|
+
size_t size;
|
|
3585
|
+
int loaded;
|
|
3586
|
+
} dict_file_read_t;
|
|
3587
|
+
|
|
3588
|
+
static VALUE dict_file_read_body(VALUE opaque) {
|
|
3589
|
+
dict_file_read_t *read = (dict_file_read_t *)(uintptr_t)opaque;
|
|
3590
|
+
|
|
3591
|
+
if (fseek(read->file, 0, SEEK_END) != 0)
|
|
3592
|
+
rb_sys_fail(read->path);
|
|
3593
|
+
|
|
3594
|
+
long file_size = ftell(read->file);
|
|
3595
|
+
if (file_size < 0)
|
|
3596
|
+
rb_sys_fail(read->path);
|
|
3597
|
+
if (file_size == 0)
|
|
3598
|
+
rb_raise(eDataError, "dictionary file is empty: %s", read->path);
|
|
3599
|
+
if ((unsigned long long)file_size > DICT_FILE_MAX_SIZE)
|
|
3600
|
+
rb_raise(eDataError, "dictionary file too large (%ld bytes, max=%d)", file_size,
|
|
3601
|
+
(int)DICT_FILE_MAX_SIZE);
|
|
3602
|
+
if (fseek(read->file, 0, SEEK_SET) != 0)
|
|
3603
|
+
rb_sys_fail(read->path);
|
|
3604
|
+
|
|
3605
|
+
read->data = ALLOC_N(uint8_t, file_size);
|
|
3606
|
+
read->path = RSTRING_PTR(read->path_value);
|
|
3607
|
+
read->size = (size_t)file_size;
|
|
3608
|
+
size_t read_bytes = fread(read->data, 1, read->size, read->file);
|
|
3609
|
+
if (read_bytes != read->size) {
|
|
3610
|
+
xfree(read->data);
|
|
3611
|
+
read->data = NULL;
|
|
3612
|
+
rb_raise(eDataError, "failed to read dictionary: %s", read->path);
|
|
3613
|
+
}
|
|
3614
|
+
|
|
3615
|
+
read->loaded = 1;
|
|
3616
|
+
return Qnil;
|
|
3617
|
+
}
|
|
3618
|
+
|
|
3619
|
+
static VALUE dict_file_read_ensure(VALUE opaque) {
|
|
3620
|
+
dict_file_read_t *read = (dict_file_read_t *)(uintptr_t)opaque;
|
|
3621
|
+
if (read->file) {
|
|
3622
|
+
fclose(read->file);
|
|
3623
|
+
read->file = NULL;
|
|
3624
|
+
}
|
|
3625
|
+
if (!read->loaded && read->data) {
|
|
3626
|
+
xfree(read->data);
|
|
3627
|
+
read->data = NULL;
|
|
3628
|
+
}
|
|
3629
|
+
return Qnil;
|
|
3630
|
+
}
|
|
3631
|
+
|
|
3412
3632
|
static VALUE dict_load(int argc, VALUE *argv, VALUE self) {
|
|
3413
3633
|
VALUE path, opts;
|
|
3414
3634
|
scan_one_required_keywords(argc, argv, &path, &opts);
|
|
@@ -3425,40 +3645,23 @@ static VALUE dict_load(int argc, VALUE *argv, VALUE self) {
|
|
|
3425
3645
|
if (algo == ALGO_LZ4)
|
|
3426
3646
|
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
|
|
3427
3647
|
|
|
3648
|
+
VALUE dict_obj = dict_alloc(cDictionary);
|
|
3649
|
+
dictionary_t *d;
|
|
3650
|
+
TypedData_Get_Struct(dict_obj, dictionary_t, &dictionary_type, d);
|
|
3651
|
+
|
|
3428
3652
|
const char *cpath = RSTRING_PTR(path);
|
|
3429
|
-
FILE *
|
|
3430
|
-
if (!
|
|
3653
|
+
FILE *file = fopen(cpath, "rb");
|
|
3654
|
+
if (!file)
|
|
3431
3655
|
rb_sys_fail(cpath);
|
|
3432
3656
|
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
if (file_size <= 0) {
|
|
3438
|
-
fclose(f);
|
|
3439
|
-
rb_raise(eDataError, "dictionary file is empty: %s", cpath);
|
|
3440
|
-
}
|
|
3441
|
-
if ((unsigned long long)file_size > DICT_FILE_MAX_SIZE) {
|
|
3442
|
-
fclose(f);
|
|
3443
|
-
rb_raise(eDataError, "dictionary file too large (%ld bytes, max=%d)", file_size,
|
|
3444
|
-
(int)DICT_FILE_MAX_SIZE);
|
|
3445
|
-
}
|
|
3446
|
-
|
|
3447
|
-
uint8_t *buf = ALLOC_N(uint8_t, file_size);
|
|
3448
|
-
size_t read_bytes = fread(buf, 1, file_size, f);
|
|
3449
|
-
fclose(f);
|
|
3657
|
+
dict_file_read_t read = {.file = file, .path_value = path, .path = cpath};
|
|
3658
|
+
rb_ensure(dict_file_read_body, (VALUE)(uintptr_t)&read, dict_file_read_ensure,
|
|
3659
|
+
(VALUE)(uintptr_t)&read);
|
|
3450
3660
|
|
|
3451
|
-
if ((long)read_bytes != file_size) {
|
|
3452
|
-
xfree(buf);
|
|
3453
|
-
rb_raise(eDataError, "failed to read dictionary: %s", cpath);
|
|
3454
|
-
}
|
|
3455
|
-
|
|
3456
|
-
VALUE dict_obj = dict_alloc(cDictionary);
|
|
3457
|
-
dictionary_t *d;
|
|
3458
|
-
TypedData_Get_Struct(dict_obj, dictionary_t, &dictionary_type, d);
|
|
3459
3661
|
d->algo = algo;
|
|
3460
|
-
d->data =
|
|
3461
|
-
d->size =
|
|
3662
|
+
d->data = read.data;
|
|
3663
|
+
d->size = read.size;
|
|
3664
|
+
read.data = NULL;
|
|
3462
3665
|
return dict_obj;
|
|
3463
3666
|
}
|
|
3464
3667
|
|
|
@@ -3488,7 +3691,7 @@ static VALUE dict_algo(VALUE self) {
|
|
|
3488
3691
|
static VALUE dict_bytes(VALUE self) {
|
|
3489
3692
|
dictionary_t *d;
|
|
3490
3693
|
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
|
|
3491
|
-
return rb_binary_str_new((const char *)d->data,
|
|
3694
|
+
return rb_binary_str_new((const char *)d->data, d->size);
|
|
3492
3695
|
}
|
|
3493
3696
|
|
|
3494
3697
|
static VALUE dict_zstd_id(VALUE self) {
|
|
@@ -3505,7 +3708,7 @@ static VALUE dict_size(VALUE self) {
|
|
|
3505
3708
|
return SIZET2NUM(d->size);
|
|
3506
3709
|
}
|
|
3507
3710
|
|
|
3508
|
-
void Init_multi_compress(void) {
|
|
3711
|
+
RUBY_FUNC_EXPORTED void Init_multi_compress(void) {
|
|
3509
3712
|
binary_encoding = rb_ascii8bit_encoding();
|
|
3510
3713
|
init_id_cache();
|
|
3511
3714
|
crc32_init_tables();
|