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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* xattr_writer.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#define SQFS_BUILDING_DLL
#include "config.h"
#include "sqfs/xattr_writer.h"
#include "sqfs/meta_writer.h"
#include "sqfs/super.h"
#include "sqfs/xattr.h"
#include "sqfs/error.h"
#include "sqfs/block.h"
#include "sqfs/io.h"
#include "str_table.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define XATTR_KEY_BUCKETS 31
#define XATTR_VALUE_BUCKETS 511
#define XATTR_INITIAL_PAIR_CAP 128
#define MK_PAIR(key, value) (((sqfs_u64)(key) << 32UL) | (sqfs_u64)(value))
#define GET_KEY(pair) ((pair >> 32UL) & 0x0FFFFFFFFUL)
#define GET_VALUE(pair) (pair & 0x0FFFFFFFFUL)
static const char *hexmap = "0123456789ABCDEF";
static char *to_base32(const void *input, size_t size)
{
const uint8_t *in = input;
char *out, *ptr;
size_t i;
out = malloc(2 * size + 1);
if (out == NULL)
return NULL;
ptr = out;
for (i = 0; i < size; ++i) {
*(ptr++) = hexmap[ in[i] & 0x0F];
*(ptr++) = hexmap[(in[i] >> 4) & 0x0F];
}
*ptr = '\0';
return out;
}
static void *from_base32(const char *input, size_t *size_out)
{
uint8_t lo, hi, *out, *ptr;
size_t len;
len = strlen(input);
*size_out = len / 2;
out = malloc(*size_out);
if (out == NULL)
return NULL;
ptr = out;
while (*input != '\0') {
lo = strchr(hexmap, *(input++)) - hexmap;
hi = strchr(hexmap, *(input++)) - hexmap;
*(ptr++) = lo | (hi << 4);
}
return out;
}
static int compare_u64(const void *a, const void *b)
{
sqfs_u64 lhs = *((const sqfs_u64 *)a);
sqfs_u64 rhs = *((const sqfs_u64 *)b);
return (lhs < rhs ? -1 : (lhs > rhs ? 1 : 0));
}
typedef struct kv_block_desc_t {
struct kv_block_desc_t *next;
size_t start;
size_t count;
uint64_t start_ref;
size_t size_bytes;
} kv_block_desc_t;
struct sqfs_xattr_writer_t {
str_table_t keys;
str_table_t values;
sqfs_u64 *kv_pairs;
size_t max_pairs;
size_t num_pairs;
size_t kv_start;
kv_block_desc_t *kv_blocks;
size_t num_blocks;
};
sqfs_xattr_writer_t *sqfs_xattr_writer_create(void)
{
sqfs_xattr_writer_t *xwr = calloc(1, sizeof(*xwr));
if (str_table_init(&xwr->keys, XATTR_KEY_BUCKETS))
goto fail_keys;
if (str_table_init(&xwr->values, XATTR_VALUE_BUCKETS))
goto fail_values;
xwr->max_pairs = XATTR_INITIAL_PAIR_CAP;
xwr->kv_pairs = alloc_array(sizeof(xwr->kv_pairs[0]), xwr->max_pairs);
if (xwr->kv_pairs == NULL)
goto fail_pairs;
return xwr;
fail_pairs:
str_table_cleanup(&xwr->values);
fail_values:
str_table_cleanup(&xwr->keys);
fail_keys:
free(xwr);
return NULL;
}
void sqfs_xattr_writer_destroy(sqfs_xattr_writer_t *xwr)
{
kv_block_desc_t *blk;
while (xwr->kv_blocks != NULL) {
blk = xwr->kv_blocks;
xwr->kv_blocks = xwr->kv_blocks->next;
free(blk);
}
free(xwr->kv_pairs);
str_table_cleanup(&xwr->values);
str_table_cleanup(&xwr->keys);
free(xwr);
}
int sqfs_xattr_writer_begin(sqfs_xattr_writer_t *xwr)
{
xwr->kv_start = xwr->num_pairs;
return 0;
}
int sqfs_xattr_writer_add(sqfs_xattr_writer_t *xwr, const char *key,
const void *value, size_t size)
{
size_t i, key_index, old_value_index, value_index, new_count;
sqfs_u64 kv_pair, *new;
char *value_str;
int err;
if (!sqfs_has_xattr(key))
return SQFS_ERROR_UNSUPPORTED;
/* resolve key and value into unique, incremental IDs */
err = str_table_get_index(&xwr->keys, key, &key_index);
if (err)
return err;
value_str = to_base32(value, size);
if (value_str == NULL)
return SQFS_ERROR_ALLOC;
err = str_table_get_index(&xwr->values, value_str, &value_index);
free(value_str);
if (err)
return err;
str_table_add_ref(&xwr->values, value_index);
if (sizeof(size_t) > sizeof(sqfs_u32)) {
if (key_index > 0x0FFFFFFFFUL || value_index > 0x0FFFFFFFFUL)
return SQFS_ERROR_OVERFLOW;
}
/* bail if already have the pair, overwrite if we have the key */
kv_pair = MK_PAIR(key_index, value_index);
for (i = xwr->kv_start; i < xwr->num_pairs; ++i) {
if (xwr->kv_pairs[i] == kv_pair)
return 0;
if (GET_KEY(xwr->kv_pairs[i]) == key_index) {
old_value_index = GET_VALUE(xwr->kv_pairs[i]);
str_table_del_ref(&xwr->values, old_value_index);
xwr->kv_pairs[i] = kv_pair;
return 0;
}
}
/* append it to the list */
if (xwr->max_pairs == xwr->num_pairs) {
new_count = xwr->max_pairs * 2;
new = realloc(xwr->kv_pairs,
sizeof(xwr->kv_pairs[0]) * new_count);
if (new == NULL)
return SQFS_ERROR_ALLOC;
xwr->kv_pairs = new;
xwr->max_pairs = new_count;
}
xwr->kv_pairs[xwr->num_pairs++] = kv_pair;
return 0;
}
int sqfs_xattr_writer_end(sqfs_xattr_writer_t *xwr, sqfs_u32 *out)
{
kv_block_desc_t *blk, *blk_prev;
size_t i, count, value_idx;
sqfs_u32 index;
int ret;
count = xwr->num_pairs - xwr->kv_start;
if (count == 0) {
*out = 0xFFFFFFFF;
return 0;
}
qsort(xwr->kv_pairs + xwr->kv_start, count,
sizeof(xwr->kv_pairs[0]), compare_u64);
blk_prev = NULL;
blk = xwr->kv_blocks;
index = 0;
while (blk != NULL) {
if (blk->count == count) {
ret = memcmp(xwr->kv_pairs + blk->start,
xwr->kv_pairs + xwr->kv_start,
sizeof(xwr->kv_pairs[0]) * count);
if (ret == 0)
break;
}
if (index == 0xFFFFFFFF)
return SQFS_ERROR_OVERFLOW;
++index;
blk_prev = blk;
blk = blk->next;
}
if (blk != NULL) {
for (i = 0; i < count; ++i) {
value_idx = GET_VALUE(xwr->kv_pairs[xwr->kv_start + i]);
str_table_del_ref(&xwr->values, value_idx);
value_idx = GET_VALUE(xwr->kv_pairs[blk->start + i]);
str_table_add_ref(&xwr->values, value_idx);
}
xwr->num_pairs = xwr->kv_start;
} else {
blk = calloc(1, sizeof(*blk));
if (blk == NULL)
return SQFS_ERROR_ALLOC;
blk->start = xwr->kv_start;
blk->count = count;
if (blk_prev == NULL) {
xwr->kv_blocks = blk;
} else {
blk_prev->next = blk;
}
xwr->num_blocks += 1;
}
*out = index;
return 0;
}
/*****************************************************************************/
static sqfs_s32 write_key(sqfs_meta_writer_t *mw, const char *key,
bool value_is_ool)
{
sqfs_xattr_entry_t kent;
int type, err;
size_t len;
type = sqfs_get_xattr_prefix_id(key);
assert(type >= 0);
key = strchr(key, '.');
assert(key != NULL);
++key;
len = strlen(key);
if (value_is_ool)
type |= SQFS_XATTR_FLAG_OOL;
memset(&kent, 0, sizeof(kent));
kent.type = htole16(type);
kent.size = htole16(len);
err = sqfs_meta_writer_append(mw, &kent, sizeof(kent));
if (err)
return err;
err = sqfs_meta_writer_append(mw, key, len);
if (err)
return err;
return sizeof(kent) + len;
}
static sqfs_s32 write_value(sqfs_meta_writer_t *mw, const char *value_str,
sqfs_u64 *value_ref_out)
{
sqfs_xattr_value_t vent;
sqfs_u32 offset;
sqfs_u64 block;
size_t size;
void *value;
int err;
value = from_base32(value_str, &size);
if (value == NULL)
return SQFS_ERROR_ALLOC;
memset(&vent, 0, sizeof(vent));
vent.size = htole32(size);
sqfs_meta_writer_get_position(mw, &block, &offset);
*value_ref_out = (block << 16) | (offset & 0xFFFF);
err = sqfs_meta_writer_append(mw, &vent, sizeof(vent));
if (err)
goto fail;
err = sqfs_meta_writer_append(mw, value, size);
if (err)
goto fail;
return sizeof(vent) + size;
fail:
free(value);
return err;
}
static sqfs_s32 write_value_ool(sqfs_meta_writer_t *mw, sqfs_u64 location)
{
sqfs_xattr_value_t vent;
sqfs_u64 ref;
int err;
memset(&vent, 0, sizeof(vent));
vent.size = htole32(sizeof(location));
ref = htole64(location);
err = sqfs_meta_writer_append(mw, &vent, sizeof(vent));
if (err)
return err;
err = sqfs_meta_writer_append(mw, &ref, sizeof(ref));
if (err)
return err;
return sizeof(vent) + sizeof(ref);
}
static bool should_store_ool(const char *val_str, size_t refcount)
{
if (refcount < 2)
return false;
/*
Storing in line needs this many bytes: refcount * len
Storing out-of-line needs this many: len + (refcount - 1) * 8
Out-of-line prefereable iff refcount > 1 and:
refcount * len > len + (refcount - 1) * 8
=> refcount * len - len > (refcount - 1) * 8
=> (refcount - 1) * len > (refcount - 1) * 8
=> len > 8
*/
return (strlen(val_str) / 2) > sizeof(sqfs_u64);
}
static int write_block_pairs(sqfs_xattr_writer_t *xwr, sqfs_meta_writer_t *mw,
kv_block_desc_t *blk, sqfs_u64 *ool_locations)
{
sqfs_u32 key_idx, val_idx;
const char *key_str, *value_str;
sqfs_s32 diff, total = 0;
size_t i, refcount;
sqfs_u64 ref;
for (i = 0; i < blk->count; ++i) {
key_idx = GET_KEY(xwr->kv_pairs[blk->start + i]);
val_idx = GET_VALUE(xwr->kv_pairs[blk->start + i]);
key_str = str_table_get_string(&xwr->keys, key_idx);
value_str = str_table_get_string(&xwr->values, val_idx);
if (ool_locations[val_idx] == 0xFFFFFFFFFFFFFFFFUL) {
diff = write_key(mw, key_str, false);
if (diff < 0)
return diff;
total += diff;
diff = write_value(mw, value_str, &ref);
if (diff < 0)
return diff;
total += diff;
refcount = str_table_get_ref_count(&xwr->values,
val_idx);
if (should_store_ool(value_str, refcount))
ool_locations[val_idx] = ref;
} else {
diff = write_key(mw, key_str, true);
if (diff < 0)
return diff;
total += diff;
diff = write_value_ool(mw, ool_locations[val_idx]);
if (diff < 0)
return diff;
total += diff;
}
}
return total;
}
static int write_kv_pairs(sqfs_xattr_writer_t *xwr, sqfs_meta_writer_t *mw)
{
sqfs_u64 block, *ool_locations;
kv_block_desc_t *blk;
sqfs_u32 offset;
sqfs_s32 size;
size_t i;
ool_locations = alloc_array(sizeof(ool_locations[0]),
xwr->values.num_strings);
if (ool_locations == NULL)
return SQFS_ERROR_ALLOC;
for (i = 0; i < xwr->values.num_strings; ++i)
ool_locations[i] = 0xFFFFFFFFFFFFFFFFUL;
for (blk = xwr->kv_blocks; blk != NULL; blk = blk->next) {
sqfs_meta_writer_get_position(mw, &block, &offset);
blk->start_ref = (block << 16) | (offset & 0xFFFF);
size = write_block_pairs(xwr, mw, blk, ool_locations);
if (size < 0) {
free(ool_locations);
return size;
}
blk->size_bytes = size;
}
free(ool_locations);
return sqfs_meta_writer_flush(mw);
}
static int write_id_table(sqfs_xattr_writer_t *xwr, sqfs_meta_writer_t *mw,
sqfs_u64 *locations)
{
sqfs_xattr_id_t id_ent;
kv_block_desc_t *blk;
sqfs_u32 offset;
sqfs_u64 block;
size_t i = 0;
int err;
locations[i++] = 0;
for (blk = xwr->kv_blocks; blk != NULL; blk = blk->next) {
memset(&id_ent, 0, sizeof(id_ent));
id_ent.xattr = htole64(blk->start_ref);
id_ent.count = htole32(blk->count);
id_ent.size = htole32(blk->size_bytes);
err = sqfs_meta_writer_append(mw, &id_ent, sizeof(id_ent));
if (err)
return err;
sqfs_meta_writer_get_position(mw, &block, &offset);
if (block != locations[i - 1])
locations[i++] = block;
}
return sqfs_meta_writer_flush(mw);
}
static int write_location_table(sqfs_xattr_writer_t *xwr, uint64_t kv_start,
sqfs_file_t *file, const sqfs_super_t *super,
sqfs_u64 *locations, size_t loc_count)
{
sqfs_xattr_id_table_t idtbl;
int err;
memset(&idtbl, 0, sizeof(idtbl));
idtbl.xattr_table_start = htole64(kv_start);
idtbl.xattr_ids = htole32(xwr->num_blocks);
err = file->write_at(file, super->xattr_id_table_start,
&idtbl, sizeof(idtbl));
if (err)
return err;
return file->write_at(file, super->xattr_id_table_start + sizeof(idtbl),
locations, sizeof(locations[0]) * loc_count);
}
static int alloc_location_table(sqfs_xattr_writer_t *xwr, sqfs_u64 **tbl_out,
size_t *szout)
{
sqfs_u64 *locations;
size_t size, count;
if (SZ_MUL_OV(xwr->num_blocks, sizeof(sqfs_xattr_id_t), &size))
return SQFS_ERROR_OVERFLOW;
count = size / SQFS_META_BLOCK_SIZE;
if (size % SQFS_META_BLOCK_SIZE)
++count;
locations = alloc_array(sizeof(sqfs_u64), count);
if (locations == NULL)
return SQFS_ERROR_ALLOC;
*tbl_out = locations;
*szout = count;
return 0;
}
int sqfs_xattr_writer_flush(sqfs_xattr_writer_t *xwr, sqfs_file_t *file,
sqfs_super_t *super, sqfs_compressor_t *cmp)
{
sqfs_u64 *locations = NULL, kv_start, id_start;
sqfs_meta_writer_t *mw;
size_t i, count;
int err;
mw = sqfs_meta_writer_create(file, cmp, 0);
if (mw == NULL)
return SQFS_ERROR_ALLOC;
kv_start = file->get_size(file);
err = write_kv_pairs(xwr, mw);
if (err)
goto out;
sqfs_meta_writer_reset(mw);
id_start = file->get_size(file);
err = alloc_location_table(xwr, &locations, &count);
if (err)
goto out;
err = write_id_table(xwr, mw, locations);
if (err)
goto out;
super->xattr_id_table_start = file->get_size(file);
super->flags &= ~SQFS_FLAG_NO_XATTRS;
for (i = 0; i < count; ++i)
locations[i] = htole64(locations[i] + id_start);
err = write_location_table(xwr, kv_start, file, super,
locations, count);
out:
free(locations);
sqfs_meta_writer_destroy(mw);
return err;
}
|