2 * Copyright (c) 2002 Mike Nordell
3 * portions Copyright (c) 2000,2001,2002 Guido Draheim <guidod@gmx.de>
4 * Use freely under the restrictions of the ZLIB License
6 * A small example that displays how the plugin I/O functions can be used
7 * to read "encrypted" zip archives.
14 #include <zzip/zzip.h>
15 #include <zzip/plugin.h>
21 #if defined ZZIP_HAVE_UNISTD_H
22 #include <unistd.h> /* read */
23 #elif defined ZZIP_HAVE_IO_H
24 #include <io.h> /* win32 */
29 #define _MSC_VER_NULL NULL
35 * Only override our the read handler. Let the system take care
39 static zzip_ssize_t our_read(int fd, void* buf, zzip_size_t len)
41 const zzip_ssize_t bytes = read(fd, buf, len);
43 char* pch = (char*)buf;
44 for (i=0; i<bytes; ++i) {
50 static zzip_plugin_io_handlers our_handlers = { _MSC_VER_NULL };
51 static const char* const our_fileext [] = { ".dat", ".sav", 0 };
54 static const char usage[] =
56 " zzobfuscated <file> [in-zip filename]\n"
57 " - Demonstrates the use of installable file I/O handlers.\n"
58 " Copies <file> to \"obfuscated[.dat]\" while \"encrypting\" it by xor'ing\n"
59 " every byte with 0x55, installs file I/O handlers, and then uses the\n"
60 " zzip_open_ext_io function to read and print the file to stdout.\n"
61 " The file can be a normal file or an inflated part of a zip-archive,\n"
62 " to get 'README' from test.zip you may write \n"
63 " zzobfuscated test.zip README \n"
67 main (int argc, char* argv[])
69 if (argc <= 1 || argc > 3 || ! strcmp (argv[1], "--help"))
74 if (! strcmp (argv[1], "--version"))
76 printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
80 if (strlen(argv[1]) > 128) {
81 fprintf(stderr, "Please provide a filename shorter than 128 chars.\n");
85 /* obfuscate the file */
90 fin = fopen(argv[1], "rb");
92 fprintf(stderr, "Can't open input file \"%s\"\n", argv[1]);
95 fout = fopen((argc == 2) ? "obfuscated" : "obfuscated.dat", "wb");
97 fprintf(stderr, "Can't open output file \"obfuscated\"\n");
100 while ((ch = fgetc(fin)) != EOF) {
108 /* install our I/O hander */
109 zzip_init_io(&our_handlers, 0);
110 our_handlers.fd.read = &our_read;
117 sprintf(name, "obfuscated/%s", argv[argn]);
119 sprintf(name, "obfuscated");
121 fp = zzip_open_ext_io (name, O_RDONLY|O_BINARY, ZZIP_PREFERZIP,
122 our_fileext, &our_handlers);
132 /* read chunks of 16 bytes into buf and print them to stdout */
133 while (0 < (n = zzip_read(fp, buf, 16)))
136 # ifdef STDOUT_FILENO
137 write (STDOUT_FILENO, buf, n);
139 fwrite (buf, 1, n, stdout);
153 * c-file-style: "stroustrup"