/examples/css.bf

https://github.com/FabianM/brainfuck · Brainfuck · 210 lines · 174 code · 36 blank · 0 comment · 147 complexity · abd0d9d84336c7f40645ae7b1aab68ec MD5 · raw file

  1. (Please bear with me: I can't use any periods or commas or
  2. hyphens in this introduction)
  3. This is an implementation of CSS decryption in the Brainfuck
  4. programming language
  5. It consumes exactly 2053 bytes of input and produces exactly
  6. 2048 bytes of output: the input is the five byte title key
  7. followed by a 2048 byte sector to decrypt and the output is
  8. the decrypted sector (or the original sector if it wasn't
  9. encrypted)
  10. You can't decrypt more than one sector at a time because BF
  11. doesn't provide any way to detect the end of the input;
  12. rather than make an infinite loop I decided to stop after
  13. one iteration
  14. CSS is the encryption used on DVDs and Brainfuck is a Turing
  15. complete programming language which actually resembles a
  16. Turing machine to no small degree: in particular it uses a
  17. linear workspace accessed through a "read/write head" which
  18. can only move by one cell at a time
  19. It's a lot of fun to watch this program in action if you
  20. have a BF interpreter which shows the movement of the head
  21. (and if you don't have one write one: it takes about five
  22. minutes)
  23. Now on with the show
  24. ____________________________________________________________
  25. read the key
  26. ,>,>,>,>,>
  27. copy the first 21 bytes of the sector unchanged
  28. +++[->+++++++[->,.<]<]
  29. convert the 21st byte to binary
  30. >>[->>[>]+<[-<]<]
  31. save bit 4 (the encryption bit); nuke the rest
  32. >>[-]>[-]>[-]>[-]>[-<<<<<<<+>>>>>>>]>[-]>[-]>[-]<<<<<<<<<<
  33. if it's set:
  34. <+>[-
  35. copy another 64 bytes
  36. <+++++++[->++++++++[->,.<]<]
  37. xor the most recent byte from input with the first key byte
  38. <<<<<[->>>>>>+<<<<<<]>>>>>>
  39. [->>>>[>>]+<<[-<<]<<]>
  40. [->>>>[>>]+<<[-<<]<<]>
  41. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  42. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  43. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  44. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  45. fill in the always set bits in the LFSRs
  46. >>>>>>>>>>+>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<
  47. move the xor'ed byte to its final location (in the 17 bit LFSR)
  48. <[->>>>>>>>+<<<<<<<<]<
  49. <[->>>>>>>>>+<<<<<<<<<]<
  50. <[->>>>>>>>>>+<<<<<<<<<<]<
  51. <[->>>>>>>>>>>+<<<<<<<<<<<]<
  52. <[->>>>>>>>>>>>+<<<<<<<<<<<<]<
  53. <[->>>>>>>>>>>>>+<<<<<<<<<<<<<]<
  54. <[->>>>>>>>>>>>>>+<<<<<<<<<<<<<<]<
  55. <[->>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<]<
  56. same thing for the next four bytes (yuck)
  57. <<<<<<<<<[->>>>>+<<<<<]>>>>>>,.<
  58. [->>>>[>>]+<<[-<<]<<]>[->>>>[>>]+<<[-<<]<<]>
  59. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  60. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  61. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  62. >>[>[-<->]<[->+<]]>>[>[-<->]<[->+<]]
  63. >[->>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<]<
  64. <[->>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<]<
  65. <[->>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<]<
  66. <[->>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<]<
  67. <[->>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<]<
  68. <[->>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<]<
  69. <[->>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<]<
  70. <[->>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<]<
  71. <<<<<<<<[->>>>+<<<<]>>>>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<<<<<<<<<<<<<<<<<<<<]<
  101. <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
  102. <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
  103. <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
  104. <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
  105. <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
  106. <[->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]<
  107. <<<<<<<<
  108. copy the remaining 39 bytes of the header
  109. +++[->+++++++++++++[->,.<]<] <<
  110. main decryption loop: for each of 1920 bytes:
  111. +++++[->++++++[->++++++++[->++++++++[->
  112. read a byte and convert it to binary
  113. ,[->>>[>>]+<<[-<<]<]
  114. perform the mangling step (bit ordering is AaBbCcDdEeFfGgHh)
  115. >>>>>>+>>+>>>>+>>>>+>
  116. [-<<<<<->>>>>] <<[-<<<<<<<<<<<+>>>>>>>>>>>] F =!h; B = g;
  117. ++<<[->>->>+<<<<<<<<<<<->>>>>>>]<< [->>>>->>+<-<<<<<]<< C =!f; H =!e; g=2 minus e minus f; h=e plus f
  118. [-<<<<<<<+>>>>>>>]<< [->>>+<<<] A = d; E = c;
  119. ++<<[->>->-<<<]<< [->>>>->>>>>>>+<<<<<<<<<<<] D =!b; G = a; c=2 minus a minus b;
  120. >>>>[[-]<<<<+<[->-<]>[-<+>]>>>>>>+>[-<->]<[->+<]<<] A ^=!!c; E ^=!!c; (!!c == !(a&b))
  121. >>>>>>>>[[-]<<<<<<<<<<+<[->-<]>[-<+>]>>>>>>>>>>] B ^=!!g; (!!g == !(e&f))
  122. >>[[-]<<<<+<[->-<]>[-<+>]>>>>]<<<<<<<<<<<<<<< F ^=!!h; (!!h == (e|f))
  123. [->>[->+>[-<->]<[->+<]>>+>[-<->]<[->+<]<<+<]>[-<+>]<<+<]>[-<+>] C ^= A&B; D ^= A&B;
  124. >>>>>>>>>>++<<<[->+>>->>+<<<<<]>[-<+>]>[-<+>>->>+<<<]<[->+<] f = 2 minus E minus F; g = E plus F;
  125. >>[[-]+>[-<->]<[->+<]]>>[[-]+>[-<->]<[->+<]] G ^=!!f; H ^=!!g;
  126. generate eight cipher bits
  127. >>++++++++[>
  128. clock the 17 bit LFSR and add end bit to previous carry
  129. >>+>> >>>>>>>>>>>>>>>>
  130. [-<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>]<
  131. [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
  132. [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
  133. [->+<]<[->+<<<<+>>>]<[->+<]<[->+<]<
  134. [->+<<->[->-<<+>]]
  135. clock the 25 bit LFSR and add end bit to total
  136. >>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>
  137. [-<<<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>>>>]<
  138. [->+<]<[->+<]<
  139. [->+<<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>>]<
  140. [->+<<<<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>>>]<
  141. [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
  142. [->+<<<<<<<<<<<<<<+>>>>>>>>>>>>>]<
  143. [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
  144. [->+<]<[->+<]<[->+<]<[->+<]<[->+<]<[->+<]<
  145. [->+<<+>[->-<<->[->+<<+>[->-<<->]]]]<
  146. [-<<<<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>>>>]<<<<<<<<<<<<<<<<<<<
  147. split total into low bit (result) and high bit (new carry)
  148. [->+<]>
  149. [-<<[<]+>[->]>]
  150. <<<[->>+<<]
  151. shift in new result bit
  152. <<<<<<<<<<<<<<<<<[-]
  153. >>[-<<+>>]>>[-<<+>>]>>[-<<+>>]>>[-<<+>>]
  154. >>[-<<+>>]>>[-<<+>>]>>[-<<+>>]>>>>[-<<<<+>>>>]
  155. <<-]
  156. xor cipher byte with sector byte
  157. <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
  158. <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
  159. <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]<
  160. <[<[->-<]>[-<+>]]< <[<[->-<]>[-<+>]]
  161. combine bits into output byte
  162. > [-<<++>>] >>>> [-<<++>>] >>>> [-<<++>>] >>>> [-<<++>>]
  163. << [-<<<<++++>>>>] <<<<<<<< [-<<<<++++>>>>]
  164. >>>> [-<<<<<<<<++++++++++++++++>>>>>>>>]
  165. and write it out
  166. <<<<<<<<.[-]
  167. end of decryption loop
  168. <<]<]<]<] >>>>>
  169. end of decryption; if the encryption bit was clear:
  170. ]<[
  171. then just copy the rest of the sector (2027 bytes)
  172. >+++++[->+++++[->+++++++++[->+++++++++[->,.<]<]<]<],.,.>
  173. end of plaintext copy
  174. ]