From 54b253d655737510f242a1659762928495525a2a Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Sun, 13 Jan 2019 15:44:29 -0500 Subject: [PATCH] common: Add a global function to read the contents of a file. Most backends now utilize it. The stragglers do something special. --- backend_common.c | 30 ++++++++++++++++++++++++++++++ backend_common.h | 3 +++ backend_kodak1400.c | 12 +++--------- backend_kodak605.c | 12 +++--------- backend_kodak6800.c | 12 +++--------- backend_mitsu9550.c | 27 ++++++++------------------- backend_shinkos1245.c | 20 +++++--------------- backend_shinkos2145.c | 13 ++++--------- backend_shinkos6145.c | 13 ++++--------- backend_shinkos6245.c | 13 ++++--------- 10 files changed, 67 insertions(+), 88 deletions(-) diff --git a/backend_common.c b/backend_common.c index 54cfd30..82726aa 100644 --- a/backend_common.c +++ b/backend_common.c @@ -28,6 +28,7 @@ */ #include "backend_common.h" +#include #define BACKEND_VERSION "0.91" #ifndef URI_PREFIX @@ -1445,6 +1446,35 @@ minimal: } } +int dyesub_read_data(char *filename, void *databuf, int datalen, + int *actual_len) +{ + int len; + int fd = open(filename, O_RDONLY); + + if (fd < 0) { + ERROR("Unable to open '%s'\n", filename); + return CUPS_BACKEND_FAILED; + } + len = read(fd, databuf, datalen); + if (len < 0) { + ERROR("Bad Read! (%d/%d)\n", len, errno); + close(fd); + return CUPS_BACKEND_FAILED; + } + if (!actual_len && (datalen != len)) { + ERROR("Read mismatch (%d vs %d)\n", len, datalen); + close(fd); + return CUPS_BACKEND_FAILED; + } + close(fd); + + if (actual_len) + *actual_len = len; + + return CUPS_BACKEND_OK; +} + uint16_t uint16_to_packed_bcd(uint16_t val) { uint16_t bcd; diff --git a/backend_common.h b/backend_common.h index ae774ca..6c31432 100644 --- a/backend_common.h +++ b/backend_common.h @@ -196,6 +196,9 @@ void dump_markers(struct marker *markers, int marker_count, int full); void print_license_blurb(void); void print_help(char *argv0, struct dyesub_backend *backend); +int dyesub_read_data(char *filename, void *databuf, int datalen, + int *actual_len); + uint16_t uint16_to_packed_bcd(uint16_t val); uint32_t packed_bcd_to_uint32(char *in, int len); diff --git a/backend_kodak1400.c b/backend_kodak1400.c index c9ad58d..6768efc 100644 --- a/backend_kodak1400.c +++ b/backend_kodak1400.c @@ -176,16 +176,10 @@ static int kodak1400_set_tonecurve(struct kodak1400_ctx *ctx, char *fname) } /* Read in file */ - int tc_fd = open(fname, O_RDONLY); - if (tc_fd < 0) { - ret = -1; + if ((ret = dyesub_read_data(fname, data, UPDATE_SIZE, NULL))) { + ERROR("Failed to read Tone Curve file\n"); goto done; } - if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) { - ret = -2; - goto done; - } - close(tc_fd); /* Byteswap data to printer's format */ for (ret = 0; ret < (UPDATE_SIZE-16)/2 ; ret++) { @@ -662,7 +656,7 @@ static const char *kodak1400_prefixes[] = { struct dyesub_backend kodak1400_backend = { .name = "Kodak 1400/805", - .version = "0.39", + .version = "0.40", .uri_prefixes = kodak1400_prefixes, .cmdline_usage = kodak1400_cmdline, .cmdline_arg = kodak1400_cmdline_arg, diff --git a/backend_kodak605.c b/backend_kodak605.c index 30b7bfc..4f2f2d5 100644 --- a/backend_kodak605.c +++ b/backend_kodak605.c @@ -591,16 +591,10 @@ static int kodak605_set_tonecurve(struct kodak605_ctx *ctx, char *fname) INFO("Set Tone Curve from '%s'\n", fname); /* Read in file */ - int tc_fd = open(fname, O_RDONLY); - if (tc_fd < 0) { - ret = -1; + if ((ret = dyesub_read_data(fname, data, UPDATE_SIZE, NULL))) { + ERROR("Failed to read Tone Curve file\n"); goto done; } - if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) { - ret = 4; - goto done; - } - close(tc_fd); /* Byteswap data to printer's format */ for (ret = 0; ret < (UPDATE_SIZE/2) ; ret++) { @@ -718,7 +712,7 @@ static const char *kodak605_prefixes[] = { /* Exported */ struct dyesub_backend kodak605_backend = { .name = "Kodak 605", - .version = "0.33", + .version = "0.34", .uri_prefixes = kodak605_prefixes, .cmdline_usage = kodak605_cmdline, .cmdline_arg = kodak605_cmdline_arg, diff --git a/backend_kodak6800.c b/backend_kodak6800.c index 065f4d9..8c1e7c5 100644 --- a/backend_kodak6800.c +++ b/backend_kodak6800.c @@ -776,16 +776,10 @@ static int kodak6800_set_tonecurve(struct kodak6800_ctx *ctx, char *fname) INFO("Set Tone Curve from '%s'\n", fname); /* Read in file */ - int tc_fd = open(fname, O_RDONLY); - if (tc_fd < 0) { - ret = -1; + if ((ret = dyesub_read_data(fname, data, UPDATE_SIZE, NULL))) { + ERROR("Failed to read Tone Curve file\n"); goto done; } - if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) { - ret = -2; - goto done; - } - close(tc_fd); /* Byteswap data to printer's format */ for (ret = 0; ret < (UPDATE_SIZE)/2 ; ret++) { @@ -1305,7 +1299,7 @@ static const char *kodak6800_prefixes[] = { /* Exported */ struct dyesub_backend kodak6800_backend = { .name = "Kodak 6800/6850", - .version = "0.65", + .version = "0.66", .uri_prefixes = kodak6800_prefixes, .cmdline_usage = kodak6800_cmdline, .cmdline_arg = kodak6800_cmdline_arg, diff --git a/backend_mitsu9550.c b/backend_mitsu9550.c index a27afce..c6e0def 100644 --- a/backend_mitsu9550.c +++ b/backend_mitsu9550.c @@ -701,31 +701,20 @@ hdr_done: /* Read in CP98xx data tables if necessary */ if (ctx->is_98xx && !job->is_raw && !ctx->m98xxdata) { - int fd; - - DEBUG("Reading in 98xx data from disk\n"); - fd = open(MITSU_M98xx_DATATABLE_FILE, O_RDONLY); - if (fd < 0) { - ERROR("Unable to open 98xx data table file '%s'\n", MITSU_M98xx_DATATABLE_FILE); - mitsu9550_cleanup_job(job); - return CUPS_BACKEND_FAILED; - } + int ret; ctx->m98xxdata = malloc(DATATABLE_SIZE); if (!ctx->m98xxdata) { ERROR("Memory allocation Failure!\n"); mitsu9550_cleanup_job(job); return CUPS_BACKEND_RETRY_CURRENT; } - remain = DATATABLE_SIZE; - while (remain) { - i = read(fd, ((uint8_t*)ctx->m98xxdata) + (DATATABLE_SIZE - remain), remain); - if (i < 0) { - mitsu9550_cleanup_job(job); - return CUPS_BACKEND_CANCEL; - } - remain -= i; + + DEBUG("Reading in 98xx data from disk\n"); + if ((ret = dyesub_read_data(MITSU_M98xx_DATATABLE_FILE, ctx->m98xxdata, DATATABLE_SIZE, NULL))) { + ERROR("Unable to read 98xx data table file '%s'\n", MITSU_M98xx_DATATABLE_FILE); + free(ctx->m98xxdata); + return ret; } - close(fd); } if (job->is_raw) { @@ -1738,7 +1727,7 @@ static const char *mitsu9550_prefixes[] = { /* Exported */ struct dyesub_backend mitsu9550_backend = { .name = "Mitsubishi CP9xxx family", - .version = "0.44", + .version = "0.45", .uri_prefixes = mitsu9550_prefixes, .cmdline_usage = mitsu9550_cmdline, .cmdline_arg = mitsu9550_cmdline_arg, diff --git a/backend_shinkos1245.c b/backend_shinkos1245.c index 8172a45..f42643d 100644 --- a/backend_shinkos1245.c +++ b/backend_shinkos1245.c @@ -1092,21 +1092,11 @@ static int set_tonecurve(struct shinkos1245_ctx *ctx, int type, int table, char } ptr = data; - /* Open file and read it in */ - { - int tc_fd = open(fname, O_RDONLY); - if (tc_fd < 0) { - ret = tc_fd; - goto done; - } - ret = read(tc_fd, data, TONE_CURVE_SIZE); - if (ret < 0) { - close(tc_fd); - goto done; - } - - close(tc_fd); + /* Read in file */ + if ((ret = dyesub_read_data(fname, data, TONE_CURVE_SIZE, NULL))) { + ERROR("Failed to read Tone Curve file\n"); + goto done; } /* Issue a tone_write_start */ @@ -1699,7 +1689,7 @@ static const char *shinkos1245_prefixes[] = { struct dyesub_backend shinkos1245_backend = { .name = "Shinko/Sinfonia CHC-S1245/E1", - .version = "0.26", + .version = "0.27", .uri_prefixes = shinkos1245_prefixes, .cmdline_usage = shinkos1245_cmdline, .cmdline_arg = shinkos1245_cmdline_arg, diff --git a/backend_shinkos2145.c b/backend_shinkos2145.c index 74156c7..33ce646 100644 --- a/backend_shinkos2145.c +++ b/backend_shinkos2145.c @@ -1235,16 +1235,11 @@ static int set_tonecurve(struct shinkos2145_ctx *ctx, int target, char *fname) } /* Read in file */ - int tc_fd = open(fname, O_RDONLY); - if (tc_fd < 0) { - ret = -1; + if ((ret = dyesub_read_data(fname, data, UPDATE_SIZE, NULL))) { + ERROR("Failed to read Tone Curve file\n"); goto done; } - if (read(tc_fd, data, UPDATE_SIZE * sizeof(uint16_t)) != (UPDATE_SIZE * sizeof(uint16_t))) { - ret = -2; - goto done; - } - close(tc_fd); + /* Byteswap data to local CPU.. */ for (ret = 0; ret < UPDATE_SIZE ; ret++) { data[ret] = be16_to_cpu(data[ret]); @@ -1804,7 +1799,7 @@ static const char *shinkos2145_prefixes[] = { struct dyesub_backend shinkos2145_backend = { .name = "Shinko/Sinfonia CHC-S2145/S2", - .version = "0.55", + .version = "0.56", .uri_prefixes = shinkos2145_prefixes, .cmdline_usage = shinkos2145_cmdline, .cmdline_arg = shinkos2145_cmdline_arg, diff --git a/backend_shinkos6145.c b/backend_shinkos6145.c index 041aef5..c83607b 100644 --- a/backend_shinkos6145.c +++ b/backend_shinkos6145.c @@ -1636,16 +1636,11 @@ static int set_tonecurve(struct shinkos6145_ctx *ctx, int target, char *fname) } /* Read in file */ - int tc_fd = open(fname, O_RDONLY); - if (tc_fd < 0) { - ret = -1; + if ((ret = dyesub_read_data(fname, data, UPDATE_SIZE * sizeof(uint16_t), NULL))) { + ERROR("Failed to read Tone Curve file\n"); goto done; } - if (read(tc_fd, data, UPDATE_SIZE * sizeof(uint16_t)) != (UPDATE_SIZE * sizeof(uint16_t))) { - ret = -2; - goto done; - } - close(tc_fd); + /* Byteswap data to local CPU.. */ for (ret = 0; ret < UPDATE_SIZE ; ret++) { data[ret] = be16_to_cpu(data[ret]); @@ -2574,7 +2569,7 @@ static const char *shinkos6145_prefixes[] = { struct dyesub_backend shinkos6145_backend = { .name = "Shinko/Sinfonia CHC-S6145/CS2", - .version = "0.30", + .version = "0.31", .uri_prefixes = shinkos6145_prefixes, .cmdline_usage = shinkos6145_cmdline, .cmdline_arg = shinkos6145_cmdline_arg, diff --git a/backend_shinkos6245.c b/backend_shinkos6245.c index e54453e..988235f 100644 --- a/backend_shinkos6245.c +++ b/backend_shinkos6245.c @@ -1314,16 +1314,11 @@ static int set_tonecurve(struct shinkos6245_ctx *ctx, int target, char *fname) } /* Read in file */ - int tc_fd = open(fname, O_RDONLY); - if (tc_fd < 0) { - ret = -1; + if ((ret = dyesub_read_data(fname, data, UPDATE_SIZE * sizeof(uint16_t), NULL))) { + ERROR("Failed to read Tone Curve file\n"); goto done; } - if (read(tc_fd, data, UPDATE_SIZE * sizeof(uint16_t)) != (UPDATE_SIZE * sizeof(uint16_t))) { - ret = -2; - goto done; - } - close(tc_fd); + /* Byteswap data to local CPU.. */ for (ret = 0; ret < UPDATE_SIZE ; ret++) { data[ret] = be16_to_cpu(data[ret]); @@ -1922,7 +1917,7 @@ static const char *shinkos6245_prefixes[] = { struct dyesub_backend shinkos6245_backend = { .name = "Shinko/Sinfonia CHC-S6245", - .version = "0.14WIP", + .version = "0.15WIP", .uri_prefixes = shinkos6245_prefixes, .cmdline_usage = shinkos6245_cmdline, .cmdline_arg = shinkos6245_cmdline_arg,