X-Git-Url: https://git.draconx.ca/gitweb/upkg.git/blobdiff_plain/6e7d67bed9f4d76943e2f1724acff1e16b8bf7ac..HEAD:/test/decodeindex.c?ds=sidebyside
diff --git a/test/decodeindex.c b/test/decodeindex.c
index d3f099a..1d4643b 100644
--- a/test/decodeindex.c
+++ b/test/decodeindex.c
@@ -1,6 +1,6 @@
/*
* Tool for decoding compact index values for testing.
- * Copyright © 2012 Nick Bowler
+ * Copyright © 2012, 2022 Nick Bowler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -13,74 +13,35 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
+ * along with this program. If not, see .
*/
#include
#include
#include
-#include
#include
#include
+#include "common.h"
#define PROGNAME "decodeindex"
static const char *progname = PROGNAME;
-static const unsigned char sopts[] = "VH";
+static const char sopts[] = "VH";
static const struct option lopts[] = {
{ "version", 0, NULL, 'V' },
{ "help", 0, NULL, 'H' },
{ 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);
}
-static void print_version(void)
+static void print_help(void)
{
- printf("%s (%s) %s\n", PROGNAME, PACKAGE_NAME, PACKAGE_VERSION);
- puts("Copyright (C) 2012 Nick Bowler.");
- puts("License GPLv3+: GNU GPL version 3 or later .");
- puts("This is free software: you are free to change and redistribute it.");
- puts("There is NO WARRANTY, to the extent permitted by law.");
+ print_usage(stdout);
+ test_print_options(lopts);
}
static void print_bytes(FILE *f, int indent, void *buf, size_t n)
@@ -88,7 +49,7 @@ static void print_bytes(FILE *f, int indent, void *buf, size_t n)
fprintf(f, "%*s", indent, "");
if (n == 0) {
- printf("(empty)\n");
+ fprintf(f, "(empty)\n");
return;
}
@@ -104,7 +65,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);
@@ -114,7 +75,7 @@ static int print_index(const char *hex)
progname, hex);
goto out;
}
-
+
rc = upkg_decode_index(&index, buf, n);
if (rc == 0) {
fprintf(stderr, "%s: invalid index encoding:\n", progname);
@@ -142,10 +103,10 @@ int main(int argc, char **argv)
while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
switch (opt) {
case 'V':
- print_version();
+ test_print_version(PROGNAME);
return EXIT_SUCCESS;
case 'H':
- print_usage(stdout);
+ print_help();
return EXIT_SUCCESS;
default:
print_usage(stderr);