cyclic.c (1905B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define VERSION "v0.1" 6 7 #define LOOPS 4 8 9 int max = 10; 10 int word[LOOPS]; 11 int word_count = 0; 12 char strword[5]; 13 14 static int * 15 str2word(char *str) 16 { 17 int len = strlen(str); 18 for (int i=0; i<len; i++) { 19 word[i] = str[i]; 20 } 21 return word; 22 } 23 24 static int 25 equal(int *a, int *b, size_t s) 26 { 27 for (int i=0; i<s; i++) { 28 if (a[i] != b[i]) 29 return 0; 30 } 31 return 1; 32 } 33 34 static void 35 search(int level, int *indices, int *word) 36 { 37 if (level == LOOPS) { 38 word_count++; 39 if (equal(indices, word, 4) != 1) { 40 return; 41 } 42 printf("[words] %d (%d with \"%s\")\n", word_count-1, 43 word_count, strword); 44 printf("[bytes] %d (%d with \"%s\")\n", 4*word_count-LOOPS, 45 LOOPS*word_count, strword); 46 exit(0); 47 } 48 49 for (int i = 'a'; i < 'a'+26; ++i) { 50 indices[level] = i; 51 search(level + 1, indices, word); 52 } 53 } 54 55 static void 56 gen(int level, int *indices) 57 { 58 if (level == LOOPS) { 59 word_count++; 60 for (int i = 0; i < LOOPS; ++i) { 61 printf("%c", indices[i]); 62 } 63 64 if (word_count >= max) { 65 printf("\n"); 66 exit(0); 67 } 68 } else { 69 for (int i = 'a'; i < 'a'+26; ++i) { 70 indices[level] = i; 71 gen(level + 1, indices); 72 } 73 } 74 } 75 76 int 77 main(int argc, char **argv) 78 { 79 int indices[LOOPS]; 80 81 if (argc == 2) { 82 if (strncmp(argv[1],"-v", 2) == 0) { 83 printf("hextools-cyclic-%s\n", VERSION); 84 return 0; 85 } else if (strncmp(argv[1],"-h", 2) == 0) { 86 printf("hextools-cyclic %s\n" 87 "by Michael Constantine Dimopoulos\n" 88 "https://mcdim.xyz <mk@mcdim.xyz>\n" 89 "\nusage: %s 256" 90 "\n %s -s abcd\n", 91 VERSION, argv[0], argv[0]); 92 return 0; 93 } else { 94 max = atoi(argv[1]); 95 } 96 } 97 98 if (argc == 3 && strncmp(argv[1],"-s", 2) == 0) { 99 strncpy(strword, argv[2], 5); 100 int *word = str2word(argv[2]); 101 search(0, indices, word); 102 return 0; 103 } 104 105 gen(0, indices); 106 printf("\n"); 107 108 return 0; 109 }