beta-0.89.2
[luatex.git] / source / libs / zziplib / zziplib-0.13.62 / bins / zzobfuscated.c
blob352afc29a13f8c1766cdd9fa849dd89fc1dcc69b
1 /*
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.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <zzip/zzip.h>
15 #include <zzip/plugin.h>
17 #ifndef O_BINARY
18 #define O_BINARY 0
19 #endif
21 #if defined ZZIP_HAVE_UNISTD_H
22 #include <unistd.h> /* read */
23 #elif defined ZZIP_HAVE_IO_H
24 #include <io.h> /* win32 */
25 #else
26 #endif
28 #ifdef _MSC_VER
29 #define _MSC_VER_NULL NULL
30 #else
31 #define _MSC_VER_NULL
32 #endif
35 * Only override our the read handler. Let the system take care
36 * the rest.
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);
42 zzip_ssize_t i;
43 char* pch = (char*)buf;
44 for (i=0; i<bytes; ++i) {
45 pch[i] ^= 0x55;
47 return bytes;
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"
66 int
67 main (int argc, char* argv[])
69 if (argc <= 1 || argc > 3 || ! strcmp (argv[1], "--help"))
71 printf (usage);
72 return 0;
74 if (! strcmp (argv[1], "--version"))
76 printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
77 return 0;
80 if (strlen(argv[1]) > 128) {
81 fprintf(stderr, "Please provide a filename shorter than 128 chars.\n");
82 exit(1);
85 /* obfuscate the file */
87 int ch;
88 FILE* fin;
89 FILE* fout;
90 fin = fopen(argv[1], "rb");
91 if (!fin) {
92 fprintf(stderr, "Can't open input file \"%s\"\n", argv[1]);
93 exit(1);
95 fout = fopen((argc == 2) ? "obfuscated" : "obfuscated.dat", "wb");
96 if (!fout) {
97 fprintf(stderr, "Can't open output file \"obfuscated\"\n");
98 exit(1);
100 while ((ch = fgetc(fin)) != EOF) {
101 ch ^= 0x55;
102 fputc(ch, fout);
104 fclose(fout);
105 fclose(fin);
108 /* install our I/O hander */
109 zzip_init_io(&our_handlers, 0);
110 our_handlers.fd.read = &our_read;
113 # define argn 2
114 ZZIP_FILE* fp;
115 char name[256];
116 if (argc == 3) {
117 sprintf(name, "obfuscated/%s", argv[argn]);
118 } else {
119 sprintf(name, "obfuscated");
121 fp = zzip_open_ext_io (name, O_RDONLY|O_BINARY, ZZIP_PREFERZIP,
122 our_fileext, &our_handlers);
124 if (! fp)
126 perror (name);
127 exit(1);
128 }else{
129 char buf[17];
130 int n;
132 /* read chunks of 16 bytes into buf and print them to stdout */
133 while (0 < (n = zzip_read(fp, buf, 16)))
135 buf[n] = '\0';
136 # ifdef STDOUT_FILENO
137 write (STDOUT_FILENO, buf, n);
138 # else
139 fwrite (buf, 1, n, stdout);
140 # endif
143 if (n == -1)
144 perror (argv[argn]);
148 return 0;
152 * Local variables:
153 * c-file-style: "stroustrup"
154 * End: