chiark / gitweb /
Use shifts rather than multiplies to scale up size requests.
[vbig.git] / vbig.cc
diff --git a/vbig.cc b/vbig.cc
index a71a42dfb5826f2c2277ceda01dae760df167ec4..86e24bf45d26d2f35689733ffdf3d9095fcd7e14 100644 (file)
--- a/vbig.cc
+++ b/vbig.cc
@@ -1,6 +1,7 @@
 /*
  * This file is part of vbig.
- * Copyright (C) 2011 Richard Kettlewell
+ * Copyright (C) 2011, 2013 Richard Kettlewell
+ * Copyright (C) 2013 Ian Jackson
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -158,20 +159,25 @@ int main(int argc, char **argv) {
   }
   argc -= optind;
   argv += optind;
+  /* expect PATH [SIZE] */
   if(argc > 2)
     fatal(0, "excess arguments");
+  /* If --both but no SIZE, assume a block device, which is to be filled */
   if(argc == 1 && mode == BOTH)
     entireopt = true;
   if(entireopt) {
     if(argc != 1)
       fatal(0, "with --entire, size should not be specified");
   } else {
+    /* --create without --entire requires PATH SIZE
+     * --verify just requires PATH, SIZE is optional */
     if(argc < (mode == VERIFY ? 1 : 2))
       fatal(0, "insufficient arguments");
   }
   if(seed && seedpath)
     fatal(0, "both --seed and --seed-file specified");
   if(mode == BOTH && !seed && !seedpath) {
+    /* --both and no seed specified; pick a random one */
 #ifdef HAVE_RANDOM_DEVICE
     seedpath = RANDOM_DEVICE;
 #else
@@ -194,11 +200,13 @@ int main(int argc, char **argv) {
     fclose(seedfile);
   }
   if (!seed) {
+    /* No seed specified, use a constant */
     seed = (void*)default_seed;
     seedlen = sizeof(default_seed)-1;
   }
   path = argv[0];
   if(argc > 1) {
+    /* Explicit size specified */
     errno = 0;
     char *end;
     size = strtoll(argv[1], &end, 10);
@@ -207,16 +215,18 @@ int main(int argc, char **argv) {
     if(end == argv[1])
       fatal(0, "invalid size");
     if(!strcmp(end, "K"))
-      size *= 1024;
+      size <<= 10;
     else if(!strcmp(end, "M"))
-      size *= 1024 * 1024;
+      size <<= 20;
     else if(!strcmp(end, "G"))
-      size *= 1024 * 1024 * 1024;
+      size <<= 30;
     else if(*end)
       fatal(0, "invalid size");
   } else if(entireopt) {
-    size = LONG_LONG_MAX;
+    /* Use stupidly large size as a proxy for 'infinite' */
+    size = LLONG_MAX;
   } else {
+    /* Retrieve size from target (which must exist) */
     struct stat sb;
     if(stat(path, &sb) < 0)
       fatal(errno, "stat %s", path);
@@ -232,17 +242,20 @@ int main(int argc, char **argv) {
   return 0;
 }
 
+// flush stdout, fatal on error
 static void flushstdout() {
   if(ferror(stdout) || fflush(stdout))
     fatal(errno, "flush stdout");
 }
 
+// clear the progress indicator
 static void clearprogress() {
   if (!progress) return;
-  printf(" %-10s %*s   \r", "", sizeof(long long)*4, "");
+  printf(" %-10s %*s   \r", "", (int)sizeof(long long)*4, "");
   flushstdout();
 }
 
+// update progress indicator
 static void showprogress(long long amount, const char *show) {
   if (!progress) return;
 
@@ -253,7 +266,7 @@ static void showprogress(long long amount, const char *show) {
   int triples = sizeof(amount);
   char rawbuf[triples*3 + 1];
   char outbuf[triples*4 + 1];
-  snprintf(rawbuf, sizeof(rawbuf), "% *lld", sizeof(rawbuf)-1, amount);
+  snprintf(rawbuf, sizeof(rawbuf), "% *lld", (int)sizeof(rawbuf)-1, amount);
   for (int i=0; i<triples; i++) {
     outbuf[i*4] = ' ';
     memcpy(outbuf + i*4 + 1, rawbuf + i*3, 3);
@@ -263,8 +276,9 @@ static void showprogress(long long amount, const char *show) {
   flushstdout();
 }
 
+// write/verify the target file
 static long long execute(mode_type mode, bool entire, const char *show) {
-  Arcfour rng((const char*)seed, seedlen);
+  Arcfour rng((const uint8_t *)seed, seedlen);
   FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
   if(!fp)
     fatal(errno, "%s", path);
@@ -272,7 +286,7 @@ static long long execute(mode_type mode, bool entire, const char *show) {
     flushCache(fp);
   if(mode == CREATE && entire)
     setvbuf(fp, 0, _IONBF, 0);
-  char generated[4096], input[4096];
+  uint8_t generated[4096], input[4096];
   long long remain = size;
   static const size_t rc4drop = 3072; // en.wikipedia.org/wiki/RC4#Security
   assert(rc4drop <= sizeof(generated));
@@ -298,10 +312,11 @@ static long long execute(mode_type mode, bool entire, const char *show) {
       if(memcmp(generated, input, bytesRead)) {
         for(size_t n = 0; n < bytesRead; ++n)
           if(generated[n] != input[n])
-            fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
+            fatal(0, "%s: corrupted at %lld/%lld bytes (expected %d got %d)",
                     path, size - remain + n, size,
                     (unsigned char)generated[n], (unsigned char)input[n]);
       }
+      /* Truncated */
       if(bytesRead < bytesGenerated) {
        if(entire) {
          assert(feof(fp));
@@ -326,6 +341,7 @@ static long long execute(mode_type mode, bool entire, const char *show) {
   }
   if(fclose(fp) < 0)
     fatal(errno, "%s", path);
+  /* Actual size written/verified */
   long long done = size - remain;
   if(show) {
     printf("%lld bytes (%lldM, %lldG) %s\n",