1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
// Program
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <assert.h>
#include <windows.h>
#include <string>
#include <cstdio>
#include <string.h>
#include <fstream>
#include <cstdint>
#include <vector>
#include <list>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
//Declarations
int searchFor(char &buff, int size, std::string str);
//Functions
int searchFor(char buff[], int size, std::string str)
{
//printf("buff[5] %x\n", buff[5]);
char c_to_search[sizeof(str)];
strncpy(c_to_search, str.c_str(), sizeof(c_to_search));
//printf("XXX %s\n", c_to_search);
int pos_search = 0; //
int pos_text = 0; // counter
int len_search = static_cast<int>(str.length());
// watch static_cast for max & negative numbers
//printf("%s - %i chars\n", c_to_search, len_search);
for (pos_text = 0; pos_text < size - len_search; ++pos_text)
{
//printf("%i - check1\n", pos_text);
if(buff[pos_text] == c_to_search[pos_search])
{
//printf("check2\n");
++pos_search;
if(pos_search == len_search)
{
// printf("match at %d 0x%02xh to %d 0x%02xh\n",
// pos_text - len_search, pos_text - len_search,
// pos_text, pos_text);
return (pos_text - len_search);
}
}
else
{
pos_text -=pos_search;
pos_search = 0;
}
}
return (-1);
}
int main()
{
char buff[523];
std::string drLetter = "K"; // GET THE RIGHT DEVICE
std::string device = ("\\\\.\\" + drLetter + ":");
std::string fileType = "FAT32";
std::FILE * fp = fopen(device.c_str(), "rb"); // read, binary
if(!fp) {
cerr << "Device '" + drLetter + "' could not be opened!" << endl;
exit (101);
}
std::ifstream infile(device.c_str(), std::ios::in | std::ios::binary);
if (!infile)
{
cerr << "Device '" + drLetter + "' Input could not be set!" << endl;
exit (102);
}
fseek(fp, 0, SEEK_SET); // reset to beginning of file
infile.read(buff, sizeof(buff)); // read in the structure length
// for (unsigned int i = 0; i < 523; i++)
// {
// printf("%i - %x - %c, ", i, buff[i], buff[i]);
// }
std::vector <string> sysList = { "FAT12", "FAT16", "FAT32", "NTFS"};
if ( ! (std::find(sysList.begin(), sysList.end(), fileType) != sysList.end()) )
{
printf("Not in list of file types!\n");
exit (100);
}
int pos = searchFor(buff, sizeof(buff), fileType);
//printf("pos %i\n" , pos);
if (pos == -1)
{
printf("Not a %s file system.\n", fileType.c_str());
return (99);
}
else
{
printf("search file type at %i - %s\n\n", pos, fileType.c_str());
}
printf("found %s at pos %i\n\n\n", fileType.c_str(), pos);
exit(0);
}
|