Skip to content

Commit

Permalink
changed var name.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Jul 22, 2020
1 parent c0a8ead commit 49e869d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ Binary based simple Base64 Codec for Arduino.
## Encode
```c++
const char* rawData = "foobar";
size_t rawSize = strlen(rawData);
size_t rawLength = strlen(rawData);

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

Serial.println(encoded);
```
## Decode
```c++
const char* encodedData = "Zm9vYmFy";
const char* encoded = "Zm9vYmFy";
uint8_t raw[BASE64::decodeLength(encodedData)];
BASE64::decode(encodedData, raw);
uint8_t raw[BASE64::decodeLength(encoded)];
BASE64::decode(encoded, raw);
Serial.println((char*)raw);
```


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

**Return**
- Nothing.
Expand All @@ -40,9 +40,9 @@ Serial.println((char*)raw);
- If the input data is `char*` string, cast it to `uint8_t*`.


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

**Return**
- Number of characters after Base64 encoding.
Expand Down
6 changes: 3 additions & 3 deletions examples/decode/decode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ void setup(){
Serial.begin(115200);
while(!Serial);

const char* encodedData = "Zm9vYmFy";
const char* encoded = "Zm9vYmFy";

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

Serial.println((char*)raw);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/encode/encode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ void setup(){
while(!Serial);

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

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

Serial.println(encoded);
}
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Base64_Codec
version=1.0.2
version=1.0.3
author=dojyorin
maintainer=https://github.com/dojyorin
sentence=Base64 Codec
Expand Down
18 changes: 9 additions & 9 deletions src/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ namespace{
}
}

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

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

if(i == 3){
Expand Down Expand Up @@ -71,17 +71,17 @@ void BASE64::encode(const uint8_t* input, char* output, size_t length){
*output = '\0';
}

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

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

while(length--){
while(inputLength--){
if(*input == '='){
break;
}
Expand Down Expand Up @@ -121,12 +121,12 @@ void BASE64::decode(const char* input, uint8_t* output){
}

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

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

return 6 * length / 8 - eq;
return 6 * inputLength / 8 - eq;
}
4 changes: 2 additions & 2 deletions src/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "string.h"

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

0 comments on commit 49e869d

Please sign in to comment.