-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbytevector.tex
655 lines (521 loc) · 25.2 KB
/
bytevector.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
\chapter{Bytevectors}
\label{bytevectorschapter}
Many applications deal with blocks of binary data by accessing
them in various ways---extracting signed or unsigned numbers of
various sizes. Therefore, the \defrsixlibrary{bytevectors} library
provides a single type for
blocks of binary data with multiple ways to access that data. It deals
with integers and floating-point representations
in various sizes with specified endianness.
Bytevectors\mainindex{bytevector} are objects of a disjoint
type. Conceptually, a bytevector represents a sequence of 8-bit
bytes. The description of bytevectors uses the term \defining{byte}
for an exact integer object in the interval $\{-128, \ldots, 127\}$ and the
term \defining{octet} for an exact integer object in the interval $\{0,
\ldots, 255\}$. A byte corresponds to its two's complement
representation as an octet.
The length of a bytevector is the number of bytes it contains. This
number is fixed. A valid index into a bytevector is an exact,
non-negative integer object less than the length of the bytevector.
The first byte of a bytevector has index 0;
the last byte has an index one less than the length of the bytevector.
Generally, the access procedures come in different flavors according
to the size of the represented integer and the endianness of the
representation. The procedures also distinguish signed and unsigned
representations.
The signed representations all use two's complement.
Like string literals, literals representing bytevectors do not need to
be quoted:
%
\begin{scheme}
\#vu8(12 23 123) \ev \#vu8(12 23 123)%
\end{scheme}
\section{Endianness}
Many operations described in this chapter accept an
\defining{endianness} argument. Endianness describes the encoding of
exact integer objects as several contiguous bytes in a bytevector~\cite{IEN137}.
For this purpose, the binary representation of the integer object is split into
consecutive bytes. \mainindex{little-endian}The little-endian
encoding places the least significant byte of an integer first, with
the other bytes following in increasing order of significance.
\mainindex{big-endian}The big-endian encoding places the most
significant byte of an integer first, with the other bytes following
in decreasing order of significance.
This terminology also applies to IEEE-754 numbers: IEEE~754 describes
how to represent a floating-point number as an exact integer object, and
endianness describes how the bytes of such an integer are laid out in
a bytevector.
\begin{note}
Little- and big-endianness are only the most common kinds of
endianness. Some architectures distinguish between the endianness
at different levels of a binary representation.
\end{note}
\section{General operations}
\begin{entry}{%
\proto{endianness}{ \hyper{endianness symbol}}{\exprtype}}
\domain{The name of \hyper{endianness symbol} must be a symbol describing an
endianness. An implementation must support at least the symbols
{\cf big} and {\cf little}, but may support other endianness
symbols.} {\cf (endianness \hyper{endianness symbol})} evaluates to
the symbol named \hyper{endianness symbol}. Whenever one of the
procedures operating on bytevectors accepts an endianness as an
argument, that argument must be one of these symbols. It is a syntax
violation for \hyper{endianness symbol} to be anything other than an
endianness symbol supported by the implementation.
\begin{note}
Implementors should use widely accepted designations
for endianness symbols other than {\cf big} and {\cf little}.
\end{note}
\begin{note}
Only the name of \hyper{endianness symbol} is significant.
\end{note}
\end{entry}
\begin{entry}{%
\proto{native-endianness}{}{procedure}}
Returns the endianness symbol associated implementation's preferred
endianness (usually that of the underlying machine architecture).
This may be any \hyper{endianness symbol}, including a symbol other
than {\cf big} and {\cf little}.
\end{entry}
\begin{entry}{%
\proto{bytevector?}{ obj}{procedure}}
Returns \schtrue{} if \var{obj} is a bytevector,
otherwise returns \schfalse{}.
\end{entry}
\begin{entry}{%
\proto{make-bytevector}{ k}{procedure}
\rproto{make-bytevector}{ k fill}{procedure}}
Returns a newly allocated bytevector of \var{k} bytes.
If the \var{fill} argument is missing, the initial contents of the
returned bytevector are unspecified.
If the \var{fill} argument is present, it must be an exact integer
object in
the interval $\{-128, \ldots 255\}$ that specifies the initial value
for the bytes of the bytevector: If \var{fill} is positive, it is
interpreted as an octet; if it is negative, it is interpreted as a byte.
\end{entry}
\begin{entry}{%
\proto{bytevector-length}{ bytevector}{procedure}}
Returns, as an exact integer object, the number of bytes in \var{bytevector}.
\end{entry}
\begin{entry}{%
\proto{bytevector=?}{ \vari{bytevector} \varii{bytevector}}{procedure}}
Returns \schtrue{} if \vari{bytevector} and \varii{bytevector} are equal---that
is, if they have the same length and equal bytes at all valid indices.
It returns \schfalse{} otherwise.
\end{entry}
\begin{entry}{%
\proto{bytevector-fill!}{ bytevector fill}}
\domain{The \var{fill} argument is as in the description of the {\cf
make-bytevector} procedure.}
The {\cf bytevector-fill!} procedure stores \var{fill} in every element of \var{bytevector}
and returns \unspecifiedreturn. Analogous to {\cf vector-fill!}.
\end{entry}
\begin{entry}{%
\pproto{(bytevector-copy! \var{source} \var{source-start}}{procedure}}
\mainschindex{bytevector-copy!}{\tt\obeyspaces\\
\var{target} \var{target-start} \var{k})}
\domain{\var{Source} and \var{target} must be bytevectors.
\var{Source-start}, \var{target-start},
and \var{k} must be non-negative exact integer objects that satisfy
\begin{displaymath}
\begin{array}{rcccccl}
0 & \leq & \var{source-start} & \leq & \var{source-start} + \var{k} & \leq & l_{\var{source}}
\\
0 & \leq & \var{target-start} & \leq & \var{target-start} + \var{k} & \leq & l_{\var{target}}
\end{array}
\end{displaymath}
%
where $l_{\var{source}}$ is the length of \var{source} and
$l_{\var{target}}$ is the length of \var{target}.}
The {\cf bytevector-copy!} procedure copies the bytes from \var{source} at indices
\begin{displaymath}
\var{source-start}, \ldots \var{source-start} + \var{k} - 1
\end{displaymath}
to consecutive indices in \var{target} starting at \var{target-index}.
This must work even if the memory regions for the source and the target
overlap, i.e., the bytes at the target location after the copy must be
equal to the bytes at the source location before the copy.
This returns \unspecifiedreturn.
\begin{scheme}
(let ((b (u8-list->bytevector '(1 2 3 4 5 6 7 8))))
(bytevector-copy! b 0 b 3 4)
(bytevector->u8-list b)) \ev (1 2 3 1 2 3 4 8)%
\end{scheme}
\end{entry}
\begin{entry}{%
\proto{bytevector-copy}{ bytevector}{procedure}}
Returns a newly allocated copy of \var{bytevector}.
\end{entry}
\section{Operations on bytes and octets}
\begin{entry}{%
\proto{bytevector-u8-ref}{ bytevector k}{procedure}
\proto{bytevector-s8-ref}{ bytevector k}{procedure}}
\domain{\var{K} must be a valid index of \var{bytevector}.}
The {\cf bytevector-u8-ref} procedure returns the byte at index \var{k} of \var{bytevector},
as an octet.
The {\cf bytevector-s8-ref} procedure returns the byte at index \var{k} of \var{bytevector},
as a (signed) byte.
\begin{scheme}
(let ((b1 (make-bytevector 16 -127))
(b2 (make-bytevector 16 255)))
(list
(bytevector-s8-ref b1 0)
(bytevector-u8-ref b1 0)
(bytevector-s8-ref b2 0)
(bytevector-u8-ref b2 0))) \lev (-127 129 -1 255)%
\end{scheme}
\end{entry}
\begin{entry}{%
\proto{bytevector-u8-set!}{ bytevector k octet}{procedure}
\proto{bytevector-s8-set!}{ bytevector k byte}{procedure}}
\domain{\var{K} must be a valid index of \var{bytevector}.}
The {\cf bytevector-u8-set!} procedure stores \var{octet} in element \var{k} of
\var{bytevector}.
The {\cf bytevector-s8-set!} procedure stores the two's-complement representation of
\var{byte} in element \var{k} of \var{bytevector}.
Both procedures return \unspecifiedreturn.
\begin{scheme}
(let ((b (make-bytevector 16 -127)))
(bytevector-s8-set! b 0 -126)
(bytevector-u8-set! b 1 246)
(list
(bytevector-s8-ref b 0)
(bytevector-u8-ref b 0)
(bytevector-s8-ref b 1)
(bytevector-u8-ref b 1))) \lev (-126 130 -10 246)%
\end{scheme}
\end{entry}
\begin{entry}{%
\proto{bytevector->u8-list}{ bytevector}{procedure}
\proto{u8-list->bytevector}{ list}{procedure}}
\domain{\var{List} must be a list of octets.}
The {\cf bytevector->u8-list} procedure returns a newly allocated list of the octets of
\var{bytevector} in the same order.
The {\cf u8-list->bytevector} procedure returns a newly allocated bytevector whose
elements are the elements of list \var{list}, in
the same order. It is analogous to {\cf list->vector}.
\end{entry}
\section{Operations on integers of arbitrary size}
\begin{entry}{%
\proto{bytevector-uint-ref}{ bytevector k endianness size}{procedure}
\proto{bytevector-sint-ref}{ bytevector k endianness size}{procedure}
\proto{bytevector-uint-set!}{ bytevector k n endianness size}{procedure}
\proto{bytevector-sint-set!}{ bytevector k n endianness size}{procedure}}
\domain{\var{Size} must be a positive exact integer object. $\var{K}, \ldots,
\var{k} + \var{size} - 1$ must be valid indices of \var{bytevector}.}
The {\cf bytevector-uint-ref} procedure retrieves the exact integer object corresponding to the
unsigned representation of size \var{size} and specified by \var{endianness}
at indices $\var{k}, \ldots, \var{k} + \var{size} - 1$.
The {\cf bytevector-sint-ref} procedure retrieves the exact integer object corresponding to the two's-complement representation of size \var{size} and specified by \var{endianness} at
indices $\var{k}, \ldots, \var{k} + \var{size} - 1$.
\domain{For {\cf bytevector-uint-set!}, \var{n} must be an exact
integer object in the interval $\{0, \ldots, 256^{\mathit{size}}-1\}$.}
The {\cf bytevector-uint-set!} procedure stores the unsigned representation of size \var{size}
and specified by \var{endianness} into \var{bytevector} at indices
$\var{k}, \ldots, \var{k} + \var{size} - 1$.
\domain{For {\cf bytevector-sint-set!}, \var{n} must be an exact
integer object in
the interval $\{-256^{\mathit{size}}/2, \ldots,
256^{\mathit{size}}/2-1\}$.}
{\cf bytevector-sint-set!} stores the two's-complement
representation of size \var{size} and specified by \var{endianness}
into \var{bytevector} at indices $\var{k}, \ldots, \var{k} + \var{size} - 1$.
The \ldots{\cf -set!} procedures return \unspecifiedreturn.
\begin{scheme}
(define b (make-bytevector 16 -127))
(bytevector-uint-set! b 0 (- (expt 2 128) 3)
(endianness little) 16)
(bytevector-uint-ref b 0 (endianness little) 16)\lev
\#xfffffffffffffffffffffffffffffffd
(bytevector-sint-ref b 0 (endianness little) 16)\lev -3
(bytevector->u8-list b)\lev (253 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255)
(bytevector-uint-set! b 0 (- (expt 2 128) 3)
(endianness big) 16)
(bytevector-uint-ref b 0 (endianness big) 16) \lev
\#xfffffffffffffffffffffffffffffffd
(bytevector-sint-ref b 0 (endianness big) 16) \lev -3
(bytevector->u8-list b) \lev (255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 253))%
\end{scheme}
\end{entry}
\begin{entry}{%
\proto{bytevector->uint-list}{ bytevector endianness size}{procedure}
\proto{bytevector->sint-list}{ bytevector endianness size}{procedure}
\proto{uint-list->bytevector}{ list endianness size}{procedure}
\proto{sint-list->bytevector}{ list endianness size}{procedure}}
\domain{\var{Size} must be a positive exact integer object. For {\cf
uint-list->bytevector}, \var{list} must be a list of exact
integer objects in the interval $\{0, \ldots, 256^{\mathit{size}}-1\}$. For
{\cf sint-list->bytevector}, \var{list} must be a list of exact
integer objects in the interval $\{-256^{\mathit{size}}/2, \ldots,
256^{\mathit{size}}/2-1\}$. The length of \var{bytevector} or,
respectively, of \var{list} must be divisible by \var{size}.}
These procedures convert between lists of integer objects and their consecutive
representations according to \var{size} and \var{endianness} in the
\var{bytevector} objects in the same way as {\cf bytevector->u8-list} and {\cf
u8-list->bytevector} do for one-byte representations.
\begin{scheme}
(let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
(bytevector->sint-list b (endianness little) 2)) \lev (513 -253 513 513)
(let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
(bytevector->uint-list b (endianness little) 2)) \lev (513 65283 513 513)%
\end{scheme}
\end{entry}
\section{Operations on 16-bit integers}
\begin{entry}{%
\proto{bytevector-u16-ref}{ bytevector k endianness}{procedure}
\proto{bytevector-s16-ref}{ bytevector k endianness}{procedure}
\proto{bytevector-u16-native-ref}{ bytevector k}{procedure}
\proto{bytevector-s16-native-ref}{ bytevector k}{procedure}
\proto{bytevector-u16-set!}{ bytevector k n endianness}{procedure}
\proto{bytevector-s16-set!}{ bytevector k n endianness}{procedure}
\proto{bytevector-u16-native-set!}{ bytevector k n}{procedure}
\proto{bytevector-s16-native-set!}{ bytevector k n}{procedure}}
\domain{\var{K} must be a valid index of \var{bytevector}; so must
$\var{k} + 1$. For {\cf bytevector-u16-set!} and {\cf
bytevector-\hp{}u16-native-\hp{}set!}, \var{n} must be an exact integer object in
the interval $\{0, \ldots, 2^{16}-1\}$. For {\cf bytevector-s16-set!}
and {\cf bytevector-s16-native-set!}, \var{n} must be an exact
integer object in the interval $\{-2^{15}, \ldots, 2^{15}-1\}$.}
These retrieve and set two-byte representations of numbers at indices
\var{k} and $\var{k}+1$, according to the endianness specified by
\var{endianness}. The procedures with {\cf u16} in their names deal with the
unsigned representation; those with {\cf s16} in their names deal
with the two's-complement representation.
The procedures with {\cf native} in their names employ the native
endianness, and work only at aligned indices:
\var{k} must be a multiple of 2.
The \ldots{\cf -set!} procedures return \unspecifiedreturn.
\begin{scheme}
(define b
(u8-list->bytevector
'(255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 253)))
(bytevector-u16-ref b 14 (endianness little)) \lev 65023
(bytevector-s16-ref b 14 (endianness little)) \lev -513
(bytevector-u16-ref b 14 (endianness big)) \lev 65533
(bytevector-s16-ref b 14 (endianness big)) \lev -3
(bytevector-u16-set! b 0 12345 (endianness little))
(bytevector-u16-ref b 0 (endianness little)) \lev 12345
(bytevector-u16-native-set! b 0 12345)
(bytevector-u16-native-ref b 0) \ev 12345
(bytevector-u16-ref b 0 (endianness little)) \lev \unspecified%
\end{scheme}
\end{entry}
\section{Operations on 32-bit integers}
\begin{entry}{%
\proto{bytevector-u32-ref}{ bytevector k endianness}{procedure}
\proto{bytevector-s32-ref}{ bytevector k endianness}{procedure}
\proto{bytevector-u32-native-ref}{ bytevector k}{procedure}
\proto{bytevector-s32-native-ref}{ bytevector k}{procedure}
\proto{bytevector-u32-set!}{ bytevector k n endianness}{procedure}
\proto{bytevector-s32-set!}{ bytevector k n endianness}{procedure}
\proto{bytevector-u32-native-set!}{ bytevector k n}{procedure}
\proto{bytevector-s32-native-set!}{ bytevector k n}{procedure}}
\domain{$\var{K}, \ldots, \var{k}+ 3$ must be valid indices of
\var{bytevector}.
For {\cf bytevector-u32-set!} and {\cf
bytevector-\hp{}u32-\hp{}native-\hp{}set!}, \var{n} must be an exact integer
object in
the interval $\{0, \ldots, 2^{32}-1\}$. For {\cf bytevector-s32-set!}
and {\cf bytevector-s32-native-set!}, \var{n} must be an exact
integer object in the interval $\{-2^{31}, \ldots, 2^{32}-1\}$.}
These retrieve and set four-byte representations of numbers at indices $\var{k},
\ldots, \var{k}+ 3$, according to the endianness specified by \var{endianness}. The
procedures with {\cf u32} in their names deal with the unsigned representation;
those with {\cf s32} with the two's-complement representation.
The procedures with {\cf native} in their names employ the native endianness, and
work only at aligned indices: \var{k} must be a multiple of 4.
The \ldots{\cf{}-set!} procedures return \unspecifiedreturn.
\begin{scheme}
(define b
(u8-list->bytevector
'(255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 253)))
(bytevector-u32-ref b 12 (endianness little)) \lev 4261412863
(bytevector-s32-ref b 12 (endianness little)) \lev -33554433
(bytevector-u32-ref b 12 (endianness big)) \lev 4294967293
(bytevector-s32-ref b 12 (endianness big)) \lev -3%
\end{scheme}
\end{entry}
\section{Operations on 64-bit integers}
\begin{entry}{%
\proto{bytevector-u64-ref}{ bytevector k endianness}{procedure}
\proto{bytevector-s64-ref}{ bytevector k endianness}{procedure}
\proto{bytevector-u64-native-ref}{ bytevector k}{procedure}
\proto{bytevector-s64-native-ref}{ bytevector k}{procedure}
\proto{bytevector-u64-set!}{ bytevector k n endianness}{procedure}
\proto{bytevector-s64-set!}{ bytevector k n endianness}{procedure}
\proto{bytevector-u64-native-set!}{ bytevector k n}{procedure}
\proto{bytevector-s64-native-set!}{ bytevector k n}{procedure}}
\domain{$\var{K}, \ldots, \var{k}+ 7$ must be valid indices of
\var{bytevector}.
For {\cf bytevector-u64-set!} and {\cf
bytevector-\hp{}u64-\hp{}native-\hp{}set!}, \var{n} must be an exact integer
object in
the interval $\{0, \ldots, 2^{64}-1\}$. For {\cf bytevector-s64-set!}
and {\cf bytevector-s64-native-set!}, \var{n} must be an exact
integer object in the interval $\{-2^{63}, \ldots, 2^{64}-1\}$.}
These retrieve and set eight-byte representations of numbers at
indices $\var{k}, \ldots, \var{k}+ 7$, according to the endianness
specified by \var{endianness}. The procedures with {\cf u64} in their names deal
with the unsigned representation; those with {\cf s64} with the
two's-complement representation.
The procedures with {\cf native} in their names employ the native endianness, and
work only at aligned indices: \var{k} must be a multiple of 8.
The \ldots{\cf{}-set!} procedures return \unspecifiedreturn.
\begin{scheme}
(define b
(u8-list->bytevector
'(255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 253)))
(bytevector-u64-ref b 8 (endianness little)) \lev 18302628885633695743
(bytevector-s64-ref b 8 (endianness little)) \lev -144115188075855873
(bytevector-u64-ref b 8 (endianness big)) \lev 18446744073709551613
(bytevector-s64-ref b 8 (endianness big)) \lev -3%
\end{scheme}
\end{entry}
\section{Operations on IEEE-754 representations}
\begin{entry}{%
\proto{bytevector-ieee-single-native-ref}{ bytevector k}{procedure}
\proto{bytevector-ieee-single-ref}{ bytevector k endianness}{procedure}}
\domain{$\var{K}, \ldots, \var{k}+3$ must be valid indices of
\var{bytevector}. For {\cf bytevector-ieee-single-native-ref}, \var{k} must
be a multiple of $4$.}
These procedures return the inexact real number object that best
represents the IEEE-754 single-precision number represented by the
four bytes beginning at index
\var{k}.
\end{entry}
\begin{entry}{%
\proto{bytevector-ieee-double-native-ref}{ bytevector k}{procedure}
\proto{bytevector-ieee-double-ref}{ bytevector k endianness}{procedure}}
\domain{$\var{K}, \ldots, \var{k}+7$ must be valid indices of
\var{bytevector}. For {\cf bytevector-ieee-double-native-ref}, \var{k} must
be a multiple of $8$.}
These procedures return the inexact real number object that best
represents the IEEE-754 double-precision number represented by the
eight bytes beginning at index \var{k}.
\end{entry}
\begin{entry}{%
\proto{bytevector-ieee-single-native-set!}{ bytevector k x}{procedure}
\pproto{(bytevector-ieee-single-set! \var{bytevector}}{procedure}}
{\tt\obeyspaces\\
\var{k} \var{x} \var{endianness})}
\domain{$\var{K}, \ldots, \var{k}+3$ must be valid indices of
\var{bytevector}. For {\cf bytevector-ieee-single-native-set!}, \var{k} must
be a multiple of $4$.}
These procedures store an IEEE-754 single-precision representation of \var{x} into
elements \var{k} through $\var{k}+3$ of \var{bytevector}, and return
\unspecifiedreturn.
\end{entry}
\begin{entry}{%
\proto{bytevector-ieee-double-native-set!}{ bytevector k x}{procedure}
\pproto{(bytevector-ieee-double-set! \var{bytevector}}{procedure}}
{\tt\obeyspaces\\
\var{k} \var{x} \var{endianness})}
\domain{$\var{K}, \ldots, \var{k}+7$ must be valid indices of
\var{bytevector}. For {\cf bytevector-ieee-double-native-set!}, \var{k} must
be a multiple of $8$.}
These procedures store an IEEE-754 double-precision representation of \var{x} into
elements \var{k} through $\var{k}+7$ of \var{bytevector}, and return
\unspecifiedreturn.
\end{entry}
\section{Operations on strings}
This section describes procedures that convert between strings and
bytevectors containing Unicode encodings of those strings. When
decoding bytevectors, encoding errors are handled as with the {\cf
replace} semantics of textual I/O (see
section~\ref{transcoderssection}): If an invalid or incomplete
character encoding is encountered, then the replacement character
U+FFFD is appended to the string being generated, an appropriate
number of bytes are ignored, and decoding continues with the following
bytes.
\begin{entry}{%
\proto{string->utf8}{ string}{procedure}}
Returns a newly allocated (unless empty) bytevector that
contains the UTF-8 encoding of the given string.
\end{entry}
\begin{entry}{%
\proto{string->utf16}{ string}{procedure}
\rproto{string->utf16}{ string endianness}{procedure}}
\domain{If \var{endianness} is specified, it must be the symbol {\cf
big} or the symbol {\cf little}.} The {\cf string->utf16}
procedure returns a newly allocated (unless empty) bytevector that
contains the UTF-16BE or UTF-16LE encoding of the given string (with
no byte-order mark). If endianness is not specified or is {\cf big},
then UTF-16BE is used. If endianness is {\cf little}, then UTF-16LE
is used.
\end{entry}
\begin{entry}{%
\proto{string->utf32}{ string}{procedure}
\rproto{string->utf32}{ string endianness}{procedure}}
\domain{If \var{endianness} is specified, it must be the symbol {\cf
big} or the symbol {\cf little}.} The {\cf string->utf32}
procedure returns
a newly allocated (unless empty) bytevector that contains the UTF-32BE
or UTF-32LE encoding of the given string (with no byte mark). If
endianness is not specified or is {\cf big}, then UTF-32BE is used.
If endianness is {\cf little}, then UTF-32LE is used.
\end{entry}
\begin{entry}{%
\proto{utf8->string}{ bytevector}{procedure}}
Returns a newly allocated (unless empty) string whose character
sequence is encoded by the given bytevector.
\end{entry}
\begin{entry}{%
\proto{utf16->string}{ bytevector endianness}{procedure}
\pproto{(utf16->string \var{bytevector}}{procedure}}
{\tt\obeyspaces\\
\var{endianness} \var{endianness-mandatory})}
\domain{\var{Endianness} must be the symbol {\cf big} or
the symbol {\cf little}.} The {\cf utf16->string} procedure returns
a newly allocated (unless empty) string whose character sequence is
encoded by the given bytevector. \var{Bytevector} is decoded
according to UTF-16BE or UTF-16LE: If \var{endianness-mandatory?} is
absent or \schfalse, {\cf utf16->string} determines the endianness
according to a UTF-16 BOM at the beginning of \var{bytevector} if a
BOM is present; in this case, the BOM is not decoded as a character.
Also in this case, if no UTF-16 BOM is present, \var{endianness}
specifies the endianness of the encoding. If
\var{endianness-mandatory?} is a true value, \var{endianness}
specifies the endianness of the encoding, and any UTF-16 BOM in the
encoding is decoded as a regular character.
\begin{note}
A UTF-16 BOM is either a sequence of bytes \sharpsign{}xFE,
\sharpsign{}xFF specifying {\cf big} and UTF-16BE, or \sharpsign{}xFF,
\sharpsign{}xFE specifying {\cf little} and UTF-16LE.
\end{note}
\end{entry}
\begin{entry}{%
\proto{utf32->string}{ bytevector endianness}{procedure}
\pproto{(utf32->string \var{bytevector}}{procedure}}
{\tt\obeyspaces\\
\var{endianness} \var{endianness-mandatory})}
\domain{\var{Endianness} must be the symbol {\cf big} or
the symbol {\cf little}.} The {\cf utf32->string} procedure returns
a newly allocated (unless empty) string whose character sequence is
encoded by the given bytevector. \var{Bytevector} is decoded
according to UTF-32BE or UTF-32LE: If \var{endianness-mandatory?} is
absent or \schfalse, {\cf utf32->string} determines the endianness
according to a UTF-32 BOM at the beginning of \var{bytevector} if a
BOM is present; in this case, the BOM is not decoded as a character.
Also in this case, if no UTF-32 BOM is present, \var{endianness}
specifies the endianness of the encoding. If
\var{endianness-mandatory?} is a true value, \var{endianness}
specifies the endianness of the encoding, and any UTF-32 BOM in the
encoding is decoded as a regular character.
\begin{note}
A UTF-32 BOM is either a sequence of bytes \sharpsign{}x00,
\sharpsign{}x00, \sharpsign{}xFE, \sharpsign{}xFF specifying {\cf
big} and UTF-32BE, or \sharpsign{}xFF, \sharpsign{}xFE,
\sharpsign{}x00, \sharpsign{}x00, specifying {\cf little} and
UTF-32LE.
\end{note}
\end{entry}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "r6rs-lib"
%%% End: