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