File: file_change_bytes.c

package info (click to toggle)
libqb 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,092 kB
  • sloc: ansic: 22,191; sh: 5,232; makefile: 607
file content (143 lines) | stat: -rw-r--r-- 3,101 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Jan Friesse <jfriesse@redhat.com>
 *
 * libqb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * libqb 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>

#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

static void usage(void)
{
	printf("Usage: [ -i input_file] [ -o output_file ] [ -n no_bytes]\n");
	printf("Changes no_bytes (default 1024) in input_file (default = - = stdin) and store\n");
	printf("result to output_file (default = - = stdout). It's possible to use same file\n");
	printf("as both input and output\n");

	exit(1);
}

static void init_rand(void) {
	unsigned int init_v;

	init_v = time(NULL) + getpid();

	srand(init_v);
}

int main(int argc, char *argv[])
{
	FILE *fi, *fo;
	int i;
	const char *input_file_name;
	const char *output_file_name;
	int no_bytes;
	char *ep;
	int ch;
	unsigned char *data;
	size_t data_size;
	size_t data_pos;
	size_t input_data_size;
	unsigned char buf[1024];

	input_file_name = "-";
	output_file_name = "-";
	no_bytes = 1024;

	while ((ch = getopt(argc, argv, "hi:o:n:")) != -1) {
		switch (ch) {
		case 'i':
			input_file_name = optarg;
			break;
		case 'n':
			no_bytes = strtol(optarg, &ep, 10);
			if (no_bytes < 0 || *ep != '\0') {
				warnx("illegal number -- %s", argv[2]);
				usage();
			}
			break;
		case 'o':
			output_file_name = optarg;
			break;
		case 'h':
		case '?':
		default:
			usage();
			/* NOTREACHED */
		}
	}

	if (strcmp(input_file_name, "-") == 0) {
		fi = stdin;
	} else {
		fi = fopen(input_file_name, "rb");
		if (fi == NULL) {
			err(1, "%s", input_file_name);
		}
	}

	/*
	 * Open and fully read input file
	 */
	data = NULL;
	data_size = 0;
	data_pos = 0;
	while ((input_data_size = fread(buf, 1, sizeof(buf), fi)) != 0) {
		if (data_pos + input_data_size >= data_size) {
			data_size = (data_size + input_data_size) * 2;
			assert((data = realloc(data, data_size)) != NULL);
		}
		memcpy(data + data_pos, buf, input_data_size);
		data_pos += input_data_size;
	}
	fclose(fi);

	/*
	 * Change bytes
	 */
	init_rand();

	for (i = 0; i < no_bytes; i++) {
		data[rand() % data_pos] = rand();
	}

	/*
	 * Fully write ouput file
	 */
	if (strcmp(output_file_name, "-") == 0) {
		fo = stdout;
	} else {
		fo = fopen(output_file_name, "wb");
		if (fo == NULL) {
			err(1, "%s", output_file_name);
		}
	}
	assert(fwrite(data, 1, data_pos, fo) == data_pos);
	fclose(fo);

	free(data);

	return (0);
}