Description: make the output of mkbindic reproducible.
Author: Chris Lamb <lamby@debian.org>
Bug-Debian: https://bugs.debian.org/861955
Forwarded: no
Reviewed-By: Francisco Vilmar Cardoso Ruviaro <francisco.ruviaro@riseup.net>
Last-Update: 2020-08-17

--- a/cmd/crxdic/crxdic.c
+++ b/cmd/crxdic/crxdic.c
@@ -31,6 +31,7 @@ static char rcsid[]="@(#) 102.1 $Id: crx
 #include <time.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <assert.h>
 #include "ccompat.h"
 #include "RKindep/file.h"
@@ -932,6 +933,9 @@ makeHeader(dic)
   unsigned		i;
   RkiCksumCalc		calc;
   unsigned		off;
+  char *source_date_epoch;
+  unsigned long long epoch;
+  char *endptr;
     
   if (RkiCksumCRCInit(&calc)
       || RkiCksumAdd(&calc, dic->Dir->buf, dic->Dir->dirsiz)) {
@@ -963,7 +967,32 @@ makeHeader(dic)
     hd.data[HD_CMPV].var = 0x300702L;
     hd.flag[HD_CMPV] = -1;
   }
-  hd.data[HD_TIME].var = tloc = time(0);
+  source_date_epoch = getenv("SOURCE_DATE_EPOCH");
+  if (source_date_epoch) {
+    errno = 0;
+    epoch = strtoull(source_date_epoch, &endptr, 10);
+    if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0))
+                    || (errno != 0 && epoch == 0)) {
+            fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: strtoull: %s\n", strerror(errno));
+            exit(EXIT_FAILURE);
+    }
+    if (endptr == source_date_epoch) {
+            fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: No digits were found: %s\n", endptr);
+            exit(EXIT_FAILURE);
+    }
+    if (*endptr != '\0') {
+            fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: Trailing garbage: %s\n", endptr);
+            exit(EXIT_FAILURE);
+    }
+    if (epoch > ULONG_MAX) {
+            fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to %lu but was found to be: %llu \n", ULONG_MAX, epoch);
+            exit(EXIT_FAILURE);
+    }
+    tloc = epoch;
+  } else {
+    tloc = time(0);
+  }
+  hd.data[HD_TIME].var = tloc;
   hd.flag[HD_TIME] = -1;
   hd.data[HD_DMNM].ptr = (unsigned char *)STrdup(dic->name);
   hd.flag[HD_DMNM] = strlen(dic->name);
