Skip to content

Commit

Permalink
add sysrand path
Browse files Browse the repository at this point in the history
  • Loading branch information
guzba committed May 1, 2023
1 parent a109dcb commit f1fd90b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/zippy.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import std/random, std/times, zippy/adler32, zippy/common, zippy/crc, zippy/deflate,
import zippy/adler32, zippy/common, zippy/crc, zippy/deflate,
zippy/gzip, zippy/inflate, zippy/internal

when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0):
import std/sysrand
else:
import std/random, std/times

export common

proc compress*(
Expand All @@ -21,10 +26,18 @@ proc compress*(
result[3] = (1.uint8 shl 3).char # Set the fname flag

block: # https://github.com/guzba/zippy/issues/61
let now = getTime()
var rand = initRand(now.toUnix * 1_000_000_000 + now.nanosecond)
let htbLen =
when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0):
var urand: array[1, uint8]
if not urandom(urand):
raise newException(ZippyError, "Failed to generate random number")
(urand[0] mod 26).int
else:
let now = getTime()
var rand = initRand(now.toUnix * 1_000_000_000 + now.nanosecond)
(rand.next() mod 26).int # mod the uint first to ensure a positive int
# Add up to 26 characters as the gzip header file name
for i in 0 ..< (rand.next() mod 26).int:
for i in 0 ..< htbLen:
result.add (97 + i).char
result.add '\0'

Expand Down

0 comments on commit f1fd90b

Please sign in to comment.