mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Base64: add option to disable any newlines in output. (#3208)
Closes #3194
This commit is contained in:
parent
ca3a1728d0
commit
157698bef9
13
cores/esp8266/base64.cpp
Normal file → Executable file
13
cores/esp8266/base64.cpp
Normal file → Executable file
@ -35,13 +35,20 @@ extern "C" {
|
|||||||
* @param length size_t
|
* @param length size_t
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
String base64::encode(uint8_t * data, size_t length) {
|
String base64::encode(uint8_t * data, size_t length, bool doNewLines) {
|
||||||
// base64 needs more size then the source data
|
// base64 needs more size then the source data
|
||||||
size_t size = ((length * 1.6f) + 1);
|
size_t size = ((length * 1.6f) + 1);
|
||||||
char * buffer = (char *) malloc(size);
|
char * buffer = (char *) malloc(size);
|
||||||
if(buffer) {
|
if(buffer) {
|
||||||
base64_encodestate _state;
|
base64_encodestate _state;
|
||||||
|
if(doNewLines)
|
||||||
|
{
|
||||||
base64_init_encodestate(&_state);
|
base64_init_encodestate(&_state);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
base64_init_encodestate_nonewlines(&_state);
|
||||||
|
}
|
||||||
int len = base64_encode_block((const char *) &data[0], length, &buffer[0], &_state);
|
int len = base64_encode_block((const char *) &data[0], length, &buffer[0], &_state);
|
||||||
len = base64_encode_blockend((buffer + len), &_state);
|
len = base64_encode_blockend((buffer + len), &_state);
|
||||||
|
|
||||||
@ -57,7 +64,7 @@ String base64::encode(uint8_t * data, size_t length) {
|
|||||||
* @param text String
|
* @param text String
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
String base64::encode(String text) {
|
String base64::encode(String text, bool doNewLines) {
|
||||||
return base64::encode((uint8_t *) text.c_str(), text.length());
|
return base64::encode((uint8_t *) text.c_str(), text.length(), doNewLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
7
cores/esp8266/base64.h
Normal file → Executable file
7
cores/esp8266/base64.h
Normal file → Executable file
@ -27,8 +27,11 @@
|
|||||||
|
|
||||||
class base64 {
|
class base64 {
|
||||||
public:
|
public:
|
||||||
static String encode(uint8_t * data, size_t length);
|
// NOTE: The default behaviour of backend (lib64)
|
||||||
static String encode(String text);
|
// is to add a newline every 72 (encoded) characters output.
|
||||||
|
// This may 'break' longer uris and json variables
|
||||||
|
static String encode(uint8_t * data, size_t length, bool doNewLines = true);
|
||||||
|
static String encode(String text, bool doNewLines = true);
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -13,6 +13,13 @@ void base64_init_encodestate(base64_encodestate* state_in){
|
|||||||
state_in->step = step_A;
|
state_in->step = step_A;
|
||||||
state_in->result = 0;
|
state_in->result = 0;
|
||||||
state_in->stepcount = 0;
|
state_in->stepcount = 0;
|
||||||
|
state_in->stepsnewline = CHARS_PER_LINE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void base64_init_encodestate_nonewlines(base64_encodestate* state_in){
|
||||||
|
base64_init_encodestate(state_in);
|
||||||
|
state_in->stepsnewline = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char base64_encode_value(char value_in){
|
char base64_encode_value(char value_in){
|
||||||
@ -65,7 +72,7 @@ int base64_encode_block(const char* plaintext_in, int length_in, char* code_out,
|
|||||||
*codechar++ = base64_encode_value(result);
|
*codechar++ = base64_encode_value(result);
|
||||||
|
|
||||||
++(state_in->stepcount);
|
++(state_in->stepcount);
|
||||||
if (state_in->stepcount == CHARS_PER_LINE/4){
|
if ((state_in->stepcount == CHARS_PER_LINE/4) && (state_in->stepsnewline > 0)){
|
||||||
*codechar++ = '\n';
|
*codechar++ = '\n';
|
||||||
state_in->stepcount = 0;
|
state_in->stepcount = 0;
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,11 @@ typedef struct {
|
|||||||
base64_encodestep step;
|
base64_encodestep step;
|
||||||
char result;
|
char result;
|
||||||
int stepcount;
|
int stepcount;
|
||||||
|
int stepsnewline;
|
||||||
} base64_encodestate;
|
} base64_encodestate;
|
||||||
|
|
||||||
void base64_init_encodestate(base64_encodestate* state_in);
|
void base64_init_encodestate(base64_encodestate* state_in);
|
||||||
|
void base64_init_encodestate_nonewlines(base64_encodestate* state_in);
|
||||||
|
|
||||||
char base64_encode_value(char value_in);
|
char base64_encode_value(char value_in);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user