minor improvements

This commit is contained in:
Balazs Komuves 2026-05-05 22:15:09 +02:00
parent cd6b61fab8
commit 3d36140218
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
4 changed files with 29 additions and 18 deletions

View File

@ -66,7 +66,7 @@ Library
ghc-options: -fwarn-tabs -fno-warn-unused-matches -fno-warn-name-shadowing -fno-warn-unused-imports
cc-options: -x c++
cxx-options: -O3 -std=c++11 -lm
cxx-options: -O3 -std=c++11
extra-libraries: stdc++
--------------------------------------------------------------------------------

View File

@ -28,8 +28,7 @@ import Leopard.Misc
-- * error handling
data LeopardResult
= Success -- ^ Operation succeeded
| NeedMoreData -- ^ Not enough recovery data received
= NeedMoreData -- ^ Not enough recovery data received
| TooMuchData -- ^ Buffer counts are too high
| InvalidSize -- ^ Buffer size must be a multiple of 64 bytes
| InvalidCounts -- ^ Invalid counts provided
@ -40,7 +39,8 @@ data LeopardResult
instance Enum LeopardResult where
toEnum ( 0) = Success -- Operation succeeded
fromEnum _ = error "LeopardResult/fromEnum: not implemented"
toEnum (-1) = NeedMoreData -- Not enough recovery data received
toEnum (-2) = TooMuchData -- Buffer counts are too high
toEnum (-3) = InvalidSize -- Buffer size must be a multiple of 64 bytes
@ -48,21 +48,18 @@ instance Enum LeopardResult where
toEnum (-5) = InvalidInput -- A function parameter was invalid
toEnum (-6) = Platform -- Platform is unsupported
toEnum (-7) = CallInitialize -- Call leo_init() first
toEnum _ = error "invalid leopard error code"
fromEnum _ = error "LeopardResult/fromEnum: not implemented"
decodeLeopardResult :: LeopardResult -> Maybe String
decodeLeopardResult :: LeopardResult -> String
decodeLeopardResult result = case result of
Success -> Nothing -- "Operation succeeded"
NeedMoreData -> Just "Not enough recovery data received"
TooMuchData -> Just "Buffer counts are too high"
InvalidSize -> Just "Buffer size must be a multiple of 64 bytes"
InvalidCounts -> Just "Invalid counts provided"
InvalidInput -> Just "A function parameter was invalid"
Platform -> Just "Platform is unsupported"
CallInitialize -> Just "Call leo_init() first"
NeedMoreData -> "Not enough recovery data received"
TooMuchData -> "Buffer counts are too high"
InvalidSize -> "Buffer size must be a multiple of 64 bytes"
InvalidCounts -> "Invalid counts provided"
InvalidInput -> "A function parameter was invalid"
Platform -> "Platform is unsupported"
CallInitialize -> "Call leo_init() first"
--------------------------------------------------------------------------------
-- * C++ bindings
@ -94,6 +91,8 @@ unsafeEncodeIOList ecParams inputChunks = do
-- We assume that the chunks have a size which is a multiple of 64 bytes, as
-- the underlying `leopard` library assumes that too...
--
-- Also it is required that @K >= 2@
--
{-# NOINLINE unsafeEncodeIO #-}
unsafeEncodeIO :: ECParams -> Array Int ByteString -> IO (Either LeopardResult (Array Int ByteString))
unsafeEncodeIO ecParams@(ECParams k n) inputChunks = do

View File

@ -58,6 +58,9 @@ isDivisibleBy64 n = (mod n 64 == 0)
roundUpToMultipleOf :: Int -> Int -> Int
roundUpToMultipleOf size x = size * (ceilDiv x size)
requiredPadToMultipleOf :: Int -> Int -> Int
requiredPadToMultipleOf size x = roundUpToMultipleOf size x - x
--------------------------------------------------------------------------------
-- * Bytestrings

View File

@ -16,7 +16,7 @@ import Leopard.Misc
--------------------------------------------------------------------------------
-- | Note: Recause of a restriction of the underlying Leopard library, you should have
-- @K >= 2@, @N <= 2*K@ and @N <= 65536@.
-- @K >= 2@, @N <= 2*K@ and @N <= 65536@.
data ECParams = ECParams
{ _ecK :: Int -- ^ @K@ is the number of original chunks
, _ecN :: Int -- ^ @N@ is the number of chunks after encoding
@ -29,9 +29,18 @@ ecM params = _ecN params - _ecK params
isValidECParams :: ECParams -> Bool
isValidECParams (ECParams k n) = and
[ k > 1
[ k >= 1 -- note: while Leopard only allows `k >= 2`, we can just do replication ourselves for `k = 1`.
, k <= 32768
, k < n
, k <= n -- note: if `k == n`, we can simply not call Leopard at all
, n <= 2 * k
]
-- | This version only accepts what Leopard should also accept
isValidECParamsStrict :: ECParams -> Bool
isValidECParamsStrict (ECParams k n) = and
[ k >= 2
, k < n
, n <= 65536
, n <= 2 * k
]