bytevectors.c 64 KB

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