|
| 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 ESP31B 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 const uint8_t * |
| 35 | + * @param length size_t |
| 36 | + * @return String |
| 37 | + */ |
| 38 | +String base64::encode(const uint8_t * data, size_t length) |
| 39 | +{ |
| 40 | + size_t size = base64_encode_expected_len(length) + 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 const String& |
| 58 | + * @return String |
| 59 | + */ |
| 60 | +String base64::encode(const String& text) |
| 61 | +{ |
| 62 | + return base64::encode((uint8_t *) text.c_str(), text.length()); |
| 63 | +} |
| 64 | + |
0 commit comments