| 1 |
;;; binary.io -- serializing binary data |
| 2 |
|
| 3 |
;;; Created: <2003-01-20 23:25:06 foof> |
| 4 |
;;; Time-stamp: <2003-01-29 20:53:45 foof> |
| 5 |
;;; Author: Alex Shinn <[email protected]> |
| 6 |
|
| 7 |
;; It is computed that eleven thousand persons have at several times |
| 8 |
;; suffered death, rather than submit to break their eggs at the smaller |
| 9 |
;; end. Many hundred large volumes have been published upon this |
| 10 |
;; controversy: but the books of the Big-endians have been long |
| 11 |
;; forbidden, and the whole party rendered incapable by law of holding |
| 12 |
;; employments. |
| 13 |
;; -- from "Gulliver's Travels" by Jonathan Swift |
| 14 |
|
| 15 |
;; This module was originally written by Alex Shinn, in pure Scheme. |
| 16 |
;; Shiro Kawai rewrote primitive routines in C for the better performance, |
| 17 |
;; renamed them for shorter names, and added uvector access routines. |
| 18 |
|
| 19 |
(define-module binary.io |
| 20 |
(export read-uint read-u8 read-u16 read-u32 read-u64 |
| 21 |
read-sint read-s8 read-s16 read-s32 read-s64 |
| 22 |
read-ber-integer read-f16 read-f32 read-f64 |
| 23 |
write-uint write-u8 write-u16 write-u32 write-u64 |
| 24 |
write-sint write-s8 write-s16 write-s32 write-s64 |
| 25 |
write-ber-integer write-f16 write-f32 write-f64 |
| 26 |
get-uint get-sint |
| 27 |
get-u8 get-u16 get-u32 get-u64 get-s8 get-s16 get-s32 get-s64 |
| 28 |
get-f16 get-f32 get-f64 |
| 29 |
get-u16be get-u16le get-u32be get-u32le get-u64be get-u64le |
| 30 |
get-s16be get-s16le get-s32be get-s32le get-s64be get-s64le |
| 31 |
get-f16be get-f16le get-f32be get-f32le get-f64be get-f64le |
| 32 |
put-uint! put-sint! |
| 33 |
put-u8! put-u16! put-u32! put-u64! put-s8! put-s16! put-s32! put-s64! |
| 34 |
put-f16! put-f32! put-f64! |
| 35 |
put-u16be! put-u16le! put-u32be! put-u32le! put-u64be! put-u64le! |
| 36 |
put-s16be! put-s16le! put-s32be! put-s32le! put-s64be! put-s64le! |
| 37 |
put-f16be! put-f16le! put-f32be! put-f32le! put-f64be! put-f64le! |
| 38 |
|
| 39 |
;; old names |
| 40 |
read-binary-uint |
| 41 |
read-binary-uint8 read-binary-uint16 |
| 42 |
read-binary-uint32 read-binary-uint64 |
| 43 |
read-binary-sint |
| 44 |
read-binary-sint8 read-binary-sint16 |
| 45 |
read-binary-sint32 read-binary-sint64 |
| 46 |
read-binary-short read-binary-ushort |
| 47 |
read-binary-long read-binary-ulong |
| 48 |
read-binary-float read-binary-double |
| 49 |
write-binary-uint |
| 50 |
write-binary-uint8 write-binary-uint16 |
| 51 |
write-binary-uint32 write-binary-uint64 |
| 52 |
write-binary-sint |
| 53 |
write-binary-sint8 write-binary-sint16 |
| 54 |
write-binary-sint32 write-binary-sint64 |
| 55 |
write-binary-short write-binary-ushort |
| 56 |
write-binary-long write-binary-ulong |
| 57 |
write-binary-float write-binary-double |
| 58 |
)) |
| 59 |
(select-module binary.io) |
| 60 |
|
| 61 |
;;; |
| 62 |
;;; config |
| 63 |
;;; |
| 64 |
|
| 65 |
(define-constant *bit-size* 2) ;; hey, you never know :) |
| 66 |
(define-constant *byte-size* 8) |
| 67 |
(define-constant *byte-magnitude* (expt *bit-size* *byte-size*)) |
| 68 |
(define-constant *byte-mask* (- *byte-magnitude* 1)) |
| 69 |
(define-constant *byte-right-shift* (* -1 *byte-size*)) |
| 70 |
|
| 71 |
;;; |
| 72 |
;;; basic reading |
| 73 |
;;; |
| 74 |
|
| 75 |
;; mind-numblingly slow, consider a uvector approach but it doesn't |
| 76 |
;; handle endianess |
| 77 |
(define (read-uint size :optional (port (current-input-port)) |
| 78 |
(endian (default-endian))) |
| 79 |
(case size |
| 80 |
[(1) (read-u8 port endian)] |
| 81 |
[(2) (read-u16 port endian)] |
| 82 |
[(4) (read-u32 port endian)] |
| 83 |
[(8) (read-u64 port endian)] |
| 84 |
[else |
| 85 |
(let loop ([ls '()] [cnt 0]) |
| 86 |
(if (= cnt size) |
| 87 |
(fold (^[a b] (+ a (* b *byte-magnitude*))) |
| 88 |
0 |
| 89 |
(if (eq? endian 'big-endian) (reverse ls) ls)) |
| 90 |
(let1 byte (read-byte port) |
| 91 |
(if (eof-object? byte) |
| 92 |
byte |
| 93 |
(loop (cons byte ls) (+ cnt 1))))))] |
| 94 |
)) |
| 95 |
|
| 96 |
(define (lognot-small int bytes) |
| 97 |
(logand (lognot int) (- (expt *bit-size* (* *byte-size* bytes)) 1))) |
| 98 |
|
| 99 |
(define (uint->sint int bytes) |
| 100 |
(let1 highbit (- (* *byte-size* bytes) 1) |
| 101 |
(if (logbit? highbit int) |
| 102 |
(* -1 (+ 1 (lognot-small int bytes))) |
| 103 |
int))) |
| 104 |
|
| 105 |
(define (sint->uint int bytes) |
| 106 |
(if (< int 0) |
| 107 |
(+ 1 (lognot-small (abs int) bytes)) |
| 108 |
int)) |
| 109 |
|
| 110 |
(define (read-sint size :optional (port (current-input-port)) |
| 111 |
(endian (default-endian))) |
| 112 |
(case size |
| 113 |
[(1) (read-s8 port endian)] |
| 114 |
[(2) (read-s16 port endian)] |
| 115 |
[(4) (read-s32 port endian)] |
| 116 |
[(8) (read-s64 port endian)] |
| 117 |
[else (uint->sint (read-uint size port endian) size)])) |
| 118 |
|
| 119 |
;;; |
| 120 |
;;; basic writing |
| 121 |
;;; |
| 122 |
|
| 123 |
(define (write-uint size int :optional (port (current-output-port)) |
| 124 |
(endian (default-endian))) |
| 125 |
(case size |
| 126 |
[(1) (write-u8 int port endian)] |
| 127 |
[(2) (write-u16 int port endian)] |
| 128 |
[(4) (write-u32 int port endian)] |
| 129 |
[(8) (write-u64 int port endian)] |
| 130 |
[else |
| 131 |
(let ([ls '()]) |
| 132 |
;; build a list of bytes |
| 133 |
(dotimes [i size] |
| 134 |
(push! ls (logand int *byte-mask*)) |
| 135 |
(set! int (ash int *byte-right-shift*))) |
| 136 |
;; reverse if big-endian |
| 137 |
(unless (eq? endian 'big-endian) |
| 138 |
(set! ls (reverse ls))) |
| 139 |
;; write the list |
| 140 |
(for-each (cut write-byte <> port) ls))])) |
| 141 |
|
| 142 |
(define (write-sint size int :optional (port (current-output-port)) |
| 143 |
(endian (default-endian))) |
| 144 |
(case size |
| 145 |
[(1) (write-s8 int port endian)] |
| 146 |
[(2) (write-s16 int port endian)] |
| 147 |
[(4) (write-s32 int port endian)] |
| 148 |
[(8) (write-s64 int port endian)] |
| 149 |
[else (write-uint size (sint->uint int size) port endian)])) |
| 150 |
|
| 151 |
;;; |
| 152 |
;;; compatibility |
| 153 |
;;; |
| 154 |
|
| 155 |
;; These are used in binary.pack for the (unofficial) features to |
| 156 |
;; read/write integers in "native" width of C system on the platform. |
| 157 |
;; Should be removed soon (after binary.pack rewrite). Do not use them. |
| 158 |
(define read-binary-short read-s16) |
| 159 |
(define read-binary-ushort read-u16) |
| 160 |
(define read-binary-long read-s32) |
| 161 |
(define read-binary-ulong read-u32) |
| 162 |
|
| 163 |
(define write-binary-short write-s16) |
| 164 |
(define write-binary-ushort write-u16) |
| 165 |
(define write-binary-long write-s32) |
| 166 |
(define write-binary-ulong write-u32) |
| 167 |
|
| 168 |
;; Other compatibility names. They have been official before 0.8.10, |
| 169 |
;; and used widely. So we keep them for a while. |
| 170 |
(define read-binary-uint read-uint) |
| 171 |
(define read-binary-sint read-sint) |
| 172 |
(define read-binary-uint8 read-u8) |
| 173 |
(define read-binary-sint8 read-s8) |
| 174 |
(define read-binary-uint16 read-u16) |
| 175 |
(define read-binary-sint16 read-s16) |
| 176 |
(define read-binary-uint32 read-u32) |
| 177 |
(define read-binary-sint32 read-s32) |
| 178 |
(define read-binary-uint64 read-u64) |
| 179 |
(define read-binary-sint64 read-s64) |
| 180 |
(define read-binary-float read-f32) |
| 181 |
(define read-binary-double read-f64) |
| 182 |
|
| 183 |
(define write-binary-uint write-uint) |
| 184 |
(define write-binary-sint write-sint) |
| 185 |
(define write-binary-uint8 write-u8) |
| 186 |
(define write-binary-sint8 write-s8) |
| 187 |
(define write-binary-uint16 write-u16) |
| 188 |
(define write-binary-sint16 write-s16) |
| 189 |
(define write-binary-uint32 write-u32) |
| 190 |
(define write-binary-sint32 write-s32) |
| 191 |
(define write-binary-uint64 write-u64) |
| 192 |
(define write-binary-sint64 write-s64) |
| 193 |
(define write-binary-float write-f32) |
| 194 |
(define write-binary-double write-f64) |
| 195 |
|
| 196 |
;;; |
| 197 |
;;; bignum encodings -- Basic Encoding Rules (BER) from X.209 |
| 198 |
;;; |
| 199 |
|
| 200 |
;; A BER compressed integer is an unsigned integer in base 128, most |
| 201 |
;; significant digit first, where the high bit is set on all but the |
| 202 |
;; final (least significant) byte. Thus any size integer can be |
| 203 |
;; encoded, but the encoding is efficient and small integers don't take |
| 204 |
;; up any more space than they would in normal char/short/int encodings. |
| 205 |
|
| 206 |
(define (read-ber-integer :optional (port (current-input-port))) |
| 207 |
(let1 first (read-byte port) |
| 208 |
(cond [(eof-object? first) first] ;; stop on eof |
| 209 |
[(< first 128) first] |
| 210 |
[else (let loop ([res (ash (logand first #b01111111) 7)] |
| 211 |
[byte (read-u8 port)]) |
| 212 |
(if (< byte 128) |
| 213 |
(+ res byte) ;; final byte |
| 214 |
(loop (ash (+ res (logand byte #b01111111)) 7) |
| 215 |
(read-u8 port))))]))) |
| 216 |
|
| 217 |
(define (write-ber-integer number :optional (port (current-output-port))) |
| 218 |
(let ([final (logand number #b01111111)] |
| 219 |
[start (ash number -7)]) |
| 220 |
(unless (zero? start) |
| 221 |
(let loop ([n start]) |
| 222 |
(cond [(< n 128) (write-u8 (logior n #b10000000) port)] |
| 223 |
[else (loop (ash n -7)) ;; write high bytes first |
| 224 |
(write-u8 (logior (logand n #b01111111) #b10000000) port)]))) |
| 225 |
(write-u8 final port))) |
| 226 |
|
| 227 |
;;; |
| 228 |
;;; Low-level stuff |
| 229 |
;;; |
| 230 |
|
| 231 |
(inline-stub |
| 232 |
(declcode |
| 233 |
(.include <gauche/priv/configP.h> |
| 234 |
"binary.h")) |
| 235 |
|
| 236 |
(define-cproc read-u8 |
| 237 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 238 |
Scm_ReadBinaryU8) |
| 239 |
(define-cproc read-u16 |
| 240 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 241 |
Scm_ReadBinaryU16) |
| 242 |
(define-cproc read-u32 |
| 243 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 244 |
Scm_ReadBinaryU32) |
| 245 |
(define-cproc read-u64 |
| 246 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 247 |
Scm_ReadBinaryU64) |
| 248 |
|
| 249 |
(define-cproc read-s8 |
| 250 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 251 |
Scm_ReadBinaryS8) |
| 252 |
(define-cproc read-s16 |
| 253 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 254 |
Scm_ReadBinaryS16) |
| 255 |
(define-cproc read-s32 |
| 256 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 257 |
Scm_ReadBinaryS32) |
| 258 |
(define-cproc read-s64 |
| 259 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 260 |
Scm_ReadBinaryS64) |
| 261 |
|
| 262 |
(define-cproc write-u8 |
| 263 |
(intval::<integer> |
| 264 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 265 |
::<void> Scm_WriteBinaryU8) |
| 266 |
(define-cproc write-u16 |
| 267 |
(intval::<integer> |
| 268 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 269 |
::<void> Scm_WriteBinaryU16) |
| 270 |
(define-cproc write-u32 |
| 271 |
(intval::<integer> |
| 272 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 273 |
::<void> Scm_WriteBinaryU32) |
| 274 |
(define-cproc write-u64 |
| 275 |
(intval::<integer> |
| 276 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 277 |
::<void> Scm_WriteBinaryU64) |
| 278 |
(define-cproc write-s8 |
| 279 |
(intval::<integer> |
| 280 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 281 |
::<void> Scm_WriteBinaryS8) |
| 282 |
(define-cproc write-s16 |
| 283 |
(intval::<integer> |
| 284 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 285 |
::<void> Scm_WriteBinaryS16) |
| 286 |
(define-cproc write-s32 |
| 287 |
(intval::<integer> |
| 288 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 289 |
::<void> Scm_WriteBinaryS32) |
| 290 |
(define-cproc write-s64 |
| 291 |
(intval::<integer> |
| 292 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 293 |
::<void> Scm_WriteBinaryS64) |
| 294 |
|
| 295 |
(define-cproc read-f16 |
| 296 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 297 |
Scm_ReadBinaryF16) |
| 298 |
(define-cproc read-f32 |
| 299 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 300 |
Scm_ReadBinaryF32) |
| 301 |
(define-cproc read-f64 |
| 302 |
(:optional (port::<input-port>? #f) (endian::<symbol>? #f)) |
| 303 |
Scm_ReadBinaryF64) |
| 304 |
|
| 305 |
(define-cproc write-f16 |
| 306 |
(val::<number> |
| 307 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 308 |
::<void> Scm_WriteBinaryF16) |
| 309 |
(define-cproc write-f32 |
| 310 |
(val::<number> |
| 311 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 312 |
::<void> Scm_WriteBinaryF32) |
| 313 |
(define-cproc write-f64 |
| 314 |
(val::<number> |
| 315 |
:optional (port::<output-port>? #f) (endian::<symbol>? #f)) |
| 316 |
::<void> Scm_WriteBinaryF64) |
| 317 |
|
| 318 |
(define-cproc get-u8 |
| 319 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 320 |
Scm_GetBinaryU8) |
| 321 |
(define-cproc get-s8 |
| 322 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 323 |
Scm_GetBinaryS8) |
| 324 |
(define-cproc get-u16 |
| 325 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 326 |
Scm_GetBinaryU16) |
| 327 |
(define-cproc get-s16 |
| 328 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 329 |
Scm_GetBinaryS16) |
| 330 |
(define-cproc get-u32 |
| 331 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 332 |
Scm_GetBinaryU32) |
| 333 |
(define-cproc get-s32 |
| 334 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 335 |
Scm_GetBinaryS32) |
| 336 |
(define-cproc get-u64 |
| 337 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 338 |
Scm_GetBinaryU64) |
| 339 |
(define-cproc get-s64 |
| 340 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 341 |
Scm_GetBinaryS64) |
| 342 |
(define-cproc get-f16 |
| 343 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 344 |
Scm_GetBinaryF16) |
| 345 |
(define-cproc get-f32 |
| 346 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 347 |
Scm_GetBinaryF32) |
| 348 |
(define-cproc get-f64 |
| 349 |
(v::<uvector> off::<uint> :optional (endian::<symbol>? #f)) |
| 350 |
Scm_GetBinaryF64) |
| 351 |
|
| 352 |
(define-cproc put-u8! |
| 353 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 354 |
::<void> Scm_PutBinaryU8) |
| 355 |
(define-cproc put-s8! |
| 356 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 357 |
::<void> Scm_PutBinaryS8) |
| 358 |
(define-cproc put-u16! |
| 359 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 360 |
::<void> Scm_PutBinaryU16) |
| 361 |
(define-cproc put-s16! |
| 362 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 363 |
::<void> Scm_PutBinaryS16) |
| 364 |
(define-cproc put-u32! |
| 365 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 366 |
::<void> Scm_PutBinaryU32) |
| 367 |
(define-cproc put-s32! |
| 368 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 369 |
::<void> Scm_PutBinaryS32) |
| 370 |
(define-cproc put-u64! |
| 371 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 372 |
::<void> Scm_PutBinaryU64) |
| 373 |
(define-cproc put-s64! |
| 374 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 375 |
::<void> Scm_PutBinaryS64) |
| 376 |
(define-cproc put-f16! |
| 377 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 378 |
::<void> Scm_PutBinaryF16) |
| 379 |
(define-cproc put-f32! |
| 380 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 381 |
::<void> Scm_PutBinaryF32) |
| 382 |
(define-cproc put-f64! |
| 383 |
(v::<uvector> off::<uint> val :optional (endian::<symbol>? #f)) |
| 384 |
::<void> Scm_PutBinaryF64) |
| 385 |
|
| 386 |
(define-cproc get-u16be (v::<uvector> off::<uint>) |
| 387 |
(return (Scm_GetBinaryU16 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 388 |
(define-cproc get-u16le (v::<uvector> off::<uint>) |
| 389 |
(return (Scm_GetBinaryU16 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 390 |
(define-cproc get-s16be (v::<uvector> off::<uint>) |
| 391 |
(return (Scm_GetBinaryS16 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 392 |
(define-cproc get-s16le (v::<uvector> off::<uint>) |
| 393 |
(return (Scm_GetBinaryS16 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 394 |
(define-cproc get-u32be (v::<uvector> off::<uint>) |
| 395 |
(return (Scm_GetBinaryU32 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 396 |
(define-cproc get-u32le (v::<uvector> off::<uint>) |
| 397 |
(return (Scm_GetBinaryU32 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 398 |
(define-cproc get-s32be (v::<uvector> off::<uint>) |
| 399 |
(return (Scm_GetBinaryS32 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 400 |
(define-cproc get-s32le (v::<uvector> off::<uint>) |
| 401 |
(return (Scm_GetBinaryS32 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 402 |
(define-cproc get-u64be (v::<uvector> off::<uint>) |
| 403 |
(return (Scm_GetBinaryU64 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 404 |
(define-cproc get-u64le (v::<uvector> off::<uint>) |
| 405 |
(return (Scm_GetBinaryU64 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 406 |
(define-cproc get-s64be (v::<uvector> off::<uint>) |
| 407 |
(return (Scm_GetBinaryS64 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 408 |
(define-cproc get-s64le (v::<uvector> off::<uint>) |
| 409 |
(return (Scm_GetBinaryS64 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 410 |
(define-cproc get-f16be (v::<uvector> off::<uint>) |
| 411 |
(return (Scm_GetBinaryF16 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 412 |
(define-cproc get-f16le (v::<uvector> off::<uint>) |
| 413 |
(return (Scm_GetBinaryF16 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 414 |
(define-cproc get-f32be (v::<uvector> off::<uint>) |
| 415 |
(return (Scm_GetBinaryF32 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 416 |
(define-cproc get-f32le (v::<uvector> off::<uint>) |
| 417 |
(return (Scm_GetBinaryF32 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 418 |
(define-cproc get-f64be (v::<uvector> off::<uint>) |
| 419 |
(return (Scm_GetBinaryF64 v off (SCM_SYMBOL SCM_SYM_BIG_ENDIAN)))) |
| 420 |
(define-cproc get-f64le (v::<uvector> off::<uint>) |
| 421 |
(return (Scm_GetBinaryF64 v off (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN)))) |
| 422 |
|
| 423 |
(define-cproc put-u16be! (v::<uvector> off::<uint> val) ::<void> |
| 424 |
(Scm_PutBinaryU16 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 425 |
(define-cproc put-u16le! (v::<uvector> off::<uint> val) ::<void> |
| 426 |
(Scm_PutBinaryU16 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 427 |
(define-cproc put-s16be! (v::<uvector> off::<uint> val) ::<void> |
| 428 |
(Scm_PutBinaryS16 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 429 |
(define-cproc put-s16le! (v::<uvector> off::<uint> val) ::<void> |
| 430 |
(Scm_PutBinaryS16 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 431 |
(define-cproc put-u32be! (v::<uvector> off::<uint> val) ::<void> |
| 432 |
(Scm_PutBinaryU32 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 433 |
(define-cproc put-u32le! (v::<uvector> off::<uint> val) ::<void> |
| 434 |
(Scm_PutBinaryU32 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 435 |
(define-cproc put-s32be! (v::<uvector> off::<uint> val ) ::<void> |
| 436 |
(Scm_PutBinaryS32 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 437 |
(define-cproc put-s32le! (v::<uvector> off::<uint> val ) ::<void> |
| 438 |
(Scm_PutBinaryS32 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 439 |
(define-cproc put-u64be! (v::<uvector> off::<uint> val ) ::<void> |
| 440 |
(Scm_PutBinaryU64 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 441 |
(define-cproc put-u64le! (v::<uvector> off::<uint> val ) ::<void> |
| 442 |
(Scm_PutBinaryU64 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 443 |
(define-cproc put-s64be! (v::<uvector> off::<uint> val ) ::<void> |
| 444 |
(Scm_PutBinaryS64 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 445 |
(define-cproc put-s64le! (v::<uvector> off::<uint> val ) ::<void> |
| 446 |
(Scm_PutBinaryS64 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 447 |
(define-cproc put-f16be! (v::<uvector> off::<uint> val) ::<void> |
| 448 |
(Scm_PutBinaryF16 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 449 |
(define-cproc put-f16le! (v::<uvector> off::<uint> val) ::<void> |
| 450 |
(Scm_PutBinaryF16 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 451 |
(define-cproc put-f32be! (v::<uvector> off::<uint> val) ::<void> |
| 452 |
(Scm_PutBinaryF32 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 453 |
(define-cproc put-f32le! (v::<uvector> off::<uint> val) ::<void> |
| 454 |
(Scm_PutBinaryF32 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 455 |
(define-cproc put-f64be! (v::<uvector> off::<uint> val) ::<void> |
| 456 |
(Scm_PutBinaryF64 v off val (SCM_SYMBOL SCM_SYM_BIG_ENDIAN))) |
| 457 |
(define-cproc put-f64le! (v::<uvector> off::<uint> val) ::<void> |
| 458 |
(Scm_PutBinaryF64 v off val (SCM_SYMBOL SCM_SYM_LITTLE_ENDIAN))) |
| 459 |
) |
| 460 |
|
| 461 |
(define (get-uint size uv pos :optional (endian (default-endian))) |
| 462 |
(case size |
| 463 |
[(1) (get-u8 uv pos)] |
| 464 |
[(2) (get-u16 uv pos endian)] |
| 465 |
[(4) (get-u32 uv pos endian)] |
| 466 |
[(8) (get-u64 uv pos endian)] |
| 467 |
[else (ecase endian |
| 468 |
[(big-endian) |
| 469 |
(do ([i 0 (+ i 1)] |
| 470 |
[r 0 (logior (ash r 8) (get-u8 uv (+ i pos)))]) |
| 471 |
[(>= i size) r] |
| 472 |
)] |
| 473 |
[(little-endian arm-little-endian) |
| 474 |
(do ([i (- size 1) (- i 1)] |
| 475 |
[r 0 (logior (ash r 8) (get-u8 uv (+ i pos)))]) |
| 476 |
[(< i 0) r] |
| 477 |
)])])) |
| 478 |
|
| 479 |
(define (get-sint size uv pos :optional (endian (default-endian))) |
| 480 |
(case size |
| 481 |
[(1) (get-s8 uv pos)] |
| 482 |
[(2) (get-s16 uv pos endian)] |
| 483 |
[(4) (get-s32 uv pos endian)] |
| 484 |
[(8) (get-s64 uv pos endian)] |
| 485 |
[else (uint->sint (get-uint size uv pos endian) size)])) |
| 486 |
|
| 487 |
(define (%put-int! size uv pos val endian) ; val can be signed or unsigned |
| 488 |
(ecase endian |
| 489 |
[(big-endian) |
| 490 |
(do ([i (- size 1) (- i 1)] |
| 491 |
[v val (ash v -8)]) |
| 492 |
[(< i 0) (undefined)] |
| 493 |
(put-u8! uv (+ i pos) (logand v #xff)))] |
| 494 |
[(little-endian arm-little-endian) |
| 495 |
(do ([i 0 (+ i 1)] |
| 496 |
[v val (ash v -8)]) |
| 497 |
[(>= i size) (undefined)] |
| 498 |
(put-u8! uv (+ i pos) (logand v #xff)))])) |
| 499 |
|
| 500 |
(define (put-uint! size uv pos val :optional (endian (default-endian))) |
| 501 |
(case size |
| 502 |
[(1) (put-u8! uv pos val)] |
| 503 |
[(2) (put-u16! uv pos val endian)] |
| 504 |
[(4) (put-u32! uv pos val endian)] |
| 505 |
[(8) (put-u64! uv pos val endian)] |
| 506 |
[else (%put-int! size uv pos val endian)])) |
| 507 |
|
| 508 |
(define (put-sint! size uv pos val :optional (endian (default-endian))) |
| 509 |
(case size |
| 510 |
[(1) (put-s8! uv pos val)] |
| 511 |
[(2) (put-s16! uv pos val endian)] |
| 512 |
[(4) (put-s32! uv pos val endian)] |
| 513 |
[(8) (put-s64! uv pos val endian)] |
| 514 |
[else (%put-int! size uv pos val endian)])) |
| 515 |
|
| 516 |
;;; |
| 517 |
;;; Machine-dependent binary parameters |
| 518 |
;;; |
| 519 |
|
| 520 |
(inline-stub |
| 521 |
(define-cvar short_align::(.struct (b::char s::short)) :static) |
| 522 |
(define-cvar int_align::(.struct (b::char s::int)) :static) |
| 523 |
(define-cvar long_align::(.struct (b::char s::long)) :static) |
| 524 |
(define-cvar float_align::(.struct (b::char s::float)) :static) |
| 525 |
(define-cvar double_align::(.struct (b::char s::double)) :static) |
| 526 |
(define-cvar int8_align::(.struct (b::char s::int8_t)) :static) |
| 527 |
(define-cvar int16_align::(.struct (b::char s::int16_t)) :static) |
| 528 |
(define-cvar int32_align::(.struct (b::char s::int32_t)) :static) |
| 529 |
(define-cvar int64_align::(.struct (b::char s::int64_t)) :static) |
| 530 |
|
| 531 |
(.if (defined HAVE_LONG_LONG) |
| 532 |
(define-ctype long_long_::(long long)) |
| 533 |
(define-ctype long_long_::long)) |
| 534 |
(define-cvar long_long_align::(.struct (b::char s::long_long_)) :static) |
| 535 |
|
| 536 |
(.define alignof (str) (- (cast intptr_t (& (ref str s))) |
| 537 |
(cast intptr_t (& (ref str b))))) |
| 538 |
|
| 539 |
(define-cproc %primitive-type-info () |
| 540 |
(return |
| 541 |
(Scm_List |
| 542 |
(SCM_LIST3 'char |
| 543 |
(SCM_MAKE_INT (sizeof (char))) |
| 544 |
(SCM_MAKE_INT 1)) |
| 545 |
(SCM_LIST3 'short |
| 546 |
(SCM_MAKE_INT (sizeof (short))) |
| 547 |
(SCM_MAKE_INT (alignof short_align))) |
| 548 |
(SCM_LIST3 'int |
| 549 |
(SCM_MAKE_INT (sizeof (int))) |
| 550 |
(SCM_MAKE_INT (alignof int_align))) |
| 551 |
(SCM_LIST3 'long |
| 552 |
(SCM_MAKE_INT (sizeof (long))) |
| 553 |
(SCM_MAKE_INT (alignof long_align))) |
| 554 |
(SCM_LIST3 'longlong |
| 555 |
(SCM_MAKE_INT (sizeof (long_long_))) |
| 556 |
(SCM_MAKE_INT (alignof long_long_align))) |
| 557 |
(SCM_LIST3 'float |
| 558 |
(SCM_MAKE_INT (sizeof (float))) |
| 559 |
(SCM_MAKE_INT (alignof float_align))) |
| 560 |
(SCM_LIST3 'double |
| 561 |
(SCM_MAKE_INT (sizeof (double))) |
| 562 |
(SCM_MAKE_INT (alignof double_align))) |
| 563 |
(SCM_LIST3 'int8 |
| 564 |
(SCM_MAKE_INT (sizeof (int8_t))) |
| 565 |
(SCM_MAKE_INT (alignof int8_align))) |
| 566 |
(SCM_LIST3 'int16 |
| 567 |
(SCM_MAKE_INT (sizeof (int16_t))) |
| 568 |
(SCM_MAKE_INT (alignof int16_align))) |
| 569 |
(SCM_LIST3 'int32 |
| 570 |
(SCM_MAKE_INT (sizeof (int32_t))) |
| 571 |
(SCM_MAKE_INT (alignof int32_align))) |
| 572 |
(SCM_LIST3 'int64 |
| 573 |
(SCM_MAKE_INT (sizeof (int64_t))) |
| 574 |
(SCM_MAKE_INT (alignof int64_align))) |
| 575 |
NULL))) |
| 576 |
) |