Created
November 11, 2012 15:56
-
-
Save iwillspeak/4055319 to your computer and use it in GitHub Desktop.
HHD Byte Count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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