some tweaks
[mkp224o.git] / base32_from.c
blob0f787e667cd4e1310793c2e3e18c36f6d60457f5
1 #include <stddef.h>
2 #include <stdint.h>
3 #include "types.h"
4 #include "base32.h"
6 static const u8 base32f[256] = {
7 //00 01 02 03 04 05 06 07
8 //08 09 0A 0B 0C 0D 0E 0F
9 // 0x00..0x3F
10 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x00
11 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08
12 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10
13 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18
14 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20
15 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x28
16 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // 0x30
17 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38
18 // 0x40..0x7F
19 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x40
20 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x48
21 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x50
22 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x58
23 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x60
24 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x68
25 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x70
26 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x78
27 // 0x80..0xBF
28 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
29 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
30 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
31 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
32 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
33 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
34 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
35 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
36 // 0xC0..0xFF
37 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
38 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
39 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
40 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
41 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
42 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
43 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
47 +--first octet--+-second octet--+--third octet--+--forth octet--+--fifth octet--+
48 |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
49 +---------+-----+---+---------+-+-------+-------+-+---------+---+-----+---------+
50 |4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|
51 +-1.index-+-2.index-+-3.index-+-4.index-+-5.index-+-6.index-+-7.index-+-8.index-+
53 size_t base32_from(u8 *dst,u8 *dmask,const char *src)
55 size_t i, j, k = (size_t)-1, l, sk = 0;
56 u8 mask = 0, cmask = 0;
57 for (i = 0;;i += 5) {
58 j = i/5;
59 if (!src[j]) {
60 if (k != (size_t)-1)
61 dst[k] &= cmask;
62 if (dmask)
63 *dmask = cmask;
64 return k + 1;
66 l = i%8;
67 k = i/8;
68 if (k != sk)
69 cmask = 0;
70 sk = k;
71 mask = (0x1F << 3) >> l;
72 cmask |= mask;
73 //printf("1:m[%02X] cm[%02X]\n",mask,cmask);
74 dst[k] &= ~mask;
75 dst[k] |= (base32f[(u8)src[j]] << 3) >> l;
76 if (((0x1F << 8) >> (l+5-8)) & 0xFF) {
77 ++k;
78 sk = k;
79 mask = ((0x1F << 8) >> (l+5-8)) & 0xFF;
80 cmask = mask;
81 //printf("2:m[%02X] cm[%02X]\n",mask,cmask);
82 dst[k] &= ~mask;
83 dst[k] |= ((base32f[(u8)src[j]] << 8) >> (l+5-8)) & 0xFF;
85 //printf("i = %02d, i/8 = %02d, i%8 = %02d, i%8+5-8 = %02d\n", i, i/8, i%8, i%8+5-8);
86 //printf("mask0: %02x\n", (0x1F << 3) >> (i%8));
87 //printf("mask1: %02x\n", ((0x1F << 8) >> (i%8+5-8)) & 0xFF);
91 int base32_valid(const char *src,size_t *count)
93 const char *p;
95 for (p = src;base32f[(u8)*p] != 0xFF;++p)
98 if (count)
99 *count = (size_t) (p - src);
100 return !*p;