bytevectors.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  1. /* Copyright (C) 2009-2015 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(_len, _sign) \
  63. size_t c_len, c_index; \
  64. _sign char *c_bv; \
  65. \
  66. SCM_VALIDATE_BYTEVECTOR (1, bv); \
  67. c_index = scm_to_uint (index); \
  68. \
  69. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  70. c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  71. \
  72. if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
  73. scm_out_of_range (FUNC_NAME, index);
  74. /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
  75. #define INTEGER_REF(_len, _sign) \
  76. SCM result; \
  77. \
  78. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  79. SCM_VALIDATE_SYMBOL (3, endianness); \
  80. \
  81. { \
  82. INT_TYPE (_len, _sign) c_result; \
  83. \
  84. memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
  85. if (!scm_is_eq (endianness, scm_i_native_endianness)) \
  86. c_result = INT_SWAP (_len) (c_result); \
  87. \
  88. result = SCM_I_MAKINUM (c_result); \
  89. } \
  90. \
  91. return result;
  92. /* Template for fixed-size integer access using the native endianness. */
  93. #define INTEGER_NATIVE_REF(_len, _sign) \
  94. SCM result; \
  95. \
  96. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  97. \
  98. { \
  99. INT_TYPE (_len, _sign) c_result; \
  100. \
  101. memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
  102. result = SCM_I_MAKINUM (c_result); \
  103. } \
  104. \
  105. return result;
  106. /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
  107. #define INTEGER_SET(_len, _sign) \
  108. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  109. SCM_VALIDATE_SYMBOL (3, endianness); \
  110. \
  111. { \
  112. scm_t_signed_bits c_value; \
  113. INT_TYPE (_len, _sign) c_value_short; \
  114. \
  115. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  116. scm_wrong_type_arg (FUNC_NAME, 3, value); \
  117. \
  118. c_value = SCM_I_INUM (value); \
  119. if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
  120. scm_out_of_range (FUNC_NAME, value); \
  121. \
  122. c_value_short = (INT_TYPE (_len, _sign)) c_value; \
  123. if (!scm_is_eq (endianness, scm_i_native_endianness)) \
  124. c_value_short = INT_SWAP (_len) (c_value_short); \
  125. \
  126. memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
  127. } \
  128. \
  129. return SCM_UNSPECIFIED;
  130. /* Template for fixed-size integer modification using the native
  131. endianness. */
  132. #define INTEGER_NATIVE_SET(_len, _sign) \
  133. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  134. \
  135. { \
  136. scm_t_signed_bits c_value; \
  137. INT_TYPE (_len, _sign) c_value_short; \
  138. \
  139. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  140. scm_wrong_type_arg (FUNC_NAME, 3, value); \
  141. \
  142. c_value = SCM_I_INUM (value); \
  143. if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
  144. scm_out_of_range (FUNC_NAME, value); \
  145. \
  146. c_value_short = (INT_TYPE (_len, _sign)) c_value; \
  147. \
  148. memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
  149. } \
  150. \
  151. return SCM_UNSPECIFIED;
  152. /* Bytevector type. */
  153. #define SCM_BYTEVECTOR_HEADER_BYTES \
  154. (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (scm_t_bits))
  155. #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
  156. SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
  157. #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
  158. SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
  159. #define SCM_BYTEVECTOR_SET_CONTIGUOUS_P(bv, contiguous_p) \
  160. SCM_SET_BYTEVECTOR_FLAGS ((bv), \
  161. SCM_BYTEVECTOR_ELEMENT_TYPE (bv) \
  162. | ((contiguous_p) << 8UL))
  163. #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
  164. SCM_SET_BYTEVECTOR_FLAGS ((bv), \
  165. (hint) \
  166. | (SCM_BYTEVECTOR_CONTIGUOUS_P (bv) << 8UL))
  167. #define SCM_BYTEVECTOR_SET_PARENT(_bv, _parent) \
  168. SCM_SET_CELL_OBJECT_3 ((_bv), (_parent))
  169. #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
  170. (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
  171. #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
  172. (SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var))
  173. /* The empty bytevector. */
  174. SCM scm_null_bytevector = SCM_UNSPECIFIED;
  175. static inline SCM
  176. make_bytevector (size_t len, scm_t_array_element_type element_type)
  177. {
  178. SCM ret;
  179. size_t c_len;
  180. if (SCM_UNLIKELY (element_type > SCM_ARRAY_ELEMENT_TYPE_LAST
  181. || scm_i_array_element_type_sizes[element_type] < 8
  182. || len >= (((size_t) -1)
  183. / (scm_i_array_element_type_sizes[element_type]/8))))
  184. /* This would be an internal Guile programming error */
  185. abort ();
  186. if (SCM_UNLIKELY (len == 0 && element_type == SCM_ARRAY_ELEMENT_TYPE_VU8
  187. && SCM_BYTEVECTOR_P (scm_null_bytevector)))
  188. ret = scm_null_bytevector;
  189. else
  190. {
  191. signed char *contents;
  192. c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
  193. contents = scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES + c_len,
  194. SCM_GC_BYTEVECTOR);
  195. ret = SCM_PACK_POINTER (contents);
  196. contents += SCM_BYTEVECTOR_HEADER_BYTES;
  197. SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
  198. SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
  199. SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret, 1);
  200. SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret, element_type);
  201. SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
  202. }
  203. return ret;
  204. }
  205. /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
  206. values taken from CONTENTS. Assume that the storage for CONTENTS will be
  207. automatically reclaimed when it becomes unreachable. */
  208. static inline SCM
  209. make_bytevector_from_buffer (size_t len, void *contents,
  210. scm_t_array_element_type element_type)
  211. {
  212. SCM ret;
  213. if (SCM_UNLIKELY (len == 0))
  214. ret = make_bytevector (len, element_type);
  215. else
  216. {
  217. size_t c_len;
  218. ret = SCM_PACK_POINTER (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES,
  219. SCM_GC_BYTEVECTOR));
  220. c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
  221. SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
  222. SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
  223. SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret, 0);
  224. SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret, element_type);
  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_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_unlocked ('#', port);
  344. scm_write (scm_array_handle_element_type (&h), port);
  345. scm_putc_unlocked ('(', 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_unlocked (' ', port);
  351. scm_write (scm_array_handle_ref (&h, i), port);
  352. }
  353. scm_putc_unlocked (')', port);
  354. return 1;
  355. }
  356. /* General operations. */
  357. SCM_SYMBOL (scm_sym_big, "big");
  358. SCM_SYMBOL (scm_sym_little, "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_uint (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_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_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_start + c_len > c_source_len))
  490. scm_out_of_range (FUNC_NAME, source_start);
  491. if (SCM_UNLIKELY (c_target_start + c_len > c_target_len))
  492. scm_out_of_range (FUNC_NAME, target_start);
  493. memmove (c_target + c_target_start,
  494. c_source + c_source_start,
  495. c_len);
  496. return SCM_UNSPECIFIED;
  497. }
  498. #undef FUNC_NAME
  499. SCM_DEFINE (scm_bytevector_copy, "bytevector-copy", 1, 0, 0,
  500. (SCM bv),
  501. "Return a newly allocated copy of @var{bv}.")
  502. #define FUNC_NAME s_scm_bytevector_copy
  503. {
  504. SCM copy;
  505. size_t c_len;
  506. signed char *c_bv, *c_copy;
  507. SCM_VALIDATE_BYTEVECTOR (1, bv);
  508. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  509. c_bv = SCM_BYTEVECTOR_CONTENTS (bv);
  510. copy = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  511. c_copy = SCM_BYTEVECTOR_CONTENTS (copy);
  512. memcpy (c_copy, c_bv, c_len);
  513. return copy;
  514. }
  515. #undef FUNC_NAME
  516. SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector",
  517. 1, 0, 0, (SCM array),
  518. "Return a newly allocated bytevector whose contents\n"
  519. "will be copied from the uniform array @var{array}.")
  520. #define FUNC_NAME s_scm_uniform_array_to_bytevector
  521. {
  522. SCM contents, ret;
  523. size_t len, sz, byte_len;
  524. scm_t_array_handle h;
  525. const void *elts;
  526. contents = scm_array_contents (array, SCM_BOOL_T);
  527. if (scm_is_false (contents))
  528. scm_wrong_type_arg_msg (FUNC_NAME, 0, array, "uniform contiguous array");
  529. scm_array_get_handle (contents, &h);
  530. assert (h.base == 0);
  531. elts = h.elements;
  532. len = h.dims->inc * (h.dims->ubnd - h.dims->lbnd + 1);
  533. sz = scm_array_handle_uniform_element_bit_size (&h);
  534. if (sz >= 8 && ((sz % 8) == 0))
  535. byte_len = len * (sz / 8);
  536. else if (sz < 8)
  537. /* Elements of sub-byte size (bitvectors) are addressed in 32-bit
  538. units. */
  539. byte_len = ((len * sz + 31) / 32) * 4;
  540. else
  541. /* an internal guile error, really */
  542. SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
  543. ret = make_bytevector (byte_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  544. memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
  545. scm_array_handle_release (&h);
  546. return ret;
  547. }
  548. #undef FUNC_NAME
  549. /* Operations on bytes and octets. */
  550. SCM_DEFINE (scm_bytevector_u8_ref, "bytevector-u8-ref", 2, 0, 0,
  551. (SCM bv, SCM index),
  552. "Return the octet located at @var{index} in @var{bv}.")
  553. #define FUNC_NAME s_scm_bytevector_u8_ref
  554. {
  555. INTEGER_NATIVE_REF (8, unsigned);
  556. }
  557. #undef FUNC_NAME
  558. SCM_DEFINE (scm_bytevector_s8_ref, "bytevector-s8-ref", 2, 0, 0,
  559. (SCM bv, SCM index),
  560. "Return the byte located at @var{index} in @var{bv}.")
  561. #define FUNC_NAME s_scm_bytevector_s8_ref
  562. {
  563. INTEGER_NATIVE_REF (8, signed);
  564. }
  565. #undef FUNC_NAME
  566. SCM_DEFINE (scm_bytevector_u8_set_x, "bytevector-u8-set!", 3, 0, 0,
  567. (SCM bv, SCM index, SCM value),
  568. "Return the octet located at @var{index} in @var{bv}.")
  569. #define FUNC_NAME s_scm_bytevector_u8_set_x
  570. {
  571. INTEGER_NATIVE_SET (8, unsigned);
  572. }
  573. #undef FUNC_NAME
  574. SCM_DEFINE (scm_bytevector_s8_set_x, "bytevector-s8-set!", 3, 0, 0,
  575. (SCM bv, SCM index, SCM value),
  576. "Return the octet located at @var{index} in @var{bv}.")
  577. #define FUNC_NAME s_scm_bytevector_s8_set_x
  578. {
  579. INTEGER_NATIVE_SET (8, signed);
  580. }
  581. #undef FUNC_NAME
  582. #undef OCTET_ACCESSOR_PROLOGUE
  583. SCM_DEFINE (scm_bytevector_to_u8_list, "bytevector->u8-list", 1, 0, 0,
  584. (SCM bv),
  585. "Return a newly allocated list of octets containing the "
  586. "contents of @var{bv}.")
  587. #define FUNC_NAME s_scm_bytevector_to_u8_list
  588. {
  589. SCM lst, pair;
  590. size_t c_len, i;
  591. scm_t_uint8 *c_bv;
  592. SCM_VALIDATE_BYTEVECTOR (1, bv);
  593. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  594. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  595. lst = scm_make_list (scm_from_size_t (c_len), SCM_UNSPECIFIED);
  596. for (i = 0, pair = lst;
  597. i < c_len;
  598. i++, pair = SCM_CDR (pair))
  599. {
  600. SCM_SETCAR (pair, SCM_I_MAKINUM (c_bv[i]));
  601. }
  602. return lst;
  603. }
  604. #undef FUNC_NAME
  605. SCM_DEFINE (scm_u8_list_to_bytevector, "u8-list->bytevector", 1, 0, 0,
  606. (SCM lst),
  607. "Turn @var{lst}, a list of octets, into a bytevector.")
  608. #define FUNC_NAME s_scm_u8_list_to_bytevector
  609. {
  610. SCM bv, item;
  611. size_t c_len, i;
  612. scm_t_uint8 *c_bv;
  613. SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len);
  614. bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  615. c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
  616. for (i = 0; i < c_len; lst = SCM_CDR (lst), i++)
  617. {
  618. item = SCM_CAR (lst);
  619. if (SCM_LIKELY (SCM_I_INUMP (item)))
  620. {
  621. scm_t_signed_bits c_item;
  622. c_item = SCM_I_INUM (item);
  623. if (SCM_LIKELY ((c_item >= 0) && (c_item < 256)))
  624. c_bv[i] = (scm_t_uint8) c_item;
  625. else
  626. goto type_error;
  627. }
  628. else
  629. goto type_error;
  630. }
  631. return bv;
  632. type_error:
  633. scm_wrong_type_arg (FUNC_NAME, 1, item);
  634. return SCM_BOOL_F;
  635. }
  636. #undef FUNC_NAME
  637. /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
  638. using (2^(SIZE * 8) - VALUE). */
  639. static inline void
  640. twos_complement (mpz_t value, size_t size)
  641. {
  642. unsigned long bit_count;
  643. /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
  644. checking on SIZE performed earlier. */
  645. bit_count = (unsigned long) size << 3UL;
  646. if (SCM_LIKELY (bit_count < sizeof (unsigned long)))
  647. mpz_ui_sub (value, 1UL << bit_count, value);
  648. else
  649. {
  650. mpz_t max;
  651. mpz_init (max);
  652. mpz_ui_pow_ui (max, 2, bit_count);
  653. mpz_sub (value, max, value);
  654. mpz_clear (max);
  655. }
  656. }
  657. static inline SCM
  658. bytevector_large_ref (const char *c_bv, size_t c_size, int signed_p,
  659. SCM endianness)
  660. {
  661. SCM result;
  662. mpz_t c_mpz;
  663. int c_endianness, negative_p = 0;
  664. if (signed_p)
  665. {
  666. if (scm_is_eq (endianness, scm_sym_big))
  667. negative_p = c_bv[0] & 0x80;
  668. else
  669. negative_p = c_bv[c_size - 1] & 0x80;
  670. }
  671. c_endianness = scm_is_eq (endianness, scm_sym_big) ? 1 : -1;
  672. mpz_init (c_mpz);
  673. mpz_import (c_mpz, 1 /* 1 word */, 1 /* word order doesn't matter */,
  674. c_size /* word is C_SIZE-byte long */,
  675. c_endianness,
  676. 0 /* nails */, c_bv);
  677. if (signed_p && negative_p)
  678. {
  679. twos_complement (c_mpz, c_size);
  680. mpz_neg (c_mpz, c_mpz);
  681. }
  682. result = scm_from_mpz (c_mpz);
  683. mpz_clear (c_mpz); /* FIXME: Needed? */
  684. return result;
  685. }
  686. static inline int
  687. bytevector_large_set (char *c_bv, size_t c_size, int signed_p,
  688. SCM value, SCM endianness)
  689. {
  690. mpz_t c_mpz;
  691. int c_endianness, c_sign, err = 0;
  692. c_endianness = scm_is_eq (endianness, scm_sym_big) ? 1 : -1;
  693. mpz_init (c_mpz);
  694. scm_to_mpz (value, c_mpz);
  695. c_sign = mpz_sgn (c_mpz);
  696. if (c_sign < 0)
  697. {
  698. if (SCM_LIKELY (signed_p))
  699. {
  700. mpz_neg (c_mpz, c_mpz);
  701. twos_complement (c_mpz, c_size);
  702. }
  703. else
  704. {
  705. err = -1;
  706. goto finish;
  707. }
  708. }
  709. if (c_sign == 0)
  710. /* Zero. */
  711. memset (c_bv, 0, c_size);
  712. else
  713. {
  714. size_t word_count, value_size;
  715. value_size = (mpz_sizeinbase (c_mpz, 2) + (8 * c_size)) / (8 * c_size);
  716. if (SCM_UNLIKELY (value_size > c_size))
  717. {
  718. err = -2;
  719. goto finish;
  720. }
  721. mpz_export (c_bv, &word_count, 1 /* word order doesn't matter */,
  722. c_size, c_endianness,
  723. 0 /* nails */, c_mpz);
  724. if (SCM_UNLIKELY (word_count != 1))
  725. /* Shouldn't happen since we already checked with VALUE_SIZE. */
  726. abort ();
  727. }
  728. finish:
  729. mpz_clear (c_mpz);
  730. return err;
  731. }
  732. #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
  733. size_t c_len, c_index, c_size; \
  734. char *c_bv; \
  735. \
  736. SCM_VALIDATE_BYTEVECTOR (1, bv); \
  737. c_index = scm_to_size_t (index); \
  738. c_size = scm_to_size_t (size); \
  739. \
  740. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  741. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  742. \
  743. /* C_SIZE must have its 3 higher bits set to zero so that \
  744. multiplying it by 8 yields a number that fits in a \
  745. size_t. */ \
  746. if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
  747. scm_out_of_range (FUNC_NAME, size); \
  748. if (SCM_UNLIKELY (c_index + c_size > c_len)) \
  749. scm_out_of_range (FUNC_NAME, index);
  750. /* Template of an integer reference function. */
  751. #define GENERIC_INTEGER_REF(_sign) \
  752. SCM result; \
  753. \
  754. if (c_size < 3) \
  755. { \
  756. int swap; \
  757. _sign int value; \
  758. \
  759. swap = !scm_is_eq (endianness, scm_i_native_endianness); \
  760. switch (c_size) \
  761. { \
  762. case 1: \
  763. { \
  764. _sign char c_value8; \
  765. memcpy (&c_value8, c_bv, 1); \
  766. value = c_value8; \
  767. } \
  768. break; \
  769. case 2: \
  770. { \
  771. INT_TYPE (16, _sign) c_value16; \
  772. memcpy (&c_value16, c_bv, 2); \
  773. if (swap) \
  774. value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
  775. else \
  776. value = c_value16; \
  777. } \
  778. break; \
  779. default: \
  780. abort (); \
  781. } \
  782. \
  783. result = SCM_I_MAKINUM ((_sign int) value); \
  784. } \
  785. else \
  786. result = bytevector_large_ref ((char *) c_bv, \
  787. c_size, SIGNEDNESS (_sign), \
  788. endianness); \
  789. \
  790. return result;
  791. static inline SCM
  792. bytevector_signed_ref (const char *c_bv, size_t c_size, SCM endianness)
  793. {
  794. GENERIC_INTEGER_REF (signed);
  795. }
  796. static inline SCM
  797. bytevector_unsigned_ref (const char *c_bv, size_t c_size, SCM endianness)
  798. {
  799. GENERIC_INTEGER_REF (unsigned);
  800. }
  801. /* Template of an integer assignment function. */
  802. #define GENERIC_INTEGER_SET(_sign) \
  803. if (c_size < 3) \
  804. { \
  805. scm_t_signed_bits c_value; \
  806. \
  807. if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
  808. goto range_error; \
  809. \
  810. c_value = SCM_I_INUM (value); \
  811. switch (c_size) \
  812. { \
  813. case 1: \
  814. if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
  815. { \
  816. _sign char c_value8; \
  817. c_value8 = (_sign char) c_value; \
  818. memcpy (c_bv, &c_value8, 1); \
  819. } \
  820. else \
  821. goto range_error; \
  822. break; \
  823. \
  824. case 2: \
  825. if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
  826. { \
  827. int swap; \
  828. INT_TYPE (16, _sign) c_value16; \
  829. \
  830. swap = !scm_is_eq (endianness, scm_i_native_endianness); \
  831. \
  832. if (swap) \
  833. c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
  834. else \
  835. c_value16 = c_value; \
  836. \
  837. memcpy (c_bv, &c_value16, 2); \
  838. } \
  839. else \
  840. goto range_error; \
  841. break; \
  842. \
  843. default: \
  844. abort (); \
  845. } \
  846. } \
  847. else \
  848. { \
  849. int err; \
  850. \
  851. err = bytevector_large_set (c_bv, c_size, \
  852. SIGNEDNESS (_sign), \
  853. value, endianness); \
  854. if (err) \
  855. goto range_error; \
  856. } \
  857. \
  858. return; \
  859. \
  860. range_error: \
  861. scm_out_of_range (FUNC_NAME, value); \
  862. return;
  863. static inline void
  864. bytevector_signed_set (char *c_bv, size_t c_size,
  865. SCM value, SCM endianness,
  866. const char *func_name)
  867. #define FUNC_NAME func_name
  868. {
  869. GENERIC_INTEGER_SET (signed);
  870. }
  871. #undef FUNC_NAME
  872. static inline void
  873. bytevector_unsigned_set (char *c_bv, size_t c_size,
  874. SCM value, SCM endianness,
  875. const char *func_name)
  876. #define FUNC_NAME func_name
  877. {
  878. GENERIC_INTEGER_SET (unsigned);
  879. }
  880. #undef FUNC_NAME
  881. #undef GENERIC_INTEGER_SET
  882. #undef GENERIC_INTEGER_REF
  883. SCM_DEFINE (scm_bytevector_uint_ref, "bytevector-uint-ref", 4, 0, 0,
  884. (SCM bv, SCM index, SCM endianness, SCM size),
  885. "Return the @var{size}-octet long unsigned integer at index "
  886. "@var{index} in @var{bv}.")
  887. #define FUNC_NAME s_scm_bytevector_uint_ref
  888. {
  889. GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
  890. return (bytevector_unsigned_ref (&c_bv[c_index], c_size, endianness));
  891. }
  892. #undef FUNC_NAME
  893. SCM_DEFINE (scm_bytevector_sint_ref, "bytevector-sint-ref", 4, 0, 0,
  894. (SCM bv, SCM index, SCM endianness, SCM size),
  895. "Return the @var{size}-octet long unsigned integer at index "
  896. "@var{index} in @var{bv}.")
  897. #define FUNC_NAME s_scm_bytevector_sint_ref
  898. {
  899. GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
  900. return (bytevector_signed_ref (&c_bv[c_index], c_size, endianness));
  901. }
  902. #undef FUNC_NAME
  903. SCM_DEFINE (scm_bytevector_uint_set_x, "bytevector-uint-set!", 5, 0, 0,
  904. (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
  905. "Set the @var{size}-octet long unsigned integer at @var{index} "
  906. "to @var{value}.")
  907. #define FUNC_NAME s_scm_bytevector_uint_set_x
  908. {
  909. GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
  910. bytevector_unsigned_set (&c_bv[c_index], c_size, value, endianness,
  911. FUNC_NAME);
  912. return SCM_UNSPECIFIED;
  913. }
  914. #undef FUNC_NAME
  915. SCM_DEFINE (scm_bytevector_sint_set_x, "bytevector-sint-set!", 5, 0, 0,
  916. (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
  917. "Set the @var{size}-octet long signed integer at @var{index} "
  918. "to @var{value}.")
  919. #define FUNC_NAME s_scm_bytevector_sint_set_x
  920. {
  921. GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
  922. bytevector_signed_set (&c_bv[c_index], c_size, value, endianness,
  923. FUNC_NAME);
  924. return SCM_UNSPECIFIED;
  925. }
  926. #undef FUNC_NAME
  927. /* Operations on integers of arbitrary size. */
  928. #define INTEGERS_TO_LIST(_sign) \
  929. SCM lst, pair; \
  930. size_t i, c_len, c_size; \
  931. \
  932. SCM_VALIDATE_BYTEVECTOR (1, bv); \
  933. SCM_VALIDATE_SYMBOL (2, endianness); \
  934. c_size = scm_to_unsigned_integer (size, 1, (size_t) -1); \
  935. \
  936. c_len = SCM_BYTEVECTOR_LENGTH (bv); \
  937. if (SCM_UNLIKELY (c_len % c_size != 0)) \
  938. scm_wrong_type_arg_msg \
  939. (FUNC_NAME, 0, size, \
  940. "an exact positive integer that divides the bytevector length"); \
  941. else if (SCM_UNLIKELY (c_len == 0)) \
  942. lst = SCM_EOL; \
  943. else \
  944. { \
  945. const char *c_bv; \
  946. \
  947. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  948. \
  949. lst = scm_make_list (scm_from_size_t (c_len / c_size), \
  950. SCM_UNSPECIFIED); \
  951. for (i = 0, pair = lst; \
  952. i <= c_len - c_size; \
  953. i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
  954. { \
  955. SCM_SETCAR (pair, \
  956. bytevector_ ## _sign ## _ref (c_bv, c_size, \
  957. endianness)); \
  958. } \
  959. } \
  960. \
  961. return lst;
  962. SCM_DEFINE (scm_bytevector_to_sint_list, "bytevector->sint-list",
  963. 3, 0, 0,
  964. (SCM bv, SCM endianness, SCM size),
  965. "Return a list of signed integers of @var{size} octets "
  966. "representing the contents of @var{bv}.")
  967. #define FUNC_NAME s_scm_bytevector_to_sint_list
  968. {
  969. INTEGERS_TO_LIST (signed);
  970. }
  971. #undef FUNC_NAME
  972. SCM_DEFINE (scm_bytevector_to_uint_list, "bytevector->uint-list",
  973. 3, 0, 0,
  974. (SCM bv, SCM endianness, SCM size),
  975. "Return a list of unsigned integers of @var{size} octets "
  976. "representing the contents of @var{bv}.")
  977. #define FUNC_NAME s_scm_bytevector_to_uint_list
  978. {
  979. INTEGERS_TO_LIST (unsigned);
  980. }
  981. #undef FUNC_NAME
  982. #undef INTEGER_TO_LIST
  983. #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
  984. SCM bv; \
  985. size_t c_len; \
  986. size_t c_size; \
  987. char *c_bv, *c_bv_ptr; \
  988. \
  989. SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
  990. SCM_VALIDATE_SYMBOL (2, endianness); \
  991. c_size = scm_to_size_t (size); \
  992. \
  993. if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
  994. scm_out_of_range (FUNC_NAME, size); \
  995. \
  996. bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
  997. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
  998. \
  999. for (c_bv_ptr = c_bv; \
  1000. !scm_is_null (lst); \
  1001. lst = SCM_CDR (lst), c_bv_ptr += c_size) \
  1002. { \
  1003. bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
  1004. SCM_CAR (lst), endianness, \
  1005. FUNC_NAME); \
  1006. } \
  1007. \
  1008. return bv;
  1009. SCM_DEFINE (scm_uint_list_to_bytevector, "uint-list->bytevector",
  1010. 3, 0, 0,
  1011. (SCM lst, SCM endianness, SCM size),
  1012. "Return a bytevector containing the unsigned integers "
  1013. "listed in @var{lst} and encoded on @var{size} octets "
  1014. "according to @var{endianness}.")
  1015. #define FUNC_NAME s_scm_uint_list_to_bytevector
  1016. {
  1017. INTEGER_LIST_TO_BYTEVECTOR (unsigned);
  1018. }
  1019. #undef FUNC_NAME
  1020. SCM_DEFINE (scm_sint_list_to_bytevector, "sint-list->bytevector",
  1021. 3, 0, 0,
  1022. (SCM lst, SCM endianness, SCM size),
  1023. "Return a bytevector containing the signed integers "
  1024. "listed in @var{lst} and encoded on @var{size} octets "
  1025. "according to @var{endianness}.")
  1026. #define FUNC_NAME s_scm_sint_list_to_bytevector
  1027. {
  1028. INTEGER_LIST_TO_BYTEVECTOR (signed);
  1029. }
  1030. #undef FUNC_NAME
  1031. #undef INTEGER_LIST_TO_BYTEVECTOR
  1032. /* Operations on 16-bit integers. */
  1033. SCM_DEFINE (scm_bytevector_u16_ref, "bytevector-u16-ref",
  1034. 3, 0, 0,
  1035. (SCM bv, SCM index, SCM endianness),
  1036. "Return the unsigned 16-bit integer from @var{bv} at "
  1037. "@var{index}.")
  1038. #define FUNC_NAME s_scm_bytevector_u16_ref
  1039. {
  1040. INTEGER_REF (16, unsigned);
  1041. }
  1042. #undef FUNC_NAME
  1043. SCM_DEFINE (scm_bytevector_s16_ref, "bytevector-s16-ref",
  1044. 3, 0, 0,
  1045. (SCM bv, SCM index, SCM endianness),
  1046. "Return the signed 16-bit integer from @var{bv} at "
  1047. "@var{index}.")
  1048. #define FUNC_NAME s_scm_bytevector_s16_ref
  1049. {
  1050. INTEGER_REF (16, signed);
  1051. }
  1052. #undef FUNC_NAME
  1053. SCM_DEFINE (scm_bytevector_u16_native_ref, "bytevector-u16-native-ref",
  1054. 2, 0, 0,
  1055. (SCM bv, SCM index),
  1056. "Return the unsigned 16-bit integer from @var{bv} at "
  1057. "@var{index} using the native endianness.")
  1058. #define FUNC_NAME s_scm_bytevector_u16_native_ref
  1059. {
  1060. INTEGER_NATIVE_REF (16, unsigned);
  1061. }
  1062. #undef FUNC_NAME
  1063. SCM_DEFINE (scm_bytevector_s16_native_ref, "bytevector-s16-native-ref",
  1064. 2, 0, 0,
  1065. (SCM bv, SCM index),
  1066. "Return the unsigned 16-bit integer from @var{bv} at "
  1067. "@var{index} using the native endianness.")
  1068. #define FUNC_NAME s_scm_bytevector_s16_native_ref
  1069. {
  1070. INTEGER_NATIVE_REF (16, signed);
  1071. }
  1072. #undef FUNC_NAME
  1073. SCM_DEFINE (scm_bytevector_u16_set_x, "bytevector-u16-set!",
  1074. 4, 0, 0,
  1075. (SCM bv, SCM index, SCM value, SCM endianness),
  1076. "Store @var{value} in @var{bv} at @var{index} according to "
  1077. "@var{endianness}.")
  1078. #define FUNC_NAME s_scm_bytevector_u16_set_x
  1079. {
  1080. INTEGER_SET (16, unsigned);
  1081. }
  1082. #undef FUNC_NAME
  1083. SCM_DEFINE (scm_bytevector_s16_set_x, "bytevector-s16-set!",
  1084. 4, 0, 0,
  1085. (SCM bv, SCM index, SCM value, SCM endianness),
  1086. "Store @var{value} in @var{bv} at @var{index} according to "
  1087. "@var{endianness}.")
  1088. #define FUNC_NAME s_scm_bytevector_s16_set_x
  1089. {
  1090. INTEGER_SET (16, signed);
  1091. }
  1092. #undef FUNC_NAME
  1093. SCM_DEFINE (scm_bytevector_u16_native_set_x, "bytevector-u16-native-set!",
  1094. 3, 0, 0,
  1095. (SCM bv, SCM index, SCM value),
  1096. "Store the unsigned integer @var{value} at index @var{index} "
  1097. "of @var{bv} using the native endianness.")
  1098. #define FUNC_NAME s_scm_bytevector_u16_native_set_x
  1099. {
  1100. INTEGER_NATIVE_SET (16, unsigned);
  1101. }
  1102. #undef FUNC_NAME
  1103. SCM_DEFINE (scm_bytevector_s16_native_set_x, "bytevector-s16-native-set!",
  1104. 3, 0, 0,
  1105. (SCM bv, SCM index, SCM value),
  1106. "Store the signed integer @var{value} at index @var{index} "
  1107. "of @var{bv} using the native endianness.")
  1108. #define FUNC_NAME s_scm_bytevector_s16_native_set_x
  1109. {
  1110. INTEGER_NATIVE_SET (16, signed);
  1111. }
  1112. #undef FUNC_NAME
  1113. /* Operations on 32-bit integers. */
  1114. /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
  1115. arbitrary 32-bit integers. Thus we fall back to using the
  1116. `large_{ref,set}' variants on 32-bit machines. */
  1117. #define LARGE_INTEGER_REF(_len, _sign) \
  1118. INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
  1119. SCM_VALIDATE_SYMBOL (3, endianness); \
  1120. \
  1121. return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
  1122. SIGNEDNESS (_sign), endianness));
  1123. #define LARGE_INTEGER_SET(_len, _sign) \
  1124. int err; \
  1125. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  1126. SCM_VALIDATE_SYMBOL (4, endianness); \
  1127. \
  1128. err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
  1129. SIGNEDNESS (_sign), value, endianness); \
  1130. if (SCM_UNLIKELY (err)) \
  1131. scm_out_of_range (FUNC_NAME, value); \
  1132. \
  1133. return SCM_UNSPECIFIED;
  1134. #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
  1135. INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
  1136. return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
  1137. SIGNEDNESS (_sign), scm_i_native_endianness));
  1138. #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
  1139. int err; \
  1140. INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
  1141. \
  1142. err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
  1143. SIGNEDNESS (_sign), value, \
  1144. scm_i_native_endianness); \
  1145. if (SCM_UNLIKELY (err)) \
  1146. scm_out_of_range (FUNC_NAME, value); \
  1147. \
  1148. return SCM_UNSPECIFIED;
  1149. SCM_DEFINE (scm_bytevector_u32_ref, "bytevector-u32-ref",
  1150. 3, 0, 0,
  1151. (SCM bv, SCM index, SCM endianness),
  1152. "Return the unsigned 32-bit integer from @var{bv} at "
  1153. "@var{index}.")
  1154. #define FUNC_NAME s_scm_bytevector_u32_ref
  1155. {
  1156. #if SIZEOF_VOID_P > 4
  1157. INTEGER_REF (32, unsigned);
  1158. #else
  1159. LARGE_INTEGER_REF (32, unsigned);
  1160. #endif
  1161. }
  1162. #undef FUNC_NAME
  1163. SCM_DEFINE (scm_bytevector_s32_ref, "bytevector-s32-ref",
  1164. 3, 0, 0,
  1165. (SCM bv, SCM index, SCM endianness),
  1166. "Return the signed 32-bit integer from @var{bv} at "
  1167. "@var{index}.")
  1168. #define FUNC_NAME s_scm_bytevector_s32_ref
  1169. {
  1170. #if SIZEOF_VOID_P > 4
  1171. INTEGER_REF (32, signed);
  1172. #else
  1173. LARGE_INTEGER_REF (32, signed);
  1174. #endif
  1175. }
  1176. #undef FUNC_NAME
  1177. SCM_DEFINE (scm_bytevector_u32_native_ref, "bytevector-u32-native-ref",
  1178. 2, 0, 0,
  1179. (SCM bv, SCM index),
  1180. "Return the unsigned 32-bit integer from @var{bv} at "
  1181. "@var{index} using the native endianness.")
  1182. #define FUNC_NAME s_scm_bytevector_u32_native_ref
  1183. {
  1184. #if SIZEOF_VOID_P > 4
  1185. INTEGER_NATIVE_REF (32, unsigned);
  1186. #else
  1187. LARGE_INTEGER_NATIVE_REF (32, unsigned);
  1188. #endif
  1189. }
  1190. #undef FUNC_NAME
  1191. SCM_DEFINE (scm_bytevector_s32_native_ref, "bytevector-s32-native-ref",
  1192. 2, 0, 0,
  1193. (SCM bv, SCM index),
  1194. "Return the unsigned 32-bit integer from @var{bv} at "
  1195. "@var{index} using the native endianness.")
  1196. #define FUNC_NAME s_scm_bytevector_s32_native_ref
  1197. {
  1198. #if SIZEOF_VOID_P > 4
  1199. INTEGER_NATIVE_REF (32, signed);
  1200. #else
  1201. LARGE_INTEGER_NATIVE_REF (32, signed);
  1202. #endif
  1203. }
  1204. #undef FUNC_NAME
  1205. SCM_DEFINE (scm_bytevector_u32_set_x, "bytevector-u32-set!",
  1206. 4, 0, 0,
  1207. (SCM bv, SCM index, SCM value, SCM endianness),
  1208. "Store @var{value} in @var{bv} at @var{index} according to "
  1209. "@var{endianness}.")
  1210. #define FUNC_NAME s_scm_bytevector_u32_set_x
  1211. {
  1212. #if SIZEOF_VOID_P > 4
  1213. INTEGER_SET (32, unsigned);
  1214. #else
  1215. LARGE_INTEGER_SET (32, unsigned);
  1216. #endif
  1217. }
  1218. #undef FUNC_NAME
  1219. SCM_DEFINE (scm_bytevector_s32_set_x, "bytevector-s32-set!",
  1220. 4, 0, 0,
  1221. (SCM bv, SCM index, SCM value, SCM endianness),
  1222. "Store @var{value} in @var{bv} at @var{index} according to "
  1223. "@var{endianness}.")
  1224. #define FUNC_NAME s_scm_bytevector_s32_set_x
  1225. {
  1226. #if SIZEOF_VOID_P > 4
  1227. INTEGER_SET (32, signed);
  1228. #else
  1229. LARGE_INTEGER_SET (32, signed);
  1230. #endif
  1231. }
  1232. #undef FUNC_NAME
  1233. SCM_DEFINE (scm_bytevector_u32_native_set_x, "bytevector-u32-native-set!",
  1234. 3, 0, 0,
  1235. (SCM bv, SCM index, SCM value),
  1236. "Store the unsigned integer @var{value} at index @var{index} "
  1237. "of @var{bv} using the native endianness.")
  1238. #define FUNC_NAME s_scm_bytevector_u32_native_set_x
  1239. {
  1240. #if SIZEOF_VOID_P > 4
  1241. INTEGER_NATIVE_SET (32, unsigned);
  1242. #else
  1243. LARGE_INTEGER_NATIVE_SET (32, unsigned);
  1244. #endif
  1245. }
  1246. #undef FUNC_NAME
  1247. SCM_DEFINE (scm_bytevector_s32_native_set_x, "bytevector-s32-native-set!",
  1248. 3, 0, 0,
  1249. (SCM bv, SCM index, SCM value),
  1250. "Store the signed integer @var{value} at index @var{index} "
  1251. "of @var{bv} using the native endianness.")
  1252. #define FUNC_NAME s_scm_bytevector_s32_native_set_x
  1253. {
  1254. #if SIZEOF_VOID_P > 4
  1255. INTEGER_NATIVE_SET (32, signed);
  1256. #else
  1257. LARGE_INTEGER_NATIVE_SET (32, signed);
  1258. #endif
  1259. }
  1260. #undef FUNC_NAME
  1261. /* Operations on 64-bit integers. */
  1262. /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
  1263. SCM_DEFINE (scm_bytevector_u64_ref, "bytevector-u64-ref",
  1264. 3, 0, 0,
  1265. (SCM bv, SCM index, SCM endianness),
  1266. "Return the unsigned 64-bit integer from @var{bv} at "
  1267. "@var{index}.")
  1268. #define FUNC_NAME s_scm_bytevector_u64_ref
  1269. {
  1270. LARGE_INTEGER_REF (64, unsigned);
  1271. }
  1272. #undef FUNC_NAME
  1273. SCM_DEFINE (scm_bytevector_s64_ref, "bytevector-s64-ref",
  1274. 3, 0, 0,
  1275. (SCM bv, SCM index, SCM endianness),
  1276. "Return the signed 64-bit integer from @var{bv} at "
  1277. "@var{index}.")
  1278. #define FUNC_NAME s_scm_bytevector_s64_ref
  1279. {
  1280. LARGE_INTEGER_REF (64, signed);
  1281. }
  1282. #undef FUNC_NAME
  1283. SCM_DEFINE (scm_bytevector_u64_native_ref, "bytevector-u64-native-ref",
  1284. 2, 0, 0,
  1285. (SCM bv, SCM index),
  1286. "Return the unsigned 64-bit integer from @var{bv} at "
  1287. "@var{index} using the native endianness.")
  1288. #define FUNC_NAME s_scm_bytevector_u64_native_ref
  1289. {
  1290. LARGE_INTEGER_NATIVE_REF (64, unsigned);
  1291. }
  1292. #undef FUNC_NAME
  1293. SCM_DEFINE (scm_bytevector_s64_native_ref, "bytevector-s64-native-ref",
  1294. 2, 0, 0,
  1295. (SCM bv, SCM index),
  1296. "Return the unsigned 64-bit integer from @var{bv} at "
  1297. "@var{index} using the native endianness.")
  1298. #define FUNC_NAME s_scm_bytevector_s64_native_ref
  1299. {
  1300. LARGE_INTEGER_NATIVE_REF (64, signed);
  1301. }
  1302. #undef FUNC_NAME
  1303. SCM_DEFINE (scm_bytevector_u64_set_x, "bytevector-u64-set!",
  1304. 4, 0, 0,
  1305. (SCM bv, SCM index, SCM value, SCM endianness),
  1306. "Store @var{value} in @var{bv} at @var{index} according to "
  1307. "@var{endianness}.")
  1308. #define FUNC_NAME s_scm_bytevector_u64_set_x
  1309. {
  1310. LARGE_INTEGER_SET (64, unsigned);
  1311. }
  1312. #undef FUNC_NAME
  1313. SCM_DEFINE (scm_bytevector_s64_set_x, "bytevector-s64-set!",
  1314. 4, 0, 0,
  1315. (SCM bv, SCM index, SCM value, SCM endianness),
  1316. "Store @var{value} in @var{bv} at @var{index} according to "
  1317. "@var{endianness}.")
  1318. #define FUNC_NAME s_scm_bytevector_s64_set_x
  1319. {
  1320. LARGE_INTEGER_SET (64, signed);
  1321. }
  1322. #undef FUNC_NAME
  1323. SCM_DEFINE (scm_bytevector_u64_native_set_x, "bytevector-u64-native-set!",
  1324. 3, 0, 0,
  1325. (SCM bv, SCM index, SCM value),
  1326. "Store the unsigned integer @var{value} at index @var{index} "
  1327. "of @var{bv} using the native endianness.")
  1328. #define FUNC_NAME s_scm_bytevector_u64_native_set_x
  1329. {
  1330. LARGE_INTEGER_NATIVE_SET (64, unsigned);
  1331. }
  1332. #undef FUNC_NAME
  1333. SCM_DEFINE (scm_bytevector_s64_native_set_x, "bytevector-s64-native-set!",
  1334. 3, 0, 0,
  1335. (SCM bv, SCM index, SCM value),
  1336. "Store the signed integer @var{value} at index @var{index} "
  1337. "of @var{bv} using the native endianness.")
  1338. #define FUNC_NAME s_scm_bytevector_s64_native_set_x
  1339. {
  1340. LARGE_INTEGER_NATIVE_SET (64, signed);
  1341. }
  1342. #undef FUNC_NAME
  1343. /* Operations on IEEE-754 numbers. */
  1344. /* There are two possible word endians, visible in glibc's <ieee754.h>.
  1345. However, in R6RS, when the endianness is `little', little endian is
  1346. assumed for both the byte order and the word order. This is clear from
  1347. Section 2.1 of R6RS-lib (in response to
  1348. http://www.r6rs.org/formal-comments/comment-187.txt). */
  1349. union scm_ieee754_float
  1350. {
  1351. float f;
  1352. scm_t_uint32 i;
  1353. };
  1354. union scm_ieee754_double
  1355. {
  1356. double d;
  1357. scm_t_uint64 i;
  1358. };
  1359. /* Convert to/from a floating-point number with different endianness. This
  1360. method is probably not the most efficient but it should be portable. */
  1361. static inline void
  1362. float_to_foreign_endianness (union scm_ieee754_float *target,
  1363. float source)
  1364. {
  1365. union scm_ieee754_float input;
  1366. input.f = source;
  1367. target->i = bswap_32 (input.i);
  1368. }
  1369. static inline float
  1370. float_from_foreign_endianness (const union scm_ieee754_float *source)
  1371. {
  1372. union scm_ieee754_float result;
  1373. result.i = bswap_32 (source->i);
  1374. return (result.f);
  1375. }
  1376. static inline void
  1377. double_to_foreign_endianness (union scm_ieee754_double *target,
  1378. double source)
  1379. {
  1380. union scm_ieee754_double input;
  1381. input.d = source;
  1382. target->i = bswap_64 (input.i);
  1383. }
  1384. static inline double
  1385. double_from_foreign_endianness (const union scm_ieee754_double *source)
  1386. {
  1387. union scm_ieee754_double result;
  1388. result.i = bswap_64 (source->i);
  1389. return (result.d);
  1390. }
  1391. /* Template macros to abstract over doubles and floats.
  1392. XXX: Guile can only convert to/from doubles. */
  1393. #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
  1394. #define IEEE754_TO_SCM(_c_type) scm_from_double
  1395. #define IEEE754_FROM_SCM(_c_type) scm_to_double
  1396. #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
  1397. _c_type ## _from_foreign_endianness
  1398. #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
  1399. _c_type ## _to_foreign_endianness
  1400. /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
  1401. #define VALIDATE_REAL(pos, v) \
  1402. do { \
  1403. SCM_ASSERT_TYPE (scm_is_real (v), v, pos, FUNC_NAME, "real"); \
  1404. } while (0)
  1405. /* Templace getters and setters. */
  1406. #define IEEE754_ACCESSOR_PROLOGUE(_type) \
  1407. INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
  1408. #define IEEE754_REF(_type) \
  1409. _type c_result; \
  1410. \
  1411. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1412. SCM_VALIDATE_SYMBOL (3, endianness); \
  1413. \
  1414. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  1415. memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
  1416. else \
  1417. { \
  1418. IEEE754_UNION (_type) c_raw; \
  1419. \
  1420. memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
  1421. c_result = \
  1422. IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
  1423. } \
  1424. \
  1425. return (IEEE754_TO_SCM (_type) (c_result));
  1426. #define IEEE754_NATIVE_REF(_type) \
  1427. _type c_result; \
  1428. \
  1429. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1430. \
  1431. memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
  1432. return (IEEE754_TO_SCM (_type) (c_result));
  1433. #define IEEE754_SET(_type) \
  1434. _type c_value; \
  1435. \
  1436. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1437. VALIDATE_REAL (3, value); \
  1438. SCM_VALIDATE_SYMBOL (4, endianness); \
  1439. c_value = IEEE754_FROM_SCM (_type) (value); \
  1440. \
  1441. if (scm_is_eq (endianness, scm_i_native_endianness)) \
  1442. memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
  1443. else \
  1444. { \
  1445. IEEE754_UNION (_type) c_raw; \
  1446. \
  1447. IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
  1448. memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
  1449. } \
  1450. \
  1451. return SCM_UNSPECIFIED;
  1452. #define IEEE754_NATIVE_SET(_type) \
  1453. _type c_value; \
  1454. \
  1455. IEEE754_ACCESSOR_PROLOGUE (_type); \
  1456. VALIDATE_REAL (3, value); \
  1457. c_value = IEEE754_FROM_SCM (_type) (value); \
  1458. \
  1459. memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
  1460. return SCM_UNSPECIFIED;
  1461. /* Single precision. */
  1462. SCM_DEFINE (scm_bytevector_ieee_single_ref,
  1463. "bytevector-ieee-single-ref",
  1464. 3, 0, 0,
  1465. (SCM bv, SCM index, SCM endianness),
  1466. "Return the IEEE-754 single from @var{bv} at "
  1467. "@var{index}.")
  1468. #define FUNC_NAME s_scm_bytevector_ieee_single_ref
  1469. {
  1470. IEEE754_REF (float);
  1471. }
  1472. #undef FUNC_NAME
  1473. SCM_DEFINE (scm_bytevector_ieee_single_native_ref,
  1474. "bytevector-ieee-single-native-ref",
  1475. 2, 0, 0,
  1476. (SCM bv, SCM index),
  1477. "Return the IEEE-754 single from @var{bv} at "
  1478. "@var{index} using the native endianness.")
  1479. #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
  1480. {
  1481. IEEE754_NATIVE_REF (float);
  1482. }
  1483. #undef FUNC_NAME
  1484. SCM_DEFINE (scm_bytevector_ieee_single_set_x,
  1485. "bytevector-ieee-single-set!",
  1486. 4, 0, 0,
  1487. (SCM bv, SCM index, SCM value, SCM endianness),
  1488. "Store real @var{value} in @var{bv} at @var{index} according to "
  1489. "@var{endianness}.")
  1490. #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
  1491. {
  1492. IEEE754_SET (float);
  1493. }
  1494. #undef FUNC_NAME
  1495. SCM_DEFINE (scm_bytevector_ieee_single_native_set_x,
  1496. "bytevector-ieee-single-native-set!",
  1497. 3, 0, 0,
  1498. (SCM bv, SCM index, SCM value),
  1499. "Store the real @var{value} at index @var{index} "
  1500. "of @var{bv} using the native endianness.")
  1501. #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
  1502. {
  1503. IEEE754_NATIVE_SET (float);
  1504. }
  1505. #undef FUNC_NAME
  1506. /* Double precision. */
  1507. SCM_DEFINE (scm_bytevector_ieee_double_ref,
  1508. "bytevector-ieee-double-ref",
  1509. 3, 0, 0,
  1510. (SCM bv, SCM index, SCM endianness),
  1511. "Return the IEEE-754 double from @var{bv} at "
  1512. "@var{index}.")
  1513. #define FUNC_NAME s_scm_bytevector_ieee_double_ref
  1514. {
  1515. IEEE754_REF (double);
  1516. }
  1517. #undef FUNC_NAME
  1518. SCM_DEFINE (scm_bytevector_ieee_double_native_ref,
  1519. "bytevector-ieee-double-native-ref",
  1520. 2, 0, 0,
  1521. (SCM bv, SCM index),
  1522. "Return the IEEE-754 double from @var{bv} at "
  1523. "@var{index} using the native endianness.")
  1524. #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
  1525. {
  1526. IEEE754_NATIVE_REF (double);
  1527. }
  1528. #undef FUNC_NAME
  1529. SCM_DEFINE (scm_bytevector_ieee_double_set_x,
  1530. "bytevector-ieee-double-set!",
  1531. 4, 0, 0,
  1532. (SCM bv, SCM index, SCM value, SCM endianness),
  1533. "Store real @var{value} in @var{bv} at @var{index} according to "
  1534. "@var{endianness}.")
  1535. #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
  1536. {
  1537. IEEE754_SET (double);
  1538. }
  1539. #undef FUNC_NAME
  1540. SCM_DEFINE (scm_bytevector_ieee_double_native_set_x,
  1541. "bytevector-ieee-double-native-set!",
  1542. 3, 0, 0,
  1543. (SCM bv, SCM index, SCM value),
  1544. "Store the real @var{value} at index @var{index} "
  1545. "of @var{bv} using the native endianness.")
  1546. #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
  1547. {
  1548. IEEE754_NATIVE_SET (double);
  1549. }
  1550. #undef FUNC_NAME
  1551. #undef IEEE754_UNION
  1552. #undef IEEE754_TO_SCM
  1553. #undef IEEE754_FROM_SCM
  1554. #undef IEEE754_FROM_FOREIGN_ENDIANNESS
  1555. #undef IEEE754_TO_FOREIGN_ENDIANNESS
  1556. #undef IEEE754_REF
  1557. #undef IEEE754_NATIVE_REF
  1558. #undef IEEE754_SET
  1559. #undef IEEE754_NATIVE_SET
  1560. /* Operations on strings. */
  1561. /* Produce a function that returns the length of a UTF-encoded string. */
  1562. #define UTF_STRLEN_FUNCTION(_utf_width) \
  1563. static inline size_t \
  1564. utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
  1565. { \
  1566. size_t len = 0; \
  1567. const uint ## _utf_width ## _t *ptr; \
  1568. for (ptr = str; \
  1569. *ptr != 0; \
  1570. ptr++) \
  1571. { \
  1572. len++; \
  1573. } \
  1574. \
  1575. return (len * ((_utf_width) / 8)); \
  1576. }
  1577. UTF_STRLEN_FUNCTION (8)
  1578. /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
  1579. #define UTF_STRLEN(_utf_width, _str) \
  1580. utf ## _utf_width ## _strlen (_str)
  1581. /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
  1582. ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
  1583. encoding name). */
  1584. static inline void
  1585. utf_encoding_name (char *name, size_t utf_width, SCM endianness)
  1586. {
  1587. strcpy (name, "UTF-");
  1588. strcat (name, ((utf_width == 8)
  1589. ? "8"
  1590. : ((utf_width == 16)
  1591. ? "16"
  1592. : ((utf_width == 32)
  1593. ? "32"
  1594. : "??"))));
  1595. strcat (name,
  1596. ((scm_is_eq (endianness, scm_sym_big))
  1597. ? "BE"
  1598. : ((scm_is_eq (endianness, scm_sym_little))
  1599. ? "LE"
  1600. : "unknown")));
  1601. }
  1602. /* Maximum length of a UTF encoding name. */
  1603. #define MAX_UTF_ENCODING_NAME_LEN 16
  1604. /* Produce the body of a `string->utf' function. */
  1605. #define STRING_TO_UTF(_utf_width) \
  1606. SCM utf; \
  1607. int err; \
  1608. char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
  1609. char *c_utf = NULL; \
  1610. size_t c_strlen, c_utf_len = 0; \
  1611. \
  1612. SCM_VALIDATE_STRING (1, str); \
  1613. if (scm_is_eq (endianness, SCM_UNDEFINED)) \
  1614. endianness = scm_sym_big; \
  1615. else \
  1616. SCM_VALIDATE_SYMBOL (2, endianness); \
  1617. \
  1618. utf_encoding_name (c_utf_name, (_utf_width), endianness); \
  1619. \
  1620. c_strlen = scm_i_string_length (str); \
  1621. if (scm_i_is_narrow_string (str)) \
  1622. { \
  1623. err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
  1624. "ISO-8859-1", c_utf_name, \
  1625. iconveh_question_mark, NULL, \
  1626. &c_utf, &c_utf_len); \
  1627. if (SCM_UNLIKELY (err)) \
  1628. scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
  1629. scm_list_1 (str), err); \
  1630. } \
  1631. else \
  1632. { \
  1633. const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
  1634. c_utf = u32_conv_to_encoding (c_utf_name, \
  1635. iconveh_question_mark, \
  1636. (scm_t_uint32 *) wbuf, \
  1637. c_strlen, NULL, NULL, &c_utf_len); \
  1638. if (SCM_UNLIKELY (c_utf == NULL)) \
  1639. scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
  1640. scm_list_1 (str), errno); \
  1641. } \
  1642. scm_dynwind_begin (0); \
  1643. scm_dynwind_free (c_utf); \
  1644. utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
  1645. memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
  1646. scm_dynwind_end (); \
  1647. \
  1648. return (utf);
  1649. SCM_DEFINE (scm_string_to_utf8, "string->utf8",
  1650. 1, 0, 0,
  1651. (SCM str),
  1652. "Return a newly allocated bytevector that contains the UTF-8 "
  1653. "encoding of @var{str}.")
  1654. #define FUNC_NAME s_scm_string_to_utf8
  1655. {
  1656. SCM utf;
  1657. scm_t_uint8 *c_utf;
  1658. size_t c_utf_len = 0;
  1659. SCM_VALIDATE_STRING (1, str);
  1660. c_utf = (scm_t_uint8 *) scm_to_utf8_stringn (str, &c_utf_len);
  1661. utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1662. memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len);
  1663. free (c_utf);
  1664. return (utf);
  1665. }
  1666. #undef FUNC_NAME
  1667. SCM_DEFINE (scm_string_to_utf16, "string->utf16",
  1668. 1, 1, 0,
  1669. (SCM str, SCM endianness),
  1670. "Return a newly allocated bytevector that contains the UTF-16 "
  1671. "encoding of @var{str}.")
  1672. #define FUNC_NAME s_scm_string_to_utf16
  1673. {
  1674. STRING_TO_UTF (16);
  1675. }
  1676. #undef FUNC_NAME
  1677. static void
  1678. swap_u32 (scm_t_wchar *vals, size_t len)
  1679. {
  1680. size_t n;
  1681. for (n = 0; n < len; n++)
  1682. vals[n] = bswap_32 (vals[n]);
  1683. }
  1684. SCM_DEFINE (scm_string_to_utf32, "string->utf32",
  1685. 1, 1, 0,
  1686. (SCM str, SCM endianness),
  1687. "Return a newly allocated bytevector that contains the UTF-32 "
  1688. "encoding of @var{str}.")
  1689. #define FUNC_NAME s_scm_string_to_utf32
  1690. {
  1691. SCM bv;
  1692. scm_t_wchar *wchars;
  1693. size_t wchar_len, bytes_len;
  1694. wchars = scm_to_utf32_stringn (str, &wchar_len);
  1695. bytes_len = wchar_len * sizeof (scm_t_wchar);
  1696. if (!scm_is_eq (SCM_UNBNDP (endianness) ? scm_endianness_big : endianness,
  1697. scm_i_native_endianness))
  1698. swap_u32 (wchars, wchar_len);
  1699. bv = make_bytevector (bytes_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1700. memcpy (SCM_BYTEVECTOR_CONTENTS (bv), wchars, bytes_len);
  1701. free (wchars);
  1702. return bv;
  1703. }
  1704. #undef FUNC_NAME
  1705. /* Produce the body of a function that converts a UTF-encoded bytevector to a
  1706. string. */
  1707. #define UTF_TO_STRING(_utf_width) \
  1708. SCM str = SCM_BOOL_F; \
  1709. int err; \
  1710. char *c_str = NULL; \
  1711. char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
  1712. char *c_utf; \
  1713. size_t c_strlen = 0, c_utf_len = 0; \
  1714. \
  1715. SCM_VALIDATE_BYTEVECTOR (1, utf); \
  1716. if (scm_is_eq (endianness, SCM_UNDEFINED)) \
  1717. endianness = scm_sym_big; \
  1718. else \
  1719. SCM_VALIDATE_SYMBOL (2, endianness); \
  1720. \
  1721. c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
  1722. c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
  1723. utf_encoding_name (c_utf_name, (_utf_width), endianness); \
  1724. \
  1725. err = mem_iconveh (c_utf, c_utf_len, \
  1726. c_utf_name, "UTF-8", \
  1727. iconveh_question_mark, NULL, \
  1728. &c_str, &c_strlen); \
  1729. if (SCM_UNLIKELY (err)) \
  1730. scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
  1731. scm_list_1 (utf), err); \
  1732. else \
  1733. { \
  1734. str = scm_from_utf8_stringn (c_str, c_strlen); \
  1735. free (c_str); \
  1736. } \
  1737. return (str);
  1738. SCM_DEFINE (scm_utf8_to_string, "utf8->string",
  1739. 1, 0, 0,
  1740. (SCM utf),
  1741. "Return a newly allocate string that contains from the UTF-8-"
  1742. "encoded contents of bytevector @var{utf}.")
  1743. #define FUNC_NAME s_scm_utf8_to_string
  1744. {
  1745. SCM str;
  1746. const char *c_utf;
  1747. size_t c_utf_len = 0;
  1748. SCM_VALIDATE_BYTEVECTOR (1, utf);
  1749. c_utf_len = SCM_BYTEVECTOR_LENGTH (utf);
  1750. c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf);
  1751. str = scm_from_utf8_stringn (c_utf, c_utf_len);
  1752. return (str);
  1753. }
  1754. #undef FUNC_NAME
  1755. SCM_DEFINE (scm_utf16_to_string, "utf16->string",
  1756. 1, 1, 0,
  1757. (SCM utf, SCM endianness),
  1758. "Return a newly allocate string that contains from the UTF-16-"
  1759. "encoded contents of bytevector @var{utf}.")
  1760. #define FUNC_NAME s_scm_utf16_to_string
  1761. {
  1762. UTF_TO_STRING (16);
  1763. }
  1764. #undef FUNC_NAME
  1765. SCM_DEFINE (scm_utf32_to_string, "utf32->string",
  1766. 1, 1, 0,
  1767. (SCM utf, SCM endianness),
  1768. "Return a newly allocate string that contains from the UTF-32-"
  1769. "encoded contents of bytevector @var{utf}.")
  1770. #define FUNC_NAME s_scm_utf32_to_string
  1771. {
  1772. UTF_TO_STRING (32);
  1773. }
  1774. #undef FUNC_NAME
  1775. /* Initialization. */
  1776. void
  1777. scm_bootstrap_bytevectors (void)
  1778. {
  1779. /* This must be instantiated here because the generalized-vector API may
  1780. want to access bytevectors even though `(rnrs bytevectors)' hasn't been
  1781. loaded. */
  1782. scm_null_bytevector = make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8);
  1783. #ifdef WORDS_BIGENDIAN
  1784. scm_i_native_endianness = scm_from_latin1_symbol ("big");
  1785. #else
  1786. scm_i_native_endianness = scm_from_latin1_symbol ("little");
  1787. #endif
  1788. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1789. "scm_init_bytevectors",
  1790. (scm_t_extension_init_func) scm_init_bytevectors,
  1791. NULL);
  1792. scm_i_register_vector_constructor
  1793. (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_VU8],
  1794. scm_make_bytevector);
  1795. }
  1796. void
  1797. scm_init_bytevectors (void)
  1798. {
  1799. #include "libguile/bytevectors.x"
  1800. scm_endianness_big = scm_sym_big;
  1801. scm_endianness_little = scm_sym_little;
  1802. }