-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb64.c
136 lines (119 loc) · 3.9 KB
/
b64.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*********************************************************************
#include <b64.h>
* Filename: base64.c
* Author: Brad Conte (brad AT bradconte.com)
* Copyright:
* Disclaimer: This code is presented "as is" without any guarantees.
* Details: Implementation of the Base64 encoding algorithm.
*********************************************************************/
/*************************** HEADER FILES ***************************/
#include <stddef.h>
#include "b64.h"
/****************************** MACROS ******************************/
#define NEWLINE_INVL 76
/**************************** VARIABLES *****************************/
// Note: To change the charset to a URL encoding, replace the '+' and '/' with '*' and '-'
static const uint8_t charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
/*********************** FUNCTION DEFINITIONS ***********************/
static uint8_t revchar(char ch)
{
if (ch >= 'A' && ch <= 'Z')
ch -= 'A';
else if (ch >= 'a' && ch <='z')
ch = ch - 'a' + 26;
else if (ch >= '0' && ch <='9')
ch = ch - '0' + 52;
else if (ch == '+')
ch = 62;
else if (ch == '/')
ch = 63;
return(ch);
}
uint16_t b64Encode(const uint8_t in[], uint8_t out[], uint16_t len, int newline_flag)
{
uint16_t idx, idx2, blks, blk_ceiling, left_over, newline_count = 0;
blks = (len / 3);
left_over = len % 3;
if (out == NULL) {
idx2 = blks * 4 ;
if (left_over)
idx2 += 4;
if (newline_flag)
idx2 += len / 57; // (NEWLINE_INVL / 4) * 3 = 57. One newline per 57 input uint8_ts.
}
else {
// Since 3 input uint8_ts = 4 output uint8_ts, determine out how many even sets of
// 3 uint8_ts the input has.
blk_ceiling = blks * 3;
for (idx = 0, idx2 = 0; idx < blk_ceiling; idx += 3, idx2 += 4) {
out[idx2] = charset[in[idx] >> 2];
out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
out[idx2 + 2] = charset[((in[idx + 1] & 0x0f) << 2) | (in[idx + 2] >> 6)];
out[idx2 + 3] = charset[in[idx + 2] & 0x3F];
// The offical standard requires a newline every 76 characters.
// (Eg, first newline is character 77 of the output.)
if (((idx2 - newline_count + 4) % NEWLINE_INVL == 0) && newline_flag) {
out[idx2 + 4] = '\n';
idx2++;
newline_count++;
}
}
if (left_over == 1) {
out[idx2] = charset[in[idx] >> 2];
out[idx2 + 1] = charset[(in[idx] & 0x03) << 4];
out[idx2 + 2] = '=';
out[idx2 + 3] = '=';
idx2 += 4;
}
else if (left_over == 2) {
out[idx2] = charset[in[idx] >> 2];
out[idx2 + 1] = charset[((in[idx] & 0x03) << 4) | (in[idx + 1] >> 4)];
out[idx2 + 2] = charset[(in[idx + 1] & 0x0F) << 2];
out[idx2 + 3] = '=';
idx2 += 4;
}
}
return(idx2);
}
uint16_t b64Decode(const uint8_t in[], uint8_t out[], uint16_t len)
{
uint8_t ch;
uint16_t idx, idx2, blks, blk_ceiling, left_over;
if (in[len - 1] == '=')
len--;
if (in[len - 1] == '=')
len--;
blks = len / 4;
left_over = len % 4;
if (out == NULL) {
if (len >= 77 && in[NEWLINE_INVL] == '\n') // Verify that newlines where used.
len -= len / (NEWLINE_INVL + 1);
blks = len / 4;
left_over = len % 4;
idx = blks * 3;
if (left_over == 2)
idx ++;
else if (left_over == 3)
idx += 2;
}
else {
blk_ceiling = blks * 4;
for (idx = 0, idx2 = 0; idx2 < blk_ceiling; idx += 3, idx2 += 4) {
if (in[idx2] == '\n')
idx2++;
out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
out[idx + 2] = (revchar(in[idx2 + 2]) << 6) | revchar(in[idx2 + 3]);
}
if (left_over == 2) {
out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
idx++;
}
else if (left_over == 3) {
out[idx] = (revchar(in[idx2]) << 2) | ((revchar(in[idx2 + 1]) & 0x30) >> 4);
out[idx + 1] = (revchar(in[idx2 + 1]) << 4) | (revchar(in[idx2 + 2]) >> 2);
idx += 2;
}
}
return(idx);
}