rehash.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2003, Dominik Reichl <dominik.reichl@t-online.de>, Germany.
  4. All rights reserved.
  5. Distributed under the terms of the GNU General Public License v2.
  6. This software is provided 'as is' with no explicit or implied warranties
  7. in respect of its properties, including, but not limited to, correctness
  8. and/or fitness for purpose.
  9. ---------------------------------------------------------------------------
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <memory.h>
  14. #include <string.h>
  15. #include "rehash.h"
  16. #include "clutil.h"
  17. #include "hashmgr.h"
  18. int main(int argc, char *argv[])
  19. {
  20. int i = 0;
  21. bool bOnce = true;
  22. char szPath[RH_MAX_PATH];
  23. char szAbsPath[RH_MAX_PATH];
  24. char szOrgPath[RH_MAX_PATH];
  25. char szTemp[RH_MAX_BUFFER];
  26. int iErrorCode = 0;
  27. CHashManager hashmgr;
  28. getcwd(szOrgPath, RH_MAX_PATH);
  29. // No arguments?
  30. if(argc == 1)
  31. {
  32. printInfo();
  33. return(RH_NO_ARGS);
  34. }
  35. memset(szPath, 0, RH_MAX_PATH);
  36. bOnce = true;
  37. hashmgr.SelectAllAlgorithms(true);
  38. for(i = 1; i < argc; i++)
  39. {
  40. // Is the argument a path descriptor?
  41. if(isArgument(argv[i]) == false)
  42. {
  43. if(bOnce == false) strcat(szPath, " ");
  44. strcat(szPath, argv[i]);
  45. bOnce = false; // Next time add a space
  46. continue;
  47. }
  48. // The argument is an optional argument
  49. if(strlen(argv[i]) >= RH_MAX_BUFFER) continue; // Non-parsable option argument, ignore
  50. fmtArgument(argv[i], szTemp); // Format the argument, i.e. remove all special chars
  51. if(strcmp(szTemp, "help" ) == 0) printInfo();
  52. if(strcmp(szTemp, "h" ) == 0) printInfo();
  53. if(strcmp(szTemp, "?" ) == 0) printInfo();
  54. if(strcmp(szTemp, "version" ) == 0) printInfo();
  55. if(strcmp(szTemp, "v" ) == 0) printInfo();
  56. if(strcmp(szTemp, "fullpath") == 0) hashmgr.SetOption(OPT_FULLPATH, true);
  57. if(strcmp(szTemp, "f" ) == 0) hashmgr.SetOption(OPT_FULLPATH, true);
  58. if(strcmp(szTemp, "nopath" ) == 0) hashmgr.SetOption(OPT_FULLPATH, false);
  59. if(strcmp(szTemp, "rcrsv" ) == 0) hashmgr.SetOption(OPT_RECURSIVE, true);
  60. if(strcmp(szTemp, "norcrsv" ) == 0) hashmgr.SetOption(OPT_RECURSIVE, false);
  61. if(strcmp(szTemp, "recur" ) == 0) hashmgr.SetOption(OPT_RECURSIVE, true);
  62. if(strcmp(szTemp, "norecur" ) == 0) hashmgr.SetOption(OPT_RECURSIVE, false);
  63. if(strcmp(szTemp, "r" ) == 0) hashmgr.SetOption(OPT_RECURSIVE, true);
  64. if(strcmp(szTemp, "all" ) == 0) hashmgr.SelectAllAlgorithms(true);
  65. if(strcmp(szTemp, "a" ) == 0) hashmgr.SelectAllAlgorithms(true);
  66. if(strcmp(szTemp, "none" ) == 0) hashmgr.SelectAllAlgorithms(false);
  67. if(strcmp(szTemp, "n" ) == 0) hashmgr.SelectAllAlgorithms(false);
  68. if(strcmp(szTemp, "crc16" ) == 0) hashmgr.SelectAlgorithm(HASHID_CRC16, true);
  69. if(strcmp(szTemp, "nocrc16" ) == 0) hashmgr.SelectAlgorithm(HASHID_CRC16, false);
  70. if(strcmp(szTemp, "crc16c" ) == 0) hashmgr.SelectAlgorithm(HASHID_CRC16CCITT, true);
  71. if(strcmp(szTemp, "nocrc16c") == 0) hashmgr.SelectAlgorithm(HASHID_CRC16CCITT, false);
  72. if(strcmp(szTemp, "crc32" ) == 0) hashmgr.SelectAlgorithm(HASHID_CRC32, true);
  73. if(strcmp(szTemp, "nocrc32" ) == 0) hashmgr.SelectAlgorithm(HASHID_CRC32, false);
  74. if(strcmp(szTemp, "fcs16" ) == 0) hashmgr.SelectAlgorithm(HASHID_FCS_16, true);
  75. if(strcmp(szTemp, "nofcs16" ) == 0) hashmgr.SelectAlgorithm(HASHID_FCS_16, false);
  76. if(strcmp(szTemp, "fcs32" ) == 0) hashmgr.SelectAlgorithm(HASHID_FCS_32, true);
  77. if(strcmp(szTemp, "nofcs32" ) == 0) hashmgr.SelectAlgorithm(HASHID_FCS_32, false);
  78. if(strcmp(szTemp, "ghash3" ) == 0) hashmgr.SelectAlgorithm(HASHID_GHASH_32_3, true);
  79. if(strcmp(szTemp, "noghash3") == 0) hashmgr.SelectAlgorithm(HASHID_GHASH_32_3, false);
  80. if(strcmp(szTemp, "ghash5" ) == 0) hashmgr.SelectAlgorithm(HASHID_GHASH_32_5, true);
  81. if(strcmp(szTemp, "noghash5") == 0) hashmgr.SelectAlgorithm(HASHID_GHASH_32_5, false);
  82. if(strcmp(szTemp, "gost" ) == 0) hashmgr.SelectAlgorithm(HASHID_GOST, true);
  83. if(strcmp(szTemp, "nogost" ) == 0) hashmgr.SelectAlgorithm(HASHID_GOST, false);
  84. if(strcmp(szTemp, "haval" ) == 0) hashmgr.SelectAlgorithm(HASHID_HAVAL, true);
  85. if(strcmp(szTemp, "nohaval" ) == 0) hashmgr.SelectAlgorithm(HASHID_HAVAL, false);
  86. if(strcmp(szTemp, "md2" ) == 0) hashmgr.SelectAlgorithm(HASHID_MD2, true);
  87. if(strcmp(szTemp, "nomd2" ) == 0) hashmgr.SelectAlgorithm(HASHID_MD2, false);
  88. if(strcmp(szTemp, "md4" ) == 0) hashmgr.SelectAlgorithm(HASHID_MD4, true);
  89. if(strcmp(szTemp, "nomd4" ) == 0) hashmgr.SelectAlgorithm(HASHID_MD4, false);
  90. if(strcmp(szTemp, "md5" ) == 0) hashmgr.SelectAlgorithm(HASHID_MD5, true);
  91. if(strcmp(szTemp, "nomd5" ) == 0) hashmgr.SelectAlgorithm(HASHID_MD5, false);
  92. if(strcmp(szTemp, "sha1" ) == 0) hashmgr.SelectAlgorithm(HASHID_SHA1 , true);
  93. if(strcmp(szTemp, "nosha1" ) == 0) hashmgr.SelectAlgorithm(HASHID_SHA1 , false);
  94. if(strcmp(szTemp, "sha256" ) == 0) hashmgr.SelectAlgorithm(HASHID_SHA2_256, true);
  95. if(strcmp(szTemp, "nosha256") == 0) hashmgr.SelectAlgorithm(HASHID_SHA2_256, false);
  96. if(strcmp(szTemp, "sha384" ) == 0) hashmgr.SelectAlgorithm(HASHID_SHA2_384, true);
  97. if(strcmp(szTemp, "nosha384") == 0) hashmgr.SelectAlgorithm(HASHID_SHA2_384, false);
  98. if(strcmp(szTemp, "sha512" ) == 0) hashmgr.SelectAlgorithm(HASHID_SHA2_512, true);
  99. if(strcmp(szTemp, "nosha512") == 0) hashmgr.SelectAlgorithm(HASHID_SHA2_512, false);
  100. if(strcmp(szTemp, "size32" ) == 0) hashmgr.SelectAlgorithm(HASHID_SIZE_32, true);
  101. if(strcmp(szTemp, "nosize32") == 0) hashmgr.SelectAlgorithm(HASHID_SIZE_32, false);
  102. if(strcmp(szTemp, "tiger" ) == 0) hashmgr.SelectAlgorithm(HASHID_TIGER, true);
  103. if(strcmp(szTemp, "notiger" ) == 0) hashmgr.SelectAlgorithm(HASHID_TIGER, false);
  104. }
  105. // Check if a path is in the arguments
  106. bOnce = false;
  107. for(i = 1; i < argc; i++) if(isArgument(argv[i]) == 0) bOnce = true;
  108. if(bOnce == false) return RH_NO_PATH; // Silently exit
  109. fmtPath(szPath);
  110. strcpy(szAbsPath, szPath);
  111. pathonly(szAbsPath);
  112. if(strlen(szAbsPath) != 0)
  113. {
  114. if(chdir(szAbsPath) != 0)
  115. {
  116. printf("ERROR! Cannot change to directory: ");
  117. printf(szAbsPath);
  118. printf(CPS_NEWLINE);
  119. return RH_DIRECTORY_ERROR;
  120. }
  121. }
  122. fileonly(szPath);
  123. iErrorCode = hashmgr.HashPath(szAbsPath, szPath);
  124. if(chdir(szOrgPath) != 0) return RH_DIRECTORY_ERROR;
  125. return iErrorCode;
  126. }
  127. void printInfo()
  128. {
  129. printf("ReichlSoft Hash Calculator");
  130. printf(CPS_NEWLINE);
  131. printf("Version: ");
  132. printf(SZ_REHASH_VERSION);
  133. printf(", Build: ");
  134. printf(__DATE__);
  135. printf(" ");
  136. printf(__TIME__);
  137. if(RH_TARGET_SYSTEM == RH_TARGET_SYSTEM_WINDOWS)
  138. printf(" - Windows");
  139. else if(RH_TARGET_SYSTEM == RH_TARGET_SYSTEM_LINUX)
  140. printf(" - Linux");
  141. printf(CPS_NEWLINE); printf(CPS_NEWLINE);
  142. printf("Free console-based hash calculator.");
  143. printf(CPS_NEWLINE); printf(CPS_NEWLINE);
  144. printf("Download latest version from ");
  145. printf(SZ_REHASH_HOMEPAGE);
  146. printf(".");
  147. printf(CPS_NEWLINE);
  148. }