Clean up crc module
Use better names, remove quite a bit of dead code. Change `><` to a name that's actually descriptive.
This commit is contained in:
parent
5ad9d874c3
commit
188bba2b3c
5 changed files with 30 additions and 76 deletions
|
|
@ -11,18 +11,12 @@ import
|
||||||
strutils
|
strutils
|
||||||
|
|
||||||
type
|
type
|
||||||
TCrc32* = int32
|
SecureHash* = int32
|
||||||
|
|
||||||
const
|
const
|
||||||
InitCrc32* = TCrc32(- 1)
|
InitCrc32 = SecureHash(- 1)
|
||||||
InitAdler32* = int32(1)
|
|
||||||
|
|
||||||
proc updateCrc32*(val: int8, crc: TCrc32): TCrc32 {.inline.}
|
proc secureHashFile*(filename: string): SecureHash
|
||||||
proc updateCrc32*(val: char, crc: TCrc32): TCrc32 {.inline.}
|
|
||||||
proc crcFromBuf*(buf: pointer, length: int): TCrc32
|
|
||||||
proc strCrc32*(s: string): TCrc32
|
|
||||||
proc crcFromFile*(filename: string): TCrc32
|
|
||||||
proc updateAdler32*(adler: int32, buf: pointer, length: int): int32
|
|
||||||
# implementation
|
# implementation
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -74,31 +68,22 @@ const
|
||||||
- 1000256840, 1567103746, 711928724, - 1274298825, - 1022587231, 1510334235,
|
- 1000256840, 1567103746, 711928724, - 1274298825, - 1022587231, 1510334235,
|
||||||
755167117]
|
755167117]
|
||||||
|
|
||||||
proc updateCrc32(val: int8, crc: TCrc32): TCrc32 =
|
proc updateCrc32(val: int8, crc: SecureHash): SecureHash =
|
||||||
result = TCrc32(crc32table[(int(crc) xor (int(val) and 0x000000FF)) and
|
result = SecureHash(crc32table[(int(crc) xor (int(val) and 0x000000FF)) and
|
||||||
0x000000FF]) xor (crc shr TCrc32(8))
|
0x000000FF]) xor (crc shr SecureHash(8))
|
||||||
|
|
||||||
proc updateCrc32(val: char, crc: TCrc32): TCrc32 =
|
proc updateCrc32(val: char, crc: SecureHash): SecureHash =
|
||||||
result = updateCrc32(toU8(ord(val)), crc)
|
result = updateCrc32(toU8(ord(val)), crc)
|
||||||
|
|
||||||
proc strCrc32(s: string): TCrc32 =
|
proc secureHash*(s: string): SecureHash =
|
||||||
result = InitCrc32
|
result = InitCrc32
|
||||||
for i in countup(0, len(s) - 1): result = updateCrc32(s[i], result)
|
|
||||||
|
|
||||||
proc `><`*(c: TCrc32, s: string): TCrc32 =
|
|
||||||
result = c
|
|
||||||
for i in 0..len(s)-1: result = updateCrc32(s[i], result)
|
for i in 0..len(s)-1: result = updateCrc32(s[i], result)
|
||||||
|
|
||||||
type
|
type
|
||||||
TByteArray = array[0..10000000, int8]
|
TByteArray = array[0..10000000, int8]
|
||||||
PByteArray = ref TByteArray
|
PByteArray = ref TByteArray
|
||||||
|
|
||||||
proc crcFromBuf(buf: pointer, length: int): TCrc32 =
|
proc secureHashFile(filename: string): SecureHash =
|
||||||
var p = cast[PByteArray](buf)
|
|
||||||
result = InitCrc32
|
|
||||||
for i in countup(0, length - 1): result = updateCrc32(p[i], result)
|
|
||||||
|
|
||||||
proc crcFromFile(filename: string): TCrc32 =
|
|
||||||
const
|
const
|
||||||
bufSize = 8000 # don't use 8K for the memory allocator!
|
bufSize = 8000 # don't use 8K for the memory allocator!
|
||||||
var
|
var
|
||||||
|
|
@ -114,34 +99,3 @@ proc crcFromFile(filename: string): TCrc32 =
|
||||||
if readBytes != bufSize: break
|
if readBytes != bufSize: break
|
||||||
dealloc(buf)
|
dealloc(buf)
|
||||||
close(bin)
|
close(bin)
|
||||||
|
|
||||||
const
|
|
||||||
base = int32(65521) # largest prime smaller than 65536
|
|
||||||
# NMAX = 5552; original code with unsigned 32 bit integer
|
|
||||||
# NMAX is the largest n
|
|
||||||
# such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
|
|
||||||
nmax = 3854 # code with signed 32 bit integer
|
|
||||||
# NMAX is the largest n such that
|
|
||||||
# 255n(n+1)/2 + (n+1)(BASE-1) <= 2^31-1
|
|
||||||
# The penalty is the time loss in the extra MOD-calls.
|
|
||||||
|
|
||||||
proc updateAdler32(adler: int32, buf: pointer, length: int): int32 =
|
|
||||||
var
|
|
||||||
s1, s2: int32
|
|
||||||
L, k, b: int
|
|
||||||
s1 = adler and int32(0x0000FFFF)
|
|
||||||
s2 = (adler shr int32(16)) and int32(0x0000FFFF)
|
|
||||||
L = length
|
|
||||||
b = 0
|
|
||||||
while (L > 0):
|
|
||||||
if L < nmax: k = L
|
|
||||||
else: k = nmax
|
|
||||||
dec(L, k)
|
|
||||||
while (k > 0):
|
|
||||||
s1 = s1 +% int32((cast[cstring](buf))[b])
|
|
||||||
s2 = s2 +% s1
|
|
||||||
inc(b)
|
|
||||||
dec(k)
|
|
||||||
s1 = `%%`(s1, base)
|
|
||||||
s2 = `%%`(s2, base)
|
|
||||||
result = (s2 shl int32(16)) or s1
|
|
||||||
|
|
|
||||||
|
|
@ -572,13 +572,13 @@ proc getCompileCFileCmd*(cfilename: string, isExternal = false): string =
|
||||||
"nim", quoteShell(getPrefixDir()),
|
"nim", quoteShell(getPrefixDir()),
|
||||||
"lib", quoteShell(libpath)])
|
"lib", quoteShell(libpath)])
|
||||||
|
|
||||||
proc footprint(filename: string): TCrc32 =
|
proc footprint(filename: string): SecureHash =
|
||||||
# note, '><' further modifies a crc value with a string.
|
result = secureHash(
|
||||||
result = crcFromFile(filename) ><
|
$secureHashFile(filename) &
|
||||||
platform.OS[targetOS].name ><
|
platform.OS[targetOS].name &
|
||||||
platform.CPU[targetCPU].name ><
|
platform.CPU[targetCPU].name &
|
||||||
extccomp.CC[extccomp.cCompiler].name ><
|
extccomp.CC[extccomp.cCompiler].name &
|
||||||
getCompileCFileCmd(filename, true)
|
getCompileCFileCmd(filename, true))
|
||||||
|
|
||||||
proc externalFileChanged(filename: string): bool =
|
proc externalFileChanged(filename: string): bool =
|
||||||
if gCmd notin {cmdCompileToC, cmdCompileToCpp, cmdCompileToOC, cmdCompileToLLVM}:
|
if gCmd notin {cmdCompileToC, cmdCompileToCpp, cmdCompileToOC, cmdCompileToLLVM}:
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ type
|
||||||
|
|
||||||
TModuleInMemory* = object
|
TModuleInMemory* = object
|
||||||
compiledAt*: float
|
compiledAt*: float
|
||||||
crc*: TCrc32
|
crc*: SecureHash
|
||||||
deps*: seq[int32] ## XXX: slurped files are currently not tracked
|
deps*: seq[int32] ## XXX: slurped files are currently not tracked
|
||||||
needsRecompile*: TNeedRecompile
|
needsRecompile*: TNeedRecompile
|
||||||
crcStatus*: TCrcStatus
|
crcStatus*: TCrcStatus
|
||||||
|
|
@ -51,19 +51,19 @@ proc crcChanged(fileIdx: int32): bool =
|
||||||
of crcNotChanged:
|
of crcNotChanged:
|
||||||
result = false
|
result = false
|
||||||
of crcCached:
|
of crcCached:
|
||||||
let newCrc = crcFromFile(fileIdx.toFilename)
|
let newCrc = secureHashFile(fileIdx.toFilename)
|
||||||
result = newCrc != gMemCacheData[fileIdx].crc
|
result = newCrc != gMemCacheData[fileIdx].crc
|
||||||
gMemCacheData[fileIdx].crc = newCrc
|
gMemCacheData[fileIdx].crc = newCrc
|
||||||
updateStatus()
|
updateStatus()
|
||||||
of crcNotTaken:
|
of crcNotTaken:
|
||||||
gMemCacheData[fileIdx].crc = crcFromFile(fileIdx.toFilename)
|
gMemCacheData[fileIdx].crc = secureHashFile(fileIdx.toFilename)
|
||||||
result = true
|
result = true
|
||||||
updateStatus()
|
updateStatus()
|
||||||
|
|
||||||
proc doCRC(fileIdx: int32) =
|
proc doCRC(fileIdx: int32) =
|
||||||
if gMemCacheData[fileIdx].crcStatus == crcNotTaken:
|
if gMemCacheData[fileIdx].crcStatus == crcNotTaken:
|
||||||
# echo "FIRST CRC: ", fileIdx.ToFilename
|
# echo "FIRST CRC: ", fileIdx.ToFilename
|
||||||
gMemCacheData[fileIdx].crc = crcFromFile(fileIdx.toFilename)
|
gMemCacheData[fileIdx].crc = secureHashFile(fileIdx.toFilename)
|
||||||
|
|
||||||
proc addDep(x: PSym, dep: int32) =
|
proc addDep(x: PSym, dep: int32) =
|
||||||
growCache gMemCacheData, dep
|
growCache gMemCacheData, dep
|
||||||
|
|
|
||||||
|
|
@ -538,7 +538,7 @@ proc cmdChangeTriggersRecompilation(old, new: TCommands): bool =
|
||||||
# else: trigger recompilation:
|
# else: trigger recompilation:
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
proc processRodFile(r: PRodReader, crc: TCrc32) =
|
proc processRodFile(r: PRodReader, crc: SecureHash) =
|
||||||
var
|
var
|
||||||
w: string
|
w: string
|
||||||
d, inclCrc: int
|
d, inclCrc: int
|
||||||
|
|
@ -598,7 +598,7 @@ proc processRodFile(r: PRodReader, crc: TCrc32) =
|
||||||
inc(r.pos) # skip ' '
|
inc(r.pos) # skip ' '
|
||||||
inclCrc = decodeVInt(r.s, r.pos)
|
inclCrc = decodeVInt(r.s, r.pos)
|
||||||
if r.reason == rrNone:
|
if r.reason == rrNone:
|
||||||
if not existsFile(w) or (inclCrc != int(crcFromFile(w))):
|
if not existsFile(w) or (inclCrc != int(secureHashFile(w))):
|
||||||
r.reason = rrInclDeps
|
r.reason = rrInclDeps
|
||||||
if r.s[r.pos] == '\x0A':
|
if r.s[r.pos] == '\x0A':
|
||||||
inc(r.pos)
|
inc(r.pos)
|
||||||
|
|
@ -649,7 +649,7 @@ proc startsWith(buf: cstring, token: string, pos = 0): bool =
|
||||||
while s < token.len and buf[pos+s] == token[s]: inc s
|
while s < token.len and buf[pos+s] == token[s]: inc s
|
||||||
result = s == token.len
|
result = s == token.len
|
||||||
|
|
||||||
proc newRodReader(modfilename: string, crc: TCrc32,
|
proc newRodReader(modfilename: string, crc: SecureHash,
|
||||||
readerIndex: int): PRodReader =
|
readerIndex: int): PRodReader =
|
||||||
new(result)
|
new(result)
|
||||||
try:
|
try:
|
||||||
|
|
@ -701,7 +701,7 @@ type
|
||||||
filename*: string
|
filename*: string
|
||||||
reason*: TReasonForRecompile
|
reason*: TReasonForRecompile
|
||||||
rd*: PRodReader
|
rd*: PRodReader
|
||||||
crc*: TCrc32
|
crc*: SecureHash
|
||||||
crcDone*: bool
|
crcDone*: bool
|
||||||
|
|
||||||
TFileModuleMap = seq[TFileModuleRec]
|
TFileModuleMap = seq[TFileModuleRec]
|
||||||
|
|
@ -794,13 +794,13 @@ proc loadMethods(r: PRodReader) =
|
||||||
r.methods.add(rrGetSym(r, d, unknownLineInfo()))
|
r.methods.add(rrGetSym(r, d, unknownLineInfo()))
|
||||||
if r.s[r.pos] == ' ': inc(r.pos)
|
if r.s[r.pos] == ' ': inc(r.pos)
|
||||||
|
|
||||||
proc getCRC*(fileIdx: int32): TCrc32 =
|
proc getCRC*(fileIdx: int32): SecureHash =
|
||||||
internalAssert fileIdx >= 0 and fileIdx < gMods.len
|
internalAssert fileIdx >= 0 and fileIdx < gMods.len
|
||||||
|
|
||||||
if gMods[fileIdx].crcDone:
|
if gMods[fileIdx].crcDone:
|
||||||
return gMods[fileIdx].crc
|
return gMods[fileIdx].crc
|
||||||
|
|
||||||
result = crcFromFile(fileIdx.toFilename)
|
result = secureHashFile(fileIdx.toFilename)
|
||||||
gMods[fileIdx].crc = result
|
gMods[fileIdx].crc = result
|
||||||
|
|
||||||
template growCache*(cache, pos) =
|
template growCache*(cache, pos) =
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ import
|
||||||
type
|
type
|
||||||
TRodWriter = object of TPassContext
|
TRodWriter = object of TPassContext
|
||||||
module: PSym
|
module: PSym
|
||||||
crc: TCrc32
|
crc: SecureHash
|
||||||
options: TOptions
|
options: TOptions
|
||||||
defines: string
|
defines: string
|
||||||
inclDeps: string
|
inclDeps: string
|
||||||
|
|
@ -38,7 +38,7 @@ type
|
||||||
|
|
||||||
PRodWriter = ref TRodWriter
|
PRodWriter = ref TRodWriter
|
||||||
|
|
||||||
proc newRodWriter(crc: TCrc32, module: PSym): PRodWriter
|
proc newRodWriter(crc: SecureHash, module: PSym): PRodWriter
|
||||||
proc addModDep(w: PRodWriter, dep: string)
|
proc addModDep(w: PRodWriter, dep: string)
|
||||||
proc addInclDep(w: PRodWriter, dep: string)
|
proc addInclDep(w: PRodWriter, dep: string)
|
||||||
proc addInterfaceSym(w: PRodWriter, s: PSym)
|
proc addInterfaceSym(w: PRodWriter, s: PSym)
|
||||||
|
|
@ -62,7 +62,7 @@ proc fileIdx(w: PRodWriter, filename: string): int =
|
||||||
template filename*(w: PRodWriter): string =
|
template filename*(w: PRodWriter): string =
|
||||||
w.module.filename
|
w.module.filename
|
||||||
|
|
||||||
proc newRodWriter(crc: TCrc32, module: PSym): PRodWriter =
|
proc newRodWriter(crc: SecureHash, module: PSym): PRodWriter =
|
||||||
new(result)
|
new(result)
|
||||||
result.sstack = @[]
|
result.sstack = @[]
|
||||||
result.tstack = @[]
|
result.tstack = @[]
|
||||||
|
|
@ -96,7 +96,7 @@ proc addInclDep(w: PRodWriter, dep: string) =
|
||||||
var resolved = dep.findModule(w.module.info.toFullPath)
|
var resolved = dep.findModule(w.module.info.toFullPath)
|
||||||
encodeVInt(fileIdx(w, dep), w.inclDeps)
|
encodeVInt(fileIdx(w, dep), w.inclDeps)
|
||||||
add(w.inclDeps, " ")
|
add(w.inclDeps, " ")
|
||||||
encodeVInt(crcFromFile(resolved), w.inclDeps)
|
encodeVInt(secureHashFile(resolved), w.inclDeps)
|
||||||
add(w.inclDeps, rodNL)
|
add(w.inclDeps, rodNL)
|
||||||
|
|
||||||
proc pushType(w: PRodWriter, t: PType) =
|
proc pushType(w: PRodWriter, t: PType) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue