-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGbase64.cpp
176 lines (145 loc) · 5.24 KB
/
Gbase64.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <Arduino.h>
#include "Gbase64.h"
const char PROGMEM B64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String Gbase64::Sbe(String input)
{
byte input_b[input.length()];
byte atalakit_6[(input.length()*8)/6];
uint16_t z = 0;
String bekodolva;
boolean tort = 0;
if ( ((input.length()*8)/6)*1.0 < (input.length()*8)/6.0 ) tort = 1;
input.getBytes(input_b, input.length() + 1);
for (uint16_t i =0;i<(input.length()*8)/6;i=i+3)
{
if(z<=(input.length()*8)/6) atalakit_6[z] = input_b[i] >> 2;
if(z+1<=(input.length()*8)/6) atalakit_6[z+1] = ((input_b[i] - (atalakit_6[z] << 2)) << 4) + (input_b[i+1] >> 4);
if(z+2<=(input.length()*8)/6) atalakit_6[z+2] = ((input_b[i+1] - ((input_b[i+1] >> 4) << 4)) << 2) + (input_b[i+2] >> 6);
if(z+3<=(input.length()*8)/6) atalakit_6[z+3] = input_b[i+2] - ((input_b[i+2] >> 6) << 6);
z=z+4;
delay(0);
}
for (uint16_t i=0;i<(input.length()*8)/6 + tort;i++)
{
delay(0);
bekodolva+=char(pgm_read_byte(B64 + atalakit_6[i]));
}
if ((bekodolva.length() / 4) * 4 < bekodolva.length() )
{
uint8_t p = 4 - (bekodolva.length() - (bekodolva.length() / 4) * 4);
for (uint8_t i=0;i<p;i++)
{
bekodolva += "=";
}
}
return bekodolva;
}
String Gbase64::Bbe(byte *input_b,uint16_t hossz)
{
byte atalakit_6[(hossz*8)/6];
uint16_t z = 0;
String bekodolva;
boolean tort = 0;
if ( ((hossz*8)/6)*1.0 < (hossz*8)/6.0 ) tort = 1;
for (uint16_t i =0;i<(hossz*8)/6;i=i+3)
{
if(z<=(hossz*8)/6) atalakit_6[z] = input_b[i] >> 2;
if(z+1<=(hossz*8)/6) atalakit_6[z+1] = ((input_b[i] - (atalakit_6[z] << 2)) << 4) + (input_b[i+1] >> 4);
if(z+2<=(hossz*8)/6) atalakit_6[z+2] = ((input_b[i+1] - ((input_b[i+1] >> 4) << 4)) << 2) + (input_b[i+2] >> 6);
if(z+3<=(hossz*8)/6) atalakit_6[z+3] = input_b[i+2] - ((input_b[i+2] >> 6) << 6);
z=z+4;
delay(0);
}
for (uint16_t i=0;i<(hossz*8)/6 + tort;i++)
{
delay(0);
bekodolva+=char(pgm_read_byte(B64 + atalakit_6[i]));
}
if ((bekodolva.length() / 4) * 4 < bekodolva.length() )
{
uint8_t p = 4 - (bekodolva.length() - (bekodolva.length() / 4) * 4);
for (uint8_t i=0;i<p;i++)
{
bekodolva += "=";
}
}
return bekodolva;
}
String Gbase64::Ski(String kikodol)
{
char todec[kikodol.length()];
byte visszaalakit_6[kikodol.length()];
uint8_t padding_v = 0;
uint16_t counter = 0;
String visszateres;
kikodol.toCharArray(todec, kikodol.length()+1);
for (uint16_t i=0;i<=kikodol.length();i++)
{
for (uint8_t j=0;j<64;j++)
{
if ( char(pgm_read_byte(B64 + j)) == todec[i] )
{
visszaalakit_6[i] = j;
break;
}
else if ( todec[i] == 61) // 61 az egyenlőségjel ASCII kódja (=)
{
padding_v++;
break;
}
delay(0);
}
}
for(uint16_t i=0;i<kikodol.length();i=i+4)
{
if (counter < (kikodol.length() * 6) /8 - padding_v) visszaalakit_6[counter] = (visszaalakit_6[i] << 2) + (visszaalakit_6[i+1] >> 4);
if (counter+1 < (kikodol.length() * 6) /8 - padding_v) visszaalakit_6[counter + 1] = ((visszaalakit_6[i+1] - ((visszaalakit_6[i+1] >> 4) << 4)) << 4) + (visszaalakit_6[i+2] >> 2);
if (counter+2 < (kikodol.length() * 6) /8 - padding_v) visszaalakit_6[counter + 2] = ((visszaalakit_6[i+2] - ((visszaalakit_6[i+2] >> 2) << 2)) << 6) + visszaalakit_6[i+3];
counter = counter + 3;
delay(0);
}
for (uint16_t i = 0 ; i < (kikodol.length() * 6) /8 - padding_v; i++)
{
visszateres += char(visszaalakit_6[i]);
delay(0);
}
return visszateres;
}
void Gbase64::Bki(String kikodol, byte *vissza)
{
char todec[kikodol.length()+1];
byte visszaalakit_6[kikodol.length()];
uint16_t padding_v = 0;
uint16_t counter = 0;
kikodol.toCharArray(todec, kikodol.length()+1);
for (uint16_t i=0;i<=kikodol.length();i++)
{
for (uint8_t j=0;j<64;j++)
{
if ( char(pgm_read_byte(B64 + j)) == todec[i] )
{
visszaalakit_6[i] = j;
break;
}
else if ( todec[i] == 61) // 61 az egyenlőségjel ASCII kódja (=)
{
padding_v++;
break;
}
delay(0);
}
}
for(uint16_t i=0;i<kikodol.length();i=i+4)
{
if (counter < (kikodol.length() * 6) /8 - padding_v) visszaalakit_6[counter] = (visszaalakit_6[i] << 2) + (visszaalakit_6[i+1] >> 4);
if (counter+1 < (kikodol.length() * 6) /8 - padding_v) visszaalakit_6[counter + 1] = ((visszaalakit_6[i+1] - ((visszaalakit_6[i+1] >> 4) << 4)) << 4) + (visszaalakit_6[i+2] >> 2);
if (counter+2 < (kikodol.length() * 6) /8 - padding_v) visszaalakit_6[counter + 2] = ((visszaalakit_6[i+2] - ((visszaalakit_6[i+2] >> 2) << 2)) << 6) + visszaalakit_6[i+3];
counter = counter + 3;
delay(0);
}
for (uint16_t i = 0 ; i < (kikodol.length() * 6) /8 - padding_v; i++)
{
vissza[i] = visszaalakit_6[i];
delay(0);
}
}