Просмотр исходного кода

* Binary writing is now a bit faster as the hash is calculated on the fly now.

svn: 1754
Bryan Drewery 21 лет назад
Родитель
Сommit
2b711800b9
2 измененных файлов с 24 добавлено и 12 удалено
  1. 1 0
      doc/UPDATES
  2. 23 12
      src/binary.c

+ 1 - 0
doc/UPDATES

@@ -5,6 +5,7 @@ Lines prefixxed with '-' were disabled before release and are not finishsed.
 1.2.3
 * Read in '.resolv.conf' before '/etc/resolv.conf'
 * Fixed binary getting an excess of 1-15 bytes during write. 
+* Binary writing is now a bit faster as the hash is calculated on the fly now.
 
 1.2.2
 * Don't sanity check flags for users on DCC CHAT if they are on the bot via .botcmd.

+ 23 - 12
src/binary.c

@@ -51,20 +51,24 @@ bin_checksum(const char *fname, int todo, MD5_CTX * ctx)
   size_t len = 0;
 
   checked_bin_buf++;
-  if (!(f = fopen(fname, "rb")))
-    werr(ERR_BINSTAT);
+ 
+  hash[0] = 0;
+
+  if (todo == GET_CHECKSUM) {
+    if (!(f = fopen(fname, "rb")))
+      werr(ERR_BINSTAT);
 
-  while ((len = fread(buf, 1, sizeof buf - 1, f))) {
-    if (!memcmp(buf, &settings.prefix, PREFIXLEN)) {
-      break;
+    while ((len = fread(buf, 1, sizeof buf - 1, f))) {
+      if (!memcmp(buf, &settings.prefix, PREFIXLEN))
+        break;
+      MD5_Update(ctx, buf, len);
     }
-    MD5_Update(ctx, buf, len);
-  }
 
-  fclose(f);
-  MD5_Final(md5out, ctx);
-  strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
-  OPENSSL_cleanse(&ctx, sizeof(ctx));
+    fclose(f);
+    MD5_Final(md5out, ctx);
+    strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
+    OPENSSL_cleanse(&ctx, sizeof(ctx));
+  }
 
   if (todo & WRITE_CHECKSUM) {
     Tempfile *newbin = new Tempfile("bin");
@@ -96,9 +100,14 @@ bin_checksum(const char *fname, int todo, MD5_CTX * ctx)
         delete newbin;
         werr(ERR_BINSTAT);
       }
+
       pos += len;
 
       if (!memcmp(buf, &settings.prefix, PREFIXLEN)) {		/* found the settings struct! */
+        MD5_Final(md5out, ctx);
+        strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
+        OPENSSL_cleanse(&ctx, sizeof(ctx));
+
         strlcpy(settings.hash, hash, 65);
         edpack(&settings, hash, PACK_ENC);		/* encrypt the entire struct with the hash (including hash) */
 
@@ -124,7 +133,8 @@ bin_checksum(const char *fname, int todo, MD5_CTX * ctx)
         skip_bytes += SIZE_PAD;
         fseek(newbin->f, pos + SIZE_PAD, SEEK_SET);
         pos += SIZE_PAD;
-      }
+      } else if (!hash[0])
+        MD5_Update(ctx, buf, len);
     }
 
     fclose(f);
@@ -139,6 +149,7 @@ bin_checksum(const char *fname, int todo, MD5_CTX * ctx)
     unlink(fname_bak);
     delete newbin;
   }
+
   return hash;
 }