raises annotations for framed format
This commit is contained in:
parent
3d39a6228a
commit
6afa8377e1
|
@ -6,6 +6,8 @@ import
|
|||
export
|
||||
types
|
||||
|
||||
{.push raises: [Defect].}
|
||||
|
||||
{.compile: "crc32c.c".}
|
||||
# TODO: we don't have a native implementation of CRC32C algorithm yet.
|
||||
# we can't use nimPNG CRC32
|
||||
|
@ -14,9 +16,11 @@ proc masked_crc32c(buf: ptr byte, len: uint): cuint {.cdecl, importc.}
|
|||
func checkCrc*(data: openArray[byte], expected: uint32): bool =
|
||||
let startPtr = if data.len == 0: nil else: data[0].unsafeAddr
|
||||
let actual = masked_crc32c(startPtr, data.len.uint)
|
||||
result = actual == expected
|
||||
actual == expected
|
||||
|
||||
proc checkCrcAndAppend*(output: OutputStream, data: openArray[byte], crc: uint32): bool =
|
||||
proc checkCrcAndAppend*(
|
||||
output: OutputStream, data: openArray[byte], crc: uint32): bool {.
|
||||
raises: [Defect, IOError].}=
|
||||
if checkCrc(data, crc):
|
||||
output.write(data)
|
||||
return true
|
||||
|
@ -35,10 +39,12 @@ const
|
|||
|
||||
STREAM_HEADER* = "\xff\x06\x00\x00sNaPpY"
|
||||
|
||||
proc uncompressFramedStream*(input: InputStream, output: OutputStream) {.fsMultiSync.} =
|
||||
proc uncompressFramedStream*(
|
||||
input: InputStream, output: OutputStream) {.
|
||||
fsMultiSync, raises: [Defect, IOError, SnappyError].} =
|
||||
try:
|
||||
if not input.readable(STREAM_HEADER.len):
|
||||
raise newException(UnexpectedEofError, "Failed to read strean header")
|
||||
raise newException(UnexpectedEofError, "Failed to read stream header")
|
||||
|
||||
if input.read(STREAM_HEADER.len) != STREAM_HEADER.toOpenArrayByte(0, STREAM_HEADER.len-1):
|
||||
raise newException(MalformedSnappyData, "Invalid header value")
|
||||
|
@ -91,15 +97,21 @@ proc uncompressFramedStream*(input: InputStream, output: OutputStream) {.fsMulti
|
|||
finally:
|
||||
close output
|
||||
|
||||
proc framingFormatUncompress*(input: InputStream): seq[byte] =
|
||||
proc framingFormatUncompress*(input: InputStream): seq[byte] {.
|
||||
raises: [Defect, SnappyError].} =
|
||||
var output = memoryOutput()
|
||||
uncompressFramedStream input, output
|
||||
try:
|
||||
uncompressFramedStream input, output
|
||||
except IOError as e: raiseAssert e.msg # Doesn't happen with memory outputs
|
||||
return output.getOutput
|
||||
|
||||
proc framingFormatUncompress*(input: openarray[byte]): seq[byte] =
|
||||
proc framingFormatUncompress*(input: openarray[byte]): seq[byte] {.
|
||||
raises: [Defect, SnappyError].} =
|
||||
framingFormatUncompress unsafeMemoryInput(input)
|
||||
|
||||
proc processFrame(output: OutputStream, dst: var openArray[byte], src: openArray[byte]) =
|
||||
proc processFrame(
|
||||
output: OutputStream, dst: var openArray[byte], src: openArray[byte]) {.
|
||||
raises: [Defect, IOError].} =
|
||||
let
|
||||
crc = masked_crc32c(src[0].unsafeAddr, src.len.uint)
|
||||
leb128 = uint32(src.len).toBytes(Leb128)
|
||||
|
@ -127,7 +139,8 @@ proc processFrame(output: OutputStream, dst: var openArray[byte], src: openArray
|
|||
output.write toBytesLE(crc)
|
||||
output.write dst.toOpenArray(0, encodedLen-1)
|
||||
|
||||
proc framingFormatCompress*(output: OutputStream, src: openArray[byte]) =
|
||||
proc framingFormatCompress*(output: OutputStream, src: openArray[byte]) {.
|
||||
raises: [Defect, IOError].} =
|
||||
const maxFrameSize = MAX_UNCOMPRESSED_DATA_LEN
|
||||
var compressedData = newSeq[byte](MAX_COMPRESSED_DATA_LEN)
|
||||
|
||||
|
@ -148,5 +161,8 @@ proc framingFormatCompress*(output: OutputStream, src: openArray[byte]) =
|
|||
|
||||
proc framingFormatCompress*(src: openArray[byte]): seq[byte] =
|
||||
var output = memoryOutput()
|
||||
framingFormatCompress(output, src)
|
||||
try:
|
||||
framingFormatCompress(output, src)
|
||||
except IOError as e: raiseAssert e.msg
|
||||
|
||||
return output.getOutput
|
||||
|
|
Loading…
Reference in New Issue