-
Notifications
You must be signed in to change notification settings - Fork 183
/
formats.c
766 lines (655 loc) · 21.9 KB
/
formats.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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
#include <ctype.h>
#include "buffer.h"
#include "test.h"
#include "util.h"
#include "formats.h"
/* I usually hate prototypes, but I want the array of function pointers at the
* top so I don't really have a choice in this case. */
static uint8_t *encode_none(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_none(void);
static uint8_t *encode_raw(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static uint8_t *decode_raw(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_raw(void);
static uint8_t *encode_html(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static uint8_t *decode_html(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_html(void);
static uint8_t *encode_html_pure(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_html_pure(void);
static uint8_t *encode_hex(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static uint8_t *decode_hex(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_hex(void);
static uint8_t *encode_cstr(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static uint8_t *decode_cstr(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_cstr(void);
static uint8_t *encode_cstr_pure(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_cstr_pure(void);
static uint8_t *encode_fancy(uint8_t *data, uint64_t data_length, uint64_t *out_length);
static void test_fancy(void);
/* Define some types so we can stores function pointers. */
typedef uint8_t* (func_encoder)(uint8_t *data, uint64_t data_length, uint64_t *out_length);
typedef uint8_t* (func_decoder)(uint8_t *data, uint64_t data_length, uint64_t *out_length);
typedef void (func_test)(void);
/* This struct defines a format - the name, an encoder, decoder, and a test
* routine. */
typedef struct {
char *name;
func_encoder *encoder;
func_decoder *decoder;
func_test *tester;
} format_t;
/* A list of format types. To add a new type, add the function to this list,
* then to the two strings below. */
static format_t formats[] = {
{"none", encode_none, NULL, test_none},
{"raw", encode_raw, decode_raw, test_raw},
{"hex", encode_hex, decode_hex, test_hex},
{"html", encode_html, decode_html, test_html},
{"html-pure", encode_html_pure, NULL, test_html_pure},
{"cstr", encode_cstr, decode_cstr, test_cstr},
{"cstr-pure", encode_cstr_pure, NULL, test_cstr_pure},
{"fancy", encode_fancy, NULL, test_fancy},
{0, 0, 0, 0}
};
const char *encode_formats = "none, raw, hex, html, html-pure, cstr, cstr-pure, fancy";
const char *decode_formats = "raw, hex, html, cstr";
/* A simple function to get the format by the name, rather than searching
* manually in code every time. */
static format_t *format_get_by_name(char *name)
{
int i;
for(i = 0; formats[i].name; i++)
{
if(!strcmp(formats[i].name, name))
return &formats[i];
}
return NULL;
}
bool format_exists(char *format_name)
{
return format_get_by_name(format_name) != NULL;
}
uint8_t *format_encode(char *format_name, uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
format_t *format = format_get_by_name(format_name);
if(format && format->encoder)
return format->encoder(data, data_length, out_length);
else
return NULL;
}
uint8_t *format_decode(char *format_name, uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
format_t *format = format_get_by_name(format_name);
if(format && format->decoder)
return format->decoder(data, data_length, out_length);
else
return NULL;
}
/* Convert a single hex digit (encoded in ascii) at the start of 'hex' to the
* actual value. */
static uint8_t hex_to_int(uint8_t *hex)
{
/* These are defined as ints because cygwin. */
int digit1 = hex[0];
int digit2 = hex[1];
return (uint8_t)
((isdigit(digit1) ? (digit1 - '0') : (tolower(digit1) - 'a' + 10)) << 4) |
((isdigit(digit2) ? (digit2 - '0') : (tolower(digit2) - 'a' + 10)) << 0);
}
static void test_hex_to_int(void)
{
int i;
char buffer[] = "AA\0";
printf("Testing hex_to_int...\n");
for(i = 0; i < 256; i++)
{
sprintf(buffer, "%02x", i);
test_check_integer("hex_to_int", hex_to_int((uint8_t*)buffer), i);
}
}
static uint8_t *encode_none(uint8_t *data, uint64_t data_length, uint64_t *out_length) {
/* Encoding the type 'none' is so easy it's basically cheating. Simply return
* a 0-length string. */
*out_length = 0;
return malloc(0);
}
static void test_none(void)
{
int i;
char raw_data[32];
size_t raw_length;
uint8_t *encoded_data;
uint64_t encoded_length;
uint8_t expected_data[32];
uint64_t expected_length;
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%%%02x \\x%02x %02x", i, i, i);
encoded_data = encode_none((uint8_t*)raw_data, raw_length, &encoded_length);
expected_data[0] = '\0';
expected_length = 0;
test_check_memory("encode_none", expected_data, expected_length, encoded_data, encoded_length);
free(encoded_data);
}
}
static uint8_t *encode_raw(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
/* Create a new string and copy the data into it. */
uint8_t *result = malloc(data_length);
memcpy(result, data, data_length);
*out_length = data_length;
return result;
}
static uint8_t *decode_raw(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
uint8_t *result = malloc(data_length);
memcpy(result, data, data_length);
*out_length = data_length;
return result;
}
static void test_raw(void)
{
int i;
char raw_data[32];
size_t raw_length;
uint8_t *encoded_data;
uint64_t encoded_length;
uint8_t *decoded_data;
uint64_t decoded_length;
uint8_t expected_data[32];
uint64_t expected_length;
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%%%02x \\x%02x %02x", i, i, i);
encoded_data = encode_raw((uint8_t*)raw_data, raw_length, &encoded_length);
expected_length = sprintf((char*)expected_data, "%%%02x \\x%02x %02x", i, i, i);
test_check_memory("encode_raw", expected_data, expected_length, encoded_data, encoded_length);
free(encoded_data);
}
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%%%02x \\x%02x %02x", i, i, i);
decoded_data = decode_raw((uint8_t*)raw_data, raw_length, &decoded_length);
expected_length = sprintf((char*)expected_data, "%%%02x \\x%02x %02x", i, i, i);
test_check_memory("decode_raw", expected_data, expected_length, decoded_data, decoded_length);
free(decoded_data);
}
}
static uint8_t *encode_html(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
int i;
buffer_t *b = buffer_create(BO_HOST);
char tmp[16];
for(i = 0; i < data_length; i++)
{
if(isalnum(data[i]))
{
/* If the character is alphanumeric, add it as-is. */
buffer_add_int8(b, data[i]);
}
else if(data[i] == ' ')
{
/* If the character is a space, add a '+'. */
buffer_add_int8(b, '+');
}
else
{
/* Otherwise, encode it as a % followed by a hex code. */
sprintf(tmp, "%%%02x", data[i]);
buffer_add_string(b, tmp);
}
}
return buffer_create_string_and_destroy(b, out_length);
}
static uint8_t *decode_html(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
buffer_t *b = buffer_create(BO_HOST);
uint64_t i = 0;
while(i < data_length)
{
/* If the character is a '%' and we aren't at the end of the string, decode
* the hex character and add it to the string.
*
* The typecasts to 'int' here are to fix warnings from cygwin. */
if(data[i] == '%' && (i + 2) < data_length && isxdigit((int)data[i + 1]) && isxdigit((int)data[i + 2]))
{
/* Add the new character to the string as a uint8_t. */
buffer_add_int8(b, hex_to_int(&data[i] + 1));
/* We consumed three digits here. */
i += 3;
}
else if(data[i] == '+')
{
/* In html encoding, a '+' is a space. */
buffer_add_int8(b, ' ');
i++;
}
else
{
/* If it's not %NN or +, it's just a raw number.k */
buffer_add_int8(b, data[i]);
i++;
}
}
return buffer_create_string_and_destroy(b, out_length);
}
static void test_html(void)
{
int i;
char raw_data[32];
size_t raw_length;
uint8_t *encoded_data;
uint64_t encoded_length;
uint8_t *decoded_data;
uint64_t decoded_length;
uint8_t expected_data[32];
uint64_t expected_length;
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%c", i);
encoded_data = encode_html((uint8_t*)raw_data, raw_length, &encoded_length);
if(isalnum(i))
{
expected_length = sprintf((char*)expected_data, "%c", i);
}
else if(i == ' ')
{
expected_length = sprintf((char*)expected_data, "+");
}
else
{
expected_length = sprintf((char*)expected_data, "%%%02x", i);
}
test_check_memory("encode_html", expected_data, expected_length, encoded_data, encoded_length);
free(encoded_data);
}
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%%%02x \\x%02x %02x", i, i, i);
decoded_data = decode_html((uint8_t*)raw_data, raw_length, &decoded_length);
expected_length = sprintf((char*)expected_data, "%c \\x%02x %02x", i, i, i);
test_check_memory("decode_html", expected_data, expected_length, decoded_data, decoded_length);
free(decoded_data);
}
/* Check some sidecases (the '%' is at the end of the string). */
decoded_data = decode_html((uint8_t*)"%", 1, &decoded_length);
test_check_memory("decode_html", (uint8_t*)"%", 1, decoded_data, decoded_length);
free(decoded_data);
decoded_data = decode_html((uint8_t*)"%2", 2, &decoded_length);
test_check_memory("decode_html", (uint8_t*)"%2", 2, decoded_data, decoded_length);
free(decoded_data);
decoded_data = decode_html((uint8_t*)"%25", 3, &decoded_length);
test_check_memory("decode_html", (uint8_t*)"%", 1, decoded_data, decoded_length);
free(decoded_data);
/* Check other sidecases (the string contains non-hex characters). */
decoded_data = decode_html((uint8_t*)"%2g", 3, &decoded_length);
test_check_memory("decode_html", (uint8_t*)"%2g", 3, decoded_data, decoded_length);
free(decoded_data);
decoded_data = decode_html((uint8_t*)"%g2", 3, &decoded_length);
test_check_memory("decode_html", (uint8_t*)"%g2", 3, decoded_data, decoded_length);
free(decoded_data);
}
static uint8_t *encode_html_pure(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
int i;
buffer_t *b = buffer_create(BO_HOST);
char tmp[16];
/* Encode every character as %xx. */
for(i = 0; i < data_length; i++)
{
sprintf(tmp, "%%%02x", data[i]);
buffer_add_string(b, tmp);
}
return buffer_create_string_and_destroy(b, out_length);
}
static void test_html_pure(void)
{
int i;
char raw_data[32];
size_t raw_length;
uint8_t *encoded_data;
uint64_t encoded_length;
uint8_t expected_data[32];
uint64_t expected_length;
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%c", i);
encoded_data = encode_html_pure((uint8_t*)raw_data, raw_length, &encoded_length);
expected_length = sprintf((char*)expected_data, "%%%02x", i);
test_check_memory("encode_html_pure", expected_data, expected_length, encoded_data, encoded_length);
free(encoded_data);
}
}
static uint8_t *encode_hex(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
int i;
buffer_t *b = buffer_create(BO_HOST);
char tmp[16];
/* Encode every character as 2 digits of hex. */
for(i = 0; i < data_length; i++)
{
sprintf(tmp, "%02x", data[i]);
buffer_add_string(b, tmp);
}
return buffer_create_string_and_destroy(b, out_length);
}
static uint8_t *decode_hex(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
buffer_t *b = buffer_create(BO_HOST);
uint64_t i = 0;
/* If we wind up with an odd number of characters, the final character is
* ignored. */
while(i + 1 < data_length)
{
/* Skip over and ignore non-hex digits. */
if(!isxdigit(data[i]) || !isxdigit(data[i+1]))
{
i++;
continue;
}
/* Add the new character to the string as a uint8_t. */
buffer_add_int8(b, hex_to_int(&data[i]));
/* We consumed three digits here. */
i += 2;
}
return buffer_create_string_and_destroy(b, out_length);
}
static void test_hex(void)
{
int i;
char raw_data[32];
size_t raw_length;
uint8_t *encoded_data;
uint64_t encoded_length;
uint8_t *decoded_data;
uint64_t decoded_length;
uint8_t expected_data[32];
uint64_t expected_length;
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%c%c%c", i, i, i);
encoded_data = encode_hex((uint8_t*)raw_data, raw_length, &encoded_length);
expected_length = sprintf((char*)expected_data, "%02x%02x%02x", i, i, i);
test_check_memory("encode_hex", expected_data, expected_length, encoded_data, encoded_length);
free(encoded_data);
}
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%02x%02x%02x", (uint8_t)(i - 1), (uint8_t)(i), (uint8_t)(i + 1));
decoded_data = decode_hex((uint8_t*)raw_data, raw_length, &decoded_length);
expected_length = sprintf((char*)expected_data, "%c%c%c", i - 1, i, i + 1);
test_check_memory("decode_hex", expected_data, expected_length, decoded_data, decoded_length);
free(decoded_data);
}
/* Side-case: odd numbers of hex digits. */
decoded_data = decode_hex((uint8_t*)"41424", 5, &decoded_length);
test_check_memory("decode_hex ('41424')", (uint8_t*)"AB", 2, decoded_data, decoded_length);
free(decoded_data);
/* Side-case: non-hex characters in the string. */
decoded_data = decode_hex((uint8_t*)"414z4", 5, &decoded_length);
test_check_memory("decode_hex ('414z4')", (uint8_t*)"A", 1, decoded_data, decoded_length);
free(decoded_data);
decoded_data = decode_hex((uint8_t*)"4141z4141", 9, &decoded_length);
test_check_memory("decode_hex ('4141z4141')", (uint8_t*)"AAAA", 4, decoded_data, decoded_length);
free(decoded_data);
}
static uint8_t *encode_cstr(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
int i;
buffer_t *b = buffer_create(BO_HOST);
char tmp[16];
for(i = 0; i < data_length; i++)
{
if(isalnum(data[i]))
{
/* Add letters/numbers as-is. */
buffer_add_int8(b, data[i]);
}
else
{
/* Encode all other characters as "\xNN". */
sprintf(tmp, "\\x%02x", data[i]);
buffer_add_string(b, tmp);
}
}
return buffer_create_string_and_destroy(b, out_length);
}
static uint8_t *decode_cstr(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
buffer_t *b = buffer_create(BO_HOST);
uint64_t i = 0;
uint64_t in_length = data_length;
while(i < in_length)
{
if(data[i] == '\\')
{
/* Consume the slash. */
i++;
/* Check for the various format specifiers - \a, \b, \t, \n, \r, etc) */
if(i < in_length && data[i] == '\\')
{
buffer_add_int8(b, '\\');
i++;
}
else if(i < in_length && data[i] == 'a')
{
buffer_add_int8(b, 0x07);
i++;
}
else if(i < in_length && data[i] == 'b')
{
buffer_add_int8(b, 0x08);
i++;
}
else if(i < in_length && data[i] == 't')
{
buffer_add_int8(b, 0x09);
i++;
}
else if(i < in_length && data[i] == 'n')
{
buffer_add_int8(b, 0x0a);
i++;
}
else if(i < in_length && data[i] == 'v')
{
buffer_add_int8(b, 0x0b);
i++;
}
else if(i < in_length && data[i] == 'f')
{
buffer_add_int8(b, 0x0c);
i++;
}
else if(i < in_length && data[i] == 'r')
{
buffer_add_int8(b, 0x0d);
i++;
}
else if(i < in_length && data[i] == 'e')
{
buffer_add_int8(b, 0x1b);
i++;
}
/* Ensure the data is sane. */
else if(i + 2 < in_length && data[i] == 'x' && isxdigit((int)data[i + 1]) && isxdigit((int)data[i + 2]))
{
/* Add the new character to the string as a uint8_t. */
buffer_add_int8(b, hex_to_int(&data[i] + 1));
/* We consumed three digits here. */
i += 3;
}
else
{
buffer_add_int8(b, '\\');
}
}
else
{
buffer_add_int8(b, data[i]);
i++;
}
}
return buffer_create_string_and_destroy(b, out_length);
}
static void test_cstr(void)
{
int i;
char raw_data[32];
size_t raw_length;
uint8_t *encoded_data;
uint64_t encoded_length;
uint8_t *decoded_data;
uint64_t decoded_length;
uint8_t expected_data[32];
uint64_t expected_length;
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%c", i);
encoded_data = encode_cstr((uint8_t*)raw_data, raw_length, &encoded_length);
if(isalnum(i))
{
expected_length = sprintf((char*)expected_data, "%c", i);
}
else
{
expected_length = sprintf((char*)expected_data, "\\x%02x", i);
}
test_check_memory("encode_cstr", expected_data, expected_length, encoded_data, encoded_length);
free(encoded_data);
}
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%%%02x \\x%02x %02x", i, i, i);
decoded_data = decode_cstr((uint8_t*)raw_data, raw_length, &decoded_length);
expected_length = sprintf((char*)expected_data, "%%%02x %c %02x", i, i, i);
test_check_memory("decode_cstr", expected_data, expected_length, decoded_data, decoded_length);
free(decoded_data);
}
/* A slash with no character following. */
decoded_data = decode_cstr((uint8_t*)"\\", 1, &decoded_length);
test_check_memory("decode_cstr ('\\')", (uint8_t*)"\\", 1, decoded_data, decoded_length);
free(decoded_data);
/* \x with no character following. */
decoded_data = decode_cstr((uint8_t*)"\\x", 2, &decoded_length);
test_check_memory("decode_cstr ('\\x')", (uint8_t*)"\\x", 2, decoded_data, decoded_length);
free(decoded_data);
/* \x with one character following. */
decoded_data = decode_cstr((uint8_t*)"\\x1", 3, &decoded_length);
test_check_memory("decode_cstr ('\\x1')", (uint8_t*)"\\x1", 3, decoded_data, decoded_length);
free(decoded_data);
/* \b with a 3 after. */
decoded_data = decode_cstr((uint8_t*)"\\b3", 3, &decoded_length);
test_check_memory("decode_cstr ('\\b3')", (uint8_t*)"\x08""3", 2, decoded_data, decoded_length);
free(decoded_data);
/* \x with an improper hex code. */
decoded_data = decode_cstr((uint8_t*)"\\x1z", 4, &decoded_length);
test_check_memory("decode_cstr ('\\x1z')", (uint8_t*)"\\x1z", 4, decoded_data, decoded_length);
free(decoded_data);
/* \x with one character following. */
decoded_data = decode_cstr((uint8_t*)"\\x1", 3, &decoded_length);
test_check_memory("decode_cstr ('\\x1')", (uint8_t*)"\\x1", 3, decoded_data, decoded_length);
free(decoded_data);
/* All the special escape codes. */
decoded_data = decode_cstr((uint8_t*)"\\\\\\a\\b\\t\\n\\v\\f\\r\\e", 18, &decoded_length);
test_check_memory("decode_cstr ('\\\\\\a\\b\\t\\n\\v\\f\\r\\e')", (uint8_t*)"\\\x07\x08\x09\x0a\x0b\x0c\x0d\x1b", 9, decoded_data, decoded_length);
free(decoded_data);
}
static uint8_t *encode_cstr_pure(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
int i;
buffer_t *b = buffer_create(BO_HOST);
char tmp[16];
/* Encode every character as \xNN. */
for(i = 0; i < data_length; i++)
{
sprintf(tmp, "\\x%02x", data[i]);
buffer_add_string(b, tmp);
}
return buffer_create_string_and_destroy(b, out_length);
}
static void test_cstr_pure(void)
{
int i;
char raw_data[32];
size_t raw_length;
uint8_t *encoded_data;
uint64_t encoded_length;
uint8_t expected_data[32];
uint64_t expected_length;
for(i = 0; i < 256; i++)
{
raw_length = sprintf(raw_data, "%c", i);
encoded_data = encode_cstr_pure((uint8_t*)raw_data, raw_length, &encoded_length);
expected_length = sprintf((char*)expected_data, "\\x%02x", i);
test_check_memory("encode_cstr", expected_data, expected_length, encoded_data, encoded_length);
free(encoded_data);
}
}
/* A helper functdion for encode_fancy. */
#define get_character_from_byte(b) ((b < 0x20 || b > 0x7f) ? '.' : b)
/* Note: This function isn't tested, so be careful about messing around! */
static uint8_t *encode_fancy(uint8_t *data, uint64_t data_length, uint64_t *out_length)
{
uint64_t i, j;
buffer_t *b = buffer_create(BO_HOST);
char tmp[64];
for(i = 0; i < data_length; i++)
{
if((i % 16) == 0) /* if i is a multiple of 16... */
{
if(i > 0)
{
sprintf(tmp, " ");
buffer_add_string(b, tmp);
for(j = 16; j > 0; j--)
buffer_add_int8(b, get_character_from_byte(data[i - j]));
}
sprintf(tmp, "\n%04X: ", (uint16_t)i);
buffer_add_string(b, tmp);
}
sprintf(tmp, "%02X ", data[i]);
buffer_add_string(b, tmp);
}
if((i % 16) == 0)
{
sprintf(tmp, " ");
buffer_add_string(b, tmp);
for(j = 16; j > 0; j--)
buffer_add_int8(b, get_character_from_byte(data[i - j]));
}
else
{
/* Add padding spaces. */
for(i = data_length % 16; i < 17; i++)
buffer_add_string(b, " ");
for(i = data_length - (data_length % 16); i < data_length; i++)
buffer_add_int8(b, get_character_from_byte(data[i]));
}
sprintf(tmp, "\nLength: 0x%"PRIX64" (%"PRId64")\n", data_length, data_length);
buffer_add_string(b, tmp);
/* Null terminate the buffer. */
buffer_add_int8(b, 0);
return buffer_create_string_and_destroy(b, out_length);
}
void test_fancy(void)
{
/* This is to UI-ey to test, not much we can do without just re-implementing
* the entire thing. */
}
void format_test(void)
{
int i;
test_hex_to_int();
for(i = 0; formats[i].name; i++)
{
if(formats[i].tester)
{
printf("Testing format %s...\n", formats[i].name);
formats[i].tester();
}
else
{
fprintf(stderr, "WARNING: No test for format %s\n", formats[i].name);
}
}
}