-
Notifications
You must be signed in to change notification settings - Fork 2
/
endian.cljc
120 lines (72 loc) · 2.38 KB
/
endian.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
;; 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.endian
"Obtaining information about endianess and changing the endianess of primitive integers.
Endianess is either `:big-endian` or `:little-endian`."
{:author "Adam Helinski"}
#?(:cljs (:require [goog.object]
[helins.binf.buffer :as binf.buffer]))
#?(:clj (:import java.nio.ByteOrder)))
;;;;;;;;;;
#?(:cljs (def ^:private -d*js-order
;; Computes the native endianess of the host in JS.
(delay
(let [b16 (js/Uint32Array. 1)
b8 (js/Uint8Array. (.-buffer b16))]
(aset b16
0
0xaa)
(if (= (aget b8
0)
0xaa)
:little-endian
:big-endian)))))
(defn order-host
"Returns the endianess of the host virtual machine.
On the JVM, always returns `:big-endian`. In JS, returns the same as [[order-native]]."
[]
#?(:clj :big-endian
:cljs @-d*js-order))
(defn order-native
"Returns the natural endianess of the machine."
[]
#?(:clj (ByteOrder/nativeOrder)
:cljs @-d*js-order))
;;;;;;;;;;
#?(:cljs (def ^:private -data-view
;; Used for swapping endianess.
(js/DataView. (binf.buffer/alloc 8))))
(defn b16
"Swaps the endianess of a 16-bit integer."
[b16]
#?@(:clj [(Short/reverseBytes (unchecked-short b16))]
:cljs [(.setUint16 -data-view
0
b16
false)
(.getUint16 -data-view
0
true)]))
(defn b32
"Swaps the endianess of a 32-bit integer."
[b32]
#?@(:clj [(Integer/reverseBytes (unchecked-int b32))]
:cljs [(.setUint32 -data-view
0
b32
false)
(.getUint32 -data-view
0
true)]))
(defn b64
"Swaps the endianess of a 64-bit integer."
[b64]
#?@(:clj [(Long/reverseBytes b64)]
:cljs [(.setBigUint64 -data-view
0
b64
false)
(.getBigUint64 -data-view
0
true)]))