beta-0.89.2
[luatex.git] / source / libs / zziplib / zziplib-0.13.62 / bins / zzxorcopy.c
blob1c06e534126700bfa006c0737230fc419aecc8ed
1 /*
2 * Copyright (c) 2000,2001,2002 Guido Draheim <guidod@gmx.de>
3 * Use freely under the restrictions of the ZLIB License
5 * show a simple program to add xor-obfuscation.
6 * look at the resulting file with zzxordir an zzxorcat
7 * Remember that xor'ing data twice will result in the original data.
8 * This file has no dependency with zziplib - it's freestanding.
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
16 #include <zzip/_config.h> /* for ZZIP_VERSION */
18 #ifndef O_BINARY
19 #define O_BINARY 0
20 #endif
22 #if __STDC_VERSION__+0 < 199901
23 #define _ssize_t int
24 #define _size_t unsigned
25 #else
26 #define _ssize_t ssize_t
27 #define _size_t size_t
28 #endif
30 static const char usage[] =
32 " zzxopy [-#] <input-file> <output-file> \n"
33 " copies data from input-file to output-file adding simple \n"
34 " obfuscation by xor-ing each byte with the numeric value given. \n"
35 " the default xor-value is 0x55. Remember that copying data twice \n"
36 " with the same xor-value will result in the original file data. \n"
39 static int xor_value;
41 static _ssize_t xor_read (FILE* f, void* p, _size_t l)
43 _ssize_t r = fread(p, 1, l, f);
44 _ssize_t x; char* q; for (x=0, q=p; x < r; x++) q[x] ^= xor_value;
45 return r;
48 int
49 main (int argc, char ** argv)
51 int argn;
52 xor_value = 0x55;
54 if (argc <= 1 || ! strcmp (argv[1], "--help"))
56 printf (usage);
57 return 0;
59 if (! strcmp (argv[1], "--version"))
61 printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
62 return 0;
65 for (argn=1; argn < argc; argn++)
67 FILE* iF = 0;
68 FILE* oF = 0;
70 if (argv[argn][0] == '-')
72 if (isdigit(argv[argn][1])) xor_value = atoi (argv[argn]+1);
73 continue;
76 if (argn + 1 >= argc)
78 printf (usage);
79 exit (1);
82 iF = fopen (argv[argn], "rb");
83 if (! iF) { perror (argv[argn]); exit (2); }
84 argn++;
85 oF = fopen (argv[argn], "wb");
86 if (! oF) { perror (argv[argn]); fclose(iF); exit (3); }
89 char buf[17];
90 _ssize_t n;
92 /* read chunks of 16 bytes into buf and print them to stdout */
93 while (0 < (n = xor_read(iF, buf, 16)))
95 buf[n] = '\0';
96 n = fwrite (buf, 1, n, oF);
97 if (n < 0) break;
100 if (n < 0 && ferror (iF))
101 perror (argv[argn]);
105 return 0;
109 * Local variables:
110 * c-file-style: "stroustrup"
111 * End: