tomb

the crypto undertaker
git clone git://parazyd.org/tomb.git
Log | Files | Refs | README | LICENSE

hexencode.c (951B)


      1 /*
      2  * A simple utility that reads from stdin and output the hexencoding (on a single line) of the input
      3  */
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <getopt.h>
      8 
      9 static int decode_mode = 0;
     10 int main(int argc, char *argv[]) {
     11 	char c;
     12 	char buf[3];
     13 	int read_bytes;
     14 	int opt;
     15 	static struct option long_options[] =
     16 	{
     17 		{"decode", no_argument, &decode_mode, 1},
     18 		{"encode", no_argument, &decode_mode, 0},
     19 		{0,0,0,0}
     20 	};
     21 	int option_index = 0;
     22 
     23 	while(1) {
     24 		option_index = 0;
     25 		opt = getopt_long(argc, argv, "", long_options, &option_index);
     26 		if(opt == -1)
     27 			break;
     28 		switch(opt) {
     29 			case 0:
     30 				break;
     31 			case '?':
     32 				return 127;
     33 			default:
     34 				abort();
     35 		}
     36 	}
     37 	if(decode_mode == 0) {
     38 		while(( c = (char)getchar() ) != EOF)
     39 			printf("%02x", c);
     40 		return 0;
     41 	} else {
     42 		while( (read_bytes=fread(buf, sizeof(char), 2, stdin)) != 0) {
     43 			if(read_bytes == 1) buf[1]='\0';
     44 			sscanf(buf, "%x", &c);
     45 			printf("%c", c);
     46 		}
     47 		return 0;
     48 	}
     49 }