modubinascii.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Paul Sokolovsky
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <assert.h>
  28. #include <string.h>
  29. #include "py/runtime.h"
  30. #include "py/binary.h"
  31. #include "extmod/modubinascii.h"
  32. mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
  33. // Second argument is for an extension to allow a separator to be used
  34. // between values.
  35. const char *sep = NULL;
  36. mp_buffer_info_t bufinfo;
  37. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  38. // Code below assumes non-zero buffer length when computing size with
  39. // separator, so handle the zero-length case here.
  40. if (bufinfo.len == 0) {
  41. return mp_const_empty_bytes;
  42. }
  43. vstr_t vstr;
  44. size_t out_len = bufinfo.len * 2;
  45. if (n_args > 1) {
  46. // 1-char separator between hex numbers
  47. out_len += bufinfo.len - 1;
  48. sep = mp_obj_str_get_str(args[1]);
  49. }
  50. vstr_init_len(&vstr, out_len);
  51. byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
  52. for (mp_uint_t i = bufinfo.len; i--;) {
  53. byte d = (*in >> 4);
  54. if (d > 9) {
  55. d += 'a' - '9' - 1;
  56. }
  57. *out++ = d + '0';
  58. d = (*in++ & 0xf);
  59. if (d > 9) {
  60. d += 'a' - '9' - 1;
  61. }
  62. *out++ = d + '0';
  63. if (sep != NULL && i != 0) {
  64. *out++ = *sep;
  65. }
  66. }
  67. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  68. }
  69. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify);
  70. mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
  71. mp_buffer_info_t bufinfo;
  72. mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
  73. if ((bufinfo.len & 1) != 0) {
  74. mp_raise_ValueError("odd-length string");
  75. }
  76. vstr_t vstr;
  77. vstr_init_len(&vstr, bufinfo.len / 2);
  78. byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
  79. byte hex_byte = 0;
  80. for (mp_uint_t i = bufinfo.len; i--;) {
  81. byte hex_ch = *in++;
  82. if (unichar_isxdigit(hex_ch)) {
  83. hex_byte += unichar_xdigit_value(hex_ch);
  84. } else {
  85. mp_raise_ValueError("non-hex digit found");
  86. }
  87. if (i & 1) {
  88. hex_byte <<= 4;
  89. } else {
  90. *out++ = hex_byte;
  91. hex_byte = 0;
  92. }
  93. }
  94. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  95. }
  96. MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj, mod_binascii_unhexlify);
  97. // If ch is a character in the base64 alphabet, and is not a pad character, then
  98. // the corresponding integer between 0 and 63, inclusively, is returned.
  99. // Otherwise, -1 is returned.
  100. static int mod_binascii_sextet(byte ch) {
  101. if (ch >= 'A' && ch <= 'Z') {
  102. return ch - 'A';
  103. } else if (ch >= 'a' && ch <= 'z') {
  104. return ch - 'a' + 26;
  105. } else if (ch >= '0' && ch <= '9') {
  106. return ch - '0' + 52;
  107. } else if (ch == '+') {
  108. return 62;
  109. } else if (ch == '/') {
  110. return 63;
  111. } else {
  112. return -1;
  113. }
  114. }
  115. mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) {
  116. mp_buffer_info_t bufinfo;
  117. mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
  118. byte *in = bufinfo.buf;
  119. vstr_t vstr;
  120. vstr_init(&vstr, (bufinfo.len / 4) * 3 + 1); // Potentially over-allocate
  121. byte *out = (byte *)vstr.buf;
  122. uint shift = 0;
  123. int nbits = 0; // Number of meaningful bits in shift
  124. bool hadpad = false; // Had a pad character since last valid character
  125. for (size_t i = 0; i < bufinfo.len; i++) {
  126. if (in[i] == '=') {
  127. if ((nbits == 2) || ((nbits == 4) && hadpad)) {
  128. nbits = 0;
  129. break;
  130. }
  131. hadpad = true;
  132. }
  133. int sextet = mod_binascii_sextet(in[i]);
  134. if (sextet == -1) {
  135. continue;
  136. }
  137. hadpad = false;
  138. shift = (shift << 6) | sextet;
  139. nbits += 6;
  140. if (nbits >= 8) {
  141. nbits -= 8;
  142. out[vstr.len++] = (shift >> nbits) & 0xFF;
  143. }
  144. }
  145. if (nbits) {
  146. mp_raise_ValueError("incorrect padding");
  147. }
  148. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  149. }
  150. MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj, mod_binascii_a2b_base64);
  151. mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) {
  152. mp_buffer_info_t bufinfo;
  153. mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
  154. vstr_t vstr;
  155. vstr_init_len(&vstr, ((bufinfo.len != 0) ? (((bufinfo.len - 1) / 3) + 1) * 4 : 0) + 1);
  156. // First pass, we convert input buffer to numeric base 64 values
  157. byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
  158. mp_uint_t i;
  159. for (i = bufinfo.len; i >= 3; i -= 3) {
  160. *out++ = (in[0] & 0xFC) >> 2;
  161. *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4;
  162. *out++ = (in[1] & 0x0F) << 2 | (in[2] & 0xC0) >> 6;
  163. *out++ = in[2] & 0x3F;
  164. in += 3;
  165. }
  166. if (i != 0) {
  167. *out++ = (in[0] & 0xFC) >> 2;
  168. if (i == 2) {
  169. *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4;
  170. *out++ = (in[1] & 0x0F) << 2;
  171. }
  172. else {
  173. *out++ = (in[0] & 0x03) << 4;
  174. *out++ = 64;
  175. }
  176. *out = 64;
  177. }
  178. // Second pass, we convert number base 64 values to actual base64 ascii encoding
  179. out = (byte*)vstr.buf;
  180. for (mp_uint_t j = vstr.len - 1; j--;) {
  181. if (*out < 26) {
  182. *out += 'A';
  183. } else if (*out < 52) {
  184. *out += 'a' - 26;
  185. } else if (*out < 62) {
  186. *out += '0' - 52;
  187. } else if (*out == 62) {
  188. *out ='+';
  189. } else if (*out == 63) {
  190. *out = '/';
  191. } else {
  192. *out = '=';
  193. }
  194. out++;
  195. }
  196. *out = '\n';
  197. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  198. }
  199. MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64);
  200. #if MICROPY_PY_UBINASCII_CRC32
  201. #include "uzlib/tinf.h"
  202. mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) {
  203. mp_buffer_info_t bufinfo;
  204. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
  205. uint32_t crc = (n_args > 1) ? mp_obj_get_int_truncated(args[1]) : 0;
  206. crc = uzlib_crc32(bufinfo.buf, bufinfo.len, crc ^ 0xffffffff);
  207. return mp_obj_new_int_from_uint(crc ^ 0xffffffff);
  208. }
  209. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_binascii_crc32);
  210. #endif
  211. #if MICROPY_PY_UBINASCII
  212. STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = {
  213. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubinascii) },
  214. { MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) },
  215. { MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) },
  216. { MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) },
  217. { MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) },
  218. #if MICROPY_PY_UBINASCII_CRC32
  219. { MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&mod_binascii_crc32_obj) },
  220. #endif
  221. };
  222. STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);
  223. const mp_obj_module_t mp_module_ubinascii = {
  224. .base = { &mp_type_module },
  225. .globals = (mp_obj_dict_t*)&mp_module_binascii_globals,
  226. };
  227. #endif //MICROPY_PY_UBINASCII