-
Notifications
You must be signed in to change notification settings - Fork 2
/
binf.cljc
825 lines (412 loc) · 14 KB
/
binf.cljc
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
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(ns helins.binf
"Core namespace for handling views.
See README for an overview."
{:author "Adam Helins"}
(:require [helins.binf.buffer :as binf.buffer]
[helins.binf.protocol :as binf.protocol]
[helins.binf.protocol.impl]
[helins.binf.string :as binf.string]))
(declare remaining)
;;;;;;;;;; Pointing to protocol implementations
;;;;; IAbsoluteReader
(defn ra-buffer
"Reads `n-byte` bytes from an absolute `position` and returns them in a new buffer or in the
given `buffer` at the specified `offset` (defaults to 0)."
([this position n-byte]
(binf.protocol/ra-buffer this
position
n-byte
(binf.buffer/alloc n-byte)
0))
([this position n-byte buffer]
(binf.protocol/ra-buffer this
position
n-byte
buffer
0))
([view position n-byte buffer offset]
(binf.protocol/ra-buffer view
position
n-byte
buffer
offset)))
(defn ra-u8
"Reads an unsigned 8-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-u8 view
position))
(defn ra-i8
"Reads a signed 8-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-i8 view
position))
(defn ra-u16
"Reads an unsigned 16-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-u16 view
position))
(defn ra-i16
"Reads a signed 16-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-i16 view
position))
(defn ra-u32
"Reads an unsigned 32-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-u32 view
position))
(defn ra-i32
"Reads a signed 32-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-i32 view
position))
(defn ra-u64
"Reads an unsigned 64-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-u64 view
position))
(defn ra-i64
"Reads a signed 64-bit integer from an absolute `position`."
[view position]
(binf.protocol/ra-i64 view
position))
(defn ra-f32
"Reads a 32-bit float from an absolute `position`."
[view position]
(binf.protocol/ra-f32 view
position))
(defn ra-f64
"Reads a 64-bit float from an absolute `position`."
[view position]
(binf.protocol/ra-f64 view
position))
(defn ra-string
"Reads a string consisting of `n-byte` bytes from an absolute `position`.
A string decoder may be provided (default is UTF-8).
See [[helins.binf.string/decoder]]."
([view position n-byte]
(binf.protocol/ra-string view
binf.string/decoder-utf-8
position
n-byte))
([view decoder position n-byte]
(binf.protocol/ra-string view
decoder
position
n-byte)))
;;;;; IAbsoluteWriter
(defn wa-buffer
"Copies the given `buffer` to an absolute `position`.
An `offset` in the buffer as well as a number of bytes to copy (`n-byte`) may be provided."
([view position buffer]
(binf.protocol/wa-buffer view
position
buffer
0
(count buffer)))
([view position buffer offset]
(binf.protocol/wa-buffer view
position
buffer
offset
(- (count buffer)
offset)))
([view position buffer offset n-byte]
(binf.protocol/wa-buffer view
position
buffer
offset
n-byte)))
(defn wa-b8
"Writes an 8-bit integer to an absolute position."
[view position int]
(binf.protocol/wa-b8 view
position
int))
(defn wa-b16
"Writes a 16-bit integer to an absolute `position`."
[view position int]
(binf.protocol/wa-b16 view
position
int))
(defn wa-b32
"Writes a 32-bit integer to an absolute `position`."
[view position int]
(binf.protocol/wa-b32 view
position
int))
(defn wa-b64
"Writes a 64-bit integer to an absolute `position`."
[view position int64]
(binf.protocol/wa-b64 view
position
int64))
(defn wa-f32
"Writes a 32-bit float to an absolute `position`."
[view position floating]
(binf.protocol/wa-f32 view
position
floating))
(defn wa-f64
"Writes a 64-bit float to an absolute `position`."
[view position floating]
(binf.protocol/wa-f64 view
position
floating))
(defn wa-string
"Writes a string (encoded as UTF-8) to an absolute `position`.
Unlike other `wa-*` functions which are implemented as a fluent interface, this function returns
a tuple indicating how many bytes and chars have been written, and if the process is finished:
`[finished? n-byte n-chars]`.
With that information, the user can continue writing if needed. On the JVM, the tuple contains a 4th
item which is a `CharBuffer` containing the rest of the unwritten string which can be passed in place
of the `string` argument."
[view position string]
(binf.protocol/wa-string view
position
string))
;;;;; IBackingBuffer
(defn backing-buffer
"Returns the buffer of `view` if it does wrap one, nil otherwise.
It is possible to implement some protocols of this library over something that does not have a backing buffer.
Actually, on the JVM, a `DirectByteBuffer` is an example of a supported view which operates over native memory
instead of a Java byte array (ie. a \"buffer\")."
[view]
(binf.protocol/backing-buffer view))
(defn buffer-offset
"Returns the offset relative to the backing buffer, nil if there is not any.
See [[backing-buffer]] for background."
[view]
(binf.protocol/buffer-offset view))
;;;;; IEndianess
(defn endian-get
"Returns the endianess of the given `view`, either `:big-endian` or `:little-endian`."
[view]
(binf.protocol/endian-get view))
(defn endian-set
"Sets the endianess of the given `view`, either `:big-endian` or `:little-endian`."
[view endianess]
(binf.protocol/endian-set view
endianess))
;;;;; IGrow
(defn grow
"Returns a new view with `n-additional-byte` added, copying entirely the given `view`.
It is particularly useful when writing data of yet unknown length, providing the ability of
growing as needed."
[view n-additional-byte]
(binf.protocol/grow view
n-additional-byte))
;;;;; IRelativeReader
(defn rr-buffer
"Reads `n-byte` and returns them in a new buffer or in the given one at the specified `offset`
(defaults to 0)."
([view n-byte]
(binf.protocol/rr-buffer view
n-byte
(binf.buffer/alloc n-byte)
0))
([view n-byte buffer]
(binf.protocol/rr-buffer view
n-byte
buffer
0))
([view n-byte buffer offset]
(binf.protocol/rr-buffer view
n-byte
buffer
offset)))
(defn rr-u8
"Reads an unsigned 8-bit integer from the current position."
[view]
(binf.protocol/rr-u8 view))
(defn rr-i8
"Reads a signed 8-bit integer from the current position."
[view]
(binf.protocol/rr-i8 view))
(defn rr-u16
"Reads an unsigned 16-bit integer from the current position."
[view]
(binf.protocol/rr-u16 view))
(defn rr-i16
"Reads a signed 16-bit integer from the current position."
[view]
(binf.protocol/rr-i16 view))
(defn rr-u32
"Reads an unsigned 32-bit integer from the current position."
[view]
(binf.protocol/rr-u32 view))
(defn rr-i32
"Reads a signed 32-bit integer from the current position."
[view]
(binf.protocol/rr-i32 view))
(defn rr-u64
"Reads an unsigned 64-bit integer from the current position."
[view]
(binf.protocol/rr-u64 view))
(defn rr-i64
"Reads a signed 64-bit integer from the current position."
[view]
(binf.protocol/rr-i64 view))
(defn rr-f32
"Reads a 32-bit float from the current position."
[view]
(binf.protocol/rr-f32 view))
(defn rr-f64
"Reads a 64-bit float from the current position."
[view]
(binf.protocol/rr-f64 view))
(defn rr-string
"Reads a string consisting of `n-byte` bytes from the current position.
A non-nil decoder may be provided (default is UTF-8).
See [[helins.binf.string/decoder]]."
([view n-byte]
(binf.protocol/rr-string view
binf.string/decoder-utf-8
n-byte))
([view decoder n-byte]
(binf.protocol/rr-string view
decoder
n-byte)))
;;;;; IRelativeWriter
(defn wr-buffer
"Copies the given `buffer` to the current position.
An `offset` in the buffer as well as a number of bytes to copy (`n-byte`) may be provided."
([view buffer]
(binf.protocol/wr-buffer view
buffer
0
(count buffer)))
([view buffer offset]
(binf.protocol/wr-buffer view
buffer
offset
(- (count buffer)
offset)))
([view buffer offset n-byte]
(binf.protocol/wr-buffer view
buffer
offset
n-byte)))
(defn wr-b8
"Writes an 8-bit integer to the current position."
[view int]
(binf.protocol/wr-b8 view
int))
(defn wr-b16
"Writes a 16-bit integer to the current position."
[view int]
(binf.protocol/wr-b16 view
int))
(defn wr-b32
"Writes a 32-bit integer to the current position."
[view int]
(binf.protocol/wr-b32 view
int))
(defn wr-b64
"Writes a 64-bit integer to the current position."
[view int]
(binf.protocol/wr-b64 view
int))
(defn wr-f32
"Writes a 32-bit float to the current position."
[view floating]
(binf.protocol/wr-f32 view
floating))
(defn wr-f64
"Writes a 64-bit float to the current position."
[view floating]
(binf.protocol/wr-f64 view
floating))
(defn wr-string
"Writes a string to the current position, encoded at UTF-8.
Cf. [[wa-string]] about the returned value"
[view string]
(binf.protocol/wr-string view
string))
;;;; IPosition
(defn limit
"Returns the number of bytes a view or a buffer contains."
[view]
(binf.protocol/limit view))
(defn position
"Returns the current position."
[view]
(binf.protocol/position view))
(defn seek
"Modifies the current position."
[view position]
(binf.protocol/seek view
position))
(defn skip
"Advances the current position by `n-byte` bytes."
[view n-byte]
(binf.protocol/skip view
n-byte))
;;;;; IViewable
(defn view
"A view can be created from a buffer (see [[helins.binf.buffer/alloc]]) or from another view.
An `offset` as well as a size (`n-byte`) may be provided.
```clojure
(def my-buffer
(binf.buffer/alloc 100))
;; View with an offset of 50 bytes, 40 bytes long
(def my-view
(binf/view my-buffer
50
40))
;; View from a view, offset of 60 bytes in the original buffer (50 + 10), 20 bytes long
(def inner-view
(binf/view my-view
10
20))
```"
([viewable]
(binf.protocol/view viewable))
([viewable offset]
(binf.protocol/view viewable
offset))
([viewable offset n-byte]
(binf.protocol/view viewable
offset
n-byte)))
;;;;;;;;;; Helper functions
(defn garanteed?
"Returns true if the `view` can consume at least `n-byte` bytes from the current position."
[view n-byte]
(>= (remaining view)
n-byte))
(defn remaining
"Returns the number of bytes remaining until the end of the view given the current position."
[view]
(- (binf.protocol/limit view)
(binf.protocol/position view)))
;;;;;;;;;; Additional types / Boolean
(defn ra-bool
"Reads a boolean value from an absolute `position`."
[view position]
(= (ra-u8 view
position)
1))
(defn rr-bool
"Reads a boolean value from the current position."
[view]
(= (rr-u8 view)
1))
(defn wa-bool
"Writes a boolean value to an absolute `position`."
[view position true?]
(wa-b8 view
position
(if true?
1
0)))
(defn wr-bool
"Writes a boolean value from the current position."
[view true?]
(wr-b8 view
(if true?
1
0)))