binary.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 '<':
  45. case '>':
  46. switch (val_type) {
  47. case 'b':
  48. case 'B':
  49. size = 1;
  50. break;
  51. case 'h':
  52. case 'H':
  53. size = 2;
  54. break;
  55. case 'i':
  56. case 'I':
  57. size = 4;
  58. break;
  59. case 'l':
  60. case 'L':
  61. size = 4;
  62. break;
  63. case 'q':
  64. case 'Q':
  65. size = 8;
  66. break;
  67. case 'P':
  68. case 'O':
  69. case 'S':
  70. size = sizeof(void *);
  71. break;
  72. case 'f':
  73. size = sizeof(float);
  74. break;
  75. case 'd':
  76. size = sizeof(double);
  77. break;
  78. }
  79. break;
  80. case '@': {
  81. // TODO:
  82. // The simplest heuristic for alignment is to align by value
  83. // size, but that doesn't work for "bigger than int" types,
  84. // for example, long long may very well have long alignment
  85. // So, we introduce separate alignment handling, but having
  86. // formal support for that is different from actually supporting
  87. // particular (or any) ABI.
  88. switch (val_type) {
  89. case BYTEARRAY_TYPECODE:
  90. case 'b':
  91. case 'B':
  92. align = size = 1;
  93. break;
  94. case 'h':
  95. case 'H':
  96. align = alignof(short);
  97. size = sizeof(short);
  98. break;
  99. case 'i':
  100. case 'I':
  101. align = alignof(int);
  102. size = sizeof(int);
  103. break;
  104. case 'l':
  105. case 'L':
  106. align = alignof(long);
  107. size = sizeof(long);
  108. break;
  109. case 'q':
  110. case 'Q':
  111. align = alignof(long long);
  112. size = sizeof(long long);
  113. break;
  114. case 'P':
  115. case 'O':
  116. case 'S':
  117. align = alignof(void *);
  118. size = sizeof(void *);
  119. break;
  120. case 'f':
  121. align = alignof(float);
  122. size = sizeof(float);
  123. break;
  124. case 'd':
  125. align = alignof(double);
  126. size = sizeof(double);
  127. break;
  128. }
  129. }
  130. }
  131. if (size == 0) {
  132. mp_raise_ValueError(MP_ERROR_TEXT("bad typecode"));
  133. }
  134. if (palign != NULL) {
  135. *palign = align;
  136. }
  137. return size;
  138. }
  139. mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) {
  140. mp_int_t val = 0;
  141. switch (typecode) {
  142. case 'b':
  143. val = ((signed char *)p)[index];
  144. break;
  145. case BYTEARRAY_TYPECODE:
  146. case 'B':
  147. val = ((unsigned char *)p)[index];
  148. break;
  149. case 'h':
  150. val = ((short *)p)[index];
  151. break;
  152. case 'H':
  153. val = ((unsigned short *)p)[index];
  154. break;
  155. case 'i':
  156. return mp_obj_new_int(((int *)p)[index]);
  157. case 'I':
  158. return mp_obj_new_int_from_uint(((unsigned int *)p)[index]);
  159. case 'l':
  160. return mp_obj_new_int(((long *)p)[index]);
  161. case 'L':
  162. return mp_obj_new_int_from_uint(((unsigned long *)p)[index]);
  163. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  164. case 'q':
  165. return mp_obj_new_int_from_ll(((long long *)p)[index]);
  166. case 'Q':
  167. return mp_obj_new_int_from_ull(((unsigned long long *)p)[index]);
  168. #endif
  169. #if MICROPY_PY_BUILTINS_FLOAT
  170. case 'f':
  171. return mp_obj_new_float_from_f(((float *)p)[index]);
  172. case 'd':
  173. return mp_obj_new_float_from_d(((double *)p)[index]);
  174. #endif
  175. // Extension to CPython: array of objects
  176. case 'O':
  177. return ((mp_obj_t *)p)[index];
  178. // Extension to CPython: array of pointers
  179. case 'P':
  180. return mp_obj_new_int((mp_int_t)(uintptr_t)((void **)p)[index]);
  181. }
  182. return MP_OBJ_NEW_SMALL_INT(val);
  183. }
  184. // The long long type is guaranteed to hold at least 64 bits, and size is at
  185. // most 8 (for q and Q), so we will always be able to parse the given data
  186. // and fit it into a long long.
  187. long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const byte *src) {
  188. int delta;
  189. if (!big_endian) {
  190. delta = -1;
  191. src += size - 1;
  192. } else {
  193. delta = 1;
  194. }
  195. long long val = 0;
  196. if (is_signed && *src & 0x80) {
  197. val = -1;
  198. }
  199. for (uint i = 0; i < size; i++) {
  200. val <<= 8;
  201. val |= *src;
  202. src += delta;
  203. }
  204. return val;
  205. }
  206. #define is_signed(typecode) (typecode > 'Z')
  207. mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr) {
  208. byte *p = *ptr;
  209. size_t align;
  210. size_t size = mp_binary_get_size(struct_type, val_type, &align);
  211. if (struct_type == '@') {
  212. // Align p relative to p_base
  213. p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
  214. #if MP_ENDIANNESS_LITTLE
  215. struct_type = '<';
  216. #else
  217. struct_type = '>';
  218. #endif
  219. }
  220. *ptr = p + size;
  221. long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
  222. if (val_type == 'O') {
  223. return (mp_obj_t)(mp_uint_t)val;
  224. } else if (val_type == 'S') {
  225. const char *s_val = (const char *)(uintptr_t)(mp_uint_t)val;
  226. return mp_obj_new_str(s_val, strlen(s_val));
  227. #if MICROPY_PY_BUILTINS_FLOAT
  228. } else if (val_type == 'f') {
  229. union { uint32_t i;
  230. float f;
  231. } fpu = {val};
  232. return mp_obj_new_float_from_f(fpu.f);
  233. } else if (val_type == 'd') {
  234. union { uint64_t i;
  235. double f;
  236. } fpu = {val};
  237. return mp_obj_new_float_from_d(fpu.f);
  238. #endif
  239. } else if (is_signed(val_type)) {
  240. if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
  241. return mp_obj_new_int((mp_int_t)val);
  242. } else {
  243. return mp_obj_new_int_from_ll(val);
  244. }
  245. } else {
  246. if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
  247. return mp_obj_new_int_from_uint((mp_uint_t)val);
  248. } else {
  249. return mp_obj_new_int_from_ull(val);
  250. }
  251. }
  252. }
  253. void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
  254. if (MP_ENDIANNESS_LITTLE && !big_endian) {
  255. memcpy(dest, &val, val_sz);
  256. } else if (MP_ENDIANNESS_BIG && big_endian) {
  257. // only copy the least-significant val_sz bytes
  258. memcpy(dest, (byte *)&val + sizeof(mp_uint_t) - val_sz, val_sz);
  259. } else {
  260. const byte *src;
  261. if (MP_ENDIANNESS_LITTLE) {
  262. src = (const byte *)&val + val_sz;
  263. } else {
  264. src = (const byte *)&val + sizeof(mp_uint_t);
  265. }
  266. while (val_sz--) {
  267. *dest++ = *--src;
  268. }
  269. }
  270. }
  271. void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr) {
  272. byte *p = *ptr;
  273. size_t align;
  274. size_t size = mp_binary_get_size(struct_type, val_type, &align);
  275. if (struct_type == '@') {
  276. // Align p relative to p_base
  277. p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
  278. if (MP_ENDIANNESS_LITTLE) {
  279. struct_type = '<';
  280. } else {
  281. struct_type = '>';
  282. }
  283. }
  284. *ptr = p + size;
  285. mp_uint_t val;
  286. switch (val_type) {
  287. case 'O':
  288. val = (mp_uint_t)val_in;
  289. break;
  290. #if MICROPY_PY_BUILTINS_FLOAT
  291. case 'f': {
  292. union { uint32_t i;
  293. float f;
  294. } fp_sp;
  295. fp_sp.f = mp_obj_get_float_to_f(val_in);
  296. val = fp_sp.i;
  297. break;
  298. }
  299. case 'd': {
  300. union { uint64_t i64;
  301. uint32_t i32[2];
  302. double f;
  303. } fp_dp;
  304. fp_dp.f = mp_obj_get_float_to_d(val_in);
  305. if (BYTES_PER_WORD == 8) {
  306. val = fp_dp.i64;
  307. } else {
  308. int be = struct_type == '>';
  309. mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]);
  310. p += sizeof(uint32_t);
  311. val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be];
  312. }
  313. break;
  314. }
  315. #endif
  316. default:
  317. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  318. if (mp_obj_is_type(val_in, &mp_type_int)) {
  319. mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p);
  320. return;
  321. }
  322. #endif
  323. val = mp_obj_get_int(val_in);
  324. // zero/sign extend if needed
  325. if (BYTES_PER_WORD < 8 && size > sizeof(val)) {
  326. int c = (is_signed(val_type) && (mp_int_t)val < 0) ? 0xff : 0x00;
  327. memset(p, c, size);
  328. if (struct_type == '>') {
  329. p += size - sizeof(val);
  330. }
  331. }
  332. break;
  333. }
  334. mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
  335. }
  336. void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in) {
  337. switch (typecode) {
  338. #if MICROPY_PY_BUILTINS_FLOAT
  339. case 'f':
  340. ((float *)p)[index] = mp_obj_get_float_to_f(val_in);
  341. break;
  342. case 'd':
  343. ((double *)p)[index] = mp_obj_get_float_to_d(val_in);
  344. break;
  345. #endif
  346. // Extension to CPython: array of objects
  347. case 'O':
  348. ((mp_obj_t *)p)[index] = val_in;
  349. break;
  350. default:
  351. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  352. if (mp_obj_is_type(val_in, &mp_type_int)) {
  353. size_t size = mp_binary_get_size('@', typecode, NULL);
  354. mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
  355. size, (uint8_t *)p + index * size);
  356. return;
  357. }
  358. #endif
  359. mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
  360. }
  361. }
  362. void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val) {
  363. switch (typecode) {
  364. case 'b':
  365. ((signed char *)p)[index] = val;
  366. break;
  367. case BYTEARRAY_TYPECODE:
  368. case 'B':
  369. ((unsigned char *)p)[index] = val;
  370. break;
  371. case 'h':
  372. ((short *)p)[index] = val;
  373. break;
  374. case 'H':
  375. ((unsigned short *)p)[index] = val;
  376. break;
  377. case 'i':
  378. ((int *)p)[index] = val;
  379. break;
  380. case 'I':
  381. ((unsigned int *)p)[index] = val;
  382. break;
  383. case 'l':
  384. ((long *)p)[index] = val;
  385. break;
  386. case 'L':
  387. ((unsigned long *)p)[index] = val;
  388. break;
  389. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  390. case 'q':
  391. ((long long *)p)[index] = val;
  392. break;
  393. case 'Q':
  394. ((unsigned long long *)p)[index] = val;
  395. break;
  396. #endif
  397. #if MICROPY_PY_BUILTINS_FLOAT
  398. case 'f':
  399. ((float *)p)[index] = (float)val;
  400. break;
  401. case 'd':
  402. ((double *)p)[index] = (double)val;
  403. break;
  404. #endif
  405. // Extension to CPython: array of pointers
  406. case 'P':
  407. ((void **)p)[index] = (void *)(uintptr_t)val;
  408. break;
  409. }
  410. }