Advertisement
Guest User

Untitled

a guest
Jun 21st, 2010
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. test@localhost ~/pycparser-1.07 $ cat rofl.c
  2. #include<stdio.h> #define __(a) goto a; #define ___(a) putchar(a); #define _(a,b) ___(a) __(b); main() { _:__(t)a:_('r',g)b:_('$',p) c:_('l',f)d:_(' ',s)e:_('a',s) f:_('o',q)g:_('l',h)h:_('d',n) i:_('e',w)j:_('e',x)k:_('\n',z) l:_('H',l)m:_('X',i)n:_('!',k) o:_('z',q)p:_('q',b)q:_(',',d) r:_('i',l)s:_('w',v)t:_('H',j) u:_('a',a)v:_('o',a)w:_(')',k) x:_('l',c)y:_('\t',g)z:___(0x0)}
  3. test@localhost ~/pycparser-1.07 $ vi rofl.c
  4. test@localhost ~/pycparser-1.07 $ cat rofl.c
  5. #include<stdio.h>
  6. #define __(a) goto a;
  7. #define ___(a) putchar(a);
  8. #define _(a,b) ___(a) __(b);
  9. main() { _:__(t)a:_('r',g)b:_('$',p) c:_('l',f)d:_(' ',s)e:_('a',s) f:_('o',q)g:_('l',h)h:_('d',n) i:_('e',w)j:_('e',x)k:_('\n',z) l:_('H',l)m:_('X',i)n:_('!',k) o:_('z',q)p:_('q',b)q:_(',',d) r:_('i',l)s:_('w',v)t:_('H',j) u:_('a',a)v:_('o',a)w:_(')',k) x:_('l',c)y:_('\t',g)z:___(0x0)}
  10. test@localhost ~/pycparser-1.07 $ gcc -E rofl.c > processed.c
  11. test@localhost ~/pycparser-1.07 $ head -n 2 processed.c
  12. # 1 "rofl.c"
  13. # 1 "<built-in>"
  14. test@localhost ~/pycparser-1.07 $ tail -n 2 processed.c
  15.  
  16. main() { _:goto t;a:putchar('r'); goto g;;b:putchar('$'); goto p;; c:putchar('l'); goto f;;d:putchar(' '); goto s;;e:putchar('a'); goto s;; f:putchar('o'); goto q;;g:putchar('l'); goto h;;h:putchar('d'); goto n;; i:putchar('e'); goto w;;j:putchar('e'); goto x;;k:putchar('\n'); goto z;; l:putchar('H'); goto l;;m:putchar('X'); goto i;;n:putchar('!'); goto k;; o:putchar('z'); goto q;;p:putchar('q'); goto b;;q:putchar(','); goto d;; r:putchar('i'); goto l;;s:putchar('w'); goto v;;t:putchar('H'); goto j;; u:putchar('a'); goto a;;v:putchar('o'); goto a;;w:putchar(')'); goto k;; x:putchar('l'); goto c;;y:putchar('\t'); goto g;;z:putchar(0x0);}
  17. test@localhost ~/pycparser-1.07 $ tail -n 1 processed.c > wat
  18. test@localhost ~/pycparser-1.07 $ mv wat processed.c
  19. test@localhost ~/pycparser-1.07 $ cat processed.c
  20. main() { _:goto t;a:putchar('r'); goto g;;b:putchar('$'); goto p;; c:putchar('l'); goto f;;d:putchar(' '); goto s;;e:putchar('a'); goto s;; f:putchar('o'); goto q;;g:putchar('l'); goto h;;h:putchar('d'); goto n;; i:putchar('e'); goto w;;j:putchar('e'); goto x;;k:putchar('\n'); goto z;; l:putchar('H'); goto l;;m:putchar('X'); goto i;;n:putchar('!'); goto k;; o:putchar('z'); goto q;;p:putchar('q'); goto b;;q:putchar(','); goto d;; r:putchar('i'); goto l;;s:putchar('w'); goto v;;t:putchar('H'); goto j;; u:putchar('a'); goto a;;v:putchar('o'); goto a;;w:putchar(')'); goto k;; x:putchar('l'); goto c;;y:putchar('\t'); goto g;;z:putchar(0x0);}
  21. test@localhost ~/pycparser-1.07 $ cat lolz.py
  22. from pycparser import CParser
  23. from pycparser.c_ast import Goto, Label, FuncCall
  24.  
  25. if __name__ == '__main__':
  26. with open('processed.c') as f:
  27. source = f.read()
  28. tree = CParser().parse(source)
  29. main = tree.children()[0]
  30. compound = main.children()[1]
  31.  
  32. first = compound.children()[0]
  33. assert isinstance(first, Label)
  34. assert len(first.children()) == 1
  35. first_goto = first.children()[0]
  36. assert isinstance(first_goto, Goto)
  37.  
  38. goto = first_goto.name
  39.  
  40. output = []
  41. children = compound.children()
  42. while True:
  43. for (i, child) in enumerate(children):
  44. if isinstance(child, Label) and child.name == goto:
  45. break
  46. else:
  47. assert False, "couldn't find label to jump to"
  48. func = child.stmt
  49. assert isinstance(func, FuncCall)
  50. func_id = func.children()[0]
  51. assert func_id.name == 'putchar'
  52. args = func.args
  53. assert len(args.exprs) == 1
  54. arg = args.exprs[0]
  55. if arg.type == 'int':
  56. assert arg.value == '0x0' # wtf...
  57. output.append(chr(int(arg.value, 16)))
  58. else:
  59. assert arg.type == 'char'
  60. output.append(arg.value[1:-1])
  61. try:
  62. goto = children[i + 1]
  63. except IndexError:
  64. break
  65. assert isinstance(goto, Goto)
  66. goto = goto.name
  67. print 'program will output the following:'
  68. print repr(''.join(output))
  69. test@localhost ~/pycparser-1.07 $ python lolz.py
  70. program will output the following:
  71. 'Hello, world!\\n\x00'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement