Advertisement
Guest User

Hunk pattern stripper.

a guest
Jan 22nd, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. /* Strips even hunks out of a file. This is useful for files with duplicate patterns. */
  6. /* Change HUNK_SIZE to change the size of the hunk. */
  7.  
  8. int main() {
  9. #define HUNK_SIZE 4096
  10.  
  11. FILE *file = fopen("original.img", "rb");
  12.  
  13. if (file == NULL) {
  14. printf("Sorry, try again! \n");
  15. return 1;
  16. }
  17.  
  18. fseek(file, 0, SEEK_END);
  19. long int size = ftell(file);
  20. fclose(file);
  21.  
  22. #define BUFFER_SIZE ((size + HUNK_SIZE) / 2)
  23.  
  24. uint8_t* buffer = malloc(sizeof(uint8_t)*size);
  25. int bytes_read = fread(buffer, sizeof(uint8_t), size, file);
  26. fclose(file);
  27.  
  28. uint8_t new_file[BUFFER_SIZE];
  29. int size_new = sizeof(new_file); // We are using 8-bit unsigned ints, don't worry.
  30.  
  31. printf("Read original! New file size will be: %d. \n", size_new);
  32.  
  33. for (int i = 0; i <= ( ((size / HUNK_SIZE) - 1) / 2 ); ++i) {
  34. for (int j = 0; j <= (HUNK_SIZE - 1); ++j) {
  35. new_file[(i * HUNK_SIZE) + j] = buffer[((i * HUNK_SIZE) * 2) + j];
  36. }
  37. }
  38.  
  39. FILE *nf = fopen("new.img", "wb");
  40. fwrite(new_file, sizeof(uint8_t), size_new, nf);
  41. fclose(nf);
  42.  
  43. printf("Read back a new image! \n");
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement