2019-07-04 18:30:24 +00:00
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# Endian conversion operations for unsigned integers, suitable for serializing
|
|
|
|
# and deserializing data. The operations are only defined for unsigned
|
|
|
|
# integers - if you wish to encode signed integers, convert / cast them to
|
|
|
|
# unsigned first!
|
|
|
|
#
|
|
|
|
# Although it would be possible to enforce correctness with endians in the type
|
|
|
|
# (`BigEndian[uin64]`) this seems like overkill. That said, some
|
2019-10-03 10:26:38 +00:00
|
|
|
# static analysis tools allow you to annotate fields with endianness - perhaps
|
2019-07-04 18:30:24 +00:00
|
|
|
# an idea for the future, akin to `TaintedString`?
|
|
|
|
#
|
|
|
|
# Keeping the above in mind, it's generally safer to use `array[N, byte]` to
|
2019-10-03 10:26:38 +00:00
|
|
|
# hold values of specific endianness and read them out with `fromBytes` when the
|
2019-07-04 18:30:24 +00:00
|
|
|
# integer interpretation of the bytes is needed.
|
2019-07-05 10:17:10 +00:00
|
|
|
|
2020-04-10 14:30:38 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
type
|
|
|
|
SomeEndianInt* = uint8|uint16|uint32|uint64
|
|
|
|
## types that we support endian conversions for - uint8 is there for
|
|
|
|
## for syntactic / generic convenience. Other candidates:
|
|
|
|
## * int/uint - uncertain size, thus less suitable for binary interop
|
|
|
|
## * intX - over and underflow protection in nim might easily cause issues -
|
|
|
|
## need to consider before adding here
|
2019-07-04 18:30:24 +00:00
|
|
|
|
2020-09-21 06:43:27 +00:00
|
|
|
const
|
|
|
|
useBuiltins = not defined(noIntrinsicsEndians)
|
|
|
|
|
|
|
|
when (defined(gcc) or defined(llvm_gcc) or defined(clang)) and useBuiltins:
|
2019-07-04 18:30:24 +00:00
|
|
|
func swapBytesBuiltin(x: uint8): uint8 = x
|
|
|
|
func swapBytesBuiltin(x: uint16): uint16 {.
|
|
|
|
importc: "__builtin_bswap16", nodecl.}
|
|
|
|
|
|
|
|
func swapBytesBuiltin(x: uint32): uint32 {.
|
|
|
|
importc: "__builtin_bswap32", nodecl.}
|
|
|
|
|
|
|
|
func swapBytesBuiltin(x: uint64): uint64 {.
|
|
|
|
importc: "__builtin_bswap64", nodecl.}
|
|
|
|
|
2020-09-21 06:43:27 +00:00
|
|
|
elif defined(icc) and useBuiltins:
|
2019-07-04 18:30:24 +00:00
|
|
|
func swapBytesBuiltin(x: uint8): uint8 = x
|
|
|
|
func swapBytesBuiltin(a: uint16): uint16 {.importc: "_bswap16", nodecl.}
|
|
|
|
func swapBytesBuiltin(a: uint32): uint32 {.importc: "_bswap", nodec.}
|
|
|
|
func swapBytesBuiltin(a: uint64): uint64 {.importc: "_bswap64", nodecl.}
|
|
|
|
|
2020-09-21 06:43:27 +00:00
|
|
|
elif defined(vcc) and useBuiltins:
|
2019-07-04 18:30:24 +00:00
|
|
|
func swapBytesBuiltin(x: uint8): uint8 = x
|
2020-05-20 00:58:46 +00:00
|
|
|
func swapBytesBuiltin(a: uint16): uint16 {.
|
2019-07-04 18:30:24 +00:00
|
|
|
importc: "_byteswap_ushort", cdecl, header: "<intrin.h>".}
|
|
|
|
|
2020-05-20 00:58:46 +00:00
|
|
|
func swapBytesBuiltin(a: uint32): uint32 {.
|
2019-07-04 18:30:24 +00:00
|
|
|
importc: "_byteswap_ulong", cdecl, header: "<intrin.h>".}
|
|
|
|
|
2020-05-20 00:58:46 +00:00
|
|
|
func swapBytesBuiltin(a: uint64): uint64 {.
|
2019-07-04 18:30:24 +00:00
|
|
|
importc: "_byteswap_uint64", cdecl, header: "<intrin.h>".}
|
|
|
|
|
|
|
|
func swapBytesNim(x: uint8): uint8 = x
|
|
|
|
func swapBytesNim(x: uint16): uint16 = (x shl 8) or (x shr 8)
|
|
|
|
|
|
|
|
func swapBytesNim(x: uint32): uint32 =
|
|
|
|
let v = (x shl 16) or (x shr 16)
|
|
|
|
|
|
|
|
((v shl 8) and 0xff00ff00'u32) or ((v shr 8) and 0x00ff00ff'u32)
|
|
|
|
|
|
|
|
func swapBytesNim(x: uint64): uint64 =
|
|
|
|
var v = (x shl 32) or (x shr 32)
|
|
|
|
v =
|
|
|
|
((v and 0x0000ffff0000ffff'u64) shl 16) or
|
|
|
|
((v and 0xffff0000ffff0000'u64) shr 16)
|
|
|
|
|
|
|
|
((v and 0x00ff00ff00ff00ff'u64) shl 8) or
|
|
|
|
((v and 0xff00ff00ff00ff00'u64) shr 8)
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func swapBytes*[T: SomeEndianInt](x: T): T {.inline.} =
|
2019-07-04 18:30:24 +00:00
|
|
|
## Reverse the bytes within an integer, such that the most significant byte
|
|
|
|
## changes place with the least significant one, etc
|
|
|
|
##
|
|
|
|
## Example:
|
|
|
|
## doAssert swapBytes(0x01234567'u32) == 0x67452301
|
|
|
|
when nimvm:
|
|
|
|
swapBytesNim(x)
|
|
|
|
else:
|
2020-09-21 06:43:27 +00:00
|
|
|
when declared(swapBytesBuiltin):
|
2019-07-04 18:30:24 +00:00
|
|
|
swapBytesBuiltin(x)
|
|
|
|
else:
|
|
|
|
swapBytesNim(x)
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func toBytes*(x: SomeEndianInt, endian: Endianness = system.cpuEndian):
|
2019-07-04 18:30:24 +00:00
|
|
|
array[sizeof(x), byte] {.noinit, inline.} =
|
|
|
|
## Convert integer to its corresponding byte sequence using the chosen
|
2019-10-03 10:26:38 +00:00
|
|
|
## endianness. By default, native endianness is used which is not portable!
|
2019-07-04 18:30:24 +00:00
|
|
|
let v =
|
|
|
|
if endian == system.cpuEndian: x
|
|
|
|
else: swapBytes(x)
|
|
|
|
|
|
|
|
# Loop since vm can't copymem - let's hope optimizer is smart here :)
|
|
|
|
for i in 0..<sizeof(result):
|
|
|
|
result[i] = byte((v shr (i * 8)) and 0xff)
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func toBytesLE*(x: SomeEndianInt):
|
2019-07-04 18:30:24 +00:00
|
|
|
array[sizeof(x), byte] {.inline.} =
|
|
|
|
## Convert a native endian integer to a little endian byte sequence
|
|
|
|
toBytes(x, littleEndian)
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func toBytesBE*(x: SomeEndianInt):
|
2019-07-04 18:30:24 +00:00
|
|
|
array[sizeof(x), byte] {.inline.} =
|
|
|
|
## Convert a native endian integer to a native endian byte sequence
|
|
|
|
toBytes(x, bigEndian)
|
|
|
|
|
|
|
|
func fromBytes*(
|
2019-07-05 10:17:10 +00:00
|
|
|
T: typedesc[SomeEndianInt],
|
2019-07-04 18:30:24 +00:00
|
|
|
x: array[sizeof(T), byte],
|
|
|
|
endian: Endianness = system.cpuEndian): T {.inline.} =
|
|
|
|
## Convert a byte sequence to a native endian integer. By default, native
|
2019-10-03 10:26:38 +00:00
|
|
|
## endianness is used which is not portable!
|
2019-07-04 18:30:24 +00:00
|
|
|
for i in 0..<sizeof(result): # No copymem in vm
|
|
|
|
result = result or T(x[i]) shl (i * 8)
|
|
|
|
|
|
|
|
if endian != system.cpuEndian:
|
|
|
|
result = swapBytes(result)
|
|
|
|
|
|
|
|
func fromBytes*(
|
2019-07-05 10:17:10 +00:00
|
|
|
T: typedesc[SomeEndianInt],
|
2019-07-04 18:30:24 +00:00
|
|
|
x: openArray[byte],
|
|
|
|
endian: Endianness = system.cpuEndian): T {.inline.} =
|
2019-10-03 10:26:38 +00:00
|
|
|
## Read bytes and convert to an integer according to the given endianness. At
|
2019-07-04 18:30:24 +00:00
|
|
|
## runtime, v must contain at least sizeof(T) bytes. By default, native
|
2019-10-03 10:26:38 +00:00
|
|
|
## endianness is used which is not portable!
|
2019-08-06 18:02:03 +00:00
|
|
|
##
|
|
|
|
## REVIEW COMMENT (zah)
|
|
|
|
## This API is very strange. Why can't I pass an open array of 3 bytes
|
|
|
|
## to be interpreted as a LE number? Also, why is `endian` left as a
|
|
|
|
## run-time parameter (with such short functions, it could easily be static).
|
|
|
|
|
2019-07-04 18:30:24 +00:00
|
|
|
const ts = sizeof(T) # Nim bug: can't use sizeof directly
|
|
|
|
var tmp: array[ts, byte]
|
|
|
|
for i in 0..<tmp.len: # Loop since vm can't copymem
|
|
|
|
tmp[i] = x[i]
|
|
|
|
fromBytes(T, tmp, endian)
|
|
|
|
|
|
|
|
func fromBytesBE*(
|
2019-07-05 10:17:10 +00:00
|
|
|
T: typedesc[SomeEndianInt],
|
2019-07-04 18:30:24 +00:00
|
|
|
x: array[sizeof(T), byte]): T {.inline.} =
|
|
|
|
## Read big endian bytes and convert to an integer. By default, native
|
2019-10-03 10:26:38 +00:00
|
|
|
## endianness is used which is not portable!
|
2019-07-04 18:30:24 +00:00
|
|
|
fromBytes(T, x, bigEndian)
|
|
|
|
|
|
|
|
func fromBytesBE*(
|
2019-07-05 10:17:10 +00:00
|
|
|
T: typedesc[SomeEndianInt],
|
2019-07-04 18:30:24 +00:00
|
|
|
x: openArray[byte]): T {.inline.} =
|
|
|
|
## Read big endian bytes and convert to an integer. At runtime, v must contain
|
2019-10-03 10:26:38 +00:00
|
|
|
## at least sizeof(T) bytes. By default, native endianness is used which is
|
|
|
|
## not portable!
|
2019-07-04 18:30:24 +00:00
|
|
|
fromBytes(T, x, bigEndian)
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func toBE*[T: SomeEndianInt](x: T): T {.inline.} =
|
2019-07-04 18:30:24 +00:00
|
|
|
## Convert a native endian value to big endian. Consider toBytesBE instead
|
|
|
|
## which may prevent some confusion.
|
|
|
|
if cpuEndian == bigEndian: x
|
|
|
|
else: x.swapBytes
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func fromBE*[T: SomeEndianInt](x: T): T {.inline.} =
|
2019-07-04 18:30:24 +00:00
|
|
|
## Read a big endian value and return the corresponding native endian
|
|
|
|
# there's no difference between this and toBE, except when reading the code
|
|
|
|
toBE(x)
|
|
|
|
|
|
|
|
func fromBytesLE*(
|
2019-07-05 10:17:10 +00:00
|
|
|
T: typedesc[SomeEndianInt],
|
2019-07-04 18:30:24 +00:00
|
|
|
x: array[sizeof(T), byte]): T {.inline.} =
|
|
|
|
## Read little endian bytes and convert to an integer. By default, native
|
2019-10-03 10:26:38 +00:00
|
|
|
## endianness is used which is not portable!
|
2019-07-04 18:30:24 +00:00
|
|
|
fromBytes(T, x, littleEndian)
|
|
|
|
|
|
|
|
func fromBytesLE*(
|
2019-07-05 10:17:10 +00:00
|
|
|
T: typedesc[SomeEndianInt],
|
2019-07-04 18:30:24 +00:00
|
|
|
x: openArray[byte]): T {.inline.} =
|
|
|
|
## Read little endian bytes and convert to an integer. At runtime, v must
|
2019-10-03 10:26:38 +00:00
|
|
|
## contain at least sizeof(T) bytes. By default, native endianness is used
|
2019-07-04 18:30:24 +00:00
|
|
|
## which is not portable!
|
|
|
|
fromBytes(T, x, littleEndian)
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func toLE*[T: SomeEndianInt](x: T): T {.inline.} =
|
2019-07-04 18:30:24 +00:00
|
|
|
## Convert a native endian value to little endian. Consider toBytesLE instead
|
|
|
|
## which may prevent some confusion.
|
|
|
|
if cpuEndian == littleEndian: x
|
|
|
|
else: x.swapBytes
|
|
|
|
|
2019-07-05 10:17:10 +00:00
|
|
|
func fromLE*[T: SomeEndianInt](x: T): T {.inline.} =
|
2019-07-04 18:30:24 +00:00
|
|
|
## Read a little endian value and return the corresponding native endian
|
|
|
|
# there's no difference between this and toLE, except when reading the code
|
|
|
|
toLE(x)
|