"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "littleutils/filehash.c" between
littleutils-1.2.4.tar.lz and littleutils-1.2.5.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

filehash.c  (littleutils-1.2.4.tar.lz):filehash.c  (littleutils-1.2.5.tar.lz)
/* filehash: Print various hash digests and filesizes for specified files. /* filehash: Print various hash digests and filesizes for specified files.
Copyright (C) 2004-2020 by Brian Lindholm. Copyright (C) 2004-2021 by Brian Lindholm.
This file is part of the littleutils utility set. This file is part of the littleutils utility set.
The filehash utility is free software; you can redistribute it and/or modify The filehash utility is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the it under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any later Free Software Foundation; either version 3, or (at your option) any later
version. version.
The filehash utility is distributed in the hope that it will be useful, but The filehash utility is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details. more details.
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
the littleutils. If not, see <https://www.gnu.org/licenses/>. */ the littleutils. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h> #include <config.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <limits.h> #include <limits.h>
#include <stdio.h> #ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
# include <stdlib.h> # include <stdlib.h>
#endif #endif
#ifdef HAVE_STRING_H #ifdef HAVE_STRING_H
# include <string.h> # include <string.h>
#endif #endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H #ifdef HAVE_SYS_STAT_H
# include <sys/stat.h> # include <sys/stat.h>
#endif #endif
#ifdef HAVE_SYS_TYPES_H
#ifdef HAVE_GETOPT_H # include <sys/types.h>
# include <getopt.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
# include <unistd.h> # include <unistd.h>
# define OPTEND -1 # define OPTEND -1
#else #else
# define OPTEND EOF # define OPTEND EOF
#endif #endif
#ifdef HAVE_GETOPT_H
#if HAVE_INTTYPES_H # include <getopt.h>
# include <inttypes.h>
#endif #endif
#ifdef MSDOS #ifdef MSDOS
# include "io.h" # include "io.h"
#endif #endif
#include "md5.h" #include "md5.h"
#include "sha1.h" #include "sha1.h"
#include "sha256.h" #include "sha256.h"
#include "sha512.h" #include "sha512.h"
skipping to change at line 87 skipping to change at line 88
char *sizefmt = (sizeof (off_t) <= sizeof (long) ? "%lu" : "%llu"); char *sizefmt = (sizeof (off_t) <= sizeof (long) ? "%lu" : "%llu");
static void static void
help (FILE *where) help (FILE *where)
{ {
fprintf (where, fprintf (where,
"filehash " PACKAGE_VERSION "\n" "filehash " PACKAGE_VERSION "\n"
"usage: filehash [-1(MD5)] [-2(SHA1)] [-3(SHA224) ][-4(SHA256)]\n" "usage: filehash [-1(MD5)] [-2(SHA1)] [-3(SHA224) ][-4(SHA256)]\n"
" [-5(SHA384)] [-6(SHA512)] [-7(BLAKE2B_256)] [-8(BLAKE2B_256)]\n" " [-5(SHA384)] [-6(SHA512)] [-7(BLAKE2B_256)] [-8(BLAKE2B_256)]\n"
" [-c(lassic)] [-f file_list] [-h(elp)] [-n byte_count]\n" " [-b(ase64url)] [-c(lassic)] [-f file_list] [-h(elp)]\n"
" [-o offset] [-p(ipe) ] [-q(uiet)] [-s(ize)] file...\n"); " [-n byte_count] [-o offset] [-p(ipe) ] [-q(uiet)] [-s(ize)]\n"
" [-v(erbose)] file...\n");
}
static void
encode_hash (unsigned char *string, unsigned char *hash, int bytes, int use_base
64)
{
int i, j;
static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
static const unsigned char hex_table[65] = "0123456789abcdef";
j = 0;
if (use_base64) {
for (i = 0; i < (bytes / 3); ++i) {
string[j++] = base64_table[hash[i*3] >> 2];
string[j++] = base64_table[((hash[i*3] & 0x03) << 4) | (hash[i*3+1] >> 4)]
;
string[j++] = base64_table[((hash[i*3+1] & 0x0F) << 2) | (hash[i*3+2] >> 6
)];
string[j++] = base64_table[hash[i*3+2] & 0x3F];
}
if ((bytes % 3) == 2) {
string[j++] = base64_table[hash[bytes-2] >> 2];
string[j++] = base64_table[((hash[bytes-2] & 0x03) << 4) | (hash[bytes-1]
>> 4)];
string[j++] = base64_table[(hash[bytes-1] & 0x0F) << 2];
}
else if ((bytes % 3) == 1) {
string[j++] = base64_table[hash[bytes-1] >> 2];
string[j++] = base64_table[(hash[bytes-1] & 0x03) << 4];
}
string[j++] = '\000';
}
else {
for (i = 0; i < bytes; ++i) {
string[j++] = hex_table[hash[i] >> 4];
string[j++] = hex_table[hash[i] & 0x0F];
}
string[j++] = '\000';
}
} }
static void static void
print_filehash (char *filename, off_t offset, off_t read_bytes, int print_size, print_filehash (char *filename, off_t offset, off_t read_bytes, int print_size,
int run_md5, int run_sha1, int run_sha224, int run_sha256, int run_sha384, int run_md5, int run_sha1, int run_sha224, int run_sha256, int run_sha384,
int run_sha512, int run_blake2b_256, int run_blake2b_512, int verbose, int cla int run_sha512, int run_blake2b_256, int run_blake2b_512, int classic,
ssic) int use_base64, int verbose)
{ {
FILE *file; FILE *file;
int i, not_first, rc; int i, not_first, rc;
struct stat file_stats; struct stat file_stats;
unsigned char md5result[16], sha1result[20], sha224result[28], unsigned char md5result[16], sha1result[20], sha224result[28],
sha256result[32], sha384result[48], sha512result[64], sha256result[32], sha384result[48], sha512result[64],
blake2b_256result[32], blake2b_512result[64]; blake2b_256result[32], blake2b_512result[64], string[129];
if (stat (filename, &file_stats)) if (stat (filename, &file_stats))
fprintf (stderr, "filehash error: can't stat %s\n", filename); fprintf (stderr, "filehash error: can't stat %s\n", filename);
else if (((file_stats.st_mode & S_IFDIR) != S_IFDIR) && else if (((file_stats.st_mode & S_IFDIR) != S_IFDIR) &&
((file_stats.st_mode & S_IFREG) == S_IFREG)) ((file_stats.st_mode & S_IFREG) == S_IFREG))
{ {
if ((file = fopen (filename, "rb")) == NULL) if ((file = fopen (filename, "rb")) == NULL)
fprintf (stderr, "filehash error: can't open %s\n", filename); fprintf (stderr, "filehash error: can't open %s\n", filename);
else else
{ {
not_first = 0; not_first = 0;
if (run_md5) { if (run_md5) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = md5_stream (file, md5result, read_bytes); rc = md5_stream (file, md5result, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: md5_stream failed on %s\n", file
"filehash error: md5_stream failed on %s\n", filename); name);
not_first = 1; not_first = 1;
} }
if (run_sha1) { if (run_sha1) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = sha1_stream (file, sha1result, read_bytes); rc = sha1_stream (file, sha1result, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: sha1_stream failed on %s\n", fil
"filehash error: sha1_stream failed on %s\n", filename); ename);
not_first = 1; not_first = 1;
} }
if (run_sha224) { if (run_sha224) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = sha224_stream (file, sha224result, read_bytes); rc = sha224_stream (file, sha224result, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: sha224_stream failed on %s\n", f
"filehash error: sha224_stream failed on %s\n", filename); ilename);
not_first = 1; not_first = 1;
} }
if (run_sha256) { if (run_sha256) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = sha256_stream (file, sha256result, read_bytes); rc = sha256_stream (file, sha256result, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: sha256_stream failed on %s\n", f
"filehash error: sha256_stream failed on %s\n", filename); ilename);
not_first = 1; not_first = 1;
} }
if (run_sha384) { if (run_sha384) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = sha384_stream (file, sha384result, read_bytes); rc = sha384_stream (file, sha384result, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: sha384_stream failed on %s\n", f
"filehash error: sha384_stream failed on %s\n", filename); ilename);
not_first = 1; not_first = 1;
} }
if (run_sha512) { if (run_sha512) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = sha512_stream (file, sha512result, read_bytes); rc = sha512_stream (file, sha512result, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: sha512_stream failed on %s\n", f
"filehash error: sha512_stream failed on %s\n", filename); ilename);
not_first = 1; not_first = 1;
} }
if (run_blake2b_256) { if (run_blake2b_256) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = blake2b_stream (file, blake2b_256result, 32, read_bytes); rc = blake2b_stream (file, blake2b_256result, 32, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: sha384_stream failed on %s\n", f
"filehash error: sha384_stream failed on %s\n", filename); ilename);
not_first = 1; not_first = 1;
} }
if (run_blake2b_512) { if (run_blake2b_512) {
if (not_first || (offset > 0)) if (not_first || (offset > 0))
(void) fseek (file, (off_t) offset, 0); (void) fseek (file, (off_t) offset, 0);
rc = blake2b_stream (file, blake2b_512result, 64, read_bytes); rc = blake2b_stream (file, blake2b_512result, 64, read_bytes);
if (rc) if (rc)
fprintf (stderr, fprintf (stderr, "filehash error: sha512_stream failed on %s\n", f
"filehash error: sha512_stream failed on %s\n", filename); ilename);
not_first = 1; not_first = 1;
} }
(void) fclose (file); (void) fclose (file);
not_first = 0; not_first = 0;
if ((verbose == 1) && (classic == 0)) { if ((verbose == 1) && (classic == 0)) {
fprintf (stdout, "%s", filename); fprintf (stdout, "%s", filename);
not_first = 1; not_first = 1;
} }
if (print_size) { if (print_size) {
if (classic) { if (classic) {
fprintf (stdout, sizefmt, file_stats.st_size); fprintf (stdout, sizefmt, file_stats.st_size);
fprintf (stdout, " %s\n", filename); fprintf (stdout, " %s\n", filename);
} }
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
fprintf (stdout, sizefmt, file_stats.st_size);
not_first = 1; not_first = 1;
fprintf (stdout, sizefmt, file_stats.st_size);
} }
} }
if (run_md5) { if (run_md5) {
if (classic) { encode_hash(string, md5result, 16, use_base64);
for (i = 0; i < 16; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) md5result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 16; ++i)
fprintf (stdout, "%02x", (unsigned int) md5result[i]);
not_first = 1; not_first = 1;
fprintf (stdout, "%s", string);
} }
} }
if (run_sha1) { if (run_sha1) {
if (classic) { encode_hash(string, sha1result, 20, use_base64);
for (i = 0; i < 20; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) sha1result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 20; ++i)
fprintf (stdout, "%02x", (unsigned int) sha1result[i]);
not_first = 1; not_first = 1;
fprintf (stdout, "%s", string);
} }
} }
if (run_sha224) { if (run_sha224) {
if (classic) { encode_hash(string, sha224result, 28, use_base64);
for (i = 0; i < 28; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) sha224result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 28; ++i)
fprintf (stdout, "%02x", (unsigned int) sha224result[i]);
not_first = 1; not_first = 1;
fprintf (stdout, "%s", string);
} }
} }
if (run_sha256) { if (run_sha256) {
if (classic) { encode_hash(string, sha256result, 32, use_base64);
for (i = 0; i < 32; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) sha256result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 32; ++i)
fprintf (stdout, "%02x", (unsigned int) sha256result[i]);
not_first = 1; not_first = 1;
fprintf (stdout, "%s", string);
} }
} }
if (run_sha384) { if (run_sha384) {
if (classic) { encode_hash(string, sha384result, 48, use_base64);
for (i = 0; i < 48; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) sha384result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 48; ++i)
fprintf (stdout, "%02x", (unsigned int) sha384result[i]);
not_first = 1; not_first = 1;
fprintf (stdout, "%s", string);
} }
} }
if (run_sha512) { if (run_sha512) {
if (classic) { encode_hash(string, sha512result, 64, use_base64);
for (i = 0; i < 64; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) sha512result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 64; ++i) not_first = 1;
fprintf (stdout, "%02x", (unsigned int) sha512result[i]); fprintf (stdout, "%s", string);
} }
} }
if (run_blake2b_256) { if (run_blake2b_256) {
if (classic) { encode_hash(string, blake2b_256result, 32, use_base64);
for (i = 0; i < 32; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) blake2b_256result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 32; ++i) not_first = 1;
fprintf (stdout, "%02x", (unsigned int) blake2b_256result[i]); fprintf (stdout, "%s", string);
} }
} }
if (run_blake2b_512) { if (run_blake2b_512) {
if (classic) { encode_hash(string, blake2b_512result, 64, use_base64);
for (i = 0; i < 64; ++i) if (classic)
fprintf (stdout, "%02x", (unsigned int) blake2b_512result[i]); fprintf (stdout, "%s %s\n", string, filename);
fprintf (stdout, " %s\n", filename);
}
else { else {
if (not_first) if (not_first)
fprintf (stdout, "\t"); fprintf (stdout, "\t");
for (i = 0; i < 64; ++i) not_first = 1;
fprintf (stdout, "%02x", (unsigned int) blake2b_512result[i]); fprintf (stdout, "%s", string);
} }
} }
if (classic == 0) if (classic == 0)
fprintf (stdout, "\n"); fprintf (stdout, "\n");
} }
} }
} }
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
FILE *infile; FILE *infile;
char filename[PATH_MAX], *listname, *newline, *rc; char filename[PATH_MAX], *listname, *newline, *rc;
int argn, classic, offset, opt, print_size, run_md5, run_sha1, run_sha224, int argn, classic, offset, opt, print_size, run_md5, run_sha1, run_sha224,
run_sha256, run_sha384, run_sha512, run_blake2b_256, run_blake2b_512, run_sha256, run_sha384, run_sha512, run_blake2b_256, run_blake2b_512,
use_file, use_pipe, verbose; use_base64, use_file, use_pipe, verbose;
off_t read_bytes, tmp_bytes; off_t read_bytes, tmp_bytes;
/* parse options */ /* parse options */
classic = 0; classic = 0;
listname = ""; listname = "";
offset = 0; offset = 0;
print_size = 0; print_size = 0;
read_bytes = -1; read_bytes = -1;
run_md5 = 0; run_md5 = 0;
run_sha1 = 0; run_sha1 = 0;
run_sha224 = 0; run_sha224 = 0;
run_sha256 = 0; run_sha256 = 0;
run_sha384 = 0; run_sha384 = 0;
run_sha512 = 0; run_sha512 = 0;
run_blake2b_256 = 0; run_blake2b_256 = 0;
run_blake2b_512 = 0; run_blake2b_512 = 0;
use_base64 = 0;
use_file = 0; use_file = 0;
use_pipe = 0; use_pipe = 0;
verbose = 0; verbose = 0;
while ((opt = getopt (argc, argv, "12345678cf:hpn:o:qsv")) != OPTEND) while ((opt = getopt (argc, argv, "12345678bcf:hpn:o:qsv")) != OPTEND)
switch (opt) switch (opt)
{ {
case '1': case '1':
run_md5 = 1; run_md5 = 1;
break; break;
case '2': case '2':
run_sha1 = 1; run_sha1 = 1;
break; break;
case '3': case '3':
run_sha224 = 1; run_sha224 = 1;
skipping to change at line 373 skipping to change at line 384
break; break;
case '6': case '6':
run_sha512 = 1; run_sha512 = 1;
break; break;
case '7': case '7':
run_blake2b_256 = 1; run_blake2b_256 = 1;
break; break;
case '8': case '8':
run_blake2b_512 = 1; run_blake2b_512 = 1;
break; break;
case 'b':
use_base64 = 1;
break;
case 'c': case 'c':
classic = 1; classic = 1;
break; break;
case 'f': case 'f':
use_file = 1; use_file = 1;
listname = optarg; listname = optarg;
break; break;
case 'h': case 'h':
help (stdout); help (stdout);
return (0); return (0);
skipping to change at line 452 skipping to change at line 466
{ {
rc = fgets (filename, PATH_MAX - 1, infile); rc = fgets (filename, PATH_MAX - 1, infile);
if (rc != NULL) if (rc != NULL)
{ {
newline = strchr (filename, '\n'); newline = strchr (filename, '\n');
if (newline != NULL) if (newline != NULL)
*newline = '\0'; *newline = '\0';
if (strlen (filename) != 0) if (strlen (filename) != 0)
print_filehash (filename, offset, read_bytes, print_size, print_filehash (filename, offset, read_bytes, print_size,
run_md5, run_sha1, run_sha224, run_sha256, run_sha384, run_md5, run_sha1, run_sha224, run_sha256, run_sha384,
run_sha512, run_blake2b_256, run_blake2b_512, verbose, run_sha512, run_blake2b_256, run_blake2b_512, classic,
classic); use_base64, verbose);
} }
} }
(void) fclose (infile); (void) fclose (infile);
} }
} }
/* process files listed on stdin (i.e., the -p option) */ /* process files listed on stdin (i.e., the -p option) */
if (use_pipe) if (use_pipe)
while (!feof (stdin)) while (!feof (stdin))
{ {
rc = fgets (filename, PATH_MAX - 1, stdin); rc = fgets (filename, PATH_MAX - 1, stdin);
if (rc != NULL) if (rc != NULL)
{ {
newline = strchr (filename, '\n'); newline = strchr (filename, '\n');
if (newline != NULL) if (newline != NULL)
*newline = '\0'; *newline = '\0';
if (strlen (filename) != 0) if (strlen (filename) != 0)
print_filehash (filename, offset, read_bytes, print_size, print_filehash (filename, offset, read_bytes, print_size, run_md5,
run_md5, run_sha1, run_sha224, run_sha256, run_sha384, run_sha1, run_sha224, run_sha256, run_sha384, run_sha512,
run_sha512, run_blake2b_256, run_blake2b_512, verbose, run_blake2b_256, run_blake2b_512, classic, use_base64, verbose);
classic);
} }
} }
/* process files given in the argument list */ /* process files given in the argument list */
for (argn = optind; argn < argc; argn++) for (argn = optind; argn < argc; argn++)
print_filehash (argv[argn], offset, read_bytes, print_size, print_filehash (argv[argn], offset, read_bytes, print_size, run_md5,
run_md5, run_sha1, run_sha224, run_sha256, run_sha384, run_sha1, run_sha224, run_sha256, run_sha384, run_sha512,
run_sha512, run_blake2b_256, run_blake2b_512, verbose, classic); run_blake2b_256, run_blake2b_512, classic, use_base64, verbose);
/* indicate successful finish */ /* indicate successful finish */
return (0); return (0);
} }
 End of changes. 48 change blocks. 
100 lines changed or deleted 124 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)