summaryrefslogtreecommitdiff
path: root/binsub.c
blob: c66148cba7f72a0991cddbe554a4eedc75a9ae49 (plain) (blame)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
 * binsub.c / 2022-12-09
 *
 * (C) 2022 Zach van Rijn <me@zv.io>
 *
 * MIT License
 *
 * This utility truncates or replaces needles in an input file;
 * truncation meaning the replacement string is empty.
 *
 * Replacement string length must be less than or equal to that
 * of the needle because the file length must remain unchanged.
 *
 * For efficient operation, consider deploying this on a '.tar'
 * file instead of individual files within a directory.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/**
 * Basic memory structure.
 */
struct buffer
{
    char *data;
    size_t len;
};


/**
 * Given a needle ('find') and optional replacement ('repl'), if
 * the needle is found, truncate it, inject the replacement, and
 * pad the tail end of the matching string with null bytes.
 *
 * The file length remains the same, and we hope that nobody is
 * relying on precomputed offsets into the strings. Mega kludge!
 *
 * Replacement = "":
 *
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  in |.|.|.|N|E|E|D|L|E|.|.|.|O|T|H|E|R| |D|A|T|A|.|.|.|0|
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *                       ^           shift data          ^
 *                       +-------------------------------+
 *
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * out |.|.|.|.|.|.|O|T|H|E|R| |D|A|T|A|.|.|.|0|0|0|0|0|0|0|
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *           ^           shift data          ^
 *           +-------------------------------+
 *
 * Replacement = "FOO":
 *
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  in |.|.|.|N|E|E|D|L|E|.|.|.|O|T|H|E|R| |D|A|T|A|.|.|.|0|
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *                       ^           shift data          ^
 *                       +-------------------------------+
 *
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * out |.|.|.|F|O|O|.|.|.|O|T|H|E|R| |D|A|T|A|.|.|.|0|0|0|0|
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *           ^ FOO ^           shift data          ^
 *           +=====+-------------------------------+
 */
void
replace (struct buffer *buf, const char *find, const char *repl)
{
    char   *match;              /* pointer to found needle    */
    size_t  idx;                /* cursor into file buffer    */

    size_t  nlen;               /* length of needle           */
    size_t  rlen = 0;           /* length of replacement      */

    size_t  mlen;               /* length of matching string  */

    nlen = strlen(find);

    /**
     * Iterate over each character in the current string in the
     * buffer, because multiple full matches may be possible. If
     * we cannot find a match in the current string, skip to the
     * next string. I don't think there is a more optimal way?
     */
    for (idx = 0; idx <= buf->len; idx++)
    {
        /**
         * Does the current string contain the needle?
         */
        match = strstr(buf->data + idx, find);
        if (match)
        {
            /**
             * How long is the current string? We need to search
             * it until we cannot find any more matches.
             */
            mlen = strlen(match);
            printf("%10zu bytes at offset 0x%010lx (%02ld%%)\n",
                mlen,
                (match - buf->data),
                (100 * (match - buf->data)) / buf->len
            );

            /**
             * The replacement string is l.e. the length of the
             * needle, so if it is non-empty, inject it first.
             */
            if (repl)
            {
                rlen = strlen(repl);
                memcpy(match, repl, rlen);
            }

            /**
             * The replacement length may be zero (if empty). In
             * any case, copy the non-needle string remainder to
             * the current matched (needle) location plus offset
             * of any injected replacement. Zero out the tail.
             */
            memmove(match + rlen, match + nlen, mlen - nlen);
            memset(match + mlen - nlen + rlen, 0, nlen - rlen);
        }
        else
        {
            /**
             * This is a partial optimization. Don't bother with
             * searching for needles in the rest of this string;
             * we already know none exist.
             */
            idx += strlen(buf->data + idx);
        }
    }
}


/**
 * Read the contents of a file into a newly allocated buffer. It
 * is possible to 'mmap()', but it leaves less room for checks.
 */
void
scanner (const char *file, const char *find, const char *repl)
{
    FILE   *fp = NULL;
    size_t  nb = 0;

    struct buffer buf;
    memset(&buf, 0, sizeof(struct buffer));

    fp = fopen(file, "rb+");
    if (!fp)
    {
        fprintf(stderr,
            "E: Could not open FILE '%s' for reading!\n",
            file
        );
        return;
    }

    fseek(fp, 0, SEEK_END);
    buf.len = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    /**
     * Allocate memory for the entire file at once. This is not
     * ideal, but we don't expect large files for our use case.
     */
    buf.data = malloc(buf.len + 1);
    if (!buf.data)
    {
        fclose(fp);
        fprintf(stderr,
            "E: Could not allocate '%zu' bytes for file '%s'\n",
            buf.len,
            file
        );
        return;
    }
    buf.data[buf.len] = 0;      /* extra byte needs to be nil */
    
    nb = fread(buf.data, 1, buf.len, fp);
    if (nb != buf.len)
    {
        free(buf.data);
        buf.data = NULL;
        fclose(fp);
        fprintf(stderr,
            "E: Only read '%zu' / '%zu' bytes of file '%s'\n",
            nb,
            buf.len,
            file
        );
        return;
    }
    fclose(fp);

    printf("Examining file '%s'...\n", file);
    replace(&buf, find, repl);

    fp = fopen(file, "wb");
    if (!fp)
    {
        fprintf(stderr,
            "E: Could not open FILE '%s' for writing!\n",
            file
        );
        return;
    }

    nb = fwrite(buf.data, 1, buf.len, fp);
    if (nb != buf.len)
    {
        free(buf.data);
        buf.data = NULL;
        fclose(fp);
        fprintf(stderr,
            "E: Only wrote '%zu' / '%zu' bytes of file '%s'\n",
            nb,
            buf.len,
            file
        );
        return;
    }
    fclose(fp);

    free(buf.data);
}


/**
 * WARNING!
 *
 * This program replaces all occurrences of NEEDLE within string
 * sections of an input file with the string REPLACE. The input
 * file is overwritten. Few, if any, sanity checks are in place.
 */
int
main (int argc, char **argv)
{
    char *prog = NULL;
    char *file = NULL;
    char *find = NULL;
    char *repl = NULL;

    prog = argv[0];

    switch (argc)
    {
    case 3:
        file = argv[1];
        find = argv[2];
        break;
    case 4:
        file = argv[1];
        find = argv[2];
        repl = argv[3];
        if (strlen(repl) > strlen(find))
        {
            fprintf(stderr,
                "E: REPLACE cannot be longer than NEEDLE\n"
            );
            return 1;
        }
        break;
    default:
        fprintf(stderr,
            "Usage: %s FILE NEEDLE [REPLACE]\n",
            prog
        );
        return 1;
    }

    scanner(file, find, repl);

    return 0;
}