B1KMusic

char[] to long[] converter

Oct 5th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. /* This program converts a char[] to a long[].
  2.  * Because it uses sizeof (long) and memcpy(), it should be portable between big/little-endian
  3.  * and 16/32/64-bit systems.
  4.  *
  5.  * Mostly pointless, just for fun, used for constructing obfuscated message-printing programs.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. #define ENCODE_BUFSZ 1000
  12. #define FMT_STR(evil_, norm_) (evil_mode ? evil_ : norm_)
  13.  
  14. static int evil_mode = 0;
  15.  
  16. void
  17. write_output(unsigned long *output, size_t len)
  18. {
  19.     if(len)
  20.         printf(FMT_STR("%lu", "0x%lx"), output[0]);
  21.  
  22.     for(int i = sizeof (long), j = 1; i < len; j++, i += sizeof (long))
  23.         printf(FMT_STR(", %lu", ", 0x%lx"), output[j]);
  24. }
  25.  
  26. void
  27. encode(char *msg)
  28. {
  29.     unsigned long output[ENCODE_BUFSZ] = {0};
  30.  
  31.     memcpy(output, msg, strlen(msg));
  32.  
  33.     printf("unsigned long msg[] = { ");
  34.     write_output(output, strlen(msg));
  35.     printf(" }; // %s\n", msg);
  36. }
  37.  
  38. int
  39. usage(void)
  40. {
  41.     fputs( "usage: ./a.out [options] \"string\" [string2 string3 ...]\n"
  42.            "options:\n"
  43.            "  -e  activate evil mode, which prints output in base 10.\n\n"
  44.            "example: ./a.out \"Hello World\" \"Goodbye World\" I\\\'m On Separate Lines\n", stderr);
  45.     return 1;
  46. }
  47.  
  48. void
  49. parse_arg(char *arg)
  50. {
  51.     if(!strcmp(arg, "-e"))
  52.         evil_mode = 1;
  53.  
  54.     else
  55.         encode(arg);
  56. }
  57.  
  58. int
  59. parse_args(char **args)
  60. {
  61.     if(!*args)
  62.         return usage();
  63.  
  64.     while(*args)
  65.         parse_arg(*(args++));
  66.  
  67.     return 0;
  68. }
  69.  
  70. int
  71. main(int argc, char **argv)
  72. {
  73.     return parse_args(argv + 1);
  74. }
Add Comment
Please, Sign In to add comment