-
Notifications
You must be signed in to change notification settings - Fork 19
/
flip.c
160 lines (142 loc) · 4.76 KB
/
flip.c
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
/* flip.c -- use the output of spoof to flip bits in a file
Copyright (C) 2021 Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Mark Adler
*/
/*
Apply the output of spoof, read from stdin, to the file whose path is the
first (and only) command-line argument.
Usage:
spoof < request | flip file
where request is the input to spoof (see spoof.c), and file is the file in
which to flip the bits chosen by spoof.
*/
#define _LARGEFILE_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/errno.h>
// Flip bit bit at position pos in file. This defers the read and write to the
// file until pos changes. Call with pos == -1 to do the last exclusive-or. bit
// will be ignored in that case. Return 0 on success, -1 if pos is past the end
// of the file, or a positive errno return value due to a file operation error.
static int flip(FILE *file, off_t pos, int bit) {
// Keep track of position and exclusive-ors until position changes.
static int xor = 0;
static off_t curr = -1;
// If the position changed, first execute the previous exclusive-or.
if (curr != pos && curr >= 0) {
int ret = fseeko(file, curr, SEEK_SET);
if (ret)
return errno;
int got = fgetc(file);
if (got == EOF)
return -1;
got ^= xor;
ret = fseeko(file, curr, SEEK_SET);
if (ret)
return errno;
ret = fputc(got, file);
if (ret == EOF)
return errno;
xor = 0;
}
curr = pos;
// If this is just a flush, then done.
if (pos < 0)
return 0;
// Accumulate exclusive-or bits.
xor |= 1 << bit;
return 0;
}
// Apply the bit flips read from stdin to file. Non-digit characters are
// ignored, except they are considered to terminate a decimal number. Return 0
// on success, -2 if a bit offset is out of range, or a flip() return value.
static int apply(FILE *file) {
int state = 0, bit, ch;
off_t pos;
do {
ch = getchar();
int dig = ch >= '0' && ch <= '9' ? ch - '0' : -1;
switch (state) {
case 0:
// looking for start of file position
if (dig != -1) {
pos = dig;
state = 1;
}
break;
case 1:
// in file position
if (dig != -1)
pos = 10 * pos + dig;
else
state = 2;
break;
case 2:
// looking for start of bit offset
if (dig != -1) {
bit = dig;
if (bit > 7)
return -2;
state = 3;
}
break;
case 3:
// in bit offset
if (dig != -1) {
bit = 10 * bit + dig;
if (bit > 7)
return -2;
}
else {
// flip bit bit at pos
int ret = flip(file, pos, bit);
if (ret)
return ret;
state = 0;
}
}
} while (ch != EOF);
// flush any remaining exclusive-ors
return flip(file, -1, 0);
}
// Update the file named on the command line with the output of spoof read from
// stdin.
int main(int argc, char **argv) {
if (argc != 2) {
fputs("* expecting one argument: the name of the file\n", stderr);
return 1;
}
FILE *file = fopen(argv[1], "r+b");
if (file == NULL) {
fprintf(stderr, "* could not open %s for reading and writing\n",
argv[1]);
return 1;
}
int ret = apply(file);
fclose(file);
if (ret) {
if (ret == -2)
fputs("* bit offset out of range -- aborted\n", stderr);
else if (ret == -1)
fputs("* file position past end -- aborted\n", stderr);
else
fprintf(stderr, "* i/o error %s -- aborted\n", strerror(ret));
return 1;
}
return 0;
}