Module: LZO

Defined in:
ext/lzoruby.c

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
rb_str_new2(VERSION)
LZO_VERSION =
rb_str_new2(lzo_version_string())

Class Method Summary collapse

Class Method Details

.adler32(v_adler, v_buf) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'ext/lzoruby.c', line 119

static VALUE lzoruby_adler32(VALUE self, VALUE v_adler, VALUE v_buf) {
  lzo_uint32 adler;
  lzo_bytep buf;
  lzo_uint len;

  Check_Type(v_adler, T_FIXNUM);
  Check_Type(v_buf, T_STRING);
  adler = FIX2LONG(v_adler);
  buf = RSTRING_PTR(v_buf);
  len = RSTRING_LEN(v_buf);

  adler = lzo_adler32(adler, buf, len);

  return LONG2FIX(adler);
}

.compress(*args) ⇒ Object



30
31
32
33
34
35
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
67
68
69
70
71
72
73
74
# File 'ext/lzoruby.c', line 30

static VALUE lzoruby_compress(int argc, const VALUE *argv, VALUE self) {
  const lzo_bytep in;
  lzo_bytep out;
  lzo_voidp wrkmem = NULL;
  lzo_uint in_len;
  lzo_uint out_len;
  lzo_uint new_len;
  VALUE v_in, v_out, v_level;
  int level, err;

  rb_scan_args(argc, argv, "11", &v_in, &v_level);
  Check_Type(v_in, T_STRING);
  level = NIL_P(v_level) ? 1 : NUM2INT(v_level);

  in = RSTRING_PTR(v_in);
  in_len = RSTRING_LEN(v_in);
  out_len = in_len + in_len / 64 + 16 + 3;
  out = xmalloc(5 + out_len);
  new_len = out_len;
  wrkmem = xmalloc((level == 1) ? LZO1X_1_MEM_COMPRESS : LZO1X_999_MEM_COMPRESS);

  if (level == 1) {
    out[0] = 0xf0;
    err = lzo1x_1_compress(in, in_len, out + 5, &new_len, wrkmem);
  } else {
    out[0] = 0xf1;
    err = lzo1x_999_compress(in, in_len, out + 5, &new_len, wrkmem);
  }

  xfree(wrkmem);

  if (err != LZO_E_OK || new_len > out_len) {
    xfree(out);
    rb_raise(LZO_Error, "Error %d while compressing data", err);
  }

  out[1] = (unsigned char) ((in_len >> 24) & 0xff);
  out[2] = (unsigned char) ((in_len >> 16) & 0xff);
  out[3] = (unsigned char) ((in_len >>  8) & 0xff);
  out[4] = (unsigned char) ((in_len >>  0) & 0xff);
  v_out = rb_str_new(out, 5 + new_len);
  xfree(out);

  return v_out;
}

.decompress(v_in) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/lzoruby.c', line 77

static VALUE lzoruby_decompress(VALUE self, VALUE v_in) {
  lzo_bytep in;
  lzo_bytep out;
  lzo_uint in_len;
  lzo_uint out_len;
  lzo_uint new_len;
  VALUE v_out;
  int err;

  Check_Type(v_in, T_STRING);

  in = RSTRING_PTR(v_in);
  in_len = RSTRING_LEN(v_in);

  if (((int) in_len) < 5 + 3 || in[0] < 0xf0 || in[0] > 0xf1) {
    rb_raise(LZO_Error, "Header error - invalid compressed data");
  }

  in_len -= 5;
  out_len = (in[1] << 24) | (in[2] << 16) | (in[3] << 8) | in[4];

  if (((int) out_len) < 0 || in_len > out_len + out_len / 64 + 16 + 3) {
    rb_raise(LZO_Error, "Header error - invalid compressed data");
  }

  out = xmalloc(out_len);
  new_len = out_len;

  err = lzo1x_decompress_safe(in + 5, in_len, out, &new_len, NULL);

  if (err != LZO_E_OK || new_len != out_len) {
    xfree(out);
    rb_raise(LZO_Error, "Compressed data violation %d", err);
  }

  v_out = rb_str_new(out, new_len);
  xfree(out);

  return v_out;
}