Class: Zstdlib::Inflate

Inherits:
ZStream show all
Defined in:
ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.2/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.2/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.1/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c,
ext/zstdlib_c/ruby/zlib-3.0/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.7/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.7/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.6/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.6/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.5/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.5/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.4/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.4/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.3/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.3/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.2/zstdlib.c,
ext/zstdlib_c/ruby/zlib-2.2/zstdlib.c

Overview

Zlib:Inflate is the class for decompressing compressed data. Unlike Zstdlib::Deflate, an instance of this class is not able to duplicate (clone, dup) itself.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZStream

#adler, #avail_in, #avail_out, #avail_out=, #close, #closed?, #data_type, #end, #ended?, #finish, #finished?, #flush_next_in, #flush_next_out, #reset, #stream_end?, #total_in, #total_out

Constructor Details

#initialize(*args) ⇒ Object

call-seq: Zstdlib::Inflate.new(window_bits = Zstdlib::MAX_WBITS)

Creates a new inflate stream for decompression. +window_bits+ sets the size of the history buffer and can have the following values:

0:: Have inflate use the window size from the zlib header of the compressed stream.

(8..15):: Overrides the window size of the inflate header in the compressed stream. The window size must be greater than or equal to the window size of the compressed stream.

Greater than 15:: Add 32 to window_bits to enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format (a Zstdlib::DataError will be raised for a non-gzip stream).

(-8..-15):: Enables raw deflate mode which will not generate a check value, and will not look for any check values for comparison at the end of the stream.

This is for use with other formats that use the deflate compressed data format such as zip which provide their own check values.

== Example

open "compressed.file" do |compressed_io| zi = Zstdlib::Inflate.new(Zstdlib::MAX_WBITS + 32)

begin
  open "uncompressed.file", "w+" do |uncompressed_io|
    uncompressed_io << zi.inflate(compressed_io.read)
  end
ensure
  zi.close
end

end



2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2006

static VALUE
rb_inflate_initialize(int argc, VALUE *argv, VALUE obj)
{
    struct zstream *z;
    VALUE wbits;
    int err;

    rb_scan_args(argc, argv, "01", &wbits);
    TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);

    err = inflateInit2(&z->stream, ARG_WBITS(wbits));
    if (err != Z_OK) {
	raise_zlib_error(err, z->stream.msg);
    }
    ZSTREAM_READY(z);

    return obj;
}

Class Method Details

.inflate(src) ⇒ Object

call-seq: Zstdlib.inflate(string) Zstdlib::Inflate.inflate(string)

Decompresses +string+. Raises a Zstdlib::NeedDict exception if a preset dictionary is needed for decompression.

This method is almost equivalent to the following code:

def inflate(string) zstream = Zstdlib::Inflate.new buf = zstream.inflate(string) zstream.finish zstream.close buf end

See also Zstdlib.deflate



2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2059

static VALUE
rb_inflate_s_inflate(VALUE obj, VALUE src)
{
    struct zstream z;
    VALUE dst, args[2];
    int err;

    StringValue(src);
    zstream_init_inflate(&z);
    err = inflateInit(&z.stream);
    if (err != Z_OK) {
	raise_zlib_error(err, z.stream.msg);
    }
    ZSTREAM_READY(&z);

    args[0] = (VALUE)&z;
    args[1] = src;
    dst = rb_ensure(inflate_run, (VALUE)args, zstream_ensure_end, (VALUE)&z);

    return dst;
}

Instance Method Details

#<<(string) ⇒ Object

Inputs +string+ into the inflate stream just like Zstdlib::Inflate#inflate, but returns the Zstdlib::Inflate object itself. The output from the stream is preserved in output buffer.



2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2225

static VALUE
rb_inflate_addstr(VALUE obj, VALUE src)
{
    struct zstream *z = get_zstream(obj);

    if (ZSTREAM_IS_FINISHED(z)) {
	if (!NIL_P(src)) {
	    StringValue(src);
	    zstream_append_buffer2(z, src);
	}
    }
    else {
	do_inflate(z, src);
	if (ZSTREAM_IS_FINISHED(z)) {
	    zstream_passthrough_input(z);
	}
    }

    return obj;
}

#add_dictionary(dictionary) ⇒ Object

call-seq: add_dictionary(string)

Provide the inflate stream with a dictionary that may be required in the future. Multiple dictionaries may be provided. The inflate stream will automatically choose the correct user-provided dictionary based on the stream's required dictionary.



2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2103

static VALUE
rb_inflate_add_dictionary(VALUE obj, VALUE dictionary)
{
    VALUE dictionaries = rb_ivar_get(obj, id_dictionaries);
    VALUE checksum = do_checksum(1, &dictionary, adler32);

    rb_hash_aset(dictionaries, checksum, dictionary);

    return obj;
}

