Module: Libdeflate

Defined in:
lib/libdeflate/version.rb,
ext/libdeflate/libdeflate_ext.c

Defined Under Namespace

Classes: BadDataError, Compressor, Decompressor, Error

Constant Summary collapse

VERSION =
'0.2.0'.freeze
DEFAULT_COMPRESSION =

Default compression level which is a compromise between speed and compression ratio

See Compressor#initialize.

INT2FIX(DEFAULT_COMPRESSION)
DEFLATE =

DEFLATE compressed data format

See Compressor#compress and Decompressor#decompress.

INT2FIX(FORMAT_DEFLATE)
ZLIB =

ZLIB compressed data format

See Compressor#compress and Decompressor#decompress.

INT2FIX(FORMAT_ZLIB)
GZIP =

GZIP compressed data format

See Compressor#compress and Decompressor#decompress.

INT2FIX(FORMAT_GZIP)

Class Method Summary collapse

Class Method Details

.adler32(str = nil, adler = nil) ⇒ Integer

Updates an Adler-32 checksum with str. If str is omitted, it returns the initial value of Adler-32 checksum. If adler is omitted, it assumes that the initial value of Adler-32 checksum is given.

Libdeflate.adler32                                #=> 1
Libdeflate.adler32('foo')                         #=> 42074437
Libdeflate.adler32('oo', Libdeflate.adler32('f')) #=> 42074437

Returns:

  • (Integer)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/libdeflate/libdeflate_ext.c', line 23

static VALUE
rb_libdeflate_adler32(int argc, VALUE *argv, VALUE self)
{
    VALUE str, adler;
    unsigned long checksum;

    rb_scan_args(argc, argv, "02", &str, &adler);

    if (!NIL_P(adler)) {
        checksum = NUM2ULONG(adler);
    } else if (!NIL_P(str)) {
        checksum = libdeflate_adler32(0, NULL, 0);
    } else {
        checksum = 0;
    }

    if (NIL_P(str)) {
        checksum = libdeflate_adler32(checksum, NULL, 0);
    } else {
        StringValue(str);
        checksum = libdeflate_adler32(checksum, RSTRING_PTR(str), RSTRING_LEN(str));
    }

    return ULONG2NUM(checksum);
}

.crc32(str = nil, crc = nil) ⇒ Integer

Updates a CRC-32 checksum with str. If str is omitted, it returns the initial value of CRC-32 checksum. If crc is omitted, it assumes that the initial value of CRC-32 checksum is given.

Libdeflate.crc32                              #=> 0
Libdeflate.crc32('foo')                       #=> 2356372769
Libdeflate.crc32('oo', Libdeflate.crc32('f')) #=> 2356372769

Returns:

  • (Integer)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/libdeflate/libdeflate_ext.c', line 61

static VALUE
rb_libdeflate_crc32(int argc, VALUE *argv, VALUE self)
{
    VALUE str, crc;
    unsigned long checksum;

    rb_scan_args(argc, argv, "02", &str, &crc);

    if (!NIL_P(crc)) {
        checksum = NUM2ULONG(crc);
    } else if (!NIL_P(str)) {
        checksum = libdeflate_crc32(0, NULL, 0);
    } else {
        checksum = 0;
    }

    if (NIL_P(str)) {
        checksum = libdeflate_crc32(checksum, NULL, 0);
    } else {
        StringValue(str);
        checksum = libdeflate_crc32(checksum, RSTRING_PTR(str), RSTRING_LEN(str));
    }

    return ULONG2NUM(checksum);
}