Class: Libdeflate::Compressor

Inherits:
Object
  • Object
show all
Defined in:
ext/libdeflate/libdeflate_ext.c

Instance Method Summary collapse

Constructor Details

#initialize(level = DEFAULT_COMPRESSION) ⇒ Object

Returns a new Libdeflate::Compressor object. level must be in range from 1 to 12, and defaults to DEFAULT_COMPRESSION.

Libdeflate::Compressor.new #=> #<Libdeflate::Compressor:0x007fad31b672c8>


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'ext/libdeflate/libdeflate_ext.c', line 114

static VALUE
rb_compressor_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE level;
    int compression_level;
    struct libdeflate_compressor *c;

    rb_scan_args(argc, argv, "01", &level);

    compression_level = NIL_P(level) ? DEFAULT_COMPRESSION : FIX2INT(level);

    c = libdeflate_alloc_compressor(compression_level);
    if (c == NULL) {
        rb_raise(rb_eLibdefalteError, "libdeflate_alloc_compressor: compression_level=%d", compression_level);
    }

    DATA_PTR(self) = c;

    return self;
}

Instance Method Details

#compress(str, format = DEFLATE, outbuf = nil) ⇒ String

Compresses the given string into format. Valid values of format are DEFLATE (default), ZLIB and GZIP. If outbuf is given, the resulting compressed data will be written to it.

compressor.compress('foo')                   #=> "\x01\x03\x00\xFC\xFFfoo"
compressor.compress('foo', Libdeflate::ZLIB) #=> "x\x9C\x01\x03\x00\xFC\xFFfoo\x02\x82\x01E"

outbuf = 'bar'
compressor.compress('foo', nil, outbuf) #=> "\x01\x03\x00\xFC\xFFfoo"
outbuf                                  #=> "\x01\x03\x00\xFC\xFFfoo"

Returns:

  • (String)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/libdeflate/libdeflate_ext.c', line 156

static VALUE
rb_compressor_compress(int argc, VALUE *argv, VALUE self)
{
    struct libdeflate_compressor *c = check_compressor(self);
    VALUE str, format, outbuf;
    size_t (*compress_func)(struct libdeflate_compressor *, const void *, size_t, void *, size_t);
    size_t (*compress_bound_func)(struct libdeflate_compressor *, size_t);
    size_t out_nbytes, max_out_nbytes;

    rb_scan_args(argc, argv, "12", &str, &format, &outbuf);

    StringValue(str);

    switch (NIL_P(format) ? FORMAT_DEFLATE : FIX2INT(format)) {
        case FORMAT_DEFLATE:
            compress_func = &libdeflate_deflate_compress;
            compress_bound_func = &libdeflate_deflate_compress_bound;
            break;
        case FORMAT_ZLIB:
            compress_func = &libdeflate_zlib_compress;
            compress_bound_func = &libdeflate_zlib_compress_bound;
            break;
        case FORMAT_GZIP:
            compress_func = &libdeflate_gzip_compress;
            compress_bound_func = &libdeflate_gzip_compress_bound;
            break;
        default:
            rb_raise(rb_eLibdefalteError, "unknown compressed data format: %d", FIX2INT(format));
    }

    if (NIL_P(outbuf)) {
        outbuf = rb_str_buf_new(compress_bound_func(c, RSTRING_LEN(str)));
    } else {
        StringValue(outbuf);
        rb_str_modify(outbuf);
    }

    out_nbytes = compress_func(c,
                               RSTRING_PTR(str),
                               RSTRING_LEN(str),
                               RSTRING_PTR(outbuf),
                               rb_str_capacity(outbuf));

    if (out_nbytes > 0) {
        rb_str_set_len(outbuf, out_nbytes);
        return outbuf;
    }

    max_out_nbytes = compress_bound_func(c, RSTRING_LEN(str));
    if (rb_str_capacity(outbuf) >= max_out_nbytes) {
        rb_raise(rb_eLibdefalteError, "failed to compress data");
    }

    rb_str_modify_expand(outbuf, max_out_nbytes - RSTRING_LEN(outbuf));

    out_nbytes = compress_func(c,
                               RSTRING_PTR(str),
                               RSTRING_LEN(str),
                               RSTRING_PTR(outbuf),
                               rb_str_capacity(outbuf));

    if (out_nbytes == 0) {
        rb_raise(rb_eLibdefalteError, "failed to compress data");
    }

    rb_str_set_len(outbuf, out_nbytes);

    return outbuf;
}