nim-stew/stew/byteutils.nim

271 lines
9.7 KiB
Nim
Raw Normal View History

2019-07-06 18:07:41 +00:00
# byteutils
2022-03-16 17:02:32 +00:00
# Copyright (c) 2018-2022 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 ###############################################
import
std/[algorithm, typetraits],
./arrayops
2019-07-06 18:07:41 +00:00
# backwards compat
export arrayops.`&`, arrayops.initArrayWith, arrayops.`[]=`
2019-07-06 18:07:41 +00:00
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
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
{.raises: [ValueError, Defect], 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
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
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
func hexToByteArrayImpl(
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int):
int {.raises: [ValueError, Defect].} =
2019-07-06 18:07:41 +00:00
var sIdx = skip0xPrefix(hexStr)
# Fun with closed intervals
doAssert fromIdx >= 0 and
toIdx <= output.high and
fromIdx <= (toIdx + 1)
2019-07-06 18:07:41 +00:00
let sz = toIdx + 1 - fromIdx
2019-07-06 18:07:41 +00:00
if hexStr.len - sIdx < 2*sz:
raise (ref ValueError)(msg: "hex string too short")
2019-07-06 18:07:41 +00:00
sIdx += fromIdx * 2
for bIdx in fromIdx ..< sz + fromIdx:
output[bIdx] =
(hexStr[sIdx].readHexChar shl 4) or
hexStr[sIdx + 1].readHexChar
2019-07-06 18:07:41 +00:00
inc(sIdx, 2)
sIdx
func hexToByteArray*(
hexStr: openArray[char], output: var openArray[byte], fromIdx, toIdx: int)
{.raises: [ValueError, Defect].} =
## 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"
discard hexToByteArrayImpl(hexStr, output, fromIdx, toIdx)
func hexToByteArray*(hexStr: openArray[char], output: var openArray[byte])
{.raises: [ValueError, Defect].} =
## 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)
func hexToByteArray*[N: static[int]](hexStr: openArray[char]): array[N, byte]
{.raises: [ValueError, Defect], noinit.}=
## 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)
func hexToByteArray*(hexStr: openArray[char], N: static int): array[N, byte]
{.raises: [ValueError, Defect], noinit.}=
## 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
hexToByteArray(hexStr, result)
func hexToByteArrayStrict*(hexStr: openArray[char], output: var openArray[byte])
{.raises: [ValueError, Defect].} =
## 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:
raise (ref ValueError)(msg: "hex string too long")
func hexToByteArrayStrict*[N: static[int]](hexStr: openArray[char]): array[N, byte]
2022-06-17 08:39:35 +00:00
{.raises: [ValueError, Defect], noinit, inline.}=
## 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
hexToByteArrayStrict(hexStr, result)
func hexToByteArrayStrict*(hexStr: openArray[char], N: static int): array[N, byte]
2022-06-17 08:39:35 +00:00
{.raises: [ValueError, Defect], noinit, inline.}=
## 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
hexToByteArrayStrict(hexStr, result)
2020-07-02 15:00:39 +00:00
func fromHex*[N](A: type array[N, byte], hexStr: string): A
2022-06-17 08:39:35 +00:00
{.raises: [ValueError, Defect], noinit, inline.}=
## 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)
func hexToPaddedByteArray*[N: static[int]](hexStr: string): array[N, byte]
2022-06-17 08:39:35 +00:00
{.raises: [ValueError, Defect].} =
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
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]
{.raises: [ValueError, Defect].} =
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.
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
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"
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.
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.
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
## simply copies the bytes without a null terminator
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))
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)
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