annotate crunchfuck.c @ 4851:e551b31f713f

<mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
author HackBot
date Fri, 12 Sep 2014 14:05:13 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4851
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
1 /*
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
2 Copyright (c) 2012, Roman Muentener, fmnssun _at_ gmail [dot] com
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
3
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
4 Permission to use, copy, modify, and/or distribute this software for any
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
5 purpose with or without fee is hereby granted, provided that the above
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
6 copyright notice and this permission notice appear in all copies.
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
7
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
8 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
10 AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
11 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
12 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
13 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
14 PERFORMANCE OF THIS SOFTWARE.
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
15 */
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
16
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
17 #include <stdlib.h>
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
18 #include <stdio.h>
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
19 #include <string.h>
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
20 #include <assert.h>
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
21 #include <limits.h>
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
22
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
23 int valid_bf(char* program);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
24 char* next_bf(char* program, int pos);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
25 void interpret(char* program, int* solutions, unsigned long int boundl, unsigned long int boundh);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
26 inline int mod(int a, int b);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
27
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
28 #define PROG_MAX_LEN 40
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
29 #define PROG_MAX_STEPS 5000
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
30
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
31 int main(int argc, char* argv[])
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
32 {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
33 printf(" _ \n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
34 printf(" / `_ _ _ /__/| _ /_\n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
35 printf("/_,//_// //_ / // /_//_ /\\ \n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
36 printf("\n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
37
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
38
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
39 char* program;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
40 int* solutions;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
41
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
42 if(argc != 5) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
43 printf("Usage: %s prog boundl boundh n\n", argv[0]);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
44 printf("\tprog - A start program (example: ++++++++)\n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
45 printf("\tn - How many (next) programs you want to check\n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
46 exit(1);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
47 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
48
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
49 fflush(stdout);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
50
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
51 program = calloc(PROG_MAX_LEN, sizeof(char));
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
52 assert(program != NULL);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
53
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
54 solutions = calloc(265, sizeof(int));
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
55 assert(solutions != NULL);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
56
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
57 strncpy(program, argv[1], PROG_MAX_LEN - 1);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
58
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
59 unsigned long int limit = strtoul(argv[2], NULL, 10), i = 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
60 unsigned long int boundl = strtoul(argv[3], NULL, 10);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
61 unsigned long int boundh = strtoul(argv[4], NULL, 10);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
62 assert(limit > 0);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
63
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
64
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
65 //printf("Reading a list of already known solutions for...\n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
66 /*while(1) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
67 int d;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
68 if(scanf("%d",&d) == 1) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
69 solutions[d] = 1;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
70 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
71 else break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
72 }*/
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
73 //printf("Done reading...\n");
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
74
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
75
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
76 for(;i < limit;i++) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
77 if(valid_bf(program))
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
78 interpret(program, solutions, boundl, boundh);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
79 program = next_bf(program, 0);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
80 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
81
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
82 printf("Last program: %s\n", program);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
83 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
84
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
85 inline int mod(int a, int b) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
86 if(a > b)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
87 return a - b;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
88 if(a < 0)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
89 return a + b;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
90 return a;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
91 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
92
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
93 void interpret(char* program, int* solutions, unsigned long int boundl, unsigned long int boundh)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
94 {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
95 unsigned char memory[256] = {0};
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
96 short iptr = 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
97 unsigned char cptr = 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
98 short steps = 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
99 short len = strlen(program);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
100
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
101 while(iptr < len) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
102 if(steps > PROG_MAX_STEPS) return;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
103 steps++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
104
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
105 //printf("%d %c (iptr := %d, cptr := %d)\n",steps, program[iptr], iptr, cptr);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
106
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
107 switch(program[iptr]) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
108 case '+':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
109 memory[cptr]++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
110 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
111 case '-':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
112 memory[cptr]--;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
113 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
114 case '>':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
115 cptr++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
116 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
117 case '<':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
118 cptr--;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
119 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
120 case '[':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
121 if(memory[cptr] == 0) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
122 int lvl = 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
123 iptr++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
124 while(iptr < len) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
125 if(program[iptr] == ']')
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
126 if(lvl < 1) break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
127 else lvl--;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
128 if(program[iptr] == '[')
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
129 lvl++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
130 iptr++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
131 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
132 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
133 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
134 case ']':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
135 if(memory[cptr] != 0) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
136 int lvl = 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
137 iptr--;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
138 while(iptr >= 0) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
139 if(program[iptr] == '[')
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
140 if(lvl < 1) break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
141 else lvl--;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
142 if(program[iptr] == ']')
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
143 lvl++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
144 iptr--;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
145 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
146 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
147 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
148 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
149
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
150 iptr++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
151 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
152
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
153 if(!solutions[memory[cptr]]) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
154 if(memory[cptr] > boundl && memory[cptr] < boundh)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
155 printf("Result: %u -> %s\n", memory[cptr], program);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
156 fflush(stdout);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
157 solutions[memory[cptr]] = 1;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
158 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
159 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
160
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
161 char* next_bf(char* program, int pos)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
162 {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
163 if(pos >= PROG_MAX_LEN) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
164 fputs("Presumed maximum length exceeded!\n", stderr);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
165 exit(1);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
166 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
167
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
168 /* Next instruction */
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
169 switch(program[pos]) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
170 case '\0':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
171 program[pos] = '+';
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
172 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
173 case '+':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
174 program[pos] = '-';
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
175 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
176 case '-':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
177 program[pos] = '>';
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
178 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
179 case '>':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
180 program[pos] = '<';
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
181 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
182 case '<':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
183 program[pos] = '[';
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
184 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
185 case '[':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
186 program[pos] = ']';
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
187 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
188 case ']':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
189 program[pos] = '+';
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
190 return next_bf(program, pos+1);
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
191 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
192
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
193 return program;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
194 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
195
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
196 int valid_bf(char* program)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
197 {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
198 int level = 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
199 while(*program) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
200 char c = *program;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
201 switch(c) {
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
202 case '[':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
203 level++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
204 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
205 case ']':
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
206 level--;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
207 break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
208 default: break;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
209 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
210
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
211 if(level < 0)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
212 return 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
213
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
214 program++;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
215 }
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
216
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
217 if(level != 0)
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
218 return 0;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
219
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
220 return 1;
e551b31f713f <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
221 }