binary.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. unsigned 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 {
  230. uint32_t i;
  231. float f;
  232. } fpu = {val};
  233. return mp_obj_new_float_from_f(fpu.f);
  234. } else if (val_type == 'd') {
  235. union {
  236. uint64_t i;
  237. double f;
  238. } fpu = {val};
  239. return mp_obj_new_float_from_d(fpu.f);
  240. #endif
  241. } else if (is_signed(val_type)) {
  242. if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
  243. return mp_obj_new_int((mp_int_t)val);
  244. } else {
  245. return mp_obj_new_int_from_ll(val);
  246. }
  247. } else {
  248. if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
  249. return mp_obj_new_int_from_uint((mp_uint_t)val);
  250. } else {
  251. return mp_obj_new_int_from_ull(val);
  252. }
  253. }
  254. }
  255. void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
  256. if (MP_ENDIANNESS_LITTLE && !big_endian) {
  257. memcpy(dest, &val, val_sz);
  258. } else if (MP_ENDIANNESS_BIG && big_endian) {
  259. // only copy the least-significant val_sz bytes
  260. memcpy(dest, (byte *)&val + sizeof(mp_uint_t) - val_sz, val_sz);
  261. } else {
  262. const byte *src;
  263. if (MP_ENDIANNESS_LITTLE) {
  264. src = (const byte *)&val + val_sz;
  265. } else {
  266. src = (const byte *)&val + sizeof(mp_uint_t);
  267. }
  268. while (val_sz--) {
  269. *dest++ = *--src;
  270. }
  271. }
  272. }
  273. void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr) {
  274. byte *p = *ptr;
  275. size_t align;
  276. size_t size = mp_binary_get_size(struct_type, val_type, &align);
  277. if (struct_type == '@') {
  278. // Align p relative to p_base
  279. p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
  280. if (MP_ENDIANNESS_LITTLE) {
  281. struct_type = '<';
  282. } else {
  283. struct_type = '>';
  284. }
  285. }
  286. *ptr = p + size;
  287. mp_uint_t val;
  288. switch (val_type) {
  289. case 'O':
  290. val = (mp_uint_t)val_in;
  291. break;
  292. #if MICROPY_PY_BUILTINS_FLOAT
  293. case 'f': {
  294. union {
  295. uint32_t i;
  296. float f;
  297. } fp_sp;
  298. fp_sp.f = mp_obj_get_float_to_f(val_in);
  299. val = fp_sp.i;
  300. break;
  301. }
  302. case 'd': {
  303. union {
  304. uint64_t i64;
  305. uint32_t i32[2];
  306. double f;
  307. } fp_dp;
  308. fp_dp.f = mp_obj_get_float_to_d(val_in);
  309. if (MP_BYTES_PER_OBJ_WORD == 8) {
  310. val = fp_dp.i64;
  311. } else {
  312. int be = struct_type == '>';
  313. mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]);
  314. p += sizeof(uint32_t);
  315. val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be];
  316. }
  317. break;
  318. }
  319. #endif
  320. default:
  321. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  322. if (mp_obj_is_exact_type(val_in, &mp_type_int)) {
  323. mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p);
  324. return;
  325. }
  326. #endif
  327. val = mp_obj_get_int(val_in);
  328. // zero/sign extend if needed
  329. if (MP_BYTES_PER_OBJ_WORD < 8 && size > sizeof(val)) {
  330. int c = (mp_int_t)val < 0 ? 0xff : 0x00;
  331. memset(p, c, size);
  332. if (struct_type == '>') {
  333. p += size - sizeof(val);
  334. }
  335. }
  336. break;
  337. }
  338. mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
  339. }
  340. void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in) {
  341. switch (typecode) {
  342. #if MICROPY_PY_BUILTINS_FLOAT
  343. case 'f':
  344. ((float *)p)[index] = mp_obj_get_float_to_f(val_in);
  345. break;
  346. case 'd':
  347. ((double *)p)[index] = mp_obj_get_float_to_d(val_in);
  348. break;
  349. #endif
  350. // Extension to CPython: array of objects
  351. case 'O':
  352. ((mp_obj_t *)p)[index] = val_in;
  353. break;
  354. default:
  355. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  356. if (mp_obj_is_exact_type(val_in, &mp_type_int)) {
  357. size_t size = mp_binary_get_size('@', typecode, NULL);
  358. mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
  359. size, (uint8_t *)p + index * size);
  360. return;
  361. }
  362. #endif
  363. mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
  364. }
  365. }
  366. void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val) {
  367. switch (typecode) {
  368. case 'b':
  369. ((signed char *)p)[index] = val;
  370. break;
  371. case BYTEARRAY_TYPECODE:
  372. case 'B':
  373. ((unsigned char *)p)[index] = val;
  374. break;
  375. case 'h':
  376. ((short *)p)[index] = val;
  377. break;
  378. case 'H':
  379. ((unsigned short *)p)[index] = val;
  380. break;
  381. case 'i':
  382. ((int *)p)[index] = val;
  383. break;
  384. case 'I':
  385. ((unsigned int *)p)[index] = val;
  386. break;
  387. case 'l':
  388. ((long *)p)[index] = val;
  389. break;
  390. case 'L':
  391. ((unsigned long *)p)[index] = val;
  392. break;
  393. #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
  394. case 'q':
  395. ((long long *)p)[index] = val;
  396. break;
  397. case 'Q':
  398. ((unsigned long long *)p)[index] = val;
  399. break;
  400. #endif
  401. #if MICROPY_PY_BUILTINS_FLOAT
  402. case 'f':
  403. ((float *)p)[index] = (float)val;
  404. break;
  405. case 'd':
  406. ((double *)p)[index] = (double)val;
  407. break;
  408. #endif
  409. // Extension to CPython: array of pointers
  410. case 'P':
  411. ((void **)p)[index] = (void *)(uintptr_t)val;
  412. break;
  413. }
  414. }