source: trunk/Examples/Others/css-brainfuck.bf

Last change on this file was 11, checked in by chronos, 12 years ago
  • Added: Some other examples from web.
File size: 9.8 KB
Line 
1(Please bear with me: I can't use any periods or commas or
2hyphens in this introduction)
3
4This is an implementation of CSS decryption in the Brainfuck
5programming language
6
7It consumes exactly 2053 bytes of input and produces exactly
82048 bytes of output: the input is the five byte title key
9followed by a 2048 byte sector to decrypt and the output is
10the decrypted sector (or the original sector if it wasn't
11encrypted)
12
13You can't decrypt more than one sector at a time because BF
14doesn't provide any way to detect the end of the input;
15rather than make an infinite loop I decided to stop after
16one iteration
17
18CSS is the encryption used on DVDs and Brainfuck is a Turing
19complete programming language which actually resembles a
20Turing machine to no small degree: in particular it uses a
21linear workspace accessed through a "read/write head" which
22can only move by one cell at a time
23
24It's a lot of fun to watch this program in action if you
25have a BF interpreter which shows the movement of the head
26(and if you don't have one write one: it takes about five
27minutes)
28
29Now on with the show
30
31____________________________________________________________
32
33
34read the key
35,>,>,>,>,>
36
37copy the first 21 bytes of the sector unchanged
38+++[->+++++++[->,.<]<]
39
40convert the 21st byte to binary
41>>[->>[>]+<[-<]<]
42
43save bit 4 (the encryption bit); nuke the rest
44>>[-]>[-]>[-]>[-]>[-<<<<<<<+>>>>>>>]>[-]>[-]>[-]<<<<<<<<<<
45
46if it's set:
47<+>[-
48
49copy another 64 bytes
50<+++++++[->++++++++[->,.<]<]
51
52xor the most recent byte from input with the first key byte
53<<<<<[->>>>>>+<<<<<<]>>>>>>
54[->>>>[>>]+<<[-<<]<<]>
55[->>>>[>>]+<<[-<<]<<]>
56>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
57>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
58>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
59>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
60
61fill in the always set bits in the LFSRs
62>>>>>>>>>>+>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<
63
64move the xor'ed byte to its final location (in the 17 bit LFSR)
65 <[->>>>>>>>+<<<<<<<<]<
66 <[->>>>>>>>>+<<<<<<<<<]<
67 <[->>>>>>>>>>+<<<<<<<<<<]<
68 <[->>>>>>>>>>>+<<<<<<<<<<<]<
69 <[->>>>>>>>>>>>+<<<<<<<<<<<<]<
70 <[->>>>>>>>>>>>>+<<<<<<<<<<<<<]<
71 <[->>>>>>>>>>>>>>+<<<<<<<<<<<<<<]<
72 <[->>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<]<
73
74same thing for the next four bytes (yuck)
75 <<<<<<<<<[->>>>>+<<<<<]>>>>>>,.<
76 [->>>>[>>]+<<[-<<]<<]>[->>>>[>>]+<<[-<<]<<]>
77 >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
78 >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
79 >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
80 >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
81 >[->>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<]<
82 <[->>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<]<
83 <[->>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<]<
84 <[->>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<]<
85 <[->>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<]<
86 <[->>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<]<
87 <[->>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<]<
88 <[->>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<]<
89 <<<<<<<<[->>>>+<<<<]>>>>>,.<
90 [->>>>[>>]+<<[-<<]<<]>[->>>>[>>]+<<[-<<]<<]>
91 >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]>>[>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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
121 <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
122 <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
123 <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
124<[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
125
126<<<<<<<<
127
128copy the remaining 39 bytes of the header
129+++[->+++++++++++++[->,.<]<] <<
130
131main decryption loop: for each of 1920 bytes:
132+++++[->++++++[->++++++++[->++++++++[->
133
134 read a byte and convert it to binary
135 ,[->>>[>>]+<<[-<<]<]
136
137 perform the mangling step (bit ordering is AaBbCcDdEeFfGgHh)
138 >>>>>>+>>+>>>>+>>>>+>
139 [-<<<<<->>>>>] <<[-<<<<<<<<<<<+>>>>>>>>>>>] F =!h; B = g;
140 ++<<[->>->>+<<<<<<<<<<<->>>>>>>]<< [->>>>->>+<-<<<<<]<< C =!f; H =!e; g=2 minus e minus f; h=e plus f
141 [-<<<<<<<+>>>>>>>]<< [->>>+<<<] A = d; E = c;
142 ++<<[->>->-<<<]<< [->>>>->>>>>>>+<<<<<<<<<<<] D =!b; G = a; c=2 minus a minus b;
143 >>>>[[-]<<<<+<[->-<]>[-<+>]>>>>>>+>[-<->]<[->+<]<<] A ^=!!c; E ^=!!c; (!!c == !(a&b))
144 >>>>>>>>[[-]<<<<<<<<<<+<[->-<]>[-<+>]>>>>>>>>>>] B ^=!!g; (!!g == !(e&f))
145 >>[[-]<<<<+<[->-<]>[-<+>]>>>>]<<<<<<<<<<<<<<< F ^=!!h; (!!h == (e|f))
146 [->>[->+>[-<->]<[->+<]>>+>[-<->]<[->+<]<<+<]>[-<+>]<<+<]>[-<+>] C ^= A&B; D ^= A&B;
147 >>>>>>>>>>++<<<[->+>>->>+<<<<<]>[-<+>]>[-<+>>->>+<<<]<[->+<] f = 2 minus E minus F; g = E plus F;
148 >>[[-]+>[-<->]<[->+<]]>>[[-]+>[-<->]<[->+<]] G ^=!!f; H ^=!!g;
149
150 generate eight cipher bits
151 >>++++++++[>
152
153 clock the 17 bit LFSR and add end bit to previous carry
154 >>+>> >>>>>>>>>>>>>>>>
155 [-<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>]<
156 [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
157 [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
158 [->+<]<[->+<<<<+>>>]<[->+<]<[->+<]<
159 [->+<<->[->-<<+>]]
160
161 clock the 25 bit LFSR and add end bit to total
162 >>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>
163 [-<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>>>>]<
164 [->+<]<[->+<]<
165 [->+<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>]<
166 [->+<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>]<
167 [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
168 [->+<<<<<<<<<<<<<<+>>>>>>>>>>>>>]<
169 [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
170 [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
171 [->+<<+>[->-<<->[->+<<+>[->-<<->]]]]<
172 [-<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>]<<<<<<<<<<<<<<<<<<<
173
174 split total into low bit (result) and high bit (new carry)
175 [->+<]>
176 [-<<[<]+>[->]>]
177 <<<[->>+<<]
178
179 shift in new result bit
180 <<<<<<<<<<<<<<<<<[-]
181 >>[-<<+>>]>>[-<<+>>]>>[-<<+>>]>>[-<<+>>]
182 >>[-<<+>>]>>[-<<+>>]>>[-<<+>>]>>>>[-<<<<+>>>>]
183
184 <<-]
185
186 xor cipher byte with sector byte
187 <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
188 <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
189 <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
190 <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]
191
192 combine bits into output byte
193 > [-<<++>>] >>>> [-<<++>>] >>>> [-<<++>>] >>>> [-<<++>>]
194 << [-<<<<++++>>>>] <<<<<<<< [-<<<<++++>>>>]
195 >>>> [-<<<<<<<<++++++++++++++++>>>>>>>>]
196
197 and write it out
198 <<<<<<<<.[-]
199
200end of decryption loop
201<<]<]<]<] >>>>>
202
203end of decryption; if the encryption bit was clear:
204]<[
205
206then just copy the rest of the sector (2027 bytes)
207>+++++[->+++++[->+++++++++[->+++++++++[->,.<]<]<]<],.,.>
208
209end of plaintext copy
210]
Note: See TracBrowser for help on using the repository browser.