diff options
author | pacien | 2018-11-25 16:45:35 +0100 |
---|---|---|
committer | pacien | 2018-11-25 16:45:35 +0100 |
commit | 680c0a3c94f0bb84a2773bc9a95dc5399b6925fb (patch) | |
tree | 8b7efa786f14aa4d17ab22ab11b55eda1981519f /src/integers.nim | |
parent | 643d2d72fab23df30d29c10614bfa89648cd3655 (diff) | |
download | gziplike-680c0a3c94f0bb84a2773bc9a95dc5399b6925fb.tar.gz |
Fix bitreader look-ahead overflow
Diffstat (limited to 'src/integers.nim')
-rw-r--r-- | src/integers.nim | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/integers.nim b/src/integers.nim index fddbfdc..7b0f166 100644 --- a/src/integers.nim +++ b/src/integers.nim | |||
@@ -15,13 +15,16 @@ | |||
15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | 16 | ||
17 | const wordBitLength* = 8 | 17 | const wordBitLength* = 8 |
18 | const wordBitMask* = 0b1111_1111'u8 | ||
19 | 18 | ||
20 | proc `/^`*[T: Natural](x, y: T): T = | 19 | proc `/^`*[T: Natural](x, y: T): T = |
21 | (x + y - 1) div y | 20 | (x + y - 1) div y |
22 | 21 | ||
23 | proc truncateToUint8*(x: SomeUnsignedInt): uint8 = | 22 | proc truncateToUint8*(x: SomeUnsignedInt): uint8 = |
24 | (x and wordBitMask).uint8 | 23 | (x and uint8.high).uint8 |
24 | |||
25 | proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T = | ||
26 | let maskOffset = sizeof(T) * wordBitLength - bits | ||
27 | if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x | ||
25 | 28 | ||
26 | iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] = | 29 | iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] = |
27 | let chunkBitLength = sizeof(chunkType) * wordBitLength | 30 | let chunkBitLength = sizeof(chunkType) * wordBitLength |