bytevectors.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. /* Copyright (C) 2009-2015, 2019 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <alloca.h>
  22. #include <assert.h>
  23. #include <gmp.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/extensions.h"
  26. #include "libguile/bytevectors.h"
  27. #include "libguile/strings.h"
  28. #include "libguile/validate.h"
  29. #include "libguile/arrays.h"
  30. #include "libguile/array-handle.h"
  31. #include "libguile/uniform.h"
  32. #include "libguile/srfi-4.h"
  33. #include <byteswap.h>
  34. #include <striconveh.h>
  35. #include <uniconv.h>
  36. #include <unistr.h>
  37. #ifdef HAVE_LIMITS_H
  38. # include <limits.h>
  39. #endif
  40. #include <string.h>
  41. /* Utilities. */
  42. /* Convenience macros. These are used by the various templates (macros) that
  43. are parameterized by integer signedness. */
  44. #define INT8_T_signed scm_t_int8
  45. #define INT8_T_unsigned scm_t_uint8
  46. #define INT16_T_signed scm_t_int16
  47. #define INT16_T_unsigned scm_t_uint16
  48. #define INT32_T_signed scm_t_int32
  49. #define INT32_T_unsigned scm_t_uint32
  50. #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
  51. #define is_unsigned_int8(_x) ((_x) <= 255UL)
  52. #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
  53. #define is_unsigned_int16(_x) ((_x) <= 65535UL)
  54. #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
  55. #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
  56. #define SIGNEDNESS_signed 1
  57. #define SIGNEDNESS_unsigned 0
  58. #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
  59. #define INT_SWAP(_size) bswap_ ## _size
  60. #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
  61. #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
  62. #define INTEGER_ACCESSOR_PROLOGUE(validate, _len, _sign) \
  63. size_t c_len, c_index; \
  64. _sign char *c_bv; \
  65. \
  66. SCM_VALIDATE_##validate (1, bv); \
  67. c_index = scm_to_size_t (index); \
  68. \
  69. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  70. c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  71. \
  72. if (SCM_UNLIKELY (c_len < c_index \
  73. || (c_len - c_index < (_len) / 8))) \
  74. scm_out_of_range (FUNC_NAME, index);
  75. #define INTEGER_GETTER_PROLOGUE(_len, _sign) \
  76. INTEGER_ACCESSOR_PROLOGUE (BYTEVECTOR, _len, _sign)
  77. #define INTEGER_SETTER_PROLOGUE(_len, _sign) \
  78. INTEGER_ACCESSOR_PROLOGUE (MUTABLE_BYTEVECTOR, _len, _sign)
  79. /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
  80. #define INTEGER_REF(_len, _sign) \
  81. SCM result; \
  82. \
  83. INTEGER_GETTER_PROLOGUE (_len, _sign); \
  84. SCM_VALIDATE_SYMBOL (3, endianness); \
  85. \
  86. { \
  87. INT_TYPE (_len, _sign) c_result; \
  88. \
  89. memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
  90. if (!scm_is_eq (endianness, scm_i_native_endianness)) \
  91. c_result = INT_SWAP (_len) (c_result); \
  92. \
  93. result = SCM_I_MAKINUM (c_result); \
  94. } \
  95. \
  96. return result;
  97. /* Template for fixed-size integer access using the native endianness. */
  98. #define INTEGER_NATIVE_REF(_len, _sign) \
  99. SCM result; \
  100. \
  101. INTEGER_GETTER_PROLOGUE (_len, _sign); \
  102. \
  103. { \
  104. INT_TYPE (_len, _sign) c_result; \
  105. \
  106. memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
  107. result = SCM_I_MAKINUM (c_result); \
  108. } \
  109. \
  110. return result;
  111. /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
  112. #define INTEGER_SET(_len, _sign) \
  113. INTEGER_SETTER_PROLOGUE (_len, _sign); \
  114. SCM_VALIDATE_SYMBOL (3, endianness); \
  115. \
  116. { \
  117. scm_t_signed_bits c_value; \
  118. INT_TYPE (_len, _sign) c_value_short; \
  119. \
  120. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  121. scm_wrong_type_arg (FUNC_NAME, 3, value); \
  122. \
  123. c_value = SCM_I_INUM (value); \
  124. if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
  125. scm_out_of_range (FUNC_NAME, value); \
  126. \
  127. c_value_short = (INT_TYPE (_len, _sign)) c_value; \
  128. if (!scm_is_eq (endianness, scm_i_native_endianness)) \
  129. c_value_short = INT_SWAP (_len) (c_value_short); \
  130. \
  131. memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
  132. } \
  133. \
  134. return SCM_UNSPECIFIED;
  135. /* Template for fixed-size integer modification using the native
  136. endianness. */
  137. #define INTEGER_NATIVE_SET(_len, _sign) \
  138. INTEGER_SETTER_PROLOGUE (_len, _sign); \
  139. \
  140. { \
  141. scm_t_signed_bits c_value; \
  142. INT_TYPE (_len, _sign) c_value_short; \
  143. \
  144. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  145. scm_wrong_type_arg (FUNC_NAME, 3, value); \
  146. \
  147. c_value = SCM_I_INUM (value); \
  148. if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
  149. scm_out_of_range (FUNC_NAME, value); \
  150. \
  151. c_value_short = (INT_TYPE (_len, _sign)) c_value; \
  152. \
  153. memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
  154. } \
  155. \
  156. return SCM_UNSPECIFIED;
  157. /* Bytevector type. */
  158. #define SCM_BYTEVECTOR_HEADER_BYTES \
  159. (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (scm_t_bits))
  160. #define SCM_BYTEVECTOR_SET_FLAG(bv, flag) \
  161. SCM_SET_BYTEVECTOR_FLAGS ((bv), SCM_BYTEVECTOR_FLAGS (bv) | flag)
  162. #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
  163. SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
  164. #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
  165. SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
  166. #define SCM_BYTEVECTOR_SET_PARENT(_bv, _parent) \
  167. SCM_SET_CELL_OBJECT_3 ((_bv), (_parent))
  168. #define SCM_VALIDATE_MUTABLE_BYTEVECTOR(pos, v) \
  169. SCM_MAKE_VALIDATE_MSG (pos, v, MUTABLE_BYTEVECTOR_P, "mutable bytevector")
  170. /* The empty bytevector. */
  171. SCM scm_null_bytevector = SCM_UNSPECIFIED;
  172. static inline SCM
  173. make_bytevector (size_t len, scm_t_array_element_type element_type)
  174. {
  175. SCM ret;
  176. size_t c_len;
  177. if (SCM_UNLIKELY (element_type > SCM_ARRAY_ELEMENT_TYPE_LAST
  178. || scm_i_array_element_type_sizes[element_type] < 8))
  179. /* This would be an internal Guile programming error */
  180. abort ();
  181. /* Make sure that the total allocation size will not overflow size_t,
  182. with ~30 extra bytes to spare to avoid an overflow within the
  183. allocator. */
  184. if (SCM_UNLIKELY (len >= (((size_t) -(SCM_BYTEVECTOR_HEADER_BYTES + 32))
  185. / (scm_i_array_element_type_sizes[element_type]/8))))
  186. scm_num_overflow ("make-bytevector");
  187. if (SCM_UNLIKELY (len == 0 && element_type == SCM_ARRAY_ELEMENT_TYPE_VU8
  188. && SCM_BYTEVECTOR_P (scm_null_bytevector)))
  189. ret = scm_null_bytevector;
  190. else
  191. {
  192. signed char *contents;
  193. c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
  194. contents = scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES + c_len,
  195. SCM_GC_BYTEVECTOR);
  196. ret = SCM_PACK_POINTER (contents);
  197. contents += SCM_BYTEVECTOR_HEADER_BYTES;
  198. SCM_SET_BYTEVECTOR_FLAGS (ret,
  199. element_type | SCM_F_BYTEVECTOR_CONTIGUOUS);
  200. SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
  201. SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
  202. SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
  203. }
  204. return ret;
  205. }
  206. /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
  207. values taken from CONTENTS. Assume that the storage for CONTENTS will be
  208. automatically reclaimed when it becomes unreachable. */
  209. static inline SCM
  210. make_bytevector_from_buffer (size_t len, void *contents,
  211. scm_t_array_element_type element_type)
  212. {
  213. SCM ret;
  214. if (SCM_UNLIKELY (len == 0))
  215. ret = make_bytevector (len, element_type);
  216. else
  217. {
  218. size_t c_len;
  219. ret = SCM_PACK_POINTER (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES,
  220. SCM_GC_BYTEVECTOR));
  221. c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
  222. SCM_SET_BYTEVECTOR_FLAGS (ret, element_type);
  223. SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
  224. SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
  225. SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
  226. }
  227. return ret;
  228. }
  229. /* Return a new bytevector of size LEN octets. */
  230. SCM
  231. scm_c_make_bytevector (size_t len)
  232. {
  233. return make_bytevector (len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  234. }
  235. /* Return a new bytevector of size LEN elements. */
  236. SCM
  237. scm_i_make_typed_bytevector (size_t len, scm_t_array_element_type element_type)
  238. {
  239. return make_bytevector (len, element_type);
  240. }
  241. /* Return a bytevector of size LEN made up of CONTENTS. The area
  242. pointed to by CONTENTS must be protected from GC somehow: either
  243. because it was allocated using `scm_gc_malloc ()', or because it is
  244. part of PARENT. */
  245. SCM
  246. scm_c_take_gc_bytevector (signed char *contents, size_t len, SCM parent)
  247. {
  248. SCM ret;
  249. ret = make_bytevector_from_buffer (len, contents, SCM_ARRAY_ELEMENT_TYPE_VU8);
  250. SCM_BYTEVECTOR_SET_PARENT (ret, parent);
  251. return ret;
  252. }
  253. SCM
  254. scm_c_take_typed_bytevector (signed char *contents, size_t len,
  255. scm_t_array_element_type element_type, SCM parent)
  256. {
  257. SCM ret;
  258. ret = make_bytevector_from_buffer (len, contents, element_type);
  259. SCM_BYTEVECTOR_SET_PARENT (ret, parent);
  260. return ret;
  261. }
  262. /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
  263. size) and return the new bytevector (possibly different from BV). */
  264. SCM
  265. scm_c_shrink_bytevector (SCM bv, size_t c_new_len)
  266. {
  267. SCM new_bv;
  268. size_t c_len;
  269. if (SCM_UNLIKELY (c_new_len % SCM_BYTEVECTOR_TYPE_SIZE (bv)))
  270. /* This would be an internal Guile programming error */
  271. abort ();
  272. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  273. if (SCM_UNLIKELY (c_new_len > c_len))
  274. abort ();
  275. SCM_BYTEVECTOR_SET_LENGTH (bv, c_new_len);
  276. if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv))
  277. {
  278. signed char *c_bv;
  279. c_bv = scm_gc_realloc (SCM2PTR (bv),
  280. c_len + SCM_BYTEVECTOR_HEADER_BYTES,
  281. c_new_len + SCM_BYTEVECTOR_HEADER_BYTES,
  282. SCM_GC_BYTEVECTOR);
  283. new_bv = PTR2SCM (c_bv);
  284. SCM_BYTEVECTOR_SET_CONTENTS (new_bv, c_bv + SCM_BYTEVECTOR_HEADER_BYTES);
  285. }
  286. else
  287. {
  288. signed char *c_bv;
  289. c_bv = scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv),
  290. c_len, c_new_len, SCM_GC_BYTEVECTOR);
  291. SCM_BYTEVECTOR_SET_CONTENTS (bv, c_bv);
  292. new_bv = bv;
  293. }
  294. return new_bv;
  295. }
  296. int
  297. scm_is_bytevector (SCM obj)
  298. {
  299. return SCM_BYTEVECTOR_P (obj);
  300. }
  301. size_t
  302. scm_c_bytevector_length (SCM bv)
  303. #define FUNC_NAME "scm_c_bytevector_length"
  304. {
  305. SCM_VALIDATE_BYTEVECTOR (1, bv);
  306. return SCM_BYTEVECTOR_LENGTH (bv);
  307. }
  308. #undef FUNC_NAME
  309. scm_t_uint8
  310. scm_c_bytevector_ref (SCM bv, size_t index)
  311. #define FUNC_NAME "scm_c_bytevector_ref"
  312. {
  313. size_t c_len;
  314. const scm_t_uint8 *c_bv;
  315. SCM_VALIDATE_BYTEVECTOR (1, bv);
  316. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  317. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  318. if (SCM_UNLIKELY (index >= c_len))
  319. scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
  320. return c_bv[index];
  321. }
  322. #undef FUNC_NAME
  323. void
  324. scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
  325. #define FUNC_NAME "scm_c_bytevector_set_x"
  326. {
  327. size_t c_len;
  328. scm_t_uint8 *c_bv;
  329. SCM_VALIDATE_MUTABLE_BYTEVECTOR (1, bv);
  330. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  331. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  332. if (SCM_UNLIKELY (index >= c_len))
  333. scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
  334. c_bv[index] = value;
  335. }
  336. #undef FUNC_NAME
  337. int
  338. scm_i_print_bytevector (SCM bv, SCM port, scm_print_state *pstate SCM_UNUSED)
  339. {
  340. ssize_t ubnd, inc, i;
  341. scm_t_array_handle h;
  342. scm_array_get_handle (bv, &h);
  343. scm_putc ('#', port);
  344. scm_write (scm_array_handle_element_type (&h), port);
  345. scm_putc ('(', port);
  346. for (i = h.dims[0].lbnd, ubnd = h.dims[0].ubnd, inc = h.dims[0].inc;
  347. i <= ubnd; i += inc)
  348. {
  349. if (i > 0)
  350. scm_putc (' ', port);
  351. scm_write (scm_array_handle_ref (&h, i), port);
  352. }
  353. scm_putc (')', port);
  354. return 1;
  355. }
  356. /* General operations. */
  357. static SCM sym_big;
  358. static SCM sym_little;
  359. SCM scm_endianness_big, scm_endianness_little;
  360. /* Host endianness (a symbol). */
  361. SCM scm_i_native_endianness = SCM_UNSPECIFIED;
  362. /* Byte-swapping. */
  363. #ifndef bswap_24
  364. # define bswap_24(_x) \
  365. ((((_x) & 0xff0000) >> 16) | \
  366. (((_x) & 0x00ff00)) | \
  367. (((_x) & 0x0000ff) << 16))
  368. #endif
  369. SCM_DEFINE (scm_native_endianness, "native-endianness", 0, 0, 0,
  370. (void),
  371. "Return a symbol denoting the machine's native endianness.")
  372. #define FUNC_NAME s_scm_native_endianness
  373. {
  374. return scm_i_native_endianness;
  375. }
  376. #undef FUNC_NAME
  377. SCM_DEFINE (scm_bytevector_p, "bytevector?", 1, 0, 0,
  378. (SCM obj),
  379. "Return true if @var{obj} is a bytevector.")
  380. #define FUNC_NAME s_scm_bytevector_p
  381. {
  382. return scm_from_bool (scm_is_bytevector (obj));
  383. }
  384. #undef FUNC_NAME
  385. SCM_DEFINE (scm_make_bytevector, "make-bytevector", 1, 1, 0,
  386. (SCM len, SCM fill),
  387. "Return a newly allocated bytevector of @var{len} bytes, "
  388. "optionally filled with @var{fill}.")
  389. #define FUNC_NAME s_scm_make_bytevector
  390. {
  391. SCM bv;
  392. size_t c_len;
  393. scm_t_uint8 c_fill = 0;
  394. SCM_VALIDATE_SIZE_COPY (1, len, c_len);
  395. if (!scm_is_eq (fill, SCM_UNDEFINED))
  396. {
  397. int value;
  398. value = scm_to_int (fill);
  399. if (SCM_UNLIKELY ((value < -128) || (value > 255)))
  400. scm_out_of_range (FUNC_NAME, fill);
  401. c_fill = (scm_t_uint8) value;
  402. }
  403. bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  404. if (!scm_is_eq (fill, SCM_UNDEFINED))
  405. {
  406. size_t i;
  407. scm_t_uint8 *contents;
  408. contents = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  409. for (i = 0; i < c_len; i++)
  410. contents[i] = c_fill;
  411. }
  412. else
  413. memset (SCM_BYTEVECTOR_CONTENTS (bv), 0, c_len);
  414. return bv;
  415. }
  416. #undef FUNC_NAME
  417. SCM_DEFINE (scm_bytevector_length, "bytevector-length", 1, 0, 0,
  418. (SCM bv),
  419. "Return the length (in bytes) of @var{bv}.")
  420. #define FUNC_NAME s_scm_bytevector_length
  421. {
  422. return scm_from_size_t (scm_c_bytevector_length (bv));
  423. }
  424. #undef FUNC_NAME
  425. SCM_DEFINE (scm_bytevector_eq_p, "bytevector=?", 2, 0, 0,
  426. (SCM bv1, SCM bv2),
  427. "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
  428. "have the same length and contents.")
  429. #define FUNC_NAME s_scm_bytevector_eq_p
  430. {
  431. SCM result = SCM_BOOL_F;
  432. size_t c_len1, c_len2;
  433. SCM_VALIDATE_BYTEVECTOR (1, bv1);
  434. SCM_VALIDATE_BYTEVECTOR (2, bv2);
  435. c_len1 = SCM_BYTEVECTOR_LENGTH (bv1);
  436. c_len2 = SCM_BYTEVECTOR_LENGTH (bv2);
  437. if (c_len1 == c_len2 && (SCM_BYTEVECTOR_ELEMENT_TYPE (bv1)
  438. == SCM_BYTEVECTOR_ELEMENT_TYPE (bv2)))
  439. {
  440. signed char *c_bv1, *c_bv2;
  441. c_bv1 = SCM_BYTEVECTOR_CONTENTS (bv1);
  442. c_bv2 = SCM_BYTEVECTOR_CONTENTS (bv2);
  443. result = scm_from_bool (!memcmp (c_bv1, c_bv2, c_len1));
  444. }
  445. return result;
  446. }
  447. #undef FUNC_NAME
  448. SCM_DEFINE (scm_bytevector_fill_x, "bytevector-fill!", 2, 0, 0,
  449. (SCM bv, SCM fill),
  450. "Fill bytevector @var{bv} with @var{fill}, a byte.")
  451. #define FUNC_NAME s_scm_bytevector_fill_x
  452. {
  453. size_t c_len, i;
  454. scm_t_uint8 *c_bv, c_fill;
  455. int value;
  456. SCM_VALIDATE_MUTABLE_BYTEVECTOR (1, bv);
  457. value = scm_to_int (fill);
  458. if (SCM_UNLIKELY ((value < -128) || (value > 255)))
  459. scm_out_of_range (FUNC_NAME, fill);
  460. c_fill = (scm_t_uint8) value;
  461. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  462. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  463. for (i = 0; i < c_len; i++)
  464. c_bv[i] = c_fill;
  465. return SCM_UNSPECIFIED;
  466. }
  467. #undef FUNC_NAME
  468. SCM_DEFINE (scm_bytevector_copy_x, "bytevector-copy!", 5, 0, 0,
  469. (SCM source, SCM source_start, SCM target, SCM target_start,
  470. SCM len),
  471. "Copy @var{len} bytes from @var{source} into @var{target}, "
  472. "starting reading from @var{source_start} (a positive index "
  473. "within @var{source}) and start writing at "
  474. "@var{target_start}.")
  475. #define FUNC_NAME s_scm_bytevector_copy_x
  476. {
  477. size_t c_len, c_source_len, c_target_len;
  478. size_t c_source_start, c_target_start;
  479. signed char *c_source, *c_target;
  480. SCM_VALIDATE_BYTEVECTOR (1, source);
  481. SCM_VALIDATE_MUTABLE_BYTEVECTOR (3, target);
  482. c_len = scm_to_size_t (len);
  483. c_source_start = scm_to_size_t (source_start);
  484. c_target_start = scm_to_size_t (target_start);
  485. c_source = SCM_BYTEVECTOR_CONTENTS (source);
  486. c_target = SCM_BYTEVECTOR_CONTENTS (target);
  487. c_source_len = SCM_BYTEVECTOR_LENGTH (source);
  488. c_target_len = SCM_BYTEVECTOR_LENGTH (target);
  489. if (SCM_UNLIKELY (c_source_len < c_source_start
  490. || (c_source_len - c_source_start < c_len)))
  491. scm_out_of_range (FUNC_NAME, source_start);
  492. if (SCM_UNLIKELY (c_target_len < c_target_start
  493. || (c_target_len - c_target_start < c_len)))
  494. scm_out_of_range (FUNC_NAME, target_start);
  495. memmove (c_target + c_target_start,
  496. c_source + c_source_start,
  497. c_len);
  498. return SCM_UNSPECIFIED;
  499. }
  500. #undef FUNC_NAME
  501. SCM_DEFINE (scm_bytevector_copy, "bytevector-copy", 1, 0, 0,
  502. (SCM bv),
  503. "Return a newly allocated copy of @var{bv}.")
  504. #define FUNC_NAME s_scm_bytevector_copy
  505. {
  506. SCM copy;
  507. size_t c_len;
  508. signed char *c_bv, *c_copy;
  509. SCM_VALIDATE_BYTEVECTOR (1, bv);
  510. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  511. c_bv = SCM_BYTEVECTOR_CONTENTS (bv);
  512. copy = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  513. c_copy = SCM_BYTEVECTOR_CONTENTS (copy);
  514. memcpy (c_copy, c_bv, c_len);
  515. return copy;
  516. }
  517. #undef FUNC_NAME
  518. SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector",
  519. 1, 0, 0, (SCM array),
  520. "Return a newly allocated bytevector whose contents\n"
  521. "will be copied from the uniform array @var{array}.")
  522. #define FUNC_NAME s_scm_uniform_array_to_bytevector
  523. {
  524. SCM contents, ret;
  525. size_t len, sz, byte_len;
  526. scm_t_array_handle h;
  527. const void *elts;
  528. contents = scm_array_contents (array, SCM_BOOL_T);
  529. if (scm_is_false (contents))
  530. scm_wrong_type_arg_msg (FUNC_NAME, 0, array, "uniform contiguous array");
  531. scm_array_get_handle (contents, &h);
  532. assert (h.base == 0);
  533. elts = h.elements;
  534. len = h.dims->inc * (h.dims->ubnd - h.dims->lbnd + 1);
  535. sz = scm_array_handle_uniform_element_bit_size (&h);
  536. if (sz >= 8 && ((sz % 8) == 0))
  537. byte_len = len * (sz / 8);
  538. else if (sz < 8)
  539. /* Elements of sub-byte size (bitvectors) are addressed in 32-bit
  540. units. */
  541. byte_len = ((len * sz + 31) / 32) * 4;
  542. else
  543. /* an internal guile error, really */
  544. SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
  545. ret = make_bytevector (byte_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  546. if (byte_len != 0)
  547. /* Empty arrays may have elements == NULL. We must avoid passing
  548. NULL to memcpy, even if the length is zero, to avoid undefined
  549. behavior. */
  550. memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
  551. scm_array_handle_release (&h);
  552. return ret;
  553. }
  554. #undef FUNC_NAME
  555. /* Operations on bytes and octets. */
  556. SCM_DEFINE (scm_bytevector_u8_ref, "bytevector-u8-ref", 2, 0, 0,
  557. (SCM bv, SCM index),
  558. "Return the octet located at @var{index} in @var{bv}.")
  559. #define FUNC_NAME s_scm_bytevector_u8_ref
  560. {
  561. INTEGER_NATIVE_REF (8, unsigned);
  562. }
  563. #undef FUNC_NAME
  564. SCM_DEFINE (scm_bytevector_s8_ref, "bytevector-s8-ref", 2, 0, 0,
  565. (SCM bv, SCM index),
  566. "Return the byte located at @var{index} in @var{bv}.")
  567. #define FUNC_NAME s_scm_bytevector_s8_ref
  568. {
  569. INTEGER_NATIVE_REF (8, signed);
  570. }
  571. #undef FUNC_NAME
  572. SCM_DEFINE (scm_bytevector_u8_set_x, "bytevector-u8-set!", 3, 0, 0,
  573. (SCM bv, SCM index, SCM value),
  574. "Return the octet located at @var{index} in @var{bv}.")
  575. #define FUNC_NAME s_scm_bytevector_u8_set_x
  576. {
  577. INTEGER_NATIVE_SET (8, unsigned);
  578. }
  579. #undef FUNC_NAME
  580. SCM_DEFINE (scm_bytevector_s8_set_x, "bytevector-s8-set!", 3, 0, 0,
  581. (SCM bv, SCM index, SCM value),
  582. "Return the octet located at @var{index} in @var{bv}.")
  583. #define FUNC_NAME s_scm_bytevector_s8_set_x
  584. {
  585. INTEGER_NATIVE_SET (8, signed);
  586. }
  587. #undef FUNC_NAME
  588. SCM_DEFINE (scm_bytevector_to_u8_list, "bytevector->u8-list", 1, 0, 0,
  589. (SCM bv),
  590. "Return a newly allocated list of octets containing the "
  591. "contents of @var{bv}.")
  592. #define FUNC_NAME s_scm_bytevector_to_u8_list
  593. {
  594. SCM lst, pair;
  595. size_t c_len, i;
  596. scm_t_uint8 *c_bv;
  597. SCM_VALIDATE_BYTEVECTOR (1, bv);
  598. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  599. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  600. lst = scm_make_list (scm_from_size_t (c_len), SCM_UNSPECIFIED);
  601. for (i = 0, pair = lst;
  602. i < c_len;
  603. i++, pair = SCM_CDR (pair))
  604. {
  605. SCM_SETCAR (pair, SCM_I_MAKINUM (c_bv[i]));
  606. }
  607. return lst;
  608. }
  609. #undef FUNC_NAME
  610. SCM_DEFINE (scm_u8_list_to_bytevector, "u8-list->bytevector", 1, 0, 0,
  611. (SCM lst),
  612. "Turn @var{lst}, a list of octets, into a bytevector.")
  613. #define FUNC_NAME s_scm_u8_list_to_bytevector
  614. {
  615. SCM bv, item;
  616. size_t c_len, i;
  617. scm_t_uint8 *c_bv;
  618. SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len);
  619. bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  620. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  621. for (i = 0; i < c_len; lst = SCM_CDR (lst), i++)
  622. {
  623. item = SCM_CAR (lst);
  624. if (SCM_LIKELY (SCM_I_INUMP (item)))
  625. {
  626. scm_t_signed_bits c_item;
  627. c_item = SCM_I_INUM (item);
  628. if (SCM_LIKELY ((c_item >= 0) && (c_item < 256)))
  629. c_bv[i] = (scm_t_uint8) c_item;
  630. else
  631. goto type_error;
  632. }
  633. else
  634. goto type_error;
  635. }
  636. return bv;
  637. type_error:
  638. scm_wrong_type_arg (FUNC_NAME, 1, item);
  639. return SCM_BOOL_F;
  640. }
  641. #undef FUNC_NAME
  642. /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
  643. using (2^(SIZE * 8) - VALUE). */
  644. static inline void
  645. twos_complement (mpz_t value, size_t size)
  646. {
  647. unsigned long bit_count;
  648. /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
  649. checking on SIZE performed earlier. */
  650. bit_count = (unsigned long) size << 3UL;
  651. if (SCM_LIKELY (bit_count < sizeof (unsigned long)))
  652. mpz_ui_sub (value, 1UL << bit_count, value);
  653. else
  654. {
  655. mpz_t max;
  656. mpz_init (max);
  657. mpz_ui_pow_ui (max, 2, bit_count);
  658. mpz_sub (value, max, value);
  659. mpz_clear (max);
  660. }
  661. }
  662. static inline SCM
  663. bytevector_large_ref (const char *c_bv, size_t c_size, int signed_p,
  664. SCM endianness)
  665. {
  666. SCM result;
  667. mpz_t c_mpz;
  668. int c_endianness, negative_p = 0;
  669. if (signed_p)
  670. {
  671. if (scm_is_eq (endianness, sym_big))
  672. negative_p = c_bv[0] & 0x80;
  673. else
  674. negative_p = c_bv[c_size - 1] & 0x80;
  675. }
  676. c_endianness = scm_is_eq (endianness, sym_big) ? 1 : -1;
  677. mpz_init (c_mpz);
  678. mpz_import (c_mpz, 1 /* 1 word */, 1 /* word order doesn't matter */,
  679. c_size /* word is C_SIZE-byte long */,
  680. c_endianness,
  681. 0 /* nails */, c_bv);
  682. if (signed_p && negative_p)
  683. {
  684. twos_complement (c_mpz, c_size);
  685. mpz_neg (c_mpz, c_mpz);
  686. }
  687. result = scm_from_mpz (c_mpz);
  688. mpz_clear (c_mpz); /* FIXME: Needed? */
  689. return result;
  690. }
  691. static inline int
  692. bytevector_large_set (char *c_bv, size_t c_size, int signed_p,
  693. SCM value, SCM endianness)
  694. {
  695. mpz_t c_mpz;
  696. int c_endianness, c_sign, err = 0;
  697. c_endianness = scm_is_eq (endianness, sym_big) ? 1 : -1;
  698. mpz_init (c_mpz);
  699. scm_to_mpz (value, c_mpz);
  700. c_sign = mpz_sgn (c_mpz);
  701. if (c_sign < 0)
  702. {
  703. if (SCM_LIKELY (signed_p))
  704. {
  705. mpz_neg (c_mpz, c_mpz);
  706. twos_complement (c_mpz, c_size);
  707. }
  708. else
  709. {
  710. err = -1;
  711. goto finish;
  712. }
  713. }
  714. if (c_sign == 0)
  715. /* Zero. */
  716. memset (c_bv, 0, c_size);
  717. else
  718. {
  719. size_t word_count, value_words;
  720. value_words = ((mpz_sizeinbase (c_mpz, 2) + (8 * c_size) - 1) /
  721. (8 * c_size));
  722. if (SCM_UNLIKELY (value_words > 1))
  723. {
  724. err = -2;
  725. goto finish;
  726. }
  727. mpz_export (c_bv, &word_count, 1 /* word order doesn't matter */,
  728. c_size, c_endianness,
  729. 0 /* nails */, c_mpz);
  730. if (SCM_UNLIKELY (word_count != 1))
  731. /* Shouldn't happen since we already checked with VALUE_SIZE. */
  732. abort ();
  733. }
  734. finish:
  735. mpz_clear (c_mpz);
  736. return err;
  737. }
  738. #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(validate, _sign) \
  739. size_t c_len, c_index, c_size; \
  740. char *c_bv; \
  741. \
  742. SCM_VALIDATE_##validate (1, bv); \
  743. c_index = scm_to_size_t (index); \
  744. c_size = scm_to_size_t (size); \
  745. \
  746. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  747. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  748. \
  749. /* C_SIZE must have its 3 higher bits set to zero so that \
  750. multiplying it by 8 yields a number that fits in a \
  751. size_t. */ \
  752. if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
  753. scm_out_of_range (FUNC_NAME, size); \
  754. if (SCM_UNLIKELY (c_len < c_index \
  755. || (c_len - c_index < c_size))) \
  756. scm_out_of_range (FUNC_NAME, index);
  757. #define GENERIC_INTEGER_GETTER_PROLOGUE(_sign) \
  758. GENERIC_INTEGER_ACCESSOR_PROLOGUE (BYTEVECTOR, _sign)
  759. #define GENERIC_INTEGER_SETTER_PROLOGUE(_sign) \
  760. GENERIC_INTEGER_ACCESSOR_PROLOGUE (MUTABLE_BYTEVECTOR, _sign)
  761. /* Template of an integer reference function. */
  762. #define GENERIC_INTEGER_REF(_sign) \
  763. SCM result; \
  764. \
  765. if (c_size < 3) \
  766. { \
  767. int swap; \
  768. _sign int value; \
  769. \
  770. swap = !scm_is_eq (endianness, scm_i_native_endianness); \
  771. switch (c_size) \
  772. { \
  773. case 1: \
  774. { \
  775. _sign char c_value8; \
  776. memcpy (&c_value8, c_bv, 1); \
  777. value = c_value8; \
  778. } \
  779. break; \
  780. case 2: \
  781. { \
  782. INT_TYPE (16, _sign) c_value16; \
  783. memcpy (&c_value16, c_bv, 2); \
  784. if (swap) \
  785. value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
  786. else \
  787. value = c_value16; \
  788. } \
  789. break; \
  790. default: \
  791. abort (); \
  792. } \
  793. \
  794. result = SCM_I_MAKINUM ((_sign int) value); \
  795. } \
  796. else \
  797. result = bytevector_large_ref ((char *) c_bv, \
  798. c_size, SIGNEDNESS (_sign), \
  799. endianness); \
  800. \
  801. return result;
  802. static inline SCM
  803. bytevector_signed_ref (const char *c_bv, size_t c_size, SCM endianness)
  804. {
  805. GENERIC_INTEGER_REF (signed);
  806. }
  807. static inline SCM
  808. bytevector_unsigned_ref (const char *c_bv, size_t c_size, SCM endianness)
  809. {
  810. GENERIC_INTEGER_REF (unsigned);
  811. }
  812. /* Template of an integer assignment function. */
  813. #define GENERIC_INTEGER_SET(_sign) \
  814. if (c_size < 3) \
  815. { \
  816. scm_t_signed_bits c_value; \
  817. \
  818. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  819. goto range_error; \
  820. \
  821. c_value = SCM_I_INUM (value); \
  822. switch (c_size) \
  823. { \
  824. case 1: \
  825. if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
  826. { \
  827. _sign char c_value8; \
  828. c_value8 = (_sign char) c_value; \
  829. memcpy (c_bv, &c_value8, 1); \
  830. } \
  831. else \
  832. goto range_error; \
  833. break; \
  834. \
  835. case 2: \
  836. if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
  837. { \
  838. int swap; \
  839. INT_TYPE (16, _sign) c_value16; \
  840. \
  841. swap = !scm_is_eq (endianness, scm_i_native_endianness); \
  842. \
  843. if (swap) \
  844. c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
  845. else \
  846. c_value16 = c_value; \
  847. \
  848. memcpy (c_bv, &c_value16, 2); \
  849. } \
  850. else \
  851. goto range_error; \
  852. break; \
  853. \
  854. default: \
  855. abort (); \
  856. } \
  857. } \
  858. else \
  859. { \
  860. int err; \
  861. \
  862. err = bytevector_large_set (c_bv, c_size, \
  863. SIGNEDNESS (_sign), \
  864. value, endianness); \
  865. if (err) \
  866. goto range_error; \
  867. } \
  868. \
  869. return; \
  870. \
  871. range_error: \
  872. scm_out_of_range (FUNC_NAME, value); \
  873. return;
  874. static inline void
  875. bytevector_signed_set (char *c_bv, size_t c_size,
  876. SCM value, SCM endianness,
  877. const char *func_name)
  878. #define FUNC_NAME func_name
  879. {
  880. GENERIC_INTEGER_SET (signed);
  881. }
  882. #undef FUNC_NAME
  883. static inline void
  884. bytevector_unsigned_set (char *c_bv, size_t c_size,
  885. SCM value, SCM endianness,
  886. const char *func_name)
  887. #define FUNC_NAME func_name
  888. {
  889. GENERIC_INTEGER_SET (unsigned);
  890. }
  891. #undef FUNC_NAME
  892. #undef GENERIC_INTEGER_SET
  893. #undef GENERIC_INTEGER_REF
  894. SCM_DEFINE (scm_bytevector_uint_ref, "bytevector-uint-ref", 4, 0, 0,
  895. (SCM bv, SCM index, SCM endianness, SCM size),
  896. "Return the @var{size}-octet long unsigned integer at index "
  897. "@var{index} in @var{bv}.")
  898. #define FUNC_NAME s_scm_bytevector_uint_ref
  899. {
  900. GENERIC_INTEGER_GETTER_PROLOGUE (unsigned);
  901. return (bytevector_unsigned_ref (&c_bv[c_index], c_size, endianness));
  902. }
  903. #undef FUNC_NAME
  904. SCM_DEFINE (scm_bytevector_sint_ref, "bytevector-sint-ref", 4, 0, 0,
  905. (SCM bv, SCM index, SCM endianness, SCM size),
  906. "Return the @var{size}-octet long unsigned integer at index "
  907. "@var{index} in @var{bv}.")
  908. #define FUNC_NAME s_scm_bytevector_sint_ref
  909. {
  910. GENERIC_INTEGER_GETTER_PROLOGUE (signed);
  911. return (bytevector_signed_ref (&c_bv[c_index], c_size, endianness));
  912. }
  913. #undef FUNC_NAME
  914. SCM_DEFINE (scm_bytevector_uint_set_x, "bytevector-uint-set!", 5, 0, 0,
  915. (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
  916. "Set the @var{size}-octet long unsigned integer at @var{index} "
  917. "to @var{value}.")
  918. #define FUNC_NAME s_scm_bytevector_uint_set_x
  919. {
  920. GENERIC_INTEGER_SETTER_PROLOGUE (unsigned);
  921. bytevector_unsigned_set (&c_bv[c_index], c_size, value, endianness,
  922. FUNC_NAME);
  923. return SCM_UNSPECIFIED;
  924. }
  925. #undef FUNC_NAME
  926. SCM_DEFINE (scm_bytevector_sint_set_x, "bytevector-sint-set!", 5, 0, 0,
  927. (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
  928. "Set the @var{size}-octet long signed integer at @var{index} "
  929. "to @var{value}.")
  930. #define FUNC_NAME s_scm_bytevector_sint_set_x
  931. {
  932. GENERIC_INTEGER_SETTER_PROLOGUE (signed);
  933. bytevector_signed_set (&c_bv[c_index], c_size, value, endianness,
  934. FUNC_NAME);
  935. return SCM_UNSPECIFIED;
  936. }
  937. #undef FUNC_NAME
  938. /* Operations on integers of arbitrary size. */
  939. #define INTEGERS_TO_LIST(_sign) \
  940. SCM lst, pair; \
  941. size_t i, c_len, c_size; \
  942. \
  943. SCM_VALIDATE_BYTEVECTOR (1, bv); \
  944. SCM_VALIDATE_SYMBOL (2, endianness); \
  945. c_size = scm_to_unsigned_integer (size, 1, (size_t) -1); \
  946. \
  947. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  948. if (SCM_UNLIKELY (c_len % c_size != 0)) \
  949. scm_wrong_type_arg_msg \
  950. (FUNC_NAME, 0, size, \
  951. "an exact positive integer that divides the bytevector length"); \
  952. else if (SCM_UNLIKELY (c_len == 0)) \
  953. lst = SCM_EOL; \
  954. else \
  955. { \
  956. const char *c_bv; \
  957. \
  958. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  959. \
  960. lst = scm_make_list (scm_from_size_t (c_len / c_size), \
  961. SCM_UNSPECIFIED); \
  962. for (i = 0, pair = lst; \
  963. i <= c_len - c_size; \
  964. i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
  965. { \
  966. SCM_SETCAR (pair, \
  967. bytevector_ ## _sign ## _ref (c_bv, c_size, \
  968. endianness)); \
  969. } \
  970. } \
  971. \
  972. return lst;
  973. SCM_DEFINE (scm_bytevector_to_sint_list, "bytevector->sint-list",
  974. 3, 0, 0,
  975. (SCM bv, SCM endianness, SCM size),
  976. "Return a list of signed integers of @var{size} octets "
  977. "representing the contents of @var{bv}.")
  978. #define FUNC_NAME s_scm_bytevector_to_sint_list
  979. {
  980. INTEGERS_TO_LIST (signed);
  981. }
  982. #undef FUNC_NAME
  983. SCM_DEFINE (scm_bytevector_to_uint_list, "bytevector->uint-list",
  984. 3, 0, 0,
  985. (SCM bv, SCM endianness, SCM size),
  986. "Return a list of unsigned integers of @var{size} octets "
  987. "representing the contents of @var{bv}.")
  988. #define FUNC_NAME s_scm_bytevector_to_uint_list
  989. {
  990. INTEGERS_TO_LIST (unsigned);
  991. }
  992. #undef FUNC_NAME
  993. #undef INTEGER_TO_LIST
  994. #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
  995. SCM bv; \
  996. size_t c_len; \
  997. size_t c_size; \
  998. char *c_bv, *c_bv_ptr; \
  999. \
  1000. SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
  1001. SCM_VALIDATE_SYMBOL (2, endianness); \
  1002. c_size = scm_to_size_t (size); \
  1003. \
  1004. if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
  1005. scm_out_of_range (FUNC_NAME, size); \
  1006. \
  1007. bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
  1008. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  1009. \
  1010. for (c_bv_ptr = c_bv; \
  1011. !scm_is_null (lst); \
  1012. lst = SCM_CDR (lst), c_bv_ptr += c_size) \
  1013. { \
  1014. bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
  1015. SCM_CAR (lst), endianness, \
  1016. FUNC_NAME); \
  1017. } \
  1018. \
  1019. return bv;
  1020. SCM_DEFINE (scm_uint_list_to_bytevector, "uint-list->bytevector",
  1021. 3, 0, 0,
  1022. (SCM lst, SCM endianness, SCM size),
  1023. "Return a bytevector containing the unsigned integers "
  1024. "listed in @var{lst} and encoded on @var{size} octets "
  1025. "according to @var{endianness}.")
  1026. #define FUNC_NAME s_scm_uint_list_to_bytevector
  1027. {
  1028. INTEGER_LIST_TO_BYTEVECTOR (unsigned);
  1029. }
  1030. #undef FUNC_NAME
  1031. SCM_DEFINE (scm_sint_list_to_bytevector, "sint-list->bytevector",
  1032. 3, 0, 0,
  1033. (SCM lst, SCM endianness, SCM size),
  1034. "Return a bytevector containing the signed integers "
  1035. "listed in @var{lst} and encoded on @var{size} octets "
  1036. "according to @var{endianness}.")
  1037. #define FUNC_NAME s_scm_sint_list_to_bytevector
  1038. {
  1039. INTEGER_LIST_TO_BYTEVECTOR (signed);
  1040. }
  1041. #undef FUNC_NAME
  1042. #undef INTEGER_LIST_TO_BYTEVECTOR
  1043. /* Operations on 16-bit integers. */
  1044. SCM_DEFINE (scm_bytevector_u16_ref, "bytevector-u16-ref",
  1045. 3, 0, 0,
  1046. (SCM bv, SCM index, SCM endianness),
  1047. "Return the unsigned 16-bit integer from @var{bv} at "
  1048. "@var{index}.")
  1049. #define FUNC_NAME s_scm_bytevector_u16_ref
  1050. {
  1051. INTEGER_REF (16, unsigned);
  1052. }
  1053. #undef FUNC_NAME
  1054. SCM_DEFINE (scm_bytevector_s16_ref, "bytevector-s16-ref",
  1055. 3, 0, 0,
  1056. (SCM bv, SCM index, SCM endianness),
  1057. "Return the signed 16-bit integer from @var{bv} at "
  1058. "@var{index}.")
  1059. #define FUNC_NAME s_scm_bytevector_s16_ref
  1060. {
  1061. INTEGER_REF (16, signed);
  1062. }
  1063. #undef FUNC_NAME
  1064. SCM_DEFINE (scm_bytevector_u16_native_ref, "bytevector-u16-native-ref",
  1065. 2, 0, 0,
  1066. (SCM bv, SCM index),
  1067. "Return the unsigned 16-bit integer from @var{bv} at "
  1068. "@var{index} using the native endianness.")
  1069. #define FUNC_NAME s_scm_bytevector_u16_native_ref
  1070. {
  1071. INTEGER_NATIVE_REF (16, unsigned);
  1072. }
  1073. #undef FUNC_NAME
  1074. SCM_DEFINE (scm_bytevector_s16_native_ref, "bytevector-s16-native-ref",
  1075. 2, 0, 0,
  1076. (SCM bv, SCM index),
  1077. "Return the unsigned 16-bit integer from @var{bv} at "
  1078. "@var{index} using the native endianness.")
  1079. #define FUNC_NAME s_scm_bytevector_s16_native_ref
  1080. {
  1081. INTEGER_NATIVE_REF (16, signed);
  1082. }
  1083. #undef FUNC_NAME
  1084. SCM_DEFINE (scm_bytevector_u16_set_x, "bytevector-u16-set!",
  1085. 4, 0, 0,
  1086. (SCM bv, SCM index, SCM value, SCM endianness),
  1087. "Store @var{value} in @var{bv} at @var{index} according to "
  1088. "@var{endianness}.")
  1089. #define FUNC_NAME s_scm_bytevector_u16_set_x
  1090. {
  1091. INTEGER_SET (16, unsigned);
  1092. }
  1093. #undef FUNC_NAME
  1094. SCM_DEFINE (scm_bytevector_s16_set_x, "bytevector-s16-set!",
  1095. 4, 0, 0,
  1096. (SCM bv, SCM index, SCM value, SCM endianness),
  1097. "Store @var{value} in @var{bv} at @var{index} according to "
  1098. "@var{endianness}.")
  1099. #define FUNC_NAME s_scm_bytevector_s16_set_x
  1100. {
  1101. INTEGER_SET (16, signed);
  1102. }
  1103. #undef FUNC_NAME
  1104. SCM_DEFINE (scm_bytevector_u16_native_set_x, "bytevector-u16-native-set!",
  1105. 3, 0, 0,
  1106. (SCM bv, SCM index, SCM value),
  1107. "Store the unsigned integer @var{value} at index @var{index} "
  1108. "of @var{bv} using the native endianness.")
  1109. #define FUNC_NAME s_scm_bytevector_u16_native_set_x
  1110. {
  1111. INTEGER_NATIVE_SET (16, unsigned);
  1112. }
  1113. #undef FUNC_NAME
  1114. SCM_DEFINE (scm_bytevector_s16_native_set_x, "bytevector-s16-native-set!",
  1115. 3, 0, 0,
  1116. (SCM bv, SCM index, SCM value),
  1117. "Store the signed integer @var{value} at index @var{index} "
  1118. "of @var{bv} using the native endianness.")
  1119. #define FUNC_NAME s_scm_bytevector_s16_native_set_x
  1120. {
  1121. INTEGER_NATIVE_SET (16, signed);
  1122. }
  1123. #undef FUNC_NAME
  1124. /* Operations on 32-bit integers. */
  1125. /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
  1126. arbitrary 32-bit integers. Thus we fall back to using the
  1127. `large_{ref,set}' variants on 32-bit machines. */
  1128. #define LARGE_INTEGER_REF(_len, _sign) \
  1129. INTEGER_GETTER_PROLOGUE(_len, _sign); \
  1130. SCM_VALIDATE_SYMBOL (3, endianness); \
  1131. \
  1132. return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
  1133. SIGNEDNESS (_sign), endianness));
  1134. #define LARGE_INTEGER_SET(_len, _sign) \
  1135. int err; \
  1136. INTEGER_SETTER_PROLOGUE (_len, _sign); \
  1137. SCM_VALIDATE_SYMBOL (4, endianness); \
  1138. \
  1139. err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
  1140. SIGNEDNESS (_sign), value, endianness); \
  1141. if (SCM_UNLIKELY (err)) \
  1142. scm_out_of_range (FUNC_NAME, value); \
  1143. \
  1144. return SCM_UNSPECIFIED;
  1145. #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
  1146. INTEGER_GETTER_PROLOGUE(_len, _sign); \
  1147. return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
  1148. SIGNEDNESS (_sign), scm_i_native_endianness));
  1149. #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
  1150. int err; \
  1151. INTEGER_SETTER_PROLOGUE (_len, _sign); \
  1152. \
  1153. err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
  1154. SIGNEDNESS (_sign), value, \
  1155. scm_i_native_endianness); \
  1156. if (SCM_UNLIKELY (err)) \
  1157. scm_out_of_range (FUNC_NAME, value); \
  1158. \
  1159. return SCM_UNSPECIFIED;
  1160. SCM_DEFINE (scm_bytevector_u32_ref, "bytevector-u32-ref",
  1161. 3, 0, 0,
  1162. (SCM bv, SCM index, SCM endianness),
  1163. "Return the unsigned 32-bit integer from @var{bv} at "
  1164. "@var{index}.")
  1165. #define FUNC_NAME s_scm_bytevector_u32_ref
  1166. {
  1167. #if SIZEOF_VOID_P > 4
  1168. INTEGER_REF (32, unsigned);
  1169. #else
  1170. LARGE_INTEGER_REF (32, unsigned);
  1171. #endif
  1172. }
  1173. #undef FUNC_NAME
  1174. SCM_DEFINE (scm_bytevector_s32_ref, "bytevector-s32-ref",
  1175. 3, 0, 0,
  1176. (SCM bv, SCM index, SCM endianness),
  1177. "Return the signed 32-bit integer from @var{bv} at "
  1178. "@var{index}.")
  1179. #define FUNC_NAME s_scm_bytevector_s32_ref
  1180. {
  1181. #if SIZEOF_VOID_P > 4
  1182. INTEGER_REF (32, signed);
  1183. #else
  1184. LARGE_INTEGER_REF (32, signed);
  1185. #endif
  1186. }
  1187. #undef FUNC_NAME
  1188. SCM_DEFINE (scm_bytevector_u32_native_ref, "bytevector-u32-native-ref",
  1189. 2, 0, 0,
  1190. (SCM bv, SCM index),
  1191. "Return the unsigned 32-bit integer from @var{bv} at "
  1192. "@var{index} using the native endianness.")
  1193. #define FUNC_NAME s_scm_bytevector_u32_native_ref
  1194. {
  1195. #if SIZEOF_VOID_P > 4
  1196. INTEGER_NATIVE_REF (32, unsigned);
  1197. #else
  1198. LARGE_INTEGER_NATIVE_REF (32, unsigned);
  1199. #endif
  1200. }
  1201. #undef FUNC_NAME
  1202. SCM_DEFINE (scm_bytevector_s32_native_ref, "bytevector-s32-native-ref",
  1203. 2, 0, 0,
  1204. (SCM bv, SCM index),
  1205. "Return the unsigned 32-bit integer from @var{bv} at "
  1206. "@var{index} using the native endianness.")
  1207. #define FUNC_NAME s_scm_bytevector_s32_native_ref
  1208. {
  1209. #if SIZEOF_VOID_P > 4
  1210. INTEGER_NATIVE_REF (32, signed);
  1211. #else
  1212. LARGE_INTEGER_NATIVE_REF (32, signed);
  1213. #endif
  1214. }
  1215. #undef FUNC_NAME
  1216. SCM_DEFINE (scm_bytevector_u32_set_x, "bytevector-u32-set!",
  1217. 4, 0, 0,
  1218. (SCM bv, SCM index, SCM value, SCM endianness),
  1219. "Store @var{value} in @var{bv} at @var{index} according to "
  1220. "@var{endianness}.")
  1221. #define FUNC_NAME s_scm_bytevector_u32_set_x
  1222. {
  1223. #if SIZEOF_VOID_P > 4
  1224. INTEGER_SET (32, unsigned);
  1225. #else
  1226. LARGE_INTEGER_SET (32, unsigned);
  1227. #endif
  1228. }
  1229. #undef FUNC_NAME
  1230. SCM_DEFINE (scm_bytevector_s32_set_x, "bytevector-s32-set!",
  1231. 4, 0, 0,
  1232. (SCM bv, SCM index, SCM value, SCM endianness),
  1233. "Store @var{value} in @var{bv} at @var{index} according to "
  1234. "@var{endianness}.")
  1235. #define FUNC_NAME s_scm_bytevector_s32_set_x
  1236. {
  1237. #if SIZEOF_VOID_P > 4
  1238. INTEGER_SET (32, signed);
  1239. #else
  1240. LARGE_INTEGER_SET (32, signed);
  1241. #endif
  1242. }
  1243. #undef FUNC_NAME
  1244. SCM_DEFINE (scm_bytevector_u32_native_set_x, "bytevector-u32-native-set!",
  1245. 3, 0, 0,
  1246. (SCM bv, SCM index, SCM value),
  1247. "Store the unsigned integer @var{value} at index @var{index} "
  1248. "of @var{bv} using the native endianness.")
  1249. #define FUNC_NAME s_scm_bytevector_u32_native_set_x
  1250. {
  1251. #if SIZEOF_VOID_P > 4
  1252. INTEGER_NATIVE_SET (32, unsigned);
  1253. #else
  1254. LARGE_INTEGER_NATIVE_SET (32, unsigned);
  1255. #endif
  1256. }
  1257. #undef FUNC_NAME
  1258. SCM_DEFINE (scm_bytevector_s32_native_set_x, "bytevector-s32-native-set!",
  1259. 3, 0, 0,
  1260. (SCM bv, SCM index, SCM value),
  1261. "Store the signed integer @var{value} at index @var{index} "
  1262. "of @var{bv} using the native endianness.")
  1263. #define FUNC_NAME s_scm_bytevector_s32_native_set_x
  1264. {
  1265. #if SIZEOF_VOID_P > 4
  1266. INTEGER_NATIVE_SET (32, signed);
  1267. #else
  1268. LARGE_INTEGER_NATIVE_SET (32, signed);
  1269. #endif
  1270. }
  1271. #undef FUNC_NAME
  1272. /* Operations on 64-bit integers. */
  1273. /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
  1274. SCM_DEFINE (scm_bytevector_u64_ref, "bytevector-u64-ref",
  1275. 3, 0, 0,
  1276. (SCM bv, SCM index, SCM endianness),
  1277. "Return the unsigned 64-bit integer from @var{bv} at "
  1278. "@var{index}.")
  1279. #define FUNC_NAME s_scm_bytevector_u64_ref
  1280. {
  1281. LARGE_INTEGER_REF (64, unsigned);
  1282. }
  1283. #undef FUNC_NAME
  1284. SCM_DEFINE (scm_bytevector_s64_ref, "bytevector-s64-ref",
  1285. 3, 0, 0,
  1286. (SCM bv, SCM index, SCM endianness),
  1287. "Return the signed 64-bit integer from @var{bv} at "
  1288. "@var{index}.")
  1289. #define FUNC_NAME s_scm_bytevector_s64_ref
  1290. {
  1291. LARGE_INTEGER_REF (64, signed);
  1292. }
  1293. #undef FUNC_NAME
  1294. SCM_DEFINE (scm_bytevector_u64_native_ref, "bytevector-u64-native-ref",
  1295. 2, 0, 0,
  1296. (SCM bv, SCM index),
  1297. "Return the unsigned 64-bit integer from @var{bv} at "
  1298. "@var{index} using the native endianness.")
  1299. #define FUNC_NAME s_scm_bytevector_u64_native_ref
  1300. {
  1301. LARGE_INTEGER_NATIVE_REF (64, unsigned);
  1302. }
  1303. #undef FUNC_NAME
  1304. SCM_DEFINE (scm_bytevector_s64_native_ref, "bytevector-s64-native-ref",
  1305. 2, 0, 0,
  1306. (SCM bv, SCM index),
  1307. "Return the unsigned 64-bit integer from @var{bv} at "
  1308. "@var{index} using the native endianness.")
  1309. #define FUNC_NAME s_scm_bytevector_s64_native_ref
  1310. {
  1311. LARGE_INTEGER_NATIVE_REF (64, signed);
  1312. }
  1313. #undef FUNC_NAME
  1314. SCM_DEFINE (scm_bytevector_u64_set_x, "bytevector-u64-set!",
  1315. 4, 0, 0,
  1316. (SCM bv, SCM index, SCM value, SCM endianness),
  1317. "Store @var{value} in @var{bv} at @var{index} according to "
  1318. "@var{endianness}.")
  1319. #define FUNC_NAME s_scm_bytevector_u64_set_x
  1320. {
  1321. LARGE_INTEGER_SET (64, unsigned);
  1322. }
  1323. #undef FUNC_NAME
  1324. SCM_DEFINE (scm_bytevector_s64_set_x, "bytevector-s64-set!",
  1325. 4, 0, 0,
  1326. (SCM bv, SCM index, SCM value, SCM endianness),
  1327. "Store @var{value} in @var{bv} at @var{index} according to "
  1328. "@var{endianness}.")
  1329. #define FUNC_NAME s_scm_bytevector_s64_set_x
  1330. {
  1331. LARGE_INTEGER_SET (64, signed);
  1332. }
  1333. #undef FUNC_NAME
  1334. SCM_DEFINE (scm_bytevector_u64_native_set_x, "bytevector-u64-native-set!",
  1335. 3, 0, 0,
  1336. (SCM bv, SCM index, SCM value),
  1337. "Store the unsigned integer @var{value} at index @var{index} "
  1338. "of @var{bv} using the native endianness.")
  1339. #define FUNC_NAME s_scm_bytevector_u64_native_set_x
  1340. {
  1341. LARGE_INTEGER_NATIVE_SET (64, unsigned);
  1342. }
  1343. #undef FUNC_NAME
  1344. SCM_DEFINE (scm_bytevector_s64_native_set_x, "bytevector-s64-native-set!",
  1345. 3, 0, 0,
  1346. (SCM bv, SCM index, SCM value),
  1347. "Store the signed integer @var{value} at index @var{index} "
  1348. "of @var{bv} using the native endianness.")
  1349. #define FUNC_NAME s_scm_bytevector_s64_native_set_x
  1350. {
  1351. LARGE_INTEGER_NATIVE_SET (64, signed);
  1352. }
  1353. #undef FUNC_NAME
  1354. /* Operations on IEEE-754 numbers. */
  1355. /* There are two possible word endians, visible in glibc's <ieee754.h>.
  1356. However, in R6RS, when the endianness is `little', little endian is
  1357. assumed for both the byte order and the word order. This is clear from
  1358. Section 2.1 of R6RS-lib (in response to
  1359. http://www.r6rs.org/formal-comments/comment-187.txt). */
  1360. union scm_ieee754_float
  1361. {
  1362. float f;
  1363. scm_t_uint32 i;
  1364. };
  1365. union scm_ieee754_double
  1366. {
  1367. double d;
  1368. scm_t_uint64 i;
  1369. };
  1370. /* Convert to/from a floating-point number with different endianness. This
  1371. method is probably not the most efficient but it should be portable. */
  1372. static inline void
  1373. float_to_foreign_endianness (union scm_ieee754_float *target,
  1374. float source)
  1375. {
  1376. union scm_ieee754_float input;
  1377. input.f = source;
  1378. target->i = bswap_32 (input.i);
  1379. }
  1380. static inline float
  1381. float_from_foreign_endianness (const union scm_ieee754_float *source)
  1382. {
  1383. union scm_ieee754_float result;
  1384. result.i = bswap_32 (source->i);
  1385. return (result.f);
  1386. }
  1387. static inline void
  1388. double_to_foreign_endianness (union scm_ieee754_double *target,
  1389. double source)
  1390. {
  1391. union scm_ieee754_double input;
  1392. input.d = source;
  1393. target->i = bswap_64 (input.i);
  1394. }
  1395. static inline double
  1396. double_from_foreign_endianness (const union scm_ieee754_double *source)
  1397. {
  1398. union scm_ieee754_double result;
  1399. result.i = bswap_64 (source->i);
  1400. return (result.d);
  1401. }
  1402. /* Template macros to abstract over doubles and floats.
  1403. XXX: Guile can only convert to/from doubles. */
  1404. #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
  1405. #define IEEE754_TO_SCM(_c_type) scm_from_double
  1406. #define IEEE754_FROM_SCM(_c_type) scm_to_double
  1407. #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
  1408. _c_type ## _from_foreign_endianness
  1409. #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
  1410. _c_type ## _to_foreign_endianness
  1411. /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
  1412. #define VALIDATE_REAL(pos, v) \
  1413. do { \
  1414. SCM_ASSERT_TYPE (scm_is_real (v), v, pos, FUNC_NAME, "real"); \
  1415. } while (0)
  1416. /* Templace getters and setters. */
  1417. #define IEEE754_GETTER_PROLOGUE(_type) \
  1418. INTEGER_GETTER_PROLOGUE (sizeof (_type) << 3UL, signed);
  1419. #define IEEE754_SETTER_PROLOGUE(_type) \
  1420. INTEGER_SETTER_PROLOGUE (sizeof (_type) << 3UL, signed);
  1421. #define IEEE754_REF(_type) \
  1422. _type c_result; \
  1423. \
  1424. IEEE754_GETTER_PROLOGUE (_type); \
  1425. SCM_VALIDATE_SYMBOL (3, endianness); \
  1426. \
  1427. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  1428. memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
  1429. else \
  1430. { \
  1431. IEEE754_UNION (_type) c_raw; \
  1432. \
  1433. memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
  1434. c_result = \
  1435. IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
  1436. } \
  1437. \
  1438. return (IEEE754_TO_SCM (_type) (c_result));
  1439. #define IEEE754_NATIVE_REF(_type) \
  1440. _type c_result; \
  1441. \
  1442. IEEE754_GETTER_PROLOGUE (_type); \
  1443. \
  1444. memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
  1445. return (IEEE754_TO_SCM (_type) (c_result));
  1446. #define IEEE754_SET(_type) \
  1447. _type c_value; \
  1448. \
  1449. IEEE754_SETTER_PROLOGUE (_type); \
  1450. VALIDATE_REAL (3, value); \
  1451. SCM_VALIDATE_SYMBOL (4, endianness); \
  1452. c_value = IEEE754_FROM_SCM (_type) (value); \
  1453. \
  1454. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  1455. memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
  1456. else \
  1457. { \
  1458. IEEE754_UNION (_type) c_raw; \
  1459. \
  1460. IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
  1461. memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
  1462. } \
  1463. \
  1464. return SCM_UNSPECIFIED;
  1465. #define IEEE754_NATIVE_SET(_type) \
  1466. _type c_value; \
  1467. \
  1468. IEEE754_SETTER_PROLOGUE (_type); \
  1469. VALIDATE_REAL (3, value); \
  1470. c_value = IEEE754_FROM_SCM (_type) (value); \
  1471. \
  1472. memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
  1473. return SCM_UNSPECIFIED;
  1474. /* Single precision. */
  1475. SCM_DEFINE (scm_bytevector_ieee_single_ref,
  1476. "bytevector-ieee-single-ref",
  1477. 3, 0, 0,
  1478. (SCM bv, SCM index, SCM endianness),
  1479. "Return the IEEE-754 single from @var{bv} at "
  1480. "@var{index}.")
  1481. #define FUNC_NAME s_scm_bytevector_ieee_single_ref
  1482. {
  1483. IEEE754_REF (float);
  1484. }
  1485. #undef FUNC_NAME
  1486. SCM_DEFINE (scm_bytevector_ieee_single_native_ref,
  1487. "bytevector-ieee-single-native-ref",
  1488. 2, 0, 0,
  1489. (SCM bv, SCM index),
  1490. "Return the IEEE-754 single from @var{bv} at "
  1491. "@var{index} using the native endianness.")
  1492. #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
  1493. {
  1494. IEEE754_NATIVE_REF (float);
  1495. }
  1496. #undef FUNC_NAME
  1497. SCM_DEFINE (scm_bytevector_ieee_single_set_x,
  1498. "bytevector-ieee-single-set!",
  1499. 4, 0, 0,
  1500. (SCM bv, SCM index, SCM value, SCM endianness),
  1501. "Store real @var{value} in @var{bv} at @var{index} according to "
  1502. "@var{endianness}.")
  1503. #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
  1504. {
  1505. IEEE754_SET (float);
  1506. }
  1507. #undef FUNC_NAME
  1508. SCM_DEFINE (scm_bytevector_ieee_single_native_set_x,
  1509. "bytevector-ieee-single-native-set!",
  1510. 3, 0, 0,
  1511. (SCM bv, SCM index, SCM value),
  1512. "Store the real @var{value} at index @var{index} "
  1513. "of @var{bv} using the native endianness.")
  1514. #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
  1515. {
  1516. IEEE754_NATIVE_SET (float);
  1517. }
  1518. #undef FUNC_NAME
  1519. /* Double precision. */
  1520. SCM_DEFINE (scm_bytevector_ieee_double_ref,
  1521. "bytevector-ieee-double-ref",
  1522. 3, 0, 0,
  1523. (SCM bv, SCM index, SCM endianness),
  1524. "Return the IEEE-754 double from @var{bv} at "
  1525. "@var{index}.")
  1526. #define FUNC_NAME s_scm_bytevector_ieee_double_ref
  1527. {
  1528. IEEE754_REF (double);
  1529. }
  1530. #undef FUNC_NAME
  1531. SCM_DEFINE (scm_bytevector_ieee_double_native_ref,
  1532. "bytevector-ieee-double-native-ref",
  1533. 2, 0, 0,
  1534. (SCM bv, SCM index),
  1535. "Return the IEEE-754 double from @var{bv} at "
  1536. "@var{index} using the native endianness.")
  1537. #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
  1538. {
  1539. IEEE754_NATIVE_REF (double);
  1540. }
  1541. #undef FUNC_NAME
  1542. SCM_DEFINE (scm_bytevector_ieee_double_set_x,
  1543. "bytevector-ieee-double-set!",
  1544. 4, 0, 0,
  1545. (SCM bv, SCM index, SCM value, SCM endianness),
  1546. "Store real @var{value} in @var{bv} at @var{index} according to "
  1547. "@var{endianness}.")
  1548. #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
  1549. {
  1550. IEEE754_SET (double);
  1551. }
  1552. #undef FUNC_NAME
  1553. SCM_DEFINE (scm_bytevector_ieee_double_native_set_x,
  1554. "bytevector-ieee-double-native-set!",
  1555. 3, 0, 0,
  1556. (SCM bv, SCM index, SCM value),
  1557. "Store the real @var{value} at index @var{index} "
  1558. "of @var{bv} using the native endianness.")
  1559. #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
  1560. {
  1561. IEEE754_NATIVE_SET (double);
  1562. }
  1563. #undef FUNC_NAME
  1564. #undef IEEE754_UNION
  1565. #undef IEEE754_TO_SCM
  1566. #undef IEEE754_FROM_SCM
  1567. #undef IEEE754_FROM_FOREIGN_ENDIANNESS
  1568. #undef IEEE754_TO_FOREIGN_ENDIANNESS
  1569. #undef IEEE754_REF
  1570. #undef IEEE754_NATIVE_REF
  1571. #undef IEEE754_SET
  1572. #undef IEEE754_NATIVE_SET
  1573. /* Operations on strings. */
  1574. /* Produce a function that returns the length of a UTF-encoded string. */
  1575. #define UTF_STRLEN_FUNCTION(_utf_width) \
  1576. static inline size_t \
  1577. utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
  1578. { \
  1579. size_t len = 0; \
  1580. const uint ## _utf_width ## _t *ptr; \
  1581. for (ptr = str; \
  1582. *ptr != 0; \
  1583. ptr++) \
  1584. { \
  1585. len++; \
  1586. } \
  1587. \
  1588. return (len * ((_utf_width) / 8)); \
  1589. }
  1590. UTF_STRLEN_FUNCTION (8)
  1591. /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
  1592. #define UTF_STRLEN(_utf_width, _str) \
  1593. utf ## _utf_width ## _strlen (_str)
  1594. /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
  1595. ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
  1596. encoding name). */
  1597. static inline void
  1598. utf_encoding_name (char *name, size_t utf_width, SCM endianness)
  1599. {
  1600. strcpy (name, "UTF-");
  1601. strcat (name, ((utf_width == 8)
  1602. ? "8"
  1603. : ((utf_width == 16)
  1604. ? "16"
  1605. : ((utf_width == 32)
  1606. ? "32"
  1607. : "??"))));
  1608. strcat (name,
  1609. ((scm_is_eq (endianness, sym_big))
  1610. ? "BE"
  1611. : ((scm_is_eq (endianness, sym_little))
  1612. ? "LE"
  1613. : "unknown")));
  1614. }
  1615. /* Maximum length of a UTF encoding name. */
  1616. #define MAX_UTF_ENCODING_NAME_LEN 16
  1617. /* Produce the body of a `string->utf' function. */
  1618. #define STRING_TO_UTF(_utf_width) \
  1619. SCM utf; \
  1620. int err; \
  1621. char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
  1622. char *c_utf = NULL; \
  1623. size_t c_strlen, c_utf_len = 0; \
  1624. \
  1625. SCM_VALIDATE_STRING (1, str); \
  1626. if (scm_is_eq (endianness, SCM_UNDEFINED)) \
  1627. endianness = sym_big; \
  1628. else \
  1629. SCM_VALIDATE_SYMBOL (2, endianness); \
  1630. \
  1631. utf_encoding_name (c_utf_name, (_utf_width), endianness); \
  1632. \
  1633. c_strlen = scm_i_string_length (str); \
  1634. if (scm_i_is_narrow_string (str)) \
  1635. { \
  1636. err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
  1637. "ISO-8859-1", c_utf_name, \
  1638. iconveh_question_mark, NULL, \
  1639. &c_utf, &c_utf_len); \
  1640. if (SCM_UNLIKELY (err)) \
  1641. scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
  1642. scm_list_1 (str), err); \
  1643. } \
  1644. else \
  1645. { \
  1646. const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
  1647. c_utf = u32_conv_to_encoding (c_utf_name, \
  1648. iconveh_question_mark, \
  1649. (scm_t_uint32 *) wbuf, \
  1650. c_strlen, NULL, NULL, &c_utf_len); \
  1651. if (SCM_UNLIKELY (c_utf == NULL)) \
  1652. scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
  1653. scm_list_1 (str), errno); \
  1654. } \
  1655. scm_dynwind_begin (0); \
  1656. scm_dynwind_free (c_utf); \
  1657. utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
  1658. memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
  1659. scm_dynwind_end (); \
  1660. \
  1661. return (utf);
  1662. SCM_DEFINE (scm_string_to_utf8, "string->utf8",
  1663. 1, 0, 0,
  1664. (SCM str),
  1665. "Return a newly allocated bytevector that contains the UTF-8 "
  1666. "encoding of @var{str}.")
  1667. #define FUNC_NAME s_scm_string_to_utf8
  1668. {
  1669. SCM utf;
  1670. scm_t_uint8 *c_utf;
  1671. size_t c_utf_len = 0;
  1672. SCM_VALIDATE_STRING (1, str);
  1673. c_utf = (scm_t_uint8 *) scm_to_utf8_stringn (str, &c_utf_len);
  1674. utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1675. memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len);
  1676. free (c_utf);
  1677. return (utf);
  1678. }
  1679. #undef FUNC_NAME
  1680. SCM_DEFINE (scm_string_to_utf16, "string->utf16",
  1681. 1, 1, 0,
  1682. (SCM str, SCM endianness),
  1683. "Return a newly allocated bytevector that contains the UTF-16 "
  1684. "encoding of @var{str}.")
  1685. #define FUNC_NAME s_scm_string_to_utf16
  1686. {
  1687. STRING_TO_UTF (16);
  1688. }
  1689. #undef FUNC_NAME
  1690. static void
  1691. swap_u32 (scm_t_wchar *vals, size_t len)
  1692. {
  1693. size_t n;
  1694. for (n = 0; n < len; n++)
  1695. vals[n] = bswap_32 (vals[n]);
  1696. }
  1697. SCM_DEFINE (scm_string_to_utf32, "string->utf32",
  1698. 1, 1, 0,
  1699. (SCM str, SCM endianness),
  1700. "Return a newly allocated bytevector that contains the UTF-32 "
  1701. "encoding of @var{str}.")
  1702. #define FUNC_NAME s_scm_string_to_utf32
  1703. {
  1704. SCM bv;
  1705. scm_t_wchar *wchars;
  1706. size_t wchar_len, bytes_len;
  1707. wchars = scm_to_utf32_stringn (str, &wchar_len);
  1708. bytes_len = wchar_len * sizeof (scm_t_wchar);
  1709. if (!scm_is_eq (SCM_UNBNDP (endianness) ? scm_endianness_big : endianness,
  1710. scm_i_native_endianness))
  1711. swap_u32 (wchars, wchar_len);
  1712. bv = make_bytevector (bytes_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1713. memcpy (SCM_BYTEVECTOR_CONTENTS (bv), wchars, bytes_len);
  1714. free (wchars);
  1715. return bv;
  1716. }
  1717. #undef FUNC_NAME
  1718. /* Produce the body of a function that converts a UTF-encoded bytevector to a
  1719. string. */
  1720. #define UTF_TO_STRING(_utf_width) \
  1721. SCM str = SCM_BOOL_F; \
  1722. int err; \
  1723. char *c_str = NULL; \
  1724. char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
  1725. char *c_utf; \
  1726. size_t c_strlen = 0, c_utf_len = 0; \
  1727. \
  1728. SCM_VALIDATE_BYTEVECTOR (1, utf); \
  1729. if (scm_is_eq (endianness, SCM_UNDEFINED)) \
  1730. endianness = sym_big; \
  1731. else \
  1732. SCM_VALIDATE_SYMBOL (2, endianness); \
  1733. \
  1734. c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
  1735. c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
  1736. utf_encoding_name (c_utf_name, (_utf_width), endianness); \
  1737. \
  1738. err = mem_iconveh (c_utf, c_utf_len, \
  1739. c_utf_name, "UTF-8", \
  1740. iconveh_question_mark, NULL, \
  1741. &c_str, &c_strlen); \
  1742. if (SCM_UNLIKELY (err)) \
  1743. scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
  1744. scm_list_1 (utf), err); \
  1745. else \
  1746. { \
  1747. str = scm_from_utf8_stringn (c_str, c_strlen); \
  1748. free (c_str); \
  1749. } \
  1750. return (str);
  1751. SCM_DEFINE (scm_utf8_to_string, "utf8->string",
  1752. 1, 0, 0,
  1753. (SCM utf),
  1754. "Return a newly allocate string that contains from the UTF-8-"
  1755. "encoded contents of bytevector @var{utf}.")
  1756. #define FUNC_NAME s_scm_utf8_to_string
  1757. {
  1758. SCM str;
  1759. const char *c_utf;
  1760. size_t c_utf_len = 0;
  1761. SCM_VALIDATE_BYTEVECTOR (1, utf);
  1762. c_utf_len = SCM_BYTEVECTOR_LENGTH (utf);
  1763. c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf);
  1764. str = scm_from_utf8_stringn (c_utf, c_utf_len);
  1765. return (str);
  1766. }
  1767. #undef FUNC_NAME
  1768. SCM_DEFINE (scm_utf16_to_string, "utf16->string",
  1769. 1, 1, 0,
  1770. (SCM utf, SCM endianness),
  1771. "Return a newly allocate string that contains from the UTF-16-"
  1772. "encoded contents of bytevector @var{utf}.")
  1773. #define FUNC_NAME s_scm_utf16_to_string
  1774. {
  1775. UTF_TO_STRING (16);
  1776. }
  1777. #undef FUNC_NAME
  1778. SCM_DEFINE (scm_utf32_to_string, "utf32->string",
  1779. 1, 1, 0,
  1780. (SCM utf, SCM endianness),
  1781. "Return a newly allocate string that contains from the UTF-32-"
  1782. "encoded contents of bytevector @var{utf}.")
  1783. #define FUNC_NAME s_scm_utf32_to_string
  1784. {
  1785. UTF_TO_STRING (32);
  1786. }
  1787. #undef FUNC_NAME
  1788. /* Initialization. */
  1789. void
  1790. scm_bootstrap_bytevectors (void)
  1791. {
  1792. /* This must be instantiated here because the generalized-vector API may
  1793. want to access bytevectors even though `(rnrs bytevectors)' hasn't been
  1794. loaded. */
  1795. scm_null_bytevector = make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1796. scm_endianness_big = sym_big = scm_from_latin1_symbol ("big");
  1797. scm_endianness_little = sym_little = scm_from_latin1_symbol ("little");
  1798. #ifdef WORDS_BIGENDIAN
  1799. scm_i_native_endianness = sym_big;
  1800. #else
  1801. scm_i_native_endianness = sym_little;
  1802. #endif
  1803. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1804. "scm_init_bytevectors",
  1805. (scm_t_extension_init_func) scm_init_bytevectors,
  1806. NULL);
  1807. scm_i_register_vector_constructor
  1808. (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_VU8],
  1809. scm_make_bytevector);
  1810. }
  1811. void
  1812. scm_init_bytevectors (void)
  1813. {
  1814. #include "libguile/bytevectors.x"
  1815. }