2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-04-05 19:12:59 +00:00
|
|
|
## 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 std/options
|
|
|
|
|
|
|
|
import pkg/leopard
|
|
|
|
import pkg/stew/results
|
|
|
|
|
|
|
|
import ../backend
|
|
|
|
|
|
|
|
type
|
|
|
|
LeoEncoderBackend* = ref object of EncoderBackend
|
|
|
|
encoder*: Option[LeoEncoder]
|
|
|
|
|
|
|
|
LeoDecoderBackend* = ref object of DecoderBackend
|
|
|
|
decoder*: Option[LeoDecoder]
|
|
|
|
|
|
|
|
method encode*(
|
2024-02-16 21:51:24 +00:00
|
|
|
self: LeoEncoderBackend,
|
|
|
|
data,
|
|
|
|
parity: var openArray[seq[byte]]): Result[void, cstring] =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Encode data using Leopard backend
|
2022-04-05 19:12:59 +00:00
|
|
|
|
2022-05-10 11:50:22 +00:00
|
|
|
if parity.len == 0:
|
|
|
|
return ok()
|
|
|
|
|
2022-04-05 19:12:59 +00:00
|
|
|
var encoder = if self.encoder.isNone:
|
|
|
|
self.encoder = (? LeoEncoder.init(
|
|
|
|
self.blockSize,
|
|
|
|
self.buffers,
|
|
|
|
self.parity)).some
|
|
|
|
self.encoder.get()
|
|
|
|
else:
|
|
|
|
self.encoder.get()
|
|
|
|
|
|
|
|
encoder.encode(data, parity)
|
|
|
|
|
|
|
|
method decode*(
|
2024-02-16 21:51:24 +00:00
|
|
|
self: LeoDecoderBackend,
|
|
|
|
data,
|
|
|
|
parity,
|
|
|
|
recovered: var openArray[seq[byte]]): Result[void, cstring] =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Decode data using given Leopard backend
|
|
|
|
|
|
|
|
var decoder =
|
|
|
|
if self.decoder.isNone:
|
2022-04-05 19:12:59 +00:00
|
|
|
self.decoder = (? LeoDecoder.init(
|
|
|
|
self.blockSize,
|
|
|
|
self.buffers,
|
|
|
|
self.parity)).some
|
|
|
|
self.decoder.get()
|
|
|
|
else:
|
|
|
|
self.decoder.get()
|
|
|
|
|
|
|
|
decoder.decode(data, parity, recovered)
|
|
|
|
|
|
|
|
method release*(self: LeoEncoderBackend) =
|
|
|
|
if self.encoder.isSome:
|
|
|
|
self.encoder.get().free()
|
|
|
|
|
|
|
|
method release*(self: LeoDecoderBackend) =
|
|
|
|
if self.decoder.isSome:
|
|
|
|
self.decoder.get().free()
|
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc new*(
|
2024-02-16 21:51:24 +00:00
|
|
|
T: type LeoEncoderBackend,
|
|
|
|
blockSize,
|
|
|
|
buffers,
|
|
|
|
parity: int): LeoEncoderBackend =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Create an instance of an Leopard Encoder backend
|
2024-02-16 21:51:24 +00:00
|
|
|
##
|
2023-06-22 15:11:18 +00:00
|
|
|
LeoEncoderBackend(
|
2022-04-05 19:12:59 +00:00
|
|
|
blockSize: blockSize,
|
|
|
|
buffers: buffers,
|
|
|
|
parity: parity)
|
|
|
|
|
2023-06-22 15:11:18 +00:00
|
|
|
proc new*(
|
2024-02-16 21:51:24 +00:00
|
|
|
T: type LeoDecoderBackend,
|
|
|
|
blockSize,
|
|
|
|
buffers,
|
|
|
|
parity: int): LeoDecoderBackend =
|
2023-06-22 15:11:18 +00:00
|
|
|
## Create an instance of an Leopard Decoder backend
|
2024-02-16 21:51:24 +00:00
|
|
|
##
|
2023-06-22 15:11:18 +00:00
|
|
|
LeoDecoderBackend(
|
2022-04-05 19:12:59 +00:00
|
|
|
blockSize: blockSize,
|
|
|
|
buffers: buffers,
|
|
|
|
parity: parity)
|