From: =?utf-8?b?0L3QsNCx?= <nabijaczleweli@nabijaczleweli.xyz>
Date: Thu, 21 Aug 2025 20:42:02 +0200
Subject: Deduplicate die/reportErr/readFile

---
 sfnt2woff-zopfli.h | 39 +++++++++++++++++++++++++++++++++++++++
 sfnt2woff.c        | 41 +----------------------------------------
 woff2sfnt.c        | 41 +----------------------------------------
 3 files changed, 41 insertions(+), 80 deletions(-)
 create mode 100644 sfnt2woff-zopfli.h

diff --git a/sfnt2woff-zopfli.h b/sfnt2woff-zopfli.h
new file mode 100644
index 0000000..8e5ab84
--- /dev/null
+++ b/sfnt2woff-zopfli.h
@@ -0,0 +1,39 @@
+static void
+die(const char * msg)
+{
+  fprintf(stderr, "# fatal error: %s\n", msg);
+  exit(2);
+}
+
+static void
+reportErr(uint32_t status)
+{
+  woffPrintStatus(stderr, status, "### ");
+  exit(status & 0xff);
+}
+
+const uint8_t *
+readFile(const char * name, uint32_t * len)
+{
+  FILE * inFile = fopen(name, "rb");
+  if (!inFile) {
+    char buf[200];
+    sprintf(buf, "unable to open file %s", name);
+    die(buf);
+  }
+
+  if (fseek(inFile, 0, SEEK_END) != 0)
+    die("seek failure");
+  *len = ftell(inFile);
+  if (fseek(inFile, 0, SEEK_SET) != 0)
+    die("seek failure");
+
+  uint8_t * data = (uint8_t *) malloc(*len);
+  if (!data)
+    die("malloc failure");
+  if (fread(data, 1, *len, inFile) != *len)
+    die("file read failure");
+  fclose(inFile);
+
+  return data;
+}
diff --git a/sfnt2woff.c b/sfnt2woff.c
index 943f2b1..83622ea 100644
--- a/sfnt2woff.c
+++ b/sfnt2woff.c
@@ -41,20 +41,7 @@
 #include <unistd.h>
 
 #include "woff.h"
-
-static void
-die(const char * msg)
-{
-  fprintf(stderr, "# fatal error: %s\n", msg);
-  exit(2);
-}
-
-static void
-reportErr(uint32_t status)
-{
-  woffPrintStatus(stderr, status, "### ");
-  exit(2);
-}
+#include "sfnt2woff-zopfli.h"
 
 static void
 usage(const char * progName)
@@ -70,32 +57,6 @@ usage(const char * progName)
                   , progName);
 }
 
-const uint8_t *
-readFile(const char * name, uint32_t * len)
-{
-  FILE * inFile = fopen(name, "rb");
-  if (!inFile) {
-    char buf[200];
-    sprintf(buf, "unable to open file %s", name);
-    die(buf);
-  }
-
-  if (fseek(inFile, 0, SEEK_END) != 0)
-    die("seek failure");
-  *len = ftell(inFile);
-  if (fseek(inFile, 0, SEEK_SET) != 0)
-    die("seek failure");
-
-  uint8_t * data = (uint8_t *) malloc(*len);
-  if (!data)
-    die("malloc failure");
-  if (fread(data, 1, *len, inFile) != *len)
-    die("file read failure");
-  fclose(inFile);
-
-  return data;
-}
-
 int
 main(int argc, char * argv[])
 {
diff --git a/woff2sfnt.c b/woff2sfnt.c
index c4d39fd..948f860 100644
--- a/woff2sfnt.c
+++ b/woff2sfnt.c
@@ -44,20 +44,7 @@
 #endif
 
 #include "woff.h"
-
-static void
-die(const char * msg)
-{
-  fprintf(stderr, "# fatal error: %s\n", msg);
-  exit(2);
-}
-
-static void
-reportErr(uint32_t status)
-{
-  woffPrintStatus(stderr, status, "### ");
-  exit(status & 0xff);
-}
+#include "sfnt2woff-zopfli.h"
 
 static void
 usage(const char * progName)
@@ -73,32 +60,6 @@ usage(const char * progName)
                   , progName);
 }
 
-const uint8_t *
-readFile(const char * name, uint32_t * len)
-{
-  FILE * inFile = fopen(name, "rb");
-  if (!inFile) {
-    char buf[200];
-    sprintf(buf, "unable to open file %s", name);
-    die(buf);
-  }
-
-  if (fseek(inFile, 0, SEEK_END) != 0)
-    die("seek failure");
-  *len = ftell(inFile);
-  if (fseek(inFile, 0, SEEK_SET) != 0)
-    die("seek failure");
-
-  uint8_t * data = (uint8_t *) malloc(*len);
-  if (!data)
-    die("malloc failure");
-  if (fread(data, 1, *len, inFile) != *len)
-    die("file read failure");
-  fclose(inFile);
-
-  return data;
-}
-
 int
 main(int argc, char *argv[])
 {
