/* bitchecker - verify numericity and quantumness of all bits in all files By Ben Hoyt (c) 2009 - http://benhoyt.com/ Inspired by http://thedailywtf.com/Articles/Some-Crazy-Reason.aspx :-) */ #include #include /* Return true if bit's numericity is wrong (bit is neither zero nor one). */ static inline int invalid_numericity(int bit) { return bit != 0 && bit != 1; } /* Return true if bit's quantum-ness is wrong (i.e., bit is both zero and one at the same time). */ static inline int invalid_quantumness(int bit) { return bit == 0 && bit == 1; } /* Check given file's bit integrity, displaying any errors. */ static void check_file(const char* dirname, const char* filename) { char fullname[MAX_PATH]; FILE *f; int byte; int bit; int pos; int i; strcpy(fullname, dirname); strcat(fullname, filename); printf("%s\n", fullname); f = fopen(fullname, "rb"); if (!f) return; /* Hope that fgetc() is buffered */ pos = 0; while((byte = fgetc(f)) != EOF) { for (i = 0; i < 8; i++) { bit = (byte >> i) & 1; if (invalid_numericity(bit) || invalid_quantumness(bit)) printf("%s: bit %d is invalid!\n", fullname, pos * 8 + i); } pos++; } fclose(f); } /* Check all files in given base path and directory, recursively. */ static void check_dir(const char* base, const char* dir) { char* path; HANDLE handle; WIN32_FIND_DATA data; path = (char*)malloc(MAX_PATH); if (!path) return; sprintf(path, "%s%s%s*.*", base, dir, (*dir ? "\\" : "")); handle = FindFirstFile(path, &data); if (handle != INVALID_HANDLE_VALUE) { /* Remove the "*.*" for later use */ path[strlen(path) - 3] = '\0'; /* Loop through all files/dirs in this directory */ do { if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(data.cFileName, ".") != 0 && strcmp(data.cFileName, "..") != 0) { /* It's a directory, recurse */ check_dir(path, data.cFileName); } } else { /* It's a file, verify it */ check_file(path, data.cFileName); } } while (FindNextFile(handle, &data) != 0); FindClose(handle); } free(path); } /* Check entire C: drive or given directory */ int main(int argc, char* argv[]) { check_dir((argc < 2 ? "c:\\" : argv[1]), ""); return 0; }