-
Notifications
You must be signed in to change notification settings - Fork 60
/
esphap_base64.c
87 lines (71 loc) · 2.37 KB
/
esphap_base64.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
83
84
85
86
#include "esphap_base64.h"
static unsigned char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char base64_encode_char(unsigned char c) {
return base64_chars[c];
}
unsigned char esphap_base64_decode_char(unsigned char c) {
if (c >= 'A' && c <= 'Z')
return c - 'A';
if (c >= 'a' && c <= 'z')
return c - 'a' + 26;
if (c >= '0' && c <= '9')
return c - '0' + 52;
if (c == '+')
return 62;
if (c == '/')
return 63;
if (c == '=')
return 64;
return 0;
}
size_t base64_encoded_size(const unsigned char *data, size_t size) {
return (size + 2)/3*4;
}
size_t esphap_base64_decoded_size(const unsigned char *encoded_data, size_t encoded_size) {
size_t size = (encoded_size + 3)/4*3;
if (encoded_data[encoded_size-1] == '=')
size--;
if (encoded_data[encoded_size-2] == '=')
size--;
return size;
}
int esphap_base64_encode(const unsigned char* data, size_t size, unsigned char *encoded_data) {
size_t i=0, j=0, size1=size - size%3;
for (; i<size1; i+=3, j+=4) {
encoded_data[j+0] = base64_encode_char(data[i+0]>>2);
encoded_data[j+1] = base64_encode_char(((data[i+0]&0x3)<<4) + (data[i+1]>>4));
encoded_data[j+2] = base64_encode_char(((data[i+1]&0xF)<<2) + (data[i+2]>>6));
encoded_data[j+3] = base64_encode_char(data[i+2]&0x3F);
}
if (size % 3 == 1) {
encoded_data[j+0] = base64_encode_char(data[i+0]>>2);
encoded_data[j+1] = base64_encode_char((data[i+0]&0x3)<<4);
encoded_data[j+2] = encoded_data[j+3] = '=';
j += 4;
} else if (size % 3 == 2) {
encoded_data[j+0] = base64_encode_char(data[i+0]>>2);
encoded_data[j+1] = base64_encode_char(((data[i+0]&0x3)<<4) + (data[i+1]>>4));
encoded_data[j+2] = base64_encode_char(((data[i+1]&0xF)<<2));
encoded_data[j+3] = '=';
j += 4;
}
return j;
}
int esphap_base64_decode(const unsigned char* encoded_data, size_t encoded_size, unsigned char *data) {
if (encoded_size % 4 != 0)
return -1;
size_t i=0, j=0;
for (; i<encoded_size; i+=4) {
unsigned char block[4];
for (size_t k=0; k<4; k++)
block[k] = esphap_base64_decode_char(encoded_data[i+k]);
data[j++] = (block[0]<<2) + (block[1]>>4);
if (block[2] == 64)
break;
data[j++] = ((block[1]&0xF)<<4) + (block[2]>>2);
if (block[3] == 64)
break;
data[j++] = ((block[2]&0x3)<<6) + block[3];
}
return j;
}