From owner-svn-src-all@freebsd.org Fri Jun 19 19:16:26 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 6B864336D56; Fri, 19 Jun 2020 19:16:26 +0000 (UTC) (envelope-from allanjude@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49pT7G2CCZz3S4p; Fri, 19 Jun 2020 19:16:26 +0000 (UTC) (envelope-from allanjude@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 2893D1FEA1; Fri, 19 Jun 2020 19:16:26 +0000 (UTC) (envelope-from allanjude@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 05JJGQUJ093843; Fri, 19 Jun 2020 19:16:26 GMT (envelope-from allanjude@FreeBSD.org) Received: (from allanjude@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 05JJGQCR093842; Fri, 19 Jun 2020 19:16:26 GMT (envelope-from allanjude@FreeBSD.org) Message-Id: <202006191916.05JJGQCR093842@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: allanjude set sender to allanjude@FreeBSD.org using -f From: Allan Jude Date: Fri, 19 Jun 2020 19:16:25 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r362412 - head/sbin/md5 X-SVN-Group: head X-SVN-Commit-Author: allanjude X-SVN-Commit-Paths: head/sbin/md5 X-SVN-Commit-Revision: 362412 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 19 Jun 2020 19:16:26 -0000 Author: allanjude Date: Fri Jun 19 19:16:25 2020 New Revision: 362412 URL: https://svnweb.freebsd.org/changeset/base/362412 Log: md5(1): fix -c flag to work with input on stdin Previously, the -p and -c flags were ignored when reading from stdin Additionally, -s and -c can be used together now. PR: 247295 Reviewed by: kevans MFC after: 2 weeks Relnotes: yes Sponsored by: Klara Inc. Differential Revision: https://reviews.freebsd.org/D25362 Modified: head/sbin/md5/md5.c Modified: head/sbin/md5/md5.c ============================================================================== --- head/sbin/md5/md5.c Fri Jun 19 18:54:55 2020 (r362411) +++ head/sbin/md5/md5.c Fri Jun 19 19:16:25 2020 (r362412) @@ -52,11 +52,14 @@ __FBSDID("$FreeBSD$"); #define TEST_BLOCK_COUNT 100000 #define MDTESTCOUNT 8 +static int pflag; static int qflag; static int rflag; static int sflag; +static int skip; static char* checkAgainst; static int checksFailed; +static int failed; typedef void (DIGEST_Init)(void *); typedef void (DIGEST_Update)(void *, const unsigned char *, size_t); @@ -86,10 +89,10 @@ typedef struct Algorithm_t { } Algorithm_t; static void MD5_Update(MD5_CTX *, const unsigned char *, size_t); -static void MDString(const Algorithm_t *, const char *); +static void MDOutput(const Algorithm_t *, char *, char **); static void MDTimeTrial(const Algorithm_t *); static void MDTestSuite(const Algorithm_t *); -static void MDFilter(const Algorithm_t *, int); +static char *MDFilter(const Algorithm_t *, char*, int); static void usage(const Algorithm_t *); typedef union { @@ -170,9 +173,9 @@ main(int argc, char *argv[]) cap_rights_t rights; #endif int ch, fd; - char *p; + char *p, *string; char buf[HEX_DIGEST_LENGTH]; - int failed; + size_t len; unsigned digest; const char* progname; @@ -191,13 +194,14 @@ main(int argc, char *argv[]) failed = 0; checkAgainst = NULL; checksFailed = 0; + skip = 0; while ((ch = getopt(argc, argv, "c:pqrs:tx")) != -1) switch (ch) { case 'c': checkAgainst = optarg; break; case 'p': - MDFilter(&Algorithm[digest], 1); + pflag = 1; break; case 'q': qflag = 1; @@ -207,13 +211,15 @@ main(int argc, char *argv[]) break; case 's': sflag = 1; - MDString(&Algorithm[digest], optarg); + string = optarg; break; case 't': MDTimeTrial(&Algorithm[digest]); + skip = 1; break; case 'x': MDTestSuite(&Algorithm[digest]); + skip = 1; break; default: usage(&Algorithm[digest]); @@ -249,32 +255,19 @@ main(int argc, char *argv[]) } p = Algorithm[digest].Fd(fd, buf); (void)close(fd); - if (p == NULL) { - warn("%s", *argv); - failed++; - } else { - if (qflag) - printf("%s", p); - else if (rflag) - printf("%s %s", p, *argv); - else - printf("%s (%s) = %s", - Algorithm[digest].name, *argv, p); - if (checkAgainst && strcasecmp(checkAgainst, p) != 0) - { - checksFailed++; - if (!qflag) - printf(" [ Failed ]"); - } - printf("\n"); - } + MDOutput(&Algorithm[digest], p, argv); } while (*++argv); - } else if (!sflag && (optind == 1 || qflag || rflag)) { + } else if (!sflag && !skip) { #ifdef HAVE_CAPSICUM if (caph_limit_stdin() < 0 || caph_enter() < 0) err(1, "capsicum"); #endif - MDFilter(&Algorithm[digest], 0); + p = MDFilter(&Algorithm[digest], (char *)&buf, pflag); + MDOutput(&Algorithm[digest], p, NULL); + } else if (sflag) { + len = strlen(string); + p = Algorithm[digest].Data(string, len, buf); + MDOutput(&Algorithm[digest], p, &string); } if (failed != 0) @@ -284,30 +277,38 @@ main(int argc, char *argv[]) return (0); } + /* - * Digests a string and prints the result. + * Common output handling */ static void -MDString(const Algorithm_t *alg, const char *string) +MDOutput(const Algorithm_t *alg, char *p, char *argv[]) { - size_t len = strlen(string); - char buf[HEX_DIGEST_LENGTH]; - - alg->Data(string,len,buf); - if (qflag) - printf("%s", buf); - else if (rflag) - printf("%s \"%s\"", buf, string); - else - printf("%s (\"%s\") = %s", alg->name, string, buf); - if (checkAgainst && strcasecmp(buf,checkAgainst) != 0) - { - checksFailed++; - if (!qflag) - printf(" [ failed ]"); + if (p == NULL) { + warn("%s", *argv); + failed++; + } else { + /* + * If argv is NULL we are reading from stdin, where the output + * format has always been just the hash. + */ + if (qflag || argv == NULL) + printf("%s", p); + else if (rflag) + printf("%s %s", p, *argv); + else + printf("%s (%s) = %s", + alg->name, *argv, p); + if (checkAgainst && strcasecmp(checkAgainst, p) != 0) + { + checksFailed++; + if (!qflag) + printf(" [ Failed ]"); + } + printf("\n"); } - printf("\n"); } + /* * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. */ @@ -507,13 +508,13 @@ MDTestSuite(const Algorithm_t *alg) /* * Digests the standard input and prints the result. */ -static void -MDFilter(const Algorithm_t *alg, int tee) +static char * +MDFilter(const Algorithm_t *alg, char *buf, int tee) { DIGEST_CTX context; unsigned int len; unsigned char buffer[BUFSIZ]; - char buf[HEX_DIGEST_LENGTH]; + char *p; alg->Init(&context); while ((len = fread(buffer, 1, BUFSIZ, stdin))) { @@ -521,7 +522,9 @@ MDFilter(const Algorithm_t *alg, int tee) err(1, "stdout"); alg->Update(&context, buffer, len); } - printf("%s\n", alg->End(&context, buf)); + p = alg->End(&context, buf); + + return (p); } static void