yaz0 0.3.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/ext/yaz0/compress.c DELETED
@@ -1,319 +0,0 @@
1
- /*
2
- * Yaz0
3
- *
4
- * Compress yaz0 files
5
- */
6
-
7
- #include <stdio.h>
8
- #include <stdlib.h>
9
- #include <stdint.h>
10
- #include <string.h>
11
-
12
- #include "yaz0.h"
13
-
14
- #define RUN_NONE 0
15
- #define RUN_DATA 1
16
- #define RUN_REF 2
17
-
18
- typedef struct
19
- {
20
- uint8_t type;
21
- union {
22
- uint8_t data;
23
- struct
24
- {
25
- uint16_t offset;
26
- uint16_t len;
27
- };
28
- };
29
- } Yaz0Run;
30
-
31
- static void makeRunNone(Yaz0Run *r)
32
- {
33
- r->type = RUN_NONE;
34
- }
35
-
36
- static void makeRunData(Yaz0Run *r, uint8_t data)
37
- {
38
- r->type = RUN_DATA;
39
- r->data = data;
40
- }
41
-
42
- static void makeRunRef(Yaz0Run *r, uint16_t offset, uint16_t len)
43
- {
44
- r->type = RUN_REF;
45
- r->offset = offset;
46
- r->len = len;
47
- }
48
-
49
- typedef struct
50
- {
51
- Yaz0Run r[8];
52
- uint32_t len;
53
- uint8_t group;
54
- } Yaz0Chunk;
55
-
56
- static void makeChunkEmpty(Yaz0Chunk *c)
57
- {
58
- for (int i = 0; i < 8; ++i)
59
- {
60
- makeRunNone(&(c->r[i]));
61
- }
62
- c->len = 0;
63
- c->group = 0;
64
- }
65
-
66
- typedef struct
67
- {
68
- Yaz0Buffer *dst;
69
- const char *data;
70
- uint32_t dataSize;
71
- int32_t inCursor;
72
- } Yaz0Compressor;
73
-
74
- static int runCost(const Yaz0Run *run)
75
- {
76
- switch (run->type)
77
- {
78
- case RUN_NONE:
79
- return 1000;
80
- case RUN_DATA:
81
- return 0;
82
- case RUN_REF:
83
- return ((run->len >= 0x12) ? 3 : 2) - run->len;
84
- }
85
- return 0;
86
- }
87
-
88
- static int chunkCost(const Yaz0Chunk *ch)
89
- {
90
- int acc;
91
-
92
- acc = 0;
93
- for (int i = 0; i < 8; ++i)
94
- {
95
- acc += runCost(ch->r + i);
96
- }
97
-
98
- return acc;
99
- }
100
-
101
- static void bestChunk(Yaz0Chunk *dst, const Yaz0Chunk *a, const Yaz0Chunk *b)
102
- {
103
- const Yaz0Chunk *src = chunkCost(a) <= chunkCost(b) ? a : b;
104
- if (src != dst)
105
- {
106
- memcpy(dst, src, sizeof(*dst));
107
- }
108
- }
109
-
110
- static void bestRun(Yaz0Run *dst, const Yaz0Run *a, const Yaz0Run *b)
111
- {
112
- const Yaz0Run *src = (runCost(a) <= runCost(b)) ? a : b;
113
- if (src != dst)
114
- {
115
- memcpy(dst, src, sizeof(*dst));
116
- }
117
- }
118
-
119
- static uint32_t runLength(const Yaz0Run *r)
120
- {
121
- switch (r->type)
122
- {
123
- case RUN_NONE:
124
- return 0;
125
- case RUN_DATA:
126
- return 1;
127
- case RUN_REF:
128
- return r->len;
129
- }
130
- return 0;
131
- }
132
-
133
- static int makeMatchPattern(Yaz0Run *run, Yaz0Compressor *compressor, int len)
134
- {
135
- const char *pattern = compressor->data + compressor->inCursor;
136
- int cursorBase = (int)compressor->inCursor - 0x1000;
137
- int cursorMax = (int)compressor->inCursor - len;
138
-
139
- if (compressor->inCursor + len > compressor->dataSize)
140
- return 0;
141
-
142
- if (cursorBase < 0)
143
- cursorBase = 0;
144
-
145
- if (cursorMax < 0)
146
- return 0;
147
-
148
- for (int i = cursorMax; i >= cursorBase; --i)
149
- {
150
- if (memcmp(compressor->data + i, pattern, len) == 0)
151
- {
152
- makeRunRef(run, compressor->inCursor - i, len);
153
- return 1;
154
- }
155
- }
156
- return 0;
157
- }
158
-
159
- static void refineMatch(Yaz0Compressor *compressor, Yaz0Run *run)
160
- {
161
- if (run->type != RUN_REF)
162
- return;
163
-
164
- if (run->offset != run->len)
165
- return;
166
-
167
- int initialLen = run->len;
168
-
169
- for (;;)
170
- {
171
- if (run->len == 0x111)
172
- return;
173
- if (compressor->inCursor + run->len == compressor->dataSize)
174
- return;
175
- if (compressor->data[compressor->inCursor + run->len] != compressor->data[compressor->inCursor + (run->len % initialLen)])
176
- return;
177
- run->len++;
178
- }
179
- }
180
-
181
- static void makeMatch(Yaz0Run *dst, Yaz0Compressor *compressor)
182
- {
183
- Yaz0Run run;
184
-
185
- for (int i = 1; i <= 0x111; ++i)
186
- {
187
- if (!makeMatchPattern(&run, compressor, i))
188
- break;
189
- refineMatch(compressor, &run);
190
- bestRun(dst, &run, dst);
191
- }
192
- }
193
-
194
- static void makeRun(Yaz0Run *dst, Yaz0Compressor *compressor, int dataOnly)
195
- {
196
- Yaz0Run tmp;
197
-
198
- makeRunNone(dst);
199
- if (compressor->inCursor >= compressor->dataSize)
200
- return;
201
- /* Get the basic data run */
202
- makeRunData(dst, compressor->data[compressor->inCursor]);
203
- if (!dataOnly)
204
- {
205
- makeRunNone(&tmp);
206
- makeMatch(&tmp, compressor);
207
- bestRun(dst, dst, &tmp);
208
- }
209
- }
210
-
211
- static void writeRun(Yaz0Compressor *c, const Yaz0Run *r)
212
- {
213
- uint8_t tmp8;
214
- uint16_t tmp16;
215
-
216
- switch (r->type)
217
- {
218
- case RUN_NONE:
219
- break;
220
- case RUN_DATA:
221
- yaz0BufferWrite(c->dst, &r->data, 1);
222
- break;
223
- case RUN_REF:
224
- tmp16 = (r->offset - 1);
225
- if (r->len <= 0x11)
226
- {
227
- tmp16 |= ((((uint16_t)r->len) - 2) << 12);
228
- tmp16 = swap16(tmp16);
229
- yaz0BufferWrite(c->dst, &tmp16, 2);
230
- }
231
- else
232
- {
233
- tmp16 = swap16(tmp16);
234
- yaz0BufferWrite(c->dst, &tmp16, 2);
235
- tmp8 = (r->len - 0x12);
236
- yaz0BufferWrite(c->dst, &tmp8, 1);
237
- }
238
- break;
239
- }
240
- }
241
-
242
- static void makeChunkCandidate(Yaz0Chunk *dst, Yaz0Compressor *c, uint8_t mask)
243
- {
244
- uint32_t cursor;
245
-
246
- cursor = c->inCursor;
247
- makeChunkEmpty(dst);
248
- for (int i = 0; i < 8; ++i)
249
- {
250
- makeRun(dst->r + i, c, mask & (1 << (7 - i)));
251
- if (dst->r[i].type == RUN_DATA)
252
- {
253
- dst->group |= (1 << (7 - i));
254
- }
255
- c->inCursor += runLength(dst->r + i);
256
- dst->len += runLength(dst->r + i);
257
- }
258
-
259
- c->inCursor = cursor;
260
- }
261
-
262
- static void makeChunk(Yaz0Compressor *c)
263
- {
264
- Yaz0Chunk ch;
265
- Yaz0Chunk ch2;
266
-
267
- makeChunkCandidate(&ch, c, 0x00);
268
- makeChunkCandidate(&ch2, c, 0x01);
269
- bestChunk(&ch, &ch, &ch2);
270
- makeChunkCandidate(&ch2, c, 0x02);
271
- bestChunk(&ch, &ch, &ch2);
272
- makeChunkCandidate(&ch2, c, 0x04);
273
- bestChunk(&ch, &ch, &ch2);
274
- makeChunkCandidate(&ch2, c, 0x08);
275
- bestChunk(&ch, &ch, &ch2);
276
- makeChunkCandidate(&ch2, c, 0x10);
277
- bestChunk(&ch, &ch, &ch2);
278
- makeChunkCandidate(&ch2, c, 0x20);
279
- bestChunk(&ch, &ch, &ch2);
280
- makeChunkCandidate(&ch2, c, 0x40);
281
- bestChunk(&ch, &ch, &ch2);
282
- makeChunkCandidate(&ch2, c, 0x80);
283
- bestChunk(&ch, &ch, &ch2);
284
-
285
- yaz0BufferWrite(c->dst, &ch.group, 1);
286
- for (int i = 0; i < 8; ++i)
287
- {
288
- writeRun(c, ch.r + i);
289
- }
290
- c->inCursor += ch.len;
291
- }
292
-
293
- static void writeHeader(Yaz0Compressor *c)
294
- {
295
- uint32_t tmp;
296
-
297
- yaz0BufferWrite(c->dst, "Yaz0", 4);
298
- tmp = swap32(c->dataSize);
299
- yaz0BufferWrite(c->dst, &tmp, 4);
300
- tmp = 0;
301
- yaz0BufferWrite(c->dst, &tmp, 4);
302
- yaz0BufferWrite(c->dst, &tmp, 4);
303
- }
304
-
305
- int yaz0Compress(Yaz0Buffer *dst, const char *data, size_t len)
306
- {
307
- Yaz0Compressor compressor;
308
-
309
- compressor.dst = dst;
310
- compressor.data = data;
311
- compressor.dataSize = len;
312
- compressor.inCursor = 0;
313
-
314
- writeHeader(&compressor);
315
-
316
- while (compressor.inCursor < compressor.dataSize)
317
- makeChunk(&compressor);
318
- return 1;
319
- }
@@ -1,65 +0,0 @@
1
- #include <stdint.h>
2
- #include <stdlib.h>
3
- #include <string.h>
4
- #include "yaz0.h"
5
-
6
- int yaz0Decompress(Yaz0Buffer *dst, const char *d, size_t dataLen)
7
- {
8
- const uint8_t *data;
9
- uint32_t dstSize;
10
- unsigned inCursor;
11
- uint8_t group;
12
- int len;
13
- int rrr;
14
-
15
- data = d;
16
- if (dataLen < 0x10)
17
- return 0;
18
- if (memcmp(data, "Yaz0", 4) != 0)
19
- return 0;
20
-
21
- dstSize = *(uint32_t *)(data + 4);
22
- dstSize = swap32(dstSize);
23
-
24
- inCursor = 0x10;
25
- dst->data = realloc(dst->data, dstSize);
26
- dst->capacity = dstSize;
27
-
28
- while (inCursor < dataLen && dst->size < dstSize)
29
- {
30
- group = data[inCursor++];
31
- for (int b = 0; b < 8; ++b)
32
- {
33
- if (inCursor >= dataLen || dst->size >= dstSize)
34
- break;
35
-
36
- if (group & (1 << (7 - b)))
37
- {
38
- /* Direct data */
39
- dst->data[dst->size++] = data[inCursor++];
40
- }
41
- else
42
- {
43
- if (data[inCursor] & 0xf0)
44
- {
45
- len = (data[inCursor] >> 4) + 2;
46
- rrr = (((data[inCursor] & 0xf) << 8) | (data[inCursor + 1])) + 1;
47
- inCursor += 2;
48
- }
49
- else
50
- {
51
- rrr = (((data[inCursor] & 0xf) << 8) | (data[inCursor + 1])) + 1;
52
- len = (data[inCursor + 2]) + 0x12;
53
- inCursor += 3;
54
- }
55
- for (int i = 0; i < len; ++i)
56
- {
57
- dst->data[dst->size] = dst->data[dst->size - rrr];
58
- dst->size++;
59
- }
60
- }
61
- }
62
- }
63
-
64
- return 1;
65
- }
data/ext/yaz0/yaz0.c DELETED
@@ -1,48 +0,0 @@
1
- #include "yaz0.h"
2
- #include <stdio.h>
3
-
4
- static VALUE compress(VALUE self, VALUE str)
5
- {
6
- Yaz0Buffer buffer;
7
- VALUE ret;
8
-
9
- if (TYPE(str) != T_STRING)
10
- {
11
- rb_raise(rb_eTypeError, "Expected a string");
12
- return Qnil;
13
- }
14
-
15
- yaz0BufferAlloc(&buffer, 16);
16
- yaz0Compress(&buffer, StringValuePtr(str), RSTRING_LEN(str));
17
- ret = rb_str_new(buffer.data, buffer.size);
18
- yaz0BufferFree(&buffer);
19
-
20
- return ret;
21
- }
22
-
23
- static VALUE decompress(VALUE self, VALUE str)
24
- {
25
- Yaz0Buffer buffer;
26
- VALUE ret;
27
-
28
- if (TYPE(str) != T_STRING)
29
- {
30
- rb_raise(rb_eTypeError, "Expected a string");
31
- return Qnil;
32
- }
33
-
34
- yaz0BufferAlloc(&buffer, 16);
35
- yaz0Decompress(&buffer, StringValuePtr(str), RSTRING_LEN(str));
36
- ret = rb_str_new(buffer.data, buffer.size);
37
- yaz0BufferFree(&buffer);
38
-
39
- return ret;
40
- }
41
-
42
- void Init_yaz0(void)
43
- {
44
- VALUE mod;
45
- mod = rb_define_module("Yaz0");
46
- rb_define_module_function(mod, "compress", &compress, 1);
47
- rb_define_module_function(mod, "decompress", &decompress, 1);
48
- }
data/ext/yaz0/yaz0.h DELETED
@@ -1,38 +0,0 @@
1
- #ifndef YAZ0_H
2
- #define YAZ0_H 1
3
-
4
- #include <stddef.h>
5
- #include "ruby.h"
6
-
7
- /*
8
- * Swap
9
- */
10
- inline static uint16_t swap16(uint16_t v)
11
- {
12
- return (v << 8) | (v >> 8);
13
- }
14
-
15
- inline static uint32_t swap32(uint32_t v)
16
- {
17
- return (v << 24) | ((v << 8) & 0x00ff0000) | ((v >> 8) & 0x0000ff00) | (v >> 24);
18
- }
19
-
20
- /*
21
- * Buffer
22
- */
23
-
24
- typedef struct
25
- {
26
- size_t size;
27
- size_t capacity;
28
- char *data;
29
- } Yaz0Buffer;
30
-
31
- void yaz0BufferAlloc(Yaz0Buffer *buf, size_t cap);
32
- void yaz0BufferFree(Yaz0Buffer *buf);
33
- void yaz0BufferWrite(Yaz0Buffer *buf, const void *data, size_t len);
34
-
35
- int yaz0Compress(Yaz0Buffer *dst, const char *data, size_t len);
36
- int yaz0Decompress(Yaz0Buffer *dst, const char *data, size_t len);
37
-
38
- #endif /* YAZ0_H */