1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * Flip one or more bits in a file.
31 */
32
33#include <sys/types.h>
34#include <sys/stat.h>
35
36#include <err.h>
37#include <fcntl.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43extern char *optarg;
44extern int optind;
45
46static long
47random_long(long mi, long ma)
48{
49        return (arc4random()  % (ma - mi + 1) + mi);
50}
51
52static void
53flip(void *ap, size_t len)
54{
55	unsigned char *cp;
56	int byte;
57	unsigned char bit, buf, mask, old __unused;
58
59	cp = (unsigned char *)ap;
60	byte = random_long(0, len - 1);
61	bit = random_long(0,7);
62	mask = ~(1 << bit);
63	buf = cp[byte];
64	old = cp[byte];
65	buf = (buf & mask) | (~buf & ~mask);
66	cp[byte] = buf;
67#if defined(DEBUG)
68	printf("Change %2x to %2x at %d by flipping bit %d\n",
69	    old, buf, byte, bit);
70#endif
71}
72
73static void
74trash(char *c)
75{
76	if (arc4random() % 2 == 1)
77		*c = 0;
78	else
79		arc4random_buf(c, sizeof(c));
80}
81
82int
83main(int argc, char *argv[])
84{
85	struct stat st;
86	off_t pos;
87	size_t size;
88	int c, fd, i, times;
89
90	times = 1;
91	size = 0;
92	while ((c = getopt(argc, argv, "n:s:")) != -1) {
93		switch (c) {
94			case 'n':
95				times = atoi(optarg);
96				break;
97			case 's':
98				size = atol(optarg);
99				break;
100			case '?':
101			default:
102				fprintf(stderr,
103				    "Usage: %s [ -n <num> <file>]\n",
104				    argv[0]);
105				exit(1);
106		}
107	}
108	argc -= optind;
109	argv += optind;
110
111	if (argc != 1) {
112		fprintf(stderr, "Missing file name\n");
113		exit(1);
114	}
115
116	if ((fd = open(argv[0], O_RDWR)) == -1)
117		err(1, "open(%s)", argv[0]);
118
119	if (size == 0) {
120		if (fstat(fd, &st) == -1)
121			err(1, "stat %s", argv[0]);
122		if ((st.st_mode & S_IFREG) == 0)
123			errx(1, "%s must be a regular file\n", argv[0]);
124		size = st.st_size;
125	}
126
127	for (i = 0; i < times; i++) {
128		char ch;
129
130		pos = arc4random() % size;
131		if (lseek(fd, pos, SEEK_SET) == -1)
132			err(1, "lseek()");
133		if (read(fd, &ch, 1) != 1)
134			err(1, "read()");
135		if (arc4random() % 100 < 98)
136			flip(&ch, 1);
137		else
138			trash(&ch);
139		if (lseek(fd, pos, SEEK_SET) == -1)
140			err(1, "lseek()");
141		if (write(fd, &ch, 1) != 1)
142			err(1, "write()");
143	}
144
145	return (0);
146}
147