summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorFlaviu Tamas <tamasflaviu@gmail.com>2015-05-23 12:59:15 -0400
committerFlaviu Tamas <tamasflaviu@gmail.com>2015-05-23 13:01:52 -0400
commit188bba2b3cd3106e0c615e712b8f5fdd995137bb (patch)
tree8343b9cf10d802edc48f25f8b263fcf92e6737b6 /compiler
parent5ad9d874c32a72ab473d3455b5d72f915729d195 (diff)
downloadNim-188bba2b3cd3106e0c615e712b8f5fdd995137bb.tar.gz
Clean up crc module
Use better names, remove quite a bit of dead code.
Change `><` to a name that's actually descriptive.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/crc.nim64
-rw-r--r--compiler/extccomp.nim14
-rw-r--r--compiler/modules.nim8
-rw-r--r--compiler/rodread.nim12
-rw-r--r--compiler/rodwrite.nim8
5 files changed, 30 insertions, 76 deletions
diff --git a/compiler/crc.nim b/compiler/crc.nim
index a8b61f2a5..7a2b6e4c2 100644
--- a/compiler/crc.nim
+++ b/compiler/crc.nim
@@ -11,18 +11,12 @@ import
   strutils
 
 type 
-  TCrc32* = int32
+  SecureHash* = int32
 
 const 
-  InitCrc32* = TCrc32(- 1)
-  InitAdler32* = int32(1)
+  InitCrc32 = SecureHash(- 1)
 
-proc updateCrc32*(val: int8, crc: TCrc32): TCrc32 {.inline.}
-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
+proc secureHashFile*(filename: string): SecureHash
 # implementation
 
 type 
@@ -74,31 +68,22 @@ const
     - 1000256840, 1567103746, 711928724, - 1274298825, - 1022587231, 1510334235, 
     755167117]
 
-proc updateCrc32(val: int8, crc: TCrc32): TCrc32 = 
-  result = TCrc32(crc32table[(int(crc) xor (int(val) and 0x000000FF)) and
-      0x000000FF]) xor (crc shr TCrc32(8))
+proc updateCrc32(val: int8, crc: SecureHash): SecureHash = 
+  result = SecureHash(crc32table[(int(crc) xor (int(val) and 0x000000FF)) and
+      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)
 
-proc strCrc32(s: string): TCrc32 = 
+proc secureHash*(s: string): SecureHash = 
   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)  
   
 type 
   TByteArray = array[0..10000000, int8]
   PByteArray = ref TByteArray
 
-proc crcFromBuf(buf: pointer, length: int): TCrc32 = 
-  var p = cast[PByteArray](buf)
-  result = InitCrc32
-  for i in countup(0, length - 1): result = updateCrc32(p[i], result)
-  
-proc crcFromFile(filename: string): TCrc32 = 
+proc secureHashFile(filename: string): SecureHash = 
   const 
     bufSize = 8000 # don't use 8K for the memory allocator!
   var 
@@ -114,34 +99,3 @@ proc crcFromFile(filename: string): TCrc32 =
     if readBytes != bufSize: break 
   dealloc(buf)
   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
diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim
index 186a3884d..cd7ac2a68 100644
--- a/compiler/extccomp.nim
+++ b/compiler/extccomp.nim
@@ -572,13 +572,13 @@ proc getCompileCFileCmd*(cfilename: string, isExternal = false): string =
     "nim", quoteShell(getPrefixDir()),
     "lib", quoteShell(libpath)])
 
-proc footprint(filename: string): TCrc32 =
-  # note, '><' further modifies a crc value with a string.
-  result = crcFromFile(filename) ><
-      platform.OS[targetOS].name ><
-      platform.CPU[targetCPU].name ><
-      extccomp.CC[extccomp.cCompiler].name ><
-      getCompileCFileCmd(filename, true)
+proc footprint(filename: string): SecureHash =
+  result = secureHash(
+    $secureHashFile(filename) &
+    platform.OS[targetOS].name &
+    platform.CPU[targetCPU].name &
+    extccomp.CC[extccomp.cCompiler].name &
+    getCompileCFileCmd(filename, true))
 
 proc externalFileChanged(filename: string): bool =
   if gCmd notin {cmdCompileToC, cmdCompileToCpp, cmdCompileToOC, cmdCompileToLLVM}:
diff --git a/compiler/modules.nim b/compiler/modules.nim
index 2fa46f356..09abd5da6 100644
--- a/compiler/modules.nim
+++ b/compiler/modules.nim
@@ -19,7 +19,7 @@ type
 
   TModuleInMemory* = object
     compiledAt*: float
-    crc*: TCrc32
+    crc*: SecureHash
     deps*: seq[int32] ## XXX: slurped files are currently not tracked
     needsRecompile*: TNeedRecompile
     crcStatus*: TCrcStatus
