forked from cathugger/mkp224o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base32_to.c
77 lines (76 loc) · 2.31 KB
/
base32_to.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
#include <stddef.h>
#include <stdint.h>
#include "types.h"
#include "base32.h"
static const char base32t[32] = {
'a', 'b', 'c', 'd', // 0
'e', 'f', 'g', 'h', // 1
'i', 'j', 'k', 'l', // 2
'm', 'n', 'o', 'p', // 3
'q', 'r', 's', 't', // 4
'u', 'v', 'w', 'x', // 5
'y', 'z', '2', '3', // 6
'4', '5', '6', '7', // 7
};
/*
+--first octet--+-second octet--+--third octet--+--forth octet--+--fifth octet--+
|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|
+---------+-----+---+---------+-+-------+-------+-+---------+---+-----+---------+
|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|
+-1.index-+-2.index-+-3.index-+-4.index-+-5.index-+-6.index-+-7.index-+-8.index-+
*/
// masks:
// 0xFF 0x7F 0x3F 0x1F 0x0F 0x07 0x03 0x01
// 255 127 63 31 15 7 3 1
char *base32_to(char *dst,const u8 *src,size_t slen)
{
//printf("slen = %d\n", slen);
//printhex(base32t, 32);
//printhex(base32f, 256);
// base32 eats in 5bit pieces;
// closest we can provide is 40, which is 5 bytes
size_t i;
for (i = 0; i + 4 < slen; i += 5) {
//printf("ei!\n");
//char *od = dst;
*dst++ = base32t[src[i+0] >> 3];
*dst++ = base32t[((src[i+0] & 7) << 2) | (src[i+1] >> 6)];
*dst++ = base32t[(src[i+1] >> 1) & 31];
*dst++ = base32t[((src[i+1] & 1) << 4) | (src[i+2] >> 4)];
*dst++ = base32t[((src[i+2] & 15) << 1) | (src[i+3] >> 7)];
*dst++ = base32t[((src[i+3]) >> 2) & 31];
*dst++ = base32t[((src[i+3] & 3) << 3) | (src[i+4] >> 5)];
*dst++ = base32t[src[i+4] & 31];
//printhex(od, 8);
}
//char *od = dst;
if (i < slen) {
//printf("oi!0\n");
*dst++ = base32t[src[i+0] >> 3];
if (i + 1 < slen) {
//printf("oi!1\n");
*dst++ = base32t[((src[i+0] & 7) << 2) | (src[i+1] >> 6)];
*dst++ = base32t[(src[i+1] >> 1) & 31];
if (i + 2 < slen) {
//printf("oi!2\n");
*dst++ = base32t[((src[i+1] & 1) << 4) | (src[i+2] >> 4)];
if (i + 3 < slen) {
//printf("oi!3\n");
*dst++ = base32t[((src[i+2] & 15) << 1) | (src[i+3] >> 7)];
*dst++ = base32t[(src[i+3] >> 2) & 31];
*dst++ = base32t[(src[i+3] & 3) << 3];
}
else {
*dst++ = base32t[(src[i+2] & 15) << 1];
}
}
else
*dst++ = base32t[(src[i+1] & 1) << 4];
}
else
*dst++ = base32t[(src[i+0] & 7) << 2];
}
//printhex(od, dst-od);
*dst = 0;
return dst;
}