bytevectors.c 64 KB

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