Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <signal.h>
  7.  
  8. // Configurations
  9. #define MAX_LEN 100
  10.  
  11. // Global variables
  12. pthread_t *threads;
  13. unsigned int nbThreads;
  14. pthread_mutex_t nom_mutex = PTHREAD_MUTEX_INITIALIZER;
  15.  
  16. const char *alphabet[63] =
  17. {
  18. "a", "b", "c", "d", "e", "f", "g",
  19. "h", "i", "j", "k", "l", "m", "n",
  20. "o", "p", "q", "r", "s", "t", "u",
  21. "v", "w", "x", "y", "z", "A", "B",
  22. "C", "D", "E", "F", "G", "H", "I",
  23. "J", "K", "L", "M", "N", "O", "P",
  24. "Q", "R", "S", "T", "U", "V", "W",
  25. "X", "Y", "Z", "1", "2", "3", "4",
  26. "5", "6", "7", "8", "9", "0", " "
  27. };
  28.  
  29.  
  30.  
  31. void signal_handler(int sig) {
  32. printf("aleeeeeeerte !!!\n");
  33. free(threads);
  34. exit(0);
  35. }
  36.  
  37.  
  38. void bruteImpl(int start_origin, unsigned int start, char* str, int index, int maxDepth)
  39. {
  40. if (start_origin == start) {
  41. str[index] = *alphabet[start];
  42.  
  43. if (strcmp(str, "teste") == 0) {
  44. printf("Trouvé teste !\n");
  45. exit(0);
  46. }
  47.  
  48. if (index != maxDepth - 1)
  49. bruteImpl(-1, 0, str, index + 1, maxDepth);
  50. } else {
  51. for (int i = start; i < 63; ++i)
  52. {
  53. str[index] = *alphabet[i];
  54.  
  55. if (strcmp(str, "teste") == 0) {
  56. printf("Trouvé teste !\n");
  57. exit(0);
  58. }
  59.  
  60. if (index != (maxDepth - 1))
  61. bruteImpl(-1, 0, str, index + 1, maxDepth);
  62. }
  63. }
  64. }
  65.  
  66. void bruteSequential(int start)
  67. {
  68. int save_start = start;
  69.  
  70. for (int i = 1; i <= MAX_LEN; i++) {
  71. while (start < 63) {
  72. char* buf = malloc(i+ 1);
  73.  
  74. for (int j = 1; j <= i; ++j)
  75. {
  76. memset(buf, 0, i + 1);
  77. bruteImpl(start, start, buf, 0, j);
  78. }
  79.  
  80. free(buf);
  81. start += nbThreads;
  82. }
  83.  
  84. start = save_start;
  85. }
  86. }
  87.  
  88.  
  89.  
  90. void start_routine(int *start) {
  91. bruteSequential(*start);
  92.  
  93. free(start);
  94. }
  95.  
  96. void start_routine2(char *string) {
  97. raise(SIGUSR1);
  98. }
  99.  
  100.  
  101. int main(int argc, char **argv) {
  102. if (argc < 3) {
  103. printf("Error : Too few arguments given !\n");
  104. return 1;
  105. }
  106.  
  107. if (argc > 3) {
  108. printf("Error : Too much arguments given !\n");
  109. return 1;
  110. }
  111.  
  112. if (atoi(argv[1]) == 0) {
  113. printf("The first argument \"%s\"is not valid (not integer)\n", argv[1]);
  114. return 1;
  115. }
  116.  
  117. nbThreads = atoi(argv[1]);
  118.  
  119. signal(SIGUSR1, signal_handler);
  120.  
  121. threads = (pthread_t*) malloc(sizeof(pthread_t) * nbThreads);
  122.  
  123. for (int i = 0; i < nbThreads; i++) {
  124. int *start = (int*) malloc(sizeof(int));
  125. *start = i;
  126. pthread_create(&threads[i], NULL, (void * (*) (void *)) start_routine, start);
  127. }
  128.  
  129. for (unsigned int j = 0; j < nbThreads; j++) {
  130. pthread_join(threads[j], NULL);
  131. }
  132.  
  133.  
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement