From e67f257479202f5df922089b8418affc7a1a62ab Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 25 May 2022 18:30:48 -0500 Subject: [PATCH] faster --- src/pixie/fileformats/jpeg.nim | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/pixie/fileformats/jpeg.nim b/src/pixie/fileformats/jpeg.nim index e2c1990..8d0695a 100644 --- a/src/pixie/fileformats/jpeg.nim +++ b/src/pixie/fileformats/jpeg.nim @@ -110,15 +110,23 @@ proc readUint8(state: var DecoderState): uint8 = proc readUint16be(state: var DecoderState): uint16 = ## Reads uint16 big-endian from the input stream. - (state.readUint8().uint16 shl 8) or state.readUint8() + if state.pos + 2 > state.len: + failInvalid() + result = + (state.buffer[state.pos].uint16 shl 8) or + state.buffer[state.pos + 1] + state.pos += 2 proc readUint32be(state: var DecoderState): uint32 = ## Reads uint32 big-endian from the input stream. - return - (state.readUint8().uint32 shl 24) or - (state.readUint8().uint32 shl 16) or - (state.readUint8().uint32 shl 8) or - state.readUint8().uint32 + if state.pos + 4 > state.len: + failInvalid() + result = + (state.buffer[state.pos + 0].uint32 shl 24) or + (state.buffer[state.pos + 1].uint32 shl 16) or + (state.buffer[state.pos + 2].uint32 shl 8) or + state.buffer[state.pos + 3] + state.pos += 4 proc readStr(state: var DecoderState, n: int): string = ## Reads n number of bytes as a string.