bytevectors.c 63 KB

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