binary_editor.c 932 B

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