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