Advertisement
schmooze

reverse engineer 12 days

Feb 20th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* the original set of strings */
  4.  
  5. static char *strings = 
  6.  
  7. "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
  8. ;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
  9. q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
  10. ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
  11. iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
  12. ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
  13. }'+}##(!!/";
  14.  
  15. /* the translation mapping */
  16.  
  17. static char *translate =
  18.                                 
  19. "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry";
  20.  
  21. #define FIRST_DAY  1
  22. #define LAST_DAY  12
  23.  
  24. /* the original "indices" of the various strings */
  25.  
  26. enum {
  27.    ON_THE = 0,
  28.    FIRST = -1,
  29.    TWELFTH = -12,
  30.    DAY_OF_CHRISTMAS = -13,
  31.    TWELVE_DRUMMERS_DRUMMING = -14,
  32.    ELEVEN_PIPERS_PIPING = -15, 
  33.    TWO_TURTLE_DOVES_AND_A = -24,
  34.    PARTRIDGE_IN_A_PEAR_TREE = -25
  35. };
  36.  
  37. /* skip -n strings (separator is /), where n is a negative value */
  38.  
  39. char* skip_n_strings(int n,char *s) {
  40.   if (n == 0)
  41.     return s;
  42.  
  43.   if (*s=='/')
  44.     return skip_n_strings(n+1,s+1);
  45.   else
  46.     return skip_n_strings(n,s+1);
  47. }
  48.  
  49. /* find the character in the translation buffer matching c and output
  50.    the translation */
  51.  
  52. void translate_and_put_char(char c, char *trans) {
  53.   if (c == *trans)
  54.     putchar(trans[31]);
  55.   else
  56.     translate_and_put_char(c,trans+1);
  57. }
  58.  
  59. void output_chars(char *s) {
  60.   if (*s == '/')
  61.     return;
  62.   translate_and_put_char(*s,translate);
  63.   output_chars(s+1);
  64. }
  65.  
  66. /* skip to the "n^th" string and print it */
  67.  
  68. void print_string(int n) {
  69.      output_chars(skip_n_strings(n,strings));
  70. }
  71.  
  72. void inner_loop(int count_day, int current_day) {
  73.   if (count_day == FIRST_DAY) {
  74.     print_string(ON_THE);               /* "On the " */
  75.     print_string(-current_day);         /* twelve days, ranges from -1 to -12 */
  76.     print_string(DAY_OF_CHRISTMAS);     /* "day of Christmas ..." */
  77.   }
  78.  
  79.   if (count_day < current_day)     /* inner iteration */
  80.     inner_loop(count_day+1,current_day);
  81.  
  82.   print_string(PARTRIDGE_IN_A_PEAR_TREE+(count_day-1));   /* print the gift */
  83. }
  84.  
  85. void outer_loop(int count_day, int current_day) {
  86.   inner_loop(count_day,current_day);
  87.   if (count_day == FIRST_DAY && current_day < LAST_DAY)  /* outer iteration */
  88.     outer_loop(1,current_day+1);
  89. }
  90.  
  91. void main() {
  92.   outer_loop(1,1);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement