Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- /* Strips even hunks out of a file. This is useful for files with duplicate patterns. */
- /* Change HUNK_SIZE to change the size of the hunk. */
- int main() {
- #define HUNK_SIZE 4096
- FILE *file = fopen("original.img", "rb");
- if (file == NULL) {
- printf("Sorry, try again! \n");
- return 1;
- }
- fseek(file, 0, SEEK_END);
- long int size = ftell(file);
- fclose(file);
- #define BUFFER_SIZE ((size + HUNK_SIZE) / 2)
- uint8_t* buffer = malloc(sizeof(uint8_t)*size);
- int bytes_read = fread(buffer, sizeof(uint8_t), size, file);
- fclose(file);
- uint8_t new_file[BUFFER_SIZE];
- int size_new = sizeof(new_file); // We are using 8-bit unsigned ints, don't worry.
- printf("Read original! New file size will be: %d. \n", size_new);
- for (int i = 0; i <= ( ((size / HUNK_SIZE) - 1) / 2 ); ++i) {
- for (int j = 0; j <= (HUNK_SIZE - 1); ++j) {
- new_file[(i * HUNK_SIZE) + j] = buffer[((i * HUNK_SIZE) * 2) + j];
- }
- }
- FILE *nf = fopen("new.img", "wb");
- fwrite(new_file, sizeof(uint8_t), size_new, nf);
- fclose(nf);
- printf("Read back a new image! \n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement