-
Notifications
You must be signed in to change notification settings - Fork 150
/
base16_from.c
82 lines (77 loc) · 2.53 KB
/
base16_from.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
#include <stddef.h>
#include <stdint.h>
#include "types.h"
#include "base16.h"
static const u8 base16f[256] = {
//00 01 02 03 04 05 06 07
//08 09 0A 0B 0C 0D 0E 0F
// 0x00..0x3F
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x00
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x28
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 0x30
0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38
// 0x40..0x7F
0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, // 0x40
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x48
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x50
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x58
0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, // 0x60
0xFF,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= 0;;i += 4) {
j = i/4;
if (!src[j]) {
if (k != (size_t)-1)
dst[k] &= cmask;
if (dmask)
*dmask = cmask;
return k + 1;
}
l = i%8;
k = i/8;
if (k != sk)
cmask = 0;
sk = k;
mask = (0x0F << 4) >> l;
cmask |= mask;
dst[k] &= ~mask;
dst[k] |= (base16f[(u8)src[j]] << 4) >> l;
}
}
int base16_valid(const char *src,size_t *count)
{
const char *p;
for (p = src;base16f[(u8)*p] != 0xFF;++p)
;
if (count)
*count = (size_t) (p - src);
return !*p;
}