Skip to content

Commit 64a37ca

Browse files
authored
Add files via upload
1 parent a406ccb commit 64a37ca

File tree

9 files changed

+1046
-0
lines changed

9 files changed

+1046
-0
lines changed
Lines changed: 133 additions & 0 deletions
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+
}
Lines changed: 75 additions & 0 deletions
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
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Websocket Server for Arduino
2+
3+
This library implements a Websocket server running on an Arduino. It's based on the [proposed standard][1] published December 2011 which is supported in the current versions (June 2012) of Firefox, Chrome, and Safari 6.0 beta (not older Safari, unfortunately) and thus is quite usable.
4+
5+
The implementation in this library has restrictions as the Arduino platform resources are very limited:
6+
7+
* The server **only** handles TXT frames.
8+
* The server **only** handles **single byte** chars. The Arduino just can't handle UTF-8 to it's full.
9+
* The server **only** accepts **final** frames with maximum payload length of 64 bytes. No fragmented data, in other words.
10+
* For now, the server silently ignores all frames except TXT and CLOSE.
11+
* The amount of simultaneous connections may be limited by RAM or hardware. (Each connection takes 16 bytes of RAM, and the W5100 shield is hardware-limited to 4 simultaneous connections.)
12+
* There's no keep-alive logic implemented.
13+
14+
_Required headers (example):_
15+
16+
GET /whatever HTTP/1.1
17+
Host: server.example.com
18+
Upgrade: websocket
19+
Connection: Upgrade
20+
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
21+
Sec-WebSocket-Version: 13
22+
23+
The server checks that all of these headers are present, but only cares that the version is 13.
24+
25+
_Response example:_
26+
27+
HTTP/1.1 101 Switching Protocols
28+
Upgrade: websocket
29+
Connection: Upgrade
30+
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
31+
32+
The last line is the Base64 encoded SHA-1 hash of the key with a concatenated GUID, as specified by the standard.
33+
34+
**Daniel O'Neill:** ***Compared to the original, this library consumes significantly less RAM and provides additional functionality.***
35+
36+
### Requirements
37+
38+
* Arduino IDE 1.0.1 or greater. You should not use 1.0 since it has a bug in the Ethernet library that will affect this library.
39+
* An Arduino Duemilanove or greater with Ethernet shield. An Arduino Ethernet should work too, but it has not been tested.
40+
* A Websocket client that conforms to version 13 of the protocol.
41+
42+
### Getting started
43+
44+
Install the library to "libraries" folder in your Arduino sketchbook folder. For example, on a mac that's `~/Documents/Arduino/libraries`.
45+
46+
Try the supplied echo example together with the [web based test client][2] to ensure that things work.
47+
48+
Start playing with your own code and enjoy!
49+
50+
### Feedback
51+
52+
I'm a pretty lousy programmer, at least when it comes to Arduino, and it's been 15 years since I last touched C++, so do file issues for every opportunity for improvement.
53+
54+
Oh by the way, quoting myself:
55+
56+
> Don't forget to place a big ***fat*** disclaimer in the README. There is most certainly bugs in the code and I may well have misunderstood some things in the specification which I have only skimmed through and not slept with. So _please_ do not use this code in appliancies where living things could get hurt, like space shuttles, dog tread mills and Large Hadron Colliders.
57+
58+
59+
[1]: http://datatracker.ietf.org/doc/rfc6455/?include_text=1 "Protol version implemented here"
60+
[2]: http://www.websocket.org/echo.html "Echo Test client"

0 commit comments

Comments
 (0)