which included commits to RCS files with non-trunk default branches.
--- /dev/null
+/*
+ * Copyright (c) 2002 Mike Nordell
+ * portions Copyright (c) 2000,2001,2002 Guido Draheim <guidod@gmx.de>
+ * Use freely under the restrictions of the ZLIB License
+ *
+ * A small example that displays how the plugin I/O functions can be used
+ * to read "encrypted" zip archives.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <zzip/zzip.h>
+#include <zzip/plugin.h>
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+#if defined ZZIP_HAVE_UNISTD_H
+#include <unistd.h> /* read */
+#elif defined ZZIP_HAVE_IO_H
+#include <io.h> /* win32 */
+#endif
+
+/*
+ * Only override our the read handler. Let the system take care
+ * the rest.
+ */
+
+static zzip_ssize_t our_read(int fd, void* buf, zzip_size_t len)
+{
+ const zzip_ssize_t bytes = read(fd, buf, len);
+ zzip_ssize_t i;
+ char* pch = (char*)buf;
+ for (i=0; i<bytes; ++i) {
+ pch[i] ^= 0x55;
+ }
+ return bytes;
+}
+
+static zzip_plugin_io_handlers our_handlers = { };
+static const char* const our_fileext [] = { ".dat", ".sav", 0 };
+
+
+static const char usage[] =
+{
+ " zzobfuscated <file> [in-zip filename]\n"
+ " - Demonstrates the use of installable file I/O handlers.\n"
+ " Copies <file> to \"obfuscated[.dat]\" while \"encrypting\" it by xor'ing\n"
+ " every byte with 0x55, installs file I/O handlers, and then uses the\n"
+ " zzip_open_ext_io function to read and print the file to stdout.\n"
+ " The file can be a normal file or an inflated part of a zip-archive,\n"
+ " to get 'README' from test.zip you may write \n"
+ " zzobfuscated test.zip README \n"
+};
+
+int
+main (int argc, char* argv[])
+{
+ if (argc <= 1 || argc > 3)
+ {
+ printf (usage);
+ exit (0);
+ }
+
+ if (strlen(argv[1]) > 128) {
+ fprintf(stderr, "Please provide a filename shorter than 128 chars.\n");
+ exit(1);
+ }
+
+ /* obfuscate the file */
+ {
+ int ch;
+ FILE* fin;
+ FILE* fout;
+ fin = fopen(argv[1], "rb");
+ if (!fin) {
+ fprintf(stderr, "Can't open input file \"%s\"\n", argv[1]);
+ exit(1);
+ }
+ fout = fopen((argc == 2) ? "obfuscated" : "obfuscated.dat", "wb");
+ if (!fout) {
+ fprintf(stderr, "Can't open output file \"obfuscated\"\n");
+ exit(1);
+ }
+ while ((ch = fgetc(fin)) != EOF) {
+ ch ^= 0x55;
+ fputc(ch, fout);
+ }
+ fclose(fout);
+ fclose(fin);
+ }
+
+ /* install our I/O hander */
+ zzip_init_io(&our_handlers, 0);
+ our_handlers.fd.read = &our_read;
+
+ {
+# define argn 2
+ ZZIP_FILE* fp;
+ char name[256];
+ if (argc == 3) {
+ sprintf(name, "obfuscated/%s", argv[argn]);
+ } else {
+ sprintf(name, "obfuscated");
+ }
+ fp = zzip_open_ext_io (name, O_RDONLY|O_BINARY, ZZIP_PREFERZIP,
+ our_fileext, &our_handlers);
+
+ if (! fp)
+ {
+ perror (name);
+ exit(1);
+ }else{
+ char buf[17];
+ int n;
+
+ /* read chunks of 16 bytes into buf and print them to stdout */
+ while (0 < (n = zzip_read(fp, buf, 16)))
+ {
+ buf[n] = '\0';
+# ifdef STDOUT_FILENO
+ write (STDOUT_FILENO, buf, n);
+# else
+ fwrite (buf, 1, n, stdout);
+# endif
+ }
+
+ if (n == -1)
+ perror (argv[argn]);
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Local variables:
+ * c-file-style: "stroustrup"
+ * End:
+ */
--- /dev/null
+/*
+ * Copyright (c) 2000,2001,2002 Guido Draheim <guidod@gmx.de>
+ * Use freely under the restrictions of the ZLIB license.
+ *
+ * show zip-reading with xor-obfuscation.
+ * Note that the difference to the standard zzdir.c is quite small.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#include <zzip/zzip.h>
+#include <zzip/plugin.h>
+
+#if defined ZZIP_HAVE_UNISTD_H
+#include <unistd.h>
+#elif defined ZZIP_HAVE_IO_H
+#include <io.h>
+#else
+#error need posix io for this example
+#endif
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+static const char usage[] =
+{
+ "zzdir <dir>.. \n"
+ " - prints a content table to stdout, but the dir can also be a zip-arch."
+ "\n"
+ " the file is part of an inflated zip-archive obfuscated with xor value,\n"
+ " given by the numeric option (default is 0x55). \n"
+ "\n"
+ " To show the contents of a zip-archive named 'test.zip', you may write \n"
+ " zzdir test \n"
+};
+
+static int xor_value;
+
+static zzip_ssize_t xor_read (int f, void* p, zzip_size_t l)
+{
+ zzip_ssize_t r = read(f, p, l);
+ zzip_ssize_t x; char* q; for (x=0, q=p; x < r; x++) q[x] ^= xor_value;
+ return r;
+}
+
+static zzip_plugin_io_handlers xor_handlers = { };
+static zzip_strings_t xor_fileext[] = { ".dat", "", 0 };
+
+int
+main (int argc, char ** argv)
+{
+ int argn;
+ int exitcode = 0;
+ xor_value = 0x55;
+
+ if (argc <= 1)
+ {
+ printf (usage);
+ exit(0);
+ }
+
+ zzip_init_io (&xor_handlers, 0); xor_handlers.fd.read = &xor_read;
+
+ for (argn=1; argn < argc; argn++)
+ {
+ ZZIP_DIR * dir;
+ ZZIP_DIRENT * d;
+
+ if (argv[argn][0] == '-')
+ {
+ if (isdigit(argv[argn][1])) xor_value = atoi (argv[argn]+1);
+ continue;
+ }
+
+ dir = zzip_opendir_ext_io(argv[argn],
+ ZZIP_ONLYZIP, xor_fileext, &xor_handlers);
+ if (! dir)
+ {
+ fprintf (stderr, "did not open %s: ", argv[argn]);
+ perror(argv[argn]);
+ exitcode++;
+ continue;
+ }
+
+ if (argc > 2) printf ("%s: \n", argv[argn]);
+
+ /* read each dir entry and show one line of info per file */
+ while ((d = zzip_readdir (dir)))
+ {
+ /* orignalsize / compression-type / compression-ratio / filename */
+ if (d->st_size > 999999)
+ {
+ printf ("%5dK %-9s %2d%% %s \n",
+ d->st_size>>10,
+ zzip_compr_str(d->d_compr),
+ 100 - (d->d_csize|1)/((d->st_size/100)|1),
+ d->d_name);
+ }else{
+ printf ("%6d %-9s %2d%% %s \n",
+ d->st_size,
+ zzip_compr_str(d->d_compr),
+ 100 - (d->d_csize|1)*100/(d->st_size|1),
+ d->d_name);
+ }
+ }
+
+ zzip_closedir(dir);
+ }
+
+ return exitcode;
+}
+
+/*
+ * Local variables:
+ * c-file-style: "stroustrup"
+ * End:
+ */
--- /dev/null
+/*
+ * Author:
+ * Guido Draheim <guidod@gmx.de>
+ *
+ * Copyright (c) 2002,2003 Guido Draheim
+ * All rights reserved
+ * use under the restrictions of the
+ * Lesser GNU General Public License
+ * or alternatively the restrictions
+ * of the Mozilla Public License 1.1
+ *
+ * the interfaces for the plugin_io system
+ *
+ * Using the following you can provide your own file I/O functions to
+ * e.g. read data directly from memory, provide simple
+ * "encryption"/"decryption" of on-disk .zip-files...
+ * Note that this currently only provides a subset of the functionality
+ * in zziplib. It does not attempt to provide any directory functions,
+ * but if your program 1) only uses ordinary on-disk files and you
+ * just want this for file obfuscation, or 2) you only access your
+ * .zip archives using zzip_open & co., this is sufficient.
+ *
+ * Currently the default io are the POSIX functions, except
+ * for 'filesize' that is zziplibs own provided zzip_filesize function,
+ * using standard POSIX fd's. You are however free to replace this with
+ * whatever data type you need, so long as you provide implementations
+ * for all the functions, and the data type fits an int.
+ *
+ * all functions receiving ext_io are able to cope with both arguments
+ * set to zero which will let them default to a ZIP ext and posix io.
+ */
+#ifndef _ZZIP_PLUGIN_H /* zzip-io.h */
+#define _ZZIP_PLUGIN_H 1
+
+#include <zzip/zzip.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* we have renamed zzip_plugin_io.use_mmap to zzip_plugin_io.sys */
+#define ZZIP_PLUGIN_IO_SYS 1
+
+struct zzip_plugin_io { /* use "zzip_plugin_io_handlers" in applications !! */
+ int (*open)(zzip_char_t* name, int flags, ...);
+ int (*close)(int fd);
+ zzip_ssize_t (*read)(int fd, void* buf, zzip_size_t len);
+ zzip_off_t (*seeks)(int fd, zzip_off_t offset, int whence);
+ zzip_off_t (*filesize)(int fd);
+ long sys;
+ long type;
+ zzip_ssize_t (*write)(int fd, _zzip_const void* buf, zzip_size_t len);
+} fd;
+
+typedef union _zzip_plugin_io
+{
+ struct zzip_plugin_io fd;
+ struct { void* padding[8]; } ptr;
+} zzip_plugin_io_handlers;
+
+#define _zzip_plugin_io_handlers zzip_plugin_io_handlers
+/* for backward compatibility, and the following to your application code:
+ * #ifndef _zzip_plugin_io_handlers
+ * #define _zzip_plugin_io_handlers struct zzip_plugin_io
+ */
+typedef zzip_plugin_io_handlers* zzip_plugin_io_handlers_t;
+
+#ifdef ZZIP_LARGEFILE_RENAME
+#define zzip_filesize zzip_filesize64
+#define zzip_get_default_io zzip_get_default_io64
+#define zzip_init_io zzip_init_io64
+#endif
+
+_zzip_export zzip_off_t
+zzip_filesize(int fd);
+
+/* get the default file I/O functions */
+_zzip_export zzip_plugin_io_t zzip_get_default_io(void);
+
+/*
+ * Initializes a zzip_plugin_io_t to the zziplib default io.
+ * This is useful if you only want to override e.g. the 'read' function.
+ * all zzip functions that can receive a zzip_plugin_io_t can
+ * handle a zero pointer in that place and default to posix io.
+ */
+_zzip_export
+int zzip_init_io(zzip_plugin_io_handlers_t io, int flags);
+
+/* zzip_init_io flags : */
+# define ZZIP_IO_USE_MMAP 1
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif