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