From dc695351914a7133dcb50ab88d724b2e4f79e669 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Sun, 28 May 2023 21:58:20 -0500 Subject: [PATCH] add ptr+len decode dimens --- src/pixie/fileformats/bmp.nim | 14 +++++++++++--- src/pixie/fileformats/gif.nim | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/pixie/fileformats/bmp.nim b/src/pixie/fileformats/bmp.nim index ffd1fe1..65d7c05 100644 --- a/src/pixie/fileformats/bmp.nim +++ b/src/pixie/fileformats/bmp.nim @@ -228,19 +228,27 @@ proc decodeBmp*(data: string): Image {.raises: [PixieError].} = decodeDib(data[14].unsafeAddr, data.len - 14) proc decodeBmpDimensions*( - data: string + data: pointer, len: int ): ImageDimensions {.raises: [PixieError].} = ## Decodes the BMP dimensions. - if data.len < 26: + if len < 26: failInvalid() + let data = cast[ptr UncheckedArray[uint8]](data) + # BMP Header - if data[0 .. 1] != "BM": + if data[0].char != 'B' or data[1].char != 'M': # Must start with BM failInvalid() result.width = data.readInt32(18).int result.height = abs(data.readInt32(22)).int +proc decodeBmpDimensions*( + data: string +): ImageDimensions {.raises: [PixieError].} = + ## Decodes the BMP dimensions. + decodeBmpDimensions(data.cstring, data.len) + proc encodeDib*(image: Image): string {.raises: [].} = ## Encodes an image into a DIB. diff --git a/src/pixie/fileformats/gif.nim b/src/pixie/fileformats/gif.nim index 47ef20b..7adfa78 100644 --- a/src/pixie/fileformats/gif.nim +++ b/src/pixie/fileformats/gif.nim @@ -379,18 +379,29 @@ proc decodeGif*(data: string): Gif {.raises: [PixieError].} = result.duration += interval proc decodeGifDimensions*( - data: string + data: pointer, len: int ): ImageDimensions {.raises: [PixieError].} = ## Decodes the GIF dimensions. - if data.len < 10: + if len < 10: failInvalid() - if data[0 .. 5] notin gifSignatures: + let data = cast[ptr UncheckedArray[uint8]](data) + + let startsWithSignature = + equalMem(data, gifSignatures[0].cstring, 6) or + equalMem(data, gifSignatures[1].cstring, 6) + + if not startsWithSignature: raise newException(PixieError, "Invalid GIF file signature") result.width = data.readInt16(6).int result.height = data.readInt16(8).int +proc decodeGifDimensions*( + data: string +): ImageDimensions {.raises: [PixieError].} = + decodeGifDimensions(data.cstring, data.len) + proc newImage*(gif: Gif): Image {.raises: [].} = gif.frames[0].copy()