Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

digest_cmd.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <getopt.h>
  23. #include <ipxe/command.h>
  24. #include <ipxe/parseopt.h>
  25. #include <ipxe/image.h>
  26. #include <ipxe/crypto.h>
  27. #include <ipxe/md5.h>
  28. #include <ipxe/sha1.h>
  29. /** @file
  30. *
  31. * Digest commands
  32. *
  33. */
  34. /** "digest" options */
  35. struct digest_options {};
  36. /** "digest" option list */
  37. static struct option_descriptor digest_opts[] = {};
  38. /** "digest" command descriptor */
  39. static struct command_descriptor digest_cmd =
  40. COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
  41. "<image> [<image>...]" );
  42. /**
  43. * The "digest" command
  44. *
  45. * @v argc Argument count
  46. * @v argv Argument list
  47. * @v digest Digest algorithm
  48. * @ret rc Return status code
  49. */
  50. static int digest_exec ( int argc, char **argv,
  51. struct digest_algorithm *digest ) {
  52. struct digest_options opts;
  53. struct image *image;
  54. uint8_t digest_ctx[digest->ctxsize];
  55. uint8_t digest_out[digest->digestsize];
  56. uint8_t buf[128];
  57. size_t offset;
  58. size_t len;
  59. size_t frag_len;
  60. int i;
  61. unsigned j;
  62. int rc;
  63. /* Parse options */
  64. if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
  65. return rc;
  66. for ( i = optind ; i < argc ; i++ ) {
  67. /* find image */
  68. if ( ( rc = parse_image ( argv[i], &image ) ) != 0 )
  69. continue;
  70. offset = 0;
  71. len = image->len;
  72. /* calculate digest */
  73. digest_init ( digest, digest_ctx );
  74. while ( len ) {
  75. frag_len = len;
  76. if ( frag_len > sizeof ( buf ) )
  77. frag_len = sizeof ( buf );
  78. copy_from_user ( buf, image->data, offset, frag_len );
  79. digest_update ( digest, digest_ctx, buf, frag_len );
  80. len -= frag_len;
  81. offset += frag_len;
  82. }
  83. digest_final ( digest, digest_ctx, digest_out );
  84. for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
  85. printf ( "%02x", digest_out[j] );
  86. printf ( " %s\n", image->name );
  87. }
  88. return 0;
  89. }
  90. static int md5sum_exec ( int argc, char **argv ) {
  91. return digest_exec ( argc, argv, &md5_algorithm );
  92. }
  93. static int sha1sum_exec ( int argc, char **argv ) {
  94. return digest_exec ( argc, argv, &sha1_algorithm );
  95. }
  96. struct command md5sum_command __command = {
  97. .name = "md5sum",
  98. .exec = md5sum_exec,
  99. };
  100. struct command sha1sum_command __command = {
  101. .name = "sha1sum",
  102. .exec = sha1sum_exec,
  103. };