|
|
1.1 ! root 1: /* ! 2: * Copyright (C) 2009 Daniel Verkamp <[email protected]>. ! 3: * ! 4: * This program is free software; you can redistribute it and/or ! 5: * modify it under the terms of the GNU General Public License as ! 6: * published by the Free Software Foundation; either version 2 of the ! 7: * License, or any later version. ! 8: * ! 9: * This program is distributed in the hope that it will be useful, but ! 10: * WITHOUT ANY WARRANTY; without even the implied warranty of ! 11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ! 12: * General Public License for more details. ! 13: * ! 14: * You should have received a copy of the GNU General Public License ! 15: * along with this program; if not, write to the Free Software ! 16: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ! 17: */ ! 18: ! 19: FILE_LICENCE ( GPL2_OR_LATER ); ! 20: ! 21: #include <stdio.h> ! 22: #include <string.h> ! 23: #include <unistd.h> ! 24: #include <getopt.h> ! 25: #include <ipxe/command.h> ! 26: #include <ipxe/parseopt.h> ! 27: #include <ipxe/image.h> ! 28: #include <ipxe/crypto.h> ! 29: #include <ipxe/md5.h> ! 30: #include <ipxe/sha1.h> ! 31: ! 32: /** @file ! 33: * ! 34: * Digest commands ! 35: * ! 36: */ ! 37: ! 38: /** "digest" options */ ! 39: struct digest_options {}; ! 40: ! 41: /** "digest" option list */ ! 42: static struct option_descriptor digest_opts[] = {}; ! 43: ! 44: /** "digest" command descriptor */ ! 45: static struct command_descriptor digest_cmd = ! 46: COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS, ! 47: "<image> [<image>...]" ); ! 48: ! 49: /** ! 50: * The "digest" command ! 51: * ! 52: * @v argc Argument count ! 53: * @v argv Argument list ! 54: * @v digest Digest algorithm ! 55: * @ret rc Return status code ! 56: */ ! 57: static int digest_exec ( int argc, char **argv, ! 58: struct digest_algorithm *digest ) { ! 59: struct digest_options opts; ! 60: struct image *image; ! 61: uint8_t digest_ctx[digest->ctxsize]; ! 62: uint8_t digest_out[digest->digestsize]; ! 63: uint8_t buf[128]; ! 64: size_t offset; ! 65: size_t len; ! 66: size_t frag_len; ! 67: int i; ! 68: unsigned j; ! 69: int rc; ! 70: ! 71: /* Parse options */ ! 72: if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 ) ! 73: return rc; ! 74: ! 75: for ( i = optind ; i < argc ; i++ ) { ! 76: ! 77: /* find image */ ! 78: if ( ( rc = parse_image ( argv[i], &image ) ) != 0 ) ! 79: continue; ! 80: offset = 0; ! 81: len = image->len; ! 82: ! 83: /* calculate digest */ ! 84: digest_init ( digest, digest_ctx ); ! 85: while ( len ) { ! 86: frag_len = len; ! 87: if ( frag_len > sizeof ( buf ) ) ! 88: frag_len = sizeof ( buf ); ! 89: copy_from_user ( buf, image->data, offset, frag_len ); ! 90: digest_update ( digest, digest_ctx, buf, frag_len ); ! 91: len -= frag_len; ! 92: offset += frag_len; ! 93: } ! 94: digest_final ( digest, digest_ctx, digest_out ); ! 95: ! 96: for ( j = 0 ; j < sizeof ( digest_out ) ; j++ ) ! 97: printf ( "%02x", digest_out[j] ); ! 98: ! 99: printf ( " %s\n", image->name ); ! 100: } ! 101: ! 102: return 0; ! 103: } ! 104: ! 105: static int md5sum_exec ( int argc, char **argv ) { ! 106: return digest_exec ( argc, argv, &md5_algorithm ); ! 107: } ! 108: ! 109: static int sha1sum_exec ( int argc, char **argv ) { ! 110: return digest_exec ( argc, argv, &sha1_algorithm ); ! 111: } ! 112: ! 113: struct command md5sum_command __command = { ! 114: .name = "md5sum", ! 115: .exec = md5sum_exec, ! 116: }; ! 117: ! 118: struct command sha1sum_command __command = { ! 119: .name = "sha1sum", ! 120: .exec = sha1sum_exec, ! 121: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.