| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Hash files using some secure hash function |
| 6 | * |
| 7 | * (c) 2000 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Header files ------------------------------------------------------*/ |
| 31 | |
| 32 | #define _FILE_OFFSET_BITS 64 |
| 33 | |
| 34 | #include "config.h" |
| 35 | |
| 36 | #include <assert.h> |
| 37 | #include <ctype.h> |
| 38 | #include <errno.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | #include <mLib/alloc.h> |
| 44 | #include <mLib/dstr.h> |
| 45 | #include <mLib/mdwopt.h> |
| 46 | #include <mLib/quis.h> |
| 47 | #include <mLib/report.h> |
| 48 | #include <mLib/sub.h> |
| 49 | #include <mLib/str.h> |
| 50 | |
| 51 | #include <mLib/hex.h> |
| 52 | #include <mLib/base32.h> |
| 53 | #include <mLib/base64.h> |
| 54 | |
| 55 | #include "ghash.h" |
| 56 | #include "cc.h" |
| 57 | |
| 58 | /*----- Static variables --------------------------------------------------*/ |
| 59 | |
| 60 | #define f_binary 1u |
| 61 | #define f_bogus 2u |
| 62 | #define f_verbose 4u |
| 63 | #define f_check 8u |
| 64 | #define f_files 16u |
| 65 | #define f_raw 32u |
| 66 | #define f_oddhash 64u |
| 67 | #define f_escape 128u |
| 68 | #define f_oddenc 256u |
| 69 | #define f_progress 512u |
| 70 | |
| 71 | /*----- Encoding and decoding ---------------------------------------------*/ |
| 72 | |
| 73 | /* --- Hex encoding --- */ |
| 74 | |
| 75 | static void puthex(const octet *buf, size_t sz, FILE *fp) |
| 76 | { |
| 77 | while (sz) { |
| 78 | fprintf(fp, "%02x", *buf++); |
| 79 | sz--; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static size_t gethex(const char *p, octet *q, size_t sz, char **pp) |
| 84 | { |
| 85 | size_t i = 0; |
| 86 | while (sz > 0 && |
| 87 | isxdigit((unsigned char)p[0]) && |
| 88 | isxdigit((unsigned char)p[1])) { |
| 89 | char buf[3]; |
| 90 | buf[0] = p[0]; |
| 91 | buf[1] = p[1]; |
| 92 | buf[2] = 0; |
| 93 | *q++ = strtoul(buf, 0, 16); |
| 94 | sz--; |
| 95 | p += 2; |
| 96 | i++; |
| 97 | } |
| 98 | if (pp) |
| 99 | *pp = (char *)p; |
| 100 | return (i); |
| 101 | } |
| 102 | |
| 103 | /* --- Base64 encoding --- */ |
| 104 | |
| 105 | static void putb64(const octet *buf, size_t sz, FILE *fp) |
| 106 | { |
| 107 | base64_ctx b; |
| 108 | dstr d = DSTR_INIT; |
| 109 | |
| 110 | base64_init(&b); |
| 111 | b.indent = ""; |
| 112 | b.maxline = 0; |
| 113 | base64_encode(&b, buf, sz, &d); |
| 114 | base64_encode(&b, 0, 0, &d); |
| 115 | dstr_write(&d, fp); |
| 116 | dstr_destroy(&d); |
| 117 | } |
| 118 | |
| 119 | static size_t getb64(const char *p, octet *q, size_t sz, char **pp) |
| 120 | { |
| 121 | base64_ctx b; |
| 122 | dstr d = DSTR_INIT; |
| 123 | size_t n = strlen(p); |
| 124 | |
| 125 | base64_init(&b); |
| 126 | base64_decode(&b, p, n, &d); |
| 127 | if (pp) *pp = (/*unconst*/ char *)p + n; |
| 128 | base64_decode(&b, 0, 0, &d); |
| 129 | assert(d.len <= sz); |
| 130 | memcpy(q, d.buf, sz); |
| 131 | n = d.len; |
| 132 | dstr_destroy(&d); |
| 133 | return (n); |
| 134 | } |
| 135 | |
| 136 | /* --- Base32 encoding --- */ |
| 137 | |
| 138 | static void putb32(const octet *buf, size_t sz, FILE *fp) |
| 139 | { |
| 140 | base32_ctx b; |
| 141 | dstr d = DSTR_INIT; |
| 142 | |
| 143 | base32_init(&b); |
| 144 | b.indent = ""; |
| 145 | b.maxline = 0; |
| 146 | base32_encode(&b, buf, sz, &d); |
| 147 | base32_encode(&b, 0, 0, &d); |
| 148 | dstr_write(&d, fp); |
| 149 | dstr_destroy(&d); |
| 150 | } |
| 151 | |
| 152 | static size_t getb32(const char *p, octet *q, size_t sz, char **pp) |
| 153 | { |
| 154 | base32_ctx b; |
| 155 | dstr d = DSTR_INIT; |
| 156 | size_t n = strlen(p); |
| 157 | |
| 158 | base32_init(&b); |
| 159 | base32_decode(&b, p, n, &d); |
| 160 | if (pp) *pp = (/*unconst*/ char *)p + n; |
| 161 | base32_decode(&b, 0, 0, &d); |
| 162 | assert(d.len <= sz); |
| 163 | memcpy(q, d.buf, sz); |
| 164 | n = d.len; |
| 165 | dstr_destroy(&d); |
| 166 | return (n); |
| 167 | } |
| 168 | |
| 169 | /* --- Table --- */ |
| 170 | |
| 171 | typedef struct encodeops { |
| 172 | const char *name; |
| 173 | void (*put)(const octet *, size_t, FILE *); |
| 174 | size_t (*get)(const char *, octet *, size_t, char **); |
| 175 | } encodeops; |
| 176 | |
| 177 | static const encodeops encodingtab[] = { |
| 178 | { "hex", puthex, gethex }, |
| 179 | { "base64", putb64, getb64 }, |
| 180 | { "base32", putb32, getb32 }, |
| 181 | { 0, 0, 0 } |
| 182 | }; |
| 183 | |
| 184 | static const encodeops *getencoding(const char *ename) |
| 185 | { |
| 186 | const encodeops *e; |
| 187 | |
| 188 | for (e = encodingtab; e->name; e++) { |
| 189 | if (strcmp(ename, e->name) == 0) |
| 190 | return (e); |
| 191 | } |
| 192 | return (0); |
| 193 | } |
| 194 | |
| 195 | /*----- Support functions -------------------------------------------------*/ |
| 196 | |
| 197 | /* --- @fhash@ --- * |
| 198 | * |
| 199 | * Arguments: @const char *file@ = file name to be hashed (null for stdin) |
| 200 | * @unsigned f@ = flags to set |
| 201 | * @const gchash *gch@ = pointer to hash function to use |
| 202 | * @void *buf@ = pointer to hash output buffer |
| 203 | * |
| 204 | * Returns: Zero if it worked, nonzero on error. |
| 205 | * |
| 206 | * Use: Hashes a file. |
| 207 | */ |
| 208 | |
| 209 | static int fhash(const char *file, unsigned f, const gchash *gch, void *buf) |
| 210 | { |
| 211 | FILE *fp; |
| 212 | char fbuf[1024 * 128]; |
| 213 | size_t sz; |
| 214 | ghash *h; |
| 215 | int e; |
| 216 | fprogress ff; |
| 217 | |
| 218 | if (!file || strcmp(file, "-") == 0) |
| 219 | fp = stdin; |
| 220 | else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0) |
| 221 | return (-1); |
| 222 | |
| 223 | if (f & f_progress) { |
| 224 | if (fprogress_init(&ff, file, fp)) return (-1); |
| 225 | } |
| 226 | |
| 227 | h = GH_INIT(gch); |
| 228 | while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0) { |
| 229 | GH_HASH(h, fbuf, sz); |
| 230 | if (f & f_progress) fprogress_update(&ff, sz); |
| 231 | } |
| 232 | GH_DONE(h, buf); |
| 233 | GH_DESTROY(h); |
| 234 | if (f & f_progress) fprogress_done(&ff); |
| 235 | e = ferror(fp); |
| 236 | if (file) |
| 237 | fclose(fp); |
| 238 | return (e ? -1 : 0); |
| 239 | } |
| 240 | |
| 241 | /* --- @gethash@ --- * |
| 242 | * |
| 243 | * Arguments: @const char *name@ = pointer to name string |
| 244 | * |
| 245 | * Returns: Pointer to appropriate hash class. |
| 246 | * |
| 247 | * Use: Chooses a hash function by name. |
| 248 | */ |
| 249 | |
| 250 | static const gchash *gethash(const char *name) |
| 251 | { |
| 252 | const gchash *const *g, *gg = 0; |
| 253 | size_t sz = strlen(name); |
| 254 | for (g = ghashtab; *g; g++) { |
| 255 | if (strncmp(name, (*g)->name, sz) == 0) { |
| 256 | if ((*g)->name[sz] == 0) { |
| 257 | gg = *g; |
| 258 | break; |
| 259 | } else if (gg) |
| 260 | return (0); |
| 261 | else |
| 262 | gg = *g; |
| 263 | } |
| 264 | } |
| 265 | return (gg); |
| 266 | } |
| 267 | |
| 268 | /* --- @getstring@ --- * |
| 269 | * |
| 270 | * Arguments: @FILE *fp@ = stream from which to read |
| 271 | * @const char *p@ = string to read from instead |
| 272 | * @dstr *d@ = destination string |
| 273 | * @unsigned raw@ = raw or cooked read |
| 274 | * |
| 275 | * Returns: Zero if OK, nonzero on end-of-file. |
| 276 | * |
| 277 | * Use: Reads a filename (or something similar) from a stream. |
| 278 | */ |
| 279 | |
| 280 | static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw) |
| 281 | { |
| 282 | int ch; |
| 283 | int q = 0; |
| 284 | |
| 285 | /* --- Raw: just read exactly what's written up to a null byte --- */ |
| 286 | |
| 287 | #define NEXTCH (fp ? getc(fp) : (unsigned char)*p++) |
| 288 | #define EOFCH (fp ? EOF : 0) |
| 289 | |
| 290 | if (raw) { |
| 291 | if ((ch = NEXTCH) == EOFCH) |
| 292 | return (EOF); |
| 293 | for (;;) { |
| 294 | if (!ch) |
| 295 | break; |
| 296 | DPUTC(d, ch); |
| 297 | if ((ch = NEXTCH) == EOFCH) |
| 298 | break; |
| 299 | } |
| 300 | DPUTZ(d); |
| 301 | return (0); |
| 302 | } |
| 303 | |
| 304 | /* --- Skip as far as whitespace --- * |
| 305 | * |
| 306 | * Also skip past comments. |
| 307 | */ |
| 308 | |
| 309 | again: |
| 310 | ch = NEXTCH; |
| 311 | while (isspace(ch)) |
| 312 | ch = NEXTCH; |
| 313 | if (ch == '#') { |
| 314 | do ch = NEXTCH; while (ch != '\n' && ch != EOFCH); |
| 315 | goto again; |
| 316 | } |
| 317 | if (ch == EOFCH) |
| 318 | return (EOF); |
| 319 | |
| 320 | /* --- If the character is a quote then read a quoted string --- */ |
| 321 | |
| 322 | switch (ch) { |
| 323 | case '`': |
| 324 | ch = '\''; |
| 325 | case '\'': |
| 326 | case '\"': |
| 327 | q = ch; |
| 328 | ch = NEXTCH; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | /* --- Now read all sorts of interesting things --- */ |
| 333 | |
| 334 | for (;;) { |
| 335 | |
| 336 | /* --- Handle an escaped thing --- */ |
| 337 | |
| 338 | if (ch == '\\') { |
| 339 | ch = NEXTCH; |
| 340 | if (ch == EOFCH) |
| 341 | break; |
| 342 | switch (ch) { |
| 343 | case 'a': ch = '\a'; break; |
| 344 | case 'b': ch = '\b'; break; |
| 345 | case 'f': ch = '\f'; break; |
| 346 | case 'n': ch = '\n'; break; |
| 347 | case 'r': ch = '\r'; break; |
| 348 | case 't': ch = '\t'; break; |
| 349 | case 'v': ch = '\v'; break; |
| 350 | } |
| 351 | DPUTC(d, ch); |
| 352 | ch = NEXTCH; |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | /* --- If it's a quote or some other end marker then stop --- */ |
| 357 | |
| 358 | if (ch == q) |
| 359 | break; |
| 360 | if (!q && isspace(ch)) |
| 361 | break; |
| 362 | |
| 363 | /* --- Otherwise contribute and continue --- */ |
| 364 | |
| 365 | DPUTC(d, ch); |
| 366 | if ((ch = NEXTCH) == EOFCH) |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | /* --- Done --- */ |
| 371 | |
| 372 | DPUTZ(d); |
| 373 | return (0); |
| 374 | |
| 375 | #undef NEXTCH |
| 376 | #undef EOFCH |
| 377 | } |
| 378 | |
| 379 | /* --- @putstring@ --- * |
| 380 | * |
| 381 | * Arguments: @FILE *fp@ = stream to write on |
| 382 | * @const char *p@ = pointer to text |
| 383 | * @unsigned raw@ = whether the string is to be written raw |
| 384 | * |
| 385 | * Returns: --- |
| 386 | * |
| 387 | * Use: Emits a string to a stream. |
| 388 | */ |
| 389 | |
| 390 | static void putstring(FILE *fp, const char *p, unsigned raw) |
| 391 | { |
| 392 | size_t sz = strlen(p); |
| 393 | unsigned qq; |
| 394 | const char *q; |
| 395 | |
| 396 | /* --- Just write the string null terminated if raw --- */ |
| 397 | |
| 398 | if (raw) { |
| 399 | fwrite(p, 1, sz + 1, fp); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | /* --- Check for any dodgy characters --- */ |
| 404 | |
| 405 | qq = 0; |
| 406 | for (q = p; *q; q++) { |
| 407 | if (isspace((unsigned char)*q)) { |
| 408 | qq = '\"'; |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (qq) |
| 414 | putc(qq, fp); |
| 415 | |
| 416 | /* --- Emit the string --- */ |
| 417 | |
| 418 | for (q = p; *q; q++) { |
| 419 | switch (*q) { |
| 420 | case '\a': fputc('\\', fp); fputc('a', fp); break; |
| 421 | case '\b': fputc('\\', fp); fputc('b', fp); break; |
| 422 | case '\f': fputc('\\', fp); fputc('f', fp); break; |
| 423 | case '\n': fputc('\\', fp); fputc('n', fp); break; |
| 424 | case '\r': fputc('\\', fp); fputc('r', fp); break; |
| 425 | case '\t': fputc('\\', fp); fputc('t', fp); break; |
| 426 | case '\v': fputc('\\', fp); fputc('v', fp); break; |
| 427 | case '`': fputc('\\', fp); fputc('`', fp); break; |
| 428 | case '\'': fputc('\\', fp); fputc('\'', fp); break; |
| 429 | case '\"': fputc('\\', fp); fputc('\"', fp); break; |
| 430 | case '#': fputc('\\', fp); fputc('#', fp); break; |
| 431 | default: |
| 432 | putc(*q, fp); |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* --- Done --- */ |
| 438 | |
| 439 | if (qq) |
| 440 | putc(qq, fp); |
| 441 | } |
| 442 | |
| 443 | /*----- Guts --------------------------------------------------------------*/ |
| 444 | |
| 445 | static int checkhash(const char *file, unsigned f, |
| 446 | const gchash *gch, const encodeops *e) |
| 447 | { |
| 448 | int rc; |
| 449 | FILE *fp; |
| 450 | dstr d = DSTR_INIT; |
| 451 | dstr dd = DSTR_INIT; |
| 452 | unsigned long n = 0, nfail = 0; |
| 453 | octet *buf = xmalloc(2 * gch->hashsz); |
| 454 | |
| 455 | if (!file || strcmp(file, "-") == 0) |
| 456 | fp = stdin; |
| 457 | else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) { |
| 458 | moan("couldn't open `%s': %s", file, strerror(errno)); |
| 459 | return (EXIT_FAILURE); |
| 460 | } |
| 461 | |
| 462 | while (DRESET(&d), dstr_putline(&d, fp) != EOF) { |
| 463 | char *p = d.buf; |
| 464 | char *q; |
| 465 | unsigned ff = f; |
| 466 | |
| 467 | /* --- Handle a directive --- */ |
| 468 | |
| 469 | if (*p == '#') { |
| 470 | p++; |
| 471 | if ((q = str_getword(&p)) == 0) |
| 472 | continue; |
| 473 | if (strcmp(q, "hash") == 0) { |
| 474 | const gchash *g; |
| 475 | if ((q = str_getword(&p)) == 0) |
| 476 | continue; |
| 477 | if ((g = gethash(q)) == 0) |
| 478 | continue; |
| 479 | gch = g; |
| 480 | xfree(buf); |
| 481 | buf = xmalloc(2 * gch->hashsz); |
| 482 | } else if (strcmp(q, "encoding") == 0) { |
| 483 | const encodeops *ee; |
| 484 | if ((q = str_getword(&p)) == 0) |
| 485 | continue; |
| 486 | if ((ee = getencoding(q)) == 0) |
| 487 | continue; |
| 488 | e = ee; |
| 489 | } else if (strcmp(q, "escape") == 0) |
| 490 | f |= f_escape; |
| 491 | continue; |
| 492 | } |
| 493 | |
| 494 | /* --- Otherwise it's a hex thing --- */ |
| 495 | |
| 496 | q = p; |
| 497 | while (*p && *p != ' ') |
| 498 | p++; |
| 499 | if (!*p) |
| 500 | continue; |
| 501 | *p++ = 0; |
| 502 | if (e->get(q, buf, gch->hashsz, 0) < gch->hashsz) |
| 503 | continue; |
| 504 | if (*p == '*') |
| 505 | ff |= f_binary; |
| 506 | else if (*p != ' ') |
| 507 | continue; |
| 508 | p++; |
| 509 | |
| 510 | if (f & f_escape) { |
| 511 | DRESET(&dd); |
| 512 | getstring(0, p, &dd, 0); |
| 513 | p = dd.buf; |
| 514 | } |
| 515 | |
| 516 | if (fhash(p, ff, gch, buf + gch->hashsz)) { |
| 517 | moan("couldn't read `%s': %s", p, strerror(errno)); |
| 518 | rc = EXIT_FAILURE; |
| 519 | continue; |
| 520 | } |
| 521 | if (memcmp(buf, buf + gch->hashsz, gch->hashsz) != 0) { |
| 522 | if (ff & f_verbose) |
| 523 | fprintf(stderr, "FAIL %s\n", p); |
| 524 | else |
| 525 | moan("%s check failed for `%s'", gch->name, p); |
| 526 | nfail++; |
| 527 | rc = EXIT_FAILURE; |
| 528 | } else { |
| 529 | if (ff & f_verbose) |
| 530 | fprintf(stderr, "OK %s\n", p); |
| 531 | } |
| 532 | n++; |
| 533 | } |
| 534 | |
| 535 | dstr_destroy(&d); |
| 536 | dstr_destroy(&dd); |
| 537 | xfree(buf); |
| 538 | if ((f & f_verbose) && nfail) |
| 539 | moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name); |
| 540 | else if (!n) |
| 541 | moan("no files checked"); |
| 542 | return (0); |
| 543 | } |
| 544 | |
| 545 | static int dohash(const char *file, unsigned f, |
| 546 | const gchash *gch, const encodeops *e) |
| 547 | { |
| 548 | int rc = 0; |
| 549 | octet *p = xmalloc(gch->hashsz); |
| 550 | |
| 551 | if (fhash(file, f, gch, p)) { |
| 552 | moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno)); |
| 553 | rc = EXIT_FAILURE; |
| 554 | } else { |
| 555 | e->put(p, gch->hashsz, stdout); |
| 556 | if (file) { |
| 557 | fputc(' ', stdout); |
| 558 | fputc(f & f_binary ? '*' : ' ', stdout); |
| 559 | if (f & f_escape) |
| 560 | putstring(stdout, file, 0); |
| 561 | else |
| 562 | fputs(file, stdout); |
| 563 | } |
| 564 | fputc('\n', stdout); |
| 565 | } |
| 566 | |
| 567 | xfree(p); |
| 568 | return (rc); |
| 569 | } |
| 570 | |
| 571 | static int dofile(const char *file, unsigned f, |
| 572 | const gchash *gch, const encodeops *e) |
| 573 | { |
| 574 | return (f & f_check ? checkhash : dohash)(file, f, gch, e); |
| 575 | } |
| 576 | |
| 577 | static int hashfiles(const char *file, unsigned f, |
| 578 | const gchash *gch, const encodeops *e) |
| 579 | { |
| 580 | FILE *fp; |
| 581 | dstr d = DSTR_INIT; |
| 582 | int rc = 0; |
| 583 | int rrc; |
| 584 | |
| 585 | if (!file || strcmp(file, "-") == 0) |
| 586 | fp = stdin; |
| 587 | else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) { |
| 588 | moan("couldn't open `%s': %s", file, strerror(errno)); |
| 589 | return (EXIT_FAILURE); |
| 590 | } |
| 591 | |
| 592 | for (;;) { |
| 593 | DRESET(&d); |
| 594 | if (getstring(fp, 0, &d, f & f_raw)) |
| 595 | break; |
| 596 | if ((rrc = dofile(d.buf, f, gch, e)) != 0) |
| 597 | rc = rrc; |
| 598 | } |
| 599 | |
| 600 | return (rc); |
| 601 | } |
| 602 | |
| 603 | static int hashsum(const char *file, unsigned f, |
| 604 | const gchash *gch, const encodeops *e) |
| 605 | { |
| 606 | return (f & f_files ? hashfiles : dofile)(file, f, gch, e); |
| 607 | } |
| 608 | |
| 609 | /*----- Main driver -------------------------------------------------------*/ |
| 610 | |
| 611 | void version(FILE *fp) |
| 612 | { |
| 613 | pquis(fp, "$, Catacomb version " VERSION "\n"); |
| 614 | } |
| 615 | |
| 616 | static void usage(FILE *fp) |
| 617 | { |
| 618 | pquis(fp, "Usage: $ [-f0ebcv] [-a ALGORITHM] [-E ENC] [FILES...]\n"); |
| 619 | } |
| 620 | |
| 621 | static void help(FILE *fp, const gchash *gch) |
| 622 | { |
| 623 | version(fp); |
| 624 | fputc('\n', fp); |
| 625 | usage(fp); |
| 626 | pquis(fp, "\n\ |
| 627 | Generates or checks message digests on files. Options available:\n\ |
| 628 | \n\ |
| 629 | -h, --help Display this help message.\n\ |
| 630 | -V, --version Display program's version number.\n\ |
| 631 | -u, --usage Display a terse usage message.\n\ |
| 632 | -l, --list [ITEM...] Show known hash functions and/or encodings.\n\ |
| 633 | \n\ |
| 634 | -a, --algorithm=ALG Use the message digest algorithm ALG.\n\ |
| 635 | -E, --encoding=ENC Represent hashes using encoding ENC.\n\ |
| 636 | \n\ |
| 637 | -f, --files Read a list of file names from standard input.\n\ |
| 638 | -0, --null File names are null terminated, not plain text.\n\ |
| 639 | \n\ |
| 640 | -e, --escape Escape funny characters in filenames.\n\ |
| 641 | -c, --check Check message digests rather than emitting them.\n\ |
| 642 | -b, --binary When reading files, treat them as binary.\n\ |
| 643 | -v, --verbose Be verbose when checking digests.\n\ |
| 644 | \n\ |
| 645 | For a list of hashing algorithms and encodings, type `$ --list'.\n\ |
| 646 | "); |
| 647 | if (gch) |
| 648 | fprintf(fp, "The default message digest algorithm is %s.\n", gch->name); |
| 649 | } |
| 650 | |
| 651 | #define LISTS(LI) \ |
| 652 | LI("Lists", list, listtab[i].name, listtab[i].name) \ |
| 653 | LI("Hash functions", hash, ghashtab[i], ghashtab[i]->name) \ |
| 654 | LI("Encodings", enc, encodingtab[i].name, encodingtab[i].name) |
| 655 | |
| 656 | MAKELISTTAB(listtab, LISTS) |
| 657 | |
| 658 | int main(int argc, char *argv[]) |
| 659 | { |
| 660 | unsigned f = 0; |
| 661 | const gchash *gch = 0; |
| 662 | const encodeops *e = &encodingtab[0]; |
| 663 | int rc; |
| 664 | |
| 665 | /* --- Initialization --- */ |
| 666 | |
| 667 | ego(argv[0]); |
| 668 | sub_init(); |
| 669 | |
| 670 | /* --- Choose a hash function from the name --- */ |
| 671 | |
| 672 | { |
| 673 | char *q = xstrdup(QUIS); |
| 674 | size_t len = strlen(q); |
| 675 | if (len > 3 && strcmp(q + len - 3, "sum") == 0) { |
| 676 | q[len - 3] = 0; |
| 677 | gch = gethash(q); |
| 678 | } |
| 679 | if (!gch) |
| 680 | gch = gethash("md5"); |
| 681 | xfree(q); |
| 682 | } |
| 683 | |
| 684 | /* --- Read options --- */ |
| 685 | |
| 686 | for (;;) { |
| 687 | static struct option opts[] = { |
| 688 | { "help", 0, 0, 'h' }, |
| 689 | { "verbose", 0, 0, 'V' }, |
| 690 | { "usage", 0, 0, 'u' }, |
| 691 | |
| 692 | { "algorithm", OPTF_ARGREQ, 0, 'a' }, |
| 693 | { "hash", OPTF_ARGREQ, 0, 'a' }, |
| 694 | { "encoding", OPTF_ARGREQ, 0, 'E' }, |
| 695 | { "list", 0, 0, 'l' }, |
| 696 | |
| 697 | { "files", 0, 0, 'f' }, |
| 698 | { "find", 0, 0, 'f' }, |
| 699 | { "null", 0, 0, '0' }, |
| 700 | |
| 701 | { "escape", 0, 0, 'e' }, |
| 702 | { "check", 0, 0, 'c' }, |
| 703 | { "binary", 0, 0, 'b' }, |
| 704 | { "verbose", 0, 0, 'v' }, |
| 705 | { "progress", 0, 0, 'p' }, |
| 706 | |
| 707 | { 0, 0, 0, 0 } |
| 708 | }; |
| 709 | int i = mdwopt(argc, argv, "hVu a:E:l f0 ecbvp", opts, 0, 0, 0); |
| 710 | if (i < 0) |
| 711 | break; |
| 712 | |
| 713 | switch (i) { |
| 714 | case 'h': |
| 715 | help(stdout, gch); |
| 716 | exit(0); |
| 717 | case 'V': |
| 718 | version(stdout); |
| 719 | exit(0); |
| 720 | case 'u': |
| 721 | usage(stdout); |
| 722 | exit(0); |
| 723 | case 'l': |
| 724 | exit(displaylists(listtab, argv + optind)); |
| 725 | case 'a': |
| 726 | if ((gch = gethash(optarg)) == 0) |
| 727 | die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg); |
| 728 | f |= f_oddhash; |
| 729 | break; |
| 730 | case 'E': |
| 731 | if ((e = getencoding(optarg)) == 0) |
| 732 | die(EXIT_FAILURE, "unknown encoding `%s'", optarg); |
| 733 | f |= f_oddenc; |
| 734 | break; |
| 735 | case 'f': |
| 736 | f |= f_files; |
| 737 | break; |
| 738 | case '0': |
| 739 | f |= f_raw; |
| 740 | break; |
| 741 | case 'e': |
| 742 | f |= f_escape; |
| 743 | break; |
| 744 | case 'c': |
| 745 | f |= f_check; |
| 746 | break; |
| 747 | case 'b': |
| 748 | f |= f_binary; |
| 749 | break; |
| 750 | case 'v': |
| 751 | f |= f_verbose; |
| 752 | break; |
| 753 | case 'p': |
| 754 | f |= f_progress; |
| 755 | break; |
| 756 | default: |
| 757 | f |= f_bogus; |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (f & f_bogus) { |
| 763 | usage(stderr); |
| 764 | exit(EXIT_FAILURE); |
| 765 | } |
| 766 | argv += optind; |
| 767 | argc -= optind; |
| 768 | |
| 769 | /* --- Generate output --- */ |
| 770 | |
| 771 | if (!(f & f_check) && (argc || (f & f_files))) { |
| 772 | if (f & f_oddhash) printf("#hash %s\n", gch->name); |
| 773 | if (f & f_oddenc) printf("#encoding %s\n", e->name); |
| 774 | if (f & f_escape) fputs("#escape\n", stdout); |
| 775 | } |
| 776 | if (!argc) |
| 777 | rc = hashsum(0, f, gch, e); |
| 778 | else { |
| 779 | int i; |
| 780 | int rrc; |
| 781 | |
| 782 | rc = 0; |
| 783 | for (i = 0; i < argc; i++) { |
| 784 | if ((rrc = hashsum(argv[i], f, gch, e)) != 0) |
| 785 | rc = rrc; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | return (rc); |
| 790 | } |
| 791 | |
| 792 | /*----- That's all, folks -------------------------------------------------*/ |