]> git.draconx.ca Git - upkg.git/blobdiff - test/decodeindex.c
engine: Add a unit test for the PCX run-length encoder.
[upkg.git] / test / decodeindex.c
index 415fa7904868598104ebed07c51cf271bc8bd1fd..c072a07c25f72a41c800aa5a7ef368dc02489d3e 100644 (file)
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <ctype.h>
 #include <getopt.h>
 
 #include <upkg.h>
+#include "common.h"
 
 #define PROGNAME "decodeindex"
 static const char *progname = PROGNAME;
@@ -33,42 +33,6 @@ static const struct option lopts[] = {
        { 0 }
 };
 
-/*
- * Decode a hexadecimal string into a sequence of bytes.  If there are an
- * odd number of nibbles, treat the first character as the least significant
- * nibble of the first byte.  The result is written to the buffer specified by
- * buf.  At most n bytes are written to the buffer.
- *
- * Returns the number of bytes that would be written provided that n was large
- * enough, or (size_t)-1 if the input is not valid.
- */
-static size_t decode_hex(const char *hex, unsigned char *buf, size_t n)
-{
-       size_t len, count = 0;
-       char tmp[] = "00";
-
-       for (len = 0; hex[len]; len++) {
-               if (!isxdigit(hex[len]))
-                       return -1;
-       }
-
-       if (!len)
-               return 0;
-
-       switch (len % 2) {
-               while (len > 0) {
-                       case 0: tmp[0] = *hex++; len--;
-                       case 1: tmp[1] = *hex++; len--;
-
-                       if (count < n)
-                               buf[count] = strtoul(tmp, NULL, 16);
-                       count++;
-               }
-       }
-
-       return count;
-}
-
 static void print_usage(FILE *f)
 {
        fprintf(f, "Usage: %s [options] index [index ...]\n", progname);
@@ -104,7 +68,7 @@ static int print_index(const char *hex)
        size_t n, rc;
        int ret = -1;
 
-       n = decode_hex(hex, buf, sizeof buf);
+       n = test_decode_hex(hex, buf, sizeof buf);
        if (n == -1) {
                fprintf(stderr, "%s: invalid hex sequence: %s\n",
                                progname, hex);