2 * Tool for decoding compact index values for testing.
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 #define PROGNAME "decodeindex"
28 static const char *progname = PROGNAME;
29 static const char sopts[] = "VH";
30 static const struct option lopts[] = {
31 { "version", 0, NULL, 'V' },
32 { "help", 0, NULL, 'H' },
36 static void print_usage(FILE *f)
38 fprintf(f, "Usage: %s [options] index [index ...]\n", progname);
41 static void print_help(void)
44 test_print_options(lopts);
47 static void print_bytes(FILE *f, int indent, void *buf, size_t n)
49 fprintf(f, "%*s", indent, "");
52 fprintf(f, "(empty)\n");
56 for (size_t i = 0; i < n; i++)
57 fprintf(f, "%*s%.2hhx", i != 0, "", ((unsigned char *)buf)[i]);
61 static int print_index(const char *hex)
63 unsigned char buf[32];
68 n = test_decode_hex(hex, buf, sizeof buf);
70 fprintf(stderr, "%s: invalid hex sequence: %s\n",
73 } else if (n > sizeof buf) {
74 fprintf(stderr, "%s: hex sequence too long: %s\n",
79 rc = upkg_decode_index(&index, buf, n);
81 fprintf(stderr, "%s: invalid index encoding:\n", progname);
82 print_bytes(stderr, 4, buf, n);
85 fprintf(stderr, "%s: trailing bytes in argument:\n", progname);
86 print_bytes(stderr, 4, buf+rc, n-rc);
92 printf("%ld\n", index);
96 int main(int argc, char **argv)
98 int opt, ret = EXIT_SUCCESS;
103 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
106 test_print_version(PROGNAME);
122 for (int i = optind; i < argc; i++) {
123 if (print_index(argv[i]) != 0) {