-
Notifications
You must be signed in to change notification settings - Fork 3
/
f5ar.c
514 lines (390 loc) · 13.2 KB
/
f5ar.c
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* API implementation for data compression using modified F5 steganography algorithm
* Written by Labunsky Artem <me@labunsky.info>
*
* This software is based in part on the work of the Independent JPEG Group
* Distributed under the Simplified BSD License
*/
#include "f5ar.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "md5.h"
#include "container.c"
struct linked_container {
struct linked_container* next;
container_t container;
};
struct f5archive_ctx {
struct linked_container* head;
struct linked_container* tail;
struct jpeg_error_mgr err;
/* Even 64-bit servers should not be able to handle more than
* 2^32 * |C| bytes for at least a decade */
uint32_t size;
uint32_t filled;
uint32_t used;
};
int f5ar_init(f5archive* archive) {
archive->ctx = (archive->ctx ? archive->ctx : calloc(sizeof(struct f5archive_ctx), 1));
if (!archive->ctx)
return F5AR_MALLOC_ERR;
return F5AR_OK;
}
static struct linked_container* append_new(f5archive *archive) {
if (!archive->ctx)
return NULL;
if (!archive->ctx->head)
archive->ctx->head = calloc(sizeof(struct linked_container), 1),
archive->ctx->tail = archive->ctx->head;
else
archive->ctx->tail->next = calloc(sizeof(struct linked_container), 1),
archive->ctx->tail = archive->ctx->tail->next;
archive->ctx->size += archive->ctx->tail ? 1 : 0;
return archive->ctx->tail;
}
static void free_tail(f5archive *archive) {
struct linked_container *el = archive->ctx->head;
while (el->next != archive->ctx->tail)
el = el->next;
archive->ctx->tail = el;
free (el->next);
el->next = NULL;
archive->ctx->size--;
}
static int copy_fs_path(f5archive *archive, struct linked_container *new, const char *path) {
const size_t path_len = strlen(path);
new->container.src.fs.path = malloc(path_len + 1);
if (!new->container.src.fs.path) {
fclose(new->container.src.fs.stream);
free_tail(archive);
return F5AR_MALLOC_ERR;
}
strcpy(new->container.src.fs.path, path);
new->container.src.fs.path[path_len] = '\0';
return F5AR_OK;
}
int f5ar_add_file(f5archive *archive, const char *path) {
FILE* src = fopen(path, "rb");
if (!src)
return F5AR_FILEIO_ERR;
struct linked_container* new = append_new(archive);
if (!new) {
fclose(src);
return F5AR_MALLOC_ERR;
}
new->container.src.type = FILE_SRC;
new->container.src.fs.stream = src;
const int err = copy_fs_path(archive, new, path);
if (!err)
archive->ctx->filled++;
return err;
}
int f5ar_add_mem(f5archive *archive, void *ptr, size_t* size) {
struct linked_container* new = append_new(archive);
if (!new)
return F5AR_MALLOC_ERR;
new->container.src.type = MEM_SRC;
new->container.src.mem.ptr = ptr;
new->container.src.mem.size = size;
archive->ctx->filled++;
return F5AR_OK;
}
static int f5archive_clear_ctx(struct f5archive_ctx* ctx) {
if (!ctx)
return F5AR_NOT_INITIALIZED;
struct linked_container *el = ctx->head, *tmp;
while (el) {
if (el->container.src.type == FILE_SRC) {
fclose(el->container.src.fs.stream);
free((el->container.src.fs.path));
}
tmp = el, el = el->next, free(tmp);
}
return F5AR_OK;
}
void f5ar_destroy(f5archive *archive) {
if (!archive->ctx)
return;
f5archive_clear_ctx(archive->ctx),
free(archive->ctx);
}
static inline void export_to(f5archive* archive, char* dest, size_t size) {
struct linked_container* el = archive->ctx->head;
while (size)
memcpy(dest, el->container.hash, MD5_SIZE),
dest += MD5_SIZE, size--,
el = el->next;
}
f5ar_blob* f5ar_export_order(f5archive* archive) {
if (!archive->ctx)
return NULL;
const size_t mem_size = archive->ctx->size * MD5_SIZE;
f5ar_blob* order = malloc(sizeof(f5ar_blob) + mem_size);
if (!order)
return NULL;
order->size = mem_size;
export_to(archive, order->body, archive->ctx->size);
return order;
}
f5ar_blob* f5ar_export_order_used(f5archive* archive) {
if (!archive->ctx)
return NULL;
const size_t mem_size = archive->ctx->used * MD5_SIZE;
f5ar_blob* order = malloc(sizeof(f5ar_blob) + mem_size);
if (!order)
return NULL;
order->size = mem_size;
export_to(archive, order->body, archive->ctx->used);
return order;
}
static inline int import_to(f5archive* archive, char* src, size_t size) {
while (size) {
append_new(archive);
if (archive->ctx->tail)
memcpy(archive->ctx->tail->container.hash, src, MD5_SIZE);
else
return F5AR_MALLOC_ERR;
size -= MD5_SIZE, src += MD5_SIZE;
}
return F5AR_OK;
}
int f5ar_import_order(f5archive *archive, f5ar_blob *order) {
if (!archive->ctx)
return F5AR_NOT_INITIALIZED;
f5archive_clear_ctx(archive->ctx);
return import_to(archive, order->body, order->size);
}
#define abs(x) (x >= 0 ? x : -x)
static f5archive_capacity capacity(container_t *container, struct f5archive_ctx* ctx) {
f5archive_capacity capacity = {};
int err = container_open(container, &ctx->err);
if (err)
return capacity;
for (JDIMENSION row_id = 0; row_id < container->jpeg.dstruct.comp_info[0].height_in_blocks; row_id++) {
JBLOCKROW row = container->jpeg.dstruct.mem->access_virt_barray(
(j_common_ptr) &container->jpeg.dstruct, container->jpeg.dct_arrays[0],
row_id, (JDIMENSION) 1, FALSE
)[0];
for (size_t block_id = 0; block_id < container->jpeg.dstruct.comp_info[0].width_in_blocks; block_id++) {
for (unsigned i = 0; i < DCTSIZE2; i++) {
const JCOEF c = abs(row[block_id][i]);
capacity.shrinkable += (c == 1) ? 1 : 0;
capacity.full += (c > 1) ? 1 : 0;
}
}
}
container_close_discard(container);
return capacity;
}
/* Will be called only once */
int f5ar_analyze(f5archive *archive) {
if (!archive->ctx)
return F5AR_NOT_INITIALIZED;
archive->capacity.full = 0,
archive->capacity.shrinkable = 0;
struct linked_container* el = archive->ctx->head;
while (el) {
const f5archive_capacity local = capacity(&el->container, archive->ctx);
archive->capacity.full += local.full;
archive->capacity.shrinkable += local.shrinkable;
el = el->next;
}
return F5AR_OK;
}
int f5ar_fill_file(f5archive *archive, const char *path) {
if (!archive->ctx)
return F5AR_NOT_INITIALIZED;
FILE* src = fopen(path, "rb");
if (!src)
return F5AR_FILEIO_ERR;
char hash[MD5_SIZE];
if (md5_file(src, hash)) {
fclose(src);
return F5AR_FILEIO_ERR;
}
struct linked_container *el = archive->ctx->head;
while (el) {
if (!memcmp(el->container.hash, hash, MD5_SIZE)) {
fseek(src, 0, SEEK_SET);
el->container.src.type = FILE_SRC;
el->container.src.fs.stream = src;
const int err = copy_fs_path(archive, el, path);
if (err) {
fclose(src);
return err;
}
archive->ctx->filled++;
return (archive->ctx->filled == archive->ctx->size) ? F5AR_OK_COMPLETE : F5AR_OK;
}
el = el->next;
}
fclose(src);
return F5AR_NOT_FOUND;
}
int f5ar_fill_mem(f5archive *archive, void *ptr, size_t* size) {
if (!archive->ctx)
return F5AR_NOT_INITIALIZED;
char hash[MD5_SIZE];
md5_buffer(ptr, *size, hash);
struct linked_container *el = archive->ctx->head;
while (el) {
if (!memcmp(el->container.hash, hash, MD5_SIZE)) {
el->container.src.type = MEM_SRC;
el->container.src.mem.ptr = ptr;
el->container.src.mem.size = size;
archive->ctx->filled++;
return (archive->ctx->filled == archive->ctx->size) ? F5AR_OK_COMPLETE : F5AR_OK;
}
el = el->next;
}
return F5AR_NOT_FOUND;
}
static unsigned f5em(JCOEF **a, size_t n) {
unsigned hash = 0;
for (size_t i = 0; i < n;)
if (*(a[i++]) & 1)
hash ^= i;
return hash;
}
static int catch_up(f5archive* archive, struct linked_container** glob, struct linked_container* local) {
struct linked_container* el = *glob;
int err = F5AR_OK;
while (el != local) {
err = container_close_keep(&el->container, &archive->ctx->err);
el = el->next, archive->ctx->used++;
}
*glob = el;
return err;
}
static unsigned calc_k(f5archive_capacity arch_capacity, size_t size) {
unsigned k = 1, capacity;
while (k < 24) {
capacity = arch_capacity.full + arch_capacity.shrinkable / k * 2;
double kn_rate = ((double) k) / ((1 << k) - 1);
double embed_rate = ((double) (size * 8)) / capacity;
if (embed_rate >= kn_rate)
return k-1;
else
k++;
}
return k;
}
int f5ar_pack(f5archive *archive, const char *data, size_t size) {
if (!archive->ctx || archive->ctx->filled != archive->ctx->size)
return F5AR_NOT_COMPLETE;
archive->ctx->used = 0;
if (size == 0)
return F5AR_OK;
if (archive->capacity.full + archive->capacity.shrinkable == 0)
f5ar_analyze(archive);
archive->meta.msg_size = size;
if (archive->meta.k == 0)
archive->meta.k = calc_k(archive->capacity, size);
size_t n = (1 << archive->meta.k) - 1;
JCOEF** a = malloc(n * sizeof(JCOEF*));
if (!a)
return F5AR_MALLOC_ERR;
struct linked_container* el = archive->ctx->head;
int err = container_open(&el->container, &archive->ctx->err);
if (err) {
free(a);
return err;
}
unsigned msg_mask = 1;
size_t msg_i = 0;
while (msg_i < size && !err) {
unsigned kword = 0;
for (unsigned k_mask = 1, k_lim = 1 << archive->meta.k; k_mask < k_lim && msg_i < size; k_mask <<= 1) {
kword = (data[msg_i] & msg_mask) ? (kword | k_mask) : kword;
msg_mask = ((msg_mask <<= 1) == 256) ? 1 : msg_mask;
msg_i += (msg_mask == 1) ? 1 : 0;
}
struct linked_container* local_el = el;
size_t ai = 0;
while (true) {
while (ai < n && !err) {
JCOEF coeff = dct_get(local_el->container.dct);
if (coeff != 0)
a[ai++] = &dct_get(local_el->container.dct);
if (c_next(&local_el->container)) {
if (local_el->next) {
local_el = local_el->next;
err = container_open(&local_el->container, &archive->ctx->err);
} else
err = F5AR_FAILURE;
}
}
if (err)
break;
unsigned s = f5em(a, n) ^ kword;
if (s == 0)
break;
JCOEF val = *(a[s-1]);
val += (val > 0) ? -1 : 1;
if ((*a[s-1] = val) != 0)
break;
ai = n-1;
while (s < n)
a[s-1] = a[s], s++;
}
err = catch_up(archive, &el, local_el);
}
archive->ctx->used++;
free(a);
err = container_close_keep(&el->container, &archive->ctx->err);
return err;
}
static unsigned f5ex(const JCOEF *a, size_t n) {
unsigned hash = 0;
for (size_t i = 0; i < n;)
if (a[i++] & 1)
hash ^= i;
return hash;
}
int f5ar_unpack(f5archive *archive, char **res_ptr, size_t *size) {
if (archive->ctx->size != archive->ctx->filled)
return F5AR_NOT_COMPLETE;
char* msg = calloc(1, archive->meta.msg_size);
if (!msg) return F5AR_MALLOC_ERR;
const unsigned k = archive->meta.k, n = (unsigned) ((1 << k) - 1);
const unsigned k_mask_max = (unsigned) (1 << k);
unsigned msg_mask = 1;
size_t msg_i = 0;
JCOEF* a = malloc(sizeof(JCOEF) * n);
if (!a)
return F5AR_MALLOC_ERR;
struct linked_container *el = archive->ctx->head;
int err = container_open(&el->container, &archive->ctx->err);
while (msg_i < archive->meta.msg_size) {
unsigned ai = 0;
while (ai < n && !err) {
const JCOEF coeff = dct_get(el->container.dct);
if (coeff != 0)
a[ai++] = coeff;
if (c_next(&el->container)) {
container_close_discard(&el->container);
el = el->next;
if (el == NULL) {
free(a), free(msg), *size = msg_i + 1;
return F5AR_FAILURE;
}
err = container_open(&el->container, &archive->ctx->err);
}
}
if (err)
break;
unsigned k_word = f5ex(a, n), k_mask = 1;
while (k_mask < k_mask_max && msg_i < archive->meta.msg_size) {
msg[msg_i] |= (k_word & k_mask) ? msg_mask : 0,
msg_mask <<= 1, k_mask <<= 1;
if (msg_mask == 256)
msg_mask = 1, msg_i++;
}
}
free(a),
container_close_discard(&el->container);
*size = archive->meta.msg_size,
*res_ptr = msg;
return F5AR_OK;
}