Module: MultiCompress
- Defined in:
- lib/multi_compress.rb,
lib/multi_compress/version.rb,
ext/multi_compress/multi_compress.c
Defined Under Namespace
Modules: Brotli, LZ4, Zstd Classes: DataError, Deflater, Dictionary, Error, Inflater, LevelError, MemError, Reader, StreamError, UnsupportedError, Writer
Constant Summary collapse
- FASTEST =
:fastest- DEFAULT =
:default- BEST =
:best- VERSION =
"0.2.0"
Class Method Summary collapse
- .adler32(*args) ⇒ Object
- .algo_from_ext(path) ⇒ Object
- .algorithms ⇒ Object
- .available?(algo_sym) ⇒ Boolean
- .brotli(data, level: nil) ⇒ Object
- .brotli_decompress(data) ⇒ Object
- .compress(*args) ⇒ Object
- .crc32(*args) ⇒ Object
- .decompress(*args) ⇒ Object
- .lz4(data, level: nil) ⇒ Object
- .lz4_decompress(data) ⇒ Object
- .version(algo_sym) ⇒ Object
- .zstd(data, level: nil) ⇒ Object
- .zstd_decompress(data) ⇒ Object
Class Method Details
.adler32(*args) ⇒ Object
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 |
# File 'ext/multi_compress/multi_compress.c', line 1247
static VALUE compress_adler32(int argc, VALUE *argv, VALUE self) {
VALUE data, prev;
rb_scan_args(argc, argv, "11", &data, &prev);
StringValue(data);
const uint8_t *src = (const uint8_t *)RSTRING_PTR(data);
size_t len = RSTRING_LEN(data);
uint32_t adler = NIL_P(prev) ? 1 : NUM2UINT(prev);
uint32_t s1 = adler & 0xFFFF;
uint32_t s2 = (adler >> 16) & 0xFFFF;
const uint32_t BASE = 65521;
while (len > 0) {
size_t chunk = len > 5552 ? 5552 : len;
len -= chunk;
for (size_t i = 0; i < chunk; i++) {
s1 += src[i];
s2 += s1;
}
s1 %= BASE;
s2 %= BASE;
src += chunk;
}
return UINT2NUM((s2 << 16) | s1);
}
|
.algo_from_ext(path) ⇒ Object
57 58 59 |
# File 'lib/multi_compress.rb', line 57 def self.algo_from_ext(path) EXTENSION_MAP[File.extname(path).downcase] end |
.algorithms ⇒ Object
1275 1276 1277 1278 1279 1280 1281 |
# File 'ext/multi_compress/multi_compress.c', line 1275 static VALUE compress_algorithms(VALUE self) { VALUE ary = rb_ary_new_capa(3); rb_ary_push(ary, ID2SYM(rb_intern("zstd"))); rb_ary_push(ary, ID2SYM(rb_intern("lz4"))); rb_ary_push(ary, ID2SYM(rb_intern("brotli"))); return ary; } |
.available?(algo_sym) ⇒ Boolean
1283 1284 1285 1286 |
# File 'ext/multi_compress/multi_compress.c', line 1283
static VALUE compress_available_p(VALUE self, VALUE algo_sym) {
sym_to_algo(algo_sym);
return Qtrue;
}
|
.brotli(data, level: nil) ⇒ Object
41 42 43 |
# File 'lib/multi_compress.rb', line 41 def self.brotli(data, level: nil) compress(data, algo: :brotli, **level_opts(level)) end |
.brotli_decompress(data) ⇒ Object
53 54 55 |
# File 'lib/multi_compress.rb', line 53 def self.brotli_decompress(data) decompress(data, algo: :brotli) end |
.compress(*args) ⇒ Object
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 |
# File 'ext/multi_compress/multi_compress.c', line 617
static VALUE compress_compress(int argc, VALUE *argv, VALUE self) {
VALUE data, opts;
rb_scan_args(argc, argv, "1:", &data, &opts);
StringValue(data);
VALUE algo_sym = Qnil, level_val = Qnil, dict_val = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
level_val = rb_hash_aref(opts, ID2SYM(rb_intern("level")));
dict_val = rb_hash_aref(opts, ID2SYM(rb_intern("dictionary")));
}
compress_algo_t algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
int level = resolve_level(algo, level_val);
dictionary_t *dict = NULL;
if (!NIL_P(dict_val)) {
if (algo == ALGO_LZ4) {
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
}
TypedData_Get_Struct(dict_val, dictionary_t, &dictionary_type, dict);
}
const char *src = RSTRING_PTR(data);
size_t slen = RSTRING_LEN(data);
switch (algo) {
case ALGO_ZSTD: {
size_t bound = ZSTD_compressBound(slen);
ZSTD_CDict *cdict = NULL;
if (dict) {
cdict = dict_get_cdict(dict, level);
if (!cdict)
rb_raise(eMemError, "zstd: failed to create/get cdict");
}
if (slen < GVL_UNLOCK_THRESHOLD) {
VALUE dst = rb_binary_str_buf_reserve(bound);
size_t csize;
if (cdict) {
ZSTD_CCtx *cctx = ZSTD_createCCtx();
if (!cctx)
rb_raise(eMemError, "zstd: failed to create context");
csize = ZSTD_compress_usingCDict(cctx, RSTRING_PTR(dst), bound, src, slen, cdict);
ZSTD_freeCCtx(cctx);
} else {
csize = ZSTD_compress(RSTRING_PTR(dst), bound, src, slen, level);
}
if (ZSTD_isError(csize))
rb_raise(eError, "zstd compress: %s", ZSTD_getErrorName(csize));
rb_str_set_len(dst, (long)csize);
RB_GC_GUARD(data);
return dst;
}
{
VALUE scheduler = current_fiber_scheduler();
if (scheduler != Qnil) {
char *out_buf = (char *)malloc(bound);
if (!out_buf)
rb_raise(eMemError, "zstd: malloc failed");
VALUE blocker = rb_obj_alloc(rb_cObject);
zstd_fiber_compress_t fargs = {
.src = src,
.src_len = slen,
.level = level,
.cdict = cdict,
.dst = out_buf,
.dst_cap = bound,
.result = 0,
.error = 0,
.scheduler = scheduler,
.blocker = blocker,
.fiber = rb_fiber_current(),
};
VALUE rb_thread = rb_thread_create(zstd_fiber_compress_thread, &fargs);
rb_fiber_scheduler_block(scheduler, blocker, Qnil);
rb_funcall(rb_thread, rb_intern("join"), 0);
if (fargs.error) {
free(out_buf);
rb_raise(eMemError, "zstd: failed to create context");
}
if (ZSTD_isError(fargs.result)) {
free(out_buf);
rb_raise(eError, "zstd compress: %s", ZSTD_getErrorName(fargs.result));
}
VALUE result = rb_binary_str_new(out_buf, (long)fargs.result);
free(out_buf);
RB_GC_GUARD(data);
return result;
}
}
{
VALUE dst = rb_binary_str_buf_reserve(bound);
zstd_compress_args_t args = {
.src = src,
.src_len = slen,
.dst = RSTRING_PTR(dst),
.dst_cap = bound,
.level = level,
.cdict = cdict,
.result = 0,
.error = 0,
};
run_without_gvl(zstd_compress_nogvl, &args);
if (args.error)
rb_raise(eMemError, "zstd: failed to create context");
if (ZSTD_isError(args.result))
rb_raise(eError, "zstd compress: %s", ZSTD_getErrorName(args.result));
rb_str_set_len(dst, (long)args.result);
RB_GC_GUARD(data);
return dst;
}
}
case ALGO_LZ4: {
if (slen > (size_t)INT_MAX)
rb_raise(eError, "lz4: input too large (max 2GB)");
int bound = LZ4_compressBound((int)slen);
int csize;
if (slen >= GVL_UNLOCK_THRESHOLD) {
VALUE dst = rb_binary_str_buf_reserve(8 + (size_t)bound + 4);
char *out = RSTRING_PTR(dst);
out[0] = (slen >> 0) & 0xFF;
out[1] = (slen >> 8) & 0xFF;
out[2] = (slen >> 16) & 0xFF;
out[3] = (slen >> 24) & 0xFF;
lz4_compress_args_t args = {
.src = src,
.src_len = (int)slen,
.dst = out + 8,
.dst_cap = bound,
.level = level,
};
VALUE scheduler = current_fiber_scheduler();
if (scheduler != Qnil) {
run_via_fiber_worker(scheduler, lz4_compress_nogvl, &args);
} else {
run_without_gvl(lz4_compress_nogvl, &args);
}
csize = args.result;
if (csize <= 0)
rb_raise(eError, "lz4 compress failed");
out[4] = (csize >> 0) & 0xFF;
out[5] = (csize >> 8) & 0xFF;
out[6] = (csize >> 16) & 0xFF;
out[7] = (csize >> 24) & 0xFF;
size_t total = 8 + (size_t)csize;
out[total] = 0;
out[total + 1] = 0;
out[total + 2] = 0;
out[total + 3] = 0;
rb_str_set_len(dst, (long)(total + 4));
RB_GC_GUARD(data);
return dst;
} else {
VALUE dst = rb_binary_str_buf_reserve(8 + bound + 4);
char *out = RSTRING_PTR(dst);
out[0] = (slen >> 0) & 0xFF;
out[1] = (slen >> 8) & 0xFF;
out[2] = (slen >> 16) & 0xFF;
out[3] = (slen >> 24) & 0xFF;
if (level > 1) {
csize = LZ4_compress_HC(src, out + 8, (int)slen, bound, level);
} else {
csize = LZ4_compress_default(src, out + 8, (int)slen, bound);
}
if (csize <= 0)
rb_raise(eError, "lz4 compress failed");
out[4] = (csize >> 0) & 0xFF;
out[5] = (csize >> 8) & 0xFF;
out[6] = (csize >> 16) & 0xFF;
out[7] = (csize >> 24) & 0xFF;
size_t total = 8 + csize;
out[total] = 0;
out[total + 1] = 0;
out[total + 2] = 0;
out[total + 3] = 0;
rb_str_set_len(dst, total + 4);
RB_GC_GUARD(data);
return dst;
}
}
case ALGO_BROTLI: {
size_t out_len = BrotliEncoderMaxCompressedSize(slen);
if (out_len == 0)
out_len = slen + (slen >> 2) + 1024;
if (dict) {
VALUE dst = rb_binary_str_buf_reserve(out_len);
BrotliEncoderState *enc = BrotliEncoderCreateInstance(NULL, NULL, NULL);
if (!enc)
rb_raise(eMemError, "brotli: failed to create encoder");
BrotliEncoderPreparedDictionary *pd =
BrotliEncoderPrepareDictionary(BROTLI_SHARED_DICTIONARY_RAW, dict->size, dict->data,
BROTLI_MAX_QUALITY, NULL, NULL, NULL);
if (!pd) {
BrotliEncoderDestroyInstance(enc);
rb_raise(eMemError, "brotli: failed to prepare dictionary");
}
if (!BrotliEncoderSetParameter(enc, BROTLI_PARAM_QUALITY, level) ||
!BrotliEncoderAttachPreparedDictionary(enc, pd)) {
BrotliEncoderDestroyPreparedDictionary(pd);
BrotliEncoderDestroyInstance(enc);
rb_raise(eError, "brotli: failed to attach dictionary");
}
size_t available_in = slen;
const uint8_t *next_in = (const uint8_t *)src;
size_t available_out = out_len;
uint8_t *next_out = (uint8_t *)RSTRING_PTR(dst);
size_t initial_out = available_out;
BROTLI_BOOL ok =
BrotliEncoderCompressStream(enc, BROTLI_OPERATION_FINISH, &available_in, &next_in,
&available_out, &next_out, NULL);
BrotliEncoderDestroyPreparedDictionary(pd);
BrotliEncoderDestroyInstance(enc);
if (!ok)
rb_raise(eError, "brotli compress with dict failed");
rb_str_set_len(dst, initial_out - available_out);
RB_GC_GUARD(data);
return dst;
} else if (slen >= GVL_UNLOCK_THRESHOLD) {
VALUE dst = rb_binary_str_buf_reserve(out_len);
size_t actual_out_len = out_len;
brotli_compress_args_t args = {
.level = level,
.src_len = slen,
.src = (const uint8_t *)src,
.out_len = &actual_out_len,
.dst = (uint8_t *)RSTRING_PTR(dst),
};
VALUE scheduler = current_fiber_scheduler();
if (scheduler != Qnil) {
run_via_fiber_worker(scheduler, brotli_compress_nogvl, &args);
} else {
run_without_gvl(brotli_compress_nogvl, &args);
}
if (!args.result)
rb_raise(eError, "brotli compress failed");
rb_str_set_len(dst, (long)actual_out_len);
RB_GC_GUARD(data);
return dst;
} else {
VALUE dst = rb_binary_str_buf_reserve(out_len);
BROTLI_BOOL ok =
BrotliEncoderCompress(level, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, slen,
(const uint8_t *)src, &out_len, (uint8_t *)RSTRING_PTR(dst));
if (!ok)
rb_raise(eError, "brotli compress failed");
rb_str_set_len(dst, out_len);
RB_GC_GUARD(data);
return dst;
}
}
}
return Qnil;
}
|
.crc32(*args) ⇒ Object
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 |
# File 'ext/multi_compress/multi_compress.c', line 1235
static VALUE compress_crc32(int argc, VALUE *argv, VALUE self) {
VALUE data, prev;
rb_scan_args(argc, argv, "11", &data, &prev);
StringValue(data);
const uint8_t *src = (const uint8_t *)RSTRING_PTR(data);
size_t len = RSTRING_LEN(data);
uint32_t crc = NIL_P(prev) ? 0 : NUM2UINT(prev);
return UINT2NUM(crc32_compute(src, len, crc));
}
|
.decompress(*args) ⇒ Object
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 |
# File 'ext/multi_compress/multi_compress.c', line 907
static VALUE compress_decompress(int argc, VALUE *argv, VALUE self) {
VALUE data, opts;
rb_scan_args(argc, argv, "1:", &data, &opts);
StringValue(data);
VALUE algo_sym = Qnil, dict_val = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
dict_val = rb_hash_aref(opts, ID2SYM(rb_intern("dictionary")));
}
const uint8_t *src = (const uint8_t *)RSTRING_PTR(data);
size_t slen = RSTRING_LEN(data);
compress_algo_t algo;
if (NIL_P(algo_sym)) {
algo = detect_algo(src, slen);
} else {
algo = sym_to_algo(algo_sym);
}
dictionary_t *dict = NULL;
if (!NIL_P(dict_val)) {
if (algo == ALGO_LZ4) {
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
}
TypedData_Get_Struct(dict_val, dictionary_t, &dictionary_type, dict);
}
switch (algo) {
case ALGO_ZSTD: {
unsigned long long frame_size = ZSTD_getFrameContentSize(src, slen);
if (frame_size == ZSTD_CONTENTSIZE_ERROR) {
rb_raise(eDataError, "zstd: not valid compressed data");
}
if (frame_size != ZSTD_CONTENTSIZE_UNKNOWN && frame_size <= MAX_DECOMPRESS_SIZE) {
size_t dsize;
if (frame_size >= GVL_UNLOCK_THRESHOLD) {
VALUE dst = rb_binary_str_buf_reserve((size_t)frame_size);
ZSTD_DDict *ddict = NULL;
if (dict) {
ddict = dict_get_ddict(dict);
if (!ddict)
rb_raise(eMemError, "zstd: failed to create ddict");
}
zstd_decompress_args_t args = {
.src = src,
.src_len = slen,
.dst = RSTRING_PTR(dst),
.dst_cap = (size_t)frame_size,
.ddict = ddict,
};
VALUE scheduler = current_fiber_scheduler();
if (scheduler != Qnil) {
run_via_fiber_worker(scheduler, zstd_decompress_nogvl, &args);
} else {
run_without_gvl(zstd_decompress_nogvl, &args);
}
if (args.error)
rb_raise(eMemError, "zstd: failed to create dctx");
dsize = args.result;
if (ZSTD_isError(dsize))
rb_raise(eDataError, "zstd decompress: %s", ZSTD_getErrorName(dsize));
rb_str_set_len(dst, (long)dsize);
RB_GC_GUARD(data);
return dst;
} else {
VALUE dst = rb_binary_str_buf_reserve((size_t)frame_size);
if (dict) {
ZSTD_DDict *ddict = dict_get_ddict(dict);
if (!ddict)
rb_raise(eMemError, "zstd: failed to create ddict");
ZSTD_DCtx *dctx = ZSTD_createDCtx();
if (!dctx)
rb_raise(eMemError, "zstd: failed to create dctx");
dsize = ZSTD_decompress_usingDDict(dctx, RSTRING_PTR(dst), (size_t)frame_size,
src, slen, ddict);
ZSTD_freeDCtx(dctx);
} else {
dsize = ZSTD_decompress(RSTRING_PTR(dst), (size_t)frame_size, src, slen);
}
if (ZSTD_isError(dsize))
rb_raise(eDataError, "zstd decompress: %s", ZSTD_getErrorName(dsize));
rb_str_set_len(dst, dsize);
RB_GC_GUARD(data);
return dst;
}
}
ZSTD_DCtx *dctx = ZSTD_createDCtx();
if (!dctx)
rb_raise(eMemError, "zstd: failed to create dctx");
if (dict) {
ZSTD_DDict *ddict = dict_get_ddict(dict);
if (ddict) {
size_t r = ZSTD_DCtx_refDDict(dctx, ddict);
if (ZSTD_isError(r)) {
ZSTD_freeDCtx(dctx);
rb_raise(eError, "zstd dict ref: %s", ZSTD_getErrorName(r));
}
}
}
size_t alloc_size = (slen > MAX_DECOMPRESS_SIZE / 8) ? MAX_DECOMPRESS_SIZE : slen * 8;
if (alloc_size < 4096)
alloc_size = 4096;
VALUE dst = rb_binary_str_buf_reserve(alloc_size);
size_t total_out = 0;
ZSTD_inBuffer input = {src, slen, 0};
while (input.pos < input.size) {
if (total_out >= alloc_size) {
if (alloc_size >= MAX_DECOMPRESS_SIZE) {
ZSTD_freeDCtx(dctx);
rb_raise(eDataError, "zstd: decompressed size exceeds limit (%lluMB)",
(unsigned long long)(MAX_DECOMPRESS_SIZE / (1024 * 1024)));
}
alloc_size *= 2;
if (alloc_size > MAX_DECOMPRESS_SIZE)
alloc_size = MAX_DECOMPRESS_SIZE;
grow_binary_str(dst, total_out, alloc_size);
}
ZSTD_outBuffer output = {RSTRING_PTR(dst) + total_out, alloc_size - total_out, 0};
size_t ret = ZSTD_decompressStream(dctx, &output, &input);
if (ZSTD_isError(ret)) {
ZSTD_freeDCtx(dctx);
rb_raise(eDataError, "zstd decompress: %s", ZSTD_getErrorName(ret));
}
total_out += output.pos;
if (ret == 0)
break;
}
ZSTD_freeDCtx(dctx);
rb_str_set_len(dst, total_out);
RB_GC_GUARD(data);
return dst;
}
case ALGO_LZ4: {
if (slen < 4)
rb_raise(eDataError, "lz4: data too short");
size_t total_orig = 0;
size_t scan_pos = 0;
while (scan_pos + 4 <= slen) {
uint32_t orig_size = (uint32_t)src[scan_pos] | ((uint32_t)src[scan_pos + 1] << 8) |
((uint32_t)src[scan_pos + 2] << 16) |
((uint32_t)src[scan_pos + 3] << 24);
if (orig_size == 0)
break;
if (scan_pos + 8 > slen)
rb_raise(eDataError, "lz4: truncated block header");
uint32_t comp_size = (uint32_t)src[scan_pos + 4] | ((uint32_t)src[scan_pos + 5] << 8) |
((uint32_t)src[scan_pos + 6] << 16) |
((uint32_t)src[scan_pos + 7] << 24);
if (scan_pos + 8 + comp_size > slen)
rb_raise(eDataError, "lz4: truncated block data");
if (orig_size > 256 * 1024 * 1024)
rb_raise(eDataError, "lz4: block too large (%u)", orig_size);
total_orig += orig_size;
if (total_orig > MAX_DECOMPRESS_SIZE)
rb_raise(eDataError, "lz4: total decompressed size exceeds limit");
scan_pos += 8 + comp_size;
}
VALUE result = rb_binary_str_buf_reserve(total_orig);
lz4_decompress_all_args_t args = {
.src = src,
.src_len = slen,
.dst = RSTRING_PTR(result),
.out_offset = 0,
.error = 0,
};
if (total_orig >= GVL_UNLOCK_THRESHOLD) {
VALUE scheduler = current_fiber_scheduler();
if (scheduler != Qnil) {
run_via_fiber_worker(scheduler, lz4_decompress_all_nogvl, &args);
} else {
run_without_gvl(lz4_decompress_all_nogvl, &args);
}
} else {
lz4_decompress_all_nogvl(&args);
}
if (args.error)
rb_raise(eDataError, "%s", args.err_msg);
rb_str_set_len(result, args.out_offset);
RB_GC_GUARD(data);
return result;
}
case ALGO_BROTLI: {
size_t alloc_size = (slen > MAX_DECOMPRESS_SIZE / 4) ? MAX_DECOMPRESS_SIZE : slen * 4;
if (alloc_size < 1024)
alloc_size = 1024;
BrotliDecoderState *dec = BrotliDecoderCreateInstance(NULL, NULL, NULL);
if (!dec)
rb_raise(eMemError, "brotli: failed to create decoder");
if (dict) {
BrotliDecoderAttachDictionary(dec, BROTLI_SHARED_DICTIONARY_RAW, dict->size,
dict->data);
}
VALUE dst = rb_binary_str_buf_reserve(alloc_size);
size_t total_out = 0;
size_t available_in = slen;
const uint8_t *next_in = src;
VALUE scheduler = current_fiber_scheduler();
BrotliDecoderResult res = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
while (res == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
size_t available_out = alloc_size - total_out;
uint8_t *next_out = (uint8_t *)RSTRING_PTR(dst) + total_out;
if (scheduler != Qnil && available_in >= FIBER_STREAM_THRESHOLD) {
brotli_decompress_stream_args_t sargs = {
.dec = dec,
.available_in = &available_in,
.next_in = &next_in,
.available_out = &available_out,
.next_out = &next_out,
.result = BROTLI_DECODER_RESULT_ERROR,
};
run_via_fiber_worker(scheduler, brotli_decompress_stream_nogvl, &sargs);
res = sargs.result;
} else {
res = BrotliDecoderDecompressStream(dec, &available_in, &next_in, &available_out,
&next_out, NULL);
}
total_out = next_out - (uint8_t *)RSTRING_PTR(dst);
if (res == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
if (alloc_size >= MAX_DECOMPRESS_SIZE) {
BrotliDecoderDestroyInstance(dec);
rb_raise(eDataError, "brotli: decompressed size exceeds limit (%lluMB)",
(unsigned long long)(MAX_DECOMPRESS_SIZE / (1024 * 1024)));
}
alloc_size *= 2;
if (alloc_size > MAX_DECOMPRESS_SIZE)
alloc_size = MAX_DECOMPRESS_SIZE;
grow_binary_str(dst, total_out, alloc_size);
}
}
BrotliDecoderDestroyInstance(dec);
if (res != BROTLI_DECODER_RESULT_SUCCESS) {
rb_raise(eDataError, "brotli decompress failed");
}
rb_str_set_len(dst, total_out);
RB_GC_GUARD(data);
return dst;
}
}
return Qnil;
}
|
.lz4(data, level: nil) ⇒ Object
37 38 39 |
# File 'lib/multi_compress.rb', line 37 def self.lz4(data, level: nil) compress(data, algo: :lz4, **level_opts(level)) end |
.lz4_decompress(data) ⇒ Object
49 50 51 |
# File 'lib/multi_compress.rb', line 49 def self.lz4_decompress(data) decompress(data, algo: :lz4) end |
.version(algo_sym) ⇒ Object
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 |
# File 'ext/multi_compress/multi_compress.c', line 1288
static VALUE compress_version(VALUE self, VALUE algo_sym) {
compress_algo_t algo = sym_to_algo(algo_sym);
switch (algo) {
case ALGO_ZSTD:
return rb_str_new_cstr(ZSTD_versionString());
case ALGO_LZ4:
return rb_sprintf("%d.%d.%d", LZ4_VERSION_MAJOR, LZ4_VERSION_MINOR, LZ4_VERSION_RELEASE);
case ALGO_BROTLI:
return rb_sprintf("%d.%d.%d", BrotliEncoderVersion() >> 24,
(BrotliEncoderVersion() >> 12) & 0xFFF, BrotliEncoderVersion() & 0xFFF);
}
return Qnil;
}
|
.zstd(data, level: nil) ⇒ Object
33 34 35 |
# File 'lib/multi_compress.rb', line 33 def self.zstd(data, level: nil) compress(data, algo: :zstd, **level_opts(level)) end |
.zstd_decompress(data) ⇒ Object
45 46 47 |
# File 'lib/multi_compress.rb', line 45 def self.zstd_decompress(data) decompress(data, algo: :zstd) end |