beta-0.89.2
[luatex.git] / source / libs / zziplib / zziplib-0.13.62 / bins / zzxorcat.c
blob97fcd51fd327dd78592104d04f82af01f26640c3
1 /*
2 * Copyright (c) 2000,2001,2002 Guido Draheim <guidod@gmx.de>
3 * Use freely under the restrictions of the ZLIB License
5 * show zip-reading with xor-obfuscation.
6 * Note that the difference to the standard zzcat.c is quite small.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <zzip/zzip.h>
14 #include <zzip/plugin.h>
16 #if defined ZZIP_HAVE_UNISTD_H
17 #include <unistd.h>
18 #elif defined ZZIP_HAVE_IO_H
19 #include <io.h>
20 #else
21 #error need posix io for this example
22 #endif
24 #ifndef O_BINARY
25 #define O_BINARY 0
26 #endif
28 static const char usage[] =
30 " zzxorcat [-#] <file>... \n"
31 " - prints the file to stdout, so you may want to redirect the output; \n"
32 " the file is part of an inflated zip-archive obfuscated with xor value,\n"
33 " given by the numeric option (default is 0x55). \n"
34 " to get 'README' from dist.dat you may write \n"
35 " zzcat dist/README \n"
38 static int xor_value;
40 static zzip_ssize_t xor_read (int f, void* p, zzip_size_t l)
42 zzip_ssize_t r = read(f, p, l);
43 zzip_ssize_t x; char* q; for (x=0, q=p; x < r; x++) q[x] ^= xor_value;
44 return r;
47 static zzip_plugin_io_handlers xor_handlers;
48 static zzip_strings_t xor_fileext[] = { ".dat", "", 0 };
50 int
51 main (int argc, char ** argv)
53 int argn;
54 xor_value = 0x55;
56 if (argc <= 1 || ! strcmp (argv[1], "--help"))
58 printf (usage);
59 return 0;
61 if (! strcmp (argv[1], "--version"))
63 printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
64 return 0;
67 zzip_init_io (&xor_handlers, 0); xor_handlers.fd.read = &xor_read;
69 for (argn=1; argn < argc; argn++)
71 ZZIP_FILE* fp;
73 if (argv[argn][0] == '-')
75 if (isdigit(argv[argn][1])) xor_value = atoi (argv[argn]+1);
76 continue;
79 fp = zzip_open_ext_io (argv[argn], O_RDONLY|O_BINARY,
80 ZZIP_CASELESS|ZZIP_ONLYZIP,
81 xor_fileext, &xor_handlers);
82 if (! fp)
84 perror (argv[argn]);
85 continue;
86 }else{
87 char buf[17];
88 zzip_ssize_t n;
90 /* read chunks of 16 bytes into buf and print them to stdout */
91 while (0 < (n = zzip_read(fp, buf, 16)))
93 buf[n] = '\0';
94 # ifdef STDOUT_FILENO
95 write (STDOUT_FILENO, buf, n);
96 # else
97 fwrite (buf, 1, n, stdout);
98 # endif
101 if (n == -1)
102 perror (argv[n]);
106 return 0;
110 * Local variables:
111 * c-file-style: "stroustrup"
112 * End: