[hackers] [farbfeld] Add util.{c|h} to deduplicate code || Laslo Hunhold
commit d25f3c73afa5beb83121c9b033bc0146c755ca3a
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Thu Mar 30 09:49:48 2017 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Thu Mar 30 09:49:48 2017 +0200
Add util.{c|h} to deduplicate code
diff --git a/ff2jpg.c b/ff2jpg.c
index 6f88a23..51f1fe6 100644
--- a/ff2jpg.c
+++ b/ff2jpg.c
_AT_@ -11,8 +11,7 @@
#include <jpeglib.h>
#include "arg.h"
-
-char *argv0;
+#include "util.h"
METHODDEF(void)
jpeg_error(j_common_ptr js)
_AT_@ -37,7 +36,7 @@ main(int argc, char *argv[])
struct jpeg_error_mgr jerr;
size_t rowlen;
uint64_t a;
- uint32_t hdr[4], width, height, i, j, k, l;
+ uint32_t width, height, i, j, k, l;
uint16_t *row, mask[3] = { 0xffff, 0xffff, 0xffff };
uint8_t *rowout;
char *color, colfmt[] = "%#x%#x%#x";
_AT_@ -75,19 +74,11 @@ main(int argc, char *argv[])
usage();
} ARGEND
- if (argc)
+ if (argc) {
usage();
-
- /* header */
- if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) {
- goto readerr;
}
- if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
- fprintf(stderr, "%s: invalid magic value\n", argv0);
- return 1;
- }
- width = ntohl(hdr[2]);
- height = ntohl(hdr[3]);
+
+ read_ff_header(&width, &height);
if (width > SIZE_MAX / ((sizeof("RGBA") - 1) * sizeof(uint16_t))) {
fprintf(stderr, "%s: row length integer overflow\n", argv0);
diff --git a/ff2pam.c b/ff2pam.c
index 93bd26c..aca15cb 100644
--- a/ff2pam.c
+++ b/ff2pam.c
_AT_@ -8,12 +8,12 @@
#include <string.h>
#include <unistd.h>
-static char *argv0;
+#include "util.h"
int
main(int argc, char *argv[])
{
- uint32_t hdr[4], width, height;
+ uint32_t width, height;
char buf[BUFSIZ];
size_t n, t;
_AT_@ -24,19 +24,9 @@ main(int argc, char *argv[])
return 1;
}
- /* header */
- if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) {
- fprintf(stderr, "%s: file too short\n", argv0);
- return 1;
- }
- if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
- fprintf(stderr, "%s: invalid magic value\n", argv0);
- return 1;
- }
- width = ntohl(hdr[2]);
- height = ntohl(hdr[3]);
+ read_ff_header(&width, &height);
- /* write header */
+ /* write PAM header */
printf("P7\n"
"WIDTH %" PRIu32 "\n"
"HEIGHT %" PRIu32 "\n"
diff --git a/ff2png.c b/ff2png.c
index 5731082..360ab00 100644
--- a/ff2png.c
+++ b/ff2png.c
_AT_@ -9,11 +9,12 @@
#include <png.h>
-static char *argv0;
+#include "util.h"
void
pngerr(png_structp pngs, const char *msg)
{
+ (void)pngs;
fprintf(stderr, "%s: libpng: %s\n", argv0, msg);
exit(1);
}
_AT_@ -24,7 +25,7 @@ main(int argc, char *argv[])
png_structp pngs;
png_infop pngi;
size_t rowlen;
- uint32_t hdr[4], width, height, i;
+ uint32_t width, height, i;
uint16_t *row;
argv0 = argv[0], argc--, argv++;
_AT_@ -34,16 +35,7 @@ main(int argc, char *argv[])
return 1;
}
- /* header */
- if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) {
- goto readerr;
- }
- if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
- fprintf(stderr, "%s: invalid magic value\n", argv0);
- return 1;
- }
- width = ntohl(hdr[2]);
- height = ntohl(hdr[3]);
+ read_ff_header(&width, &height);
/* load png */
pngs = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, pngerr,
diff --git a/ff2ppm.c b/ff2ppm.c
index 2a67eb2..f0f3956 100644
--- a/ff2ppm.c
+++ b/ff2ppm.c
_AT_@ -9,8 +9,7 @@
#include <string.h>
#include "arg.h"
-
-char *argv0;
+#include "util.h"
static void
usage(void)
_AT_@ -24,7 +23,7 @@ main(int argc, char *argv[])
{
size_t rowlen;
uint64_t a;
- uint32_t hdr[4], width, height, i, j, k, l;
+ uint32_t width, height, i, j, k, l;
uint16_t *row, mask[3] = { 0xffff, 0xffff, 0xffff };
uint8_t *rowout;
char *color, colfmt[] = "%#x%#x%#x";
_AT_@ -58,16 +57,7 @@ main(int argc, char *argv[])
if (argc)
usage();
- /* header */
- if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) {
- goto readerr;
- }
- if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
- fprintf(stderr, "%s: invalid magic value\n", argv0);
- return 1;
- }
- width = ntohl(hdr[2]);
- height = ntohl(hdr[3]);
+ read_ff_header(&width, &height);
if (width > SIZE_MAX / ((sizeof("RGBA") - 1) * sizeof(uint16_t))) {
fprintf(stderr, "%s: row length integer overflow\n", argv0);
diff --git a/jpg2ff.c b/jpg2ff.c
index 8ebc81d..85c935d 100644
--- a/jpg2ff.c
+++ b/jpg2ff.c
_AT_@ -8,7 +8,7 @@
#include <jpeglib.h>
-static char *argv0;
+#include "util.h"
METHODDEF(void)
jpeg_error(j_common_ptr js)
_AT_@ -23,7 +23,7 @@ main(int argc, char *argv[])
{
struct jpeg_decompress_struct js;
struct jpeg_error_mgr jerr;
- uint32_t width, height, tmp32;
+ uint32_t width, height;
uint16_t *row;
size_t rowlen, i;
JSAMPARRAY jpgrow;
_AT_@ -63,14 +63,8 @@ main(int argc, char *argv[])
return 1;
}
- /* write header */
- fputs("farbfeld", stdout);
- tmp32 = htonl(width);
- if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1)
- goto writerr;
- tmp32 = htonl(height);
- if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1)
- goto writerr;
+ /* write data */
+ write_ff_header(width, height);
while (js.output_scanline < js.output_height) {
/* jpeg_read_scanlines expects an array of pointers to
_AT_@ -87,7 +81,6 @@ main(int argc, char *argv[])
row[4*i + 3] = htons(65535);
}
- /* write data */
if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen)
goto writerr;
}
diff --git a/png2ff.c b/png2ff.c
index 55456e3..515979d 100644
--- a/png2ff.c
+++ b/png2ff.c
_AT_@ -8,7 +8,7 @@
#include <png.h>
-static char *argv0;
+#include "util.h"
void
pngerr(png_structp pngs, const char *msg)
_AT_@ -22,7 +22,7 @@ main(int argc, char *argv[])
{
png_structp pngs;
png_infop pngi;
- uint32_t width, height, rowlen, tmp32, r, i;
+ uint32_t width, height, rowlen, r, i;
uint16_t *row;
uint8_t **pngrows;
_AT_@ -66,16 +66,9 @@ main(int argc, char *argv[])
return 1;
}
- /* write header */
- fputs("farbfeld", stdout);
- tmp32 = htonl(width);
- if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1)
- goto writerr;
- tmp32 = htonl(height);
- if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1)
- goto writerr;
-
/* write data */
+ write_ff_header(width, height);
+
switch(png_get_bit_depth(pngs, pngi)) {
case 8:
for (r = 0; r < height; ++r) {
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..b374df9
--- /dev/null
+++ b/util.c
_AT_@ -0,0 +1,51 @@
+/* See LICENSE file for copyright and license details. */
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+
+char *argv0;
+
+void
+read_ff_header(uint32_t *width, uint32_t *height)
+{
+ uint32_t hdr[4];
+
+ if (fread(hdr, sizeof(*hdr), LEN(hdr), stdin) != LEN(hdr)) {
+ fprintf(stderr, "%s: fread: %s", argv0, strerror(errno));
+ exit(1);
+ }
+
+ if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
+ fprintf(stderr, "%s: invalid magic value\n", argv0);
+ exit(1);
+ }
+
+ *width = ntohl(hdr[2]);
+ *height = ntohl(hdr[3]);
+}
+
+void
+write_ff_header(uint32_t width, uint32_t height)
+{
+ uint32_t tmp;
+
+ fputs("farbfeld", stdout);
+
+ tmp = htonl(width);
+ if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) {
+ fprintf(stderr, "%s: write: %s", argv0, strerror(errno));
+ exit(1);
+ }
+
+ tmp = htonl(height);
+ if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) {
+ fprintf(stderr, "%s: write: %s", argv0, strerror(errno));
+ exit(1);
+ }
+}
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..02aee25
--- /dev/null
+++ b/util.h
_AT_@ -0,0 +1,10 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdint.h>
+#include <stdio.h>
+
+extern char *argv0;
+
+#define LEN(x) (sizeof (x) / sizeof *(x))
+
+void read_ff_header(uint32_t *width, uint32_t *height);
+void write_ff_header(uint32_t width, uint32_t height);
Received on Thu Mar 30 2017 - 09:50:29 CEST
This archive was generated by hypermail 2.3.0
: Thu Mar 30 2017 - 10:00:17 CEST