Skip to content

Commit

Permalink
WIP: update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin authored May 26, 2023
1 parent 46891ef commit 159a007
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 82 deletions.
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.
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
35 changes: 19 additions & 16 deletions src/arduino_base64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,41 @@
#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++
* ```
*/
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++
* ```
*/
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++
* ```
*/
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++
* ```
*/
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 159a007

Please sign in to comment.