| 1 |
/* |
| 2 |
* Copyright (c) 2002 Mike Nordell |
| 3 |
* portions Copyright (c) 2000,2001,2002 Guido Draheim <[email protected]> |
| 4 |
* Use freely under the restrictions of the ZLIB License |
| 5 |
* |
| 6 |
* A small example that displays how the plugin I/O functions can be used |
| 7 |
* to read "encrypted" zip archives. |
| 8 |
*/ |
| 9 |
|
| 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> |
| 16 |
|
| 17 |
#ifndef O_BINARY |
| 18 |
#define O_BINARY 0 |
| 19 |
#endif |
| 20 |
|
| 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 |
| 27 |
|
| 28 |
#ifdef _MSC_VER |
| 29 |
#define _MSC_VER_NULL NULL |
| 30 |
#else |
| 31 |
#define _MSC_VER_NULL |
| 32 |
#endif |
| 33 |
|
| 34 |
/* |
| 35 |
* Only override our the read handler. Let the system take care |
| 36 |
* the rest. |
| 37 |
*/ |
| 38 |
|
| 39 |
static zzip_ssize_t our_read(int fd, void* buf, zzip_size_t len) |
| 40 |
{ |
| 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; |
| 46 |
} |
| 47 |
return bytes; |
| 48 |
} |
| 49 |
|
| 50 |
static zzip_plugin_io_handlers our_handlers = { _MSC_VER_NULL }; |
| 51 |
static const char* const our_fileext [] = { ".dat", ".sav", 0 }; |
| 52 |
|
| 53 |
|
| 54 |
static const char usage[] = |
| 55 |
{ |
| 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" |
| 64 |
}; |
| 65 |
|
| 66 |
int |
| 67 |
main (int argc, char* argv[]) |
| 68 |
{ |
| 69 |
if (argc <= 1 || argc > 3 || ! strcmp (argv[1], "--help")) |
| 70 |
{ |
| 71 |
printf (usage); |
| 72 |
return 0; |
| 73 |
} |
| 74 |
if (! strcmp (argv[1], "--version")) |
| 75 |
{ |
| 76 |
printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n"); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
if (strlen(argv[1]) > 128) { |
| 81 |
fprintf(stderr, "Please provide a filename shorter than 128 chars.\n"); |
| 82 |
exit(1); |
| 83 |
} |
| 84 |
|
| 85 |
/* obfuscate the file */ |
| 86 |
{ |
| 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); |
| 94 |
} |
| 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); |
| 99 |
} |
| 100 |
while ((ch = fgetc(fin)) != EOF) { |
| 101 |
ch ^= 0x55; |
| 102 |
fputc(ch, fout); |
| 103 |
} |
| 104 |
fclose(fout); |
| 105 |
fclose(fin); |
| 106 |
} |
| 107 |
|
| 108 |
/* install our I/O hander */ |
| 109 |
zzip_init_io(&our_handlers, 0); |
| 110 |
our_handlers.fd.read = &our_read; |
| 111 |
|
| 112 |
{ |
| 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"); |
| 120 |
} |
| 121 |
fp = zzip_open_ext_io (name, O_RDONLY|O_BINARY, ZZIP_PREFERZIP, |
| 122 |
our_fileext, &our_handlers); |
| 123 |
|
| 124 |
if (! fp) |
| 125 |
{ |
| 126 |
perror (name); |
| 127 |
exit(1); |
| 128 |
}else{ |
| 129 |
char buf[17]; |
| 130 |
int n; |
| 131 |
|
| 132 |
/* read chunks of 16 bytes into buf and print them to stdout */ |
| 133 |
while (0 < (n = zzip_read(fp, buf, 16))) |
| 134 |
{ |
| 135 |
buf[n] = '\0'; |
| 136 |
# ifdef STDOUT_FILENO |
| 137 |
write (STDOUT_FILENO, buf, n); |
| 138 |
# else |
| 139 |
fwrite (buf, 1, n, stdout); |
| 140 |
# endif |
| 141 |
} |
| 142 |
|
| 143 |
if (n == -1) |
| 144 |
perror (argv[argn]); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* Local variables: |
| 153 |
* c-file-style: "stroustrup" |
| 154 |
* End: |
| 155 |
*/ |