|
| 1 | +/** |
| 2 | + * Base64 encoder/decoder for arduino repo |
| 3 | + * Uses common web conventions - '+' for 62, '/' for 63, '=' for padding. |
| 4 | + * Note that invalid base64 characters are interpreted as padding. |
| 5 | + * Author: Densaugeo |
| 6 | + * Maintainer: Densaugeo |
| 7 | + * Version: 1.2.1.1 |
| 8 | + * Changed unsigned int to uint16_t for use in the professional Ender 3V2/S1 firmware |
| 9 | + * Url: https://www.arduino.cc/reference/en/libraries/base64/ |
| 10 | + */ |
| 11 | + |
| 12 | +#ifndef BASE64_H_INCLUDED |
| 13 | +#define BASE64_H_INCLUDED |
| 14 | + |
| 15 | +/* binary_to_base64: |
| 16 | + * Description: |
| 17 | + * Converts a single byte from a binary value to the corresponding base64 character |
| 18 | + * Parameters: |
| 19 | + * v - Byte to convert |
| 20 | + * Returns: |
| 21 | + * ascii code of base64 character. If byte is >= 64, then there is not corresponding base64 character |
| 22 | + * and 255 is returned |
| 23 | + */ |
| 24 | +unsigned char binary_to_base64(unsigned char v); |
| 25 | + |
| 26 | +/* base64_to_binary: |
| 27 | + * Description: |
| 28 | + * Converts a single byte from a base64 character to the corresponding binary value |
| 29 | + * Parameters: |
| 30 | + * c - Base64 character (as ascii code) |
| 31 | + * Returns: |
| 32 | + * 6-bit binary value |
| 33 | + */ |
| 34 | +unsigned char base64_to_binary(unsigned char c); |
| 35 | + |
| 36 | +/* encode_base64_length: |
| 37 | + * Description: |
| 38 | + * Calculates length of base64 string needed for a given number of binary bytes |
| 39 | + * Parameters: |
| 40 | + * input_length - Amount of binary data in bytes |
| 41 | + * Returns: |
| 42 | + * Number of base64 characters needed to encode input_length bytes of binary data |
| 43 | + */ |
| 44 | +uint16_t encode_base64_length(uint16_t input_length); |
| 45 | + |
| 46 | +/* decode_base64_length: |
| 47 | + * Description: |
| 48 | + * Calculates number of bytes of binary data in a base64 string |
| 49 | + * Variant that does not use input_length no longer used within library, retained for API compatibility |
| 50 | + * Parameters: |
| 51 | + * input - Base64-encoded null-terminated string |
| 52 | + * input_length (optional) - Number of bytes to read from input pointer |
| 53 | + * Returns: |
| 54 | + * Number of bytes of binary data in input |
| 55 | + */ |
| 56 | +uint16_t decode_base64_length(unsigned char input[]); |
| 57 | +uint16_t decode_base64_length(unsigned char input[], uint16_t input_length); |
| 58 | + |
| 59 | +/* encode_base64: |
| 60 | + * Description: |
| 61 | + * Converts an array of bytes to a base64 null-terminated string |
| 62 | + * Parameters: |
| 63 | + * input - Pointer to input data |
| 64 | + * input_length - Number of bytes to read from input pointer |
| 65 | + * output - Pointer to output string. Null terminator will be added automatically |
| 66 | + * Returns: |
| 67 | + * Length of encoded string in bytes (not including null terminator) |
| 68 | + */ |
| 69 | +uint16_t encode_base64(unsigned char input[], uint16_t input_length, unsigned char output[]); |
| 70 | + |
| 71 | +/* decode_base64: |
| 72 | + * Description: |
| 73 | + * Converts a base64 null-terminated string to an array of bytes |
| 74 | + * Parameters: |
| 75 | + * input - Pointer to input string |
| 76 | + * input_length (optional) - Number of bytes to read from input pointer |
| 77 | + * output - Pointer to output array |
| 78 | + * Returns: |
| 79 | + * Number of bytes in the decoded binary |
| 80 | + */ |
| 81 | +uint16_t decode_base64(unsigned char input[], unsigned char output[]); |
| 82 | +uint16_t decode_base64(unsigned char input[], uint16_t input_length, unsigned char output[]); |
| 83 | + |
| 84 | +unsigned char binary_to_base64(unsigned char v) { |
| 85 | + // Capital letters - 'A' is ascii 65 and base64 0 |
| 86 | + if (v < 26) return v + 'A'; |
| 87 | + |
| 88 | + // Lowercase letters - 'a' is ascii 97 and base64 26 |
| 89 | + if (v < 52) return v + 71; |
| 90 | + |
| 91 | + // Digits - '0' is ascii 48 and base64 52 |
| 92 | + if (v < 62) return v - 4; |
| 93 | + |
| 94 | + // '+' is ascii 43 and base64 62 |
| 95 | + if (v == 62) return '+'; |
| 96 | + |
| 97 | + // '/' is ascii 47 and base64 63 |
| 98 | + if (v == 63) return '/'; |
| 99 | + |
| 100 | + return 64; |
| 101 | +} |
| 102 | + |
| 103 | +unsigned char base64_to_binary(unsigned char c) { |
| 104 | + // Capital letters - 'A' is ascii 65 and base64 0 |
| 105 | + if ('A' <= c && c <= 'Z') return c - 'A'; |
| 106 | + |
| 107 | + // Lowercase letters - 'a' is ascii 97 and base64 26 |
| 108 | + if ('a' <= c && c <= 'z') return c - 71; |
| 109 | + |
| 110 | + // Digits - '0' is ascii 48 and base64 52 |
| 111 | + if ('0' <= c && c <= '9') return c + 4; |
| 112 | + |
| 113 | + // '+' is ascii 43 and base64 62 |
| 114 | + if (c == '+') return 62; |
| 115 | + |
| 116 | + // '/' is ascii 47 and base64 63 |
| 117 | + if (c == '/') return 63; |
| 118 | + |
| 119 | + return 255; |
| 120 | +} |
| 121 | + |
| 122 | +uint16_t encode_base64_length(uint16_t input_length) { |
| 123 | + return (input_length + 2)/3*4; |
| 124 | +} |
| 125 | + |
| 126 | +uint16_t decode_base64_length(unsigned char input[]) { |
| 127 | + return decode_base64_length(input, -1); |
| 128 | +} |
| 129 | + |
| 130 | +uint16_t decode_base64_length(unsigned char input[], uint16_t input_length) { |
| 131 | + unsigned char *start = input; |
| 132 | + |
| 133 | + while (base64_to_binary(input[0]) < 64 && (unsigned char)(input - start) < input_length) { |
| 134 | + ++input; |
| 135 | + } |
| 136 | + |
| 137 | + input_length = input - start; |
| 138 | + return input_length/4*3 + (input_length % 4 ? input_length % 4 - 1 : 0); |
| 139 | +} |
| 140 | + |
| 141 | +uint16_t encode_base64(unsigned char input[], uint16_t input_length, unsigned char output[]) { |
| 142 | + uint16_t full_sets = input_length/3; |
| 143 | + |
| 144 | + // While there are still full sets of 24 bits... |
| 145 | + for (uint16_t i = 0; i < full_sets; ++i) { |
| 146 | + output[0] = binary_to_base64( input[0] >> 2); |
| 147 | + output[1] = binary_to_base64((input[0] & 0x03) << 4 | input[1] >> 4); |
| 148 | + output[2] = binary_to_base64((input[1] & 0x0F) << 2 | input[2] >> 6); |
| 149 | + output[3] = binary_to_base64( input[2] & 0x3F); |
| 150 | + |
| 151 | + input += 3; |
| 152 | + output += 4; |
| 153 | + } |
| 154 | + |
| 155 | + switch(input_length % 3) { |
| 156 | + case 0: |
| 157 | + output[0] = '\0'; |
| 158 | + break; |
| 159 | + case 1: |
| 160 | + output[0] = binary_to_base64( input[0] >> 2); |
| 161 | + output[1] = binary_to_base64((input[0] & 0x03) << 4); |
| 162 | + output[2] = '='; |
| 163 | + output[3] = '='; |
| 164 | + output[4] = '\0'; |
| 165 | + break; |
| 166 | + case 2: |
| 167 | + output[0] = binary_to_base64( input[0] >> 2); |
| 168 | + output[1] = binary_to_base64((input[0] & 0x03) << 4 | input[1] >> 4); |
| 169 | + output[2] = binary_to_base64((input[1] & 0x0F) << 2); |
| 170 | + output[3] = '='; |
| 171 | + output[4] = '\0'; |
| 172 | + break; |
| 173 | + } |
| 174 | + |
| 175 | + return encode_base64_length(input_length); |
| 176 | +} |
| 177 | + |
| 178 | +uint16_t decode_base64(unsigned char input[], unsigned char output[]) { |
| 179 | + return decode_base64(input, -1, output); |
| 180 | +} |
| 181 | + |
| 182 | +uint16_t decode_base64(unsigned char input[], uint16_t input_length, unsigned char output[]) { |
| 183 | + uint16_t output_length = decode_base64_length(input, input_length); |
| 184 | + |
| 185 | + // While there are still full sets of 24 bits... |
| 186 | + for (uint16_t i = 2; i < output_length; i += 3) { |
| 187 | + output[0] = base64_to_binary(input[0]) << 2 | base64_to_binary(input[1]) >> 4; |
| 188 | + output[1] = base64_to_binary(input[1]) << 4 | base64_to_binary(input[2]) >> 2; |
| 189 | + output[2] = base64_to_binary(input[2]) << 6 | base64_to_binary(input[3]); |
| 190 | + |
| 191 | + input += 4; |
| 192 | + output += 3; |
| 193 | + } |
| 194 | + |
| 195 | + switch(output_length % 3) { |
| 196 | + case 1: |
| 197 | + output[0] = base64_to_binary(input[0]) << 2 | base64_to_binary(input[1]) >> 4; |
| 198 | + break; |
| 199 | + case 2: |
| 200 | + output[0] = base64_to_binary(input[0]) << 2 | base64_to_binary(input[1]) >> 4; |
| 201 | + output[1] = base64_to_binary(input[1]) << 4 | base64_to_binary(input[2]) >> 2; |
| 202 | + break; |
| 203 | + } |
| 204 | + |
| 205 | + return output_length; |
| 206 | +} |
| 207 | + |
| 208 | +#endif // ifndef |
0 commit comments