From: Sascha Hauer <s.hauer@pengutronix.de>
To: Hubert Feurstein <h.feurstein@gmail.com>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCHv2] commands/digest: add verify support
Date: Wed, 24 Apr 2013 07:55:23 +0200 [thread overview]
Message-ID: <20130424055523.GX32299@pengutronix.de> (raw)
In-Reply-To: <1366709085-20067-1-git-send-email-h.feurstein@gmail.com>
On Tue, Apr 23, 2013 at 11:24:45AM +0200, Hubert Feurstein wrote:
> Signed-off-by: Hubert Feurstein <h.feurstein@gmail.com>
> ---
> Changes:
> v2:
> - updated according to comments from Sascha
> - add support for verify against hash-file
>
> commands/digest.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 131 insertions(+), 10 deletions(-)
>
> diff --git a/commands/digest.c b/commands/digest.c
> index c9bb132..a4611d7 100644
> --- a/commands/digest.c
> +++ b/commands/digest.c
> @@ -25,24 +25,122 @@
> #include <xfuncs.h>
> #include <malloc.h>
> #include <digest.h>
> +#include <linux/ctype.h>
> +#include <getopt.h>
> +
> +static inline unsigned char parse_hexchar(char c)
> +{
> + if (!isxdigit(c))
> + return 0;
> +
> + return isdigit(c) ? (c - '0') : ((islower(c) ? toupper(c) : c) - 'A' + 0xA);
> +}
> +
> +static inline unsigned char parse_hexbyte(const char *p)
> +{
> + return (parse_hexchar(*p) << 4) | parse_hexchar(*(p + 1));
> +}
> +
> +static unsigned char *parse_hash(int hash_len, const char *hexstr)
> +{
> + int i;
> + unsigned char *p;
> +
> + p = calloc(hash_len, sizeof(unsigned char));
> + if (!p) {
> + perror("calloc");
> + return NULL;
> + }
> +
> + for (i = 0; i < hash_len; i++)
> + p[i] = parse_hexbyte(&hexstr[i * 2]);
> +
> + return p;
> +}
>
> static int do_digest(char *algorithm, int argc, char *argv[])
> {
> struct digest *d;
> int ret = 0;
> int i;
> - unsigned char *hash;
> + unsigned char *hash = NULL;
> + int opt;
> + unsigned char *verify_hash = NULL;
> + int verify = 0;
> + int min_argc = 2;
> + void *buf;
> + ssize_t bufsz;
>
> d = digest_get_by_name(algorithm);
> BUG_ON(!d);
>
> - if (argc < 2)
> - return COMMAND_ERROR_USAGE;
> + while ((opt = getopt(argc, argv, "v:V:")) > 0) {
> + switch (opt) {
> + case 'v':
> + if (verify) {
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
> +
> + verify = 1;
> + min_argc += 2;
> +
> + if (d->length != (strlen(optarg) / 2)) {
> + printf("invalid hash length (%d chars required)\n",
> + d->length * 2);
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
> +
> + verify_hash = parse_hash(d->length, optarg);
> + if (!verify_hash) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + break;
> + case 'V':
> + if (verify) {
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
> +
> + verify = 1;
> + min_argc += 2;
> +
> + buf = read_file(optarg, &bufsz);
> + if (!buf) {
> + ret = -ENOMEM;
> + goto out;
> + }
> +
> + if (bufsz < d->length) {
> + free(buf);
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
> +
> + verify_hash = parse_hash(d->length, buf);
> + free(buf);
> + break;
> + default:
> + ret = COMMAND_ERROR_USAGE;
> + goto out;
> + }
> + }
Please use something around the following lines to avoid some code
duplication.
case 'v':
verify++;
free(hash);
hash = xstrdup(optarg);
break;
case 'V':
verify++;
free(hash);
hash = read_file(optarg, &bufsiz);
break;
default:
ret = COMMAND_ERROR_USAGE;
goto out;
}
if (verify > 1)
return COMMAND_ERROR_USAGE;
if (verify) {
min_argc += 2;
verify_hash = parse_hash(d, buf);
if (!verify_hash)
return -ESOMETHING;
}
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
prev parent reply other threads:[~2013-04-24 5:55 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-19 8:43 [PATCH] " Hubert Feurstein
2013-04-21 11:22 ` Sascha Hauer
2013-04-23 9:24 ` [PATCHv2] " Hubert Feurstein
2013-04-24 5:55 ` Sascha Hauer [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130424055523.GX32299@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=h.feurstein@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox