Skip to content

Commit c023c7d

Browse files
committed
Add websocket support
1 parent 0c9720a commit c023c7d

File tree

10 files changed

+647
-59
lines changed

10 files changed

+647
-59
lines changed

Base64.cpp

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "Base64.h"
2+
3+
const char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
4+
"abcdefghijklmnopqrstuvwxyz"
5+
"0123456789+/";
6+
7+
/* 'Private' declarations */
8+
inline void a3_to_a4(unsigned char * a4, unsigned char * a3);
9+
inline void a4_to_a3(unsigned char * a3, unsigned char * a4);
10+
inline unsigned char b64_lookup(char c);
11+
12+
int base64_encode(char *output, char *input, int inputLen) {
13+
int i = 0, j = 0;
14+
int encLen = 0;
15+
unsigned char a3[3];
16+
unsigned char a4[4];
17+
18+
while(inputLen--) {
19+
a3[i++] = *(input++);
20+
if(i == 3) {
21+
a3_to_a4(a4, a3);
22+
23+
for(i = 0; i < 4; i++) {
24+
output[encLen++] = b64_alphabet[a4[i]];
25+
}
26+
27+
i = 0;
28+
}
29+
}
30+
31+
if(i) {
32+
for(j = i; j < 3; j++) {
33+
a3[j] = '\0';
34+
}
35+
36+
a3_to_a4(a4, a3);
37+
38+
for(j = 0; j < i + 1; j++) {
39+
output[encLen++] = b64_alphabet[a4[j]];
40+
}
41+
42+
while((i++ < 3)) {
43+
output[encLen++] = '=';
44+
}
45+
}
46+
output[encLen] = '\0';
47+
return encLen;
48+
}
49+
50+
int base64_decode(char * output, char * input, int inputLen) {
51+
int i = 0, j = 0;
52+
int decLen = 0;
53+
unsigned char a3[3];
54+
unsigned char a4[4];
55+
56+
57+
while (inputLen--) {
58+
if(*input == '=') {
59+
break;
60+
}
61+
62+
a4[i++] = *(input++);
63+
if (i == 4) {
64+
for (i = 0; i <4; i++) {
65+
a4[i] = b64_lookup(a4[i]);
66+
}
67+
68+
a4_to_a3(a3,a4);
69+
70+
for (i = 0; i < 3; i++) {
71+
output[decLen++] = a3[i];
72+
}
73+
i = 0;
74+
}
75+
}
76+
77+
if (i) {
78+
for (j = i; j < 4; j++) {
79+
a4[j] = '\0';
80+
}
81+
82+
for (j = 0; j <4; j++) {
83+
a4[j] = b64_lookup(a4[j]);
84+
}
85+
86+
a4_to_a3(a3,a4);
87+
88+
for (j = 0; j < i - 1; j++) {
89+
output[decLen++] = a3[j];
90+
}
91+
}
92+
output[decLen] = '\0';
93+
return decLen;
94+
}
95+
96+
int base64_enc_len(int plainLen) {
97+
int n = plainLen;
98+
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
99+
}
100+
101+
int base64_dec_len(char * input, int inputLen) {
102+
int i = 0;
103+
int numEq = 0;
104+
for(i = inputLen - 1; input[i] == '='; i--) {
105+
numEq++;
106+
}
107+
108+
return ((6 * inputLen) / 8) - numEq;
109+
}
110+
111+
inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
112+
a4[0] = (a3[0] & 0xfc) >> 2;
113+
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
114+
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
115+
a4[3] = (a3[2] & 0x3f);
116+
}
117+
118+
inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
119+
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
120+
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
121+
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
122+
}
123+
124+
inline unsigned char b64_lookup(char c) {
125+
int i;
126+
for(i = 0; i < 64; i++) {
127+
if(b64_alphabet[i] == c) {
128+
return i;
129+
}
130+
}
131+
132+
return -1;
133+
}

Base64.h

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef _BASE64_H
2+
#define _BASE64_H
3+
4+
/* b64_alphabet:
5+
* Description: Base64 alphabet table, a mapping between integers
6+
* and base64 digits
7+
* Notes: This is an extern here but is defined in Base64.c
8+
*/
9+
extern const char b64_alphabet[];
10+
11+
/* base64_encode:
12+
* Description:
13+
* Encode a string of characters as base64
14+
* Parameters:
15+
* output: the output buffer for the encoding, stores the encoded string
16+
* input: the input buffer for the encoding, stores the binary to be encoded
17+
* inputLen: the length of the input buffer, in bytes
18+
* Return value:
19+
* Returns the length of the encoded string
20+
* Requirements:
21+
* 1. output must not be null or empty
22+
* 2. input must not be null
23+
* 3. inputLen must be greater than or equal to 0
24+
*/
25+
int base64_encode(char *output, char *input, int inputLen);
26+
27+
/* base64_decode:
28+
* Description:
29+
* Decode a base64 encoded string into bytes
30+
* Parameters:
31+
* output: the output buffer for the decoding,
32+
* stores the decoded binary
33+
* input: the input buffer for the decoding,
34+
* stores the base64 string to be decoded
35+
* inputLen: the length of the input buffer, in bytes
36+
* Return value:
37+
* Returns the length of the decoded string
38+
* Requirements:
39+
* 1. output must not be null or empty
40+
* 2. input must not be null
41+
* 3. inputLen must be greater than or equal to 0
42+
*/
43+
int base64_decode(char *output, char *input, int inputLen);
44+
45+
/* base64_enc_len:
46+
* Description:
47+
* Returns the length of a base64 encoded string whose decoded
48+
* form is inputLen bytes long
49+
* Parameters:
50+
* inputLen: the length of the decoded string
51+
* Return value:
52+
* The length of a base64 encoded string whose decoded form
53+
* is inputLen bytes long
54+
* Requirements:
55+
* None
56+
*/
57+
int base64_enc_len(int inputLen);
58+
59+
/* base64_dec_len:
60+
* Description:
61+
* Returns the length of the decoded form of a
62+
* base64 encoded string
63+
* Parameters:
64+
* input: the base64 encoded string to be measured
65+
* inputLen: the length of the base64 encoded string
66+
* Return value:
67+
* Returns the length of the decoded form of a
68+
* base64 encoded string
69+
* Requirements:
70+
* 1. input must not be null
71+
* 2. input must be greater than or equal to zero
72+
*/
73+
int base64_dec_len(char *input, int inputLen);
74+
75+
#endif // _BASE64_H

0 commit comments

Comments
 (0)