2020-02-16 23:15:54 +03:00
|
|
|
(system-include "carp_binary.h")
|
2020-02-18 00:04:09 +03:00
|
|
|
(load "StdInt.carp")
|
2020-02-16 23:15:54 +03:00
|
|
|
|
2020-02-18 19:41:29 +03:00
|
|
|
;; Helper functions for making working with Maybe easier
|
|
|
|
;; TODO: Replace all of these with a single type-generic
|
|
|
|
;; zip-n macro.
|
|
|
|
(defmodule Maybe
|
2021-01-19 17:59:37 +03:00
|
|
|
(defndynamic zip- [f args names]
|
|
|
|
(if (empty? args)
|
|
|
|
`(Maybe.Just (~%f %@names))
|
|
|
|
(let [n (gensym-with 'zip)]
|
|
|
|
`(match %(car args)
|
2020-02-18 19:41:29 +03:00
|
|
|
(Maybe.Nothing) (Maybe.Nothing)
|
2021-01-19 17:59:37 +03:00
|
|
|
(Maybe.Just %n) %(Maybe.zip- f (cdr args) (cons-last n names))))))
|
|
|
|
|
|
|
|
(defmacro zip [f :rest args]
|
|
|
|
(Maybe.zip- f args '()))
|
2020-02-18 19:41:29 +03:00
|
|
|
)
|
|
|
|
|
2020-03-21 02:02:55 +03:00
|
|
|
;; Temporary fix for issue #698
|
|
|
|
;; The underlying issue is deeper, and should probably be fixed.
|
|
|
|
(doc ByteOrder
|
2020-02-18 03:03:52 +03:00
|
|
|
"The type of byte orders.
|
2020-02-16 23:15:54 +03:00
|
|
|
|
|
|
|
LittleEndian designates the little endian ordering, and indicates the least
|
|
|
|
significant byte appears first in a given byte sequence.
|
|
|
|
|
|
|
|
BigEndian designates the big endian ordering, and indicates the most
|
|
|
|
significant byte occurs first in a given byte sequence.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(deftype ByteOrder LittleEndian BigEndian)
|
2020-02-16 23:15:54 +03:00
|
|
|
|
2021-07-05 15:48:35 +03:00
|
|
|
(doc Binary "provides various helper functions to work with bits and bytes.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(defmodule Binary
|
2020-02-18 00:04:09 +03:00
|
|
|
(register to-int16 (λ [Byte Byte] Uint16))
|
|
|
|
(register to-int32 (λ [Byte Byte Byte Byte] Uint32))
|
|
|
|
(register to-int64 (λ [Byte Byte Byte Byte Byte Byte Byte Byte] Uint64))
|
2020-02-18 21:35:35 +03:00
|
|
|
(register int16-to-byte (λ [(Ref Uint16)] Byte))
|
|
|
|
(register int32-to-byte (λ [(Ref Uint32)] Byte))
|
|
|
|
(register int64-to-byte (λ [(Ref Uint64)] Byte))
|
2020-02-17 23:38:55 +03:00
|
|
|
(register system-endianness-internal (λ [] Int))
|
|
|
|
|
2020-02-20 22:12:38 +03:00
|
|
|
(defn unwrap-success [x]
|
|
|
|
(Result.unwrap-or-zero @x))
|
|
|
|
|
|
|
|
(defn unwrap-error [x]
|
|
|
|
(Result.from-error @x (zero)))
|
|
|
|
|
|
|
|
(doc byte-converter
|
|
|
|
"Returns a function that, when called, attempts to convert an array of bytes using `f` and `order`
|
|
|
|
|
|
|
|
If the conversion is successful, returns a `Result.Success` containing the converted value.
|
|
|
|
|
|
|
|
If the conversion fails, returns a `Result.Error` containing the byte array passed as an argument.")
|
|
|
|
(defn byte-converter [f order]
|
|
|
|
(fn [bs]
|
|
|
|
(match (~f order bs)
|
|
|
|
(Maybe.Nothing) (Result.Error @bs)
|
|
|
|
(Maybe.Just i) (Result.Success i))))
|
|
|
|
(doc interpreted
|
|
|
|
"Returns the interpreted value from a sequence of byte-converion results")
|
|
|
|
(private interpreted)
|
|
|
|
(defn interpreted [results]
|
|
|
|
(==> results
|
|
|
|
(Array.copy-filter &Result.success?)
|
|
|
|
(ref)
|
|
|
|
(Array.copy-map &unwrap-success)))
|
|
|
|
|
|
|
|
(doc remaining-bytes
|
|
|
|
"Returns the number of uninterpreted bytes from a seuqence of byte-conversion results")
|
|
|
|
(private remaining-bytes)
|
|
|
|
(defn remaining-bytes [results]
|
|
|
|
(==> results
|
|
|
|
(Array.copy-filter &Result.error?)
|
|
|
|
(ref)
|
|
|
|
(Array.copy-map &unwrap-error)
|
|
|
|
(ref)
|
|
|
|
(Array.copy-map &Array.length)
|
|
|
|
(ref)
|
|
|
|
(Array.reduce &(fn [x y] (+ x @y)) 0)))
|
|
|
|
|
2020-02-18 03:03:52 +03:00
|
|
|
(doc system-endianness
|
2020-02-18 00:04:09 +03:00
|
|
|
"Returns the endianness of the host system.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig system-endianness (λ [] ByteOrder))
|
2020-02-18 03:03:52 +03:00
|
|
|
(defn system-endianness []
|
2020-02-17 23:38:55 +03:00
|
|
|
(if (= (system-endianness-internal) 1)
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
|
|
|
(ByteOrder.BigEndian)))
|
2020-02-16 23:15:54 +03:00
|
|
|
|
2020-02-18 21:56:52 +03:00
|
|
|
(doc unsafe-bytes->int16
|
2020-02-18 19:50:24 +03:00
|
|
|
"Interprets the first two bytes in a byte sequence as an Uint16 value.
|
2020-02-16 23:15:54 +03:00
|
|
|
**This operation is unsafe.**")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig unsafe-bytes->int16 (Fn [ByteOrder (Ref (Array Byte) a)] Uint16))
|
2020-02-18 21:56:52 +03:00
|
|
|
(defn unsafe-bytes->int16 [order bs]
|
2020-02-18 03:03:52 +03:00
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2020-02-18 00:04:09 +03:00
|
|
|
(to-int16 @(Array.unsafe-nth bs 0) @(Array.unsafe-nth bs 1))
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2020-02-16 23:15:54 +03:00
|
|
|
(to-int16 @(Array.unsafe-nth bs 1) @(Array.unsafe-nth bs 0))))
|
|
|
|
|
2020-02-18 19:41:29 +03:00
|
|
|
(doc bytes->int16
|
2020-02-18 19:50:24 +03:00
|
|
|
"Interprets the first two bytes in a byte sequence as an Uint16 value.
|
2020-02-18 19:41:29 +03:00
|
|
|
|
|
|
|
If the first two bytes are inaccessible, or the given array contains less
|
|
|
|
than two bytes, returns Maybe.Nothing.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int16 (Fn [ByteOrder (Ref (Array Byte) a)] (Maybe Uint16)))
|
2020-02-18 19:41:29 +03:00
|
|
|
(defn bytes->int16 [order bytes]
|
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2020-02-18 19:41:29 +03:00
|
|
|
(Maybe.zip &to-int16 (Array.nth bytes 0) (Array.nth bytes 1))
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2020-02-18 19:41:29 +03:00
|
|
|
(Maybe.zip &to-int16 (Array.nth bytes 1) (Array.nth bytes 0))))
|
|
|
|
|
2020-02-18 21:35:35 +03:00
|
|
|
(doc int16->bytes
|
|
|
|
"Converts a Uint16 to a sequence of bytes representing the value using the provided `order`")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig int16->bytes (Fn [ByteOrder Uint16] (Array Byte)))
|
2020-02-18 21:35:35 +03:00
|
|
|
(defn int16->bytes [order i]
|
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2020-02-18 21:35:35 +03:00
|
|
|
(Array.copy-map &int16-to-byte &[i (Uint16.bit-shift-right i (Uint16.from-long 8l))])
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2020-02-18 21:35:35 +03:00
|
|
|
(Array.copy-map &int16-to-byte &[(Uint16.bit-shift-right i (Uint16.from-long 8l)) i])))
|
|
|
|
|
2020-02-18 21:56:52 +03:00
|
|
|
(doc unsafe-bytes->int16-seq
|
2020-02-18 21:50:07 +03:00
|
|
|
"Interprets a sequence of bytes as a sequence of Uint16 values.
|
|
|
|
**This operation is unsafe.**")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig unsafe-bytes->int16-seq (Fn [ByteOrder (Ref (Array Byte) a)] (Array Uint16)))
|
2020-02-18 21:56:52 +03:00
|
|
|
(defn unsafe-bytes->int16-seq [order bs]
|
2020-02-18 21:50:07 +03:00
|
|
|
(let [partitions (Array.partition bs 2)
|
2020-02-18 21:56:52 +03:00
|
|
|
f (fn [b] (unsafe-bytes->int16 order b))]
|
2020-02-18 21:50:07 +03:00
|
|
|
(Array.copy-map &f &partitions)))
|
|
|
|
|
|
|
|
(doc bytes->int16-seq
|
|
|
|
"Interprets a sequence of bytes as a sequence of Uint16 values.
|
|
|
|
|
2020-02-20 22:12:38 +03:00
|
|
|
Returns a pair containing interpreted values and the number of bytes that were not interpreted.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int16-seq (Fn [ByteOrder (Ref (Array Byte) a)] (Pair (Array Uint16) Int)))
|
2020-02-18 21:50:07 +03:00
|
|
|
(defn bytes->int16-seq [order bs]
|
|
|
|
(let [partitions (Array.partition bs 2)
|
2020-02-20 22:12:38 +03:00
|
|
|
f (byte-converter &bytes->int16 order)]
|
|
|
|
(let [results (Array.copy-map &f &partitions)]
|
|
|
|
(Pair.init (interpreted &results) (remaining-bytes &results)))))
|
|
|
|
|
|
|
|
(doc bytes->int16-seq-exact
|
|
|
|
"Attempts to interpret a given byte sequence as an exact sequence of Uint16 values.
|
|
|
|
|
|
|
|
If successful, returns the interpreted values. If unsuccessful, returns the number of excess bytes.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int16-seq-exact (Fn [ByteOrder (Ref (Array Byte) a)] (Result (Array Uint16) Int)))
|
2020-02-20 22:12:38 +03:00
|
|
|
(defn bytes->int16-seq-exact [order bs]
|
|
|
|
(let [r (bytes->int16-seq order bs)]
|
|
|
|
(if (= 0 @(Pair.b &r))
|
|
|
|
(Result.Success @(Pair.a &r))
|
|
|
|
(Result.Error @(Pair.b &r)))))
|
2020-02-18 21:50:07 +03:00
|
|
|
|
|
|
|
(doc int16-seq->bytes
|
|
|
|
"Converts an array of Uint16 values into byte sequences.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig int16-seq->bytes (Fn [ByteOrder (Ref (Array Uint16) a)] (Array (Array Byte))))
|
2020-02-18 21:50:07 +03:00
|
|
|
(defn int16-seq->bytes [order is]
|
|
|
|
(let [f (fn [i] (int16->bytes order @i))]
|
|
|
|
(Array.copy-map &f is)))
|
|
|
|
|
2020-02-18 21:56:52 +03:00
|
|
|
(doc unsafe-bytes->int32
|
2020-02-18 19:50:24 +03:00
|
|
|
"Interprets the first four bytes in a byte sequence as an Uint32 value.
|
2020-02-16 23:15:54 +03:00
|
|
|
**This operation is unsafe.**")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig unsafe-bytes->int32 (Fn [ByteOrder (Ref (Array Byte))] Uint32))
|
2020-02-18 21:56:52 +03:00
|
|
|
(defn unsafe-bytes->int32 [order bs]
|
2020-02-18 03:03:52 +03:00
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2020-02-18 03:03:52 +03:00
|
|
|
(to-int32 @(Array.unsafe-nth bs 0) @(Array.unsafe-nth bs 1)
|
|
|
|
@(Array.unsafe-nth bs 2) @(Array.unsafe-nth bs 3))
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2020-02-16 23:15:54 +03:00
|
|
|
(to-int32 @(Array.unsafe-nth bs 3) @(Array.unsafe-nth bs 2)
|
|
|
|
@(Array.unsafe-nth bs 1) @(Array.unsafe-nth bs 0))))
|
|
|
|
|
2020-02-18 19:41:29 +03:00
|
|
|
(doc bytes->int32
|
2020-02-18 19:50:24 +03:00
|
|
|
"Interprets the first four bytes in a byte sequence as an Uint32 value.
|
2020-02-18 19:41:29 +03:00
|
|
|
|
|
|
|
If the first four bytes are inaccessible, or the given array contains less
|
|
|
|
than four bytes, returns Maybe.Nothing.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int32 (Fn [ByteOrder (Ref (Array Byte))] (Maybe Uint32)))
|
2020-02-18 19:41:29 +03:00
|
|
|
(defn bytes->int32 [order bs]
|
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2021-01-19 17:59:37 +03:00
|
|
|
(Maybe.zip &to-int32 (Array.nth bs 0) (Array.nth bs 1)
|
|
|
|
(Array.nth bs 2) (Array.nth bs 3))
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2021-01-19 17:59:37 +03:00
|
|
|
(Maybe.zip &to-int32 (Array.nth bs 3) (Array.nth bs 2)
|
|
|
|
(Array.nth bs 1) (Array.nth bs 0))))
|
2020-02-18 19:41:29 +03:00
|
|
|
|
2020-02-18 21:35:35 +03:00
|
|
|
(doc int32->bytes
|
|
|
|
"Converts a Uint32 to a sequence of bytes representing the value using the provided `order`")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig int32->bytes (Fn [ByteOrder Uint32] (Array Byte)))
|
2020-02-18 21:35:35 +03:00
|
|
|
(defn int32->bytes [order i]
|
|
|
|
(let [shift (fn [lng] (Uint32.bit-shift-right i (Uint32.from-long lng)))]
|
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2020-02-18 21:35:35 +03:00
|
|
|
(Array.copy-map &int32-to-byte
|
|
|
|
&[i (shift 8l) (shift 16l) (shift 24l)])
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2020-02-18 21:35:35 +03:00
|
|
|
(Array.copy-map &int32-to-byte
|
|
|
|
&[(shift 24l) (shift 16l) (shift 8l) i]))))
|
|
|
|
|
2020-02-18 21:56:52 +03:00
|
|
|
(doc unsafe-bytes->int32-seq
|
2020-02-18 21:50:07 +03:00
|
|
|
"Interprets a sequence of bytes as a sequence of Uint32 values.
|
|
|
|
**This operation is unsafe.**")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig unsafe-bytes->int32-seq (Fn [ByteOrder (Ref (Array Byte) a)] (Array Uint32)))
|
2020-02-18 21:56:52 +03:00
|
|
|
(defn unsafe-bytes->int32-seq [order bs]
|
2020-02-18 21:50:07 +03:00
|
|
|
(let [partitions (Array.partition bs 4)
|
2020-02-18 21:56:52 +03:00
|
|
|
f (fn [b] (unsafe-bytes->int32 order b))]
|
2020-02-18 21:50:07 +03:00
|
|
|
(Array.copy-map &f &partitions)))
|
|
|
|
|
|
|
|
(doc bytes->int32-seq
|
|
|
|
"Interprets a sequence of bytes as a sequence of Uint32 values.
|
|
|
|
|
2020-02-20 22:12:38 +03:00
|
|
|
Returns a pair containing interpreted values and the number of bytes that were not interpreted.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int32-seq (Fn [ByteOrder (Ref (Array Byte) a)] (Pair (Array Uint32) Int)))
|
2020-02-18 21:50:07 +03:00
|
|
|
(defn bytes->int32-seq [order bs]
|
|
|
|
(let [partitions (Array.partition bs 4)
|
2020-02-20 22:12:38 +03:00
|
|
|
f (byte-converter &bytes->int32 order)]
|
|
|
|
(let [results (Array.copy-map &f &partitions)]
|
|
|
|
(Pair.init (interpreted &results) (remaining-bytes &results)))))
|
|
|
|
|
|
|
|
(doc bytes->int32-seq-exact
|
|
|
|
"Attempts to interpret a given byte sequence as an exact sequence of Uint32 values.
|
|
|
|
|
|
|
|
If successful, returns the interpreted values. If unsuccessful, returns the number of excess bytes.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int32-seq-exact (Fn [ByteOrder (Ref (Array Byte) a)] (Result (Array Uint32) Int)))
|
2020-02-20 22:12:38 +03:00
|
|
|
(defn bytes->int32-seq-exact [order bs]
|
|
|
|
(let [r (bytes->int32-seq order bs)]
|
|
|
|
(if (= 0 @(Pair.b &r))
|
|
|
|
(Result.Success @(Pair.a &r))
|
|
|
|
(Result.Error @(Pair.b &r)))))
|
2020-02-18 21:50:07 +03:00
|
|
|
|
|
|
|
(doc int32-seq->bytes
|
|
|
|
"Converts an array of Uint32 values into byte sequences.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig int32-seq->bytes (Fn [ByteOrder (Ref (Array Uint32) a)] (Array (Array Byte))))
|
2020-02-18 21:50:07 +03:00
|
|
|
(defn int32-seq->bytes [order is]
|
|
|
|
(let [f (fn [i] (int32->bytes order @i))]
|
|
|
|
(Array.copy-map &f is)))
|
|
|
|
|
2020-02-18 21:56:52 +03:00
|
|
|
(doc unsafe-bytes->int64
|
2020-02-18 19:50:24 +03:00
|
|
|
"Interprets the first eight bytes in a byte sequence as an Uint64 value.
|
2020-02-16 23:15:54 +03:00
|
|
|
**This operation is unsafe.**")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig unsafe-bytes->int64 (Fn [ByteOrder (Ref (Array Byte) a)] Uint64))
|
2020-02-18 21:56:52 +03:00
|
|
|
(defn unsafe-bytes->int64 [order bs]
|
2020-02-18 03:03:52 +03:00
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2020-02-18 03:03:52 +03:00
|
|
|
(to-int64 @(Array.unsafe-nth bs 0) @(Array.unsafe-nth bs 1)
|
2020-02-16 23:15:54 +03:00
|
|
|
@(Array.unsafe-nth bs 2) @(Array.unsafe-nth bs 3)
|
|
|
|
@(Array.unsafe-nth bs 4) @(Array.unsafe-nth bs 5)
|
|
|
|
@(Array.unsafe-nth bs 6) @(Array.unsafe-nth bs 7))
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2020-02-16 23:15:54 +03:00
|
|
|
(to-int64 @(Array.unsafe-nth bs 7) @(Array.unsafe-nth bs 6)
|
|
|
|
@(Array.unsafe-nth bs 5) @(Array.unsafe-nth bs 4)
|
|
|
|
@(Array.unsafe-nth bs 3) @(Array.unsafe-nth bs 2)
|
|
|
|
@(Array.unsafe-nth bs 1) @(Array.unsafe-nth bs 0))))
|
2020-02-18 03:03:52 +03:00
|
|
|
|
2020-02-18 19:41:29 +03:00
|
|
|
(doc bytes->int64
|
2020-02-18 19:50:24 +03:00
|
|
|
"Interprets the first eight bytes in a byte sequence as an Uint64 value.
|
2020-02-18 19:41:29 +03:00
|
|
|
|
|
|
|
If the first eight bytes are inaccessible, or the given array contains less
|
|
|
|
than eight bytes, returns Maybe.Nothing.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int64 (Fn [ByteOrder (Ref (Array Byte) a)] (Maybe Uint64)))
|
2020-02-18 19:41:29 +03:00
|
|
|
(defn bytes->int64 [order bs]
|
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2021-01-19 17:59:37 +03:00
|
|
|
(Maybe.zip &to-int64 (Array.nth bs 0) (Array.nth bs 1)
|
|
|
|
(Array.nth bs 2) (Array.nth bs 3)
|
|
|
|
(Array.nth bs 4) (Array.nth bs 5)
|
|
|
|
(Array.nth bs 6) (Array.nth bs 7))
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2021-01-19 17:59:37 +03:00
|
|
|
(Maybe.zip &to-int64 (Array.nth bs 7) (Array.nth bs 6)
|
|
|
|
(Array.nth bs 5) (Array.nth bs 4)
|
|
|
|
(Array.nth bs 3) (Array.nth bs 2)
|
|
|
|
(Array.nth bs 1) (Array.nth bs 0))))
|
2020-02-18 21:35:35 +03:00
|
|
|
(doc int64->bytes
|
|
|
|
"Converts a Uint64 to a sequence of bytes representing the value using the provided `order`")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig int64->bytes (Fn [ByteOrder Uint64] (Array Byte)))
|
2020-02-18 21:35:35 +03:00
|
|
|
(defn int64->bytes [order i]
|
|
|
|
(let [shift (fn [lng] (Uint64.bit-shift-right i (Uint64.from-long lng)))]
|
|
|
|
(match order
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.LittleEndian)
|
2020-02-18 21:35:35 +03:00
|
|
|
(Array.copy-map &int64-to-byte
|
|
|
|
&[i (shift 8l) (shift 16l)
|
|
|
|
(shift 24l) (shift 32l)
|
|
|
|
(shift 40l) (shift 48l) (shift 56l)])
|
2020-03-21 02:02:55 +03:00
|
|
|
(ByteOrder.BigEndian)
|
2020-02-18 21:35:35 +03:00
|
|
|
(Array.copy-map &int64-to-byte
|
|
|
|
&[(shift 56l) (shift 48l)
|
|
|
|
(shift 40l) (shift 32l)
|
|
|
|
(shift 24l) (shift 16l) (shift 8l) i]))))
|
2020-02-18 19:41:29 +03:00
|
|
|
|
2020-02-18 21:56:52 +03:00
|
|
|
(doc unsafe-bytes->int64-seq
|
2020-02-18 19:50:24 +03:00
|
|
|
"Interprets a sequence of bytes as a sequence of Uint64 values.
|
2020-02-16 23:15:54 +03:00
|
|
|
**This operation is unsafe.**")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig unsafe-bytes->int64-seq (Fn [ByteOrder (Ref (Array Byte) a)] (Array Uint64)))
|
2020-02-18 21:56:52 +03:00
|
|
|
(defn unsafe-bytes->int64-seq [order bs]
|
2020-02-16 23:57:38 +03:00
|
|
|
(let [partitions (Array.partition bs 8)
|
2020-02-18 21:56:52 +03:00
|
|
|
f (fn [b] (unsafe-bytes->int64 order b))]
|
2020-02-16 23:15:54 +03:00
|
|
|
(Array.copy-map &f &partitions)))
|
2020-02-18 03:03:52 +03:00
|
|
|
|
2020-02-18 19:41:29 +03:00
|
|
|
(doc bytes->int64-seq
|
|
|
|
"Interprets a sequence of bytes as a sequence of Uint64 values.
|
|
|
|
|
2020-02-20 22:12:38 +03:00
|
|
|
Returns a pair containing interpreted values and the number of bytes that were not interpreted.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int64-seq (Fn [ByteOrder (Ref (Array Byte) a)] (Pair (Array Uint64) Int)))
|
2020-02-18 19:41:29 +03:00
|
|
|
(defn bytes->int64-seq [order bs]
|
|
|
|
(let [partitions (Array.partition bs 8)
|
2020-02-20 22:12:38 +03:00
|
|
|
f (byte-converter &bytes->int64 order)]
|
|
|
|
(let [results (Array.copy-map &f &partitions)]
|
|
|
|
(Pair.init (interpreted &results) (remaining-bytes &results)))))
|
|
|
|
|
|
|
|
(doc bytes->int64-seq-exact
|
|
|
|
"Attempts to interpret a given byte sequence as an exact sequence of Uint64 values.
|
|
|
|
|
|
|
|
If successful, returns the interpreted values. If unsuccessful, returns the number of excess bytes.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig bytes->int64-seq-exact (Fn [ByteOrder (Ref (Array Byte) a)] (Result (Array Uint64) Int)))
|
2020-02-20 22:12:38 +03:00
|
|
|
(defn bytes->int64-seq-exact [order bs]
|
|
|
|
(let [r (bytes->int64-seq order bs)]
|
|
|
|
(if (= 0 @(Pair.b &r))
|
|
|
|
(Result.Success @(Pair.a &r))
|
|
|
|
(Result.Error @(Pair.b &r)))))
|
2020-02-18 21:45:18 +03:00
|
|
|
|
|
|
|
(doc int64-seq->bytes
|
|
|
|
"Converts an array of Uint64 values into byte sequences.")
|
2020-03-21 02:02:55 +03:00
|
|
|
(sig int64-seq->bytes (Fn [ByteOrder (Ref (Array Uint64) a)] (Array (Array Byte))))
|
2020-02-18 21:45:18 +03:00
|
|
|
(defn int64-seq->bytes [order is]
|
|
|
|
(let [f (fn [i] (int64->bytes order @i))]
|
|
|
|
(Array.copy-map &f is)))
|
2021-11-06 13:24:17 +03:00
|
|
|
|
|
|
|
(defn to-hex-str [b]
|
|
|
|
(let [hi (Byte.bit-and b (from-int 0xF0))
|
|
|
|
lo (Byte.bit-shift-left b (from-int 4))
|
|
|
|
nib-one (case hi
|
|
|
|
(from-int 0x00) @"0"
|
|
|
|
(from-int 0x10) @"1"
|
|
|
|
(from-int 0x20) @"2"
|
|
|
|
(from-int 0x30) @"3"
|
|
|
|
(from-int 0x40) @"4"
|
|
|
|
(from-int 0x50) @"5"
|
|
|
|
(from-int 0x60) @"6"
|
|
|
|
(from-int 0x70) @"7"
|
|
|
|
(from-int 0x80) @"8"
|
|
|
|
(from-int 0x90) @"9"
|
|
|
|
(from-int 0xA0) @"A"
|
|
|
|
(from-int 0xB0) @"B"
|
|
|
|
(from-int 0xC0) @"C"
|
|
|
|
(from-int 0xD0) @"D"
|
|
|
|
(from-int 0xE0) @"E"
|
|
|
|
(from-int 0xF0) @"F"
|
|
|
|
@"FATAL ERROR IN BIT LAND! ALL IS LOST")
|
|
|
|
nib-two (case lo
|
|
|
|
(from-int 0x00) @"0"
|
|
|
|
(from-int 0x10) @"1"
|
|
|
|
(from-int 0x20) @"2"
|
|
|
|
(from-int 0x30) @"3"
|
|
|
|
(from-int 0x40) @"4"
|
|
|
|
(from-int 0x50) @"5"
|
|
|
|
(from-int 0x60) @"6"
|
|
|
|
(from-int 0x70) @"7"
|
|
|
|
(from-int 0x80) @"8"
|
|
|
|
(from-int 0x90) @"9"
|
|
|
|
(from-int 0xA0) @"A"
|
|
|
|
(from-int 0xB0) @"B"
|
|
|
|
(from-int 0xC0) @"C"
|
|
|
|
(from-int 0xD0) @"D"
|
|
|
|
(from-int 0xE0) @"E"
|
|
|
|
(from-int 0xF0) @"F"
|
|
|
|
@"FATAL ERROR IN BIT LAND! ALL IS LOST")]
|
|
|
|
(String.concat &[nib-one nib-two])))
|
|
|
|
|
|
|
|
(doc bytes->hex-string
|
|
|
|
"Converts an array of bytes to a string of its hexadecimal representation")
|
|
|
|
(sig bytes->hex-string (Fn [(Ref (Array Byte) q)] String))
|
|
|
|
(defn bytes->hex-string [bs]
|
|
|
|
(let [f (fn [b] (to-hex-str @b))]
|
|
|
|
(String.join " " &(Array.copy-map &f bs))))
|
2020-02-16 23:15:54 +03:00
|
|
|
)
|