Module: LZF

Defined in:
ext/lzfruby.c

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
rb_str_new2(VERSION)

Class Method Summary collapse

Class Method Details

.compress(*args) ⇒ Object



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/lzfruby.c', line 41

static VALUE lzfruby_compress(int argc, VALUE *argv, VALUE self) {
  VALUE from, to, blocksize;
  unsigned char buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
  unsigned char buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
  unsigned char *header;
  int i_blocksize;

  rb_scan_args(argc, argv, "21", &from, &to, &blocksize);
  Check_IO(from);
  Check_IO(to);

  if (NIL_P(blocksize)) {
    i_blocksize = BLOCKSIZE;
  } else {
    i_blocksize = NUM2INT(blocksize);

    if (blocksize < 1 || MAX_BLOCKSIZE < blocksize) {
      i_blocksize = BLOCKSIZE;
    }
  }

  blocksize = INT2FIX(i_blocksize);

  while (1) {
    VALUE in = rb_funcall(from, rb_intern("read"), 1, blocksize);
    ssize_t us, cs, len;

    if (NIL_P(in) || (us = RSTRING_LEN(in)) < 1) {
      break;
    }

    memcpy(&buf1[MAX_HDR_SIZE], RSTRING_PTR(in), us);
    cs = lzf_compress(&buf1[MAX_HDR_SIZE], us, &buf2[MAX_HDR_SIZE], (us > 4) ? us - 4 : us);

    if (cs) {
      header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE];
      header[0] = 'Z';
      header[1] = 'V';
      header[2] = 1;
      header[3] = cs >> 8;
      header[4] = cs & 0xff;
      header[5] = us >> 8;
      header[6] = us & 0xff;
      len = cs + TYPE1_HDR_SIZE;
    } else {
      header = &buf1[MAX_HDR_SIZE - TYPE0_HDR_SIZE];
      header[0] = 'Z';
      header[1] = 'V';
      header[2] = 0;
      header[3] = us >> 8;
      header[4] = us & 0xff;
      len = us + TYPE0_HDR_SIZE;
    }

    rb_funcall(to, rb_intern("write"), 1, rb_str_new(header, len));
  }

  return Qnil;
}

.decompress(from, to) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
# File 'ext/lzfruby.c', line 102

static VALUE lzfruby_decompress(VALUE self, VALUE from, VALUE to) {
  unsigned char header[MAX_HDR_SIZE];
  unsigned char buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
  unsigned char buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16];
  ssize_t rc, cs, us, bytes, over = 0;
  int l, rd;

  Check_IO(from);
  Check_IO(to);

  while (1) {
    VALUE in, in_header;
    unsigned char *p;

    in_header = rb_funcall(from, rb_intern("read"), 1, INT2FIX(MAX_HDR_SIZE - over));

    if (NIL_P(in_header) || (rc = RSTRING_LEN(in_header)) < 1) {
      break;
    }

    memcpy(header + over, RSTRING_PTR(in_header), MAX_HDR_SIZE - over);
    rc += over;
    over = 0;

    if (header[0] == 0) {
      break;
    }

    if (rc < MIN_HDR_SIZE || header[0] != 'Z' || header[1] != 'V') {
      rb_raise(LZF_Error, "invalid data stream - magic not found or short header");
    }

    switch (header[2]) {
    case 0:
      cs = -1;
      us = (header[3] << 8) | header[4];
      p = &header[TYPE0_HDR_SIZE];
      break;

    case 1:
      if (rc < TYPE1_HDR_SIZE) {
        rb_raise(LZF_Error, "short data");
      }

      cs = (header[3] << 8) | header[4];
      us = (header[5] << 8) | header[6];
      p = &header[TYPE1_HDR_SIZE];
      break;

    default:
      rb_raise(LZF_Error, "unknown blocktype");
    }

    bytes = (cs == -1) ? us : cs;
    l = &header[rc] - p;

    if (l > 0) {
      memcpy(buf1, p, l);
    }

    if (l > bytes) {
      over = l - bytes;
      memmove(header, &p[bytes], over);
    }

    p = &buf1[l];
    rd = bytes - l;

    if (rd > 0) {
      in = rb_funcall(from, rb_intern("read"), 1, INT2FIX(rd));

      if (NIL_P(in) || (rc = RSTRING_LEN(in)) < 1 || rc != rd) {
        rb_raise(LZF_Error, "short data");
      }

      memcpy(p, RSTRING_PTR(in), rc);
    }

    if (cs == -1) {
      rb_funcall(to, rb_intern("write"), 1, rb_str_new(buf1, us)); 
    } else {
      if (lzf_decompress(buf1, cs, buf2, us) != us) {
        rb_raise(LZF_Error, "decompress: invalid stream - data corrupted");
      }

      rb_funcall(to, rb_intern("write"), 1, rb_str_new(buf2, us));
    }
  }

  return Qnil;
}