1 | |
---|
2 | ;; |
---|
3 | ;; An I/O port that supports different endian formats. |
---|
4 | ;; |
---|
5 | ;; Copyright 2005-2008, 2012 Ivan Raikov, Shawn Rutledge |
---|
6 | ;; Ported to Chicken 4 by Shawn Rutledge s@ecloud.org |
---|
7 | ;; |
---|
8 | ;; |
---|
9 | ;; This program is free software: you can redistribute it and/or |
---|
10 | ;; modify it under the terms of the GNU General Public License as |
---|
11 | ;; published by the Free Software Foundation, either version 3 of the |
---|
12 | ;; License, or (at your option) any later version. |
---|
13 | ;; |
---|
14 | ;; This program is distributed in the hope that it will be useful, but |
---|
15 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | ;; General Public License for more details. |
---|
18 | ;; |
---|
19 | ;; A full copy of the GPL license can be found at |
---|
20 | ;; <http://www.gnu.org/licenses/>. |
---|
21 | ;; |
---|
22 | |
---|
23 | (module endian-port |
---|
24 | (make-endian-port |
---|
25 | endian-port? |
---|
26 | endian-port-fileno |
---|
27 | endian-port-filename |
---|
28 | endian-port-byte-order |
---|
29 | close-endian-port |
---|
30 | open-endian-port |
---|
31 | port->endian-port |
---|
32 | endian-port-set-bigendian! |
---|
33 | endian-port-set-littlendian! |
---|
34 | endian-port-setpos |
---|
35 | endian-port-pos |
---|
36 | endian-port-eof? |
---|
37 | endian-port-read-int1 |
---|
38 | endian-port-read-int2 |
---|
39 | endian-port-read-int4 |
---|
40 | endian-port-read-uint1 |
---|
41 | endian-port-read-uint2 |
---|
42 | endian-port-read-uint4 |
---|
43 | endian-port-read-ieee-float32 |
---|
44 | endian-port-read-ieee-float64 |
---|
45 | endian-port-read-bit-vector |
---|
46 | endian-port-read-byte-vector |
---|
47 | endian-port-write-int1 |
---|
48 | endian-port-write-int2 |
---|
49 | endian-port-write-int4 |
---|
50 | endian-port-write-uint1 |
---|
51 | endian-port-write-uint2 |
---|
52 | endian-port-write-uint4 |
---|
53 | endian-port-write-ieee-float32 |
---|
54 | endian-port-write-ieee-float64 |
---|
55 | endian-port-write-bit-vector |
---|
56 | endian-port-write-byte-vector) |
---|
57 | |
---|
58 | (import chicken scheme posix iset srfi-4 endian-blob byte-blob) |
---|
59 | |
---|
60 | (use endian-blob) |
---|
61 | |
---|
62 | ;------------------------------------ |
---|
63 | ; Endian port data structures |
---|
64 | ; |
---|
65 | |
---|
66 | ; Structure: endian-port |
---|
67 | ; |
---|
68 | ; * fileno: file handle corresponding to the port |
---|
69 | ; * filename: file name corresponding to the port |
---|
70 | ; * byte-order: can be MSB or LSB (type defined in unit endian-blob) |
---|
71 | ; |
---|
72 | |
---|
73 | (define-record endian-port fileno filename byte-order) |
---|
74 | |
---|
75 | |
---|
76 | ;------------------------------------ |
---|
77 | ; Constants re-used from endian-blob |
---|
78 | ; |
---|
79 | ;(define MSB MSB) |
---|
80 | ;(define LSB LSB) |
---|
81 | |
---|
82 | ;------------------------------------ |
---|
83 | ; Endian port routines |
---|
84 | ; |
---|
85 | |
---|
86 | |
---|
87 | ; Procedure: |
---|
88 | ; close-endian-port:: ENDIAN-PORT -> UNDEFINED |
---|
89 | ; |
---|
90 | ; Closes the endian port. |
---|
91 | ; |
---|
92 | (define (close-endian-port eport) |
---|
93 | (file-close (endian-port-fileno eport))) |
---|
94 | |
---|
95 | ; Procedure: |
---|
96 | ; open-endian-port MODE FILENAME -> ENDIAN-PORT |
---|
97 | ; |
---|
98 | ; Opens an endian port to the specified file. Mode can be one of 'read |
---|
99 | ; or 'write. In write mode, the file is created if it doesn't exist, |
---|
100 | ; otherwise the new data is appended to its end. The default |
---|
101 | ; endianness of the newly created endian port is MSB. |
---|
102 | ; |
---|
103 | (define (open-endian-port mode filename) |
---|
104 | (cond ((eq? mode 'read) |
---|
105 | (let ((fd (file-open filename (bitwise-ior open/read open/binary)))) |
---|
106 | (if (< fd 0) |
---|
107 | (error 'endian-port "unable to open file: " filename) |
---|
108 | (make-endian-port fd filename MSB)))) |
---|
109 | (else |
---|
110 | (let ((fd (file-open filename (bitwise-ior open/append open/creat open/binary)))) |
---|
111 | (if (< fd 0) |
---|
112 | (error 'endian-port "unable to open file: " filename) |
---|
113 | (make-endian-port fd filename MSB)))))) |
---|
114 | |
---|
115 | ; Procedure: |
---|
116 | ; port->endian-port:: PORT -> ENDIAN-PORT |
---|
117 | ; |
---|
118 | ; Creates an endian port to the file specified by the given port. The |
---|
119 | ; default endianness of the newly created endian port is MSB. |
---|
120 | ; |
---|
121 | (define (port->endian-port port) |
---|
122 | (make-endian-port (port->fileno port) (port-name port) MSB)) |
---|
123 | |
---|
124 | |
---|
125 | ; Procedure: |
---|
126 | ; endian-port-set-bigendian!:: EPORT -> UNSPECIFIED |
---|
127 | ; |
---|
128 | ; Sets the endianness of the given endian port to MSB. |
---|
129 | ; |
---|
130 | (define (endian-port-set-bigendian! eport) |
---|
131 | (endian-port-byte-order-set! eport MSB)) |
---|
132 | |
---|
133 | ; Procedure: |
---|
134 | ; endian-port-set-littlendian!:: EPORT -> UNSPECIFIED |
---|
135 | ; |
---|
136 | ; Sets the endianness of the given endian port to LSB. |
---|
137 | ; |
---|
138 | (define (endian-port-set-littlendian! eport) |
---|
139 | (endian-port-byte-order-set! eport LSB)) |
---|
140 | |
---|
141 | |
---|
142 | ; Procedure: |
---|
143 | ; endian-port-setpos:: EPORT INTEGER [WHENCE] -> UNSPECIFIED |
---|
144 | ; |
---|
145 | ; Sets the file position of the given endian port to the specified |
---|
146 | ; position. The optional argument WHENCE is one of seek/set, seek/cur, |
---|
147 | ; seek/end. The default is seek/set (current position). |
---|
148 | ; |
---|
149 | (define (endian-port-setpos eport pos . rest) |
---|
150 | (let-optionals rest ((whence #f)) |
---|
151 | (cond ((not whence) |
---|
152 | (set-file-position! (endian-port-fileno eport) pos seek/set)) |
---|
153 | (else (set-file-position! (endian-port-fileno eport) pos whence))))) |
---|
154 | |
---|
155 | |
---|
156 | ; Procedure: |
---|
157 | ; endian-port-pos:: EPORT -> INTEGER |
---|
158 | ; |
---|
159 | ; Returns the current file position of the given endian port, relative |
---|
160 | ; to the beginning of the file. |
---|
161 | ; |
---|
162 | (define (endian-port-pos eport) |
---|
163 | (file-position (endian-port-fileno eport))) |
---|
164 | |
---|
165 | |
---|
166 | ; Procedure: |
---|
167 | ; endian-port-eof?:: EPORT -> BOOLEAN |
---|
168 | ; |
---|
169 | ; Returns true if the current file position of the given endian port |
---|
170 | ; is at the end of the file, false otherwise. |
---|
171 | ; |
---|
172 | (define (endian-port-eof? eport) |
---|
173 | (zero? (- (file-size (endian-port-fileno eport)) |
---|
174 | (file-position (endian-port-fileno eport))))) |
---|
175 | |
---|
176 | |
---|
177 | ; Procedure: |
---|
178 | ; endian-port-read-uint1:: EPORT [* BYTE-ORDER] -> UINTEGER |
---|
179 | ; |
---|
180 | ; Reads an unsigned integer of size 1 byte. Optional argument |
---|
181 | ; BYTE-ORDER is one of MSB or LSB. If byte order is not specified, |
---|
182 | ; then use the byte order setting of the given endian port. |
---|
183 | ; |
---|
184 | (define (endian-port-read-uint1 eport . rest) |
---|
185 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
186 | (let* ( [buf (make-blob 1)] |
---|
187 | [ret (file-read (endian-port-fileno eport) 1 buf)]) |
---|
188 | (and (= (cadr ret) 1) (endian-blob->uint1 (byte-blob->endian-blob |
---|
189 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
190 | |
---|
191 | |
---|
192 | ; Procedure: |
---|
193 | ; endian-port-read-uint2:: EPORT [* BYTE-ORDER] -> UINTEGER. |
---|
194 | ; |
---|
195 | ; Reads an unsigned integer of size 2 bytes. Optional argument |
---|
196 | ; BYTE-ORDER is one of MSB or LSB. If byte order is not specified, |
---|
197 | ; then use the byte order setting of the given endian port. |
---|
198 | ; |
---|
199 | (define (endian-port-read-uint2 eport . rest) |
---|
200 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
201 | (let* ( [buf (make-blob 2)] |
---|
202 | [ret (file-read (endian-port-fileno eport) 2 buf)]) |
---|
203 | (and (= (cadr ret) 2) (endian-blob->uint2 (byte-blob->endian-blob |
---|
204 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
205 | |
---|
206 | |
---|
207 | ; Procedure: |
---|
208 | ; endian-port-read-uint4:: EPORT [* BYTE-ORDER] -> UINTEGER |
---|
209 | ; |
---|
210 | ; Reads an unsigned integer of size 4 bytes. Optional argument |
---|
211 | ; BYTE-ORDER is one of MSB or LSB. If byte order is not specified, |
---|
212 | ; then use the byte order setting of the given endian port. |
---|
213 | ; |
---|
214 | (define (endian-port-read-uint4 eport . rest) |
---|
215 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
216 | (let* ( [buf (make-blob 4)] |
---|
217 | [ret (file-read (endian-port-fileno eport) 4 buf)]) |
---|
218 | (and (= (cadr ret) 4) (endian-blob->uint4 (byte-blob->endian-blob |
---|
219 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
220 | |
---|
221 | ; Procedure: |
---|
222 | ; endian-port-read-int1:: EPORT [* BYTE-ORDER] -> INTEGER |
---|
223 | ; |
---|
224 | ; Reads a signed integer of size 1 byte. Optional argument |
---|
225 | ; BYTE-ORDER is one of MSB or LSB. If byte order is not specified, |
---|
226 | ; then use the byte order setting of the given endian port. |
---|
227 | ; |
---|
228 | (define (endian-port-read-int1 eport . rest) |
---|
229 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
230 | (let* ( [buf (make-blob 1)] |
---|
231 | [ret (file-read (endian-port-fileno eport) 1 buf)]) |
---|
232 | (and (= (cadr ret) 1) (endian-blob->sint1 (byte-blob->endian-blob |
---|
233 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
234 | |
---|
235 | |
---|
236 | ; Procedure: |
---|
237 | ; endian-port-read-int2:: EPORT [* BYTE-ORDER] -> INTEGER. |
---|
238 | ; |
---|
239 | ; Reads a signed integer of size 2 bytes. Optional argument |
---|
240 | ; BYTE-ORDER is one of MSB or LSB. If byte order is not specified, |
---|
241 | ; then use the byte order setting of the given endian port. |
---|
242 | ; |
---|
243 | (define (endian-port-read-int2 eport . rest) |
---|
244 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
245 | (let* ( [buf (make-blob 2)] |
---|
246 | [ret (file-read (endian-port-fileno eport) 2 buf)]) |
---|
247 | (and (= (cadr ret) 2) (endian-blob->sint2 (byte-blob->endian-blob |
---|
248 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
249 | |
---|
250 | |
---|
251 | ; Procedure: |
---|
252 | ; endian-port-read-int4:: EPORT [* BYTE-ORDER] -> INTEGER |
---|
253 | ; |
---|
254 | ; Reads a signed integer of size 4 bytes. Optional argument |
---|
255 | ; BYTE-ORDER is one of MSB or LSB. If byte order is not specified, |
---|
256 | ; then use the byte order setting of the given endian port. |
---|
257 | ; |
---|
258 | (define (endian-port-read-int4 eport . rest) |
---|
259 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
260 | (let* ( [buf (make-blob 4)] |
---|
261 | [ret (file-read (endian-port-fileno eport) 4 buf)]) |
---|
262 | (and (= (cadr ret) 4) (endian-blob->sint4 (byte-blob->endian-blob |
---|
263 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
264 | |
---|
265 | ; Procedure: |
---|
266 | ; |
---|
267 | ; endian-port-read-ieee-float32:: EPORT [* BYTE-ORDER] -> REAL |
---|
268 | ; |
---|
269 | ; Reads an IEEE 754 single precision floating-point number. Optional |
---|
270 | ; argument BYTE-ORDER is one of MSB or LSB. If byte order is not |
---|
271 | ; specified, then use the byte order setting of the given endian port. |
---|
272 | ; |
---|
273 | (define (endian-port-read-ieee-float32 eport . rest) |
---|
274 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
275 | (let* ( [buf (make-blob 4)] |
---|
276 | [ret (file-read (endian-port-fileno eport) 4 buf)]) |
---|
277 | (and (= (cadr ret) 4) |
---|
278 | (endian-blob->ieee_float32 |
---|
279 | (byte-blob->endian-blob |
---|
280 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
281 | |
---|
282 | ; Procedure: |
---|
283 | ; |
---|
284 | ; endian-port-read-ieee-float32:: EPORT [* BYTE-ORDER] -> REAL |
---|
285 | ; |
---|
286 | ; Reads an IEEE 754 double precision floating-point number. Optional |
---|
287 | ; argument BYTE-ORDER is one of MSB or LSB. If byte order is not |
---|
288 | ; specified, then use the byte order setting of the given endian port. |
---|
289 | ; |
---|
290 | (define (endian-port-read-ieee-float64 eport . rest) |
---|
291 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
292 | (let* ( [buf (make-blob 8)] |
---|
293 | [ret (file-read (endian-port-fileno eport) 8 buf)] ) |
---|
294 | (and (= (cadr ret) 8) |
---|
295 | (endian-blob->ieee_float64 |
---|
296 | (byte-blob->endian-blob |
---|
297 | (blob->byte-blob (car ret)) byte-order) ) )))) |
---|
298 | |
---|
299 | |
---|
300 | ; Procedure: |
---|
301 | ; set-bv! |
---|
302 | ; |
---|
303 | ; Helper function for endian-port-read-bit-vector below |
---|
304 | ; * set len bits in vector bv starting at position pos, |
---|
305 | ; according to the bits set in vector b |
---|
306 | ; |
---|
307 | ; * len may not be larger than 8 |
---|
308 | ; |
---|
309 | (define (set-bv! bv b pos len) |
---|
310 | (let loop ((i (- len 1)) (j 0)) |
---|
311 | (begin |
---|
312 | (bit-vector-set! bv (- pos j) (bit-vector-ref b i)) |
---|
313 | (if (= i 0) bv (loop (- i 1) (+ j 1)))))) |
---|
314 | |
---|
315 | |
---|
316 | ; Procedure: |
---|
317 | ; endian-port-read-bit-vector:: PORT * SIZE (in bits) [* BYTE-ORDER] -> BIT-VECTOR |
---|
318 | ; |
---|
319 | ; Reads a bit vector of the specified size (in bits) and returns an |
---|
320 | ; iset bit vector (see module iset). Optional argument BYTE-ORDER is |
---|
321 | ; one of MSB or LSB. If byte order is not specified, then use the |
---|
322 | ; byte order setting of the given endian port. |
---|
323 | ; |
---|
324 | (define (endian-port-read-bit-vector eport size . rest) |
---|
325 | (let-optionals |
---|
326 | rest ((byte-order #f)) |
---|
327 | (let ((nb (inexact->exact (ceiling (/ size 8)))) |
---|
328 | (bv (make-bit-vector size)) |
---|
329 | (byte-order (if byte-order byte-order (endian-port-byte-order eport)))) |
---|
330 | (cond |
---|
331 | ((eq? byte-order MSB) |
---|
332 | ;; if big engian, we start with the most significant bit |
---|
333 | (let loop ((b (integer->bit-vector (endian-port-read-int1 eport))) |
---|
334 | (bi (- nb 1)) |
---|
335 | (pos (- size 1)) |
---|
336 | (rem size)) |
---|
337 | (begin |
---|
338 | (set-bv! bv b pos (min rem 8)) |
---|
339 | (if (= bi 0) bv |
---|
340 | (loop (integer->bit-vector (endian-port-read-int1 eport)) |
---|
341 | (- bi 1) |
---|
342 | (- pos 8) |
---|
343 | (- rem 8)))))) |
---|
344 | (else |
---|
345 | ;; if little endian, we start with the least significant bit |
---|
346 | (let loop ((b (integer->bit-vector (endian-port-read-int1 eport))) |
---|
347 | (bi (- nb 1)) |
---|
348 | (pos 7) |
---|
349 | (rem size)) |
---|
350 | (begin |
---|
351 | (set-bv! bv b pos (min rem 8)) |
---|
352 | (if (= bi 0) bv |
---|
353 | (loop (integer->bit-vector (endian-port-read-int1 eport)) |
---|
354 | (- bi 1) |
---|
355 | (+ pos 8) |
---|
356 | (- rem 8)))))))))) |
---|
357 | |
---|
358 | ; Procedure: |
---|
359 | ; endian-port-read-byte-vector:: PORT * SIZE [* BYTE-ORDER] -> BYTE-VECTOR |
---|
360 | ; |
---|
361 | ; Reads an unsigned byte vector of the specified size and returns a Scheme byte |
---|
362 | ; vector. Optional argument BYTE-ORDER is one of MSB or LSB. If |
---|
363 | ; byte order is not specified, then use the byte order setting of the |
---|
364 | ; given endian port. |
---|
365 | ; |
---|
366 | (define (endian-port-read-byte-vector eport size . rest) |
---|
367 | (let-optionals rest ((byte-order #f)) |
---|
368 | (let ((byte-order (if byte-order byte-order |
---|
369 | (endian-port-byte-order eport)))) |
---|
370 | (let loop ((size size) (data '())) |
---|
371 | (if (> size 0) |
---|
372 | (let ((b (endian-port-read-uint1 eport))) |
---|
373 | (loop (- size 1) (cons b data))) |
---|
374 | (if (eq? byte-order MSB) |
---|
375 | (u8vector->blob (list->u8vector (reverse data))) |
---|
376 | (u8vector->blob (list->u8vector data)))))))) |
---|
377 | |
---|
378 | |
---|
379 | |
---|
380 | ; Procedure: |
---|
381 | ; endian-port-write-uint1:: EPORT * WORD [* BYTE-ORDER] -> UINTEGER |
---|
382 | ; |
---|
383 | ; Writes an unsigned integer of size 1 byte. Returns the number of |
---|
384 | ; bytes written (always 1). Optional argument BYTE-ORDER is one of |
---|
385 | ; MSB or LSB. If byte order is not specified, then use the byte |
---|
386 | ; order setting of the given endian port. |
---|
387 | ; |
---|
388 | (define (endian-port-write-uint1 eport word . rest) |
---|
389 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
390 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (uint1->endian-blob word byte-order)))]) |
---|
391 | (file-write (endian-port-fileno eport) buf)))) |
---|
392 | |
---|
393 | ; Procedure: |
---|
394 | ; endian-port-write-uint2:: EPORT * WORD [* BYTE-ORDER] -> UINTEGER |
---|
395 | ; |
---|
396 | ; Writes an unsigned integer of size 2 bytes. Returns the number of |
---|
397 | ; bytes written (always 2). Optional argument BYTE-ORDER is one of |
---|
398 | ; MSB or LSB. If byte order is not specified, then use the byte |
---|
399 | ; order setting of the given endian port. |
---|
400 | ; |
---|
401 | (define (endian-port-write-uint2 eport word . rest) |
---|
402 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
403 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (uint2->endian-blob word byte-order)))]) |
---|
404 | (file-write (endian-port-fileno eport) buf)))) |
---|
405 | |
---|
406 | ; Procedure: |
---|
407 | ; endian-port-write-uint4:: EPORT * WORD [* BYTE-ORDER] -> UINTEGER |
---|
408 | ; |
---|
409 | ; Writes an unsigned integer of size 4 bytes. Returns the number of |
---|
410 | ; bytes written (always 4). Optional argument BYTE-ORDER is one of |
---|
411 | ; MSB or LSB. If byte order is not specified, then use the byte |
---|
412 | ; order setting of the given endian port. |
---|
413 | ; |
---|
414 | (define (endian-port-write-uint4 eport word . rest) |
---|
415 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
416 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (uint4->endian-blob word byte-order)))]) |
---|
417 | (file-write (endian-port-fileno eport) buf)))) |
---|
418 | |
---|
419 | ; Procedure: |
---|
420 | ; Procedure: |
---|
421 | ; endian-port-write-int1:: EPORT * WORD [* BYTE-ORDER] -> INTEGER |
---|
422 | ; |
---|
423 | ; Writes a signed integer of size 1 byte. Returns the number of |
---|
424 | ; bytes written (always 1). Optional argument BYTE-ORDER is one of |
---|
425 | ; MSB or LSB. If byte order is not specified, then use the byte |
---|
426 | ; order setting of the given endian port. |
---|
427 | ; |
---|
428 | (define (endian-port-write-int1 eport word . rest) |
---|
429 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
430 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (sint1->endian-blob word byte-order)))]) |
---|
431 | (file-write (endian-port-fileno eport) buf)))) |
---|
432 | |
---|
433 | ; Procedure: |
---|
434 | ; endian-port-write-int2:: EPORT * WORD [* BYTE-ORDER] -> INTEGER |
---|
435 | ; |
---|
436 | ; Writes a signed integer of size 2 bytes. Returns the number of |
---|
437 | ; bytes written (always 2). Optional argument BYTE-ORDER is one of |
---|
438 | ; MSB or LSB. If byte order is not specified, then use the byte |
---|
439 | ; order setting of the given endian port. |
---|
440 | ; |
---|
441 | (define (endian-port-write-int2 eport word . rest) |
---|
442 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
443 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (sint2->endian-blob word byte-order)))]) |
---|
444 | (file-write (endian-port-fileno eport) buf)))) |
---|
445 | |
---|
446 | ; Procedure: |
---|
447 | ; endian-port-write-int4:: EPORT * WORD [* BYTE-ORDER] -> INTEGER |
---|
448 | ; |
---|
449 | ; Writes a signed integer of size 4 bytes. Returns the number of |
---|
450 | ; bytes written (always 4). Optional argument BYTE-ORDER is one of |
---|
451 | ; MSB or LSB. If byte order is not specified, then use the byte |
---|
452 | ; order setting of the given endian port. |
---|
453 | ; |
---|
454 | (define (endian-port-write-int4 eport word . rest) |
---|
455 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
456 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (sint4->endian-blob word byte-order)))]) |
---|
457 | (file-write (endian-port-fileno eport) buf)))) |
---|
458 | |
---|
459 | ; Procedure: |
---|
460 | ; endian-port-write-ieee-float32:: EPORT * WORD [* BYTE-ORDER] -> UINTEGER |
---|
461 | ; |
---|
462 | ; Writes an IEEE 754 single precision floating-point number. Returns |
---|
463 | ; the number of bytes written (always 4). Optional argument BYTE-ORDER |
---|
464 | ; is one of MSB or LSB. If byte order is not specified, then use |
---|
465 | ; the byte order setting of the given endian port. |
---|
466 | ; |
---|
467 | (define (endian-port-write-ieee-float32 eport word . rest) |
---|
468 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
469 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (ieee_float32->endian-blob word byte-order)))]) |
---|
470 | ;(let ([testval (endian-blob->ieee_float32 (byte-blob->endian-blob (blob->byte-blob buf) byte-order)) ]) |
---|
471 | ; (unless (eq? testval word) (printf "trying to write float ~a, but after conversions it turns out as ~a~%" word testval) ) ) |
---|
472 | (file-write (endian-port-fileno eport) buf)))) |
---|
473 | |
---|
474 | ; Procedure: |
---|
475 | ; endian-port-write-ieee-float64:: EPORT * WORD [* BYTE-ORDER] -> UINTEGER |
---|
476 | ; |
---|
477 | ; Writes an IEEE 754 double precision floating-point number. Returns |
---|
478 | ; the number of bytes written (always 8). Optional argument BYTE-ORDER |
---|
479 | ; is one of MSB or LSB. If byte order is not specified, then use |
---|
480 | ; the byte order setting of the given endian port. |
---|
481 | ; |
---|
482 | (define (endian-port-write-ieee-float64 eport word . rest) |
---|
483 | (let-optionals rest ([byte-order (endian-port-byte-order eport)]) |
---|
484 | (let* ( [buf (u8vector->blob (endian-blob->u8vector (ieee_float64->endian-blob word byte-order)))]) |
---|
485 | ;(printf "trying to write float ~a, but after conversions it turns out as ~a~%" word |
---|
486 | ; (endian-blob->ieee_float64 (byte-blob->endian-blob (blob->byte-blob buf) byte-order) ) |
---|
487 | (file-write (endian-port-fileno eport) buf)))) |
---|
488 | |
---|
489 | ; Procedure: |
---|
490 | ; endian-port-write-byte-vector:: PORT * BYTE-VECTOR [* BYTE-ORDER] -> UINTEGER |
---|
491 | ; |
---|
492 | ; Writes the given unsigned byte vector and returns the number of bytes |
---|
493 | ; written. The argument must be a byte vector object. Optional |
---|
494 | ; argument BYTE-ORDER is one of MSB or LSB. If byte order is not |
---|
495 | ; specified, then use the byte order setting of the given endian port. |
---|
496 | ; |
---|
497 | (define (endian-port-write-byte-vector eport vect . rest) |
---|
498 | (let-optionals rest ((byte-order #f)) |
---|
499 | (let* ([u8vect (cond [(blob? vect) (blob->u8vector vect)] |
---|
500 | [(u8vector? vect) vect] |
---|
501 | [(endian-blob? vect) (endian-blob->u8vector vect)])] |
---|
502 | [len (u8vector-length u8vect)] |
---|
503 | [byte-order (if byte-order byte-order |
---|
504 | (endian-port-byte-order eport))]) |
---|
505 | (if (eq? byte-order MSB) |
---|
506 | (let loop ((i 0) (bytes 0)) |
---|
507 | (if (< i len) |
---|
508 | (loop (+ i 1) (+ bytes (endian-port-write-int1 eport (u8vector-ref u8vect i)))) |
---|
509 | bytes)) |
---|
510 | (let loop ((i (- len 1)) (bytes 0)) |
---|
511 | (if (positive? i) |
---|
512 | (loop (- i 1) (+ bytes (endian-port-write-int1 eport (u8vector-ref u8vect i)))) |
---|
513 | bytes)))))) |
---|
514 | |
---|
515 | ; |
---|
516 | ; Procedure: |
---|
517 | ; endian-port-write-bit-vector:: PORT * BIT-VECTOR [* BIT-ORDER] -> UINTEGER |
---|
518 | ; |
---|
519 | ; Writes the given bit vector and returns the number of bytes |
---|
520 | ; written. The argument must be a bit vector as defined in the iset |
---|
521 | ; module. Optional argument BIT-ORDER is one of MSB or LSB. If |
---|
522 | ; bit order is not specified, then use the byte order setting of the |
---|
523 | ; given endian port. |
---|
524 | ; |
---|
525 | ; Note that here the "byte order" type is interpreted as bit order: |
---|
526 | ; i.e. the bits are ordered in MSB or LSB order, not the bytes that |
---|
527 | ; comprise the bit vector. |
---|
528 | ; |
---|
529 | (define (endian-port-write-bit-vector eport bv . rest) |
---|
530 | (let-optionals rest ((byte-order #f)) |
---|
531 | (let ((len (bit-vector-length bv)) |
---|
532 | (byte-order (if byte-order byte-order |
---|
533 | (endian-port-byte-order eport)))) |
---|
534 | (if (eq? byte-order LSB) |
---|
535 | (let loop ((i 0) (bytes 0)) |
---|
536 | (if (< i len) |
---|
537 | (let ((byte (bitwise-ior (if (bit-vector-ref i bv) #b10000000 0) |
---|
538 | (if (bit-vector-ref (+ i 1) bv) #b01000000 0) |
---|
539 | (if (bit-vector-ref (+ i 2) bv) #b00100000 0) |
---|
540 | (if (bit-vector-ref (+ i 3) bv) #b00010000 0) |
---|
541 | (if (bit-vector-ref (+ i 4) bv) #b00001000 0) |
---|
542 | (if (bit-vector-ref (+ i 5) bv) #b00000100 0) |
---|
543 | (if (bit-vector-ref (+ i 6) bv) #b00000010 0) |
---|
544 | (if (bit-vector-ref (+ i 7) bv) #b00000001 0)))) |
---|
545 | (loop (+ i 8) (+ bytes (endian-port-write-int1 eport byte)))) |
---|
546 | bytes)) |
---|
547 | (let loop ((i (- len 1)) (bytes 0)) |
---|
548 | (if (positive? i) |
---|
549 | (let ((byte (bitwise-ior (if (bit-vector-ref i bv) #b00000001 0) |
---|
550 | (if (bit-vector-ref (- i 1) bv) #b00000010 0) |
---|
551 | (if (bit-vector-ref (- i 2) bv) #b00000100 0) |
---|
552 | (if (bit-vector-ref (- i 3) bv) #b00001000 0) |
---|
553 | (if (bit-vector-ref (- i 4) bv) #b00010000 0) |
---|
554 | (if (bit-vector-ref (- i 5) bv) #b00100000 0) |
---|
555 | (if (bit-vector-ref (- i 6) bv) #b01000000 0) |
---|
556 | (if (bit-vector-ref (- i 7) bv) #b10000000 0)))) |
---|
557 | (loop (- i 8) (+ bytes (endian-port-write-int1 eport byte))) |
---|
558 | bytes))))))) |
---|
559 | ) ;; end of module |
---|
560 | |
---|
561 | |
---|