-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdk_codec.c
More file actions
349 lines (318 loc) · 12.5 KB
/
Copy pathdk_codec.c
File metadata and controls
349 lines (318 loc) · 12.5 KB
1
2
3
4
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
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
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
100
101
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdarg.h>
#include "common.h"
#include "pakka.h"
/* Daikatana custom byte-codec decoder. Reference: yquake2/pakextract
* (BSD-2-Clause) — the only published implementation of this codec.
*
* Opcode table (b is the next input byte):
* b < 64 : literal run of (b + 1) bytes from input
* b < 128 : (b - 62) zero bytes
* b < 192 : (b - 126) copies of the next input byte
* b < 254 : back-reference, length (b - 190), distance read as next
* input byte d, distance = d + 2, copy from
* produced - distance in OUTPUT
* b == 254 : invalid opcode (gap in the table)
* b == 255 : terminator (any trailing input is ignored)
*
* Termination accepts either an explicit 0xFF or clean input
* exhaustion; what matters is that produced == out_len at the end.
* Anything else (partial fill, OOB read/write, back-ref underflow,
* invalid opcode) returns PAKKA_ERR_FORMAT and leaves the output
* buffer's tail uninitialized — callers must treat partial output as
* garbage. */
static pakka_status_t dk_err_fill(pakka_error_t *err,
pakka_status_t status,
const char *fmt, ...) {
int written;
va_list args;
if (err == NULL) {
return status;
}
err->status = status;
err->domain = PAKKA_ERR_DOMAIN_NONE;
err->system_code = 0;
err->entry_name[0] = '\0';
err->entry_name_truncated = 0;
err->entry_index = (size_t)-1;
err->offset = 0;
err->length = 0;
err->message_truncated = 0;
{
static const char op[] = "dk_inflate";
size_t op_len = sizeof(op) - 1;
if (op_len >= PAKKA_OPERATION_SIZE) {
op_len = PAKKA_OPERATION_SIZE - 1;
}
memcpy(err->operation, op, op_len);
err->operation[op_len] = '\0';
}
if (fmt == NULL) {
err->message[0] = '\0';
} else {
va_start(args, fmt);
written = vsnprintf(err->message, PAKKA_MESSAGE_SIZE, fmt, args);
va_end(args);
if (written < 0) {
err->message[0] = '\0';
} else if ((size_t)written >= PAKKA_MESSAGE_SIZE) {
err->message_truncated = 1;
}
}
return status;
}
pakka_status_t pakka_dk_inflate(const unsigned char *in, size_t in_len,
unsigned char *out, size_t out_len,
pakka_error_t *err) {
size_t read = 0;
size_t produced = 0;
unsigned int opcode;
size_t copy_len;
size_t distance;
size_t i;
if ((in == NULL && in_len > 0) || (out == NULL && out_len > 0)) {
return dk_err_fill(err, PAKKA_ERR_INVALID_ARGUMENT,
"NULL buffer with non-zero length");
}
while (read < in_len) {
opcode = in[read++];
if (opcode < 64) {
copy_len = (size_t)opcode + 1;
if (copy_len > in_len - read) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"literal run overruns input "
"(want %zu, have %zu)",
copy_len, in_len - read);
}
if (copy_len > out_len - produced) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"literal run overruns output "
"(want %zu, have %zu)",
copy_len, out_len - produced);
}
memcpy(out + produced, in + read, copy_len);
read += copy_len;
produced += copy_len;
} else if (opcode < 128) {
copy_len = (size_t)opcode - 62;
if (copy_len > out_len - produced) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"zero run overruns output "
"(want %zu, have %zu)",
copy_len, out_len - produced);
}
memset(out + produced, 0, copy_len);
produced += copy_len;
} else if (opcode < 192) {
if (read >= in_len) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"byte-run missing data byte");
}
copy_len = (size_t)opcode - 126;
if (copy_len > out_len - produced) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"byte run overruns output "
"(want %zu, have %zu)",
copy_len, out_len - produced);
}
memset(out + produced, in[read], copy_len);
read++;
produced += copy_len;
} else if (opcode < 254) {
if (read >= in_len) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"back-reference missing distance byte");
}
copy_len = (size_t)opcode - 190;
distance = (size_t)in[read++] + 2;
if (distance > produced) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"back-reference underflow "
"(distance %zu > produced %zu)",
distance, produced);
}
if (copy_len > out_len - produced) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"back-reference overruns output "
"(want %zu, have %zu)",
copy_len, out_len - produced);
}
/* Byte-by-byte so LZ-style overlap (copy_len > distance)
* repeats the run correctly — memcpy/memmove would not. */
for (i = 0; i < copy_len; i++) {
out[produced] = out[produced - distance];
produced++;
}
} else if (opcode == 254) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"invalid opcode 0xFE");
} else {
break; /* 0xFF terminator; trailing input ignored */
}
}
if (produced != out_len) {
return dk_err_fill(err, PAKKA_ERR_FORMAT,
"decoder produced %zu of %zu expected bytes",
produced, out_len);
}
return PAKKA_OK;
}
/* Daikatana byte-codec encoder. Greedy LZSS-style matcher over a
* 256-byte window with three non-literal token classes (zero-run,
* byte-RLE, back-reference) plus a literal-run fallback.
*
* - Zero-run preference via a `zer * 2` tiebreak: a zero-run is a
* 1-byte opcode (no payload), while byte-RLE and back-ref are
* 2-byte opcodes (op + payload), so equal length favours zero-run.
* - Back-ref length is capped at the distance — no-overlap rule.
* Skipping LZ overlap keeps the encoder a single greedy pass.
* - Caps sit one byte inside the decoder envelope (65/65/63 there,
* 64/64/62 here). Length-2 byte-RLE and back-ref break even on
* bytes but split surrounding literal runs, so they're skipped. */
/* Decoder envelope is 65 / 65 / 63 respectively. */
#define DK_MAX_ZERO_RUN 64
#define DK_MAX_BYTE_RUN 64
#define DK_MAX_BACK_REF 62
/* Back-ref distance is encoded as `next_byte + 2`, so range 2..257. */
#define DK_MAX_BACK_DIST 257
/* Literal opcode encodes (len - 1) in the low 6 bits — max 64. */
#define DK_MAX_LITERAL 64
#define DK_MIN_ZERO_RUN 2
#define DK_MIN_BYTE_RUN 3
#define DK_MIN_BACK_REF 3
static size_t dk_count_zero_run(const unsigned char *in, size_t in_len,
size_t i) {
size_t len = 0;
size_t max = in_len - i;
if (max > DK_MAX_ZERO_RUN) max = DK_MAX_ZERO_RUN;
while (len < max && in[i + len] == 0) len++;
return len;
}
static size_t dk_count_byte_run(const unsigned char *in, size_t in_len,
size_t i) {
unsigned char b;
size_t len;
size_t max;
if (i >= in_len) return 0;
b = in[i];
len = 1;
max = in_len - i;
if (max > DK_MAX_BYTE_RUN) max = DK_MAX_BYTE_RUN;
while (len < max && in[i + len] == b) len++;
return len;
}
/* Longest match search over distances [2, 257]. Returns 0 below the
* minimum profitable length. Match length capped at the distance
* (no-overlap rule, see encoder comment above). */
static size_t dk_find_match(const unsigned char *in, size_t in_len,
size_t i, size_t *out_dist) {
size_t best_len = 0;
size_t best_dist = 0;
size_t d, max_d, remaining, len, per_d_cap;
if (i < 2) return 0;
max_d = (i < DK_MAX_BACK_DIST) ? i : DK_MAX_BACK_DIST;
remaining = in_len - i;
if (remaining < DK_MIN_BACK_REF) return 0;
for (d = 2; d <= max_d; d++) {
per_d_cap = (d < DK_MAX_BACK_REF) ? d : DK_MAX_BACK_REF;
if (per_d_cap > remaining) per_d_cap = remaining;
if (per_d_cap < DK_MIN_BACK_REF) continue;
/* First-byte mismatch filter cuts the inner loop ~256x on
* non-repetitive input. */
if (in[i - d] != in[i]) continue;
len = 1;
while (len < per_d_cap && in[i + len] == in[i + len - d]) {
len++;
}
if (len > best_len) {
best_len = len;
best_dist = d;
if (best_len == DK_MAX_BACK_REF) break;
}
}
if (best_len < DK_MIN_BACK_REF) return 0;
*out_dist = best_dist;
return best_len;
}
pakka_status_t pakka_dk_deflate(const unsigned char *in, size_t in_len,
unsigned char *out, size_t out_cap,
size_t *out_len, pakka_error_t *err) {
size_t i = 0;
size_t op = 0;
size_t literal_start = 0;
size_t literal_len = 0;
uint64_t worst;
if ((in == NULL && in_len > 0) || (out == NULL && out_cap > 0)) {
return dk_err_fill(err, PAKKA_ERR_INVALID_ARGUMENT,
"NULL buffer with non-zero length");
}
/* Worst case is literal-only: one op-byte per 64 input bytes,
* plus the bytes, plus the terminator. Reject up front rather
* than partial-write into an undersized buffer. */
worst = (uint64_t)in_len + ((uint64_t)in_len + 63u) / 64u + 1u;
if (worst > out_cap) {
return dk_err_fill(err, PAKKA_ERR_LIMIT,
"output buffer too small for worst-case "
"encoding (need %llu, have %zu)",
(unsigned long long)worst, out_cap);
}
while (i < in_len) {
size_t zlen = dk_count_zero_run(in, in_len, i);
size_t rlen = dk_count_byte_run(in, in_len, i);
size_t mdist = 0;
size_t mlen = dk_find_match(in, in_len, i, &mdist);
size_t z_eff = (zlen >= DK_MIN_ZERO_RUN) ? zlen : 0;
size_t r_eff = (rlen >= DK_MIN_BYTE_RUN) ? rlen : 0;
size_t m_eff = (mlen >= DK_MIN_BACK_REF) ? mlen : 0;
int kind;
size_t take;
if (z_eff == 0 && r_eff == 0 && m_eff == 0) {
kind = 0;
take = 1;
} else if (z_eff * 2 > r_eff && z_eff * 2 > m_eff) {
kind = 1;
take = z_eff;
} else if (m_eff > r_eff) {
kind = 3;
take = m_eff;
} else {
kind = 2;
take = r_eff;
}
if (kind == 0) {
if (literal_len == 0) literal_start = i;
literal_len++;
i++;
if (literal_len == DK_MAX_LITERAL) {
out[op++] = (unsigned char)(literal_len - 1);
memcpy(out + op, in + literal_start, literal_len);
op += literal_len;
literal_len = 0;
}
} else {
if (literal_len > 0) {
out[op++] = (unsigned char)(literal_len - 1);
memcpy(out + op, in + literal_start, literal_len);
op += literal_len;
literal_len = 0;
}
if (kind == 1) {
out[op++] = (unsigned char)(take + 62);
} else if (kind == 2) {
out[op++] = (unsigned char)(take + 126);
out[op++] = in[i];
} else {
out[op++] = (unsigned char)(take + 190);
out[op++] = (unsigned char)(mdist - 2);
}
i += take;
}
}
if (literal_len > 0) {
out[op++] = (unsigned char)(literal_len - 1);
memcpy(out + op, in + literal_start, literal_len);
op += literal_len;
}
out[op++] = 0xFFu;
if (out_len != NULL) *out_len = op;
return PAKKA_OK;
}