#inflate(src) ⇒ Object

call-seq: inflate(deflate_string) -> String inflate(deflate_string) { |chunk| ... } -> nil

Inputs +deflate_string+ into the inflate stream and returns the output from the stream. Calling this method, both the input and the output buffer of the stream are flushed. If string is +nil+, this method finishes the stream, just like Zstdlib::ZStream#finish.

If a block is given consecutive inflated chunks from the +deflate_string+ are yielded to the block and +nil+ is returned.

Raises a Zstdlib::NeedDict exception if a preset dictionary is needed to decompress. Set the dictionary by Zstdlib::Inflate#set_dictionary and then call this method again with an empty string to flush the stream:

inflater = Zstdlib::Inflate.new

begin out = inflater.inflate compressed rescue Zstdlib::NeedDict # ensure the dictionary matches the stream's required dictionary raise unless inflater.adler == Zstdlib.adler32(dictionary)

inflater.set_dictionary dictionary
inflater.inflate ''

end

# ...

inflater.close

See also Zstdlib::Inflate.new



2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2160

static VALUE
rb_inflate_inflate(int argc, VALUE* argv, VALUE obj)
{
    struct zstream *z = get_zstream(obj);
    VALUE dst, src, opts, buffer = Qnil;

    if (OPTHASH_GIVEN_P(opts)) {
        VALUE buf;
        rb_get_kwargs(opts, &id_buffer, 0, 1, &buf);
        if (buf != Qundef && buf != Qnil) {
            buffer = StringValue(buf);
        }
    }
    if (buffer != Qnil) {
        if (!(ZSTREAM_REUSE_BUFFER_P(z) && z->buf == buffer)) {
            long len = RSTRING_LEN(buffer);
            if (len >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
                rb_str_modify(buffer);
            }
            else {
                len = ZSTREAM_AVAIL_OUT_STEP_MAX - len;
                rb_str_modify_expand(buffer, len);
            }
            rb_str_set_len(buffer, 0);
            z->flags |= ZSTREAM_REUSE_BUFFER;
            z->buf = buffer;
        }
    } else if (ZSTREAM_REUSE_BUFFER_P(z)) {
        z->flags &= ~ZSTREAM_REUSE_BUFFER;
        z->buf = Qnil;
    }
    rb_scan_args(argc, argv, "10", &src);

    if (ZSTREAM_IS_FINISHED(z)) {
	if (NIL_P(src)) {
	    dst = zstream_detach_buffer(z);
	}
	else {
	    StringValue(src);
	    zstream_append_buffer2(z, src);
	    if (ZSTREAM_REUSE_BUFFER_P(z)) {
                dst = rb_str_resize(buffer, 0);
            } else {
                dst = rb_str_new(0, 0);
            }
	}
    }
    else {
	do_inflate(z, src);
	dst = zstream_detach_buffer(z);
	if (ZSTREAM_IS_FINISHED(z)) {
	    zstream_passthrough_input(z);
	}
    }

    return dst;
}

#set_dictionary(dic) ⇒ Object

Sets the preset dictionary and returns +string+. This method is available just only after a Zstdlib::NeedDict exception was raised. See zlib.h for details.



2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2293

static VALUE
rb_inflate_set_dictionary(VALUE obj, VALUE dic)
{
    struct zstream *z = get_zstream(obj);
    VALUE src = dic;
    int err;

    StringValue(src);
    err = inflateSetDictionary(&z->stream,
			       (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
    if (err != Z_OK) {
	raise_zlib_error(err, z->stream.msg);
    }

    return dic;
}

#sync(string) ⇒ Object

Inputs +string+ into the end of input buffer and skips data until a full flush point can be found. If the point is found in the buffer, this method flushes the buffer and returns false. Otherwise it returns +true+ and the following data of full flush point is preserved in the buffer.



2254
2255
2256
2257
2258
2259
2260
2261
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2254

static VALUE
rb_inflate_sync(VALUE obj, VALUE src)
{
    struct zstream *z = get_zstream(obj);

    StringValue(src);
    return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src));
}

#sync_point?Boolean

Quoted verbatim from original documentation:

What is this?

:)

Returns:

  • (Boolean)


2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
# File 'ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c', line 2270

static VALUE
rb_inflate_sync_point_p(VALUE obj)
{
    struct zstream *z = get_zstream(obj);
    int err;

    err = inflateSyncPoint(&z->stream);
    if (err == 1) {
	return Qtrue;
    }
    if (err != Z_OK) {
	raise_zlib_error(err, z->stream.msg);
    }
    return Qfalse;
}