/usr/share/scheme48-1.9/r6rs/bytevector.scm is in scheme48 1.9-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ; Part of Scheme 48 1.9. See file COPYING for notices and license.
; Authors: Harald Glab-Phlak, Mike Sperber, Will Clinger
; This is taken from the R6RS reference implementation by Mike
; Sperber, modified by Will Clinger.
(import-dynamic-externals "=scheme48external/r6rs")
(define-enumeration endianness
(little big)
endianness*)
(define bytevector? byte-vector?)
(define make-bytevector
(opt-lambda (size (fill 0))
(if (and (>= fill -128)
(<= fill 255))
(make-byte-vector size fill)
(error #f "wrong value to fill a byte vector must be octet" fill))))
(define bytevector-length byte-vector-length)
(define bytevector=? byte-vector=?)
(define (bytevector-fill! vector fill)
(let loop ((index 0))
(if (< index (bytevector-length vector))
(begin (bytevector-u8-set! vector index fill)
(loop (+ index 1))))))
;; may be we need a few checks to fit the need -- look carefully
(define (bytevector-copy! source source-start target target-start count)
(copy-bytes! source source-start target target-start count))
(define (bytevector-copy vector)
(let* ((size (byte-vector-length vector))
(copy (make-byte-vector size 0)))
(bytevector-copy! vector 0 copy 0 size)
copy))
;; now the stuff with the typed bytevectors begins
(define (u8->s8 val)
(if (> val 127)
(- val 256)
val))
(define (s8->u8 val)
(if (negative? val)
(+ val 256)
val))
(define (bytevector-u8-ref vector k)
(check-index vector k)
(byte-vector-ref vector k))
(define (bytevector-u8-set! vector k val)
(check-index vector k)
(check-range val 1 #f)
(byte-vector-set! vector k val))
(define (bytevector-s8-ref vector k)
(check-index vector k)
(u8->s8 (bytevector-u8-ref vector k)))
(define (bytevector-s8-set! vector k val)
(check-index vector k)
(check-range val 1 #t)
( bytevector-u8-set! vector k (s8->u8 val)))
(define (bytevector->u8-list octets)
(let loop ((n (bytevector-length octets)) (r '()))
(if (zero? n)
r
(loop (- n 1)
(cons (bytevector-u8-ref octets (- n 1)) r)))))
(define (u8-list->bytevector list)
(let ((vect (make-bytevector (length list))))
(let loop ((l list)
(index 0))
(if (not (eq? l '()))
(begin (bytevector-u8-set! vect index (car l))
(loop (cdr l)
(+ index 1)))))
vect))
;; the integer view (native integers) to a bytevector
(define (bytevector-uint-ref bytevector index endness size)
(case endness
((big)
(do ((i 0 (+ i 1))
(result 0 (+ (arithmetic-shift result 8)
(bytevector-u8-ref bytevector (+ index i)))))
((>= i size)
result)))
((little)
(do ((i (- size 1) (- i 1))
(result 0 (+ (arithmetic-shift result 8)
(bytevector-u8-ref bytevector (+ index i)))))
((< i 0)
result)))
(else
(error 'bytevector-uint-ref "Invalid endianness: " endness))))
(define (bytevector-sint-ref bytevector index endness size)
(let* ((high-byte (bytevector-u8-ref bytevector
(if (eq? endness (endianness big))
index
(+ index size -1))))
(uresult (bytevector-uint-ref bytevector index endness size)))
(if (> high-byte 127)
(- uresult (expt 256 size))
uresult)))
(define (bytevector-uint-set! bytevector index val endness size)
(check-range val size #f)
(case endness
((little)
(do ((i 0 (+ i 1))
(val val (quotient val 256)))
((>= i size))
(bytevector-u8-set! bytevector (+ index i) (remainder val 256))))
((big)
(do ((i (- size 1) (- i 1))
(val val (quotient val 256)))
((< i 0))
(bytevector-u8-set! bytevector (+ index i) (remainder val 256))))
(else
(error 'bytevector-uint-set! "Invalid endianness: " endness))))
(define (bytevector-sint-set! bytevector index val endness size)
(check-range val size #t)
(let ((uval (if (< val 0)
(+ val (* 128 (expt 256 (- size 1))))
val)))
(bytevector-uint-set! bytevector index uval endness size)))
(define (bytevector->uint-list vector endness size)
((make-bytevect->int-list bytevector-uint-ref)
vector endness size))
(define (bytevector->sint-list vector endness size)
((make-bytevect->int-list bytevector-sint-ref)
vector endness size))
(define (uint-list->bytevector list endness size)
((make-int-list->bytevect bytevector-uint-set!)
list endness size))
(define (sint-list->bytevector list endness size)
((make-int-list->bytevect bytevector-sint-set!)
list endness size))
(define (make-uint-ref size)
(lambda (bytevector k endianness)
(bytevector-uint-ref bytevector k endianness size)))
(define (make-sint-ref size)
(lambda (bytevector k endianness)
(bytevector-sint-ref bytevector k endianness size)))
(define (make-uint-set! size)
(lambda (bytevector k n endianness)
(bytevector-uint-set! bytevector k n endianness size)))
(define (make-sint-set! size)
(lambda (bytevector k n endianness)
(bytevector-sint-set! bytevector k n endianness size)))
(define (make-ref/native base base-ref)
(lambda (bytevector index)
(ensure-aligned index base)
(base-ref bytevector index (native-endianness))))
(define (make-set!/native base base-set!)
(lambda (bytevector index val)
(ensure-aligned index base)
(base-set! bytevector index val (native-endianness))))
;; uint16
(define bytevector-u16-ref (make-uint-ref 2))
(define bytevector-s16-ref (make-sint-ref 2))
(define bytevector-u16-native-ref (make-ref/native 2 bytevector-u16-ref))
(define bytevector-s16-native-ref (make-ref/native 2 bytevector-s16-ref))
(define bytevector-u16-set! (make-uint-set! 2))
(define bytevector-s16-set! (make-sint-set! 2))
(define bytevector-u16-native-set! (make-set!/native 2 bytevector-u16-set!))
(define bytevector-s16-native-set! (make-set!/native 2 bytevector-s16-set!))
;; uint32
(define bytevector-u32-ref (make-uint-ref 4))
(define bytevector-s32-ref (make-sint-ref 4))
(define bytevector-u32-native-ref (make-ref/native 4 bytevector-u32-ref))
(define bytevector-s32-native-ref (make-ref/native 4 bytevector-s32-ref))
(define bytevector-u32-set! (make-uint-set! 4))
(define bytevector-s32-set! (make-sint-set! 4))
(define bytevector-u32-native-set! (make-set!/native 4 bytevector-u32-set!))
(define bytevector-s32-native-set! (make-set!/native 4 bytevector-s32-set!))
;; uint64
(define bytevector-u64-ref (make-uint-ref 8))
(define bytevector-s64-ref (make-sint-ref 8))
(define bytevector-u64-native-ref (make-ref/native 8 bytevector-u64-ref))
(define bytevector-s64-native-ref (make-ref/native 8 bytevector-s64-ref))
(define bytevector-u64-set! (make-uint-set! 8))
(define bytevector-s64-set! (make-sint-set! 8))
(define bytevector-u64-native-set! (make-set!/native 8 bytevector-u64-set!))
(define bytevector-s64-native-set! (make-set!/native 8 bytevector-s64-set!))
;; helper procedures
(define (make-bytevect->int-list bytevect-ref)
(lambda (vect endness size)
(let ((length (bytevector-length vect)))
(let loop ((i 0) (r '()))
(if (>= i length)
(reverse r)
(loop (+ i size)
(cons (bytevect-ref vect i endness size) r)))))))
(define (make-int-list->bytevect bytevect-set!)
(lambda (l endness size)
(let ((bytevect (make-bytevector (* size (length l)))))
(let loop ((i 0) (l l))
(if (null? l)
bytevect
(begin
(bytevect-set! bytevect i (car l) endness size)
(loop (+ i size) (cdr l))))))))
;; general checks
(define (ensure-aligned index base)
(if (not (zero? (remainder index base)))
(assertion-violation 'ensure-aligned "non-aligned bytevector access" index base)))
(define (check-range value byte-count signed-check?)
(let* ((bits (* byte-count 8))
(unsigned-low 0)
(unsigned-high (- (expt 2 bits) 1))
(signed-low (* -1 (expt 2 (- bits 1))))
(signed-high (- (expt 2 (- bits 1)) 1)))
(if signed-check?
(if (not (and (>= value signed-low) (<= value signed-high)))
(assertion-violation 'check-range
"range check for value failed / signed - value does not fit into "
byte-count 'bytes 'checked-value: value))
(if (not (and (>= value unsigned-low) (<= value unsigned-high)))
(assertion-violation 'check-range
"range check for value failed / unsigned - value does not fit into "
byte-count 'bytes 'checked-value: value)))))
(define (check-index b i)
(if (or (> i (- (bytevector-length b) 1)) (< i 0))
(assertion-violation 'check-index
"invalid index forr vector must be in the range of"
0 'to (- (bytevector-length b) 1))))
(define (native-endianness)
(if (external-r6rs-big-endian?)
(endianness big)
(endianness little)))
;; external fun definition
(import-lambda-definition-2 external-r6rs-big-endian?
()
"r6rs_is_big_endian")
|