Module: Lzfx

Defined in:
ext/lzfxruby.c

Class Method Summary collapse

Class Method Details

.compress(input) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'ext/lzfxruby.c', line 5

static VALUE rb_lzfx_compress(VALUE self, VALUE input) {
  unsigned char *compressed, *data;
  int out_length, data_length;
  int retcode;
  VALUE retval;

  Check_Type(input, T_STRING);
  data = StringValuePtr(input);
  data_length = RSTRING_LEN(input);

  if (data_length < 1) {
    return Qnil;
  }

  out_length = data_length * COFFICIENT_OF_BUFFER;
  compressed = malloc(out_length);

  retcode = lzfx_compress(data, data_length, compressed, &out_length);

  if(retcode < 0) {
    ruby_xfree(compressed);
    return Qnil;
  }

  retval = rb_str_new(compressed, out_length);

  ruby_xfree(compressed);

  return retval;
}

.decompress(input) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/lzfxruby.c', line 36

static VALUE rb_lzfx_decompress(VALUE self, VALUE input) {
  unsigned char *decompressed, *data;
  int out_length, data_length;
  int retcode;
  VALUE retval;

  Check_Type(input, T_STRING);

  data = RSTRING_PTR(input);
  data_length = RSTRING_LEN(input);

  if (data_length < 1) {
    return Qnil;
  }

  out_length = data_length * COFFICIENT_OF_BUFFER;
  decompressed = malloc(out_length);

  retcode = lzfx_decompress(data, data_length, decompressed, &out_length);

  if(retcode < 0) {
    ruby_xfree(decompressed);
    return Qnil;
  }

  retval = rb_str_new(decompressed, out_length);

  ruby_xfree(decompressed);

  return retval;
}