libutil

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

libutil/src/base64.c (89 lines, 2.4 KiB) -rw-r--r-- file download

8a5f06f Jamozed 2022-02-09 20:04:50
0
// util/base64.c, version 1.1.5
98e5939 Jamozed 2021-11-27 17:53:46
1
// Base64 source file from libutil
0becaf1 Jamozed 2021-02-05 02:10:07
2
// Copyright (C) 2021, Jakob Wakeling
7f427d9 Jamozed 2022-03-06 12:55:13
3
// MIT Licence
0becaf1 Jamozed 2021-02-05 02:10:07
4
0becaf1 Jamozed 2021-02-05 02:10:07
5
#include "base64.h"
0becaf1 Jamozed 2021-02-05 02:10:07
6
0becaf1 Jamozed 2021-02-05 02:10:07
7
#include <stdbool.h>
0becaf1 Jamozed 2021-02-05 02:10:07
8
#include <stdint.h>
0becaf1 Jamozed 2021-02-05 02:10:07
9
#include <stdlib.h>
32a9a74 Jamozed 2021-02-20 18:11:25
10
#include <string.h>
0becaf1 Jamozed 2021-02-05 02:10:07
11
0becaf1 Jamozed 2021-02-05 02:10:07
12
static const uint8_t B64E[];
0becaf1 Jamozed 2021-02-05 02:10:07
13
static const uint8_t B64D[];
0becaf1 Jamozed 2021-02-05 02:10:07
14
0becaf1 Jamozed 2021-02-05 02:10:07
15
/* Encode Base64 */
b8e17ee Jamozed 2021-02-05 17:52:29
16
size_t b64encode(uint8_t *dst, uint8_t *src, size_t len) {
0becaf1 Jamozed 2021-02-05 02:10:07
17
	register uint8_t *i = src, *o = dst;
0becaf1 Jamozed 2021-02-05 02:10:07
18
	
0becaf1 Jamozed 2021-02-05 02:10:07
19
	for (; len >= 3; len -= 3) {
0becaf1 Jamozed 2021-02-05 02:10:07
20
		*o++ = B64E[i[0] >> 2];
0becaf1 Jamozed 2021-02-05 02:10:07
21
		*o++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
0becaf1 Jamozed 2021-02-05 02:10:07
22
		*o++ = B64E[((i[1] & 0x0F) << 2) | (i[2] >> 6)];
0becaf1 Jamozed 2021-02-05 02:10:07
23
		*o++ = B64E[i[2] & 0x3F]; i += 3;
0becaf1 Jamozed 2021-02-05 02:10:07
24
	}
0becaf1 Jamozed 2021-02-05 02:10:07
25
	
32a9a74 Jamozed 2021-02-20 18:11:25
26
	switch (len) {
32a9a74 Jamozed 2021-02-20 18:11:25
27
	case 1: {
0becaf1 Jamozed 2021-02-05 02:10:07
28
		*o++ = B64E[i[0] >> 2];
32a9a74 Jamozed 2021-02-20 18:11:25
29
		*o++ = B64E[(i[0] & 0x03) << 4];
32a9a74 Jamozed 2021-02-20 18:11:25
30
		memset(o, '=', 2); o += 2; break;
32a9a74 Jamozed 2021-02-20 18:11:25
31
	}
32a9a74 Jamozed 2021-02-20 18:11:25
32
	case 2: {
32a9a74 Jamozed 2021-02-20 18:11:25
33
		*o++ = B64E[i[0] >> 2];
32a9a74 Jamozed 2021-02-20 18:11:25
34
		*o++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
32a9a74 Jamozed 2021-02-20 18:11:25
35
		*o++ = B64E[(i[1] & 0x0F) << 2];
32a9a74 Jamozed 2021-02-20 18:11:25
36
		*o++ = '='; break;
32a9a74 Jamozed 2021-02-20 18:11:25
37
	}
0becaf1 Jamozed 2021-02-05 02:10:07
38
	}
0becaf1 Jamozed 2021-02-05 02:10:07
39
	
b8e17ee Jamozed 2021-02-05 17:52:29
40
	return o - dst;
0becaf1 Jamozed 2021-02-05 02:10:07
41
}
0becaf1 Jamozed 2021-02-05 02:10:07
42
0becaf1 Jamozed 2021-02-05 02:10:07
43
/* Decode Base64 */
b8e17ee Jamozed 2021-02-05 17:52:29
44
size_t b64decode(uint8_t *dst, uint8_t *src, size_t len) {
9e513ff Jamozed 2021-02-20 19:08:39
45
	register uint8_t *i = src, *o = dst;
0becaf1 Jamozed 2021-02-05 02:10:07
46
	
9e513ff Jamozed 2021-02-20 19:08:39
47
	for (; src[len - 1] == '='; --len);
0becaf1 Jamozed 2021-02-05 02:10:07
48
	
9e513ff Jamozed 2021-02-20 19:08:39
49
	for (; len >= 4; len -= 4) {
9e513ff Jamozed 2021-02-20 19:08:39
50
		*o++ = (B64D[i[0]] << 2) | (B64D[i[1]] >> 4);
9e513ff Jamozed 2021-02-20 19:08:39
51
		*o++ = (B64D[i[1]] << 4) | (B64D[i[2]] >> 2);
9e513ff Jamozed 2021-02-20 19:08:39
52
		*o++ = (B64D[i[2]] << 6) | (B64D[i[3]]); i += 4;
0becaf1 Jamozed 2021-02-05 02:10:07
53
	}
0becaf1 Jamozed 2021-02-05 02:10:07
54
	
9e513ff Jamozed 2021-02-20 19:08:39
55
	switch (len) {
9e513ff Jamozed 2021-02-20 19:08:39
56
	case 2: {
9e513ff Jamozed 2021-02-20 19:08:39
57
		*o++ = (B64D[i[0]] << 2) | (B64D[i[1]] >> 4);
9e513ff Jamozed 2021-02-20 19:08:39
58
		*o++ = (B64D[i[1]] << 4); break;
9e513ff Jamozed 2021-02-20 19:08:39
59
	}
9e513ff Jamozed 2021-02-20 19:08:39
60
	case 3: {
9e513ff Jamozed 2021-02-20 19:08:39
61
		*o++ = (B64D[i[0]] << 2) | (B64D[i[1]] >> 4);
9e513ff Jamozed 2021-02-20 19:08:39
62
		*o++ = (B64D[i[1]] << 4) | (B64D[i[2]] >> 2);
9e513ff Jamozed 2021-02-20 19:08:39
63
		*o++ = (B64D[i[2]] << 6); break;
9e513ff Jamozed 2021-02-20 19:08:39
64
	}
9e513ff Jamozed 2021-02-20 19:08:39
65
	}
0becaf1 Jamozed 2021-02-05 02:10:07
66
	
b8e17ee Jamozed 2021-02-05 17:52:29
67
	return o - dst;
0becaf1 Jamozed 2021-02-05 02:10:07
68
}
0becaf1 Jamozed 2021-02-05 02:10:07
69
32a9a74 Jamozed 2021-02-20 18:11:25
70
static const uint8_t B64E[] = {
32a9a74 Jamozed 2021-02-20 18:11:25
71
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
32a9a74 Jamozed 2021-02-20 18:11:25
72
	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
32a9a74 Jamozed 2021-02-20 18:11:25
73
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
32a9a74 Jamozed 2021-02-20 18:11:25
74
	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
32a9a74 Jamozed 2021-02-20 18:11:25
75
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
32a9a74 Jamozed 2021-02-20 18:11:25
76
};
0becaf1 Jamozed 2021-02-05 02:10:07
77
0becaf1 Jamozed 2021-02-05 02:10:07
78
static const uint8_t B64D[] = {
0becaf1 Jamozed 2021-02-05 02:10:07
79
	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
0becaf1 Jamozed 2021-02-05 02:10:07
80
	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
32a9a74 Jamozed 2021-02-20 18:11:25
81
	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 62,  0,  0,  0, 63,
0becaf1 Jamozed 2021-02-05 02:10:07
82
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61,  0,  0,  0,  0,  0,  0,
0becaf1 Jamozed 2021-02-05 02:10:07
83
	 0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
32a9a74 Jamozed 2021-02-20 18:11:25
84
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  0,  0,  0,  0,  0,
0becaf1 Jamozed 2021-02-05 02:10:07
85
	 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
0becaf1 Jamozed 2021-02-05 02:10:07
86
	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
0becaf1 Jamozed 2021-02-05 02:10:07
87
};
88