Debian Codegraph @

libs/zziplib/zziplib-src/bins/zzxorcopy.c

raw
1 /*
2 * Copyright (c) 2000,2001,2002 Guido Draheim <[email protected]>
3 * Use freely under the restrictions of the ZLIB License
4 *
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 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 #include <zzip/_config.h> /* for ZZIP_PACKAGE_VERSION */
17
18 #if __STDC_VERSION__+0 < 199901
19 #define _ssize_t int
20 #define _size_t unsigned
21 #else
22 #define _ssize_t ssize_t
23 #define _size_t size_t
24 #endif
25
26 static const char usage[] =
27 {
28 " zzxopy [-#] <input-file> <output-file> \n"
29 " copies data from input-file to output-file adding simple \n"
30 " obfuscation by xor-ing each byte with the numeric value given. \n"
31 " the default xor-value is 0x55. Remember that copying data twice \n"
32 " with the same xor-value will result in the original file data. \n"
33 };
34
35 static int xor_value;
36
37 static _ssize_t xor_read (FILE* f, void* p, _size_t l)
38 {
39 _ssize_t r = fread(p, 1, l, f);
40 _ssize_t x; char* q; for (x=0, q=p; x < r; x++) q[x] ^= xor_value;
41 return r;
42 }
43
44 int
45 main (int argc, char ** argv)
46 {
47 int argn;
48 xor_value = 0x55;
49
50 if (argc <= 1 || ! strcmp (argv[1], "--help"))
51 {
52 printf (usage);
53 return 0;
54 }
55 if (! strcmp (argv[1], "--version"))
56 {
57 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
58 return 0;
59 }
60
61 for (argn=1; argn < argc; argn++)
62 {
63 FILE* iF = 0;
64 FILE* oF = 0;
65
66 if (argv[argn][0] == '-')
67 {
68 if (isdigit(argv[argn][1])) xor_value = atoi (argv[argn]+1);
69 continue;
70 }
71
72 if (argn + 1 >= argc)
73 {
74 printf (usage);
75 exit (1);
76 }
77
78 iF = fopen (argv[argn], "rb");
79 if (! iF) { perror (argv[argn]); exit (2); }
80 argn++;
81 oF = fopen (argv[argn], "wb");
82 if (! oF) { perror (argv[argn]); fclose(iF); exit (3); }
83
84 {
85 char buf[17];
86 _ssize_t n;
87
88 /* read chunks of 16 bytes into buf and print them to stdout */
89 while (0 < (n = xor_read(iF, buf, 16)))
90 {
91 buf[n] = '\0';
92 n = fwrite (buf, 1, n, oF);
93 if (n < 0) break;
94 }
95
96 if (n < 0 && ferror (iF))
97 perror (argv[argn]);
98 }
99 }
100
101 return 0;
102 }
103
104 /*
105 * Local variables:
106 * c-file-style: "stroustrup"
107 * End:
108 */