Skip to content

Commit 62f38bf

Browse files
committed
add base64 class
1 parent b1e3d22 commit 62f38bf

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

cores/esp8266/base64.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* base64.cpp
3+
*
4+
* Created on: 09.12.2015
5+
*
6+
* Copyright (c) 2015 Markus Sattler. All rights reserved.
7+
* This file is part of the ESP8266 core for Arduino.
8+
*
9+
* This library is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 2.1 of the License, or (at your option) any later version.
13+
*
14+
* This library is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with this library; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*
23+
*/
24+
25+
#include "Arduino.h"
26+
extern "C" {
27+
#include "libb64/cdecode.h"
28+
#include "libb64/cencode.h"
29+
}
30+
#include "base64.h"
31+
32+
/**
33+
* convert input data to base64
34+
* @param data uint8_t *
35+
* @param length size_t
36+
* @return String
37+
*/
38+
String base64::encode(uint8_t * data, size_t length) {
39+
// base64 needs more size then the source data
40+
size_t size = ((length * 1.6f) + 1);
41+
char * buffer = (char *) malloc(size);
42+
if(buffer) {
43+
base64_encodestate _state;
44+
base64_init_encodestate(&_state);
45+
int len = base64_encode_block((const char *) &data[0], length, &buffer[0], &_state);
46+
len = base64_encode_blockend((buffer + len), &_state);
47+
48+
String base64 = String(buffer);
49+
free(buffer);
50+
return base64;
51+
}
52+
return String("-FAIL-");
53+
}
54+
55+
/**
56+
* convert input data to base64
57+
* @param text String
58+
* @return String
59+
*/
60+
String base64::encode(String text) {
61+
return base64::encode((uint8_t *) text.c_str(), text.length());
62+
}
63+

cores/esp8266/base64.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* base64.h
3+
*
4+
* Created on: 09.12.2015
5+
*
6+
* Copyright (c) 2015 Markus Sattler. All rights reserved.
7+
* This file is part of the ESP8266 core for Arduino.
8+
*
9+
* This library is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 2.1 of the License, or (at your option) any later version.
13+
*
14+
* This library is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with this library; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*
23+
*/
24+
25+
#ifndef CORE_BASE64_H_
26+
#define CORE_BASE64_H_
27+
28+
class base64 {
29+
public:
30+
static String encode(uint8_t * data, size_t length);
31+
static String encode(String text);
32+
private:
33+
};
34+
35+
36+
#endif /* CORE_BASE64_H_ */

0 commit comments

Comments
 (0)