compare_to_value.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. int main(int argc, char *argv[]) {
  5. long i, j, size, num;
  6. size_t count = 0, total = 0;
  7. int offset = 0, toread = 1, toskip = 0;
  8. uint32_t value;
  9. uint32_t *buf;
  10. if ((argc != 4)&&(argc != 7)) {
  11. printf("Usage: %s <file> <dwords> <value> [offset_dwords read_dwords skip_dwords] \n", argv[0]);
  12. exit(0);
  13. }
  14. FILE *f = fopen(argv[1], "r");
  15. if (!f) {
  16. printf("Can't open %s\n", argv[1]);
  17. exit(1);
  18. }
  19. size = atol(argv[2]);
  20. if (size <= 0) {
  21. printf("Can't parse size %s\n", argv[2]);
  22. exit(1);
  23. }
  24. if (sscanf(argv[3], "%x", &value) != 1) {
  25. printf("Can't parse register %s\n", argv[3]);
  26. exit(1);
  27. }
  28. buf = malloc(size * sizeof(uint32_t));
  29. if (!buf) {
  30. printf("Can't allocate %lu bytes of memory\n", size * sizeof(uint32_t));
  31. exit(1);
  32. }
  33. if (argc == 7) {
  34. offset = atoi(argv[4]);
  35. toread = atoi(argv[5]);
  36. toskip = atoi(argv[6]);
  37. }
  38. num = fread(buf, 4, size, f);
  39. if (num != size) {
  40. printf("Only %lu of %lu dwords in the file\n", num, size);
  41. exit(1);
  42. }
  43. fclose(f);
  44. for (i = offset; i < size; i += toskip) {
  45. for (j = 0; j < toread; j++, i++) {
  46. total++;
  47. if (buf[i] != value) {
  48. count++;
  49. }
  50. }
  51. }
  52. free(buf);
  53. printf("%lu of %lu is wrong\n", count, total);
  54. return 0;
  55. }