Skip to content

Commit

Permalink
Merge pull request #10 from dojyorin/dev
Browse files Browse the repository at this point in the history
update docs.
  • Loading branch information
dojyorin authored May 26, 2023
2 parents 684d5ea + 6a52405 commit b00fdfb
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 100 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Release
"on":
name: release
on:
push:
tags: v[0-9]+.[0-9]+.[0-9]+
jobs:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Test
"on":
name: test
on:
push:
branches:
- dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows_json/release.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Release",
"name": "release",
"on": {
"push": {
"tags": "v[0-9]+.[0-9]+.[0-9]+"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows_json/test.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Test",
"name": "test",
"on": {
"push": {
"branches": [
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows_json/to_yaml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -eu

cd ${0%/*}

yq -P -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml
yq -o y -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml
62 changes: 6 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,13 @@
![actions:test](https://github.com/dojyorin/arduino_base64/actions/workflows/test.yaml/badge.svg)
![actions:release](https://github.com/dojyorin/arduino_base64/actions/workflows/release.yaml/badge.svg)

Convert between binary and Base64 encoded string.
Easily convert sensor raw values, structures, etc.

# Example
## Encode
```c++
const uint8_t data[] = {0x17, 0x77, 0x3B, 0x11, 0x82, 0xA4, 0xC4, 0xC8, 0x27, 0xBC, 0xED, 0x27, 0x07, 0xC1, 0x56, 0x57};
auto dataLength = sizeof(data);
char result[base64::encodeLength(dataLength)];

base64::encode(data, dataLength, result);
```
## Decode
```c++
const char data[] = "F3c7EYKkxMgnvO0nB8FWVw==";
uint8_t result[base64::decodeLength(data)];
base64::decode(data, result);
```
Convert between binary and base64 encoded string.
Easily convert sensor raw values, structures, etc...

# Details
The only export of this library will be [`arduino_base64.hpp`](./src/arduino_base64.hpp).
Other source files are for internal use and should not normally be include.

This is library made to convert binary data (e.g. raw sensor values) to Base64 encoded string.
String can be convert by cast them to byte arrays, but that's not what this library is for, nor do we plan to provide a means.
If you want to convert string, use this library and implement the wrapper functions yourself.
This library made to convert binary data (e.g. raw sensor values) to base64 encoded string.
String can be convert by cast them to `uint8_t*`, but that not what this library is for, nor do we plan to provide means.
If you want to convert string, use this library and implement wrapper functions yourself.

# API
## `base64::encode(input, inputLength, output)`
- Arguments
- `input` : `const uint8_t*` ... Binary data.
- `inputLength` : `size_t` ... Number of input bytes.
- `output` : `char*` ... Base64 encoded string.
- Result
- `void`

If the input is string, cast it to `uint8_t*`.

## `base64::encodeLength(inputLength)`
- Arguments
- `inputLength` : `size_t` ... Number of input bytes.
- Result
- `size_t` ... Number of output characters.

## `base64::decode(input, output)`
- Arguments
- `input` : `const char*` ... Base64 encoded string.
- `output` : `uint8_t*` ... Binary data.
- Result
- `void`

If the output is string, cast it to `char*`.

## `base64::decodeLength(input)`
- Arguments
- `input` : `const char*` ... Base64 encoded string.
- Result
- `size_t` ... Number of output bytes.
See [`arduino_base64.hpp`](./src/arduino_base64.hpp) for details.
9 changes: 4 additions & 5 deletions examples/decode/decode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ void setup(){
Serial.begin(115200);
while(!Serial);

const char data[] = "F3c7EYKkxMgnvO0nB8FWVw==";
uint8_t result[base64::decodeLength(data)];
const char input[] = "F3c7EYKkxMgnvO0nB8FWVw==";
uint8_t output[base64::decodeLength(input)];
base64::decode(input, output);

base64::decode(data, result);

Serial.println((const char*)result);
Serial.println((const char*)output);
}

void loop(){}
11 changes: 5 additions & 6 deletions examples/encode/encode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ void setup(){
Serial.begin(115200);
while(!Serial);

const uint8_t data[] = {0x17, 0x77, 0x3B, 0x11, 0x82, 0xA4, 0xC4, 0xC8, 0x27, 0xBC, 0xED, 0x27, 0x07, 0xC1, 0x56, 0x57};
auto dataLength = sizeof(data);
char result[base64::encodeLength(dataLength)];
const uint8_t input[] = {0x17, 0x77, 0x3B, 0x11, 0x82, 0xA4, 0xC4, 0xC8};
auto inputLength = sizeof(input);
char output[base64::encodeLength(inputLength)];
base64::encode(input, inputLength, output);

base64::encode(data, dataLength, result);

Serial.println(result);
Serial.println(output);
}

void loop(){}
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=base64_encode
author=dojyorin
version=2.0.0
version=2.0.1
architectures=*
includes=arduino_base64.hpp
sentence=Convert between binary and Base64 encoded string.
paragraph=Easily convert sensor raw values, structures, etc.
sentence=Convert between binary and base64 encoded string.
paragraph=Easily convert sensor raw values, structures, etc...
category=Other
maintainer=https://github.com/dojyorin
url=https://github.com/dojyorin/arduino_base64.git
49 changes: 33 additions & 16 deletions src/arduino_base64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,55 @@
#include "string.h"

/**
* Convert between binary and Base64 encoded string.
* Convert between binary and base64 encoded string.
* @see https://github.com/dojyorin/arduino_base64
*/
namespace base64{
/**
* Convert binary to Base64 encoded string.
* If the input is string, cast it to `uint8_t*`.
* @param input Binary data.
* @param inputLength Number of input bytes.
* @param output Base64 encoded string.
* Convert binary to base64 encoded string.
* If input is string, cast to `uint8_t*`.
* @example
* ```c++
* const uint8_t input[] = {0x17, 0x77, 0x3B, 0x11, 0x82, 0xA4, 0xC4, 0xC8};
* auto inputLength = sizeof(input);
* char output[base64::encodeLength(inputLength)];
* base64::encode(input, inputLength, output);
* ```
*/
void encode(const uint8_t* input, size_t inputLength, char* output);

/**
* Calculate the number of output characters.
* @param inputLength Number of input bytes.
* @return Number of output characters.
* Calculate number of output characters.
* @example
* ```c++
* const uint8_t input[] = {0x17, 0x77, 0x3B, 0x11, 0x82, 0xA4, 0xC4, 0xC8};
* auto inputLength = sizeof(input);
* char output[base64::encodeLength(inputLength)];
* base64::encode(input, inputLength, output);
* ```
*/
size_t encodeLength(size_t inputLength);

/**
* Convert Base64 encoded string to binary.
* If the output is string, cast it to `char*`.
* @param input Base64 encoded string.
* @param output Binary data.
* Convert base64 encoded string to binary.
* If output is string, cast to `char*`.
* @example
* ```c++
* const char input[] = "F3c7EYKkxMgnvO0nB8FWVw==";
* uint8_t output[base64::decodeLength(input)];
* base64::decode(input, output);
* ```
*/
void decode(const char* input, uint8_t* output);

/**
* Calculate the number of output bytes.
* @param input Base64 encoded string.
* @return Number of output bytes.
* Calculate number of output bytes.
* @example
* ```c++
* const char input[] = "F3c7EYKkxMgnvO0nB8FWVw==";
* uint8_t output[base64::decodeLength(input)];
* base64::decode(input, output);
* ```
*/
size_t decodeLength(const char* input);
}
14 changes: 7 additions & 7 deletions src/base64.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "./arduino_base64.hpp"

namespace{
constexpr char token[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
constexpr char symbols[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

uint8_t tokenOf(char search){
uint8_t indexOf(char search){
for(uint8_t i = 0; i < 64; i++){
if(token[i] == search){
if(symbols[i] == search){
return i;
}
}

return 255;
return 0xFF;
}

void to6x4(uint8_t* input, uint8_t* output){
Expand Down Expand Up @@ -39,7 +39,7 @@ void base64::encode(const uint8_t* input, size_t inputLength, char* output){
to6x4(bit8x3, bit6x4);

for(const auto &v: bit6x4){
*output++ = token[v];
*output++ = symbols[v];
}

position = 0;
Expand All @@ -54,7 +54,7 @@ void base64::encode(const uint8_t* input, size_t inputLength, char* output){
to6x4(bit8x3, bit6x4);

for(uint8_t i = 0; i < position + 1; i++){
*output++ = token[bit6x4[i]];
*output++ = symbols[bit6x4[i]];
}

while(position++ < 3){
Expand All @@ -80,7 +80,7 @@ void base64::decode(const char* input, uint8_t* output){
break;
}

bit6x4[position++] = tokenOf(*input++);
bit6x4[position++] = indexOf(*input++);

if(position == 4){
to8x3(bit6x4, bit8x3);
Expand Down

0 comments on commit b00fdfb

Please sign in to comment.