My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base64.hpp 6.6KB

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