-
Notifications
You must be signed in to change notification settings - Fork 1
/
endian-blob.scm
979 lines (770 loc) · 23.7 KB
/
endian-blob.scm
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
;;
;; Utility procedures for manipulating blobs in different endian
;; formats.
;;
;; Copyright 2009-2019 Ivan Raikov.
;;
;;
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; A full copy of the GPL license can be found at
;; <http://www.gnu.org/licenses/>.
;;
(module endian-blob
(endian-blob?
byte-blob->endian-blob
endian-blob->byte-blob
endian-blob-length
endian-blob->sint1
endian-blob->sint2
endian-blob->sint4
endian-blob->uint1
endian-blob->uint2
endian-blob->uint4
endian-blob->ieee_float32
endian-blob->ieee_float64
endian-blob->s8vector
endian-blob->s16vector
endian-blob->s32vector
endian-blob->u8vector
endian-blob->u16vector
endian-blob->u32vector
endian-blob->f32vector
endian-blob->f64vector
sint1->endian-blob
sint2->endian-blob
sint4->endian-blob
uint1->endian-blob
uint2->endian-blob
uint4->endian-blob
ieee_float32->endian-blob
ieee_float64->endian-blob
s8vector->endian-blob
s16vector->endian-blob
s32vector->endian-blob
u8vector->endian-blob
u16vector->endian-blob
u32vector->endian-blob
f32vector->endian-blob
f64vector->endian-blob
MSB LSB
)
(import scheme (chicken base) (chicken foreign)
srfi-4 byte-blob)
(define-record-type endian-blob
(make-endian-blob object mode )
endian-blob?
(object endian-blob-object )
(mode endian-blob-mode )
)
(define MSB 0)
(define LSB 1)
(define (byte-blob->endian-blob b . rest)
(let-optionals rest ((mode MSB))
(assert (byte-blob? b))
(make-endian-blob b mode)))
(define endian-blob->byte-blob endian-blob-object)
(define (endian-blob-length b)
(let ((bb (endian-blob->byte-blob b)))
(byte-blob-length bb)))
#>
#include "floatformat.h"
typedef enum { MSB, LSB } byte_order_t;
// Is the present machine mode MSB first?
int machine_mode_is_MSB (void)
{
union {
int i;
char c[sizeof(int)];
} x;
x.i = 1;
return (!(x.c[0] == 1));
}
// Is the given byte order MSB first?
int endian_mode_is_MSB(byte_order_t b)
{
return (b == MSB);
}
void cfrom_sint1 (char item, unsigned char *b, unsigned int offset, int mode)
{
b[offset] = item;
}
void cfrom_sint2 (short item, unsigned char *b, unsigned int offset, int mode)
{
if ( endian_mode_is_MSB(mode) )
{
b[offset+0] = (item>>8) & 0xff;
b[offset+1] = item & 0xff;
}
else
{
b[offset+0] = item & 0xff;
b[offset+1] = (item>>8) & 0xff;
}
}
void cfrom_sint4 (int item, unsigned char *b, unsigned int offset, int mode)
{
int i, k;
if ( endian_mode_is_MSB(mode) )
{
k = 24;
for(i=0; i < 4; i++)
{
b[offset+i] = (item >> k) & 0xff;
k -= 8;
}
}
else
{
for(i=0; i < 4; i++)
{
b[offset+i] = item & 0xff;
item >>= 8;
}
}
}
void cfrom_uint1 (unsigned char item, unsigned char *b, unsigned int offset, int mode)
{
b[offset] = item;
}
void cfrom_uint2 (unsigned short item, unsigned char *b, unsigned int offset, int mode)
{
if ( endian_mode_is_MSB(mode) )
{
b[offset+0] = (item>>8) & 0xff;
b[offset+1] = item & 0xff;
}
else
{
b[offset+0] = item & 0xff;
b[offset+1] = (item>>8) & 0xff;
}
}
void cfrom_uint4 (unsigned int item, unsigned char *b, unsigned int offset, int mode)
{
int i, k;
if ( endian_mode_is_MSB(mode) )
{
k = 24;
for(i=0; i < 4; i++)
{
b[offset+i] = (item >> k) & 0xff;
k -= 8;
}
}
else
{
for(i=0; i < 4; i++)
{
b[offset+i] = item & 0xff;
item >>= 8;
}
}
}
void cfrom_ieee_float32 (float item, unsigned char *b, unsigned int offset, int mode)
{
double d;
d = item;
if ( endian_mode_is_MSB(mode) )
{
double_to_floatformat (&floatformat_ieee_single_big, &d, b+offset);
}
else
{
double_to_floatformat (&floatformat_ieee_single_little, &d, b+offset);
}
}
void cfrom_ieee_float64 (double item, unsigned char *b, unsigned int offset, int mode)
{
double d;
d = item;
if ( endian_mode_is_MSB(mode) )
{
double_to_floatformat (&floatformat_ieee_double_big, &d, b+offset);
}
else
{
double_to_floatformat (&floatformat_ieee_double_little, &d, b+offset);
}
}
void cfrom_s8vector (unsigned int n, unsigned char *b, char *v, unsigned int offset, int mode)
{
memcpy((void *)(b+offset),(const void *)v,n);
}
void cfrom_s16vector (unsigned int n, unsigned char *b, short *v, unsigned int offset, int mode)
{
unsigned int i,p;
p = offset;
for (i=0; i<n; i++,p += 2)
{
cfrom_sint2 (v[i], b, p, mode);
}
}
void cfrom_s32vector (unsigned int n, unsigned char *b, int *v, unsigned int offset, int mode)
{
unsigned int i,p;
p = offset;
for (i=0; i<n; i++,p += 4)
{
cfrom_sint4 (v[i], b, p, mode);
}
}
void cfrom_u8vector (unsigned int n, unsigned char *b, unsigned char *v, unsigned int offset, int mode)
{
memcpy((void *)(b+offset),(const void *)v,n);
}
void cfrom_u16vector (unsigned int n, unsigned char *b, unsigned short *v, unsigned int offset, int mode)
{
unsigned int i,p;
p = offset;
for (i=0; i<n; i++,p += 2)
{
cfrom_uint2 (v[i], b, p, mode);
}
}
void cfrom_u32vector (unsigned int n, unsigned char *b, unsigned int *v, unsigned int offset, int mode)
{
unsigned int i,p;
p = offset;
for (i=0; i<n; i++,p += 4)
{
cfrom_uint4 (v[i], b, p, mode);
}
}
void cfrom_f32vector (unsigned int n, unsigned char *b, float *v, unsigned int offset, int mode)
{
unsigned int i,p;
p = offset;
for (i=0; i<n; i++,p += 4)
{
cfrom_ieee_float32 (v[i], b, p, mode);
}
}
void cfrom_f64vector (unsigned int n, unsigned char *b, double *v, unsigned int offset, int mode)
{
unsigned int i,p;
p = offset;
for (i=0; i<n; i++,p += 8)
{
cfrom_ieee_float64 (v[i], b, p, mode);
}
}
char cto_sint1 (unsigned char *b, unsigned int offset, int mode)
{
return b[offset];
}
short cto_sint2 (unsigned char *b, unsigned int offset, int mode)
{
short result;
if( endian_mode_is_MSB(mode) )
result = (b[offset+0] << 8) | b[offset+1];
else
result = (b[offset+1] << 8) | b[offset+0];
return (result);
}
int cto_sint4 (unsigned char *b, unsigned int offset, int mode)
{
int result;
if ( endian_mode_is_MSB(mode) )
result = (b[offset+0] << 24) | (b[offset+1] << 16) |
(b[offset+2] << 8) | (b[offset+3]);
else
result = (b[offset+3] << 24) | (b[offset+2] << 16) |
(b[offset+1] << 8) | (b[offset+0]);
return(result);
}
unsigned char cto_uint1 (unsigned char *b, unsigned int offset, int mode)
{
return b[offset];
}
unsigned short cto_uint2 (unsigned char *b, unsigned int offset, int mode)
{
unsigned short result;
if( endian_mode_is_MSB(mode) )
result = (b[offset+0] << 8) | b[offset+1];
else
result = (b[offset+1] << 8) | b[offset+0];
return (result);
}
unsigned int cto_uint4 (unsigned char *b, unsigned int offset, int mode)
{
unsigned int result;
if ( endian_mode_is_MSB(mode) )
result = (b[offset+0] << 24) | (b[offset+1] << 16) |
(b[offset+2] << 8) | (b[offset+3]);
else
result = (b[offset+3] << 24) | (b[offset+2] << 16) |
(b[offset+1] << 8) | (b[offset+0]);
return(result);
}
float cto_ieee_float32 (unsigned char *b, unsigned int offset, int mode)
{
ssize_t s;
double d;
const struct floatformat *fmt;
unsigned long exponent;
d = 0.0;
if ( endian_mode_is_MSB(mode) )
{
fmt = &floatformat_ieee_single_big;
}
else
{
fmt = &floatformat_ieee_single_little;
}
exponent = get_field ((unsigned char *)(b+offset), fmt->byteorder, fmt->totalsize,
fmt->exp_start, fmt->exp_len);
floatformat_to_double(fmt, b+offset, &d);
return((float)d);
}
double cto_ieee_float64 (unsigned char *b, unsigned int offset, int mode)
{
ssize_t s;
double d;
const struct floatformat *fmt;
unsigned long exponent;
d = 0.0;
if ( endian_mode_is_MSB(mode) )
{
fmt = &floatformat_ieee_double_big;
}
else
{
fmt = &floatformat_ieee_double_little;
}
exponent = get_field ((unsigned char *)(b+offset), fmt->byteorder, fmt->totalsize,
fmt->exp_start, fmt->exp_len);
floatformat_to_double(fmt, b+offset, &d);
return(d);
}
void cto_s8vector (unsigned int n, char *v, unsigned char *b, unsigned int offset, int mode)
{
memcpy((void *)v,(const void *)(b+offset),n);
}
void cto_s16vector (unsigned int n, short *v, unsigned char *b, unsigned int offset, int mode)
{
unsigned int i,p; short r;
p = offset;
for (i=0; i<n; i++,p += 2)
{
r = cto_sint2 (b, p, mode);
v[i] = r;
}
}
void cto_s32vector (unsigned int n, int *v, unsigned char *b, unsigned int offset, int mode)
{
unsigned int i,p; int r;
p = offset;
for (i=0; i<n; i++,p += 4)
{
r = cto_sint4 (b, p, mode);
v[i] = r;
}
}
void cto_u8vector (unsigned int n, unsigned char *v, unsigned char *b, unsigned int offset, int mode)
{
memcpy((void *)v,(const void *)(b+offset),n);
}
void cto_u16vector (unsigned int n, unsigned short *v, unsigned char *b, unsigned int offset, int mode)
{
unsigned int i,p; unsigned short r;
p = offset;
for (i=0; i<n; i++,p += 2)
{
r = cto_uint2 (b, p, mode);
v[i] = r;
}
}
void cto_u32vector (unsigned int n, unsigned int *v, unsigned char *b, unsigned int offset, int mode)
{
unsigned int i,p; unsigned int r;
p = offset;
for (i=0; i<n; i++,p += 4)
{
r = cto_uint4 (b, p, mode);
v[i] = r;
}
}
void cto_f32vector (unsigned int n, float *v, unsigned char *b, unsigned int offset, int mode)
{
unsigned int i,p; float r;
p = offset;
for (i=0; i<n; i++,p += 4)
{
r = cto_ieee_float32 (b, p, mode);
v[i] = r;
}
}
void cto_f64vector (unsigned int n, double *v, unsigned char *b, unsigned int offset, int mode)
{
unsigned int i,p; double r;
p = offset;
for (i=0; i<n; i++,p += 8)
{
r = cto_ieee_float64 (b, p, mode);
v[i] = r;
}
}
<#
(define machine_mode_is_MSB
(foreign-lambda bool "machine_mode_is_MSB" ))
(define from_sint1
(foreign-lambda void "cfrom_sint1" byte nonnull-blob unsigned-int integer))
(define from_sint2
(foreign-lambda void "cfrom_sint2" short nonnull-blob unsigned-int integer))
(define from_sint4
(foreign-lambda void "cfrom_sint4" int nonnull-blob unsigned-int integer))
(define from_uint1
(foreign-lambda void "cfrom_uint1" unsigned-byte nonnull-blob unsigned-int integer))
(define from_uint2
(foreign-lambda void "cfrom_uint2" unsigned-short nonnull-blob unsigned-int integer))
(define from_uint4
(foreign-lambda void "cfrom_uint4" unsigned-int nonnull-blob unsigned-int integer))
(define from_ieee_float32
(foreign-lambda void "cfrom_ieee_float32" float nonnull-blob unsigned-int integer ))
(define from_ieee_float64
(foreign-lambda void "cfrom_ieee_float64" double nonnull-blob unsigned-int integer ))
(define from_s8vector
(foreign-lambda void "cfrom_s8vector" unsigned-int nonnull-blob s8vector unsigned-int integer ))
(define from_s16vector
(foreign-lambda void "cfrom_s16vector" unsigned-int nonnull-blob s16vector unsigned-int integer))
(define from_s32vector
(foreign-lambda void "cfrom_s32vector" unsigned-int nonnull-blob s32vector unsigned-int integer))
(define from_u8vector
(foreign-lambda void "cfrom_u8vector" unsigned-int nonnull-blob u8vector unsigned-int integer ))
(define from_u16vector
(foreign-lambda void "cfrom_u16vector" unsigned-int nonnull-blob u16vector unsigned-int integer))
(define from_u32vector
(foreign-lambda void "cfrom_u32vector" unsigned-int nonnull-blob u32vector unsigned-int integer))
(define from_f32vector
(foreign-lambda void "cfrom_f32vector" unsigned-int nonnull-blob f32vector unsigned-int integer))
(define from_f64vector
(foreign-lambda void "cfrom_f64vector" unsigned-int nonnull-blob f64vector unsigned-int integer))
(define to_sint1
(foreign-lambda byte "cto_sint1" nonnull-blob unsigned-int integer))
(define to_sint2
(foreign-lambda short "cto_sint2" nonnull-blob unsigned-int integer))
(define to_sint4
(foreign-lambda int "cto_sint4" nonnull-blob unsigned-int integer))
(define to_uint1
(foreign-lambda unsigned-byte "cto_uint1" nonnull-blob unsigned-int integer))
(define to_uint2
(foreign-lambda unsigned-short "cto_uint2" nonnull-blob unsigned-int integer))
(define to_uint4
(foreign-lambda unsigned-int "cto_uint4" nonnull-blob unsigned-int integer))
(define to_ieee_float32
(foreign-lambda float "cto_ieee_float32" nonnull-blob unsigned-int integer ))
(define to_ieee_float64
(foreign-lambda double "cto_ieee_float64" nonnull-blob unsigned-int integer ))
(define to_s8vector
(foreign-lambda void "cto_s8vector" unsigned-int s8vector nonnull-blob unsigned-int integer ))
(define to_s16vector
(foreign-lambda void "cto_s16vector" unsigned-int s16vector nonnull-blob unsigned-int integer))
(define to_s32vector
(foreign-lambda void "cto_s32vector" unsigned-int s32vector nonnull-blob unsigned-int integer))
(define to_u8vector
(foreign-lambda void "cto_u8vector" unsigned-int u8vector nonnull-blob unsigned-int integer ))
(define to_u16vector
(foreign-lambda void "cto_u16vector" unsigned-int u16vector nonnull-blob unsigned-int integer))
(define to_u32vector
(foreign-lambda void "cto_u32vector" unsigned-int u32vector nonnull-blob unsigned-int integer))
(define to_f32vector
(foreign-lambda void "cto_f32vector" unsigned-int f32vector nonnull-blob unsigned-int integer))
(define to_f64vector
(foreign-lambda void "cto_f64vector" unsigned-int f64vector nonnull-blob unsigned-int integer))
(define machine-mode (if (machine_mode_is_MSB) MSB LSB))
(define (sint1->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (s8vector->blob/shared (s8vector n))))
(if (not (= machine-mode mode))
(from_sint1 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (sint2->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (s16vector->blob/shared (s16vector n))))
(if (not (= machine-mode mode))
(from_sint2 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (sint4->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (s32vector->blob/shared (s32vector n))))
(if (not (= machine-mode mode))
(from_sint4 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (uint1->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (u8vector->blob/shared (u8vector n))))
(if (not (= machine-mode mode))
(from_uint1 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (uint2->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (u16vector->blob/shared (u16vector n))))
(if (not (= machine-mode mode))
(from_uint2 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (uint4->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (u32vector->blob/shared (u32vector n))))
(if (not (= machine-mode mode))
(from_uint4 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (ieee_float32->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (f32vector->blob/shared (f32vector n))))
(if (not (= machine-mode mode))
(from_ieee_float32 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (ieee_float64->endian-blob n . rest)
(let-optionals rest ((mode MSB))
(let ((bb (f64vector->blob/shared (f64vector n))))
(if (not (= machine-mode mode))
(from_ieee_float64 n bb 0 mode))
(make-endian-blob (blob->byte-blob bb) mode))))
(define (s8vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (s8vector->byte-blob v) mode)
(let* ((n (s8vector-length v))
(bb (byte-blob-replicate n 0))
(b (make-endian-blob bb mode)))
(from_s8vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (s16vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (s16vector->byte-blob v) mode)
(let* ((n (s16vector-length v))
(bb (byte-blob-replicate (* 2 n) 0))
(b (make-endian-blob bb mode)))
(from_s16vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (s32vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (s32vector->byte-blob v) mode)
(let* ((n (s32vector-length v))
(bb (byte-blob-replicate (* 4 n) 0))
(b (make-endian-blob bb mode)))
(from_s32vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (u8vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (u8vector->byte-blob v) mode)
(let* ((n (u8vector-length v))
(bb (byte-blob-replicate n 0))
(b (make-endian-blob bb mode)))
(from_u8vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (u16vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (u16vector->byte-blob v) mode)
(let* ((n (u16vector-length v))
(bb (byte-blob-replicate (* 2 n) 0))
(b (make-endian-blob bb mode)))
(from_u16vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (u32vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (u32vector->byte-blob v) mode)
(let* ((n (u32vector-length v))
(bb (byte-blob-replicate (* 4 n) 0))
(b (make-endian-blob bb mode)))
(from_u32vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (f32vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (f32vector->byte-blob v) mode)
(let* ((n (f32vector-length v))
(bb (byte-blob-replicate (* 4 n) 0))
(b (make-endian-blob bb mode)))
(from_f32vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (f64vector->endian-blob v . rest)
(let-optionals rest ((mode MSB))
(if (= machine-mode mode)
(make-endian-blob (f64vector->byte-blob v) mode)
(let* ((n (f64vector-length v))
(bb (byte-blob-replicate (* 8 n) 0))
(b (make-endian-blob bb mode)))
(from_f64vector n (byte-blob->blob bb) v
(byte-blob-offset bb)
(endian-blob-mode b))
b))))
(define (endian-blob->sint1 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 1))
(to_sint1 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->sint2 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 2))
(to_sint2 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->sint4 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 4))
(to_sint4 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->uint1 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 1))
(to_uint1 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->uint2 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 2))
(to_uint2 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->uint4 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 4))
(to_uint4 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->ieee_float32 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 4))
(to_ieee_float32 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->ieee_float64 b)
(let ((bb (endian-blob->byte-blob b)))
(assert (= (byte-blob-length bb) 8))
(to_ieee_float64 (byte-blob->blob bb) (byte-blob-offset bb)
(endian-blob-mode b))))
(define (endian-blob->s8vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (positive? n))
(if (= machine-mode mode)
(byte-blob->s8vector bb)
(let ((v (make-s8vector n 0)))
(to_s8vector n v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
(define (endian-blob->s16vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (and (positive? n) (zero? (modulo n 2))))
(if (= machine-mode mode)
(byte-blob->s16vector bb)
(let* ((s (/ n 2))
(v (make-s16vector s 0)))
(to_s16vector s v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
(define (endian-blob->s32vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (and (positive? n) (zero? (modulo n 4))))
(if (= machine-mode mode)
(byte-blob->s32vector bb)
(let* ((s (/ n 4))
(v (make-s32vector s 0)))
(to_s32vector s v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
(define (endian-blob->u8vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (positive? n))
(if (= machine-mode mode)
(byte-blob->u8vector bb)
(let ((v (make-u8vector n 0)))
(to_u8vector n v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
(define (endian-blob->u16vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (and (positive? n) (zero? (modulo n 2))))
(if (= machine-mode mode)
(byte-blob->u16vector bb)
(let* ((s (/ n 2))
(v (make-u16vector s 0)))
(to_u16vector s v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
(define (endian-blob->u32vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (and (positive? n) (zero? (modulo n 4))))
(if (= machine-mode mode)
(byte-blob->u32vector bb)
(let* ((s (/ n 4))
(v (make-u32vector s 0)))
(to_u32vector s v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
(define (endian-blob->f32vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (and (positive? n) (zero? (modulo n 4))))
(if (= machine-mode mode)
(byte-blob->f32vector bb)
(let* ((s (/ n 4))
(v (make-f32vector s 0)))
(to_f32vector s v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
(define (endian-blob->f64vector b)
(let* ((bb (endian-blob->byte-blob b))
(mode (endian-blob-mode b))
(n (byte-blob-length bb)))
(assert (and (positive? n) (zero? (modulo n 8))))
(if (= machine-mode mode)
(byte-blob->f64vector bb)
(let* ((s (/ n 8))
(v (make-f64vector s 0)))
(to_f64vector s v
(byte-blob->blob bb)
(byte-blob-offset bb)
mode)
v))))
)