2 * Tool for testing PCX run-length encoding.
3 * Copyright © 2012, 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
27 #include <engine/pcx.h>
30 #define PROGNAME "pcxrle"
31 static const char *progname = PROGNAME;
32 static const char sopts[] = "VH";
33 static const struct option lopts[] = {
34 { "version", 0, NULL, 'V' },
35 { "help", 0, NULL, 'H' },
39 static void print_usage(FILE *f)
41 fprintf(f, "Usage: %s [options] bytes [bytes ...]\n", progname);
44 static void print_help(void)
47 test_print_options(lopts);
50 static void rle_error(const unsigned char *raw, size_t n, const char *str)
52 fprintf(stderr, "%s: %s\n", progname, str);
53 fprintf(stderr, "%s: raw input was:\n", progname);
55 fprintf(stderr, "%*s", 4, "");
56 for (size_t i = 0; i < n; i++)
57 printf("%.2hhx", raw[i]);
61 static int compare_rle(const unsigned char *rle_buf, size_t rle_len,
62 const unsigned char *raw_buf, size_t raw_len)
64 unsigned char tmp[raw_len];
67 for (size_t i = 0; i < rle_len; i++) {
70 if ((rle_buf[i] & 0xc0) == 0xc0) {
71 count = rle_buf[i] & 0x3f;
73 rle_error(raw_buf, raw_len, "invalid RLE encoding");
78 if (offset + count > raw_len) {
79 rle_error(raw_buf, raw_len, "RLE expansion exceeds raw size");
83 memset(tmp+offset, rle_buf[i], count);
87 if (offset < raw_len) {
88 rle_error(raw_buf, raw_len, "RLE expansion falls short of raw size");
92 if (memcmp(raw_buf, tmp, raw_len) != 0) {
93 rle_error(raw_buf, raw_len, "RLE expansion does not match raw data");
100 static int encode_rle(FILE *tmp, const char *hex)
102 unsigned char buf[256], rle_buf[sizeof buf*2];
103 struct pcx_head head;
108 n = test_decode_hex(hex, buf, sizeof buf);
110 fprintf(stderr, "%s: invalid hex sequence: %s\n",
114 fprintf(stderr, "%s: empty argument\n", progname);
116 } else if (n > sizeof buf) {
117 fprintf(stderr, "%s: hex sequence too long: %s\n",
123 head.width = head.height = n;
124 rc = pcx_write_scanline(&head, buf, tmp);
126 fprintf(stderr, "%s: failed to encode data: %s\n",
127 progname, strerror(errno));
133 fprintf(stderr, "%s: failed to get file offset: %s\n",
134 progname, strerror(errno));
139 if (fread(rle_buf, offset, 1, tmp) != 1) {
140 fprintf(stderr, "%s: failed to read RLE data: %s\n",
141 progname, strerror(errno));
145 ret = compare_rle(rle_buf, offset, buf, n);
146 for (long i = 0; i < offset; i++)
147 printf("%.2hhx", rle_buf[i]);
152 int main(int argc, char **argv)
154 int opt, ret = EXIT_SUCCESS;
160 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
163 test_print_version(PROGNAME);
181 fprintf(stderr, "%s: failed to create temporary file: %s\n",
182 progname, strerror(errno));
186 for (int i = optind; i < argc; i++) {
187 if (encode_rle(tmp, argv[i]) != 0) {