2019-07-06 18:07:41 +00:00
|
|
|
# byteutils
|
2023-07-31 21:07:48 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2019-07-06 18:07:41 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################################################
|
|
|
|
#################################### Array utilities ###############################################
|
|
|
|
|
2020-12-10 11:05:22 +00:00
|
|
|
import
|
|
|
|
std/[algorithm, typetraits],
|
2022-01-06 07:32:29 +00:00
|
|
|
./arrayops
|
2019-07-06 18:07:41 +00:00
|
|
|
|
2020-12-10 11:05:22 +00:00
|
|
|
# backwards compat
|
|
|
|
export arrayops.`&`, arrayops.initArrayWith, arrayops.`[]=`
|
2019-07-06 18:07:41 +00:00
|
|
|
|
2023-07-31 21:07:48 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
{.pragma: hexRaises, raises: [ValueError].}
|
2020-03-04 23:42:04 +00:00
|
|
|
|
2019-07-06 18:07:41 +00:00
|
|
|
########################################################################################################
|
|
|
|
##################################### Hex utilities ################################################
|
|
|
|
|
2020-07-02 09:48:05 +00:00
|
|
|
proc readHexChar*(c: char): byte
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises, noSideEffect, inline.} =
|
2019-07-06 18:07:41 +00:00
|
|
|
## Converts an hex char to a byte
|
|
|
|
case c
|
|
|
|
of '0'..'9': result = byte(ord(c) - ord('0'))
|
|
|
|
of 'a'..'f': result = byte(ord(c) - ord('a') + 10)
|
|
|
|
of 'A'..'F': result = byte(ord(c) - ord('A') + 10)
|
|
|
|
else:
|
2022-03-16 17:02:32 +00:00
|
|
|
raise newException(ValueError, $c & " is not a hexadecimal character")
|
2019-07-06 18:07:41 +00:00
|
|
|
|
2022-06-17 08:37:06 +00:00
|
|
|
template skip0xPrefix(hexStr: openArray[char]): int =
|
2019-07-06 18:07:41 +00:00
|
|
|
## Returns the index of the first meaningful char in `hexStr` by skipping
|
|
|
|
## "0x" prefix
|
2020-04-09 09:20:21 +00:00
|
|
|
if hexStr.len > 1 and hexStr[0] == '0' and hexStr[1] in {'x', 'X'}: 2
|
2019-07-06 18:07:41 +00:00
|
|
|
else: 0
|
|
|
|
|
2023-01-19 07:19:26 +00:00
|
|
|
func hexToByteArrayImpl(
|
|
|
|
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int):
|
2023-02-14 14:56:10 +00:00
|
|
|
int {.hexRaises.} =
|
2019-07-06 18:07:41 +00:00
|
|
|
var sIdx = skip0xPrefix(hexStr)
|
2023-01-19 07:19:26 +00:00
|
|
|
# Fun with closed intervals
|
|
|
|
doAssert fromIdx >= 0 and
|
|
|
|
toIdx <= output.high and
|
|
|
|
fromIdx <= (toIdx + 1)
|
2019-07-06 18:07:41 +00:00
|
|
|
|
2023-01-19 07:19:26 +00:00
|
|
|
let sz = toIdx + 1 - fromIdx
|
2019-07-06 18:07:41 +00:00
|
|
|
|
2020-04-09 09:20:21 +00:00
|
|
|
if hexStr.len - sIdx < 2*sz:
|
|
|
|
raise (ref ValueError)(msg: "hex string too short")
|
2023-01-19 07:19:26 +00:00
|
|
|
|
2019-07-06 18:07:41 +00:00
|
|
|
sIdx += fromIdx * 2
|
|
|
|
for bIdx in fromIdx ..< sz + fromIdx:
|
2023-01-19 07:19:26 +00:00
|
|
|
output[bIdx] =
|
|
|
|
(hexStr[sIdx].readHexChar shl 4) or
|
|
|
|
hexStr[sIdx + 1].readHexChar
|
2019-07-06 18:07:41 +00:00
|
|
|
inc(sIdx, 2)
|
|
|
|
|
2022-06-17 08:37:06 +00:00
|
|
|
sIdx
|
|
|
|
|
2023-01-19 07:19:26 +00:00
|
|
|
func hexToByteArray*(
|
|
|
|
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int)
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises.} =
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr[mapHex(fromIdx..toIdx)]` and store
|
|
|
|
## corresponding bytes in `output[fromIdx..toIdx]` where `mapHex` takes into
|
|
|
|
## account stripped characters.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short or contains invalid
|
|
|
|
## data in the parsed part
|
|
|
|
## * Longer strings are allowed
|
|
|
|
## * No "endianness" reordering is done
|
|
|
|
## * Allows specifying the byte range to process into the array - the indices
|
|
|
|
## are mapped to the string after potentially stripping "0x"
|
2022-06-17 08:37:06 +00:00
|
|
|
discard hexToByteArrayImpl(hexStr, output, fromIdx, toIdx)
|
|
|
|
|
2023-01-19 07:19:26 +00:00
|
|
|
func hexToByteArray*(hexStr: openArray[char], output: var openArray[byte])
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises.} =
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
|
|
|
## `output`.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short or contains invalid
|
|
|
|
## data
|
|
|
|
## * Longer strings are allowed
|
|
|
|
## * No "endianness" reordering is done
|
2019-07-06 18:07:41 +00:00
|
|
|
hexToByteArray(hexStr, output, 0, output.high)
|
|
|
|
|
2023-01-19 07:19:26 +00:00
|
|
|
func hexToByteArray*[N: static[int]](hexStr: openArray[char]): array[N, byte]
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises, noinit.}=
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short or contains invalid
|
|
|
|
## data
|
|
|
|
## * Longer strings are allowed
|
|
|
|
## * No "endianness" reordering is done
|
2019-07-06 18:07:41 +00:00
|
|
|
hexToByteArray(hexStr, result)
|
|
|
|
|
2023-01-19 07:19:26 +00:00
|
|
|
func hexToByteArray*(hexStr: openArray[char], N: static int): array[N, byte]
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises, noinit.}=
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short or contains invalid
|
|
|
|
## data
|
|
|
|
## * Longer strings are allowed
|
|
|
|
## * No "endianness" reordering is done
|
2021-03-19 01:56:01 +00:00
|
|
|
hexToByteArray(hexStr, result)
|
|
|
|
|
2023-01-19 07:19:26 +00:00
|
|
|
func hexToByteArrayStrict*(hexStr: openArray[char], output: var openArray[byte])
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises.} =
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
|
|
|
## `output`.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short, too long or contains
|
|
|
|
## invalid data
|
|
|
|
## * No "endianness" reordering is done
|
|
|
|
if hexToByteArrayImpl(hexStr, output, 0, output.high) != hexStr.len:
|
2022-06-17 08:37:06 +00:00
|
|
|
raise (ref ValueError)(msg: "hex string too long")
|
|
|
|
|
|
|
|
func hexToByteArrayStrict*[N: static[int]](hexStr: openArray[char]): array[N, byte]
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises, noinit, inline.}=
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
|
|
|
## `output`.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short, too long or contains
|
|
|
|
## invalid data
|
|
|
|
## * No "endianness" reordering is done
|
2022-06-17 08:37:06 +00:00
|
|
|
hexToByteArrayStrict(hexStr, result)
|
|
|
|
|
|
|
|
func hexToByteArrayStrict*(hexStr: openArray[char], N: static int): array[N, byte]
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises, noinit, inline.}=
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr` and store corresponding bytes in
|
|
|
|
## `output`.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short, too long or contains
|
|
|
|
## invalid data
|
|
|
|
## * No "endianness" reordering is done
|
2022-06-17 08:37:06 +00:00
|
|
|
hexToByteArrayStrict(hexStr, result)
|
|
|
|
|
2020-07-02 15:00:39 +00:00
|
|
|
func fromHex*[N](A: type array[N, byte], hexStr: string): A
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises, noinit, inline.}=
|
2023-01-19 07:19:26 +00:00
|
|
|
## Read hex-encoded data from `hexStr` returning an array of N bytes.
|
|
|
|
##
|
|
|
|
## * `0x`/`0X` is stripped if present
|
|
|
|
## * `ValueError` is raised if the string is too short or contains invalid
|
|
|
|
## data
|
|
|
|
## * Longer strings are allowed
|
|
|
|
## * No "endianness" reordering is done
|
2020-07-02 15:00:39 +00:00
|
|
|
hexToByteArray(hexStr, result)
|
|
|
|
|
2020-07-02 10:00:38 +00:00
|
|
|
func hexToPaddedByteArray*[N: static[int]](hexStr: string): array[N, byte]
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises.} =
|
2019-07-06 18:07:41 +00:00
|
|
|
## Read a hex string and store it in a byte array `output`.
|
|
|
|
## The string may be shorter than the byte array.
|
|
|
|
## No "endianness" reordering is done.
|
|
|
|
let
|
|
|
|
p = skip0xPrefix(hexStr)
|
|
|
|
sz = hexStr.len - p
|
|
|
|
maxStrSize = result.len * 2
|
|
|
|
var
|
|
|
|
bIdx: int
|
|
|
|
shift = 4
|
|
|
|
|
2020-04-09 09:20:21 +00:00
|
|
|
if hexStr.len - p > maxStrSize:
|
|
|
|
# TODO this is a bit strange, compared to the hexToByteArray above...
|
|
|
|
raise (ref ValueError)(msg: "hex string too long")
|
2019-12-10 14:39:57 +00:00
|
|
|
|
2019-07-06 18:07:41 +00:00
|
|
|
if sz < maxStrSize:
|
|
|
|
# include extra byte if odd length
|
2019-12-10 14:39:57 +00:00
|
|
|
bIdx = result.len - (sz + 1) div 2
|
2019-07-06 18:07:41 +00:00
|
|
|
# start with shl of 4 if length is even
|
|
|
|
shift = 4 - sz mod 2 * 4
|
|
|
|
|
|
|
|
for sIdx in p ..< hexStr.len:
|
|
|
|
let nibble = hexStr[sIdx].readHexChar shl shift
|
|
|
|
result[bIdx] = result[bIdx] or nibble
|
|
|
|
shift = shift + 4 and 4
|
|
|
|
bIdx += shift shr 2
|
|
|
|
|
2020-07-02 09:48:05 +00:00
|
|
|
func hexToSeqByte*(hexStr: string): seq[byte]
|
2023-02-14 14:56:10 +00:00
|
|
|
{.hexRaises.} =
|
2019-07-06 18:07:41 +00:00
|
|
|
## Read an hex string and store it in a sequence of bytes. No "endianness" reordering is done.
|
2020-04-09 09:20:21 +00:00
|
|
|
if (hexStr.len and 1) == 1:
|
|
|
|
raise (ref ValueError)(msg: "hex string must have even length")
|
2019-07-06 18:07:41 +00:00
|
|
|
|
|
|
|
let skip = skip0xPrefix(hexStr)
|
|
|
|
let N = (hexStr.len - skip) div 2
|
|
|
|
|
|
|
|
result = newSeq[byte](N)
|
|
|
|
for i in 0 ..< N:
|
|
|
|
result[i] = hexStr[2*i + skip].readHexChar shl 4 or hexStr[2*i + 1 + skip].readHexChar
|
|
|
|
|
2022-01-06 16:05:47 +00:00
|
|
|
func toHexAux(ba: openArray[byte], with0x: static bool): string =
|
2019-07-06 18:07:41 +00:00
|
|
|
## Convert a byte-array to its hex representation
|
|
|
|
## Output is in lowercase
|
|
|
|
## No "endianness" reordering is done.
|
|
|
|
const hexChars = "0123456789abcdef"
|
|
|
|
|
2022-01-06 16:05:47 +00:00
|
|
|
let extra = when with0x: 2 else: 0
|
|
|
|
result = newStringOfCap(2 * ba.len + extra)
|
|
|
|
when with0x:
|
|
|
|
result.add("0x")
|
|
|
|
|
|
|
|
for b in ba:
|
|
|
|
result.add(hexChars[int(b shr 4 and 0x0f'u8)])
|
|
|
|
result.add(hexChars[int(b and 0x0f'u8)])
|
2019-07-06 18:07:41 +00:00
|
|
|
|
2021-12-02 15:24:02 +00:00
|
|
|
func toHex*(ba: openArray[byte]): string {.inline.} =
|
2019-07-06 18:07:41 +00:00
|
|
|
## Convert a byte-array to its hex representation
|
|
|
|
## Output is in lowercase
|
|
|
|
## No "endianness" reordering is done.
|
2022-01-06 16:05:47 +00:00
|
|
|
toHexAux(ba, false)
|
2019-07-06 18:07:41 +00:00
|
|
|
|
|
|
|
func toHex*[N: static[int]](ba: array[N, byte]): string {.inline.} =
|
|
|
|
## Convert a big endian byte-array to its hex representation
|
|
|
|
## Output is in lowercase
|
|
|
|
## No "endianness" reordering is done.
|
2022-01-06 16:05:47 +00:00
|
|
|
toHexAux(ba, false)
|
|
|
|
|
|
|
|
func to0xHex*(ba: openArray[byte]): string {.inline.} =
|
|
|
|
## Convert a byte-array to its hex representation
|
|
|
|
## Output is in lowercase
|
|
|
|
## No "endianness" reordering is done.
|
|
|
|
toHexAux(ba, true)
|
|
|
|
|
|
|
|
func to0xHex*[N: static[int]](ba: array[N, byte]): string {.inline.} =
|
|
|
|
## Convert a big endian byte-array to its hex representation
|
|
|
|
## Output is in lowercase
|
|
|
|
## No "endianness" reordering is done.
|
|
|
|
toHexAux(ba, true)
|
2019-12-10 14:39:57 +00:00
|
|
|
|
|
|
|
func toBytes*(s: string): seq[byte] =
|
|
|
|
## Convert a string to the corresponding byte sequence - since strings in
|
|
|
|
## nim essentially are byte sequences without any particular encoding, this
|
2020-03-04 22:09:42 +00:00
|
|
|
## simply copies the bytes without a null terminator
|
2020-05-06 13:51:07 +00:00
|
|
|
when nimvm:
|
|
|
|
var r = newSeq[byte](s.len)
|
|
|
|
for i, c in s:
|
|
|
|
r[i] = cast[byte](c)
|
|
|
|
r
|
|
|
|
else:
|
|
|
|
@(s.toOpenArrayByte(0, s.high))
|
2020-03-04 22:09:42 +00:00
|
|
|
|
|
|
|
func fromBytes*(T: type string, v: openArray[byte]): string =
|
2020-03-07 22:29:46 +00:00
|
|
|
if v.len > 0:
|
|
|
|
result = newString(v.len)
|
2020-05-06 13:51:07 +00:00
|
|
|
when nimvm:
|
|
|
|
for i, c in v:
|
|
|
|
result[i] = cast[char](c)
|
|
|
|
else:
|
|
|
|
copyMem(addr result[0], unsafeAddr v[0], v.len)
|
2019-12-19 12:29:38 +00:00
|
|
|
|
|
|
|
func `<`*(a, b: openArray[byte]): bool =
|
|
|
|
## Lexicographical compare of two byte arrays
|
|
|
|
let minlen = min(a.len, b.len)
|
|
|
|
|
|
|
|
for i in 0..<minlen:
|
|
|
|
if a[i] != b[i]: return a[i] < b[i]
|
|
|
|
|
|
|
|
a.len < b.len
|