Skip to content

Instantly share code, notes, and snippets.

@Risca
Created April 23, 2017 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Risca/3eda5e7aba3dc6b72d61e79eaf7cc147 to your computer and use it in GitHub Desktop.
Save Risca/3eda5e7aba3dc6b72d61e79eaf7cc147 to your computer and use it in GitHub Desktop.
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHUNK_SIZE (512*1024)
#define START_GB ((off_t)2928) // ~13 % of 22 TB
int main(int argc, char* argv[])
{
if (argc != 3) {
printf("%s: <disk1> <disk2>\n", basename(argv[0]));
return EXIT_FAILURE;
}
FILE* f1 = fopen(argv[1], "r");
if (f1 == NULL) {
perror(argv[1]);
return EXIT_FAILURE;
}
FILE* f2 = fopen(argv[2], "r");
if (f2 == NULL) {
perror(argv[2]);
fclose(f1);
return EXIT_FAILURE;
}
char* buf1 = malloc(CHUNK_SIZE);
if (buf1 == NULL) {
perror("buf1");
fclose(f2);
fclose(f1);
return EXIT_FAILURE;
}
char* buf2 = malloc(CHUNK_SIZE);
if (buf2 == NULL) {
perror("buf2");
free(buf1);
fclose(f2);
fclose(f1);
return EXIT_FAILURE;
}
const char* zero = calloc(1, CHUNK_SIZE);
if (zero == NULL) {
perror("zero");
free(buf2);
free(buf1);
fclose(f2);
fclose(f1);
return EXIT_FAILURE;
}
off_t offset = START_GB * 1024 * 1024 * 1024;
fseeko(f1, offset, SEEK_SET);
fseeko(f2, offset, SEEK_SET);
while (fread(buf1, CHUNK_SIZE, 1, f1) == 1 &&
fread(buf2, CHUNK_SIZE, 1, f2) == 1)
{
if (memcmp(buf1, zero, CHUNK_SIZE) != 0 && memcmp(buf1, buf2, CHUNK_SIZE) == 0) {
offset = ftello(f1) - CHUNK_SIZE;
printf("Found identical data at offset 0x%lX\n", offset);
}
}
free(buf2);
free(buf1);
fclose(f2);
fclose(f1);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment