heb_strip_bad_values.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. int main(int argc, char *argv[]) {
  8. long i, num;
  9. size_t count = 0, total = 0, size;
  10. int offset = 3, toread = 1, toskip = 3;
  11. uint32_t value;
  12. uint32_t *buf;
  13. uint32_t expected = 0;
  14. uint32_t blocks = 0, status_good = 0;
  15. char fixed[4096];
  16. struct stat st_buf;
  17. if ((argc != 2)&&(argc != 5)) {
  18. printf("Usage: %s <file> [offset_dwords read_dwords skip_dwords] \n", argv[0]);
  19. exit(0);
  20. }
  21. FILE *f = fopen(argv[1], "r");
  22. if (!f) {
  23. printf("Can't open %s\n", argv[1]);
  24. exit(1);
  25. }
  26. stat(argv[1], &st_buf);
  27. size = st_buf.st_size / sizeof(uint32_t);
  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 == 5) {
  34. offset = atoi(argv[2]);
  35. toread = atoi(argv[3]);
  36. toskip = atoi(argv[4]);
  37. }
  38. num = fread(buf, 4, size, f);
  39. if (num != size) {
  40. printf("Failed to read %lu dwords, only %lu read\n", size, num);
  41. exit(1);
  42. }
  43. fclose(f);
  44. sprintf(fixed, "%s.fixed", argv[1]);
  45. f = fopen(fixed, "w");
  46. if (!f) {
  47. printf("Failed to open %s for output\n", fixed);
  48. exit(1);
  49. }
  50. expected = (buf[offset]>>24) + 2;
  51. for (i = 1; i < size; i += (toread + toskip)) {
  52. total++;
  53. value = buf[i + offset] >> 24;
  54. // printf("0x%lx: value (%lx) = expected (%lx)\n", i + offset, value, expected);
  55. if (value == expected) {
  56. if (!status_good) {
  57. status_good = 1;
  58. blocks++;
  59. }
  60. fwrite(&buf[i], 4, toread + toskip, f);
  61. expected += 2;
  62. if (expected == 0xb8)
  63. expected = 0;
  64. } else if ((!status_good)&&(value == 0)&&((i + toread + toskip)< size)) {
  65. value = buf[i + offset + toread + toskip] >> 24;
  66. if (value == 2) {
  67. status_good = 1;
  68. blocks++;
  69. fwrite(&buf[i], 4, toread + toskip, f);
  70. expected = 2;
  71. } else {
  72. count++;
  73. }
  74. } else {
  75. printf("0x%lx: value (%x) = expected (%x)\n", (i + offset)*sizeof(uint32_t), value, expected);
  76. status_good = 0;
  77. count++;
  78. }
  79. }
  80. fclose(f);
  81. free(buf);
  82. printf("%lu of %lu is wrong\n", count, total);
  83. return 0;
  84. }