Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This program converts a char[] to a long[].
- * Because it uses sizeof (long) and memcpy(), it should be portable between big/little-endian
- * and 16/32/64-bit systems.
- *
- * Mostly pointless, just for fun, used for constructing obfuscated message-printing programs.
- */
- #include <stdio.h>
- #include <string.h>
- #define ENCODE_BUFSZ 1000
- #define FMT_STR(evil_, norm_) (evil_mode ? evil_ : norm_)
- static int evil_mode = 0;
- void
- write_output(unsigned long *output, size_t len)
- {
- if(len)
- printf(FMT_STR("%lu", "0x%lx"), output[0]);
- for(int i = sizeof (long), j = 1; i < len; j++, i += sizeof (long))
- printf(FMT_STR(", %lu", ", 0x%lx"), output[j]);
- }
- void
- encode(char *msg)
- {
- unsigned long output[ENCODE_BUFSZ] = {0};
- memcpy(output, msg, strlen(msg));
- printf("unsigned long msg[] = { ");
- write_output(output, strlen(msg));
- printf(" }; // %s\n", msg);
- }
- int
- usage(void)
- {
- fputs( "usage: ./a.out [options] \"string\" [string2 string3 ...]\n"
- "options:\n"
- " -e activate evil mode, which prints output in base 10.\n\n"
- "example: ./a.out \"Hello World\" \"Goodbye World\" I\\\'m On Separate Lines\n", stderr);
- return 1;
- }
- void
- parse_arg(char *arg)
- {
- if(!strcmp(arg, "-e"))
- evil_mode = 1;
- else
- encode(arg);
- }
- int
- parse_args(char **args)
- {
- if(!*args)
- return usage();
- while(*args)
- parse_arg(*(args++));
- return 0;
- }
- int
- main(int argc, char **argv)
- {
- return parse_args(argv + 1);
- }
Add Comment
Please, Sign In to add comment