|
| 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 | + |
| 12 | +static void |
| 13 | +streaming_compress_mark(void *p) |
| 14 | +{ |
| 15 | + struct streaming_compress_t *sc = p; |
| 16 | + rb_gc_mark(sc->buf); |
| 17 | + rb_gc_mark(sc->buf_size); |
| 18 | + rb_gc_mark(sc->pos); |
| 19 | +} |
| 20 | + |
| 21 | +static void |
| 22 | +streaming_compress_free(void *p) |
| 23 | +{ |
| 24 | + struct streaming_compress_t *sc = p; |
| 25 | + ZSTD_CCtx* ctx = sc->ctx; |
| 26 | + if (ctx != NULL) { |
| 27 | + ZSTD_freeCCtx(ctx); |
| 28 | + } |
| 29 | + xfree(sc); |
| 30 | +} |
| 31 | + |
| 32 | +static size_t |
| 33 | +streaming_compress_memsize(const void *p) |
| 34 | +{ |
| 35 | + /* n.b. this does not track memory managed via zalloc/zfree callbacks */ |
| 36 | + return sizeof(struct streaming_compress_t); |
| 37 | +} |
| 38 | + |
| 39 | +static const rb_data_type_t streaming_compress_type = { |
| 40 | + "streaming_compress", |
| 41 | + { streaming_compress_mark, streaming_compress_free, streaming_compress_memsize, }, |
| 42 | + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY |
| 43 | +}; |
| 44 | + |
| 45 | +static VALUE |
| 46 | +rb_streaming_compress_allocate(VALUE klass) |
| 47 | +{ |
| 48 | + struct streaming_compress_t* sc; |
| 49 | + VALUE obj = TypedData_Make_Struct(klass, struct streaming_compress_t, &streaming_compress_type, sc); |
| 50 | + sc->ctx = NULL; |
| 51 | + sc->buf = Qnil; |
| 52 | + sc->buf_size = 0; |
| 53 | + sc->pos = 0; |
| 54 | + return obj; |
| 55 | +} |
| 56 | + |
| 57 | +static VALUE |
| 58 | +rb_streaming_compress_initialize(int argc, VALUE *argv, VALUE obj) |
| 59 | +{ |
| 60 | + struct streaming_compress_t* sc; |
| 61 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 62 | + size_t const buffOutSize = ZSTD_CStreamOutSize(); |
| 63 | + |
| 64 | + ZSTD_CCtx* ctx = ZSTD_createCCtx(); |
| 65 | + if (ctx == NULL) { |
| 66 | + rb_raise(rb_eRuntimeError, "%s", "ZSTD_createCCtx error"); |
| 67 | + } |
| 68 | + ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, 1); |
| 69 | + sc->ctx = ctx; |
| 70 | + sc->buf = rb_str_new(NULL, buffOutSize); |
| 71 | + sc->buf_size = buffOutSize; |
| 72 | + |
| 73 | + return obj; |
| 74 | +} |
| 75 | + |
| 76 | +static VALUE |
| 77 | +rb_streaming_compress_compress(VALUE obj, VALUE src) |
| 78 | +{ |
| 79 | + StringValue(src); |
| 80 | + const char* input_data = RSTRING_PTR(src); |
| 81 | + size_t input_size = RSTRING_LEN(src); |
| 82 | + ZSTD_inBuffer input = { input_data, input_size, 0 }; |
| 83 | + |
| 84 | + struct streaming_compress_t* sc; |
| 85 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 86 | + const char* output_data = RSTRING_PTR(sc->buf); |
| 87 | + ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 }; |
| 88 | + |
| 89 | + size_t const result = ZSTD_compressStream2(sc->ctx, &output, &input, ZSTD_e_continue); |
| 90 | + if (ZSTD_isError(result)) { |
| 91 | + rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(result)); |
| 92 | + } |
| 93 | + sc->pos += output.pos; |
| 94 | + return obj; |
| 95 | +} |
| 96 | + |
| 97 | +static VALUE |
| 98 | +rb_streaming_compress_finish(VALUE obj) |
| 99 | +{ |
| 100 | + ZSTD_inBuffer input = { NULL, 0, 0 }; |
| 101 | + |
| 102 | + struct streaming_compress_t* sc; |
| 103 | + TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); |
| 104 | + const char* output_data = RSTRING_PTR(sc->buf); |
| 105 | + ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 }; |
| 106 | + |
| 107 | + size_t const result = ZSTD_compressStream2(sc->ctx, &output, &input, ZSTD_e_end); |
| 108 | + if (ZSTD_isError(result)) { |
| 109 | + rb_raise(rb_eRuntimeError, "finish error error code: %s", ZSTD_getErrorName(result)); |
| 110 | + } |
| 111 | + sc->pos += output.pos; |
| 112 | + rb_str_resize(sc->buf, sc->pos); |
| 113 | + return sc->buf; |
| 114 | +} |
| 115 | + |
| 116 | +extern VALUE rb_mZstd, cStreamingCompress; |
| 117 | +void |
| 118 | +zstd_ruby_streaming_compress_init(void) |
| 119 | +{ |
| 120 | + VALUE cStreamingCompress = rb_define_class_under(rb_mZstd, "StreamingCompress", rb_cObject); |
| 121 | + rb_define_alloc_func(cStreamingCompress, rb_streaming_compress_allocate); |
| 122 | + rb_define_method(cStreamingCompress, "initialize", rb_streaming_compress_initialize, -1); |
| 123 | + rb_define_method(cStreamingCompress, "<<", rb_streaming_compress_compress, 1); |
| 124 | + rb_define_method(cStreamingCompress, "finish", rb_streaming_compress_finish, 0); |
| 125 | +} |
| 126 | + |
0 commit comments