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; 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 int fd, i, times; 79 char c; 80 81 times = 1; 82 while ((c = getopt(argc, argv, "n:")) != -1) { 83 switch (c) { 84 case 'n': 85 times = atoi(optarg); 86 break; 87 case '?': 88 default: 89 fprintf(stderr, 90 "Usage: %s [ -n <num> <file>]\n", 91 argv[0]); 92 exit(1); 93 } 94 } 95 argc -= optind; 96 argv += optind; 97 98 if (argc != 1) { 99 fprintf(stderr, "Missing file name\n"); 100 exit(1); 101 } 102 103 if ((fd = open(argv[0], O_RDWR)) == -1) 104 err(1, "open(%s)", argv[0]); 105 106 if (fstat(fd, &st) == -1) 107 err(1, "stat %s", argv[0]); 108 109 for (i = 0; i < times; i++) { 110 pos = arc4random() % st.st_size; 111 if (lseek(fd, pos, SEEK_SET) == -1) 112 err(1, "lseek()"); 113 read(fd, &c, 1); 114 flip(&c, 1); 115 if (lseek(fd, pos, SEEK_SET) == -1) 116 err(1, "lseek()"); 117 write(fd, &c, 1); 118 } 119 120 return (0); 121 } 122