summaryrefslogtreecommitdiff
path: root/src/corrupt_mpeg2.c
blob: 86a71c6d9bdfe9a20f7a1e00b1daec0b0cc8956a (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * corrupt_mpeg2.c
 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
 * See http://libmpeg2.sourceforge.net/ for updates.
 *
 * mpeg2dec is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mpeg2dec is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#ifdef HAVE_IO_H
#include <fcntl.h>
#include <io.h>
#endif
#include <inttypes.h>

static FILE * in_file;
static FILE * seed_file;
static int seed_loaded = 0;
static uint32_t rsl[55];
static int rsl_i = -1;

typedef struct {
    uint32_t p, q[8], r;
} randbyte_t;

#define CORRUPT_RANDOM 0
#define CORRUPT_VALUE 1

typedef struct corrupt_s {
    int type;
    int chunk_start, chunk_stop;
    int bit_start, bit_stop;
    union {
	randbyte_t prob;
    } u;
    struct corrupt_s * next;
    uint8_t mask;
} corrupt_t;

#define CORRUPT_LIST_SIZE 10
static corrupt_t corrupt_list[10];
static int corrupt_list_index = 0;
static corrupt_t * corrupt_head = NULL;
static int current_chunk = -1, current_bit = 0, target_bit = 0x7fffffff;

static inline uint32_t fastrand (void)
{
    if (++rsl_i == 55) rsl_i = 0;
    return rsl[rsl_i] += rsl[(rsl_i < 31) ? rsl_i + 24 : rsl_i - 31];
}

static uint32_t clip (double p)
{
    return (p < 0) ? 0 : ((p >= 1) ? 0xffffffff : (uint32_t)(p*4294967296.0));
}

static void randbyte_init (double p, randbyte_t * rnd)
{
    double q, r;
    int i;

    rnd->p = clip (p);
    r = 1;
    for (i = 0; i < 8; i++) {
	r *= 1 - p;
	q = p / (1 - r);
	rnd->q[i] = clip (q);
    }
    rnd->r = clip (1 - r);
}

static inline uint8_t randbyte (const randbyte_t * const rnd)
{
    int i, j;

    if (fastrand () > rnd->r || rnd->r == 0)
	return 0;

    i = 7; j = 0;
    do
	if (fastrand () <= (j ? rnd->p : rnd->q[i]))
	    j |= 1 << i;
    while (i--);
    return j;
}

static void print_usage (char ** argv)
{
    fprintf (stderr, "usage: %s [-h] [-l <seed>] [-s <seedfile>] \\\n"
	     "\t\t[-r prob[,restrict] [-v prob[,restrict]] <file>\n"
	     "\t-h\tdisplay help\n"
	     "\t-l load seed\n"
	     "\t-s save seed file\n"
	     "\t-r random corruption\n"
	     "\t-v random value\n"
	     "restrict: chunk[-endchunk][,bit[-endbit]]\n", argv[0]);

    exit (1);
}

static void corrupt_arg (corrupt_t * corrupt, int type, char * s, char ** argv)
{
    corrupt->type = type;
    if (! *s)
	s = (char *)",0-0xff,0-";
    else if (*s != ',' || !isdigit (s[1]))
	print_usage (argv);
    corrupt->chunk_start = strtol (s + 1, &s, 0);
    if (*s != '-')
	corrupt->chunk_stop = corrupt->chunk_start;
    else if (isdigit (* ++s))
	corrupt->chunk_stop = strtol (s, &s, 0);
    else
	print_usage (argv);
    if (! *s)
	s = (char *)",32-";
    else if (*s != ',' || !isdigit (s[1]))
	print_usage (argv);
    corrupt->bit_start = strtol (s + 1, &s, 0);
    if (*s != '-')
	corrupt->bit_stop = corrupt->bit_start;
    else if (isdigit (* ++s))
	corrupt->bit_stop = strtol (s, &s, 0);
    else
	corrupt->bit_stop = 0x7ffffffe;
    if (corrupt->chunk_start < 0 ||
	corrupt->chunk_start > corrupt->chunk_stop ||
	corrupt->chunk_stop >= 0x1000 ||
	corrupt->bit_start < 0 || corrupt->bit_start > corrupt->bit_stop || *s)
	print_usage (argv);
    if (corrupt->chunk_stop < 0x100) {
	corrupt->chunk_start <<= 4;
	corrupt->chunk_stop = (corrupt->chunk_stop << 4) | 0xf;
    }
}

static void handle_args (int argc, char ** argv)
{
    int c;
    double prob;
    char * s;
    corrupt_t * corrupt;

    while ((c = getopt (argc, argv, "hl:s:r:v:")) != -1)
	switch (c) {
	case 'l':
	    if (seed_file || seed_loaded)
		print_usage (argv);
	    if (sscanf (optarg, "%08x%08x%08x%08x",
			rsl, rsl+1, rsl+2, rsl+3) != 4)
		print_usage (argv);
	    seed_loaded = 1;
	    break;

	case 's':
	    if (seed_file || seed_loaded)
		print_usage (argv);
	    seed_file = fopen (optarg, "wt");
	    if (!seed_file)
		print_usage (argv);
	    break;

	case 'r':
	    prob = strtod (optarg, &s);
	    if (prob < 0 || prob > 1 || 
		corrupt_list_index == CORRUPT_LIST_SIZE)
		print_usage (argv);
	    corrupt = corrupt_list + corrupt_list_index++;
	    corrupt_arg (corrupt, CORRUPT_RANDOM, s, argv);
	    randbyte_init (prob, &corrupt->u.prob);
	    break;

	case 'v':
	    prob = strtod (optarg, &s);
	    if (prob < 0 || prob > 1 || 
		corrupt_list_index == CORRUPT_LIST_SIZE)
		print_usage (argv);
	    corrupt = corrupt_list + corrupt_list_index++;
	    corrupt_arg (corrupt, CORRUPT_VALUE, s, argv);
	    randbyte_init (prob, &corrupt->u.prob);
	    break;

	default:
	    print_usage (argv);
	}

    if (optind < argc) {
	in_file = fopen (argv[optind], "rb");
	if (!in_file) {
	    fprintf (stderr, "%s - could not open file %s\n", strerror (errno),
		     argv[optind]);
	    exit (1);
	}
    } else
	in_file = stdin;

    if (!seed_file && !seed_loaded)
	seed_file = fopen ("seed", "wt");
}

static void update_corrupt_list (void)
{
    corrupt_t * corrupt;
    corrupt_t ** corrupt_link;

    corrupt_link = &corrupt_head;
    target_bit = 0x7fffffff;
    for (corrupt = corrupt_list;
	 corrupt < corrupt_list + corrupt_list_index; corrupt++)
	if (corrupt->chunk_start <= current_chunk &&
	    corrupt->chunk_stop >= current_chunk &&
	    corrupt->bit_stop >= current_bit) {
	    if (corrupt->bit_start >= current_bit + 8) {
		if (corrupt->bit_start <= target_bit)
		    target_bit = corrupt->bit_start & ~7;
	    } else {
		*corrupt_link = corrupt;
		corrupt_link = &corrupt->next;
		if (corrupt->bit_stop >= current_bit + 7) {
		    corrupt->mask = 0xff;
		    if (corrupt->bit_stop <= target_bit)
			target_bit = (corrupt->bit_stop + 1) & ~7;
		} else {
		    corrupt->mask = -1 << (7 - (corrupt->bit_stop & 7));
		    target_bit = current_bit + 8;
		}
		if (corrupt->bit_start > current_bit) {
		    corrupt->mask &= 0xff >> (corrupt->bit_start & 7);
		    target_bit = current_bit + 8;
		}
	    }
	}
    *corrupt_link = NULL;
}

static void corrupt (uint8_t * ptr)
{
    corrupt_t * corrupt_ptr;

    if (ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 1) {
	current_chunk = (ptr[3] << 4) | (ptr[4] >> 4);
	current_bit = 0;
	update_corrupt_list ();
    } else if (current_bit == target_bit)
	update_corrupt_list ();

    current_bit += 8;

    for (corrupt_ptr = corrupt_head; corrupt_ptr; corrupt_ptr = corrupt_ptr->next)
	switch (corrupt_ptr->type) {
	case CORRUPT_RANDOM:
	    *ptr ^= randbyte (&corrupt_ptr->u.prob) & corrupt_ptr->mask;
	    break;
	case CORRUPT_VALUE:
	    *ptr = ((*ptr & ~corrupt_ptr->mask) |
		    (randbyte (&corrupt_ptr->u.prob) & corrupt_ptr->mask));
	    break;
	}
}

static void corrupt_loop (void)
{
#define BUFFER_SIZE 4096
    static uint8_t buffer1[BUFFER_SIZE + 4];
    static uint8_t buffer2[BUFFER_SIZE + 4];
    static uint8_t terminator[4] = {0xff, 0xff, 0xff, 0xff};

    uint8_t * buf;
    uint8_t * end;
    uint8_t * current;

    buf = buffer1;
    end = buf + fread (buf, 1, BUFFER_SIZE, in_file);

    while (end == buf + BUFFER_SIZE) {
	uint8_t * lastbuf;
	uint8_t * lastbuf_end;

	lastbuf = buf;	lastbuf_end = buf + BUFFER_SIZE;
	buf = (buf == buffer1) ? buffer2 : buffer1;
	memcpy (buf, terminator, 4);
	end = buf + fread (buf, 1, BUFFER_SIZE, in_file);
	memcpy (lastbuf_end, buf, 4);
	for (current = lastbuf; current < lastbuf_end; current++)
	    corrupt (current);
	fwrite (lastbuf, BUFFER_SIZE, 1, stdout);
    }

    memcpy (end, terminator, 4);
    for (current = buf; current < end; current++)
	corrupt (current);
    fwrite (buf, end - buf, 1, stdout);
}

int main (int argc, char ** argv)
{
    int i;

#ifdef HAVE_IO_H
    setmode (fileno (stdin), O_BINARY);
    setmode (fileno (stdout), O_BINARY);
#endif

    handle_args (argc, argv);

    if (!seed_loaded) {
	srand (time (NULL));
	for (i = 0; i < 4; i++)
	    rsl[i] = rand ();
	fprintf (seed_file, "%08x%08x%08x%08x\n",
		 rsl[0], rsl[1], rsl[2], rsl[3]);
	fclose (seed_file);
    }

    for (i = 4; i < 55; i++)
	rsl[i] = 0;
    for (i = 0; i < 1000; i++)
	fastrand ();

    corrupt_loop ();

    return 0;
}