lsflib/tools/dump.c
changeset 6 c3dc3eb3b541
child 77 49e0babccb23
equal deleted inserted replaced
5:d752cbe8208e 6:c3dc3eb3b541
       
     1 #include <stdio.h>
       
     2 #include <stdlib.h>
       
     3 #include <fcntl.h>
       
     4 #include <string.h>
       
     5 #include <unistd.h>
       
     6 #include <getopt.h>
       
     7 
       
     8 #include <bits.h>
       
     9 
       
    10 int main (int argc, char **argv)
       
    11 {
       
    12     int fd;;
       
    13     int len;
       
    14     int i;
       
    15     char buffer[81];
       
    16     char key = 0xf3;
       
    17 
       
    18     int use_BINARY, use_NOT, use_HEX, use_XOR;
       
    19 
       
    20     use_XOR = use_HEX = use_BINARY = use_NOT = *buffer = 0;
       
    21 
       
    22     while ((i = getopt (argc, argv, "bhnk:x?")) >= 0) {
       
    23         switch (i) {
       
    24         case 'b':
       
    25             use_BINARY = 1;
       
    26             break;
       
    27         case 'h':
       
    28             use_HEX = 1;
       
    29             break;
       
    30         case 'k':
       
    31             key = atoi (optarg);
       
    32             break;
       
    33         case 'n':
       
    34             use_NOT = 1;
       
    35             break;
       
    36         case 'x':
       
    37             use_XOR = 1;
       
    38             break;
       
    39         case '?':
       
    40             printf ("Usage: %s [ -b|-h|-n|-x|-? ] [ file ]\n", argv[0]);
       
    41             printf ("b: binary\nh: hex\nn: not\nx: xor\n");
       
    42             printf ("If no filename is given, stdin is assumed\n");
       
    43             return 0;
       
    44         }
       
    45     }
       
    46 
       
    47     if (optind == argc)
       
    48         fd = fileno (stdin);
       
    49     else {
       
    50         if ((fd = open (argv[optind], O_RDONLY)) == -1) {
       
    51             perror ("OPEN");
       
    52             return EXIT_SUCCESS;
       
    53         }
       
    54     }
       
    55     while ((len = read (fd, buffer, 1)) > 0) {
       
    56         buffer[len] = 0;
       
    57 
       
    58         if (use_BINARY && use_XOR && use_NOT) {
       
    59             bindump (not (xor (buffer, key)), BITS);
       
    60         } else if (use_BINARY && use_XOR) {
       
    61             bindump (xor (buffer, key), BITS);
       
    62         } else if (use_BINARY && use_NOT) {
       
    63             bindump (not (buffer), BITS);
       
    64         } else if (use_BINARY) {
       
    65             bindump (buffer, BITS);
       
    66 
       
    67         } else if (use_HEX && use_XOR && use_NOT) {
       
    68             hexdump (not (xor (buffer, key)), len);
       
    69         } else if (use_HEX && use_XOR) {
       
    70             hexdump (xor (buffer, key), len);
       
    71         } else if (use_HEX && use_NOT) {
       
    72             hexdump (not (buffer), len);
       
    73         } else if (use_HEX) {
       
    74             hexdump (buffer, len);
       
    75         } else if (use_XOR && use_NOT) {
       
    76             printf ("%s", not (xor (buffer, key)));
       
    77         } else if (use_XOR) {
       
    78             printf ("%s", xor (buffer, key));
       
    79         } else if (use_NOT) {
       
    80             printf ("%s", not (buffer));
       
    81         } else
       
    82             printf ("%s", buffer);
       
    83     }
       
    84 
       
    85     close (fd);
       
    86 
       
    87     return 0;
       
    88 }