Class: MultiCompress::Dictionary
- Inherits:
-
Object
- Object
- MultiCompress::Dictionary
- Defined in:
- ext/multi_compress/multi_compress.c
Class Method Summary collapse
Instance Method Summary collapse
Constructor Details
#initialize(*args) ⇒ Object
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 |
# File 'ext/multi_compress/multi_compress.c', line 2537
static VALUE dict_initialize(int argc, VALUE *argv, VALUE self) {
VALUE raw, opts;
rb_scan_args(argc, argv, "1:", &raw, &opts);
StringValue(raw);
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
VALUE algo_sym = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
}
d->algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
if (d->algo == ALGO_LZ4)
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
d->size = RSTRING_LEN(raw);
d->data = ALLOC_N(uint8_t, d->size);
memcpy(d->data, RSTRING_PTR(raw), d->size);
return self;
}
|
Class Method Details
.load(*args) ⇒ Object
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 |
# File 'ext/multi_compress/multi_compress.c', line 2652
static VALUE dict_load(int argc, VALUE *argv, VALUE self) {
VALUE path, opts;
rb_scan_args(argc, argv, "1:", &path, &opts);
StringValue(path);
VALUE algo_sym = Qnil;
if (!NIL_P(opts)) {
algo_sym = rb_hash_aref(opts, ID2SYM(rb_intern("algo")));
}
compress_algo_t algo = NIL_P(algo_sym) ? ALGO_ZSTD : sym_to_algo(algo_sym);
if (algo == ALGO_LZ4)
rb_raise(eUnsupportedError, "LZ4 does not support dictionaries");
const char *cpath = RSTRING_PTR(path);
FILE *f = fopen(cpath, "rb");
if (!f)
rb_sys_fail(cpath);
fseek(f, 0, SEEK_END);
long file_size = ftell(f);
fseek(f, 0, SEEK_SET);
if (file_size <= 0) {
fclose(f);
rb_raise(eDataError, "dictionary file is empty: %s", cpath);
}
if ((unsigned long long)file_size > DICT_FILE_MAX_SIZE) {
fclose(f);
rb_raise(eDataError, "dictionary file too large (%ld bytes, max=%d)", file_size,
(int)DICT_FILE_MAX_SIZE);
}
uint8_t *buf = ALLOC_N(uint8_t, file_size);
size_t read_bytes = fread(buf, 1, file_size, f);
fclose(f);
if ((long)read_bytes != file_size) {
xfree(buf);
rb_raise(eDataError, "failed to read dictionary: %s", cpath);
}
VALUE dict_obj = dict_alloc(cDictionary);
dictionary_t *d;
TypedData_Get_Struct(dict_obj, dictionary_t, &dictionary_type, d);
d->algo = algo;
d->data = buf;
d->size = (size_t)file_size;
return dict_obj;
}
|
Instance Method Details
#algo ⇒ Object
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 |
# File 'ext/multi_compress/multi_compress.c', line 2720
static VALUE dict_algo(VALUE self) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
switch (d->algo) {
case ALGO_ZSTD:
return ID2SYM(rb_intern("zstd"));
case ALGO_LZ4:
return ID2SYM(rb_intern("lz4"));
case ALGO_BROTLI:
return ID2SYM(rb_intern("brotli"));
}
return Qnil;
}
|
#save(path) ⇒ Object
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 |
# File 'ext/multi_compress/multi_compress.c', line 2703
static VALUE dict_save(VALUE self, VALUE path) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
const char *cpath = StringValueCStr(path);
FILE *f = fopen(cpath, "wb");
if (!f)
rb_sys_fail(cpath);
size_t written = fwrite(d->data, 1, d->size, f);
fclose(f);
if (written != d->size)
rb_raise(eError, "failed to write dictionary to %s", cpath);
return path;
}
|
#size ⇒ Object
2734 2735 2736 2737 2738 |
# File 'ext/multi_compress/multi_compress.c', line 2734
static VALUE dict_size(VALUE self) {
dictionary_t *d;
TypedData_Get_Struct(self, dictionary_t, &dictionary_type, d);
return SIZET2NUM(d->size);
}
|