bytevectors.c 64 KB

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