Skip to content

Commit 0a57c52

Browse files
committed
mDNS responder for LinConnect added, new JSON parsing, heap optimizations, file splitting
1 parent 608cc79 commit 0a57c52

File tree

10 files changed

+6795
-1662
lines changed

10 files changed

+6795
-1662
lines changed

MatrixWeatherClock/MatrixWeatherClock.ino

Lines changed: 1391 additions & 1658 deletions
Large diffs are not rendered by default.

MatrixWeatherClock/base64.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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 b64_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+
i = 0;
27+
}
28+
}
29+
30+
if(i) {
31+
for(j = i; j < 3; j++) {
32+
a3[j] = '\0';
33+
}
34+
35+
a3_to_a4(a4, a3);
36+
37+
for(j = 0; j < i + 1; j++) {
38+
output[encLen++] = b64_alphabet[a4[j]];
39+
}
40+
41+
while((i++ < 3)) {
42+
output[encLen++] = '=';
43+
}
44+
}
45+
output[encLen] = '\0';
46+
return encLen;
47+
}
48+
49+
50+
int b64_decode(char *output, int outputLen, 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+
if (decLen < (outputLen - 1))
72+
{
73+
output[decLen++] = a3[i];
74+
}
75+
}
76+
i = 0;
77+
}
78+
}
79+
80+
if (i) {
81+
for (j = i; j < 4; j++) {
82+
a4[j] = '\0';
83+
}
84+
85+
for (j = 0; j <4; j++) {
86+
a4[j] = b64_lookup(a4[j]);
87+
}
88+
89+
a4_to_a3(a3,a4);
90+
91+
for (j = 0; j < i - 1; j++) {
92+
if (decLen < (outputLen - 1))
93+
{
94+
output[decLen++] = a3[j];
95+
}
96+
}
97+
}
98+
output[decLen] = '\0';
99+
return decLen;
100+
}
101+
102+
int b64_enc_len(int plainLen) {
103+
int n = plainLen;
104+
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
105+
}
106+
107+
int b64_dec_len(char * input, int inputLen) {
108+
int i = 0;
109+
int numEq = 0;
110+
for(i = inputLen - 1; input[i] == '='; i--) {
111+
numEq++;
112+
}
113+
114+
return ((6 * inputLen) / 8) - numEq;
115+
}
116+
117+
inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
118+
a4[0] = (a3[0] & 0xfc) >> 2;
119+
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
120+
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
121+
a4[3] = (a3[2] & 0x3f);
122+
}
123+
124+
inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
125+
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
126+
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
127+
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
128+
}
129+
130+
inline unsigned char b64_lookup(char c) {
131+
int i;
132+
for(i = 0; i < 64; i++) {
133+
if(b64_alphabet[i] == c) {
134+
return i;
135+
}
136+
}
137+
138+
return -1;
139+
}

MatrixWeatherClock/base64.h

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+
/* b64_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 b64_encode(char *output, char *input, int inputLen);
26+
27+
/* b64_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 b64_decode(char *output, int outputlen, char *input, int inputLen);
44+
45+
/* b64_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 b64_enc_len(int inputLen);
58+
59+
/* b64_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 b64_dec_len(char *input, int inputLen);
74+
75+
#endif // _BASE64_H

0 commit comments

Comments
 (0)