| 1 |
;;; |
| 2 |
;;; scheme.bytevector - R6RS-compatible bytevector library |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2020-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module scheme.bytevector |
| 35 |
(use util.match) |
| 36 |
(use gauche.uvector) |
| 37 |
(use gauche.unicode) |
| 38 |
(use binary.io) |
| 39 |
(export endianness native-endianness |
| 40 |
bytevector? ; gauche.uvector |
| 41 |
make-bytevector ; gauche.uvector |
| 42 |
bytevector-length ; gauche.uvector |
| 43 |
bytevector=? ; gauche.uvector |
| 44 |
bytevector-fill! ; gauche.uvector |
| 45 |
(rename bytevector-copy!-r6 bytevector-copy!) ; gauche.uvector |
| 46 |
bytevector-copy ; subset of gauche.uvector |
| 47 |
bytevector-u8-ref ; gauche.uvector |
| 48 |
bytevector-s8-ref ; gauche.uvector |
| 49 |
bytevector-u8-set! ; gauche.uvector |
| 50 |
bytevector-s8-set! ; gauche.uvector |
| 51 |
bytevector->u8-list ; gauche.uvector |
| 52 |
u8-list->bytevector ; gauche.uvector |
| 53 |
bytevector-uint-ref |
| 54 |
bytevector-sint-ref |
| 55 |
bytevector-uint-set! |
| 56 |
bytevector-sint-set! |
| 57 |
bytevector->uint-list |
| 58 |
bytevector->sint-list |
| 59 |
uint-list->bytevector |
| 60 |
sint-list->bytevector |
| 61 |
bytevector-u16-ref |
| 62 |
bytevector-s16-ref |
| 63 |
bytevector-u16-native-ref |
| 64 |
bytevector-s16-native-ref |
| 65 |
bytevector-u16-set! |
| 66 |
bytevector-s16-set! |
| 67 |
bytevector-u16-native-set! |
| 68 |
bytevector-s16-native-set! |
| 69 |
bytevector-u32-ref |
| 70 |
bytevector-s32-ref |
| 71 |
bytevector-u32-native-ref |
| 72 |
bytevector-s32-native-ref |
| 73 |
bytevector-u32-set! |
| 74 |
bytevector-s32-set! |
| 75 |
bytevector-u32-native-set! |
| 76 |
bytevector-s32-native-set! |
| 77 |
bytevector-u64-ref |
| 78 |
bytevector-s64-ref |
| 79 |
bytevector-u64-native-ref |
| 80 |
bytevector-s64-native-ref |
| 81 |
bytevector-u64-set! |
| 82 |
bytevector-s64-set! |
| 83 |
bytevector-u64-native-set! |
| 84 |
bytevector-s64-native-set! |
| 85 |
bytevector-ieee-single-ref |
| 86 |
bytevector-ieee-double-ref |
| 87 |
bytevector-ieee-single-native-ref |
| 88 |
bytevector-ieee-double-native-ref |
| 89 |
bytevector-ieee-single-set! |
| 90 |
bytevector-ieee-double-set! |
| 91 |
bytevector-ieee-single-native-set! |
| 92 |
bytevector-ieee-double-native-set! |
| 93 |
string->utf8 ;gauche.unicode |
| 94 |
string->utf16 ;gauche.unicode |
| 95 |
string->utf32 ;gauche.unicode |
| 96 |
utf8->string ;gauche.unicode |
| 97 |
utf16->string ;gauche.unicode |
| 98 |
utf32->string ;gauche.unicode |
| 99 |
)) |
| 100 |
(select-module scheme.bytevector) |
| 101 |
|
| 102 |
(define-syntax endianness |
| 103 |
(er-macro-transformer |
| 104 |
(^[f r c] |
| 105 |
(match f |
| 106 |
[(_ e) |
| 107 |
(let1 ee (unwrap-syntax e) |
| 108 |
(unless (memq e |
| 109 |
'(big big-endian little little-endian |
| 110 |
arm-little-endian)) |
| 111 |
(error "Unrecognized endianness symbol. Must be either one \ |
| 112 |
of big, big-endian, little, little-endian or \ |
| 113 |
arm-little-endian, but got:" ee)) |
| 114 |
(quasirename r `',ee))] |
| 115 |
[_ (error "Malformed endianness: " f)])))) |
| 116 |
|
| 117 |
(define (native-endianness) (native-endian)) |
| 118 |
|
| 119 |
(define (bytevector-uint-ref v pos endian size) |
| 120 |
(get-uint size v pos endian)) |
| 121 |
(define (bytevector-sint-ref v pos endian size) |
| 122 |
(get-sint size v pos endian)) |
| 123 |
(define (bytevector-uint-set! v pos val endian size) |
| 124 |
(put-uint! size v pos val endian)) |
| 125 |
(define (bytevector-sint-set! v pos val endian size) |
| 126 |
(put-sint! size v pos val endian)) |
| 127 |
|
| 128 |
(define (%v->list get v endian size) |
| 129 |
(assume-type <u8vector> v) |
| 130 |
(let1 len (u8vector-length v) |
| 131 |
(unless (zero? (mod len size)) |
| 132 |
(errorf "bytevector length (~s) is not divisible by integer size (~s)" |
| 133 |
len size)) |
| 134 |
(let loop ([pos 0] [r '()]) |
| 135 |
(if (= pos len) |
| 136 |
(reverse r) |
| 137 |
(loop (+ pos size) |
| 138 |
(cons (get size v pos endian) r)))))) |
| 139 |
|
| 140 |
(define (bytevector->uint-list v endian size) |
| 141 |
(%v->list get-uint v endian size)) |
| 142 |
(define (bytevector->sint-list v endian size) |
| 143 |
(%v->list get-sint v endian size)) |
| 144 |
|
| 145 |
(define (%list->v put lis endian size) |
| 146 |
(rlet1 v (make-u8vector (* size (length list))) |
| 147 |
(do ([lis lis (cdr lis)] |
| 148 |
[pos 0 (+ pos size)]) |
| 149 |
[(null? lis)] |
| 150 |
(put size v pos (car lis) endian)))) |
| 151 |
|
| 152 |
(define (uint-list->bytevector lis endian size) |
| 153 |
(%list->v put-uint! lis endian size)) |
| 154 |
(define (sint-list->bytevector lis endian size) |
| 155 |
(%list->v put-sint! lis endian size)) |
| 156 |
|
| 157 |
(define ne (native-endian)) |
| 158 |
|
| 159 |
(define (bytevector-u16-ref v k endian) (get-u16 v k endian)) |
| 160 |
(define (bytevector-s16-ref v k endian) (get-s16 v k endian)) |
| 161 |
(define (bytevector-u32-ref v k endian) (get-u32 v k endian)) |
| 162 |
(define (bytevector-s32-ref v k endian) (get-s32 v k endian)) |
| 163 |
(define (bytevector-u64-ref v k endian) (get-u64 v k endian)) |
| 164 |
(define (bytevector-s64-ref v k endian) (get-s64 v k endian)) |
| 165 |
(define (bytevector-u16-native-ref v k endian) (get-u16 v k ne)) |
| 166 |
(define (bytevector-s16-native-ref v k endian) (get-s16 v k ne)) |
| 167 |
(define (bytevector-u32-native-ref v k endian) (get-u32 v k ne)) |
| 168 |
(define (bytevector-s32-native-ref v k endian) (get-s32 v k ne)) |
| 169 |
(define (bytevector-u64-native-ref v k endian) (get-u64 v k ne)) |
| 170 |
(define (bytevector-s64-native-ref v k endian) (get-s64 v k ne)) |
| 171 |
|
| 172 |
(define (bytevector-u16-set! v k n endian) (put-u16! v k n endian)) |
| 173 |
(define (bytevector-s16-set! v k n endian) (put-s16! v k n endian)) |
| 174 |
(define (bytevector-u32-set! v k n endian) (put-u32! v k n endian)) |
| 175 |
(define (bytevector-s32-set! v k n endian) (put-s32! v k n endian)) |
| 176 |
(define (bytevector-u64-set! v k n endian) (put-u64! v k n endian)) |
| 177 |
(define (bytevector-s64-set! v k n endian) (put-s64! v k n endian)) |
| 178 |
(define (bytevector-u16-native-set! v k n) (put-u16! v k n ne)) |
| 179 |
(define (bytevector-s16-native-set! v k n) (put-s16! v k n ne)) |
| 180 |
(define (bytevector-u32-native-set! v k n) (put-u32! v k n ne)) |
| 181 |
(define (bytevector-s32-native-set! v k n) (put-s32! v k n ne)) |
| 182 |
(define (bytevector-u64-native-set! v k n) (put-u64! v k n ne)) |
| 183 |
(define (bytevector-s64-native-set! v k n) (put-s64! v k n ne)) |
| 184 |
|
| 185 |
(define (bytevector-ieee-single-ref v k endian) (get-f32 v k endian)) |
| 186 |
(define (bytevector-ieee-double-ref v k endian) (get-f64 v k endian)) |
| 187 |
(define (bytevector-ieee-single-native-ref v k) (get-f32 v k ne)) |
| 188 |
(define (bytevector-ieee-double-native-ref v k) (get-f64 v k ne)) |
| 189 |
(define (bytevector-ieee-single-set! v k x endian) (put-f32! v k x endian)) |
| 190 |
(define (bytevector-ieee-double-set! v k x endian) (put-f64! v k x endian)) |
| 191 |
(define (bytevector-ieee-single-native-set! v k x) (put-f32! v k x ne)) |
| 192 |
(define (bytevector-ieee-double-native-set! v k x) (put-f64! v k x ne)) |