@@ -51,19 +51,19 @@ proc crcChanged(fileIdx: int32): bool =
   of crcNotChanged:
     result = false
   of crcCached:
-    let newCrc = crcFromFile(fileIdx.toFilename)
+    let newCrc = secureHashFile(fileIdx.toFilename)
     result = newCrc != gMemCacheData[fileIdx].crc
     gMemCacheData[fileIdx].crc = newCrc
     updateStatus()
   of crcNotTaken:
-    gMemCacheData[fileIdx].crc = crcFromFile(fileIdx.toFilename)
+    gMemCacheData[fileIdx].crc = secureHashFile(fileIdx.toFilename)
     result = true
     updateStatus()
 
 proc doCRC(fileIdx: int32) =
   if gMemCacheData[fileIdx].crcStatus == crcNotTaken:
     # echo "FIRST CRC: ", fileIdx.ToFilename
-    gMemCacheData[fileIdx].crc = crcFromFile(fileIdx.toFilename)
+    gMemCacheData[fileIdx].crc = secureHashFile(fileIdx.toFilename)
 
 proc addDep(x: PSym, dep: int32) =
   growCache gMemCacheData, dep
diff --git a/compiler/rodread.nim b/compiler/rodread.nim
index e92f7ecfa..066a02b1d 100644
--- a/compiler/rodread.nim
+++ b/compiler/rodread.nim
@@ -538,7 +538,7 @@ proc cmdChangeTriggersRecompilation(old, new: TCommands): bool =
   # else: trigger recompilation:
   result = true
   
-proc processRodFile(r: PRodReader, crc: TCrc32) = 
+proc processRodFile(r: PRodReader, crc: SecureHash) = 
   var 
     w: string
     d, inclCrc: int
@@ -598,7 +598,7 @@ proc processRodFile(r: PRodReader, crc: TCrc32) =
         inc(r.pos)            # skip ' '
         inclCrc = decodeVInt(r.s, r.pos)
         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
         if r.s[r.pos] == '\x0A': 
           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
   result = s == token.len
 
-proc newRodReader(modfilename: string, crc: TCrc32, 
+proc newRodReader(modfilename: string, crc: SecureHash, 
                   readerIndex: int): PRodReader = 
   new(result)
   try:
@@ -701,7 +701,7 @@ type
     filename*: string
     reason*: TReasonForRecompile
     rd*: PRodReader
-    crc*: TCrc32
+    crc*: SecureHash
     crcDone*: bool
 
   TFileModuleMap = seq[TFileModuleRec]
@@ -794,13 +794,13 @@ proc loadMethods(r: PRodReader) =
     r.methods.add(rrGetSym(r, d, unknownLineInfo()))
     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
 
   if gMods[fileIdx].crcDone:
     return gMods[fileIdx].crc
   
-  result = crcFromFile(fileIdx.toFilename)
+  result = secureHashFile(fileIdx.toFilename)
   gMods[fileIdx].crc = result
 
 template growCache*(cache, pos) =
diff --git a/compiler/rodwrite.nim b/compiler/rodwrite.nim
index e178b7ce6..c83e728db 100644
--- a/compiler/rodwrite.nim
+++ b/compiler/rodwrite.nim
@@ -20,7 +20,7 @@ import
 type 
   TRodWriter = object of TPassContext
     module: PSym
-    crc: TCrc32
+    crc: SecureHash
     options: TOptions
     defines: string
     inclDeps: string
@@ -38,7 +38,7 @@ type
 
   PRodWriter = ref TRodWriter
 
-proc newRodWriter(crc: TCrc32, module: PSym): PRodWriter
+proc newRodWriter(crc: SecureHash, module: PSym): PRodWriter
 proc addModDep(w: PRodWriter, dep: string)
 proc addInclDep(w: PRodWriter, dep: string)
 proc addInterfaceSym(w: PRodWriter, s: PSym)
@@ -62,7 +62,7 @@ proc fileIdx(w: PRodWriter, filename: string): int =
 template filename*(w: PRodWriter): string =
   w.module.filename
 
-proc newRodWriter(crc: TCrc32, module: PSym): PRodWriter = 
+proc newRodWriter(crc: SecureHash, module: PSym): PRodWriter = 
   new(result)
   result.sstack = @[]
   result.tstack = @[]
@@ -96,7 +96,7 @@ proc addInclDep(w: PRodWriter, dep: string) =
   var resolved = dep.findModule(w.module.info.toFullPath)
   encodeVInt(fileIdx(w, dep), w.inclDeps)
   add(w.inclDeps, " ")
-  encodeVInt(crcFromFile(resolved), w.inclDeps)
+  encodeVInt(secureHashFile(resolved), w.inclDeps)
   add(w.inclDeps, rodNL)
 
 proc pushType(w: PRodWriter, t: PType) =