## Nim-Leopard ## Copyright (c) 2022 Status Research & Development GmbH ## Licensed under either of ## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) ## * MIT license ([LICENSE-MIT](LICENSE-MIT)) ## at your option. ## This file may not be copied, modified, or distributed except according to ## those terms. import pkg/upraises push: {.upraises: [].} {.deadCodeElim: on.} import pkg/stew/results import pkg/stew/byteutils import ./wrapper import ./utils export wrapper, results const BuffMultiples* = 64 type LeoBufferPtr = ptr UncheckedArray[byte] Leo = object of RootObj bufSize*: int # size of the buffer in multiples of 64 buffers*: int # total number of data buffers (K) parity*: int # total number of parity buffers (M) dataBufferPtr: seq[LeoBufferPtr] # buffer where data is copied before encoding parityWorkCount: int # number of parity work buffers parityBufferPtr: seq[LeoBufferPtr] # buffer where parity is copied before encoding LeoEncoder* = object of Leo LeoDecoder* = object of Leo decodeWorkCount: int # number of decoding work buffers decodeBufferPtr: seq[LeoBufferPtr] # work buffer used for decoding proc encode*( self: var LeoEncoder, data, parity: var openArray[seq[byte]]): Result[void, cstring] = # zero encode work buffer to avoid corrupting with previous run for i in 0.. 0: dataPtr[i] = self.dataBufferPtr[i] copyMem(self.dataBufferPtr[i], addr data[i][0], self.bufSize) else: dataPtr[i] = nil # copy parity into aligned buffer for i in 0.. 0: parityPtr[i] = self.parityBufferPtr[i] copyMem(self.parityBufferPtr[i], addr parity[i][0], self.bufSize) else: parityPtr[i] = nil let res = leo_decode( self.bufSize.cuint, self.buffers.cuint, self.parity.cuint, self.decodeWorkCount.cuint, cast[ptr pointer](addr dataPtr[0]), cast[ptr pointer](addr self.parityBufferPtr[0]), cast[ptr pointer](addr self.decodeBufferPtr[0])) if ord(res) != ord(LeopardSuccess): return err(leoResultString(res.LeopardResult)) for i in 0..