libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/base32.c (122 lines, 3.5 KiB) -rw-r--r-- file download

8a5f06f Jamozed 2022-02-09 20:04:50
0
// util/base32.c, version 1.0.4
98e5939 Jamozed 2021-11-27 17:53:46
1
// Base32 source file from libutil
f0b8e1b Jamozed 2021-02-19 18:05:40
2
// Copyright (C) 2021, Jakob Wakeling
7f427d9 Jamozed 2022-03-06 12:55:13
3
// MIT Licence
f0b8e1b Jamozed 2021-02-19 18:05:40
4
f0b8e1b Jamozed 2021-02-19 18:05:40
5
#include "base32.h"
f0b8e1b Jamozed 2021-02-19 18:05:40
6
f0b8e1b Jamozed 2021-02-19 18:05:40
7
#include <stdint.h>
f0b8e1b Jamozed 2021-02-19 18:05:40
8
#include <stdlib.h>
f0b8e1b Jamozed 2021-02-19 18:05:40
9
#include <string.h>
f0b8e1b Jamozed 2021-02-19 18:05:40
10
f0b8e1b Jamozed 2021-02-19 18:05:40
11
static const uint8_t B32E[];
f0b8e1b Jamozed 2021-02-19 18:05:40
12
static const uint8_t B32D[];
f0b8e1b Jamozed 2021-02-19 18:05:40
13
f0b8e1b Jamozed 2021-02-19 18:05:40
14
/* Encode Base32 */
f0b8e1b Jamozed 2021-02-19 18:05:40
15
size_t b32encode(uint8_t *dst, uint8_t *src, size_t len) {
f0b8e1b Jamozed 2021-02-19 18:05:40
16
	register uint8_t *i = src, *o = dst;
f0b8e1b Jamozed 2021-02-19 18:05:40
17
	
f0b8e1b Jamozed 2021-02-19 18:05:40
18
	for (; len >= 5; len -= 5) {
f0b8e1b Jamozed 2021-02-19 18:05:40
19
		*o++ = B32E[i[0] >> 3];
f0b8e1b Jamozed 2021-02-19 18:05:40
20
		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
f0b8e1b Jamozed 2021-02-19 18:05:40
21
		*o++ = B32E[(i[1] >> 1) & 0x1F];
f0b8e1b Jamozed 2021-02-19 18:05:40
22
		*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
f0b8e1b Jamozed 2021-02-19 18:05:40
23
		*o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)];
f0b8e1b Jamozed 2021-02-19 18:05:40
24
		*o++ = B32E[(i[3] >> 2) & 0x1F];
f0b8e1b Jamozed 2021-02-19 18:05:40
25
		*o++ = B32E[((i[3] & 0x03) << 3) | (i[4] >> 5)];
f0b8e1b Jamozed 2021-02-19 18:05:40
26
		*o++ = B32E[i[4] & 0x1F]; i += 5;
f0b8e1b Jamozed 2021-02-19 18:05:40
27
	}
f0b8e1b Jamozed 2021-02-19 18:05:40
28
	
