├── README.markdown ├── library.properties ├── keywords.txt ├── LICENSE ├── examples └── base64 │ └── base64.ino ├── Base64.h └── Base64.cpp /README.markdown: -------------------------------------------------------------------------------- 1 | Introduction 2 | ------------ 3 | 4 | This library provides methods for encoding binary into base64 strings and the reverse operation. -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=base64 2 | version=1.0.0 3 | author=Adam Rudd 4 | maintainer=Adam Rudd 5 | sentence=A base64 library for the arduino platform, written in C 6 | paragraph= 7 | category=Data Processing 8 | url=https://github.com/adamvr/arduino-base64 9 | architectures=* 10 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Base64 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | # I WANT BASE64.h HIGHLIGHTED, DAMMIT! 10 | Base64 KEYWORD1 11 | 12 | ####################################### 13 | # Methods and Functions (KEYWORD2) 14 | ####################################### 15 | 16 | base64_encode KEYWORD2 17 | base64_decode KEYWORD2 18 | base64_enc_len KEYWORD2 19 | base64_dec_len KEYWORD2 20 | 21 | ####################################### 22 | # Constants (LITERAL1) 23 | ####################################### 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Adam Rudd 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /examples/base64/base64.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | Base64 Encode/Decode example 5 | 6 | Encodes the text "Hello world" to "SGVsbG8gd29ybGQA" and decodes "Zm9vYmFy" to "foobar" 7 | 8 | Created 29 April 2015 9 | by Nathan Friedly - http://nfriedly.com/ 10 | 11 | This example code is in the public domain. 12 | 13 | */ 14 | 15 | 16 | void setup() 17 | { 18 | // start serial port at 9600 bps: 19 | Serial.begin(9600); 20 | while (!Serial) { 21 | ; // wait for serial port to connect. Needed for Leonardo only 22 | } 23 | 24 | Serial.println("Base64 example"); 25 | 26 | 27 | 28 | // encoding 29 | char input[] = "Hello world"; 30 | int inputLen = sizeof(input); 31 | 32 | int encodedLen = base64_enc_len(inputLen); 33 | char encoded[encodedLen]; 34 | 35 | Serial.print(input); Serial.print(" = "); 36 | 37 | // note input is consumed in this step: it will be empty afterwards 38 | base64_encode(encoded, input, inputLen); 39 | 40 | Serial.println(encoded); 41 | 42 | 43 | 44 | // decoding 45 | char input2[] = "Zm9vYmFy"; 46 | int input2Len = sizeof(input2); 47 | 48 | int decodedLen = base64_dec_len(input2, input2Len); 49 | char decoded[decodedLen]; 50 | 51 | base64_decode(decoded, input2, input2Len); 52 | 53 | Serial.print(input2); Serial.print(" = "); Serial.println(decoded); 54 | } 55 | 56 | 57 | void loop() 58 | { 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Base64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Adam Rudd. 3 | * See LICENSE for more information 4 | */ 5 | #ifndef _BASE64_H 6 | #define _BASE64_H 7 | 8 | /* b64_alphabet: 9 | * Description: Base64 alphabet table, a mapping between integers 10 | * and base64 digits 11 | * Notes: This is an extern here but is defined in Base64.c 12 | */ 13 | extern const char b64_alphabet[]; 14 | 15 | /* base64_encode: 16 | * Description: 17 | * Encode a string of characters as base64 18 | * Parameters: 19 | * output: the output buffer for the encoding, stores the encoded string 20 | * input: the input buffer for the encoding, stores the binary to be encoded 21 | * inputLen: the length of the input buffer, in bytes 22 | * Return value: 23 | * Returns the length of the encoded string 24 | * Requirements: 25 | * 1. output must not be null or empty 26 | * 2. input must not be null 27 | * 3. inputLen must be greater than or equal to 0 28 | */ 29 | int base64_encode(char *output, char *input, int inputLen); 30 | 31 | /* base64_decode: 32 | * Description: 33 | * Decode a base64 encoded string into bytes 34 | * Parameters: 35 | * output: the output buffer for the decoding, 36 | * stores the decoded binary 37 | * input: the input buffer for the decoding, 38 | * stores the base64 string to be decoded 39 | * inputLen: the length of the input buffer, in bytes 40 | * Return value: 41 | * Returns the length of the decoded string 42 | * Requirements: 43 | * 1. output must not be null or empty 44 | * 2. input must not be null 45 | * 3. inputLen must be greater than or equal to 0 46 | */ 47 | int base64_decode(char *output, char *input, int inputLen); 48 | 49 | /* base64_enc_len: 50 | * Description: 51 | * Returns the length of a base64 encoded string whose decoded 52 | * form is inputLen bytes long 53 | * Parameters: 54 | * inputLen: the length of the decoded string 55 | * Return value: 56 | * The length of a base64 encoded string whose decoded form 57 | * is inputLen bytes long 58 | * Requirements: 59 | * None 60 | */ 61 | int base64_enc_len(int inputLen); 62 | 63 | /* base64_dec_len: 64 | * Description: 65 | * Returns the length of the decoded form of a 66 | * base64 encoded string 67 | * Parameters: 68 | * input: the base64 encoded string to be measured 69 | * inputLen: the length of the base64 encoded string 70 | * Return value: 71 | * Returns the length of the decoded form of a 72 | * base64 encoded string 73 | * Requirements: 74 | * 1. input must not be null 75 | * 2. input must be greater than or equal to zero 76 | */ 77 | int base64_dec_len(char *input, int inputLen); 78 | 79 | #endif // _BASE64_H 80 | -------------------------------------------------------------------------------- /Base64.cpp: -------------------------------------------------------------------------------- 1 | #include "Base64.h" 2 | #include 3 | const char PROGMEM b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 4 | "abcdefghijklmnopqrstuvwxyz" 5 | "0123456789+/"; 6 | 7 | /* 'Private' declarations */ 8 | inline void a3_to_a4(unsigned char * a4, unsigned char * a3); 9 | inline void a4_to_a3(unsigned char * a3, unsigned char * a4); 10 | inline unsigned char b64_lookup(char c); 11 | 12 | int base64_encode(char *output, char *input, int inputLen) { 13 | int i = 0, j = 0; 14 | int encLen = 0; 15 | unsigned char a3[3]; 16 | unsigned char a4[4]; 17 | 18 | while(inputLen--) { 19 | a3[i++] = *(input++); 20 | if(i == 3) { 21 | a3_to_a4(a4, a3); 22 | 23 | for(i = 0; i < 4; i++) { 24 | output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]); 25 | } 26 | 27 | i = 0; 28 | } 29 | } 30 | 31 | if(i) { 32 | for(j = i; j < 3; j++) { 33 | a3[j] = '\0'; 34 | } 35 | 36 | a3_to_a4(a4, a3); 37 | 38 | for(j = 0; j < i + 1; j++) { 39 | output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]); 40 | } 41 | 42 | while((i++ < 3)) { 43 | output[encLen++] = '='; 44 | } 45 | } 46 | output[encLen] = '\0'; 47 | return encLen; 48 | } 49 | 50 | int base64_decode(char * output, char * input, int inputLen) { 51 | int i = 0, j = 0; 52 | int decLen = 0; 53 | unsigned char a3[3]; 54 | unsigned char a4[4]; 55 | 56 | 57 | while (inputLen--) { 58 | if(*input == '=') { 59 | break; 60 | } 61 | 62 | a4[i++] = *(input++); 63 | if (i == 4) { 64 | for (i = 0; i <4; i++) { 65 | a4[i] = b64_lookup(a4[i]); 66 | } 67 | 68 | a4_to_a3(a3,a4); 69 | 70 | for (i = 0; i < 3; i++) { 71 | output[decLen++] = a3[i]; 72 | } 73 | i = 0; 74 | } 75 | } 76 | 77 | if (i) { 78 | for (j = i; j < 4; j++) { 79 | a4[j] = '\0'; 80 | } 81 | 82 | for (j = 0; j <4; j++) { 83 | a4[j] = b64_lookup(a4[j]); 84 | } 85 | 86 | a4_to_a3(a3,a4); 87 | 88 | for (j = 0; j < i - 1; j++) { 89 | output[decLen++] = a3[j]; 90 | } 91 | } 92 | output[decLen] = '\0'; 93 | return decLen; 94 | } 95 | 96 | int base64_enc_len(int plainLen) { 97 | int n = plainLen; 98 | return (n + 2 - ((n + 2) % 3)) / 3 * 4; 99 | } 100 | 101 | int base64_dec_len(char * input, int inputLen) { 102 | int i = 0; 103 | int numEq = 0; 104 | for(i = inputLen - 1; input[i] == '='; i--) { 105 | numEq++; 106 | } 107 | 108 | return ((6 * inputLen) / 8) - numEq; 109 | } 110 | 111 | inline void a3_to_a4(unsigned char * a4, unsigned char * a3) { 112 | a4[0] = (a3[0] & 0xfc) >> 2; 113 | a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); 114 | a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); 115 | a4[3] = (a3[2] & 0x3f); 116 | } 117 | 118 | inline void a4_to_a3(unsigned char * a3, unsigned char * a4) { 119 | a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4); 120 | a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2); 121 | a3[2] = ((a4[2] & 0x3) << 6) + a4[3]; 122 | } 123 | 124 | inline unsigned char b64_lookup(char c) { 125 | if(c >='A' && c <='Z') return c - 'A'; 126 | if(c >='a' && c <='z') return c - 71; 127 | if(c >='0' && c <='9') return c + 4; 128 | if(c == '+') return 62; 129 | if(c == '/') return 63; 130 | return -1; 131 | } 132 | --------------------------------------------------------------------------------