1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 43 extern char *optarg; 44 extern int optind; 45 46 static long 47 random_long(long mi, long ma) 48 { 49 return (arc4random() % (ma - mi + 1) + mi); 50 } 51 52 static void 53 flip(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); 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 73 int 74 main(int argc, char *argv[]) 75 { 76 struct stat st; 77 off_t pos; 78 size_t size; 79 int fd, i, times; 80 char c; 81 82 times = 1; 83 size = 0; 84 while ((c = getopt(argc, argv, "n:s:")) != -1) { 85 switch (c) { 86 case 'n': 87 times = atoi(optarg); 88 break; 89 case 's': 90 size = atol(optarg); 91 break; 92 case '?': 93 default: 94 fprintf(stderr, 95 "Usage: %s [ -n <num> <file>]\n", 96 argv[0]); 97 exit(1); 98 } 99 } 100 argc -= optind; 101 argv += optind; 102 103 if (argc != 1) { 104 fprintf(stderr, "Missing file name\n"); 105 exit(1); 106 } 107 108 if ((fd = open(argv[0], O_RDWR)) == -1) 109 err(1, "open(%s)", argv[0]); 110 111 if (size == 0) { 112 if (fstat(fd, &st) == -1) 113 err(1, "stat %s", argv[0]); 114 size = st.st_size; 115 } 116 117 for (i = 0; i < times; i++) { 118 pos = arc4random() % size; 119 if (lseek(fd, pos, SEEK_SET) == -1) 120 err(1, "lseek()"); 121 if (read(fd, &c, 1) != 1) 122 err(1, "read()"); 123 flip(&c, 1); 124 if (lseek(fd, pos, SEEK_SET) == -1) 125 err(1, "lseek()"); 126 if (write(fd, &c, 1) != 1) 127 err(1, "write()"); 128 } 129 130 return (0); 131 } 132