├── 32c3-presentation.pdf ├── BalCCon-presentation.pdf ├── README.md ├── bfprogs ├── 99bottles.bf ├── README.md ├── demo.bf ├── fib.bf ├── hello.bf ├── life.bf ├── mandelbrot.bf └── sierpinski.bf └── src ├── Makefile ├── pbf_pre.c └── toker.py /32c3-presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HexHive/printbf/162943a568beae443b2dac843e043dfa2a2d57d2/32c3-presentation.pdf -------------------------------------------------------------------------------- /BalCCon-presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HexHive/printbf/162943a568beae443b2dac843e043dfa2a2d57d2/BalCCon-presentation.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # printbf -- Brainfuck interpreter in printf 2 | 3 | ## Authors 4 | 5 | * [Mathias Payer](http://nebelwelt.net) 6 | * [Nicholas Carlini](http://nicholas.carlini.com) 7 | 8 | 9 | ## Background 10 | 11 | Generic POSIX printf itself can be Turing complete as shown in [Control-Flow 12 | Bending](http://nebelwelt.net/publications/#15SEC). Here we take 13 | printf-oriented programming one step further and preset a brainfuck 14 | interpreter inside a single printf statement. 15 | 16 | An attacker can control a printf statement through a format string 17 | vulnerability (where an attacker-controlled string is used as first parameter 18 | to a printf-like statement) or if the attacker can control the first argument 19 | to a printf statement through, e.g., a generic memory corruption. See the 20 | disclaimer below for practical *in the wild* considerations. 21 | 22 | Brainfuck is a Turing-complete language that has the following commands (and 23 | their mapping to format strings): 24 | 25 | * `>` == dataptr++ (`%1$.*1$d %2$hn`) 26 | * `<` == dataptr-- (`%1$65535d%1$.*1$d%2$hn`) 27 | * `+` == (\*dataptr)++ (`%3$.*3$d %4$hhn`) 28 | * `-` == (\*dataptr)-- (`%3$255d%3$.*3$d%4$hhn` -- plus check for ovfl) 29 | * `.` == putchar(\*dataptr) (`%3$.*3$d%5$hn`) 30 | * `,` == getchar(dataptr) (`%13$.*13$d%4$hn`) 31 | * `[` == if (\*dataptr == 0) goto `]` (`%1$.*1$d%10$.*10$d%2$hn`) 32 | * `]` == if (\*dataptr != 0) goto `[` (`%1$.*1$d%10$.*10$d%2$hn`) 33 | 34 | 35 | ## Demo and sources 36 | 37 | Have a look at the bf_pre.c sources to see what is needed to setup the 38 | interpreter and also look at the tokenizer in toker.py. 39 | 40 | Run make in ./src to generate a couple of sample programs (in ./src). 41 | 42 | 43 | ## Disclaimer 44 | 45 | Keep in mind that this printbf interpreter is supposed to be a fun example of 46 | Turing completeness that is available in current programs and not a new 47 | generic attack vector. This demo is NOT intended to be a generic 48 | FORTIFY_SOURCE bypass. 49 | 50 | Current systems often either (i) disable %n (which is used to write to memory 51 | and allowed according to the standard but rarely used in practice) or (ii) 52 | through a set of of patches that test for attack-like conditions, e.g., if 53 | the format string is in writable memory. 54 | 55 | To use printbf in the wild an attacker will either have to disable 56 | FORTIFY_SOURCE checking or get around the checks by placing lining up the 57 | format strings and placing them in readonly memory. The FORTIFY_SOURCE 58 | mitigations are glibc specific. The attacker model for printbf assumes that 59 | the attacker can use memory corruption vulnerabilities to set-up the attack or 60 | that the sources are compiled without enabled FORTIFY_SOURCE defenses. 61 | -------------------------------------------------------------------------------- /bfprogs/99bottles.bf: -------------------------------------------------------------------------------- 1 | 99 Bottles of Beer in Urban Mueller's BrainF*** (The actual 2 | name is impolite) 3 | 4 | by Ben Olmstead 5 | 6 | ANSI C interpreter available on the internet; due to 7 | constraints in comments the address below needs to have the 8 | stuff in parenthesis replaced with the appropriate symbol: 9 | 10 | http://www(dot)cats(dash)eye(dot)com/cet/soft/lang/bf/ 11 | 12 | Believe it or not this language is indeed Turing complete! 13 | Combines the speed of BASIC with the ease of INTERCAL and 14 | the readability of an IOCCC entry! 15 | 16 | >+++++++++[<+++++++++++>-]<[>[-]>[-]<<[>+>+<<-]>>[<<+>>-]>>> 17 | [-]<<<+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+< 18 | -]<<-<-]+++++++++>[<->-]>>+>[<[-]<<+>>>-]>[-]+<<[>+>-<<-]<<< 19 | [>>+>+<<<-]>>>[<<<+>>>-]>[<+>-]<<-[>[-]<[-]]>>+<[>[-]<-]<+++ 20 | +++++[<++++++<++++++>>-]>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>- 21 | ]<<<<<<.>>[-]>[-]++++[<++++++++>-]<.>++++[<++++++++>-]<++.>+ 22 | ++++[<+++++++++>-]<.><+++++..--------.-------.>>[>>+>+<<<-]> 23 | >>[<<<+>>>-]<[<<<<++++++++++++++.>>>>-]<<<<[-]>++++[<+++++++ 24 | +>-]<.>+++++++++[<+++++++++>-]<--.---------.>+++++++[<------ 25 | ---->-]<.>++++++[<+++++++++++>-]<.+++..+++++++++++++.>++++++ 26 | ++[<---------->-]<--.>+++++++++[<+++++++++>-]<--.-.>++++++++ 27 | [<---------->-]<++.>++++++++[<++++++++++>-]<++++.----------- 28 | -.---.>+++++++[<---------->-]<+.>++++++++[<+++++++++++>-]<-. 29 | >++[<----------->-]<.+++++++++++..>+++++++++[<---------->-]< 30 | -----.---.>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>>+++ 31 | +[<++++++>-]<--.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<. 32 | ><+++++..--------.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++ 33 | ++++++++++++.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<++ 34 | +++++++>-]<--.---------.>+++++++[<---------->-]<.>++++++[<++ 35 | +++++++++>-]<.+++..+++++++++++++.>++++++++++[<---------->-]< 36 | -.---.>+++++++[<++++++++++>-]<++++.+++++++++++++.++++++++++. 37 | ------.>+++++++[<---------->-]<+.>++++++++[<++++++++++>-]<-. 38 | -.---------.>+++++++[<---------->-]<+.>+++++++[<++++++++++>- 39 | ]<--.+++++++++++.++++++++.---------.>++++++++[<---------->-] 40 | <++.>+++++[<+++++++++++++>-]<.+++++++++++++.----------.>++++ 41 | +++[<---------->-]<++.>++++++++[<++++++++++>-]<.>+++[<-----> 42 | -]<.>+++[<++++++>-]<..>+++++++++[<--------->-]<--.>+++++++[< 43 | ++++++++++>-]<+++.+++++++++++.>++++++++[<----------->-]<++++ 44 | .>+++++[<+++++++++++++>-]<.>+++[<++++++>-]<-.---.++++++.---- 45 | ---.----------.>++++++++[<----------->-]<+.---.[-]<<<->[-]>[ 46 | -]<<[>+>+<<-]>>[<<+>>-]>>>[-]<<<+++++++++<[>>>+<<[>+>[-]<<-] 47 | >[<+>-]>[<<++++++++++>>>+<-]<<-<-]+++++++++>[<->-]>>+>[<[-]< 48 | <+>>>-]>[-]+<<[>+>-<<-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<>>[<+>-]< 49 | <-[>[-]<[-]]>>+<[>[-]<-]<++++++++[<++++++<++++++>>-]>>>[>+>+ 50 | <<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>[-]>[-]++++[<++++++++> 51 | -]<.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.><+++++..--- 52 | -----.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++++++++++++++ 53 | .>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<+++++++++>-]<- 54 | -.---------.>+++++++[<---------->-]<.>++++++[<+++++++++++>-] 55 | <.+++..+++++++++++++.>++++++++[<---------->-]<--.>+++++++++[ 56 | <+++++++++>-]<--.-.>++++++++[<---------->-]<++.>++++++++[<++ 57 | ++++++++>-]<++++.------------.---.>+++++++[<---------->-]<+. 58 | >++++++++[<+++++++++++>-]<-.>++[<----------->-]<.+++++++++++ 59 | ..>+++++++++[<---------->-]<-----.---.+++.---.[-]<<<] 60 | -------------------------------------------------------------------------------- /bfprogs/README.md: -------------------------------------------------------------------------------- 1 | # Some brainfuck sample programs. 2 | 3 | These programs are not my own but were released as open-source. The 4 | respective owerns are named in the source files. 5 | 6 | * life.bf: Game of life -- Linus Akesson 7 | * fib.bf: Fibonacci -- Daniel Cristofani 8 | * hello.bf: Hello World -- from Wikipedia page 9 | * sierpinski.bf: Print a sierpinski triangle 10 | -------------------------------------------------------------------------------- /bfprogs/demo.bf: -------------------------------------------------------------------------------- 1 | [ bfdemo made by SirDayBat (ircnet, quakenet, nuutti.holtta@luukku.com) ] 2 | 3 | ,>++++++++[<-------->-]<->>>+++++++[<++++++++++>-]++++++++>++++++++<[>>>++++[<+++++>-]<-[>++++++++++[>++++++++++>+++<<-]>>++<[>.<-]>[-]<<+++++++++++++.---.[-]<-]+++[>>++++>+<<+++++++++<<[>>-<<-]>>[<<+>>-]<<[>>+>-[>]>[<++++>>+++<[>-<-]>[<+>-]>]<<<<<<-]+++++++++>>[<<->>-]>>>++++++++++[>>+++<<<<<++++++++++>>>-]>>-<<<<<[>-[>]>[<++++>>+++<[>-<-]>[<+>-]>]<<[>+>>+++<<<-]>>>.<<[<+>>>---<<-]<<<-]+++++++++++++.---.[-]>>>>>[-]<<<[-]<[-]<<-]<<<<++[>>>>+++++++++<<[>>-<<-]>>[<<+>>-]<<[>>+>>+++++[>+>+>+>+>+++>+++++>+>++>+++>+>+++>+>+++>+++>+++>++>+++++>+++>++++>+>+>+>+>+[<]>-]+>---->---->---->--->++>->---->+++>++>---->+++>->++>->++>+>->++>++>---->---->---->---->----[<]<<<-[>>+>>++++[>++++>>++>++>-->---->++++>-->->>>>->+>-->>->--->>+++>++>+>>[<]>-]+>>>+++>>+>>+++>-->+>+++>+++>->->>>-->+>->>+>->++>>[<]<<<-[>>+>>++++[>->>+>-->>+++>--->+>+>>--->++>-->>->->>++>->-->++>+>+++>[<]>-]+>--->+++>++>+>+++>>-->+++>++>--->--->+>>++>->-->+>>-->>+>++>+>[<]<<<-[>>+>>++++[>-->>---->>->--->+++>>--->+++>>->+>-->++>++++>-->++>>->---->-->--->[<]>-]+>-->--->>-->>->++>+++>---->>-->>++>->->->>+>->->>---->->[<]<<<-[>>+>>++++[>>+++>>+>>+>--->++>++>+>++>->+>>>--->-->>-->++>+++>>>[<]>-]+>>>>+++>++>++>+>->>+>+++>--->+>++>-->+>+>>--->++>+>>>[<]<<<-[>>+>>++++[>>--->>+>>>>->>---->+>+>-->-->>>>--->++>+>--->>>[<]>-]+>>>>+++>+++>-->--->->+>->>++>>->+++>+>->+>>->->>>[<]<<<-[>>+>>++++[>+>>>+>-->->+>-->++>++++>--->++>+>+>+>->+++>>->--->>++++>+>++++[<]>-]++++++++++++++>++>+++>+++>+>>>->---->->+>+>+++>++>+++>+>-->+++>>-->->+>>+++>-[<]<<<-[>>+>>---------[>++>>+>---->>++>->++>>---->>>++>->+>++>>->+++>+++>++>--->+>--[<]>-]++++++++>+>--->->+>++>++>--->>>>-->+++>++>--->->>+++>-->+++>+>++>--->+>+[<]<<<-]]]]]]]]+++++++++>>[<<->>-]>>[>]<[[[>>>>>>>>>>>>+<<<<<<<<<<<<-]<]>>>>>>>>>>>>>[<+>-]>[>]<]<[[>+>+>+>+>+>+>+>+>+<<<<<<<<<-[>>+>>>+>>>+<<<<<<<<-[>>+>>>-<<<<<-[>>-->>>>++<<<<<<-[>>>>+>>-<<<<<<-[>>>>>>>+>>+<<<<<<<<<-[>>>>->>->>+<<<<<<<<-[>>>>>++<<<<<-[>>>>>>>>>-<<<<<<<<<-[>>>>>>>>->+<<<<<<<<<-[>>>>>>>>-<<<<<<<<-[>+>>>+<<<<-[>>>>>>>>++<<<<<<<<-[>->++>>>>>-<<<<<<<-[>>>>>>+<<<<<<-[>>>>>>>+<<<<<<<-[>>>>>>>>>-<<<<<<<<<-[>>>>>>>>--<<<<<<<<-[>>>>>>-<<<<<<-[>+>>+>>>>>>+<<<<<<<<<-[>->>->->>+>>++<<<<<<<<-[>>-->+<<<-[>>>->+>>>->->-<<<<<<<<<-[>>>>->->+>>>++<<<<<<<<<-]]]]]]]]]]]]]]]]]]]]]]]]<<<<<<<<<<<<<]>>>>>>>>>>>>>>[[<<<+++<+>[>>>[<<+++++[>++++++<-]>++>-[<<+++++++++[>++++++++++<-]>++>-[<<+++++[>------<-]>+>-]]]<.[-]<<[>+<-]>-]<+++++[>++++++<-]>++.[-]>>>>>>>>>>>>>]+++++++++++++.---.[-]<<<<<<<<<<[<<<<<<<<<<<<<]>>>>>>>>>>>>>]<<<<<<<<<<<<<<<<<<<<[[-<<<]<<<<]<<<<<-<<-]>>++>>+++[>>++++>+<<<<[>>+>-[>]>[<++++>>+++<[>-<-]>[<+>-]>]<<<<<<-]>>[<<+>>-]>>>++++++++++[>>+++<<<<<++++++++++>>>-]>>-<<<<<[>-[>]>[<++++>>+++<[>-<-]>[<+>-]>]<<[>+>>+++<<<-]>>>.<<[<+>>>---<<-]<<<-]+++++++++++++.---.[-]>>>>>[-]<<<[-]<[-]<<-]>++++[<+++++>-]<-[>++++++++++[>++++++++++>+++<<-]>>++<[>.<-]>[-]<<+++++++++++++.---.[-]<-]<<<<<[>>>>>+++>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<[>>>++++++[<++++++[<+++++++>-]>-]<<+++[>+>+<<-]>[>[<<+>>-]<<[>>+<<-]>-]>[-]<<<-]<<<-[>]>[-->>+++++++[<<<++++++++++>>>-]>]<+<-[>]>[<++++++++>>>]<<-<<]>[-]<<[-]++++++++++[<++++++++++>-]+<[>>>+++++[<++++++++++>-]<[>+<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]++++++++++[<++++++++++++++++++++>-]<<[>>+>+<<<-]>>[<<+>>-]>>>++++++++++>>+<<<<<<[>>[>>-[<]<<[>>>>-[<]<<<<[<<<[>>+>>>>>+<<<<<<<-]>>[<<+>>-]]>>>++++++++++<<<]>-]<<<[>>+>+<<<-]>>[<<+>>-]<-]>>[-]>>[-]>+++++[<++++++++++>-]<[>>[<]<<[<<<<<[>+>>>>>>+<<<<<<<-]>[<+>-]>>>]>>>-<<-]<<<<<[>+>>>>>+<<<<<<-]>[<+>-]>>>>>>[<->-]<<++++++++++[<++++++++++>-]<[>>-[<]<<[>>+>[<->-]<[>+<-]<<<<<<[>+>>>>>+<<<<<<-]>[<+>-]>>]>>>>[>+>+++<<-]++++[>>++++++++<<-]>>.[-]<[<+>-]<<<<-]>>[-]>[-]+++++++++++++.---.[-]<<<<<<<<-]<-[>]>>[<<++++++++++++++++++++>>>]<[-]<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<[>>>++++++[<++++++[<+++++++>-]>-]<<+++[>+>+<<-]>[>[<<+>>-]<<[>>+<<-]>-]>[-]<<<-]<<-]>[-]++++++++++[<+++++++++++++++>-]>>>>>>>>++++++++++[>++++++++++<-]>>>+++++[<++++++++++>-]<[>>+++++[>++++++++++>>>+++++<<<<-]<<[[<<+>>>>>>>>[>>]<<<+<<<[>>]<<-]<<[>>+<<-]>>>>>>>>[>>]<<<[-[>->]<[<]>]>[-[<+>-]>+>]<<<[>+<-]<<<<<<]>>>>>>>>[>>>++<<<-]>>>[>+>>++<<<-[>>>-<<<-[>+<-]]]>[<+>-]>>[<<++++++++[>++++++++++<-]>>-]<<<<[<++++++++++>-]<<<+>>[>>>+<<<<<<+>-[>]>>[<<<[>+<-]<+>>>>>]<-]<<<[>+<-]>[>>>+<<<-]<++++[>+++++[>++++++++++<-]<-]<+++++[>+<-]>>>[<<<+>-[>]>>[<<<[>+<-]<<<+>>>>>>>]<-]<<[-]<[-]+++++[>++++++++<-]<<<[>>>>-<<<<-]>>>>>>>>>[<<+>>-[<<->>-]]<<<<<+[>>>>>+>>+<<<<<<<-]>>>[>>[>>--<<-]<<-]>>>>[>+<-]>[-[->>+<]<]>>[<]>+[>>>+<<<-]<<<<<[-]>[<+>----------]>>>+>+++<<<<<+[[>>+>+<<<-]>>[>[<<<<<+>>>>>>-[<]<[>>[<+>>+<-]>>+<[<+>-]<++<<<<<]>>>-]<<<<<[>>>>>+<<<<<-]>>>>-]>[-]>[[<+>-]>]>[<+>-]<<<<<<<<]>>>>[-]>[-]++++[<+++++[>>++++++++++<<-]>-]>[>>+<-[<]<[>>[<+>-]>+<<<<<]>>-]>[-]>[-]>+<<++++++>>[<<-[>]>>>[<<<++++++<<+>[<->-]<[>+<-]>>>>>>]<<-]+++++++<<[>>-<<-]++++++>>>[<<<-[>]>>>[<<<++++++<<+>[<->-]<[>+<-]>>>>>>]<-]+++++++<<<[>>>-<<<-]<+[>>+<<-]>>>>[[<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>-]<]<<<<<<<<<<<<[>>>>+<<<<-]<[>>>>+<<<<-]>>>>-[>]>[>++++[>+++++<-]>++[>>++++++++++[>+++<<++++++++++>-]>++<<[>>.<<-]>>[-]+++++++++++++.---.[-]<<<-]++++++++++[<++++++++++>-]<<[<+>>--<-]<[>+<-]++++++[>>>+++++[>+++++++<-]<[>+>.<<-]>>---<[<+>-]<<[>>+>..<<<-]>>>[-]+++++++++++++.---.[-]<[<<+>>-]<<<-]>>[-]++++[>+++++<-]>++[>>++++++++++[>+++<<++++++++++>-]>++<<[>>.<<-]>>[-]+++++++++++++.---.[-]<<<-]<<<<++++++++++[>++++++++++<-]>>-[[>>>>+<<<<-]<]>>>>>>>>]<<]<<<<<<<[-]<<<<<[[<<<<]<<<<]<[>>>>>>>[[->>>+++++++<<[>>-<<-]>>[<<+>>-]+<<-[>>[-]>>>>+<<<<<<-]>>[<<++++++<[>>>-<<<-]>>>[<<<+>>>-]]>>>>[<<<<<<+>>>>>>-]<<<<+++++++<<[>>-<<-]>>[<<+>>-]+<-[>[-]>>>>+<<<<<-]>[<++++++<<[>>>-<<<-]>>>[<<<+>>>-]]>>>>[<<<<<+>>>>>-]<<<<<<<[>>>+++>>>>+<<<<<<<-]+>>>>>>>[<<<<<<<+>>>>>>>-]++++[<<<<++++++++>>>>-]<<<]>>>>]<[-]<<<<[[.[-]<<<<]+++++++++++++.---.[-]<<<<]<<<[>>++>+<<<-]>>>[<<<+>>>-]<[>>>++++++[<++++++[<+++++++>-]>-]<<+++[>+>+<<-]>[>[<<+>>-]<<[>>+<<-]>-]>[-]<<<-]<-]>>>>>>>[[>>>>]>>>>]<<<<<<[[[[-]<]<]<<<<]++++++++++[<+++++++++++++++>-]>>>>++++++++++[>++++++++++<-]>>>+++++[<++++++++++>-]<[>+++++[>+++++<-]<[>+>>+<-[[>>+<<-]>-<]>[>+++++[<<+++++>>-]<-]>[<<+>>-]<<<<-]>[<+>>>+<<-]>>>+++++[<<<+++++>>>-]<<[<->-]<[>++++<-]+++++[>>>++++++>>>+++++<<<<<<-]>>>+++<<<<<[>>+>>>>+<-[[>>+<<-]>-<]>[>+++++[<<++++++>>-]<<+++>-]>[<<+>>-]>>+<-[[>>+<<-]>-<]>[>+++++[<<+++++>>-]<-]>[<<+>>-]<<<<<<<<<<-]>>[<<+>>-]>>>>>+++++[>>+++++<<<++++++>-]<+++<[>-<-]>[<+++>-]>>[>-<-]>[<<<++++>>>-]<<<+<+<+<+[[>>>>>>+<<<<<<-]>>>>>>>>+++++[<++++++++++>-]<<[-[>->]<[<]>]>[-[<+>-]<<<+>>>>>]<+++++[<+++++>-]<<[-[>->]<[<]>]>[-[<+>-]>>]<<<[<+>>+<-]>>+++++[>++++++<-]>+<<<<[>>[>>>>+<<-[<]<<[>+++++[>++++++<-]>+>+<<<<]>-]>>>>[<<<<+>>>>-]<<<<<<-]>>[-]>>[-]>+<<<<<<[>>>>+++++[>++++++++<-]>>[<->-]<[>+<-]<<<<<-]>>>>>>[<<<<<<<<<<+>>>>>>>>>>-]<<<<<<<<<]+++++++++++++<<<<[[>>[<<<+>>>>>>+<-[[>>+<<-]>-<]>[>>+<<-<+++++++++++++>]>[<<+>>-]<<<<-]<<<[>>>+<<<-]>-]>>[-]>>[-]>>>[<<<+>>>-]<<+++++++++++++<<<<]>>>>[-]>++++[<++++>-]<++<[<+>-]<[>>>+<-[<]<<[>>>>+<[<+>-]<<<<]>-]>>[-]>[-]>+[<<<<<<<<<<<<+>>>>>>>>>>>>-]<<<<<<<<<<[>>+<<-]<[>>+<<-]>>-[>]>[>++++[>+++++<-]>++[>>++++++++++[>+++<<++++++++++>-]>++<<[>>.<<-]>>[-]+++++++++++++.---.[-]<<<-]++++++++++[<++++++++++>-]<<[<+>>--<-]<[>+<-]++++++[>>>+++++[>+++++++<-]<[>+>.<<-]>>---<[<+>-]<<[>>+>..<<<-]>>>[-]+++++++++++++.---.[-]<[<<+>>-]<<<-]>>[-]++++[>+++++<-]>++[>>++++++++++[>+++<<++++++++++>-]>++<<[>>.<<-]>>[-]+++++++++++++.---.[-]<<<-]<<<<++++++++++[>++++++++++<-]>>-[>>+<<-]<[>>+<<-]>>>>>]<<]<[-]<<<<<[[<<]<<]<[>>>>>[[<<+>>-[<]<<[>>+++++++++++++<<<]>->>>>]>>]<<<<[[[<<<+>>+>-]>++++[<++++++++>-]<<[-[->+++++++<[->+++++<[->[>++<-]>[<+>-]<<[->>++++[<----->-]<-<[->>+++++[<------>-]<--<[-[->>+++++[<++++++>-]<<[->++<[->>++++[<+++++>-]<+<[->[>+<--]>[<+>-]<<[->-----<[->-------<]]]]]]]]]]]]]>.[-]<<<[>>>+<<<-]>]+++++++++++++.---.[-]<<]<<[>>+>+<<<-]>>>[<<<+>>>-]<[>>>++++++[<++++++[<+++++++>-]>-]<<+++[>+>+<<-]>[>[<<+>>-]<<[>>+<<-]>-]>[-]<<<-]<-]>>>>>[[>>]>>]<<<<[[[-]<<]<<]<++++[>>>+++++[>++++++++++<-]>[<<<[-]>[-]++++>>>>>+++++[<++++++++++>-]<[<<<<-[>]>>[<<++<[>--<--]>[<++>--]++++>>>]>[>+>>+<<<-]>>>>>+<<<<[>>[>>>>+<<-[<]<<[>++++++[>++++++<-]>>+<<<<]>-]>>>>[<<<<+>>>>-]<<<<<<-]>>[<<<+>>>-]>>[-]>[<<<<<<<+>>>>>>>-]<<<<<++++++++++[<<++++++++++>>-]<<<[<+>>+<-]<[>+<-]++++[>>[>>+>>+<<<<-]>>[<<+>>-]++[>>[<+>>>+<<-]++++++++++[>++++++++++<-]>+[-[>->]<[<]>]>[-[<+>-]<<++++++++++[<---------->-]>>>>]<<<[-]<<[>+<-]<-]>>>>+++++[<++++++++++>-]<<[-[>->]<[<]>]>[-[<+>-]<<<+>>>>>]<<<[<+>>>+<<-]++++[>++++++++++<-]>->+<[-[>->]<[<]>]>[-[<+>-]<<<[-]<[-]++++++++[>++++++++++<-]>+>>>>>]<<<[-]<<[>+<-]>>>+++++[<+++++>-]<<[-[>->]<[<]>]>[-[<+>-]>>]<<<[<+>>+<-]>>+++++[>++++++<-]>+<<<<[>>[>>>>+<<-[<]<<[>+++++[>++++++<-]>+>+<<<<]>-]>>>>[<<<<+>>>>-]<<<<<<-]>>[-]>>[-]>+<<<<<<[>>>>+++++[>++++++++<-]>>[<->-]<[>+<-]<<<<<-]>>>>>+++++[>++++++<-]>>>>[>>]+[<<]<[>>>[>>]<<+[<<]<-]<<<<<<+++++[<<----->>-]<<<<-]>>>>>>>>>>+++[>>>>>[<<[-[>>->>>>>+<<<<]<[<<<]>]>>[-[<<+>>-]>>>>>+<<]>>[<<<<<+<<+>>>>>>>-]<<<]<<[<<]<-]>>>[-]++++++++++[<<++++++++++>>-]<<--->>>>[<+<<<->>>>-]<[>+<-]>>>[[<+<->>-]<[>+<-]>>>]+++++[<++++++>-]<++<[>.<-]>+++.[-]<<<<<<<<<<<<<<<<<<<<<<[>>+>>>>>>>>>>>>+>>+<<<<<<<<<<<<<<<<-]>>[<<+>>-]>>>>>>>>>>>>>>>>>[[>+>>+<<<-]>>>[-[-[-[->>+<]]]<]>>[<]>+[>+<<<+>>-]++++[<++++++++>-]<<<<<<<<[>>>>>>>++>++<<<<<<<<--]>>>>[>>-[<]<<[>>++>>[<--<-->>--]<<[>++>++<<--]>>>[<<<+>>>>+<-]>[<+>-]<<<<<<<]>>>>.<<<-]>>>>>[-]<[<-->--]<+++.[-]<[-]<<<<<]++++[<++++++++>-]<<[>.<-]>[-]+++++++++++++.---.[-]<<<<<<<<<<[-]>-]<<<<<<<[>>+>>>>+<<<<<<-]>>[<<+>>-]>>>>[>>>++++++[<++++++[<+++++++>-]>-]<<+++[>+>+<<-]>[>[<<+>>-]<<[>>+<<-]>-]>[-]<<<-]<-]<<[-]<<-]. 4 | -------------------------------------------------------------------------------- /bfprogs/fib.bf: -------------------------------------------------------------------------------- 1 | >++++++++++>+>+[ 2 | [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[ 3 | [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 4 | [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>> 5 | ]<<< 6 | ] 7 | This program doesn't terminate; you will have to kill it. 8 | Daniel B Cristofani (cristofdathevanetdotcom) 9 | http://www.hevanet.com/cristofd/brainfuck/ 10 | -------------------------------------------------------------------------------- /bfprogs/hello.bf: -------------------------------------------------------------------------------- 1 | [ This program prints "Hello World!" and a newline to the screen, its 2 | length is 106 active command characters. [It is not the shortest.] 3 | 4 | This loop is a "comment loop", a simple way of adding a comment 5 | to a BF program such that you don't have to worry about any command 6 | characters. Any ".", ",", "+", "-", "<" and ">" characters are simply 7 | ignored, the "[" and "]" characters just have to be balanced. This 8 | loop and the commands it contains are ignored because the current cell 9 | defaults to a value of 0; the 0 value causes this loop to be skipped. 10 | (c) wikipedia.org 11 | ] 12 | +++++ +++ Set Cell #0 to 8 13 | [ 14 | >++++ Add 4 to Cell #1; this will always set Cell #1 to 4 15 | [ as the cell will be cleared by the loop 16 | >++ Add 2 to Cell #2 17 | >+++ Add 3 to Cell #3 18 | >+++ Add 3 to Cell #4 19 | >+ Add 1 to Cell #5 20 | <<<<- Decrement the loop counter in Cell #1 21 | ] Loop till Cell #1 is zero; number of iterations is 4 22 | >+ Add 1 to Cell #2 23 | >+ Add 1 to Cell #3 24 | >- Subtract 1 from Cell #4 25 | >>+ Add 1 to Cell #6 26 | [<] Move back to the first zero cell you find; this will 27 | be Cell #1 which was cleared by the previous loop 28 | <- Decrement the loop Counter in Cell #0 29 | ] Loop till Cell #0 is zero; number of iterations is 8 30 | 31 | The result of this is: 32 | Cell No : 0 1 2 3 4 5 6 33 | Contents: 0 0 72 104 88 32 8 34 | Pointer : ^ 35 | 36 | >>. Cell #2 has value 72 which is 'H' 37 | >---. Subtract 3 from Cell #3 to get 101 which is 'e' 38 | +++++++..+++. Likewise for 'llo' from Cell #3 39 | >>. Cell #5 is 32 for the space 40 | <-. Subtract 1 from Cell #4 for 87 to give a 'W' 41 | <. Cell #3 was set to 'o' from the end of 'Hello' 42 | +++.------.--------. Cell #3 for 'rl' and 'd' 43 | >>+. Add 1 to Cell #5 gives us an exclamation point 44 | >++. And finally a newline from Cell #6 45 | -------------------------------------------------------------------------------- /bfprogs/life.bf: -------------------------------------------------------------------------------- 1 | 2 | Linus Akesson presents: 3 | The Game Of Life implemented in Brainfuck 4 | 5 | +>>++++[<++++>-]<[<++++++>-]+[<[>>>>+<<<<-]>>>>[<<<<+>>>>>>+<<-]<+ 6 | +++[>++++++++<-]>.[-]<+++[>+++<-]>+[>>.+<<-]>>[-]<<<++[<+++++>-]<.<<[>>>>+ 7 | <<<<-]>>>>[<<<<+>>>>>>+<<-]<<[>>>>.+<<<++++++++++[<[>>+<<-]>>[<<+>>>>>++++++++ 8 | +++<<<-]<[>+<-]>[<+>>>>+<<<-]>>>[>>>>>>>>>>>>+>+<< <<<<<<<<<<<-]>>>>>>>>>> 9 | >>[-[>>>>+<<<<-]>[>>>>+<<<<-]>>>]> >>[<<<+>> >- ]<<<[>>+>+<<<-]>[->[<<< 10 | <+>>>>-]<[<<< <+> >>>-]<<<< ]< ++++++ ++ +[>+++++<-]>>[<<+>>-]< 11 | <[>---<-]>.[- ] <<<<<<<<< < <<<<<< < -]++++++++++.[-]<-]>>> 12 | >[-]<[-]+++++ +++[>++++ ++++< - ]>--.[-]<,----------[<+ 13 | >-]>>>>>>+<<<<< < <[>+>>>>>+>[ -]<<< << <<-]>++++++++++>>>>>[[-] 14 | <<,<<<<<<<->>>> > >>[<<<<+>>>>-]<<<<[>>>>+ >+<<<<<-]>>>>>----------[<<<< 15 | <<<<+<[>>>>+<<< <-]>>>>[<<<<+>>>>>>+<<- ]>[>-<-]>++++++++++[>+++++++++ 16 | ++<-]<<<<<<[>>> >+<<<<-]>>>>[<<<<+>>>>> >+<<-]>>>>[<<->>-]<<++++++++++ 17 | [>+<-]>[>>>>>>> >>>>>+>+<<<< <<<<< <<<<-]>>> >> >>>>>>>[-[>>> 18 | >+<<<<-]>[>>>> +<<<<-]>> > ]>> > [<< < +>>>-]+<<<[> 19 | >>-<<<-]>[->[< <<<+>>>>-] <[ < < < <+>>>>-]<<< 20 | <]<<<<<<<<<<<, [ -]]>]>[-+++ ++ + +++ ++[>+++++++ 21 | ++++>+++++++++ + +<<-]>[-[>>> +<<<- ]>>>[ < <<+ >>>>>>>+>+< 22 | <<<<-]>>>>[-[> > >>+<<<<-]>[> >>>+< < <<-]> > >]> >>[<<<+>>>- 23 | ]<<<[>>+>+<<< - ]>[->[<<<<+> >>>-] < [<<< < +>> >>-]<<<<]<< 24 | <<<<<<[>>>+<< < -]>>>[<<<+>> >>>>> + >+<< < <<-]<<[>>+<< 25 | -]>>[<<+>>>>> >+>+<<<<<-]>> >>[-[ > >>>+ < <<<-]>[>>>>+< 26 | <<<-]>[>>>>+< <<<-]>>]>>>[ - ]<[>+< - ]<[ - [<<<<+>>>>-]<<< 27 | <]<<<<<<<<]<< <<<<<<<<++++ + +++++ [ >+++ + ++++++[<[>>+<<-]>>[<<+ 28 | >>>>>++++++++ + ++<<< -] < [>+<- ] >[<+ > >>>+<<<-]>>>[<<<+>>>-] 29 | <<<[>>>+>>>> > +<<<< << <<-]> > >>>> >>>[>>+<<-]>>[<<+<+>> 30 | >-]<<<------ - -----[ >> >+<<< - ]>>> [<<<+> > >>>>>+>+<<<< 31 | <-]>>>>[-[>> > >+<<<< -] > [>>>> + <<<<- ]>>> ] >>>[<<<+>>>- 32 | ]<<<[>>+>+<< < -]>>> >> > > [<<<+ >>>-]<<<[>>> 33 | +<<<<<+>>- ]> > >>>>>[< <<+>>>-]<<<[> 34 | >>+<<<<<<< <<+ > >>>>>-]< <<<<<<[->[<<<<+ 35 | >>>>-]<[<<<<+>>>>-]<<<<]>[<<<<<< <+>>> >>>>-]<<<< <<<<<+++++++++++[> 36 | >>+<<<-]>>>[<<<+>>>>>>>+>+<<<<<-]>>>>[-[> >>>+<<<<-]>[>>>>+<<<<-]>>>]>>>[<<< 37 | +>>>-]<<<[>>+>+<<<-]>>>>>>>[<<<+>>>-]<<<[ >>>+<<<<<+>>-]>>>>>>>[<<<+>>>-]<<< 38 | [>>>+<<<<<<<<<+>>>>>>-]<<<<<<<[->[< < < <+>>>>-]<[<<<<+>>>>-]<<<<]>[<<<<<<< 39 | +>>>>>>>-]<<<<<<<<<+++++++++++[>>> > >>>+>+<<<<<<<<-]>>>>>>>[-[>>>>+<<<<- 40 | ]>[>>>>+<<<<-]>>>]>>>[<<<+>>>-]<<< [ >>+>+<<<-]>>>>>>>[<<<+>>>-]<<<[>>>+<< 41 | <<<+>>-]>>>>>>>[<<<+>>>-]<<<[>>>+< <<<<<<<<+>>>>>>-]<<<<<<<[->[<<<<+>>>>- 42 | ]<[<<<<+>>>>-]<<<<]>[<<<<<<<+>>>>> >>-]<<<<<<<----[>>>>>>>+<<<<<<<+[>>>>> 43 | >>-<<<<<<<[-]]<<<<<<<[>>>>>>>>>>>>+>+<<<<<<<<<<<<<-][ lft@df.lth.se ]>>>>> 44 | >>>>>>>[-[>>>>+<<<<-]>[>>>>+<<<<-]>[>>>>+<<<<-]>>]>>>[-]<[>+<-]<[-[<<<<+>> 45 | >>-]<<<<]<<<<<<[-]]<<<<<<<[-]<<<<-]<-]>>>>>>>>>>>[-]<<]<<<<<<<<<<] 46 | 47 | Type for instance "fg" to toggle the cell at row f and column g 48 | Hit enter to calculate the next generation 49 | Type q to quit 50 | -------------------------------------------------------------------------------- /bfprogs/mandelbrot.bf: -------------------------------------------------------------------------------- 1 | A mandelbrot set fractal viewer in brainfuck written by Erik Bosman 2 | +++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>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->>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<< 141 | <<]>>>>[-]+>>>>>[>>>>>>>>>]>+<]]+>>>>>>>>[-<<<<<<<<->>>>>>>>]+<<<<<<<<[->>>>>>>> 142 | -<<[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+>>>>>>[>>>>>> 143 | >>>]>[-]+<]]+>[-<[>>>>>>>>>]<<<<<<<<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++ 144 | +[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<< 145 | <<<<<]]>>>] 146 | 147 | -------------------------------------------------------------------------------- /bfprogs/sierpinski.bf: -------------------------------------------------------------------------------- 1 | [ This program prints Sierpinski triangle on 80-column display. ] 2 | > 3 | + + 4 | + + 5 | [ < + + 6 | + + 7 | + + + + 8 | > - ] > 9 | + + + + + + + + 10 | [ > 11 | + + + + 12 | < - ] > 13 | > + + > > > + > 14 | > > + < 15 | < < < < < < < < 16 | < [ - [ - > + < 17 | ] > [ - < + > > > . < < ] > > > 18 | [ [ 19 | - > + + 20 | + + + + 21 | + + [ > + + + + 22 | < - ] > 23 | . < < [ - > + < 24 | ] + > [ - > + + 25 | + + + + + + + + < < + > ] > . [ 26 | - ] > ] 27 | ] + < < < [ - [ 28 | - > + < ] + > [ 29 | - < + > > > - [ - > + < ] + + > 30 | [ - < - > ] < < 31 | < ] < < < < ] + + + + + + + + + 32 | + . + + + . [ - ] < ] + + + + + 33 | * * * * * M a d e * B y : * N Y Y R I K K I * 2 0 0 2 * * * * * 34 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | GENSRC=pbf_fib.c pbf_life.c pbf_hello.c pbf_mandel.c pbf_sierp.c pbf_demo.c pbf_99b.c 2 | BIN=$(GENSRC:.c=) 3 | 4 | # Without cheating but demo may run a little slower 5 | #FLAGS=-W -Wall -Wextra -Wpedantic -O3 -D_FORTIFY_SOURCE=1 -DNOCHEATING 6 | 7 | # Slight cheating during instruction execute but runs faster 8 | FLAGS=-W -Wall -Wextra -Wpedantic -O3 -U_FORTIFY_SOURCE 9 | 10 | all: $(BIN) 11 | 12 | pbf_fib: pbf_pre.c ../bfprogs/fib.bf 13 | python toker.py -t pbf_pre.c -bf ../bfprogs/fib.bf > pbf_fib.c 14 | gcc $(FLAGS) pbf_fib.c -o pbf_fib 15 | 16 | pbf_life: pbf_pre.c ../bfprogs/life.bf 17 | python toker.py -t pbf_pre.c -i "efeefe\\n\\n\\n" -bf ../bfprogs/life.bf > pbf_life.c 18 | gcc $(FLAGS) pbf_life.c -o pbf_life 19 | 20 | pbf_hello: pbf_pre.c ../bfprogs/hello.bf 21 | python toker.py -t pbf_pre.c -bf ../bfprogs/hello.bf > pbf_hello.c 22 | gcc $(FLAGS) pbf_hello.c -o pbf_hello 23 | 24 | pbf_mandel: pbf_pre.c ../bfprogs/mandelbrot.bf 25 | python toker.py -t pbf_pre.c -bf ../bfprogs/mandelbrot.bf > pbf_mandel.c 26 | gcc $(FLAGS) pbf_mandel.c -o pbf_mandel 27 | 28 | pbf_sierp: pbf_pre.c ../bfprogs/sierpinski.bf 29 | python toker.py -t pbf_pre.c -bf ../bfprogs/sierpinski.bf > pbf_sierp.c 30 | gcc $(FLAGS) pbf_sierp.c -o pbf_sierp 31 | 32 | pbf_demo: pbf_pre.c ../bfprogs/demo.bf 33 | python toker.py -t pbf_pre.c -i A -bf ../bfprogs/demo.bf > pbf_demo.c 34 | gcc $(FLAGS) pbf_demo.c -o pbf_demo 35 | 36 | pbf_99b: pbf_pre.c ../bfprogs/99bottles.bf 37 | python toker.py -t pbf_pre.c -i A -bf ../bfprogs/99bottles.bf > pbf_99b.c 38 | gcc $(FLAGS) pbf_99b.c -o pbf_99b 39 | 40 | .PHONY=clean 41 | clean: 42 | rm -f $(BIN) $(GENSRC) 43 | -------------------------------------------------------------------------------- /src/pbf_pre.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define ARRSIZE (2<<24) 8 | 9 | char buf[250000]; 10 | 11 | /* array will be used to not spill to stdout */ 12 | unsigned char* array_; 13 | unsigned char* array; 14 | int ptr; 15 | 16 | int progn_[1000000]; 17 | int* progn; 18 | int pc; 19 | 20 | char* output_; 21 | char* output; 22 | int outptr; 23 | 24 | char* input_; 25 | char* input; 26 | int inptr; 27 | 28 | 29 | char* format_specifiers; 30 | 31 | /* Arrays that hold our format strings */ 32 | char bf_LT_fmt[100]; 33 | char bf_GT_fmt[100]; 34 | char bf_PLUS_fmt[100]; 35 | char bf_MINUS_fmt[100]; 36 | char bf_DOT_fmt1[100]; 37 | char bf_DOT_fmt2[100]; 38 | char bf_GOTO_fmt[100]; 39 | char bf_COMMA_fmt1[100]; 40 | char bf_COMMA_fmt2[100]; 41 | char bf_CGOTO_fmt1[100]; 42 | char bf_CGOTO_fmt2[100]; 43 | char bf_CGOTO_fmt3[100]; 44 | 45 | char bf_ZERO_fmt[100]; 46 | char bf_FORWARD_N_fmt[100]; 47 | char bf_BACKWARD_N_fmt1[100]; 48 | char bf_BACKWARD_N_fmt2[100]; 49 | char bf_BACKWARD_N_fmt3[100]; 50 | char bf_ADD_N_fmt[100]; 51 | 52 | char bf_FETCH_fmt[100]; 53 | 54 | char* syms[] = { 55 | 0, 56 | bf_LT_fmt, 57 | bf_GT_fmt, 58 | bf_PLUS_fmt, 59 | bf_MINUS_fmt, 60 | bf_DOT_fmt1, 61 | bf_DOT_fmt2, 62 | bf_COMMA_fmt1, 63 | bf_COMMA_fmt2, 64 | bf_CGOTO_fmt1, 65 | bf_CGOTO_fmt2, 66 | bf_CGOTO_fmt3, 67 | bf_GOTO_fmt, 68 | 69 | bf_ZERO_fmt, 70 | bf_FORWARD_N_fmt, 71 | bf_BACKWARD_N_fmt1, 72 | bf_BACKWARD_N_fmt2, 73 | bf_BACKWARD_N_fmt3, 74 | bf_ADD_N_fmt, 75 | bf_FETCH_fmt 76 | }; 77 | 78 | char** real_syms; 79 | 80 | /** 81 | * setup - initialize basic variables and status for interpreter 82 | * Initialize interpreter, nothing that can't be done without a couple 83 | * of controlled memory writes and some limited alignment. 84 | * No cheating here, move along. 85 | */ 86 | void setup() { 87 | unsigned int i; 88 | inptr = 0; 89 | ptr = 0; 90 | pc = 2; 91 | 92 | real_syms = (char**)calloc(1,1<<17); 93 | real_syms = (char**)((char*)real_syms + 0x10000-(((long)real_syms)&0xFFFF)); 94 | 95 | for (i = 0; i < sizeof syms/sizeof(*syms); i++) { 96 | real_syms[i] = syms[i]; 97 | } 98 | 99 | array_ = malloc(ARRSIZE); 100 | array = (void*)((char*)array_ + 0x1000000-(((long)array_)&0xFFFFFF)); 101 | output_ = malloc(ARRSIZE); 102 | output = (void*)((char*)output_ + 0x1000000-(((long)output_)&0xFFFFFF)); 103 | input_ = malloc(ARRSIZE); 104 | input = (void*)((char*)input_ + 0x1000000-(((long)input_)&0xFFFFFF)); 105 | 106 | progn = (void*)((char*)progn_ + 0x10000-(((long)progn_)&0xFFFF)); 107 | 108 | for (i = 0; i < ARRSIZE/sizeof(*array); i++) { 109 | array_[i] = 0; 110 | } 111 | for (i = 0; i < sizeof progn_/sizeof(*progn); i++) { 112 | progn_[i] = 0; 113 | } 114 | for (i = 0; i < ARRSIZE /sizeof(*input); i++) { 115 | input_[i] = -1; 116 | } 117 | 118 | strcpy(bf_LT_fmt, "%1$65535d%1$.*1$d%2$hn"); 119 | strcpy(bf_GT_fmt, "%1$.*1$d %2$hn"); 120 | strcpy(bf_PLUS_fmt, "%3$.*3$d %4$hhn"); 121 | strcpy(bf_MINUS_fmt, "%3$255d%3$.*3$d%4$hhn"); 122 | 123 | strcpy(bf_DOT_fmt1, "%3$.*3$d%5$hn"); 124 | strcpy(bf_DOT_fmt2, "%6$.*6$d %7$hn"); 125 | 126 | strcpy(bf_COMMA_fmt1, "%13$.*13$d%4$hn"); 127 | strcpy(bf_COMMA_fmt2, "%14$.*14$d %15$hn"); 128 | 129 | strcpy(bf_GOTO_fmt, "%10$.*10$d%11$hn"); 130 | 131 | strcpy(bf_CGOTO_fmt1, "%8$n%4$s%8$hhn"); 132 | strcpy(bf_CGOTO_fmt2, "%8$s%12$255d%9$hhn"); 133 | strcpy(bf_CGOTO_fmt3, " %10$.*10$d%11$hn"); 134 | 135 | strcpy(bf_ZERO_fmt, "%4$hhn"); 136 | 137 | strcpy(bf_FORWARD_N_fmt, "%1$.*1$d%10$.*10$d%2$hn"); 138 | 139 | strcpy(bf_BACKWARD_N_fmt1, "%1$.*1$d%10$.*10$d%2$hn"); 140 | 141 | strcpy(bf_ADD_N_fmt, "%3$.*3$d%10$.*10$d%4$hhn"); 142 | 143 | strcpy(bf_FETCH_fmt, "%1$.*1$d%1$.*1$d%1$.*1$d%1$.*1$d%1$.*1$d%1$.*1$d%1$." 144 | "*1$d%1$.*1$d%2$hn"); 145 | } 146 | 147 | int cond = 0; 148 | 149 | /** 150 | * Loop --execute one bf instruction 151 | * Main interpreter loop, with only slight cheating to get around 152 | * overwriting printf internal data structures. 153 | * First decode the next bf instruction and set corresponding ptrs 154 | * then execute the instruction and update the output. 155 | */ 156 | void loop() { 157 | char* last = output; 158 | int* rpc = &progn[pc]; 159 | 160 | #ifdef NOCHEATING 161 | long copyarray = 0; 162 | long copyoutput = 0; 163 | long copyinput = 0; 164 | int rpc1; 165 | #endif 166 | 167 | while (*rpc != 0) { 168 | /* fetch -- decode next instruction */ 169 | sprintf(buf, bf_FETCH_fmt, *rpc, (short*)(&real_syms)); 170 | 171 | #ifdef NOCHEATING 172 | sprintf((void*)(©array), "%s", (char*)(&array)); 173 | sprintf((void*)(©input), "%s", (char*)(&input)); 174 | sprintf((void*)(©output), "%s", (char*)(&output)); 175 | sprintf((void*)(&rpc1), "%s", (char*)(&rpc[1])); 176 | sprintf((char*)(&rpc1)+1, "%s", (char*)(&rpc[1])+1); 177 | 178 | /* execute instruction */ 179 | sprintf(buf, *real_syms, 180 | copyarray, &array, /* 1, 2 */ 181 | *array, array, /* 3, 4 */ 182 | output, /* 5 */ 183 | copyoutput, &output, /* 6, 7 */ 184 | &cond, &bf_CGOTO_fmt3[0], /* 8, 9 */ 185 | rpc1, &rpc, /* 10, 11 */ 186 | 0, /* 12 */ 187 | *input, /* 13 */ 188 | copyinput, &input /* 14, 15 */ 189 | ); 190 | #else 191 | /* execute instruction */ 192 | sprintf(buf, *real_syms, 193 | /* slight cheating for bitwise and */ 194 | ((long)array)&0xFFFF, &array, /* 1, 2 */ 195 | *array, array, /* 3, 4 */ 196 | output, /* 5 */ 197 | /* slight cheating for bitwise and */ 198 | ((long)output)&0xFFFF, &output, /* 6, 7 */ 199 | &cond, &bf_CGOTO_fmt3[0], /* 8, 9 */ 200 | rpc[1], &rpc, /* 10, 11 */ 201 | 0, /* 12 */ 202 | *input, /* 13 */ 203 | /* slight cheating for bitwise and */ 204 | ((long)input)&0xFFFF, &input /* 14, 15 */ 205 | ); 206 | #endif 207 | /* retire -- update PC */ 208 | sprintf(buf, "12345678%.*d%hn", 209 | (int)(((long)rpc)&0xFFFF), 0, (short*)&rpc); 210 | 211 | /* for debug: do we need to print? */ 212 | if (output != last) { 213 | putchar(output[-1]); 214 | last = output; 215 | } 216 | } 217 | } 218 | 219 | int main() { 220 | struct timeval tval_before, tval_after, tval_result; 221 | 222 | setup(); 223 | 224 | /* program */ 225 | ###TOKER### 226 | 227 | gettimeofday(&tval_before, NULL); 228 | 229 | strcpy(input, "###INPUT###"); 230 | 231 | /* execute the bf program */ 232 | loop(); 233 | 234 | gettimeofday(&tval_after, NULL); 235 | 236 | timersub(&tval_after, &tval_before, &tval_result); 237 | 238 | printf("Time elapsed: %ld.%06ld\n", 239 | (long int)tval_result.tv_sec, 240 | (long int)tval_result.tv_usec 241 | ); 242 | 243 | return 0; 244 | } 245 | -------------------------------------------------------------------------------- /src/toker.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import itertools 5 | import sys 6 | from argparse import ArgumentParser 7 | 8 | __author__ = "Nicholas Carlini and Mathias Payer " 9 | __description__ = "Script to tokenize a BF script into a printf interpreter." 10 | __version__ = filter(str.isdigit, "$Revision: 1 $") 11 | 12 | parser = ArgumentParser(description=__description__) 13 | parser.add_argument('-v', '--version', action='version', version='%(prog)s {:s}'.format(__version__)) 14 | parser.add_argument('-t', '--template', type=str, metavar='template filename', help='Filename for the template to use.', required=False, default='bf_pre.c') 15 | parser.add_argument('-bf', '--brainfuck', type=str, metavar='bf file', help='BF program', required=True) 16 | parser.add_argument('-i', '--input', type=str, metavar='input to bf', help='BF input', required=False, default='') 17 | args = parser.parse_args() 18 | 19 | prog = open(args.brainfuck).read() 20 | prog = "".join([x for x in prog if x in "<>+-.,[]"]) 21 | prog = prog.replace("[-]", "Z") 22 | 23 | remap = {'<':[1], 24 | '>':[2], 25 | '+':[3], 26 | '-':[4], 27 | '.':[5,6], 28 | ',':[7,8], 29 | '[':[9,10,11], 30 | ']':[12], 31 | 'Z':[13], 32 | 'F':[14], 33 | 'B':[15],#,16,17], 34 | 'A':[18], 35 | } 36 | 37 | newprog = [] 38 | progiter = iter(prog) 39 | while len(prog): 40 | e = prog[0] 41 | if e in '<>+-': 42 | res = itertools.takewhile(lambda x: x == e, prog) 43 | count = len(list(res)) 44 | if e == '>': 45 | newprog.append(('F',count)) 46 | elif e == '<': 47 | assert count < 256 48 | newprog.append(('B',65536-count)) 49 | elif e == '+': 50 | newprog.append(('A',count)) 51 | elif e == '-': 52 | newprog.append(('A',256-count)) 53 | prog = prog[count:] 54 | else: 55 | newprog.append(e) 56 | prog = prog[1:] 57 | 58 | stack = [] 59 | index = 2 60 | 61 | txt = open(args.template).read() 62 | txt = txt.replace('###INPUT###', args.input) 63 | print txt[0:txt.find('###TOKER###')], 64 | 65 | for e in newprog: 66 | count = 0 67 | if type(e) == type(tuple()): 68 | count = e[1] 69 | e = e[0] 70 | for i,insn in enumerate(remap[e]): 71 | print ' progn[%d] = %d;'%(index+i*2,insn) 72 | 73 | if count != 0: 74 | assert i == 0 75 | print ' progn[%d] = %d;'%(index+1,count) 76 | 77 | if e == '[': # this is a cgoto 78 | stack.append(index) 79 | elif e == ']': 80 | backto = stack.pop() 81 | print ' progn[%d] = %d;'%(backto+1,index*4-4) 82 | print ' progn[%d] = %d;'%(backto+3,index*4-4) 83 | print ' progn[%d] = %d;'%(backto+5,index*4-4) 84 | 85 | print ' progn[%d] = %d;'%(index+1,(backto-2)*4) # we always increment PC by 1 86 | 87 | index += 2*len(remap[e]) 88 | 89 | print txt[txt.find('###TOKER###')+11:], 90 | --------------------------------------------------------------------------------