File: zzobfuscated.c

package info (click to toggle)
zziplib 0.13.56-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,760 kB
  • ctags: 2,135
  • sloc: sh: 12,147; ansic: 7,392; perl: 2,826; python: 2,190; makefile: 968; sed: 44
file content (155 lines) | stat: -rw-r--r-- 3,821 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 *	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 */
#else
#endif

#ifdef _MSC_VER
#define _MSC_VER_NULL NULL
#else
#define _MSC_VER_NULL
#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 = { _MSC_VER_NULL };
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 || ! strcmp (argv[1], "--help"))
    {
        printf (usage);
        return 0;
    }
    if (! strcmp (argv[1], "--version"))
    {
	printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
	return 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:
 */