From 46891ef454acdb6a10545485fba56f2ccaeb75bf Mon Sep 17 00:00:00 2001 From: Kazuki Ota Date: Fri, 26 May 2023 20:40:19 +0900 Subject: [PATCH 1/3] update workflows. --- .github/workflows/release.yaml | 4 ++-- .github/workflows/test.yaml | 4 ++-- .github/workflows_json/release.json | 2 +- .github/workflows_json/test.json | 2 +- .github/workflows_json/to_yaml.sh | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 307785e..905db04 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,5 +1,5 @@ -name: Release -"on": +name: release +on: push: tags: v[0-9]+.[0-9]+.[0-9]+ jobs: diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3d7c3ad..a7dd708 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,5 +1,5 @@ -name: Test -"on": +name: test +on: push: branches: - dev diff --git a/.github/workflows_json/release.json b/.github/workflows_json/release.json index 10fa5e9..0dab922 100644 --- a/.github/workflows_json/release.json +++ b/.github/workflows_json/release.json @@ -1,5 +1,5 @@ { - "name": "Release", + "name": "release", "on": { "push": { "tags": "v[0-9]+.[0-9]+.[0-9]+" diff --git a/.github/workflows_json/test.json b/.github/workflows_json/test.json index 55df8ed..d8fb271 100644 --- a/.github/workflows_json/test.json +++ b/.github/workflows_json/test.json @@ -1,5 +1,5 @@ { - "name": "Test", + "name": "test", "on": { "push": { "branches": [ diff --git a/.github/workflows_json/to_yaml.sh b/.github/workflows_json/to_yaml.sh index c052906..4878624 100644 --- a/.github/workflows_json/to_yaml.sh +++ b/.github/workflows_json/to_yaml.sh @@ -3,4 +3,4 @@ set -eu cd ${0%/*} -yq -P -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml \ No newline at end of file +yq -o y -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml \ No newline at end of file From 159a0071de077b8d32b0e81ad87943d82d2a363f Mon Sep 17 00:00:00 2001 From: Kazuki Ota Date: Fri, 26 May 2023 21:17:30 +0900 Subject: [PATCH 2/3] WIP: update docs. --- README.md | 62 ++++-------------------------------------- library.properties | 6 ++-- src/arduino_base64.hpp | 35 +++++++++++++----------- src/base64.cpp | 14 +++++----- 4 files changed, 35 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 19a653f..1e5c3e5 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +See [`arduino_base64.hpp`](./src/arduino_base64.hpp) for details. \ No newline at end of file diff --git a/library.properties b/library.properties index cd12cc6..418cc0f 100644 --- a/library.properties +++ b/library.properties @@ -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 \ No newline at end of file diff --git a/src/arduino_base64.hpp b/src/arduino_base64.hpp index e07ba0c..5913ee6 100644 --- a/src/arduino_base64.hpp +++ b/src/arduino_base64.hpp @@ -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); } \ No newline at end of file diff --git a/src/base64.cpp b/src/base64.cpp index 4593d33..4797b03 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -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){ @@ -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; @@ -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){ @@ -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); From 6a524057dd4abfd72b240c1552e7904fe3240168 Mon Sep 17 00:00:00 2001 From: Kazuki Ota Date: Fri, 26 May 2023 21:22:50 +0900 Subject: [PATCH 3/3] update docs. --- examples/decode/decode.ino | 9 ++++----- examples/encode/encode.ino | 11 +++++------ src/arduino_base64.hpp | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/examples/decode/decode.ino b/examples/decode/decode.ino index 275f043..5513849 100644 --- a/examples/decode/decode.ino +++ b/examples/decode/decode.ino @@ -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(){} \ No newline at end of file diff --git a/examples/encode/encode.ino b/examples/encode/encode.ino index 217e0ab..2e6ec72 100644 --- a/examples/encode/encode.ino +++ b/examples/encode/encode.ino @@ -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(){} \ No newline at end of file diff --git a/src/arduino_base64.hpp b/src/arduino_base64.hpp index 5913ee6..4eb78dc 100644 --- a/src/arduino_base64.hpp +++ b/src/arduino_base64.hpp @@ -13,6 +13,10 @@ namespace base64{ * 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); @@ -21,6 +25,10 @@ namespace base64{ * 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); @@ -30,6 +38,9 @@ namespace base64{ * 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); @@ -38,6 +49,9 @@ namespace base64{ * 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);