md5_sha1_sum.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2003 Glenn L. McGrath
  3. * Copyright (C) 2003-2004 Erik Andersen
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <fcntl.h>
  20. #include <limits.h>
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include "busybox.h"
  27. #define FLAG_SILENT 1
  28. #define FLAG_CHECK 2
  29. #define FLAG_WARN 4
  30. /* This might be useful elsewhere */
  31. static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
  32. unsigned char hash_length)
  33. {
  34. int x, len, max;
  35. unsigned char *hex_value;
  36. max = (hash_length * 2) + 2;
  37. hex_value = xmalloc(max);
  38. for (x = len = 0; x < hash_length; x++) {
  39. len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
  40. }
  41. return (hex_value);
  42. }
  43. static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
  44. {
  45. int src_fd = strcmp(filename, "-") == 0 ? STDIN_FILENO :
  46. open(filename, O_RDONLY);
  47. if (src_fd == -1) {
  48. bb_perror_msg("%s", filename);
  49. return NULL;
  50. } else {
  51. uint8_t *hash_value;
  52. RESERVE_CONFIG_UBUFFER(hash_value_bin, 20);
  53. hash_value = hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2 ?
  54. hash_bin_to_hex(hash_value_bin, hash_algo == HASH_MD5 ? 16 : 20) :
  55. NULL;
  56. RELEASE_CONFIG_BUFFER(hash_value_bin);
  57. close(src_fd);
  58. return hash_value;
  59. }
  60. }
  61. /* This could become a common function for md5 as well, by using md5_stream */
  62. static int hash_files(int argc, char **argv, const uint8_t hash_algo)
  63. {
  64. int return_value = EXIT_SUCCESS;
  65. uint8_t *hash_value;
  66. #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
  67. unsigned int flags;
  68. flags = bb_getopt_ulflags(argc, argv, "scw");
  69. #endif
  70. #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
  71. if (!(flags & FLAG_CHECK)) {
  72. if (flags & FLAG_SILENT) {
  73. bb_error_msg_and_die
  74. ("the -s option is meaningful only when verifying checksums");
  75. } else if (flags & FLAG_WARN) {
  76. bb_error_msg_and_die
  77. ("the -w option is meaningful only when verifying checksums");
  78. }
  79. }
  80. #endif
  81. if (argc == optind) {
  82. argv[argc++] = "-";
  83. }
  84. #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
  85. if (flags & FLAG_CHECK) {
  86. FILE *pre_computed_stream;
  87. int count_total = 0;
  88. int count_failed = 0;
  89. char *file_ptr = argv[optind];
  90. char *line;
  91. if (optind + 1 != argc) {
  92. bb_error_msg_and_die
  93. ("only one argument may be specified when using -c");
  94. }
  95. if (strcmp(file_ptr, "-") == 0) {
  96. pre_computed_stream = stdin;
  97. } else {
  98. pre_computed_stream = bb_xfopen(file_ptr, "r");
  99. }
  100. while ((line = bb_get_chomped_line_from_file(pre_computed_stream)) != NULL) {
  101. char *filename_ptr;
  102. count_total++;
  103. filename_ptr = strstr(line, " ");
  104. if (filename_ptr == NULL) {
  105. if (flags & FLAG_WARN) {
  106. bb_error_msg("Invalid format");
  107. }
  108. free(line);
  109. continue;
  110. }
  111. *filename_ptr = '\0';
  112. filename_ptr += 2;
  113. hash_value = hash_file(filename_ptr, hash_algo);
  114. if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
  115. if (!(flags & FLAG_SILENT))
  116. printf("%s: OK\n", filename_ptr);
  117. } else {
  118. if (!(flags & FLAG_SILENT))
  119. printf("%s: FAILED\n", filename_ptr);
  120. count_failed++;
  121. return_value = EXIT_FAILURE;
  122. }
  123. /* possible free(NULL) */
  124. free(hash_value);
  125. free(line);
  126. }
  127. if (count_failed && !(flags & FLAG_SILENT)) {
  128. bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
  129. count_failed, count_total);
  130. }
  131. if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
  132. bb_perror_msg_and_die("Couldnt close file %s", file_ptr);
  133. }
  134. } else
  135. #endif
  136. {
  137. uint8_t hash_length;
  138. if (hash_algo == HASH_MD5) {
  139. hash_length = 16;
  140. } else {
  141. hash_length = 20;
  142. }
  143. hash_value = xmalloc(hash_length);
  144. while (optind < argc) {
  145. char *file_ptr = argv[optind++];
  146. hash_value = hash_file(file_ptr, hash_algo);
  147. if (hash_value == NULL) {
  148. return_value = EXIT_FAILURE;
  149. } else {
  150. printf("%s %s\n", hash_value, file_ptr);
  151. free(hash_value);
  152. }
  153. }
  154. }
  155. return (return_value);
  156. }
  157. #ifdef CONFIG_MD5SUM
  158. extern int md5sum_main(int argc, char **argv)
  159. {
  160. return(hash_files(argc, argv, HASH_MD5));
  161. }
  162. #endif
  163. #ifdef CONFIG_SHA1SUM
  164. extern int sha1sum_main(int argc, char **argv)
  165. {
  166. return(hash_files(argc, argv, HASH_SHA1));
  167. }
  168. #endif