初始化提交
This commit is contained in:
105
arduino-cli/libraries/rBASE64/README.adoc
Normal file
105
arduino-cli/libraries/rBASE64/README.adoc
Normal file
@@ -0,0 +1,105 @@
|
||||
= rBASE64 Library for Arduino =
|
||||
|
||||
Real Base64 library is a speed optimized flexible implementations of BASE64
|
||||
Encoding and Decoding logic.
|
||||
|
||||
There are native as well as object oriented functions.
|
||||
|
||||
Here is the List of the Native Functions for those who want more Control:
|
||||
|
||||
- `size_t rbase64_encode(char *output, char *input, size_t inputLen)`
|
||||
|
||||
This function helps to Encode the Stream of Bytes provided at the *input* to *output*
|
||||
User needs to ensure that that the Output buffer is adequacy sized.
|
||||
|
||||
- `size_t rbase64_decode(char *output, char *input, size_t inputLen)`
|
||||
|
||||
This function helps to Decode the Stream ob Bytes provided at the *input* to *output*
|
||||
User needs to ensure that that the Output buffer is adequacy sized.
|
||||
|
||||
- `size_t rbase64_enc_len(size_t inputLen)`
|
||||
|
||||
This function can be used to ascertain the maximum size of buffer needed to accommodate
|
||||
the converted BASE64 output
|
||||
|
||||
- `size_t rbase64_dec_len(char *input, size_t inputLen)`
|
||||
|
||||
This function can be used to find out the maximum size of the buffer needed to accommodate
|
||||
the converted original string from BASE64
|
||||
|
||||
|
||||
Here is the List of the OO Function in `rbase64` class:
|
||||
|
||||
- `String rBASE64::encode(uint8_t *data, size_t length)` - For Direct byte Stream
|
||||
|
||||
- `String rBASE64::encode(char *data)` - For NULL Terminated character Array as shown in the **Example Below**
|
||||
|
||||
- `String rBASE64::encode(String text)` - String containing the source
|
||||
|
||||
Function to Convert into BASE64 encoded string else returns the string `"-FAIL"`
|
||||
|
||||
- `String rBASE64::decode(uint8_t *data, size_t length)` - For Direct byte Stream
|
||||
|
||||
- `String rBASE64::decode(char *data)` - For NULL Terminated character Array as shown in the **Example Below**
|
||||
|
||||
- `String rBASE64::decode(String text)` - String containing the source
|
||||
|
||||
Function to Convert back from BASE64 encoding else returns the string `"-FAIL"`
|
||||
|
||||
|
||||
== Example ==
|
||||
|
||||
```arduino
|
||||
#include <rBase64.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println(rbase64.encode("Hello There, I am doing Good."));
|
||||
Serial.println(rbase64.decode("SGVsbG8gVGhlcmUsIEkgYW0gZG9pbmcgR29vZC4="));
|
||||
delay(2000);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
=== Tip : How to test base64 in Python ===
|
||||
|
||||
At the Python terminal prompt (get this by executing `python.exe` on *Windows* or `python` on *Linux* terminal) :
|
||||
|
||||
```python
|
||||
>>> import base64
|
||||
>>> base64.b64encode("Hello There, I am doing Good.")
|
||||
'SGVsbG8gVGhlcmUsIEkgYW0gZG9pbmcgR29vZC4='
|
||||
>>> base64.b64decode("SGVsbG8gVGhlcmUsIEkgYW0gZG9pbmcgR29vZC4=")
|
||||
'Hello There, I am doing Good.'
|
||||
>>>
|
||||
```
|
||||
|
||||
This way one can also verify other strings.
|
||||
|
||||
|
||||
=== Dependencies ===
|
||||
WString Library
|
||||
Thread Safe: No
|
||||
Extendable: Yes
|
||||
|
||||
|
||||
=== Limitations ===
|
||||
|
||||
==== OO Implementation Limits:
|
||||
- Can't take Encodeing strings more than "70 Characters".
|
||||
- Can't Decode the BASE64 strings beyond "100 Characters".
|
||||
- Memory usage is approximately 280Bytes in RAM.
|
||||
|
||||
==== The Native Implementation has no such Limiations.
|
||||
|
||||
For more information about this library please view the code at
|
||||
http://github.com/boseji/rBASE64
|
||||
|
||||
|
||||
== License ==
|
||||
|
||||
Released Under creative commons license 3.0: Attribution-ShareAlike CC BY-SA
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
|
||||
Easy to Use example of rBASE64 Library
|
||||
|
||||
This example shows the calling convention for the various functions.
|
||||
|
||||
For more information about this library please visit us at
|
||||
http://github.com/boseji/BASE64
|
||||
|
||||
Created by Abhijit Bose (boseji) on 22/02/16.
|
||||
Copyright 2016 - Under creative commons license 3.0:
|
||||
Attribution-ShareAlike CC BY-SA
|
||||
|
||||
@version API 1.0.0
|
||||
@author boseji - salearj@hotmail.com
|
||||
|
||||
*/
|
||||
|
||||
#include <rBase64.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println(rbase64.encode("Hello There, I am doing Good."));
|
||||
Serial.println(rbase64.decode("SGVsbG8gVGhlcmUsIEkgYW0gZG9pbmcgR29vZC4="));
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
27
arduino-cli/libraries/rBASE64/keywords.txt
Normal file
27
arduino-cli/libraries/rBASE64/keywords.txt
Normal file
@@ -0,0 +1,27 @@
|
||||
###########################################
|
||||
# Syntax Coloring Map For rBase64
|
||||
###########################################
|
||||
|
||||
###########################################
|
||||
# Datatypes (KEYWORD1)
|
||||
###########################################
|
||||
|
||||
rbase64 KEYWORD1 rbase64
|
||||
Base64 KEYWORD1 Base64
|
||||
|
||||
###########################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
###########################################
|
||||
|
||||
rbase64_encode KEYWORD2
|
||||
rbase64_decode KEYWORD2
|
||||
rbase64_enc_len KEYWORD2
|
||||
rbase64_dec_len KEYWORD2
|
||||
encode KEYWORD2
|
||||
decode KEYWORD2
|
||||
|
||||
###########################################
|
||||
# Constants (LITERAL1)
|
||||
###########################################
|
||||
|
||||
|
||||
9
arduino-cli/libraries/rBASE64/library.properties
Normal file
9
arduino-cli/libraries/rBASE64/library.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
name=rBase64
|
||||
version=1.0.0
|
||||
author=Abhijit Bose
|
||||
maintainer=boseji <salearj@hotmail.com>
|
||||
sentence=Real BASE64 Function Library
|
||||
paragraph=Helps to Encode and Decode in BASE64 form using simple String operations
|
||||
category=Communication
|
||||
url=http://github.com/boseji/rBASE64
|
||||
architectures=*
|
||||
262
arduino-cli/libraries/rBASE64/src/rBase64.cpp
Normal file
262
arduino-cli/libraries/rBASE64/src/rBase64.cpp
Normal file
@@ -0,0 +1,262 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
// Created by Abhijit Bose (boseji) on 22/02/16.
|
||||
// Copyright 2016 - Under creative commons license 3.0:
|
||||
// Attribution-ShareAlike CC BY-SA
|
||||
//
|
||||
// This software is furnished "as is", without technical support, and with no
|
||||
// warranty, express or implied, as to its usefulness for any purpose.
|
||||
//
|
||||
// Thread Safe: No
|
||||
// Extendable: Yes
|
||||
//
|
||||
// @file rBase64.cpp
|
||||
//
|
||||
// @brief
|
||||
// Library to provide the BASE64 conversion and vis-versa
|
||||
//
|
||||
// @version API 1.0.0
|
||||
//
|
||||
//
|
||||
// @author boseji - salearj@hotmail.com
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "rBase64.h"
|
||||
|
||||
const char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
// Internal Buffer
|
||||
char buf[128];
|
||||
/* 'Private' declarations */
|
||||
inline void a3_to_a4(unsigned char * a4, unsigned char * a3);
|
||||
inline void a4_to_a3(unsigned char * a3, unsigned char * a4);
|
||||
inline unsigned char b64_lookup(char c);
|
||||
|
||||
size_t rbase64_encode(char *output, char *input, size_t inputLen) {
|
||||
int i = 0, j = 0;
|
||||
size_t encLen = 0;
|
||||
unsigned char a3[3];
|
||||
unsigned char a4[4];
|
||||
|
||||
while(inputLen--) {
|
||||
a3[i++] = *(input++);
|
||||
if(i == 3) {
|
||||
a3_to_a4(a4, a3);
|
||||
|
||||
for(i = 0; i < 4; i++) {
|
||||
output[encLen++] = b64_alphabet[a4[i]];
|
||||
}
|
||||
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(i) {
|
||||
for(j = i; j < 3; j++) {
|
||||
a3[j] = '\0';
|
||||
}
|
||||
|
||||
a3_to_a4(a4, a3);
|
||||
|
||||
for(j = 0; j < i + 1; j++) {
|
||||
output[encLen++] = b64_alphabet[a4[j]];
|
||||
}
|
||||
|
||||
while((i++ < 3)) {
|
||||
output[encLen++] = '=';
|
||||
}
|
||||
}
|
||||
output[encLen] = '\0';
|
||||
return encLen;
|
||||
}
|
||||
|
||||
size_t rbase64_decode(char * output, char * input, size_t inputLen) {
|
||||
int i = 0, j = 0;
|
||||
size_t decLen = 0;
|
||||
unsigned char a3[3];
|
||||
unsigned char a4[4];
|
||||
|
||||
|
||||
while (inputLen--) {
|
||||
if(*input == '=') {
|
||||
break;
|
||||
}
|
||||
|
||||
a4[i++] = *(input++);
|
||||
if (i == 4) {
|
||||
for (i = 0; i <4; i++) {
|
||||
a4[i] = b64_lookup(a4[i]);
|
||||
}
|
||||
|
||||
a4_to_a3(a3,a4);
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
output[decLen++] = a3[i];
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i) {
|
||||
for (j = i; j < 4; j++) {
|
||||
a4[j] = '\0';
|
||||
}
|
||||
|
||||
for (j = 0; j <4; j++) {
|
||||
a4[j] = b64_lookup(a4[j]);
|
||||
}
|
||||
|
||||
a4_to_a3(a3,a4);
|
||||
|
||||
for (j = 0; j < i - 1; j++) {
|
||||
output[decLen++] = a3[j];
|
||||
}
|
||||
}
|
||||
output[decLen] = '\0';
|
||||
return decLen;
|
||||
}
|
||||
|
||||
size_t rbase64_enc_len(size_t plainLen) {
|
||||
size_t n = plainLen;
|
||||
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
|
||||
}
|
||||
|
||||
size_t rbase64_dec_len(char * input, size_t inputLen) {
|
||||
uint32_t i = 0;
|
||||
size_t numEq = 0;
|
||||
for(i = inputLen - 1; input[i] == '='; i--) {
|
||||
numEq++;
|
||||
}
|
||||
|
||||
return (size_t)(((6 * inputLen) / 8) - numEq);
|
||||
}
|
||||
|
||||
inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
|
||||
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);
|
||||
}
|
||||
|
||||
inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
|
||||
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
|
||||
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
|
||||
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
|
||||
}
|
||||
|
||||
inline unsigned char b64_lookup(char c) {
|
||||
int i;
|
||||
for(i = 0; i < 64; i++) {
|
||||
if(b64_alphabet[i] == c) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to Encode the Byte Array to a BASE64 encoded String
|
||||
*
|
||||
* @param data Pointer to the Source Buffer
|
||||
* @param length Source Buffer Length
|
||||
*
|
||||
* @return Encoded String else the '-FAIL-' string in case of Error
|
||||
*/
|
||||
String rBASE64::encode(uint8_t *data, size_t length)
|
||||
{
|
||||
size_t o_length = rbase64_enc_len(length);
|
||||
String s = "-FAIL-";
|
||||
// Check Size
|
||||
if(o_length < 128)
|
||||
{
|
||||
s.reserve(o_length);
|
||||
|
||||
/* Make sure that the Length is Ok for the Output */
|
||||
if(o_length == rbase64_encode(buf,(char *)data,length))
|
||||
{
|
||||
s = String(buf);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to Encode the Byte Array to a BASE64 encoded String
|
||||
*
|
||||
* @param data Pointer to the Source Buffer
|
||||
* @waring This function assumes that the Input String is a NULL terminated buffer
|
||||
*
|
||||
* @return Encoded String else the '-FAIL-' string in case of Error
|
||||
*/
|
||||
String rBASE64::encode(char *data)
|
||||
{
|
||||
return rBASE64::encode((uint8_t *)data, strlen(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to Encode the Byte Array to a BASE64 encoded String
|
||||
*
|
||||
* @param text String containing the Source Buffer
|
||||
*
|
||||
* @return Encoded String else the '-FAIL-' string in case of Error
|
||||
*/
|
||||
String rBASE64::encode(String text)
|
||||
{
|
||||
return rBASE64::encode((uint8_t *) text.c_str(), text.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to Decode the Byte Array with BASE64 encoded String to Normal String
|
||||
*
|
||||
* @param data Pointer to the Source Buffer
|
||||
* @param length Source Buffer Length
|
||||
*
|
||||
* @return Decoded String else the '-FAIL-' string in case of Error
|
||||
*/
|
||||
String rBASE64::decode(uint8_t *data, size_t length)
|
||||
{
|
||||
size_t o_length = rbase64_dec_len((char *)data, length);
|
||||
String s = "-FAIL-";
|
||||
|
||||
// Check Size
|
||||
if(o_length < 128)
|
||||
{
|
||||
/* Make sure that the Length is Ok for the Output */
|
||||
if(o_length == rbase64_decode(buf,(char *)data,length))
|
||||
{
|
||||
s = String(buf);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to Decode the Byte Array with BASE64 encoded String to Normal String
|
||||
*
|
||||
* @param data Pointer to the Source Buffer
|
||||
* @waring This function assumes that the Input String is a NULL terminated buffer
|
||||
*
|
||||
* @return Decoded String else the '-FAIL-' string in case of Error
|
||||
*/
|
||||
String rBASE64::decode(char *data)
|
||||
{
|
||||
return rBASE64::decode((uint8_t *)data, strlen(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to Decode the Byte Array with BASE64 encoded String to Normal String
|
||||
*
|
||||
* @param text String containing the Source Buffer
|
||||
*
|
||||
* @return Decoded String else the '-FAIL-' string in case of Error
|
||||
*/
|
||||
String rBASE64::decode(String text)
|
||||
{
|
||||
return rBASE64::decode((uint8_t *) text.c_str(), text.length());
|
||||
}
|
||||
|
||||
/* Declaring the Main Class Instance */
|
||||
rBASE64 rbase64;
|
||||
116
arduino-cli/libraries/rBASE64/src/rBase64.h
Normal file
116
arduino-cli/libraries/rBASE64/src/rBase64.h
Normal file
@@ -0,0 +1,116 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
// Created by Abhijit Bose (boseji) on 22/02/16.
|
||||
// Copyright 2016 - Under creative commons license 3.0:
|
||||
// Attribution-ShareAlike CC BY-SA
|
||||
//
|
||||
// This software is furnished "as is", without technical support, and with no
|
||||
// warranty, express or implied, as to its usefulness for any purpose.
|
||||
//
|
||||
// Thread Safe: No
|
||||
// Extendable: Yes
|
||||
//
|
||||
// @file rBase64.h
|
||||
//
|
||||
// @brief
|
||||
// Library to provide the BASE64 conversion and vis-versa
|
||||
//
|
||||
// @attribution
|
||||
// This is based on the prior work done by Markus Sattler for ESP8266 BASE64
|
||||
// implementation and Per Ejeklint for ArduinoWebsocketServer project
|
||||
//
|
||||
// @version API 1.0.0
|
||||
//
|
||||
//
|
||||
// @author boseji - salearj@hotmail.com
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#ifndef _RBASE64_H
|
||||
#define _RBASE64_H
|
||||
|
||||
/* b64_alphabet:
|
||||
* Description: Base64 alphabet table, a mapping between integers
|
||||
* and base64 digits
|
||||
* Notes: This is an extern here but is defined in Base64.c
|
||||
*/
|
||||
extern const char b64_alphabet[];
|
||||
|
||||
/* base64_encode:
|
||||
* Description:
|
||||
* Encode a string of characters as base64
|
||||
* Parameters:
|
||||
* output: the output buffer for the encoding, stores the encoded string
|
||||
* input: the input buffer for the encoding, stores the binary to be encoded
|
||||
* inputLen: the length of the input buffer, in bytes
|
||||
* Return value:
|
||||
* Returns the length of the encoded string
|
||||
* Requirements:
|
||||
* 1. output must not be null or empty
|
||||
* 2. input must not be null
|
||||
* 3. inputLen must be greater than or equal to 0
|
||||
*/
|
||||
size_t rbase64_encode(char *output, char *input, size_t inputLen);
|
||||
|
||||
/* base64_decode:
|
||||
* Description:
|
||||
* Decode a base64 encoded string into bytes
|
||||
* Parameters:
|
||||
* output: the output buffer for the decoding,
|
||||
* stores the decoded binary
|
||||
* input: the input buffer for the decoding,
|
||||
* stores the base64 string to be decoded
|
||||
* inputLen: the length of the input buffer, in bytes
|
||||
* Return value:
|
||||
* Returns the length of the decoded string
|
||||
* Requirements:
|
||||
* 1. output must not be null or empty
|
||||
* 2. input must not be null
|
||||
* 3. inputLen must be greater than or equal to 0
|
||||
*/
|
||||
size_t rbase64_decode(char *output, char *input, size_t inputLen);
|
||||
|
||||
/* base64_enc_len:
|
||||
* Description:
|
||||
* Returns the length of a base64 encoded string whose decoded
|
||||
* form is inputLen bytes long
|
||||
* Parameters:
|
||||
* inputLen: the length of the decoded string
|
||||
* Return value:
|
||||
* The length of a base64 encoded string whose decoded form
|
||||
* is inputLen bytes long
|
||||
* Requirements:
|
||||
* None
|
||||
*/
|
||||
size_t rbase64_enc_len(size_t inputLen);
|
||||
|
||||
/* base64_dec_len:
|
||||
* Description:
|
||||
* Returns the length of the decoded form of a
|
||||
* base64 encoded string
|
||||
* Parameters:
|
||||
* input: the base64 encoded string to be measured
|
||||
* inputLen: the length of the base64 encoded string
|
||||
* Return value:
|
||||
* Returns the length of the decoded form of a
|
||||
* base64 encoded string
|
||||
* Requirements:
|
||||
* 1. input must not be null
|
||||
* 2. input must be greater than or equal to zero
|
||||
*/
|
||||
size_t rbase64_dec_len(char *input, size_t inputLen);
|
||||
|
||||
/*
|
||||
Base 64 Class for Implementing the bidirectional transactions on BASE64
|
||||
*/
|
||||
class rBASE64 {
|
||||
public:
|
||||
String encode(uint8_t *data, size_t length);
|
||||
String encode(char *data);
|
||||
String encode(String text);
|
||||
String decode(uint8_t *data, size_t length);
|
||||
String decode(char *data);
|
||||
String decode(String text);
|
||||
};
|
||||
|
||||
extern rBASE64 rbase64;
|
||||
|
||||
#endif // _RBASE64_H
|
||||
Reference in New Issue
Block a user