Skip to content

Instantly share code, notes, and snippets.

@iwillspeak
Created November 11, 2012 15:56
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 iwillspeak/4055319 to your computer and use it in GitHub Desktop.
Save iwillspeak/4055319 to your computer and use it in GitHub Desktop.
HHD Byte Count
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#define BLOCK_SIZE (4096 * 1024)
typedef struct {
uint8_t byte;
uint64_t count;
} ByteCount;
int main(int argc, char* argv[]) {
FILE* hdd;
ByteCount counts[UINT8_MAX + 1];
size_t len, read;
if (argc < 2)
return 1;
for (int i = 0; i < UINT8_MAX + 1; i++) {
counts[i].byte = i;
counts[i].count = 0;
}
hdd = fopen(argv[1], "rb");
if (!hdd)
return 1;
if (fseeko(hdd, 0, SEEK_END))
return 1;
len = ftello(hdd);
read = 0;
if (fseek(hdd, 0, SEEK_SET))
return 1;
printf("Scanning the %zuGB volume %s\n", len / (1024 * 1024 * 1024), argv[1]);
do {
uint8_t block[BLOCK_SIZE];
size_t count;
count = fread(block, sizeof(uint8_t), BLOCK_SIZE, hdd);
read += count;
for (size_t i = 0; i < count; i++) {
counts[block[i]].count++;
}
if (!(read % (10 * BLOCK_SIZE)))
printf("%zu MB read\n", read / (1024 * 1024));
} while (!feof(hdd));
fclose(hdd);
printf("Ordering frequencies\n");
qsort_b(counts, UINT8_MAX + 1, sizeof(ByteCount), ^(const void* a, const void* b) { return (int)(((ByteCount*)a)->count - ((ByteCount*)b)->count); });
for (int i = 0; i < UINT8_MAX + 1; i++) {
printf("%0.2x : %" PRId64 "\n", counts[i].byte, counts[i].count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment