Skip to content

Commit

Permalink
initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Jul 20, 2020
1 parent 21e774d commit 0145db9
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
# arduino_base64
Binary based (uint8_t) simple Base64 Codec for Arduino.
# Arduino Base64 Codec
Binary based simple Base64 Codec for Arduino.


# API
## `void BASE64::encode(const uint8_t* input, char* output, size_t length)`
**Arguments**
- `input`: Receives raw data as a binary array.
- `output`: Base64 encoded string.
- `length`: Number of bytes of input data.

**Return**
- Nothing.

**Note**
- If the input data is a `char*`, cast it to `uint8_t*`.


## `size_t BASE64::encodeLength(size_t length)`
**Arguments**
- `length`: Number of bytes of input data.

**Return**
- Number of characters after Base64 encoding.


## `void BASE64::decode(const char* input, uint8_t* output)`
**Arguments**
- `input`: Receives string data as a base64 encoded.
- `output`: Raw binary data.

**Return**
- Nothing.


## `size_t BASE64::decodeLength(const char* input)`
**Arguments**
- `input`: Receives string data as a base64 encoded.

**Return**
- Number of bytes after Base64 decoding.

# Disclaimer
According to MIT License.
16 changes: 16 additions & 0 deletions examples/decode/decode.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Arduino.h"
#include "Base64.h"

void setup(){
Serial.begin(115200);
while(!Serial);

const char* encodedData = "Zm9vYmFy";

uint8_t raw[BASE64::decodeLength(encodedData)];
BASE64::decode(encodedData, raw);

Serial.println((char*)raw);
}

void loop(){}
17 changes: 17 additions & 0 deletions examples/encode/encode.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "Arduino.h"
#include "Base64.h"

void setup(){
Serial.begin(115200);
while(!Serial);

const char* rawData = "foobar";
size_t rawSize = strlen(rawData);

char encoded[BASE64::encodeLength(rawSize)];
BASE64::encode((const uint8_t*)rawData, encoded, rawSize);

Serial.println(encoded);
}

void loop(){}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Base64_Codec
version=1.0.0
author=dojyorin
maintainer=https://github.com/dojyorin
sentence=Base64 Codec
paragraph=Binary based simple Base64 Codec.
category=Other
url=https://github.com/dojyorin/arduino_base64.git
architectures=*
132 changes: 132 additions & 0 deletions src/Base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include "Base64.h"

namespace{
const char alphaTable[64] = {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
};

const uint8_t charTable[128] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

void a3to4(uint8_t* a3, uint8_t* a4){
a4[0] = (a3[0] & 0xFC) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xF0) >> 4);
a4[2] = ((a3[1] & 0x0F) << 2) + ((a3[2] & 0xC0) >> 6);
a4[3] = a3[2] & 0x3F;
}

void a4to3(uint8_t* a4, uint8_t* a3){
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0x0F) << 4) + ((a4[2] & 0x3C) >> 2);
a3[2] = ((a4[2] & 0x03) << 6) + a4[3];
}
}

void BASE64::encode(const uint8_t* input, char* output, size_t length){
uint8_t i = 0;
uint8_t a3[3];
uint8_t a4[4];

while(length--){
a3[i++] = *input++;

if(i == 3){
a3to4(a3, a4);

for(i = 0; i < 4; i++){
*output++ = alphaTable[a4[i]];
}

i = 0;
}
}

if(i){
for(uint8_t j = i; j < 3; j++){
a3[j] = '\0';
}

a3to4(a3, a4);

for(uint8_t j = 0; j < i + 1; j++){
*output++ = alphaTable[a4[j]];
}

while(i++ < 3){
*output++ = '=';
}
}

*output = '\0';
}

size_t BASE64::encodeLength(size_t length){
return (length + 2 - ((length + 2) % 3)) / 3 * 4 + 1;
}

void BASE64::decode(const char* input, uint8_t* output){
size_t length = strlen(input);
uint8_t i = 0;
uint8_t a3[3];
uint8_t a4[4];

while(length--){
if(*input == '='){
break;
}

a4[i++] = *input++;

if(i == 4){
for(i = 0; i < 4; i++){
a4[i] = charTable[a4[i]];
}

a4to3(a4, a3);

for(i = 0; i < 3; i++){
*output++ = a3[i];
}

i = 0;
}
}

if(i){
for(uint8_t j = i; j < 4; j++){
a4[j] = '\0';
}

for(uint8_t j = 0; j < 4; j++){
a4[j] = charTable[a4[j]];
}

a4to3(a4, a3);

for(uint8_t j = 0; j < i - 1; j++){
*output++ = a3[j];
}
}
}

size_t BASE64::decodeLength(const char* input){
size_t length = strlen(input);
uint8_t eq = 0;

for(uint32_t i = length - 1; input[i] == '='; i--){
eq++;
}

return 6 * length / 8 - eq;
}
14 changes: 14 additions & 0 deletions src/Base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef Base64_Header
#define Base64_Header

#include "inttypes.h"
#include "string.h"

namespace BASE64{
void encode(const uint8_t* input, char* output, size_t length);
size_t encodeLength(size_t length);
void decode(const char* input, uint8_t* output);
size_t decodeLength(const char* input);
}

#endif

0 comments on commit 0145db9

Please sign in to comment.