SRFI-66: Octet Vectors

Abstract

This SRFI defines a set of procedures for creating, accessing, and manipulating uniform vectors of octets.

For more information see: SRFI-66: Octet Vectors

Rationale

A number of applications deal with sequences of octets (often called bytes), most prominently interfaces to C and I/O. Vectors are typically too space-costly and too slow to work well in these circumstance. This justifies having a separate type for octet vectors.

This SRFI is related to SRFI 4 (Homogeneous numeric vector datatypes), which also provides vectors of octets, and uses names consistent with it. However, the extension described here does not require any extensions to the syntax of the underlying Scheme system, and provides a slightly richer set of primtives.

Scheme systems implementing both SRFI 4 and this SRFI should use the same type for u8vector and octet vectors as specified here.

Specification

Octet vectors are objects of a new type. This type may or may not be disjoint from the type of regular vectors. Its elements must be octets, that is, exact integers in the range [0,255].

As with vectors, the length of a octet vector is the number of elements it contains. This number is fixed. A valid index into a octet vector is an exact, non-negative integer. The first element of a octet vector has index 0, the last element has an index one less than the length of the vector.

[procedure] (u8vector? {{obj}})

Returns #t if obj is an octect vector, otherwise returns #f. Analogous to vector?.

[procedure] (make-u8vector k fill)

Returns a newly allocated octet vector of k elements. Each element is initialized to fill. Fill must be an octet. Analogous to make-vector.

[procedure] (u8vector octet ...)

Returns a newly allocated octet vector whose elements contain the given arguments, which must all be octets. Analogous to vector.

[procedure] (u8vector->list u8vector)
[procedure] (list->u8vector octets)

u8vector->list returns a newly allocated list of the elements of u8vector in the same order.Analogous to vector->list.

list->u8vector returns a newly allocated octet vector whose elements are the elements of list octets, which must all be octets. Analogous to list->vector.

[procedure] (u8vector-length u8vector)

Returns the number of elements in u8vector as an exact integer. Analogous to vector-length.

[procedure] (u8vector-ref u8vector k)

k must be a valid index of u8vector. u8vector-ref returns the contents of element k of u8vector. Analogous to vector-ref.

[procedure] (u8vector-set! u8vector k octet)

k must be a valid index of u8vector. u8vector-set! stores octet in element k of u8vector. The number of return values and the return values are unspecified. However, the number of return values is such that it is accepted by a continuation created by begin. Analogous to vector-set!.

[procedure] (u8vector=? u8vector-1 u8vector-2)

Returns #t if u8vector-1 and u8vector-2 are equal---that is, if they have the same length and equal elements at all valid indices.

[procedure] (u8vector-compare u8vector-1 u8vector-2)

Compares u8vector-1 and u8vector-2 and returns a value consistent with the vector ordering specified in SRFI 67, i.e. -1 if u8vector-1 is smaller than u8vector-2, 0 if they are equal, and 1 if u8vector-1 is greater than u8vector-2. Shorter vectors are always smaller than longer ones, and vectors of equal length are compared lexicographically.

[procedure] (u8vector-copy! source source-start target target-start n)

Copies data from octet vector source to octet vector target. Source-start, target-start, and n must be non-negative exact integers that satisfy

This copies the octets from source at indices [source-start, source-start + n) to consecutive indices in target starting at target-index.

This must work even if the memory regions for the source and the target overlap, i.e., the octets at the target location after the copy must be equal to the octets at the source location before the copy.

The number of return values and the return values are unspecified. However, the number of return values is such that it is accepted by a continuation created by begin. Analogous to vector-ref.

[procedure] (u8vector-copy u8vector)

Returns a newly allocated copy of octet vector u8vector.

References

Authors

Repository

https://github.com/diamond-lizard/srfi-66

Copyright (C) Michael Sperber (2005). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version history