3d88ccb Jamozed 2021-02-20 18:09:27
29
	switch (len) {
3d88ccb Jamozed 2021-02-20 18:09:27
30
	case 1: {
f0b8e1b Jamozed 2021-02-19 18:05:40
31
		*o++ = B32E[i[0] >> 3];
3d88ccb Jamozed 2021-02-20 18:09:27
32
		*o++ = B32E[(i[0] & 0x07) << 2];
3d88ccb Jamozed 2021-02-20 18:09:27
33
		memset(o, '=', 6); o += 6; break;
3d88ccb Jamozed 2021-02-20 18:09:27
34
	}
3d88ccb Jamozed 2021-02-20 18:09:27
35
	case 2: {
3d88ccb Jamozed 2021-02-20 18:09:27
36
		*o++ = B32E[i[0] >> 3];
3d88ccb Jamozed 2021-02-20 18:09:27
37
		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
3d88ccb Jamozed 2021-02-20 18:09:27
38
		*o++ = B32E[(i[1] >> 1) & 0x1F];
3d88ccb Jamozed 2021-02-20 18:09:27
39
		*o++ = B32E[(i[1] & 0x01) << 4];
3d88ccb Jamozed 2021-02-20 18:09:27
40
		memset(o, '=', 4); o += 4; break;
3d88ccb Jamozed 2021-02-20 18:09:27
41
	}
3d88ccb Jamozed 2021-02-20 18:09:27
42
	case 3: {
3d88ccb Jamozed 2021-02-20 18:09:27
43
		*o++ = B32E[i[0] >> 3];
3d88ccb Jamozed 2021-02-20 18:09:27
44
		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
3d88ccb Jamozed 2021-02-20 18:09:27
45
		*o++ = B32E[(i[1] >> 1) & 0x1F];
3d88ccb Jamozed 2021-02-20 18:09:27
46
		*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
3d88ccb Jamozed 2021-02-20 18:09:27
47
		*o++ = B32E[(i[2] & 0x0F) << 1];
3d88ccb Jamozed 2021-02-20 18:09:27
48
		memset(o, '=', 3); o += 3; break;
3d88ccb Jamozed 2021-02-20 18:09:27
49
	}
3d88ccb Jamozed 2021-02-20 18:09:27
50
	case 4: {
3d88ccb Jamozed 2021-02-20 18:09:27
51
		*o++ = B32E[i[0] >> 3];
3d88ccb Jamozed 2021-02-20 18:09:27
52
		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
3d88ccb Jamozed 2021-02-20 18:09:27
53
		*o++ = B32E[(i[1] >> 1) & 0x1F];
3d88ccb Jamozed 2021-02-20 18:09:27
54
		*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
3d88ccb Jamozed 2021-02-20 18:09:27
55
		*o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)];
3d88ccb Jamozed 2021-02-20 18:09:27
56
		*o++ = B32E[(i[3] >> 2) & 0x1F];
3d88ccb Jamozed 2021-02-20 18:09:27
57
		*o++ = B32E[(i[3] & 0x03) << 3];
3d88ccb Jamozed 2021-02-20 18:09:27
58
		*o++ = '='; break;
3d88ccb Jamozed 2021-02-20 18:09:27
59
	}
f0b8e1b Jamozed 2021-02-19 18:05:40
60
	}
f0b8e1b Jamozed 2021-02-19 18:05:40
61
	
f0b8e1b Jamozed 2021-02-19 18:05:40
62
	return o - dst;
f0b8e1b Jamozed 2021-02-19 18:05:40
63
}
f0b8e1b Jamozed 2021-02-19 18:05:40
64
f0b8e1b Jamozed 2021-02-19 18:05:40
65
/* Decode Base32 */
f0b8e1b Jamozed 2021-02-19 18:05:40
66
size_t b32decode(uint8_t *dst, uint8_t *src, size_t len) {
0f438d6 Jamozed 2021-02-19 22:50:56
67
	register uint8_t *i = src, *o = dst;
0f438d6 Jamozed 2021-02-19 22:50:56
68
	
0f438d6 Jamozed 2021-02-19 22:50:56
69
	for (; src[len - 1] == '='; --len);
0f438d6 Jamozed 2021-02-19 22:50:56
70
	
0f438d6 Jamozed 2021-02-19 22:50:56
71
	for (; len >= 8; len -= 8) {
0f438d6 Jamozed 2021-02-19 22:50:56
72
		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
0f438d6 Jamozed 2021-02-19 22:50:56
73
		*o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4);
0f438d6 Jamozed 2021-02-19 22:50:56
74
		*o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1);
0f438d6 Jamozed 2021-02-19 22:50:56
75
		*o++ = (B32D[i[4]] << 7) | (B32D[i[5]] << 2) | (B32D[i[6]] >> 3);
0f438d6 Jamozed 2021-02-19 22:50:56
76
		*o++ = (B32D[i[6]] << 5) | (B32D[i[7]]); i += 8;
