|
| 1 | +#include <common.h> |
| 2 | +#include <streaming_compress.h> |
| 3 | + |
| 4 | +struct streaming_compress_t { |
| 5 | + ZSTD_CCtx* ctx; |
| 6 | + VALUE buf; |
| 7 | + size_t buf_size; |
| 8 | + size_t pos; |
| 9 | +}; |
| 10 | + |
| 11 | +static void |
| 12 | +streaming_compress_mark(void *p) |
| 13 | +{ |
| 14 | + struct streaming_compress_t *sc = p; |
| 15 | + rb_gc_mark((VALUE)sc->ctx); |
| 16 | + rb_gc_mark(sc->buf); |
| 17 | + rb_gc_mark(sc->buf_size); |
| 18 | +} |
| 19 | + |
| 20 | +static void |
| 21 | +streaming_compress_free(void *p) |
| 22 | +{ |
| 23 | + struct streaming_compress_t *sc = p; |
| 24 | + ZSTD_CCtx* ctx = sc->ctx; |
| 25 | + if (ctx != NULL) { |
| 26 | + ZSTD_freeCCtx(ctx); |
| 27 | + } |
| 28 | + xfree(sc); |
| 29 | +} |
| 30 | + |
| 31 | +static size_t |
| 32 | +streaming_compress_memsize(const void *p) |
| 33 | +{ |
| 34 | + return sizeof(struct streaming_compress_t); |
| 35 | +} |
| 36 | + |
| 37 | +static const rb_data_type_t streaming_compress_type = { |
| 38 | + "streaming_compress", |
| 39 | + { streaming_compress_mark, streaming_compress_free, streaming_compress_memsize, }, |
| 40 | + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY |
| 41 | +}; |
| 42 | + |
| 43 | +static VALUE |
| 44 | +rb_streaming_compress_allocate(VALUE klass) |
| 45 | +{ |
| 46 | + struct streaming_compress_t* sc; |
| 47 | + VALUE obj = TypedData_Make_Struct(klass, struct streaming_compress_t, &streaming_compress_type, sc); |
| 48 | + sc->ctx = NULL; |
| 49 | + sc->buf = Qnil; |
| 50 | + sc->buf_size = 0; |
| 51 | + return obj; |
| 52 | +} |
| 53 | + |
| 54 | +static VALUE |
| 55 | +rb_streaming_compress_initialize(int argc, VALUE *argv, VALUE obj) |
| 56 | +{ |
| 57 | + VALUE compression_level_value; |
| 58 | + rb_scan_args(argc, argv, "01", &compression_level_value); |
| 59 | + |
| 60 | + int compression_level; |
| 61 | + if (NIL_P(compression_level_value)) { |
| 62 | + compression_level = 0; // The default. See ZSTD_CLEVEL_DEFAULT in zstd_compress.c |
| 63 | + } else { |
| 64 | + compression_level = NUM2INT(compression_level_value); |
| 65 | + } |
| 66 | + |
| 67 | + struct streaming_compress_t* sc; |
| 68 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 69 | + size_t const buffOutSize = ZSTD_CStreamOutSize(); |
| 70 | + |
| 71 | + ZSTD_CCtx* ctx = ZSTD_createCCtx(); |
| 72 | + if (ctx == NULL) { |
| 73 | + rb_raise(rb_eRuntimeError, "%s", "ZSTD_createCCtx error"); |
| 74 | + } |
| 75 | + ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, compression_level); |
| 76 | + sc->ctx = ctx; |
| 77 | + sc->buf = rb_str_new(NULL, buffOutSize); |
| 78 | + sc->buf_size = buffOutSize; |
| 79 | + |
| 80 | + return obj; |
| 81 | +} |
| 82 | + |
| 83 | +#define FIXNUMARG(val, ifnil) \ |
| 84 | + (NIL_P((val)) ? (ifnil) \ |
| 85 | + : (FIX2INT((val)))) |
| 86 | +#define ARG_CONTINUE(val) FIXNUMARG((val), ZSTD_e_continue) |
| 87 | + |
| 88 | +static VALUE |
| 89 | +no_compress(struct streaming_compress_t* sc, ZSTD_EndDirective endOp) |
| 90 | +{ |
| 91 | + ZSTD_inBuffer input = { NULL, 0, 0 }; |
| 92 | + const char* output_data = RSTRING_PTR(sc->buf); |
| 93 | + VALUE result = rb_str_new(0, 0); |
| 94 | + size_t ret; |
| 95 | + do { |
| 96 | + ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 }; |
| 97 | + |
| 98 | + size_t const ret = ZSTD_compressStream2(sc->ctx, &output, &input, endOp); |
| 99 | + if (ZSTD_isError(ret)) { |
| 100 | + rb_raise(rb_eRuntimeError, "flush error error code: %s", ZSTD_getErrorName(ret)); |
| 101 | + } |
| 102 | + rb_str_cat(result, output.dst, output.pos); |
| 103 | + } while (ret > 0); |
| 104 | + return result; |
| 105 | +} |
| 106 | + |
| 107 | +static VALUE |
| 108 | +rb_streaming_compress_compress(VALUE obj, VALUE src) |
| 109 | +{ |
| 110 | + StringValue(src); |
| 111 | + const char* input_data = RSTRING_PTR(src); |
| 112 | + size_t input_size = RSTRING_LEN(src); |
| 113 | + ZSTD_inBuffer input = { input_data, input_size, 0 }; |
| 114 | + |
| 115 | + struct streaming_compress_t* sc; |
| 116 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 117 | + const char* output_data = RSTRING_PTR(sc->buf); |
| 118 | + VALUE result = rb_str_new(0, 0); |
| 119 | + while (input.pos < input.size) { |
| 120 | + ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 }; |
| 121 | + size_t const ret = ZSTD_compressStream2(sc->ctx, &output, &input, ZSTD_e_continue); |
| 122 | + if (ZSTD_isError(ret)) { |
| 123 | + rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret)); |
| 124 | + } |
| 125 | + rb_str_cat(result, output.dst, output.pos); |
| 126 | + } |
| 127 | + return result; |
| 128 | +} |
| 129 | + |
| 130 | +static VALUE |
| 131 | +rb_streaming_compress_addstr(VALUE obj, VALUE src) |
| 132 | +{ |
| 133 | + StringValue(src); |
| 134 | + const char* input_data = RSTRING_PTR(src); |
| 135 | + size_t input_size = RSTRING_LEN(src); |
| 136 | + ZSTD_inBuffer input = { input_data, input_size, 0 }; |
| 137 | + |
| 138 | + struct streaming_compress_t* sc; |
| 139 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 140 | + const char* output_data = RSTRING_PTR(sc->buf); |
| 141 | + |
| 142 | + while (input.pos < input.size) { |
| 143 | + ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 }; |
| 144 | + size_t const result = ZSTD_compressStream2(sc->ctx, &output, &input, ZSTD_e_continue); |
| 145 | + if (ZSTD_isError(result)) { |
| 146 | + rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(result)); |
| 147 | + } |
| 148 | + } |
| 149 | + return obj; |
| 150 | +} |
| 151 | + |
| 152 | +static VALUE |
| 153 | +rb_streaming_compress_flush(VALUE obj) |
| 154 | +{ |
| 155 | + struct streaming_compress_t* sc; |
| 156 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 157 | + VALUE result = no_compress(sc, ZSTD_e_flush); |
| 158 | + return result; |
| 159 | +} |
| 160 | + |
| 161 | +static VALUE |
| 162 | +rb_streaming_compress_finish(VALUE obj) |
| 163 | +{ |
| 164 | + struct streaming_compress_t* sc; |
| 165 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 166 | + VALUE result = no_compress(sc, ZSTD_e_end); |
| 167 | + return result; |
| 168 | +} |
| 169 | + |
| 170 | +extern VALUE rb_mZstd, cStreamingCompress; |
| 171 | +void |
| 172 | +zstd_ruby_streaming_compress_init(void) |
| 173 | +{ |
| 174 | + VALUE cStreamingCompress = rb_define_class_under(rb_mZstd, "StreamingCompress", rb_cObject); |
| 175 | + rb_define_alloc_func(cStreamingCompress, rb_streaming_compress_allocate); |
| 176 | + rb_define_method(cStreamingCompress, "initialize", rb_streaming_compress_initialize, -1); |
| 177 | + rb_define_method(cStreamingCompress, "compress", rb_streaming_compress_compress, 1); |
| 178 | + rb_define_method(cStreamingCompress, "<<", rb_streaming_compress_addstr, 1); |
| 179 | + rb_define_method(cStreamingCompress, "flush", rb_streaming_compress_flush, 0); |
| 180 | + rb_define_method(cStreamingCompress, "finish", rb_streaming_compress_finish, 0); |
| 181 | + |
| 182 | + rb_define_const(cStreamingCompress, "CONTINUE", INT2FIX(ZSTD_e_continue)); |
| 183 | + rb_define_const(cStreamingCompress, "FLUSH", INT2FIX(ZSTD_e_flush)); |
| 184 | + rb_define_const(cStreamingCompress, "END", INT2FIX(ZSTD_e_end)); |
| 185 | +} |
| 186 | + |
0 commit comments