xref: /aosp_15_r20/external/vboot_reference/tests/futility/binary_editor.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2014 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * This is a very simple binary editor, used to create corrupted structs for
6  * testing. It copies stdin to stdout, replacing bytes beginning at the given
7  * offset with the specified 8-bit values.
8  *
9  * There is NO conversion checking of the arguments.
10  */
11 
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 	uint32_t offset, curpos, curarg;
19 	int c;
20 
21 	if (argc < 3) {
22 		fprintf(stderr, "Need two or more args:  OFFSET VAL [VAL...]\n");
23 		return 1;
24 	}
25 
26 	offset = (uint32_t)strtoul(argv[1], 0, 0);
27 	curarg = 2;
28 	for ( curpos = 0; (c = fgetc(stdin)) != EOF; curpos++) {
29 
30 		if (curpos == offset && curarg < argc) {
31 			c = (uint8_t)strtoul(argv[curarg++], 0, 0);
32 			offset++;
33 		}
34 
35 		fputc(c, stdout);
36 	}
37 	return 0;
38 }
39