2 * Tool for decoding compact index values for testing.
3 * Copyright © 2012 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 <http://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_version(void)
43 printf("%s (%s) %s\n", PROGNAME, PACKAGE_NAME, PACKAGE_VERSION);
44 puts("Copyright (C) 2012 Nick Bowler.");
45 puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
46 puts("This is free software: you are free to change and redistribute it.");
47 puts("There is NO WARRANTY, to the extent permitted by law.");
50 static void print_bytes(FILE *f, int indent, void *buf, size_t n)
52 fprintf(f, "%*s", indent, "");
59 for (size_t i = 0; i < n; i++)
60 fprintf(f, "%*s%.2hhx", i != 0, "", ((unsigned char *)buf)[i]);
64 static int print_index(const char *hex)
66 unsigned char buf[32];
71 n = test_decode_hex(hex, buf, sizeof buf);
73 fprintf(stderr, "%s: invalid hex sequence: %s\n",
76 } else if (n > sizeof buf) {
77 fprintf(stderr, "%s: hex sequence too long: %s\n",
82 rc = upkg_decode_index(&index, buf, n);
84 fprintf(stderr, "%s: invalid index encoding:\n", progname);
85 print_bytes(stderr, 4, buf, n);
88 fprintf(stderr, "%s: trailing bytes in argument:\n", progname);
89 print_bytes(stderr, 4, buf+rc, n-rc);
95 printf("%ld\n", index);
99 int main(int argc, char **argv)
101 int opt, ret = EXIT_SUCCESS;
106 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
125 for (int i = optind; i < argc; i++) {
126 if (print_index(argv[i]) != 0) {