0f438d6 Jamozed 2021-02-19 22:50:56
77
	}
0f438d6 Jamozed 2021-02-19 22:50:56
78
	
0f438d6 Jamozed 2021-02-19 22:50:56
79
	switch (len) {
0f438d6 Jamozed 2021-02-19 22:50:56
80
	case 2: {
0f438d6 Jamozed 2021-02-19 22:50:56
81
		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
3d88ccb Jamozed 2021-02-20 18:09:27
82
		*o++ = (B32D[i[1]] << 6); break;
0f438d6 Jamozed 2021-02-19 22:50:56
83
	}
0f438d6 Jamozed 2021-02-19 22:50:56
84
	case 4: {
0f438d6 Jamozed 2021-02-19 22:50:56
85
		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
0f438d6 Jamozed 2021-02-19 22:50:56
86
		*o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4);
3d88ccb Jamozed 2021-02-20 18:09:27
87
		*o++ = (B32D[i[3]] << 4); break;
0f438d6 Jamozed 2021-02-19 22:50:56
88
	}
0f438d6 Jamozed 2021-02-19 22:50:56
89
	case 5: {
0f438d6 Jamozed 2021-02-19 22:50:56
90
		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
0f438d6 Jamozed 2021-02-19 22:50:56
91
		*o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4);
0f438d6 Jamozed 2021-02-19 22:50:56
92
		*o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1);
3d88ccb Jamozed 2021-02-20 18:09:27
93
		*o++ = (B32D[i[4]] << 7); break;
0f438d6 Jamozed 2021-02-19 22:50:56
94
	}
0f438d6 Jamozed 2021-02-19 22:50:56
95
	case 7: {
0f438d6 Jamozed 2021-02-19 22:50:56
96
		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
0f438d6 Jamozed 2021-02-19 22:50:56
97
		*o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4);
0f438d6 Jamozed 2021-02-19 22:50:56
98
		*o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1);
0f438d6 Jamozed 2021-02-19 22:50:56
99
		*o++ = (B32D[i[4]] << 7) | (B32D[i[5]] << 2) | (B32D[i[6]] >> 3);
3d88ccb Jamozed 2021-02-20 18:09:27
100
		*o++ = (B32D[i[6]] << 5); break;
0f438d6 Jamozed 2021-02-19 22:50:56
101
	}
0f438d6 Jamozed 2021-02-19 22:50:56
102
	}
0f438d6 Jamozed 2021-02-19 22:50:56
103
	
0f438d6 Jamozed 2021-02-19 22:50:56
104
	return o - dst;
f0b8e1b Jamozed 2021-02-19 18:05:40
105
}
f0b8e1b Jamozed 2021-02-19 18:05:40
106
3d88ccb Jamozed 2021-02-20 18:09:27
107
static const uint8_t B32E[] = {
3d88ccb Jamozed 2021-02-20 18:09:27
108
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
3d88ccb Jamozed 2021-02-20 18:09:27
109
	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
3d88ccb Jamozed 2021-02-20 18:09:27
110
	'2', '3', '4', '5', '6', '7'
3d88ccb Jamozed 2021-02-20 18:09:27
111
};
f0b8e1b Jamozed 2021-02-19 18:05:40
112
0f438d6 Jamozed 2021-02-19 22:50:56
113
static const uint8_t B32D[] = {
0f438d6 Jamozed 2021-02-19 22:50:56
114
	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
0f438d6 Jamozed 2021-02-19 22:50:56
115
	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
0f438d6 Jamozed 2021-02-19 22:50:56
116
	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
0f438d6 Jamozed 2021-02-19 22:50:56
117
	 0,  0, 26, 27, 28, 29, 30, 31,  0,  0,  0,  0,  0,  0,  0,  0,
0f438d6 Jamozed 2021-02-19 22:50:56
118
	 0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
0f438d6 Jamozed 2021-02-19 22:50:56
119
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
0f438d6 Jamozed 2021-02-19 22:50:56
120
};
121