# Stint # Copyright 2018 Status Research & Development GmbH # Licensed under either of # # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) # * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) # # at your option. This file may not be copied, modified, or distributed except according to those terms. import private/[bitops2_priv, endians2_priv, datatypes] import stew/endians2 export endians2 func swapBytes*(x: StUint): StUint {.inline.} = StUint(data: swapBytes(x.data)) func toBytes*[bits: static int](x: StUint[bits], endian: Endianness = system.cpuEndian): array[bits div 8, byte] {.inline.} = toBytes(x.data, endian) func toBytesLE*[bits: static int](x: StUint[bits]): array[bits div 8, byte] {.inline.} = toBytes(x, littleEndian) func toBytesBE*[bits: static int](x: StUint[bits]): array[bits div 8, byte] {.inline.} = toBytes(x, bigEndian) func fromBytes*[bits: static int]( T: typedesc[StUint[bits]], x: array[bits div 8, byte], endian: Endianness = system.cpuEndian): T {.inline, noinit.} = # TODO compile-time version copyMem(addr result, unsafeAddr x[0], bits div 8) if endian != system.cpuEndian: result = swapBytes(result) func fromBytes*[bits: static int]( T: typedesc[StUint[bits]], x: openArray[byte], endian: Endianness = system.cpuEndian): T {.inline.} = # TODO fromBytesBE in io.nim handles this better, merge the two! var tmp: array[bits div 8, byte] if x.len < tmp.len: let offset = if endian == bigEndian: tmp.len - x.len else: 0 for i in 0..