binary.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014-2017 Paul Sokolovsky
  7. * Copyright (c) 2014-2019 Damien P. George
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <stddef.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include "py/binary.h"
  33. #include "py/smallint.h"
  34. #include "py/objint.h"
  35. #include "py/runtime.h"
  36. // Helpers to work with binary-encoded data
  37. #ifndef alignof
  38. #define alignof(type) offsetof(struct { char c; type t; }, t)
  39. #endif
  40. size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) {
  41. size_t size = 0;
  42. int align = 1;
  43. switch (struct_type) {
  44. case '<': case '>':
  45. switch (val_type) {
  46. case 'b': case 'B':
  47. size = 1; break;
  48. case 'h': case 'H':
  49. size = 2; break;
  50. case 'i': case 'I':
  51. size = 4; break;
  52. case 'l': case 'L':
  53. size = 4; break;
  54. case 'q': case 'Q':
  55. size = 8; break;
  56. case 'P': case 'O': case 'S':
  57. size = sizeof(void*); break;
  58. case 'f':
  59. size = sizeof(float); break;
  60. case 'd':
  61. size = sizeof(double); break;
  62. }
  63. break;
  64. case '@': {
  65. // TODO:
  66. // The simplest heuristic for alignment is to align by value
  67. // size, but that doesn't work for "bigger than int" types,
  68. // for example, long long may very well have long alignment
  69. // So, we introduce separate alignment handling, but having
  70. // formal support for that is different from actually supporting
  71. // particular (or any) ABI.
  72. switch (val_type) {
  73. case BYTEARRAY_TYPECODE:
  74. case 'b': case 'B':
  75. align = size = 1; break;
  76. case 'h': case 'H':
  77. align = alignof(short);
  78. size = sizeof(short); break;
  79. case 'i': case 'I':
  80. align = alignof(int);
  81. size = sizeof(int); break;
  82. case 'l': case 'L':
  83. align = alignof(long);
  84. size = sizeof(long); break;
  85. case 'q': case 'Q':
  86. align = alignof(long long);
  87. size = sizeof(long long); break;
  88. case 'P': case 'O': case 'S':
  89. align = alignof(void*);
  90. size = sizeof(void*); break;
  91. case 'f':
  92. align = alignof(float);
  93. size = sizeof(float); break;
  94. case 'd':
  95. align = alignof(double);
  96. size = sizeof(double); break;
  97. }
  98. }
  99. }
  100. if (size == 0) {
  101. mp_raise_ValueError("bad typecode");
  102. }
  103. if (palign != NULL) {
  104. *palign = align;
  105. }
  106. return size;
  107. }
  108. mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) {
  109. mp_int_t val = 0;
  110. switch (typecode) {
  111. case 'b':
  112. val = ((signed char*)p)[index];
  113. break;
  114. case BYTEARRAY_TYPECODE:
  115. case 'B':
  116. val = ((unsigned char*)p)[index];
  117. break;
  118. case 'h':
  119. val = ((short*)p)[index];
  120. break;
  121. case 'H':
  122. val = ((unsigned short*)p)[index];
  123. break;
  124. case 'i':
  125. return mp_obj_new_int(((int*)p)[index]);
  126. case 'I':
  127. return mp_obj_new_int_from_uint(((unsigned int*)p)[index]);
  128. case 'l':
  129. return mp_obj_new_int(((long*)p)[index]);
  130. case 'L':
  131. return mp_obj_new_int_from_uint(((unsigned long*)p)[index]);
  132. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  133. case 'q':
  134. return mp_obj_new_int_from_ll(((long long*)p)[index]);
  135. case 'Q':
  136. return mp_obj_new_int_from_ull(((unsigned long long*)p)[index]);
  137. #endif
  138. #if MICROPY_PY_BUILTINS_FLOAT
  139. case 'f':
  140. return mp_obj_new_float(((float*)p)[index]);
  141. case 'd':
  142. return mp_obj_new_float(((double*)p)[index]);
  143. #endif
  144. // Extension to CPython: array of objects
  145. case 'O':
  146. return ((mp_obj_t*)p)[index];
  147. // Extension to CPython: array of pointers
  148. case 'P':
  149. return mp_obj_new_int((mp_int_t)(uintptr_t)((void**)p)[index]);
  150. }
  151. return MP_OBJ_NEW_SMALL_INT(val);
  152. }
  153. // The long long type is guaranteed to hold at least 64 bits, and size is at
  154. // most 8 (for q and Q), so we will always be able to parse the given data
  155. // and fit it into a long long.
  156. long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const byte *src) {
  157. int delta;
  158. if (!big_endian) {
  159. delta = -1;
  160. src += size - 1;
  161. } else {
  162. delta = 1;
  163. }
  164. long long val = 0;
  165. if (is_signed && *src & 0x80) {
  166. val = -1;
  167. }
  168. for (uint i = 0; i < size; i++) {
  169. val <<= 8;
  170. val |= *src;
  171. src += delta;
  172. }
  173. return val;
  174. }
  175. #define is_signed(typecode) (typecode > 'Z')
  176. mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr) {
  177. byte *p = *ptr;
  178. size_t align;
  179. size_t size = mp_binary_get_size(struct_type, val_type, &align);
  180. if (struct_type == '@') {
  181. // Align p relative to p_base
  182. p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
  183. #if MP_ENDIANNESS_LITTLE
  184. struct_type = '<';
  185. #else
  186. struct_type = '>';
  187. #endif
  188. }
  189. *ptr = p + size;
  190. long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
  191. if (val_type == 'O') {
  192. return (mp_obj_t)(mp_uint_t)val;
  193. } else if (val_type == 'S') {
  194. const char *s_val = (const char*)(uintptr_t)(mp_uint_t)val;
  195. return mp_obj_new_str(s_val, strlen(s_val));
  196. #if MICROPY_PY_BUILTINS_FLOAT
  197. } else if (val_type == 'f') {
  198. union { uint32_t i; float f; } fpu = {val};
  199. return mp_obj_new_float(fpu.f);
  200. } else if (val_type == 'd') {
  201. union { uint64_t i; double f; } fpu = {val};
  202. return mp_obj_new_float(fpu.f);
  203. #endif
  204. } else if (is_signed(val_type)) {
  205. if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
  206. return mp_obj_new_int((mp_int_t)val);
  207. } else {
  208. return mp_obj_new_int_from_ll(val);
  209. }
  210. } else {
  211. if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
  212. return mp_obj_new_int_from_uint((mp_uint_t)val);
  213. } else {
  214. return mp_obj_new_int_from_ull(val);
  215. }
  216. }
  217. }
  218. void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
  219. if (MP_ENDIANNESS_LITTLE && !big_endian) {
  220. memcpy(dest, &val, val_sz);
  221. } else if (MP_ENDIANNESS_BIG && big_endian) {
  222. // only copy the least-significant val_sz bytes
  223. memcpy(dest, (byte*)&val + sizeof(mp_uint_t) - val_sz, val_sz);
  224. } else {
  225. const byte *src;
  226. if (MP_ENDIANNESS_LITTLE) {
  227. src = (const byte*)&val + val_sz;
  228. } else {
  229. src = (const byte*)&val + sizeof(mp_uint_t);
  230. }
  231. while (val_sz--) {
  232. *dest++ = *--src;
  233. }
  234. }
  235. }
  236. void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr) {
  237. byte *p = *ptr;
  238. size_t align;
  239. size_t size = mp_binary_get_size(struct_type, val_type, &align);
  240. if (struct_type == '@') {
  241. // Align p relative to p_base
  242. p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
  243. if (MP_ENDIANNESS_LITTLE) {
  244. struct_type = '<';
  245. } else {
  246. struct_type = '>';
  247. }
  248. }
  249. *ptr = p + size;
  250. mp_uint_t val;
  251. switch (val_type) {
  252. case 'O':
  253. val = (mp_uint_t)val_in;
  254. break;
  255. #if MICROPY_PY_BUILTINS_FLOAT
  256. case 'f': {
  257. union { uint32_t i; float f; } fp_sp;
  258. fp_sp.f = mp_obj_get_float(val_in);
  259. val = fp_sp.i;
  260. break;
  261. }
  262. case 'd': {
  263. union { uint64_t i64; uint32_t i32[2]; double f; } fp_dp;
  264. fp_dp.f = mp_obj_get_float(val_in);
  265. if (BYTES_PER_WORD == 8) {
  266. val = fp_dp.i64;
  267. } else {
  268. int be = struct_type == '>';
  269. mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]);
  270. p += sizeof(uint32_t);
  271. val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be];
  272. }
  273. break;
  274. }
  275. #endif
  276. default:
  277. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  278. if (mp_obj_is_type(val_in, &mp_type_int)) {
  279. mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p);
  280. return;
  281. } else
  282. #endif
  283. {
  284. val = mp_obj_get_int(val_in);
  285. // zero/sign extend if needed
  286. if (BYTES_PER_WORD < 8 && size > sizeof(val)) {
  287. int c = (is_signed(val_type) && (mp_int_t)val < 0) ? 0xff : 0x00;
  288. memset(p, c, size);
  289. if (struct_type == '>') {
  290. p += size - sizeof(val);
  291. }
  292. }
  293. }
  294. }
  295. mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
  296. }
  297. void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in) {
  298. switch (typecode) {
  299. #if MICROPY_PY_BUILTINS_FLOAT
  300. case 'f':
  301. ((float*)p)[index] = mp_obj_get_float(val_in);
  302. break;
  303. case 'd':
  304. ((double*)p)[index] = mp_obj_get_float(val_in);
  305. break;
  306. #endif
  307. // Extension to CPython: array of objects
  308. case 'O':
  309. ((mp_obj_t*)p)[index] = val_in;
  310. break;
  311. default:
  312. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  313. if (mp_obj_is_type(val_in, &mp_type_int)) {
  314. size_t size = mp_binary_get_size('@', typecode, NULL);
  315. mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
  316. size, (uint8_t*)p + index * size);
  317. return;
  318. }
  319. #endif
  320. mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
  321. }
  322. }
  323. void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val) {
  324. switch (typecode) {
  325. case 'b':
  326. ((signed char*)p)[index] = val;
  327. break;
  328. case BYTEARRAY_TYPECODE:
  329. case 'B':
  330. ((unsigned char*)p)[index] = val;
  331. break;
  332. case 'h':
  333. ((short*)p)[index] = val;
  334. break;
  335. case 'H':
  336. ((unsigned short*)p)[index] = val;
  337. break;
  338. case 'i':
  339. ((int*)p)[index] = val;
  340. break;
  341. case 'I':
  342. ((unsigned int*)p)[index] = val;
  343. break;
  344. case 'l':
  345. ((long*)p)[index] = val;
  346. break;
  347. case 'L':
  348. ((unsigned long*)p)[index] = val;
  349. break;
  350. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  351. case 'q':
  352. ((long long*)p)[index] = val;
  353. break;
  354. case 'Q':
  355. ((unsigned long long*)p)[index] = val;
  356. break;
  357. #endif
  358. #if MICROPY_PY_BUILTINS_FLOAT
  359. case 'f':
  360. ((float*)p)[index] = val;
  361. break;
  362. case 'd':
  363. ((double*)p)[index] = val;
  364. break;
  365. #endif
  366. // Extension to CPython: array of pointers
  367. case 'P':
  368. ((void**)p)[index] = (void*)(uintptr_t)val;
  369. break;
  370. }
  371. }