basefiles.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. struct option
  2. {
  3. int proc = 0;
  4. int mode = 0;
  5. int log = 0;
  6. int high = 0;
  7. std::string str_search;
  8. std::string rgx_search;
  9. std::string outputfile;
  10. };
  11. option conf;
  12. int config()
  13. {
  14. std::ifstream conffile ("sygcpp.conf");
  15. if(!conffile) // проверка наличия конфига
  16. {
  17. std::cout << " Configuration file not found... ";
  18. conffile.close();
  19. std::ofstream newconf ("sygcpp.conf"); // создание конфига
  20. if(!newconf)
  21. {
  22. std::cerr << "CREATION FAILED" << std::endl;
  23. return -1;
  24. }
  25. newconf << "####################################################################\n"
  26. << "# SimpleYggGen C++ configuration file. #\n"
  27. << "# If you have some errors, try delete this file and run SYG again. #\n"
  28. << "####################################################################\n\n"
  29. << "* Count of thread: 1\n\n"
  30. << " 0 - by pattern, 1 - high address, 2 - search by pattern & high,\n"
  31. << " 3 - regexp, 4 - search by regexp & high.\n"
  32. << "* Mining option: 1\n\n"
  33. << " 0 - console output only, 1 - log to file.\n"
  34. << "* Logging mode: 1\n\n"
  35. << " High address search. Parameter is set in hexadecimal (0-9, a-f).\n"
  36. << "* Start position (2xx): 14\n\n"
  37. << " Used when \"Mining mode\" set as 0 or 2.\n"
  38. << "* Pattern: ::\n\n"
  39. << " Used when \"Mining mode\" set as 3 or 4. Extended grep type.\n"
  40. << "* Regexp: ^2.*.(1:ace|ace:1)$";
  41. newconf.close();
  42. std::ifstream conffile ("sygcpp.conf");
  43. if(conffile)
  44. std::cout << "CREATED" << std::endl;
  45. config();
  46. return 0;
  47. } else { // чтение конфигурации
  48. std::string str_temp_read;
  49. std::string str_read;
  50. while(getline(conffile, str_temp_read))
  51. str_read += str_temp_read;
  52. conffile.close();
  53. struct check
  54. {
  55. bool proc = false;
  56. bool mode = false;
  57. bool log = false;
  58. bool high = false;
  59. bool str_search = false;
  60. bool rgx_search = false;
  61. bool ok()
  62. {
  63. if(proc & mode & log & high & str_search & rgx_search)
  64. return true;
  65. return false;
  66. }
  67. };
  68. check complete;
  69. std::istringstream ss_input(str_read); // чтение конфига
  70. while(!ss_input.eof())
  71. {
  72. ss_input >> str_temp_read;
  73. if(str_temp_read == "thread:") // поиск параметра по предыдущему слову
  74. {
  75. ss_input >> conf.proc; // запись в соответствующую переменную
  76. if(ss_input.fail())
  77. {
  78. std::cerr << " Count of thread value incorrect." << std::endl;
  79. return -2;
  80. }
  81. complete.proc = true;
  82. }
  83. if(str_temp_read == "option:")
  84. {
  85. ss_input >> conf.mode;
  86. if(ss_input.fail() || (conf.mode > 4 || conf.mode < 0))
  87. {
  88. std::cerr << " Mining option value incorrect." << std::endl;
  89. return -3;
  90. }
  91. complete.mode = true;
  92. }
  93. if(str_temp_read == "mode:")
  94. {
  95. ss_input >> conf.log;
  96. if(ss_input.fail() || (conf.log != 0 && conf.log != 1))
  97. {
  98. std::cerr << " Logging mode value incorrect." << std::endl;
  99. return -4;
  100. }
  101. complete.log = true;
  102. }
  103. if(str_temp_read == "(2xx):")
  104. {
  105. ss_input >> std::hex >> conf.high;
  106. if(ss_input.fail())
  107. {
  108. std::cerr << " Start position value incorrect." << std::endl;
  109. return -5;
  110. }
  111. complete.high = true;
  112. }
  113. if(str_temp_read == "Pattern:")
  114. {
  115. ss_input >> conf.str_search;
  116. if(ss_input.fail())
  117. {
  118. std::cerr << " Pattern value incorrect." << std::endl;
  119. return -6;
  120. }
  121. complete.str_search = true;
  122. }
  123. if(str_temp_read == "Regexp:")
  124. {
  125. ss_input >> conf.rgx_search;
  126. if(ss_input.fail())
  127. {
  128. std::cerr << " Regexp value incorrect." << std::endl;
  129. return -7;
  130. }
  131. complete.rgx_search = true;
  132. }
  133. }
  134. if(!complete.ok())
  135. {
  136. std::cerr << " Corrupted configuration file. Some parameters not found." << std::endl;
  137. return -8;
  138. }
  139. unsigned int processor_count = std::thread::hardware_concurrency(); // кол-во процессоров
  140. if (conf.proc > (int)processor_count)
  141. conf.proc = (int)processor_count;
  142. countsize = 800 << __bsrq(conf.proc);
  143. }
  144. return 0;
  145. }
  146. void DisplayConfig()
  147. {
  148. // вывод конфигурации на экран
  149. std::cout << " Threads: " << conf.proc << ", ";
  150. if(conf.mode == 0)
  151. std::cout << "search by pattern (" << conf.str_search << "), ";
  152. else if(conf.mode == 1)
  153. std::cout << "search high addresses (2" << std::setw(2) << std::setfill('0') <<
  154. std::hex << conf.high << std::dec << "+), ";
  155. else if(conf.mode == 2)
  156. std::cout << "search by pattern & high (" << conf.str_search << " & 2" <<
  157. std::setw(2) << std::setfill('0') << std::hex << conf.high << std::dec <<"+), ";
  158. else if(conf.mode == 3)
  159. std::cout << "search by regexp (" << conf.rgx_search << "), ";
  160. else if(conf.mode == 4)
  161. std::cout << "search by regexp & high (" << conf.rgx_search << " & 2" <<
  162. std::setw(2) << std::setfill('0') << std::hex << conf.high << std::dec << "+), ";
  163. if(conf.log)
  164. std::cout << "logging to text file.";
  165. else
  166. std::cout << "console log only.";
  167. std::cout << std::endl << std::endl;
  168. }
  169. void testoutput()
  170. {
  171. if(conf.log) // проверка включено ли логирование
  172. {
  173. if(conf.mode == 0)
  174. conf.outputfile = "syg-pattern.txt";
  175. else if(conf.mode == 1)
  176. conf.outputfile = "syg-high.txt";
  177. else if(conf.mode == 2)
  178. conf.outputfile = "syg-pattern-high.txt";
  179. else if(conf.mode == 3)
  180. conf.outputfile = "syg-regexp.txt";
  181. else if(conf.mode == 4)
  182. conf.outputfile = "syg-regexp-high.txt";
  183. std::ifstream test(conf.outputfile);
  184. if(!test) // проверка наличия выходного файла
  185. {
  186. test.close();
  187. std::ofstream output(conf.outputfile);
  188. output << "**************************************************************************\n"
  189. << "Change EncryptionPublicKey and EncryptionPrivateKey to your yggdrasil.conf\n"
  190. << "Windows: C:\\ProgramData\\Yggdrasil\\yggdrasil.conf\n"
  191. << "Debian: /etc/yggdrasil.conf\n"
  192. << "**************************************************************************\n";
  193. output.close();
  194. } else test.close();
  195. }
  196. }