diff --git a/FileSearch/CDriveIndex.cpp b/FileSearch/CDriveIndex.cpp index fadde0b..38e9b18 100644 --- a/FileSearch/CDriveIndex.cpp +++ b/FileSearch/CDriveIndex.cpp @@ -1,1117 +1,1323 @@ -/********************************************************************************** -Module name: CDriveIndex.cpp -Written by: Christian Sander -Credits for original code this is based on: Jeffrey Cooperstein & Jeffrey Richter -**********************************************************************************/ - -#include "stdafx.h" -#include "CDriveIndex.h" -#include -#include -#include - - - -// Exported function to create the index of a drive -CDriveIndex* _stdcall CreateIndex(WCHAR cDrive) -{ - CDriveIndex *di = new CDriveIndex(); - di->Init(cDrive); - di->PopulateIndex(); - return di; -} - - - -// Exported function to delete the index of a drive -void _stdcall DeleteIndex(CDriveIndex *di) -{ - if(dynamic_cast(di)) - delete di; -} - - - -// Exported function to search in the index of a drive. -// Returns a string that contains the filepaths of the results, -// separated by newlines for easier processing in non-C++ languages. -// nResults is -1 if more results than the limit were found and 0 if an error occured. In this case the return value is NULL. -WCHAR* _stdcall Search(CDriveIndex *di, WCHAR *szQuery, WCHAR *szPath, BOOL bSort, BOOL bEnhancedSearch, int maxResults, int *nResults) -{ - if(dynamic_cast(di) && szQuery) - { - vector results; - wstring result; - int numResults = di->Find(&wstring(szQuery), szPath != NULL ? &wstring(szPath) : NULL, &results, bSort, bEnhancedSearch, maxResults); - if(nResults != NULL) - *nResults = numResults; - for(unsigned int i = 0; i != results.size(); i++) - result += (i == 0 ? TEXT("") : TEXT("\n")) + results[i].Path + results[i].Filename; - WCHAR * szOutput = new WCHAR[result.length() + 1]; - ZeroMemory(szOutput, (result.length() + 1) * sizeof(szOutput[0])); - _snwprintf(szOutput, result.length(), TEXT("%s"), result.c_str()); - return szOutput; - } - if(nResults != NULL) - *nResults = 0; - return NULL; -} - - - -// Exported function to clear the memory of the string returned by Search(). -// This needs to be called after every call to Search to avoid memory leaks. -void _stdcall FreeResultsBuffer(WCHAR *szResults) -{ - if(szResults) - delete[] szResults; -} - - - -// Exported function that loads the database from disk -CDriveIndex* _stdcall LoadIndexFromDisk(WCHAR *szPath) -{ - if(szPath) - return new CDriveIndex(wstring(szPath)); - return NULL; -} - - - -// Exported function that saves the database to disk -BOOL _stdcall SaveIndexToDisk(CDriveIndex *di, WCHAR *szPath) -{ - if(dynamic_cast(di) && szPath) - return di->SaveToDisk(wstring(szPath)); - return false; -} - - -// Exported function that returns the number of files and directories -void _stdcall GetDriveInfo(CDriveIndex *di, DriveInfo *driveInfo) -{ - if(dynamic_cast(di)) - *driveInfo = di->GetInfo(); -} - - - -// Constructor -CDriveIndex::CDriveIndex() -{ - // Initialize member variables - m_hVol = INVALID_HANDLE_VALUE; -} - - - -// Destructor -CDriveIndex::~CDriveIndex() -{ - CleanUp(); -} - - - -// Cleanup function to free resources -void CDriveIndex::CleanUp() -{ - // Cleanup the memory and handles we were using - if (m_hVol != INVALID_HANDLE_VALUE) - CloseHandle(m_hVol); -} - - - -// This is a helper function that opens a handle to the volume specified -// by the cDriveLetter parameter. -HANDLE CDriveIndex::Open(TCHAR cDriveLetter, DWORD dwAccess) -{ - TCHAR szVolumePath[_MAX_PATH]; - wsprintf(szVolumePath, TEXT("\\\\.\\%c:"), cDriveLetter); - HANDLE hCJ = CreateFile(szVolumePath, dwAccess, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); - return(hCJ); -} - - -// This function creates a journal on the volume. If a journal already -// exists this function will adjust the MaximumSize and AllocationDelta -// parameters of the journal -BOOL CDriveIndex::Create(DWORDLONG MaximumSize, DWORDLONG AllocationDelta) -{ - DWORD cb; - CREATE_USN_JOURNAL_DATA cujd; - cujd.MaximumSize = MaximumSize; - cujd.AllocationDelta = AllocationDelta; - BOOL fOk = DeviceIoControl(m_hVol, FSCTL_CREATE_USN_JOURNAL, - &cujd, sizeof(cujd), NULL, 0, &cb, NULL); - return(fOk); -} - -// Return statistics about the journal on the current volume -BOOL CDriveIndex::Query(PUSN_JOURNAL_DATA pUsnJournalData) -{ - DWORD cb; - BOOL fOk = DeviceIoControl(m_hVol, FSCTL_QUERY_USN_JOURNAL, NULL, 0, - pUsnJournalData, sizeof(*pUsnJournalData), &cb, NULL); - return(fOk); -} - -// Call this to initialize the structure. The cDrive parameter -// specifies the drive that this instance will access. The cbBuffer -// parameter specifies the size of the interal buffer used to read records -// from the journal. This should be large enough to hold several records -// (for example, 10 kilobytes will allow this class to buffer several -// dozen journal records at a time) -BOOL CDriveIndex::Init(WCHAR cDrive) -{ - // You should not call this function twice for one instance. - if (m_hVol != INVALID_HANDLE_VALUE) - DebugBreak(); - m_cDrive = cDrive; - ClearLastResult(); - BOOL fOk = FALSE; - __try { - // Open a handle to the volume - m_hVol = Open(m_cDrive, GENERIC_WRITE | GENERIC_READ); - if (INVALID_HANDLE_VALUE == m_hVol) - __leave; - fOk = TRUE; - } - __finally { - if (!fOk) - CleanUp(); - } - return(fOk); -} - -void CDriveIndex::ClearLastResult() -{ - LastResult = SearchResult(); -} - -// Adds a file to the database -BOOL CDriveIndex::Add(DWORDLONG Index, wstring *szName, DWORDLONG ParentIndex, DWORDLONG Filter) -{ - IndexedFile i; - i.Index = Index; - if(!Filter) - Filter = MakeFilter(szName); - i.Filter = Filter; - rgFiles.insert(rgFiles.end(), i); - return(TRUE); -} - - - -// Adds a directory to the database -BOOL CDriveIndex::AddDir(DWORDLONG Index, wstring *szName, DWORDLONG ParentIndex, DWORDLONG Filter) -{ - IndexedDirectory i; - i.Index = Index; - if(!Filter) - Filter = MakeFilter(szName); - i.Filter = Filter; - i.nFiles = 0; - rgDirectories.insert(rgDirectories.end(), i); - return(TRUE); -} - - - -// Calculates a 64bit value that is used to filter out many files before comparing their filenames -// This method gives a huge speed boost. -DWORDLONG CDriveIndex::MakeFilter(wstring *szName) -{ - /* - Creates an address that is used to filter out strings that don't contain the queried characters - Explanation of the meaning of the single bits: - 0-25 a-z - 26-35 0-9 - 36 . - 37 space - 38 !#$&'()+,-~_ - 39 2 same characters - 40 3 same characters - The fields below indicate the presence of 2-character sequences. Based off http://en.wikipedia.org/wiki/Letter_frequency - 41 TH - 42 HE - 43 AN - 44 RE - 45 ER - 46 IN - 47 ON - 48 AT - 49 ND - 50 ST - 51 ES - 52 EN - 53 OF - 54 TE - 55 ED - 56 OR - 57 TI - 58 HI - 59 AS - 60 TO - 61-63 length (max. 8 characters. Queries are usually shorter than this) - */ - if(!(szName->length() > 0)) - return 0; - DWORDLONG Address = 0; - WCHAR c; - wstring szlower(*szName); - transform(szlower.begin(), szlower.end(), szlower.begin(), tolower); - int counts[26] = {0}; //This array is used to check if characters occur two or three times in the string - wstring::size_type l = szlower.length(); - for(unsigned int i = 0; i != l; i++) - { - c = szlower[i]; - if(c > 96 && c < 123) //a-z - { - Address |= 1ui64 << (DWORDLONG)((DWORDLONG)c - 97ui64); - counts[c-97]++; - if(i < l - 1) - { - if(c == L't' && szlower[i+1] == L'h') //th - Address |= 1ui64 << 41; - else if(c == L'h' && szlower[i+1] == L'e') //he - Address |= 1ui64 << 41; - else if(c == L'a' && szlower[i+1] == L'n') //an - Address |= 1ui64 << 41; - else if(c == L'r' && szlower[i+1] == L'e') //re - Address |= 1ui64 << 41; - else if(c == L'e' && szlower[i+1] == L'r') //er - Address |= 1ui64 << 41; - else if(c == L'i' && szlower[i+1] == L'n') //in - Address |= 1ui64 << 41; - else if(c == L'o' && szlower[i+1] == L'n') //on - Address |= 1ui64 << 41; - else if(c == L'a' && szlower[i+1] == L't') //at - Address |= 1ui64 << 41; - else if(c == L'n' && szlower[i+1] == L'd') //nd - Address |= 1ui64 << 41; - else if(c == L's' && szlower[i+1] == L't') //st - Address |= 1ui64 << 41; - else if(c == L'e' && szlower[i+1] == L's') //es - Address |= 1ui64 << 41; - else if(c == L'e' && szlower[i+1] == L'n') //en - Address |= 1ui64 << 41; - else if(c == L'o' && szlower[i+1] == L'f') //of - Address |= 1ui64 << 41; - else if(c == L't' && szlower[i+1] == L'e') //te - Address |= 1ui64 << 41; - else if(c == L'e' && szlower[i+1] == L'd') //ed - Address |= 1ui64 << 41; - else if(c == L'o' && szlower[i+1] == L'r') //or - Address |= 1ui64 << 41; - else if(c == L't' && szlower[i+1] == L'i') //ti - Address |= 1ui64 << 41; - else if(c == L'h' && szlower[i+1] == L'i') //hi - Address |= 1ui64 << 41; - else if(c == L'a' && szlower[i+1] == L's') //as - Address |= 1ui64 << 41; - else if(c == L't' && szlower[i+1] == L'o') //to - Address |= 1ui64 << 41; - } - } - else if(c >= L'0' && c <= '9') //0-9 - Address |= 1ui64 << (c - L'0' + 26ui64); - else if(c == L'.') //. - Address |= 1ui64 << 36; - else if(c == L' ') // space - Address |= 1ui64 << 37; - else if(c == L'!' || c == L'#' || c == L'$' || c == L'&' || c == L'\'' || c == L'(' || c == L')' || c == L'+' || c == L',' || c == L'-' || c == L'~' || c == L'_') - Address |= 1ui64 << 38; // !#$&'()+,-~_ - } - for(unsigned int i = 0; i != 26; i++) - { - if(counts[i] == 2) - Address |= 1ui64 << 39; - else if(counts[i] > 2) - Address |= 1ui64 << 40; - } - DWORDLONG length = (szlower.length() > 7 ? 7ui64 : (DWORDLONG)szlower.length()) & 0x00000007ui64; //3 bits for length -> 8 max - Address |= length << 61ui64; - return Address; -} - - - -// Internal function for searching in the database. -// For projects in C++ which use this project it might be preferable to use this function -// to skip the wrapper. -// Returns: number of results, -1 if maxResults != -1 and not all results were found -int CDriveIndex::Find(wstring *strQuery, wstring *strQueryPath, vector *rgsrfResults, BOOL bSort, BOOL bEnhancedSearch, int maxResults) -{ - //These variables are used to control the flow of execution in this function. - - //Indicates where results should be searched - unsigned int SearchWhere = IN_FILES; - //Offset for vector marked by SearchWhere - unsigned int iOffset = 0; - //Used to skip the search when the previous two properties should be carried over to the next search without actually using them now. - BOOL bSkipSearch = false; - - //Number of results in this search. -1 if more than maximum number of results. - int nResults = 0; - - //No query, just ignore this call - if(strQuery->length() == 0) - { - // Store this query - LastResult.Query = wstring(TEXT("")); - LastResult.Results = vector(); - return nResults; - } - - if(strQueryPath != NULL) - { - //Check if the path actually matches the drive of this index - WCHAR szDrive[_MAX_DRIVE]; - _wsplitpath(strQueryPath->c_str(), szDrive, NULL, NULL, NULL); - for(unsigned int j = 0; j != _MAX_DRIVE; j++) - szDrive[j] = toupper(szDrive[j]); - if(wstring(szDrive).compare(wstring(1,toupper(m_cDrive))) == 0) - return 0; - } - - //Create lower query string for case-insensitive search - wstring strQueryLower(*strQuery); - for(unsigned int j = 0; j != strQueryLower.length(); j++) - strQueryLower[j] = tolower(strQueryLower[j]); - const WCHAR *szQueryLower = strQueryLower.c_str(); - - //Create lower query path string for case-insensitive search - wstring strQueryPathLower(strQueryPath != NULL ? *strQueryPath : TEXT("")); - for(unsigned int j = 0; j != strQueryPathLower.length(); j++) - strQueryPathLower[j] = tolower((*strQueryPath)[j]); - wstring* pstrQueryPathLower = strQueryPath != NULL && strQueryPathLower.length() > 0 ? &strQueryPathLower : NULL; - - //If the query path is different from the last query so that the results are not valid anymore, the last query needs to be dropped - if(!(strQueryPath != NULL && (LastResult.maxResults == -1 || LastResult.iOffset == 0) && (LastResult.SearchPath.length() == 0 || strQueryPathLower.find(LastResult.SearchPath) == 0))) - LastResult = SearchResult(); - - //Calculate Filter value and length of the current query which are compared with the cached ones to skip many of them - DWORDLONG QueryFilter = MakeFilter(&strQueryLower); - DWORDLONG QueryLength = (QueryFilter & 0xE000000000000000ui64) >> 61ui64; //Bits 61-63 for storing lengths up to 8 - QueryFilter = QueryFilter & 0x1FFFFFFFFFFFFFFFui64; //All but the last 3 bits - - //If the same query string as in the last query was used - if(strQueryLower.compare(LastResult.Query) == 0 && LastResult.Results.size() > 0 && (LastResult.SearchEndedWhere == NO_WHERE && iOffset != 1)) // need proper condition here to skip - { - //Keep the position of the last result - SearchWhere = LastResult.SearchEndedWhere; - iOffset = LastResult.iOffset; - bSkipSearch = true; - for(int i = 0; i != LastResult.Results.size(); i++) - { - BOOL bFound = true; - if(pstrQueryPathLower != NULL) - { - wstring strPathLower(LastResult.Results[i].Path); - for(unsigned int j = 0; j != strPathLower.length(); j++) - strPathLower[j] = tolower(LastResult.Results[i].Path[j]); - bFound = strPathLower.find(strQueryPathLower) != -1; - } - if(bFound) - { - nResults++; - //If the result limit has decreased and we have found all (shouldn't happen in common scenarios) - if(maxResults != -1 && nResults > maxResults) - { - nResults = -1; - - //If we get here, the next incremental should start fresh, but only if it requires more results than this one. - //To accomplish this we make this result contain no information about the origin of these results. - SearchWhere = NO_WHERE; - iOffset = 1; - break; - } - rgsrfResults->insert(rgsrfResults->end(), LastResult.Results[i]); - } - } - //if the last search was limited and didn't finish because it found enough files and we don't have the maximum number of results yet - //we need to continue the search where the last one stopped. - if(LastResult.maxResults != -1 && LastResult.SearchEndedWhere != NO_WHERE && (maxResults == -1 || nResults < maxResults)) - bSkipSearch = false; - } - //If this query is more specific than the previous one, it can use the results from the previous query - else if(strQueryLower.find(LastResult.Query) != -1 && LastResult.Results.size() > 0) - { - bSkipSearch = true; - //Keep the position of the last result - SearchWhere = LastResult.SearchEndedWhere; - iOffset = LastResult.iOffset; - FindInPreviousResults(*strQuery, szQueryLower, QueryFilter, QueryLength, pstrQueryPathLower, *rgsrfResults, 0, bEnhancedSearch, maxResults, nResults); - - //if the last search was limited and didn't finish because it found enough files and we don't have the maximum number of results yet - //we need to continue the search where the last one stopped. - if(LastResult.maxResults != -1 && LastResult.SearchEndedWhere != NO_WHERE && (maxResults == -1 || nResults < maxResults)) - bSkipSearch = false; - } - DWORDLONG FRNPath; - long long nFilesInDir = -1; - if(strQueryPath != NULL && strQueryPath->length()) - { - FRNPath = PathToFRN(strQueryPath); - wstring strPath2; - GetDir(FRNPath, &strPath2); - int iOffset = (int) FindDirOffsetByIndex(FRNPath); - if(iOffset != -1) - nFilesInDir = rgDirectories[iOffset].nFiles; - } - if(SearchWhere == IN_FILES && iOffset == 0 && nFilesInDir != -1 && nFilesInDir < 10000 && !bSkipSearch) - { - FindRecursively(*strQuery, szQueryLower, QueryFilter, QueryLength, strQueryPath, *rgsrfResults, bEnhancedSearch, maxResults, nResults); - SearchWhere = NO_WHERE; - } - else if(SearchWhere == IN_FILES && !bSkipSearch) - { - //Find in file index - FindInJournal(*strQuery, szQueryLower, QueryFilter, QueryLength, (strQueryPath != NULL ? &strQueryPathLower : NULL), rgFiles, *rgsrfResults, iOffset, bEnhancedSearch, maxResults, nResults); - //If we found the maximum number of results in the file index we stop here - if(maxResults != -1 && nResults == -1) - iOffset++; //Start with next entry on the next incremental search - else //Search isn't limited or not all results found yet, continue in directory index - { - SearchWhere = IN_DIRECTORIES; - iOffset = 0; - } - } - - if(SearchWhere == IN_DIRECTORIES && !bSkipSearch) - { - //Find in directory index - FindInJournal(*strQuery, szQueryLower, QueryFilter, QueryLength, pstrQueryPathLower, rgDirectories, *rgsrfResults, iOffset, bEnhancedSearch, maxResults, nResults); - //If we found the maximum number of results in the directory index we stop here - if(maxResults != -1 && nResults == -1) - iOffset++; //Start with next entry on the next incremental search - else //Search isn't limited or less than the maximum number of results found - { - SearchWhere = NO_WHERE; - iOffset = 0; - } - } - - //Sort by match quality and name - if(bSort) - sort(rgsrfResults->begin(), rgsrfResults->end()); - - // Store this query - LastResult.Query = wstring(strQueryLower); - - // Store search path - LastResult.SearchPath = strQueryPathLower; - - //Clear old results, they will be replaced with the current ones - LastResult.Results = vector(); - - //Store number of results (Needed for incremental search) - LastResult.iOffset = iOffset; - - //Store if this search was limited - LastResult.maxResults = maxResults; - - //Store where the current search ended due to file limit (or if it didn't); - LastResult.SearchEndedWhere = SearchWhere; - - //Update last results - for(unsigned int i = 0; i != rgsrfResults->size(); i++) - LastResult.Results.insert(LastResult.Results.end(), (*rgsrfResults)[i]); - - return nResults; -} - -void CDriveIndex::FindRecursively(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring* strQueryPath, vector &rgsrfResults, BOOL bEnhancedSearch, int maxResults, int &nResults) -{ - WIN32_FIND_DATA ffd; - size_t length_of_arg; - HANDLE hFind = INVALID_HANDLE_VALUE; - - // Check that the input path plus 3 is not longer than MAX_PATH. - // Three characters are for the "\*" plus NULL appended below. - length_of_arg = strQueryPath->length(); - if (length_of_arg > (MAX_PATH - 3)) - return; - - // Prepare string for use with FindFile functions. First, copy the - // string to a buffer, then append '\*' to the directory name. - wstring strPath = wstring(*strQueryPath); - if((*strQueryPath)[strQueryPath->length() - 1] != L'\\') - strPath += wstring(TEXT("\\*")); - else - strPath += wstring(TEXT("*")); - - const WCHAR* szDir = strPath.c_str(); - - // Find the first file in the directory. - hFind = FindFirstFile(szDir, &ffd); - - if (hFind == INVALID_HANDLE_VALUE) - return; - unsigned int nFiles = 0; - // List all the files in the directory with some info about them. - do - { - if(ffd.dwFileAttributes & FILE_ATTRIBUTE_VIRTUAL || ffd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) - continue; - float MatchQuality; - wstring strFilename(ffd.cFileName); - if(strFilename.compare(TEXT(".")) == 0 || strFilename.compare(TEXT("..")) == 0) - continue; - nFiles++; - if(bEnhancedSearch) - MatchQuality = FuzzySearch(strFilename, strQuery); - else - { - wstring szLower(strFilename); - for(unsigned int j = 0; j != szLower.length(); j++) - szLower[j] = tolower(szLower[j]); - MatchQuality = szLower.find(strQuery) != -1; - } - - if(MatchQuality > 0.6f) - { - nResults++; - if(maxResults != -1 && nResults > maxResults) - { - nResults = -1; - break; - } - SearchResultFile srf; - srf.Filename = strFilename; - srf.Path = *strQueryPath + TEXT("\\"); - srf.Filter = MAXULONG64; - srf.MatchQuality = MatchQuality; - rgsrfResults.insert(rgsrfResults.end(), srf); - } - - if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - { - wstring strSubPath = wstring(*strQueryPath); - if((*strQueryPath)[strQueryPath->length() - 1] != L'\\') - strSubPath += L'\\'; - strSubPath += ffd.cFileName; - FindRecursively(strQuery, szQueryLower, QueryFilter, QueryLength, &strSubPath, rgsrfResults, bEnhancedSearch, maxResults, nResults); - if(nResults == -1) - break; - } - } - while (FindNextFile(hFind, &ffd) != 0); - FindClose(hFind); -} - -//T needs to be IndexedFile or IndexedDirectory -template -void CDriveIndex::FindInJournal(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring * strQueryPath, vector &rgJournalIndex, vector &rgsrfResults, unsigned int iOffset, BOOL bEnhancedSearch, int maxResults, int &nResults) -{ - for(unsigned int j = 0; j != rgJournalIndex.size(); j++) - { - IndexedFile* i = (IndexedFile*)&rgJournalIndex[j]; - DWORDLONG Length = (i->Filter & 0xE000000000000000ui64) >> 61ui64; //Bits 61-63 for storing lengths up to 8 - DWORDLONG Filter = i->Filter & 0x1FFFFFFFFFFFFFFFui64; //All but the last 3 bits - if((Filter & QueryFilter) == QueryFilter && QueryLength <= Length) - { - USNEntry file = FRNToName(i->Index); - float MatchQuality; - if(bEnhancedSearch) - MatchQuality = FuzzySearch(file.Name, strQuery); - else - { - wstring szLower(file.Name); - for(unsigned int j = 0; j != szLower.length(); j++) - szLower[j] = tolower(szLower[j]); - MatchQuality = szLower.find(strQuery) != -1; - } - - if(MatchQuality > 0.6f) - { - nResults++; - if(maxResults != -1 && nResults > maxResults) - { - nResults = -1; - break; - } - SearchResultFile srf; - srf.Filename = file.Name; - srf.Path.reserve(MAX_PATH); - Get(i->Index, &srf.Path); - BOOL bFound = true; - if(strQueryPath != NULL) - { - wstring strPathLower(srf.Path); - for(unsigned int j = 0; j != strPathLower.length(); j++) - strPathLower[j] = tolower(strPathLower[j]); - bFound = strPathLower.find(*strQueryPath) != -1; - } - if(bFound) - { - //split path - WCHAR szDrive[_MAX_DRIVE]; - WCHAR szPath[_MAX_PATH]; - WCHAR szName[_MAX_FNAME]; - WCHAR szExt[_MAX_EXT]; - _wsplitpath(srf.Path.c_str(), szDrive, szPath, szName, szExt); - srf.Path = wstring(szDrive) + wstring(szPath); - srf.Filter = i->Filter; - srf.MatchQuality = MatchQuality; - rgsrfResults.insert(rgsrfResults.end(), srf); - } - } - } - } -} -void CDriveIndex::FindInPreviousResults(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring * strQueryPath, vector &rgsrfResults, unsigned int iOffset, BOOL bEnhancedSearch, int maxResults, int &nResults) -{ - for(int i = 0; i != LastResult.Results.size() && (maxResults == -1 || i < maxResults); i++) - { - SearchResultFile *srf = & LastResult.Results[i]; - DWORDLONG Length = (srf->Filter & 0xE000000000000000ui64) >> 61ui64; //Bits 61-63 for storing lengths up to 8 - DWORDLONG Filter = srf->Filter & 0x1FFFFFFFFFFFFFFFui64; //All but the last 3 bits - if((Filter & QueryFilter) == QueryFilter && QueryLength <= Length) - { - if(bEnhancedSearch) - srf->MatchQuality = FuzzySearch(srf->Filename, strQuery); - else - { - wstring szLower(srf->Filename); - for(unsigned int j = 0; j != szLower.length(); j++) - szLower[j] = tolower(szLower[j]); - srf->MatchQuality = szLower.find(szQueryLower) != -1; - } - if(srf->MatchQuality > 0.6f) - { - BOOL bFound = true; - if(strQueryPath != NULL) - { - wstring strPathLower(srf->Path); - for(unsigned int j = 0; j != srf->Path.length(); j++) - strPathLower[j] = tolower(srf->Path[j]); - bFound = strPathLower.find(*strQueryPath) != -1; - } - if(bFound) - { - nResults++; - if(maxResults != -1 && nResults > maxResults) - { - nResults = -1; - break; - } - rgsrfResults.insert(rgsrfResults.end(), *srf); - } - } - } - } -} - - -// Clears the database -BOOL CDriveIndex::Empty() -{ - rgFiles.clear(); - rgDirectories.clear(); - return(TRUE); -} - - - -// Constructs a path for a file -BOOL CDriveIndex::Get(DWORDLONG Index, wstring *sz) -{ - *sz = TEXT(""); - int n = 0; - do { - USNEntry file = FRNToName(Index); - *sz = file.Name + ((n != 0) ? TEXT("\\") : TEXT("")) + *sz; - Index = file.ParentIndex; - n++; - } while (Index != 0); - return(TRUE); -} - - - -// Constructs a path for a directory -BOOL CDriveIndex::GetDir(DWORDLONG Index, wstring *sz) -{ - *sz = TEXT(""); - do { - USNEntry file = FRNToName(Index); - *sz = file.Name + ((sz->length() != 0) ? TEXT("\\") : TEXT("")) + *sz; - Index = file.ParentIndex; - } while (Index != 0); - return(TRUE); -} - - - -//Finds the position of a file in the database by the FileReferenceNumber -INT64 CDriveIndex::FindOffsetByIndex(DWORDLONG Index) { - - vector::difference_type pos; - IndexedFile i; - i.Index = Index; - pos = distance(rgFiles.begin(), lower_bound(rgFiles.begin(), rgFiles.end(), i)); - return (INT64) (pos == rgFiles.size() ? -1 : pos); // this is valid because the number of files doesn't exceed the range of INT64 -} - - - -//Finds the position of a directory in the database by the FileReferenceNumber -INT64 CDriveIndex::FindDirOffsetByIndex(DWORDLONG Index) -{ - vector::difference_type pos; - IndexedDirectory i; - i.Index = Index; - pos = distance(rgDirectories.begin(), lower_bound(rgDirectories.begin(), rgDirectories.end(), i)); - return (INT64) (pos == rgDirectories.size() ? -1 : pos); // this is valid because the number of files doesn't exceed the range of INT64 -} - -DWORDLONG PathToFRN(wstring* strPath) -{ - HANDLE hDir = CreateFile(strPath->c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - if(hDir == INVALID_HANDLE_VALUE) - return 0; - BY_HANDLE_FILE_INFORMATION fi; - GetFileInformationByHandle(hDir, &fi); - CloseHandle(hDir); - return (((DWORDLONG) fi.nFileIndexHigh) << 32) | fi.nFileIndexLow; -} - -// Enumerate the MFT for all entries. Store the file reference numbers of -// any directories in the database. -void CDriveIndex::PopulateIndex() -{ - Empty(); - - vector FileParents; - vector DirectoryParents; - - USN_JOURNAL_DATA ujd; - Query(&ujd); - - // Get the FRN of the root directory - // This had BETTER work, or we can't do anything - - WCHAR szRoot[_MAX_PATH]; - wsprintf(szRoot, TEXT("%c:\\"), m_cDrive); - HANDLE hDir = CreateFile(szRoot, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - - BY_HANDLE_FILE_INFORMATION fi; - GetFileInformationByHandle(hDir, &fi); - CloseHandle(hDir); - DWORDLONG IndexRoot = (((DWORDLONG) fi.nFileIndexHigh) << 32) | fi.nFileIndexLow; - wsprintf(szRoot, TEXT("%c:"), m_cDrive); - AddDir(IndexRoot, &wstring(szRoot), 0); - DirectoryParents.insert(DirectoryParents.end(), 0); - m_dwDriveFRN = IndexRoot; - - MFT_ENUM_DATA med; - med.StartFileReferenceNumber = 0; - med.LowUsn = 0; - med.HighUsn = ujd.NextUsn; - - // Process MFT in 64k chunks - BYTE pData[sizeof(DWORDLONG) + 0x10000]; - DWORDLONG fnLast = 0; - DWORD cb; - unsigned int num = 0; - unsigned int numDirs = 1; - while (DeviceIoControl(m_hVol, FSCTL_ENUM_USN_DATA, &med, sizeof(med), pData, sizeof(pData), &cb, NULL) != FALSE) { - - PUSN_RECORD pRecord = (PUSN_RECORD) &pData[sizeof(USN)]; - while ((PBYTE) pRecord < (pData + cb)) { - if ((pRecord->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) - numDirs++; - else - num++; - pRecord = (PUSN_RECORD) ((PBYTE) pRecord + pRecord->RecordLength); - } - med.StartFileReferenceNumber = * (DWORDLONG *) pData; - } - - FileParents.reserve(num); - DirectoryParents.reserve(numDirs); - rgFiles.reserve(num); - rgDirectories.reserve(numDirs); - hash_map hmFiles; - hash_map hmDirectories; - hash_map::iterator it; - med.StartFileReferenceNumber = 0; - while (DeviceIoControl(m_hVol, FSCTL_ENUM_USN_DATA, &med, sizeof(med), pData, sizeof(pData), &cb, NULL) != FALSE) - { - PUSN_RECORD pRecord = (PUSN_RECORD) &pData[sizeof(USN)]; - while ((PBYTE) pRecord < (pData + cb)) - { - wstring sz((LPCWSTR) ((PBYTE) pRecord + pRecord->FileNameOffset), pRecord->FileNameLength / sizeof(WCHAR)); - if ((pRecord->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) - { - AddDir(pRecord->FileReferenceNumber, &sz, pRecord->ParentFileReferenceNumber); - //DirectoryParents.insert(DirectoryParents.end(), pRecord->ParentFileReferenceNumber); - HashMapEntry hme; - hme.iOffset = rgDirectories.size() - 1; - hme.ParentFRN = pRecord->ParentFileReferenceNumber; - hmDirectories[pRecord->FileReferenceNumber] = hme; - } - else - { - Add(pRecord->FileReferenceNumber, &sz, pRecord->ParentFileReferenceNumber); - HashMapEntry hme; - hme.iOffset = rgFiles.size() - 1; - hme.ParentFRN = pRecord->ParentFileReferenceNumber; - //FileParents.insert(FileParents.end(), pRecord->ParentFileReferenceNumber); - hmFiles[pRecord->FileReferenceNumber] = hme; - } - pRecord = (PUSN_RECORD) ((PBYTE) pRecord + pRecord->RecordLength); - } - med.StartFileReferenceNumber = * (DWORDLONG *) pData; - } - - //Calculate files per directory. This takes most of the indexing time, but this information can be useful to reduce the time needed - //for searching in directories with few files (less than 10k). - for ( it=hmFiles.begin() ; it != hmFiles.end(); it++ ) - { - HashMapEntry* hme = &hmDirectories[it->second.ParentFRN]; - do - { - rgDirectories[hme->iOffset].nFiles++; - HashMapEntry* hme2 = &hmDirectories[it->second.ParentFRN]; - - if(hme != hme2) - hme = hme2; - else // This must not happen, otherwise a directory is its own parent! - break; - } while(hme->ParentFRN != 0); - } - //for(unsigned int i = 0; i != FileParents.size(); i++) - //{ - // DWORDLONG dwIndex = FileParents[i]; - // while(dwIndex != 0) - // { - // int iOffset = -1; - // for(unsigned int j = 0; j != rgDirectories.size(); j++) - // if(rgDirectories[j].Index == dwIndex) - // { - // iOffset = j; - // break; - // } - // if(iOffset == -1) - // break; - // rgDirectories[iOffset].nFiles++; - // DWORDLONG dwIndex2 = DirectoryParents[iOffset]; - - // if(dwIndex != dwIndex2) - // dwIndex = dwIndex2; - // else // This must not happen, otherwise a directory is its own parent! - // break; - // } - // //wstring strPath; - // //GetDir(dwIndex, &strPath); - - // //do { - // // //USNEntry file = FRNToName(dwIndex); - // // int iOffset = -1; - // // for(int j = 0; j != rgDirectories.size(); j++) - // // if(rgDirectories[j].Index == dwIndex) - // // { - // // iOffset = j; - // // break; - // // } - // // if(iOffset == -1) - // // break; - // // //USNEntry parent = FRNToName(file.ParentIndex); - // // //USNEntry parent2 = FRNToName(rgDirectories[iOffset].Index); - // // rgDirectories[iOffset].nFiles++; - // // dwIndex = file.ParentIndex; - // //} while (dwIndex != 0); - //} - rgFiles.shrink_to_fit(); - rgDirectories.shrink_to_fit(); - sort(rgFiles.begin(), rgFiles.end()); - sort(rgDirectories.begin(), rgDirectories.end()); -} - -// Resolve FRN to filename by enumerating USN journal with StartFileReferenceNumber=FRN -USNEntry CDriveIndex::FRNToName(DWORDLONG FRN) -{ - if(FRN == m_dwDriveFRN) - return USNEntry(wstring(1, m_cDrive) + wstring(TEXT(":")), 0); - USN_JOURNAL_DATA ujd; - Query(&ujd); - - MFT_ENUM_DATA med; - med.StartFileReferenceNumber = FRN; - med.LowUsn = 0; - med.HighUsn = ujd.NextUsn; - - // The structure only needs a single entry so it can be pretty small - BYTE pData[sizeof(DWORDLONG) + 0x300]; - DWORD cb; - while (DeviceIoControl(m_hVol, FSCTL_ENUM_USN_DATA, &med, sizeof(med), pData, sizeof(pData), &cb, NULL) != FALSE) { - - PUSN_RECORD pRecord = (PUSN_RECORD) &pData[sizeof(USN)]; - while ((PBYTE) pRecord < (pData + cb)) { - if(pRecord->FileReferenceNumber == FRN) - return USNEntry(wstring((LPCWSTR) ((PBYTE) pRecord + pRecord->FileNameOffset), pRecord->FileNameLength / sizeof(WCHAR)), pRecord->ParentFileReferenceNumber); - pRecord = (PUSN_RECORD) ((PBYTE) pRecord + pRecord->RecordLength); - } - med.StartFileReferenceNumber = * (DWORDLONG *) pData; - } - return USNEntry(wstring(TEXT("")), 0); -} - - - -// Saves the database to disk. The file can be used to create an instance of CDriveIndex. -BOOL CDriveIndex::SaveToDisk(wstring &strPath) -{ - ofstream::pos_type size; - ofstream file (strPath.c_str(), ios::out|ios::binary|ios::trunc); - if (file.is_open()) - { - //Drive character - file.write((char*) &m_cDrive, sizeof(m_cDrive)); - - //Drive FileReferenceNumber - file.write((char*) &m_dwDriveFRN, sizeof(m_dwDriveFRN)); - - unsigned int size = rgFiles.size(); - //Number of files - file.write((char*) &size, sizeof(rgFiles.size())); - //indexed files - file.write((char*) &(rgFiles[0]), sizeof(IndexedFile) * rgFiles.size()); - - size = rgDirectories.size(); - //Number of directories - file.write((char*) &size, sizeof(rgDirectories.size())); - //indexed directories - file.write((char*) &(rgDirectories[0]), sizeof(IndexedDirectory) * rgDirectories.size()); - file.close(); - return true; - } - return false; -} - - - -// Constructor for loading the index from a previously saved file -CDriveIndex::CDriveIndex(wstring &strPath) -{ - m_hVol = INVALID_HANDLE_VALUE; - Empty(); - - ifstream::pos_type size; - - ifstream file (strPath.c_str(), ios::in | ios::binary); - if (file.is_open()) - { - //Drive - WCHAR cDrive; - file.read((char*) &cDrive, sizeof(WCHAR)); - - if(Init(cDrive)) - { - // Drive FileReferenceNumber - file.read((char*) &m_dwDriveFRN, sizeof(m_dwDriveFRN)); - - //Number of files - unsigned int numFiles = 0; - file.read((char*) &numFiles, sizeof(numFiles)); - rgFiles.reserve(numFiles); - - //indexed files - for(unsigned int j = 0; j != numFiles; j++) - { - IndexedFile i; - file.read((char*) &i, sizeof(IndexedFile)); - rgFiles.insert(rgFiles.end(), i); - } - - //Number of directories - unsigned int numDirs = 0; - file.read((char*) &numDirs, sizeof(numDirs)); - rgDirectories.reserve(numDirs); - - //indexed directories - for(unsigned int j = 0; j != numDirs; j++) - { - IndexedDirectory i; - file.read((char*) &i, sizeof(IndexedDirectory)); - rgDirectories.insert(rgDirectories.end(), i); - } - } - file.close(); - } - return; -} - - - -// Returns the number of files and folders on this drive -DriveInfo CDriveIndex::GetInfo() -{ - DriveInfo di; - di.NumFiles = (DWORDLONG) rgFiles.size(); - di.NumDirectories = (DWORDLONG) rgDirectories.size(); - return di; -} - - - - -//Performs a fuzzy search for shorter in longer. -//return values range from 0.0 = identical to 1.0 = completely different. 0.4 seems appropriate -float FuzzySearch(wstring &longer, wstring &shorter) -{ - //Note: All string lengths are shorter than MAX_PATH, so an uint is perfectly fitted. - unsigned int lenl = (unsigned int) longer.length(); - unsigned int lens = (unsigned int) shorter.length(); - - if(lens > lenl) - return 0.0f; - - //Check if the shorter string is a substring of the longer string - unsigned int Contained = (unsigned int) longer.find(shorter); - if(Contained != wstring::npos) - return Contained == 0 ? 1.0f : 0.8f; - - wstring longerlower(longer); - wstring shorterlower(shorter); - for(unsigned int i = 0; i != lenl; i++) - longerlower[i] = tolower(longer[i]); - for(unsigned int i = 0; i != lens; i++) - shorterlower[i] = tolower(shorter[i]); - - //Check if the shorter string is a substring of the longer string - Contained = (unsigned int) longerlower.find(shorterlower); - if(Contained != wstring::npos) - return Contained == 0 ? 0.9f : 0.7f; - - //Check if string can be matched by omitting characters - if(lens < 5) - { - unsigned int pos = 0; - unsigned int matched = 0; - for(unsigned int i = 0; i != lens; i++) - { - WCHAR c = toupper(shorter[i]); //only look for capital letters in longer string, (e.g. match tc in TrueCrypt) - for(unsigned int j = 0; j != lenl - pos; j++) - { - if(longer[pos + j] == c) - { - pos = j; - matched++; - break; - } - else - continue; - } - } - if(matched == lens) - return 0.9f; //Slightly worse than direct matches - } - return 0; +/********************************************************************************** +Module name: CDriveIndex.cpp +Written by: Christian Sander +Credits for original code this is based on: Jeffrey Cooperstein & Jeffrey Richter +**********************************************************************************/ + +#include "stdafx.h" +#include "CDriveIndex.h" +#include +#include +#include +#include +#include +#include + +#include "lz4.h" + +#include "stxutif.h" +using namespace gel; + +using namespace tinyxml2; + +#if (NTDDI_VERSION >= NTDDI_WIN8) +typedef MFT_ENUM_DATA_V0 LEGACY_MFT_ENUM_DATA; +#else +typedef MFT_ENUM_DATA LEGACY_MFT_ENUM_DATA; +#endif + + +namespace { +template +T swap_endian(T u) +{ + union + { + T u; + unsigned char u8[sizeof(T)]; + } source, dest; + + source.u = u; + + for (size_t k = 0; k < sizeof(T); k++) + dest.u8[k] = source.u8[sizeof(T) - k - 1]; + + return dest.u; +} +}; + + +// Exported function to create the index of a drive +CDriveIndex* _stdcall CreateIndex(WCHAR cDrive) +{ + CDriveIndex *di = new CDriveIndex(); + di->Init(cDrive); + di->PopulateIndex(); + DriveInfo info(di->GetInfo()); + if (info.NumFiles <= 2) + { + delete di; + di = 0; + } + return di; +} + + + +// Exported function to delete the index of a drive +void _stdcall DeleteIndex(CDriveIndex *di) +{ + if(dynamic_cast(di)) + delete di; +} + +void CDriveIndex::attach(vector &dirHandles, unordered_map::size_type> &umDirFrnToHandle, int NodeType, DWORDLONG Index) const +{ + if (umDirFrnToHandle.find(Index) != umDirFrnToHandle.end()) { // already processed? + return; + } + + USNEntry file = FRNToName(Index); + if (file.ParentIndex == 0) { // No upper directory + return; + } + if(!(file.ParentIndex == 0 && file.Name.length() == 2 && file.Name[1] == wchar_t(L':'))) // to exclude drive itself + { + SearchResultFile srf; + srf.Filename = file.Name; + srf.Path.reserve(MAX_PATH); + // Obtain path into buffer, and split it + Get(Index, &srf.Path); + if(srf.Path.length() < 3) { + return; + } + + WCHAR szDrive[_MAX_DRIVE]; + WCHAR szPath[_MAX_PATH]; + WCHAR szName[_MAX_FNAME]; + WCHAR szExt[_MAX_EXT]; + _wsplitpath(srf.Path.c_str(), szDrive, szPath, szName, szExt); + + // Skip metadata files and other special directories + int compareCount = sizeof(L"$RECYCLE.BIN") / sizeof(WCHAR) - 1; + if (*szDrive == wchar_t(0) || srf.Path.compare(3, compareCount, L"$RECYCLE.BIN", compareCount) == 0) { + return; + } + + unordered_map::size_type>::const_iterator it(umDirFrnToHandle.find(file.ParentIndex)); + if (it == umDirFrnToHandle.end()) { + attach(dirHandles, umDirFrnToHandle, IN_DIRECTORIES, file.ParentIndex); + } + it = umDirFrnToHandle.find(file.ParentIndex); + if (it == umDirFrnToHandle.end()) { + return; // unexpected + } + XMLHandle parentHandle(dirHandles[it->second]); + + XMLNode * parent = parentHandle.ToNode(); + if (!parent) { + return; + } + + XMLElement * element = parent->GetDocument()->NewElement( (NodeType == IN_DIRECTORIES) ? "Directory" : "File" ); + element->SetAttribute("Name", stdx::wstring_to_utf8(file.Name).c_str()); + + if (NodeType == IN_DIRECTORIES) { + vector::iterator insertedIt(dirHandles.insert(dirHandles.end(), XMLHandle(element))); + umDirFrnToHandle[Index] = insertedIt - dirHandles.begin(); + } + else { + HANDLE hFile = CreateFile( srf.Path.c_str(), 0, + FILE_SHARE_READ, 0, OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, 0 ); + if (hFile != INVALID_HANDLE_VALUE) { + BY_HANDLE_FILE_INFORMATION FileInfo = {0}; + GetFileInformationByHandle(hFile, &FileInfo); + if (!(FileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) { + ULARGE_INTEGER filesize = {FileInfo.nFileSizeLow, FileInfo.nFileSizeHigh}; + element->SetAttribute("Size", std::to_string(DWORDLONG(filesize.QuadPart)).c_str());; + } + CloseHandle(hFile); + } + } + + parent->InsertEndChild(element); + } +} + + +// Exports database to an exchange format. +// format is ignored for now, pass 0 for the default format. +// returns number of processed entries and 0 if an error occured. +BOOL CDriveIndex::ExportToFileListing(wstring &strPath, int format) const +{ + if (format < ExportFormat::ExportFormatAdcXml || format > ExportFormat::ExportFormatAdcXml_LZ4) { + return FALSE; + } + vector results; + XMLDocument doc; + XMLPrinter printer; + __time64_t tm; + CHAR tmbuf[200]; + + _time64(&tm); + strftime(tmbuf, sizeof(tmbuf) / sizeof(tmbuf[0]), "%Y-%m-%dT%H:%M:%SZ", _gmtime64(&tm)); + + stringstream emptyxml; + emptyxml << + "\n" + ""; + + XMLError err = doc.Parse(emptyxml.str().c_str()); + if (err != XML_SUCCESS) { + return false; + } + XMLHandle docHandle( &doc ); + + vector dirHandles; + dirHandles.reserve(rgDirectories.size()); + unordered_map::size_type> umDirFrnToHandle; + + vector::iterator insertedIt(dirHandles.insert(dirHandles.end(), XMLHandle(docHandle.FirstChildElement()))); + umDirFrnToHandle[m_dwDriveFRN] = insertedIt - dirHandles.begin(); + + for(unsigned int j = 0; j != rgDirectories.size(); j++) { + const IndexedDirectory* i = &rgDirectories[j]; + attach(dirHandles, umDirFrnToHandle, IN_DIRECTORIES, i->Index); + } + for(unsigned int j = 0; j != rgFiles.size(); j++) { + const IndexedFile* i = &rgFiles[j]; + attach(dirHandles, umDirFrnToHandle, IN_FILES, i->Index); + } + doc.Print( &printer ); + + std::ofstream file; // closes at end of scope + file.open(strPath.c_str(), ios::out|ios::binary|ios::trunc); + if (file.is_open()) + { + if (format == ExportFormat::ExportFormatAdcXml) { + file.write(printer.CStr(), printer.CStrSize() - 1); + return file.good(); + } + else if (format == ExportFormat::ExportFormatAdcXml_LZ4) { + int const uncompressed_size = printer.CStrSize() - 1; + int max_compressed_size = LZ4_compressBound( uncompressed_size ); + std::unique_ptr compressed( new char[max_compressed_size] ); + int const compressed_size = LZ4_compress( printer.CStr(), compressed.get(), uncompressed_size ); + int const final_compressed_size = ( compressed_size >= uncompressed_size ) ? uncompressed_size : compressed_size; + +#if defined(BIG_ENDIAN) + int value; + value = swap_endian(uncompressed_size); file.write(reinterpret_cast(&value), sizeof(int)); + value = swap_endian(final_compressed_size); file.write(reinterpret_cast(&value), sizeof(int)); +#else + file.write(reinterpret_cast(&uncompressed_size), sizeof(int)); + file.write(reinterpret_cast(&final_compressed_size), sizeof(int)); +#endif + file.write(( compressed_size >= uncompressed_size ) ? printer.CStr() : compressed.get(), final_compressed_size); + + return true; + } + } + + + return false; +} + + +// Exported function to search in the index of a drive. +// Returns a string that contains the filepaths of the results, +// separated by newlines for easier processing in non-C++ languages. +// nResults is -1 if more results than the limit were found and 0 if an error occured. In this case the return value is NULL. +WCHAR* _stdcall Search(CDriveIndex *di, WCHAR *szQuery, WCHAR *szPath, BOOL bSort, BOOL bEnhancedSearch, int maxResults, int *nResults) +{ + if(dynamic_cast(di) && szQuery) + { + vector results; + wstring result; + int numResults = di->Find(&wstring(szQuery), szPath != NULL ? &wstring(szPath) : NULL, &results, bSort, bEnhancedSearch, maxResults); + if(nResults != NULL) + *nResults = numResults; + for(unsigned int i = 0; i != results.size(); i++) + result += (i == 0 ? TEXT("") : TEXT("\n")) + results[i].Path + results[i].Filename; + WCHAR * szOutput = new WCHAR[result.length() + 1]; + ZeroMemory(szOutput, (result.length() + 1) * sizeof(szOutput[0])); + _snwprintf(szOutput, result.length(), TEXT("%s"), result.c_str()); + return szOutput; + } + if(nResults != NULL) + *nResults = 0; + return NULL; +} + + + +// Exported function to clear the memory of the string returned by Search(). +// This needs to be called after every call to Search to avoid memory leaks. +void _stdcall FreeResultsBuffer(WCHAR *szResults) +{ + if(szResults) + delete[] szResults; +} + + + +// Exported function that loads the database from disk +CDriveIndex* _stdcall LoadIndexFromDisk(WCHAR *szPath) +{ + if(szPath) + return new CDriveIndex(wstring(szPath)); + return NULL; +} + + + +// Exported function that saves the database to disk +BOOL _stdcall SaveIndexToDisk(CDriveIndex *di, WCHAR *szPath) +{ + if(dynamic_cast(di) && szPath) + return di->SaveToDisk(wstring(szPath)); + return false; +} + + +// Exported function that returns the number of files and directories +void _stdcall GetDriveInfo(CDriveIndex *di, DriveInfo *driveInfo) +{ + if(dynamic_cast(di)) + *driveInfo = di->GetInfo(); +} + + + +BOOL _stdcall ExportIndex(CDriveIndex *di, WCHAR *szPath, int format) +{ + if(dynamic_cast(di) && szPath) + return di->ExportToFileListing(wstring(szPath), format); + return false; +} + +// Constructor +CDriveIndex::CDriveIndex() +{ + // Initialize member variables + m_hVol = INVALID_HANDLE_VALUE; +} + + + +// Destructor +CDriveIndex::~CDriveIndex() +{ + CleanUp(); +} + + + +// Cleanup function to free resources +void CDriveIndex::CleanUp() +{ + // Cleanup the memory and handles we were using + if (m_hVol != INVALID_HANDLE_VALUE) + CloseHandle(m_hVol); +} + + + +// This is a helper function that opens a handle to the volume specified +// by the cDriveLetter parameter. +HANDLE CDriveIndex::Open(TCHAR cDriveLetter, DWORD dwAccess) +{ + TCHAR szVolumePath[_MAX_PATH]; + wsprintf(szVolumePath, TEXT("\\\\.\\%c:"), cDriveLetter); + HANDLE hCJ = CreateFile(szVolumePath, dwAccess, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + return(hCJ); +} + + +// This function creates a journal on the volume. If a journal already +// exists this function will adjust the MaximumSize and AllocationDelta +// parameters of the journal +BOOL CDriveIndex::Create(DWORDLONG MaximumSize, DWORDLONG AllocationDelta) +{ + DWORD cb; + CREATE_USN_JOURNAL_DATA cujd; + cujd.MaximumSize = MaximumSize; + cujd.AllocationDelta = AllocationDelta; + BOOL fOk = DeviceIoControl(m_hVol, FSCTL_CREATE_USN_JOURNAL, + &cujd, sizeof(cujd), NULL, 0, &cb, NULL); + return(fOk); +} + +// Return statistics about the journal on the current volume +BOOL CDriveIndex::Query(PUSN_JOURNAL_DATA pUsnJournalData) const +{ + DWORD cb; + BOOL fOk = DeviceIoControl(m_hVol, FSCTL_QUERY_USN_JOURNAL, NULL, 0, + pUsnJournalData, sizeof(*pUsnJournalData), &cb, NULL); + return(fOk); +} + +// Call this to initialize the structure. The cDrive parameter +// specifies the drive that this instance will access. The cbBuffer +// parameter specifies the size of the interal buffer used to read records +// from the journal. This should be large enough to hold several records +// (for example, 10 kilobytes will allow this class to buffer several +// dozen journal records at a time) +BOOL CDriveIndex::Init(WCHAR cDrive) +{ + // You should not call this function twice for one instance. + if (m_hVol != INVALID_HANDLE_VALUE) + DebugBreak(); + m_cDrive = cDrive; + ClearLastResult(); + BOOL fOk = FALSE; + __try { + // Open a handle to the volume + m_hVol = Open(m_cDrive, GENERIC_WRITE | GENERIC_READ); + if (INVALID_HANDLE_VALUE == m_hVol) + __leave; + fOk = TRUE; + } + __finally { + if (!fOk) + CleanUp(); + } + return(fOk); +} + +void CDriveIndex::ClearLastResult() +{ + LastResult = SearchResult(); +} + +// Adds a file to the database +BOOL CDriveIndex::Add(DWORDLONG Index, wstring *szName, DWORDLONG ParentIndex, DWORDLONG Filter) +{ + IndexedFile i; + i.Index = Index; + if(!Filter) + Filter = MakeFilter(szName); + i.Filter = Filter; + rgFiles.insert(rgFiles.end(), i); + return(TRUE); +} + + + +// Adds a directory to the database +BOOL CDriveIndex::AddDir(DWORDLONG Index, wstring *szName, DWORDLONG ParentIndex, DWORDLONG Filter) +{ + IndexedDirectory i; + i.Index = Index; + if(!Filter) + Filter = MakeFilter(szName); + i.Filter = Filter; + i.nFiles = 0; + rgDirectories.insert(rgDirectories.end(), i); + return(TRUE); +} + + + +// Calculates a 64bit value that is used to filter out many files before comparing their filenames +// This method gives a huge speed boost. +DWORDLONG CDriveIndex::MakeFilter(wstring *szName) +{ + /* + Creates an address that is used to filter out strings that don't contain the queried characters + Explanation of the meaning of the single bits: + 0-25 a-z + 26-35 0-9 + 36 . + 37 space + 38 !#$&'()+,-~_ + 39 2 same characters + 40 3 same characters + The fields below indicate the presence of 2-character sequences. Based off http://en.wikipedia.org/wiki/Letter_frequency + 41 TH + 42 HE + 43 AN + 44 RE + 45 ER + 46 IN + 47 ON + 48 AT + 49 ND + 50 ST + 51 ES + 52 EN + 53 OF + 54 TE + 55 ED + 56 OR + 57 TI + 58 HI + 59 AS + 60 TO + 61-63 length (max. 8 characters. Queries are usually shorter than this) + */ + if(!(szName->length() > 0)) + return 0; + DWORDLONG Address = 0; + WCHAR c; + wstring szlower(*szName); + transform(szlower.begin(), szlower.end(), szlower.begin(), tolower); + int counts[26] = {0}; //This array is used to check if characters occur two or three times in the string + wstring::size_type l = szlower.length(); + for(unsigned int i = 0; i != l; i++) + { + c = szlower[i]; + if(c > 96 && c < 123) //a-z + { + Address |= 1ui64 << (DWORDLONG)((DWORDLONG)c - 97ui64); + counts[c-97]++; + if(i < l - 1) + { + if(c == L't' && szlower[i+1] == L'h') //th + Address |= 1ui64 << 41; + else if(c == L'h' && szlower[i+1] == L'e') //he + Address |= 1ui64 << 41; + else if(c == L'a' && szlower[i+1] == L'n') //an + Address |= 1ui64 << 41; + else if(c == L'r' && szlower[i+1] == L'e') //re + Address |= 1ui64 << 41; + else if(c == L'e' && szlower[i+1] == L'r') //er + Address |= 1ui64 << 41; + else if(c == L'i' && szlower[i+1] == L'n') //in + Address |= 1ui64 << 41; + else if(c == L'o' && szlower[i+1] == L'n') //on + Address |= 1ui64 << 41; + else if(c == L'a' && szlower[i+1] == L't') //at + Address |= 1ui64 << 41; + else if(c == L'n' && szlower[i+1] == L'd') //nd + Address |= 1ui64 << 41; + else if(c == L's' && szlower[i+1] == L't') //st + Address |= 1ui64 << 41; + else if(c == L'e' && szlower[i+1] == L's') //es + Address |= 1ui64 << 41; + else if(c == L'e' && szlower[i+1] == L'n') //en + Address |= 1ui64 << 41; + else if(c == L'o' && szlower[i+1] == L'f') //of + Address |= 1ui64 << 41; + else if(c == L't' && szlower[i+1] == L'e') //te + Address |= 1ui64 << 41; + else if(c == L'e' && szlower[i+1] == L'd') //ed + Address |= 1ui64 << 41; + else if(c == L'o' && szlower[i+1] == L'r') //or + Address |= 1ui64 << 41; + else if(c == L't' && szlower[i+1] == L'i') //ti + Address |= 1ui64 << 41; + else if(c == L'h' && szlower[i+1] == L'i') //hi + Address |= 1ui64 << 41; + else if(c == L'a' && szlower[i+1] == L's') //as + Address |= 1ui64 << 41; + else if(c == L't' && szlower[i+1] == L'o') //to + Address |= 1ui64 << 41; + } + } + else if(c >= L'0' && c <= '9') //0-9 + Address |= 1ui64 << (c - L'0' + 26ui64); + else if(c == L'.') //. + Address |= 1ui64 << 36; + else if(c == L' ') // space + Address |= 1ui64 << 37; + else if(c == L'!' || c == L'#' || c == L'$' || c == L'&' || c == L'\'' || c == L'(' || c == L')' || c == L'+' || c == L',' || c == L'-' || c == L'~' || c == L'_') + Address |= 1ui64 << 38; // !#$&'()+,-~_ + } + for(unsigned int i = 0; i != 26; i++) + { + if(counts[i] == 2) + Address |= 1ui64 << 39; + else if(counts[i] > 2) + Address |= 1ui64 << 40; + } + DWORDLONG length = (szlower.length() > 7 ? 7ui64 : (DWORDLONG)szlower.length()) & 0x00000007ui64; //3 bits for length -> 8 max + Address |= length << 61ui64; + return Address; +} + + + +// Internal function for searching in the database. +// For projects in C++ which use this project it might be preferable to use this function +// to skip the wrapper. +// Returns: number of results, -1 if maxResults != -1 and not all results were found +int CDriveIndex::Find(wstring *strQuery, wstring *strQueryPath, vector *rgsrfResults, BOOL bSort, BOOL bEnhancedSearch, int maxResults) +{ + //These variables are used to control the flow of execution in this function. + + //Indicates where results should be searched + unsigned int SearchWhere = IN_FILES; + //Offset for vector marked by SearchWhere + unsigned int iOffset = 0; + //Used to skip the search when the previous two properties should be carried over to the next search without actually using them now. + BOOL bSkipSearch = false; + + //Number of results in this search. -1 if more than maximum number of results. + int nResults = 0; + + //No query, just ignore this call + if(strQuery->length() == 0) + { + // Store this query + LastResult.Query = wstring(TEXT("")); + LastResult.Results = vector(); + return nResults; + } + + if(strQueryPath != NULL) + { + //Check if the path actually matches the drive of this index + WCHAR szDrive[_MAX_DRIVE]; + _wsplitpath(strQueryPath->c_str(), szDrive, NULL, NULL, NULL); + for(unsigned int j = 0; j != _MAX_DRIVE; j++) + szDrive[j] = toupper(szDrive[j]); + if(wstring(szDrive).compare(wstring(1,toupper(m_cDrive))) == 0) + return 0; + } + + //Create lower query string for case-insensitive search + wstring strQueryLower(*strQuery); + for(unsigned int j = 0; j != strQueryLower.length(); j++) + strQueryLower[j] = tolower(strQueryLower[j]); + const WCHAR *szQueryLower = strQueryLower.c_str(); + + //Create lower query path string for case-insensitive search + wstring strQueryPathLower(strQueryPath != NULL ? *strQueryPath : TEXT("")); + for(unsigned int j = 0; j != strQueryPathLower.length(); j++) + strQueryPathLower[j] = tolower((*strQueryPath)[j]); + wstring* pstrQueryPathLower = strQueryPath != NULL && strQueryPathLower.length() > 0 ? &strQueryPathLower : NULL; + + //If the query path is different from the last query so that the results are not valid anymore, the last query needs to be dropped + if(!(strQueryPath != NULL && (LastResult.maxResults == -1 || LastResult.iOffset == 0) && (LastResult.SearchPath.length() == 0 || strQueryPathLower.find(LastResult.SearchPath) == 0))) + LastResult = SearchResult(); + + //Calculate Filter value and length of the current query which are compared with the cached ones to skip many of them + DWORDLONG QueryFilter = MakeFilter(&strQueryLower); + DWORDLONG QueryLength = (QueryFilter & 0xE000000000000000ui64) >> 61ui64; //Bits 61-63 for storing lengths up to 8 + QueryFilter = QueryFilter & 0x1FFFFFFFFFFFFFFFui64; //All but the last 3 bits + + //If the same query string as in the last query was used + if(strQueryLower.compare(LastResult.Query) == 0 && LastResult.Results.size() > 0 && (LastResult.SearchEndedWhere == NO_WHERE && iOffset != 1)) // need proper condition here to skip + { + //Keep the position of the last result + SearchWhere = LastResult.SearchEndedWhere; + iOffset = LastResult.iOffset; + bSkipSearch = true; + for(int i = 0; i != LastResult.Results.size(); i++) + { + BOOL bFound = true; + if(pstrQueryPathLower != NULL) + { + wstring strPathLower(LastResult.Results[i].Path); + for(unsigned int j = 0; j != strPathLower.length(); j++) + strPathLower[j] = tolower(LastResult.Results[i].Path[j]); + bFound = strPathLower.find(strQueryPathLower) != -1; + } + if(bFound) + { + nResults++; + //If the result limit has decreased and we have found all (shouldn't happen in common scenarios) + if(maxResults != -1 && nResults > maxResults) + { + nResults = -1; + + //If we get here, the next incremental should start fresh, but only if it requires more results than this one. + //To accomplish this we make this result contain no information about the origin of these results. + SearchWhere = NO_WHERE; + iOffset = 1; + break; + } + rgsrfResults->insert(rgsrfResults->end(), LastResult.Results[i]); + } + } + //if the last search was limited and didn't finish because it found enough files and we don't have the maximum number of results yet + //we need to continue the search where the last one stopped. + if(LastResult.maxResults != -1 && LastResult.SearchEndedWhere != NO_WHERE && (maxResults == -1 || nResults < maxResults)) + bSkipSearch = false; + } + //If this query is more specific than the previous one, it can use the results from the previous query + else if(strQueryLower.find(LastResult.Query) != -1 && LastResult.Results.size() > 0) + { + bSkipSearch = true; + //Keep the position of the last result + SearchWhere = LastResult.SearchEndedWhere; + iOffset = LastResult.iOffset; + FindInPreviousResults(*strQuery, szQueryLower, QueryFilter, QueryLength, pstrQueryPathLower, *rgsrfResults, 0, bEnhancedSearch, maxResults, nResults); + + //if the last search was limited and didn't finish because it found enough files and we don't have the maximum number of results yet + //we need to continue the search where the last one stopped. + if(LastResult.maxResults != -1 && LastResult.SearchEndedWhere != NO_WHERE && (maxResults == -1 || nResults < maxResults)) + bSkipSearch = false; + } + DWORDLONG FRNPath; + long long nFilesInDir = -1; + if(strQueryPath != NULL && strQueryPath->length()) + { + FRNPath = PathToFRN(strQueryPath); + wstring strPath2; + GetDir(FRNPath, &strPath2); + int iOffset = (int) FindDirOffsetByIndex(FRNPath); + if(iOffset != -1) + nFilesInDir = rgDirectories[iOffset].nFiles; + } + if(SearchWhere == IN_FILES && iOffset == 0 && nFilesInDir != -1 && nFilesInDir < 10000 && !bSkipSearch) + { + FindRecursively(*strQuery, szQueryLower, QueryFilter, QueryLength, strQueryPath, *rgsrfResults, bEnhancedSearch, maxResults, nResults); + SearchWhere = NO_WHERE; + } + else if(SearchWhere == IN_FILES && !bSkipSearch) + { + //Find in file index + FindInJournal(*strQuery, szQueryLower, QueryFilter, QueryLength, (strQueryPath != NULL ? &strQueryPathLower : NULL), rgFiles, *rgsrfResults, iOffset, bEnhancedSearch, maxResults, nResults); + //If we found the maximum number of results in the file index we stop here + if(maxResults != -1 && nResults == -1) + iOffset++; //Start with next entry on the next incremental search + else //Search isn't limited or not all results found yet, continue in directory index + { + SearchWhere = IN_DIRECTORIES; + iOffset = 0; + } + } + + if(SearchWhere == IN_DIRECTORIES && !bSkipSearch) + { + //Find in directory index + FindInJournal(*strQuery, szQueryLower, QueryFilter, QueryLength, pstrQueryPathLower, rgDirectories, *rgsrfResults, iOffset, bEnhancedSearch, maxResults, nResults); + //If we found the maximum number of results in the directory index we stop here + if(maxResults != -1 && nResults == -1) + iOffset++; //Start with next entry on the next incremental search + else //Search isn't limited or less than the maximum number of results found + { + SearchWhere = NO_WHERE; + iOffset = 0; + } + } + + //Sort by match quality and name + if(bSort) + sort(rgsrfResults->begin(), rgsrfResults->end()); + + // Store this query + LastResult.Query = wstring(strQueryLower); + + // Store search path + LastResult.SearchPath = strQueryPathLower; + + //Clear old results, they will be replaced with the current ones + LastResult.Results = vector(); + + //Store number of results (Needed for incremental search) + LastResult.iOffset = iOffset; + + //Store if this search was limited + LastResult.maxResults = maxResults; + + //Store where the current search ended due to file limit (or if it didn't); + LastResult.SearchEndedWhere = SearchWhere; + + //Update last results + for(unsigned int i = 0; i != rgsrfResults->size(); i++) + LastResult.Results.insert(LastResult.Results.end(), (*rgsrfResults)[i]); + + return nResults; +} + +void CDriveIndex::FindRecursively(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring* strQueryPath, vector &rgsrfResults, BOOL bEnhancedSearch, int maxResults, int &nResults) +{ + WIN32_FIND_DATA ffd; + size_t length_of_arg; + HANDLE hFind = INVALID_HANDLE_VALUE; + + // Check that the input path plus 3 is not longer than MAX_PATH. + // Three characters are for the "\*" plus NULL appended below. + length_of_arg = strQueryPath->length(); + if (length_of_arg > (MAX_PATH - 3)) + return; + + // Prepare string for use with FindFile functions. First, copy the + // string to a buffer, then append '\*' to the directory name. + wstring strPath = wstring(*strQueryPath); + if((*strQueryPath)[strQueryPath->length() - 1] != L'\\') + strPath += wstring(TEXT("\\*")); + else + strPath += wstring(TEXT("*")); + + const WCHAR* szDir = strPath.c_str(); + + // Find the first file in the directory. + hFind = FindFirstFile(szDir, &ffd); + + if (hFind == INVALID_HANDLE_VALUE) + return; + unsigned int nFiles = 0; + // List all the files in the directory with some info about them. + do + { + if(ffd.dwFileAttributes & FILE_ATTRIBUTE_VIRTUAL || ffd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) + continue; + float MatchQuality; + wstring strFilename(ffd.cFileName); + if(strFilename.compare(TEXT(".")) == 0 || strFilename.compare(TEXT("..")) == 0) + continue; + nFiles++; + if(bEnhancedSearch) + MatchQuality = FuzzySearch(strFilename, strQuery); + else + { + wstring szLower(strFilename); + for(unsigned int j = 0; j != szLower.length(); j++) + szLower[j] = tolower(szLower[j]); + MatchQuality = szLower.find(strQuery) != -1; + } + + if(MatchQuality > 0.6f) + { + nResults++; + if(maxResults != -1 && nResults > maxResults) + { + nResults = -1; + break; + } + SearchResultFile srf; + srf.Filename = strFilename; + srf.Path = *strQueryPath + TEXT("\\"); + srf.Filter = MAXULONG64; + srf.MatchQuality = MatchQuality; + rgsrfResults.insert(rgsrfResults.end(), srf); + } + + if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + wstring strSubPath = wstring(*strQueryPath); + if((*strQueryPath)[strQueryPath->length() - 1] != L'\\') + strSubPath += L'\\'; + strSubPath += ffd.cFileName; + FindRecursively(strQuery, szQueryLower, QueryFilter, QueryLength, &strSubPath, rgsrfResults, bEnhancedSearch, maxResults, nResults); + if(nResults == -1) + break; + } + } + while (FindNextFile(hFind, &ffd) != 0); + FindClose(hFind); +} + +//T needs to be IndexedFile or IndexedDirectory +template +void CDriveIndex::FindInJournal(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring * strQueryPath, vector &rgJournalIndex, vector &rgsrfResults, unsigned int iOffset, BOOL bEnhancedSearch, int maxResults, int &nResults) +{ + for(unsigned int j = 0; j != rgJournalIndex.size(); j++) + { + IndexedFile* i = (IndexedFile*)&rgJournalIndex[j]; + DWORDLONG Length = (i->Filter & 0xE000000000000000ui64) >> 61ui64; //Bits 61-63 for storing lengths up to 8 + DWORDLONG Filter = i->Filter & 0x1FFFFFFFFFFFFFFFui64; //All but the last 3 bits + if((Filter & QueryFilter) == QueryFilter && QueryLength <= Length) + { + USNEntry file = FRNToName(i->Index); + float MatchQuality; + if(bEnhancedSearch) + MatchQuality = FuzzySearch(file.Name, strQuery); + else + { + wstring szLower(file.Name); + for(unsigned int j = 0; j != szLower.length(); j++) + szLower[j] = tolower(szLower[j]); + MatchQuality = szLower.find(strQuery) != -1; + } + + if(MatchQuality > 0.6f) + { + nResults++; + if(maxResults != -1 && nResults > maxResults) + { + nResults = -1; + break; + } + SearchResultFile srf; + srf.Filename = file.Name; + srf.Path.reserve(MAX_PATH); + Get(i->Index, &srf.Path); + BOOL bFound = true; + if(strQueryPath != NULL) + { + wstring strPathLower(srf.Path); + for(unsigned int j = 0; j != strPathLower.length(); j++) + strPathLower[j] = tolower(strPathLower[j]); + bFound = strPathLower.find(*strQueryPath) != -1; + } + if(bFound) + { + //split path + WCHAR szDrive[_MAX_DRIVE]; + WCHAR szPath[_MAX_PATH]; + WCHAR szName[_MAX_FNAME]; + WCHAR szExt[_MAX_EXT]; + _wsplitpath(srf.Path.c_str(), szDrive, szPath, szName, szExt); + srf.Path = wstring(szDrive) + wstring(szPath); + srf.Filter = i->Filter; + srf.MatchQuality = MatchQuality; + rgsrfResults.insert(rgsrfResults.end(), srf); + } + } + } + } +} +void CDriveIndex::FindInPreviousResults(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring * strQueryPath, vector &rgsrfResults, unsigned int iOffset, BOOL bEnhancedSearch, int maxResults, int &nResults) +{ + for(int i = 0; i != LastResult.Results.size() && (maxResults == -1 || i < maxResults); i++) + { + SearchResultFile *srf = & LastResult.Results[i]; + DWORDLONG Length = (srf->Filter & 0xE000000000000000ui64) >> 61ui64; //Bits 61-63 for storing lengths up to 8 + DWORDLONG Filter = srf->Filter & 0x1FFFFFFFFFFFFFFFui64; //All but the last 3 bits + if((Filter & QueryFilter) == QueryFilter && QueryLength <= Length) + { + if(bEnhancedSearch) + srf->MatchQuality = FuzzySearch(srf->Filename, strQuery); + else + { + wstring szLower(srf->Filename); + for(unsigned int j = 0; j != szLower.length(); j++) + szLower[j] = tolower(szLower[j]); + srf->MatchQuality = szLower.find(szQueryLower) != -1; + } + if(srf->MatchQuality > 0.6f) + { + BOOL bFound = true; + if(strQueryPath != NULL) + { + wstring strPathLower(srf->Path); + for(unsigned int j = 0; j != srf->Path.length(); j++) + strPathLower[j] = tolower(srf->Path[j]); + bFound = strPathLower.find(*strQueryPath) != -1; + } + if(bFound) + { + nResults++; + if(maxResults != -1 && nResults > maxResults) + { + nResults = -1; + break; + } + rgsrfResults.insert(rgsrfResults.end(), *srf); + } + } + } + } +} + + +// Clears the database +BOOL CDriveIndex::Empty() +{ + rgFiles.clear(); + rgDirectories.clear(); + return(TRUE); +} + + + +// Constructs a path for a file +BOOL CDriveIndex::Get(DWORDLONG Index, wstring *sz) const +{ + *sz = TEXT(""); + int n = 0; + do { + USNEntry file = FRNToName(Index); + *sz = file.Name + ((n != 0) ? TEXT("\\") : TEXT("")) + *sz; + Index = file.ParentIndex; + n++; + } while (Index != 0); + return(TRUE); +} + + + +// Constructs a path for a directory +BOOL CDriveIndex::GetDir(DWORDLONG Index, wstring *sz) const +{ + *sz = TEXT(""); + do { + USNEntry file = FRNToName(Index); + *sz = file.Name + ((sz->length() != 0) ? TEXT("\\") : TEXT("")) + *sz; + Index = file.ParentIndex; + } while (Index != 0); + return(TRUE); +} + + + +//Finds the position of a file in the database by the FileReferenceNumber +INT64 CDriveIndex::FindOffsetByIndex(DWORDLONG Index) { + + vector::difference_type pos; + IndexedFile i; + i.Index = Index; + pos = distance(rgFiles.begin(), lower_bound(rgFiles.begin(), rgFiles.end(), i)); + return (INT64) (pos == rgFiles.size() ? -1 : pos); // this is valid because the number of files doesn't exceed the range of INT64 +} + + + +//Finds the position of a directory in the database by the FileReferenceNumber +INT64 CDriveIndex::FindDirOffsetByIndex(DWORDLONG Index) +{ + vector::difference_type pos; + IndexedDirectory i; + i.Index = Index; + pos = distance(rgDirectories.begin(), lower_bound(rgDirectories.begin(), rgDirectories.end(), i)); + return (INT64) (pos == rgDirectories.size() ? -1 : pos); // this is valid because the number of files doesn't exceed the range of INT64 +} + +DWORDLONG PathToFRN(wstring* strPath) +{ + HANDLE hDir = CreateFile(strPath->c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if(hDir == INVALID_HANDLE_VALUE) + return 0; + BY_HANDLE_FILE_INFORMATION fi; + GetFileInformationByHandle(hDir, &fi); + CloseHandle(hDir); + return (((DWORDLONG) fi.nFileIndexHigh) << 32) | fi.nFileIndexLow; +} + +// Enumerate the MFT for all entries. Store the file reference numbers of +// any directories in the database. +void CDriveIndex::PopulateIndex() +{ + Empty(); + + vector FileParents; + vector DirectoryParents; + + USN_JOURNAL_DATA ujd; + Query(&ujd); + + // Get the FRN of the root directory + // This had BETTER work, or we can't do anything + + WCHAR szRoot[_MAX_PATH]; + wsprintf(szRoot, TEXT("%c:\\"), m_cDrive); + HANDLE hDir = CreateFile(szRoot, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + + BY_HANDLE_FILE_INFORMATION fi; + GetFileInformationByHandle(hDir, &fi); + CloseHandle(hDir); + DWORDLONG IndexRoot = (((DWORDLONG) fi.nFileIndexHigh) << 32) | fi.nFileIndexLow; + wsprintf(szRoot, TEXT("%c:"), m_cDrive); + AddDir(IndexRoot, &wstring(szRoot), 0); + DirectoryParents.insert(DirectoryParents.end(), 0); + m_dwDriveFRN = IndexRoot; + + LEGACY_MFT_ENUM_DATA med; + med.StartFileReferenceNumber = 0; + med.LowUsn = 0; + med.HighUsn = ujd.NextUsn; + + // Process MFT in 64k chunks + BYTE pData[sizeof(DWORDLONG) + 0x10000]; + DWORDLONG fnLast = 0; + DWORD cb; + unsigned int num = 0; + unsigned int numDirs = 1; + while (DeviceIoControl(m_hVol, FSCTL_ENUM_USN_DATA, &med, sizeof(med), pData, sizeof(pData), &cb, NULL) != FALSE) { + + PUSN_RECORD pRecord = (PUSN_RECORD) &pData[sizeof(USN)]; + while ((PBYTE) pRecord < (pData + cb)) { + if ((pRecord->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) + numDirs++; + else + num++; + pRecord = (PUSN_RECORD) ((PBYTE) pRecord + pRecord->RecordLength); + } + med.StartFileReferenceNumber = * (DWORDLONG *) pData; + } + DWORD err1 = GetLastError(); + + FileParents.reserve(num); + DirectoryParents.reserve(numDirs); + rgFiles.reserve(num); + rgDirectories.reserve(numDirs); + hash_map hmFiles; + hash_map hmDirectories; + hash_map::iterator it; + med.StartFileReferenceNumber = 0; + while (DeviceIoControl(m_hVol, FSCTL_ENUM_USN_DATA, &med, sizeof(med), pData, sizeof(pData), &cb, NULL) != FALSE) + { + PUSN_RECORD pRecord = (PUSN_RECORD) &pData[sizeof(USN)]; + while ((PBYTE) pRecord < (pData + cb)) + { + wstring sz((LPCWSTR) ((PBYTE) pRecord + pRecord->FileNameOffset), pRecord->FileNameLength / sizeof(WCHAR)); + if ((pRecord->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) + { + AddDir(pRecord->FileReferenceNumber, &sz, pRecord->ParentFileReferenceNumber); + //DirectoryParents.insert(DirectoryParents.end(), pRecord->ParentFileReferenceNumber); + HashMapEntry hme; + hme.iOffset = rgDirectories.size() - 1; + hme.ParentFRN = pRecord->ParentFileReferenceNumber; + hmDirectories[pRecord->FileReferenceNumber] = hme; + } + else + { + Add(pRecord->FileReferenceNumber, &sz, pRecord->ParentFileReferenceNumber); + HashMapEntry hme; + hme.iOffset = rgFiles.size() - 1; + hme.ParentFRN = pRecord->ParentFileReferenceNumber; + //FileParents.insert(FileParents.end(), pRecord->ParentFileReferenceNumber); + hmFiles[pRecord->FileReferenceNumber] = hme; + } + pRecord = (PUSN_RECORD) ((PBYTE) pRecord + pRecord->RecordLength); + } + med.StartFileReferenceNumber = * (DWORDLONG *) pData; + } + err1 = GetLastError(); + + //Calculate files per directory. This takes most of the indexing time, but this information can be useful to reduce the time needed + //for searching in directories with few files (less than 10k). + for ( it=hmFiles.begin() ; it != hmFiles.end(); it++ ) + { + HashMapEntry* hme = &hmDirectories[it->second.ParentFRN]; + do + { + rgDirectories[hme->iOffset].nFiles++; + HashMapEntry* hme2 = &hmDirectories[it->second.ParentFRN]; + + if(hme != hme2) + hme = hme2; + else // This must not happen, otherwise a directory is its own parent! + break; + } while(hme->ParentFRN != 0); + } + //for(unsigned int i = 0; i != FileParents.size(); i++) + //{ + // DWORDLONG dwIndex = FileParents[i]; + // while(dwIndex != 0) + // { + // int iOffset = -1; + // for(unsigned int j = 0; j != rgDirectories.size(); j++) + // if(rgDirectories[j].Index == dwIndex) + // { + // iOffset = j; + // break; + // } + // if(iOffset == -1) + // break; + // rgDirectories[iOffset].nFiles++; + // DWORDLONG dwIndex2 = DirectoryParents[iOffset]; + + // if(dwIndex != dwIndex2) + // dwIndex = dwIndex2; + // else // This must not happen, otherwise a directory is its own parent! + // break; + // } + // //wstring strPath; + // //GetDir(dwIndex, &strPath); + + // //do { + // // //USNEntry file = FRNToName(dwIndex); + // // int iOffset = -1; + // // for(int j = 0; j != rgDirectories.size(); j++) + // // if(rgDirectories[j].Index == dwIndex) + // // { + // // iOffset = j; + // // break; + // // } + // // if(iOffset == -1) + // // break; + // // //USNEntry parent = FRNToName(file.ParentIndex); + // // //USNEntry parent2 = FRNToName(rgDirectories[iOffset].Index); + // // rgDirectories[iOffset].nFiles++; + // // dwIndex = file.ParentIndex; + // //} while (dwIndex != 0); + //} + rgFiles.shrink_to_fit(); + rgDirectories.shrink_to_fit(); + sort(rgFiles.begin(), rgFiles.end()); + sort(rgDirectories.begin(), rgDirectories.end()); +} + +// Resolve FRN to filename by enumerating USN journal with StartFileReferenceNumber=FRN +USNEntry CDriveIndex::FRNToName(DWORDLONG FRN) const +{ + if(FRN == m_dwDriveFRN) + return USNEntry(wstring(1, m_cDrive) + wstring(TEXT(":")), 0); + USN_JOURNAL_DATA ujd; + Query(&ujd); + + LEGACY_MFT_ENUM_DATA med; + med.StartFileReferenceNumber = FRN; + med.LowUsn = 0; + med.HighUsn = ujd.NextUsn; + + // The structure only needs a single entry so it can be pretty small + BYTE pData[sizeof(DWORDLONG) + 0x300]; + DWORD cb; + while (DeviceIoControl(m_hVol, FSCTL_ENUM_USN_DATA, &med, sizeof(med), pData, sizeof(pData), &cb, NULL) != FALSE) { + + PUSN_RECORD pRecord = (PUSN_RECORD) &pData[sizeof(USN)]; + while ((PBYTE) pRecord < (pData + cb)) { + if(pRecord->FileReferenceNumber == FRN) + return USNEntry(wstring((LPCWSTR) ((PBYTE) pRecord + pRecord->FileNameOffset), pRecord->FileNameLength / sizeof(WCHAR)), pRecord->ParentFileReferenceNumber); + pRecord = (PUSN_RECORD) ((PBYTE) pRecord + pRecord->RecordLength); + } + med.StartFileReferenceNumber = * (DWORDLONG *) pData; + } + return USNEntry(wstring(TEXT("")), 0); +} + + + +// Saves the database to disk. The file can be used to create an instance of CDriveIndex. +BOOL CDriveIndex::SaveToDisk(wstring &strPath) const +{ + ofstream::pos_type size; + ofstream file (strPath.c_str(), ios::out|ios::binary|ios::trunc); + if (file.is_open()) + { + //Drive character + file.write((char*) &m_cDrive, sizeof(m_cDrive)); + + //Drive FileReferenceNumber + file.write((char*) &m_dwDriveFRN, sizeof(m_dwDriveFRN)); + + unsigned int size = rgFiles.size(); + //Number of files + file.write((char*) &size, sizeof(rgFiles.size())); + //indexed files + file.write((char*) &(rgFiles[0]), sizeof(IndexedFile) * rgFiles.size()); + + size = rgDirectories.size(); + //Number of directories + file.write((char*) &size, sizeof(rgDirectories.size())); + //indexed directories + file.write((char*) &(rgDirectories[0]), sizeof(IndexedDirectory) * rgDirectories.size()); + file.close(); + return true; + } + return false; +} + + + +// Constructor for loading the index from a previously saved file +CDriveIndex::CDriveIndex(wstring &strPath) +{ + m_hVol = INVALID_HANDLE_VALUE; + Empty(); + + ifstream::pos_type size; + + ifstream file (strPath.c_str(), ios::in | ios::binary); + if (file.is_open()) + { + //Drive + WCHAR cDrive; + file.read((char*) &cDrive, sizeof(WCHAR)); + + if(Init(cDrive)) + { + // Drive FileReferenceNumber + file.read((char*) &m_dwDriveFRN, sizeof(m_dwDriveFRN)); + + //Number of files + unsigned int numFiles = 0; + file.read((char*) &numFiles, sizeof(numFiles)); + rgFiles.reserve(numFiles); + + //indexed files + for(unsigned int j = 0; j != numFiles; j++) + { + IndexedFile i; + file.read((char*) &i, sizeof(IndexedFile)); + rgFiles.insert(rgFiles.end(), i); + } + + //Number of directories + unsigned int numDirs = 0; + file.read((char*) &numDirs, sizeof(numDirs)); + rgDirectories.reserve(numDirs); + + //indexed directories + for(unsigned int j = 0; j != numDirs; j++) + { + IndexedDirectory i; + file.read((char*) &i, sizeof(IndexedDirectory)); + rgDirectories.insert(rgDirectories.end(), i); + } + } + file.close(); + } + return; +} + + + +// Returns the number of files and folders on this drive +DriveInfo CDriveIndex::GetInfo() const +{ + DriveInfo di; + di.NumFiles = (DWORDLONG) rgFiles.size(); + di.NumDirectories = (DWORDLONG) rgDirectories.size(); + return di; +} + + + + +//Performs a fuzzy search for shorter in longer. +//return values range from 0.0 = identical to 1.0 = completely different. 0.4 seems appropriate +float FuzzySearch(wstring &longer, wstring &shorter) +{ + //Note: All string lengths are shorter than MAX_PATH, so an uint is perfectly fitted. + unsigned int lenl = (unsigned int) longer.length(); + unsigned int lens = (unsigned int) shorter.length(); + + if(lens > lenl) + return 0.0f; + + //Check if the shorter string is a substring of the longer string + unsigned int Contained = (unsigned int) longer.find(shorter); + if(Contained != wstring::npos) + return Contained == 0 ? 1.0f : 0.8f; + + wstring longerlower(longer); + wstring shorterlower(shorter); + for(unsigned int i = 0; i != lenl; i++) + longerlower[i] = tolower(longer[i]); + for(unsigned int i = 0; i != lens; i++) + shorterlower[i] = tolower(shorter[i]); + + //Check if the shorter string is a substring of the longer string + Contained = (unsigned int) longerlower.find(shorterlower); + if(Contained != wstring::npos) + return Contained == 0 ? 0.9f : 0.7f; + + //Check if string can be matched by omitting characters + if(lens < 5) + { + unsigned int pos = 0; + unsigned int matched = 0; + for(unsigned int i = 0; i != lens; i++) + { + WCHAR c = toupper(shorter[i]); //only look for capital letters in longer string, (e.g. match tc in TrueCrypt) + for(unsigned int j = 0; j != lenl - pos; j++) + { + if(longer[pos + j] == c) + { + pos = j; + matched++; + break; + } + else + continue; + } + } + if(matched == lens) + return 0.9f; //Slightly worse than direct matches + } + return 0; } \ No newline at end of file diff --git a/FileSearch/CDriveIndex.h b/FileSearch/CDriveIndex.h index 740e49e..1e3454b 100644 --- a/FileSearch/CDriveIndex.h +++ b/FileSearch/CDriveIndex.h @@ -12,7 +12,9 @@ Credits for original code this is based on: Jeffrey Cooperstein & Jeffrey Richte #include #include #include -#include +#include +#include "tinyxml2.h" + using namespace std; #define NO_WHERE 0 @@ -133,6 +135,9 @@ struct SearchResult } }; class CDriveIndex { +public: + enum ExportFormat {ExportFormatAdcXml, ExportFormatAdcXml_LZ4}; + public: CDriveIndex(); CDriveIndex(wstring &strPath); @@ -140,29 +145,31 @@ class CDriveIndex { BOOL Init(WCHAR cDrive); int Find(wstring *strQuery, wstring *strPath, vector *rgsrfResults, BOOL bSort = true, BOOL bEnhancedSearch = true, int maxResults = -1); void PopulateIndex(); - BOOL SaveToDisk(wstring &strPath); - DriveInfo GetInfo(); + BOOL SaveToDisk(wstring &strPath) const; + BOOL ExportToFileListing(wstring &strPath, int format) const; + DriveInfo GetInfo() const; protected: BOOL Empty(); HANDLE Open(WCHAR cDriveLetter, DWORD dwAccess); BOOL Create(DWORDLONG MaximumSize, DWORDLONG AllocationDelta); - BOOL Query(PUSN_JOURNAL_DATA pUsnJournalData); + BOOL Query(PUSN_JOURNAL_DATA pUsnJournalData) const; void FindRecursively(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring * strQueryPath, vector &rgsrfResults, BOOL bEnhancedSearch, int maxResults, int &nResults); template void FindInJournal(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring * strQueryPath, vector &rgJournalIndex, vector &rgsrfResults, unsigned int iOffset, BOOL bEnhancedSearch, int maxResults, int &nResults); void FindInPreviousResults(wstring &strQuery, const WCHAR* &szQueryLower, DWORDLONG QueryFilter, DWORDLONG QueryLength, wstring * strQueryPath, vector &rgsrfResults, unsigned int iOffset, BOOL bEnhancedSearch, int maxResults, int &nResults); + void attach(vector &dirHandles, unordered_map::size_type> &umDirFrnToHandle, int NodeType, DWORDLONG Index) const; INT64 FindOffsetByIndex(DWORDLONG Index); INT64 FindDirOffsetByIndex(DWORDLONG Index); DWORDLONG MakeFilter(wstring *szName); - USNEntry FRNToName(DWORDLONG FRN); + USNEntry FRNToName(DWORDLONG FRN) const; void CleanUp(); BOOL Add(DWORDLONG Index, wstring *szName, DWORDLONG ParentIndex, DWORDLONG Address = 0); BOOL AddDir(DWORDLONG Index, wstring *szName, DWORDLONG ParentIndex, DWORDLONG Address = 0); - BOOL Get(DWORDLONG Index, wstring *sz); - BOOL GetDir(DWORDLONG Index, wstring *sz); - unsigned int GetParentDirectory(DWORDLONG Index); + BOOL Get(DWORDLONG Index, wstring *sz) const; + BOOL GetDir(DWORDLONG Index, wstring *sz) const; + //unsigned int GetParentDirectory(DWORDLONG Index); void ClearLastResult(); // Members used to enumerate journal records HANDLE m_hVol; // handle to volume @@ -184,4 +191,6 @@ WCHAR* _stdcall Search(CDriveIndex *di, WCHAR *szQuery, WCHAR *szPath, BOOL bSor void _stdcall FreeResultsBuffer(WCHAR *szResults); BOOL _stdcall SaveIndexToDisk(CDriveIndex *di, WCHAR *szPath); CDriveIndex* _stdcall LoadIndexFromDisk(WCHAR *szPath); -void _stdcall GetDriveInfo(CDriveIndex *di, DriveInfo *driveInfo); \ No newline at end of file +void _stdcall GetDriveInfo(CDriveIndex *di, DriveInfo *driveInfo); +BOOL _stdcall SaveIndexToDisk(CDriveIndex *di, WCHAR *szPath); +BOOL _stdcall ExportIndex(CDriveIndex *di, WCHAR *szPath, int format); diff --git a/FileSearch/FileSearch.def b/FileSearch/FileSearch.def index 5f2f91d..e612086 100644 --- a/FileSearch/FileSearch.def +++ b/FileSearch/FileSearch.def @@ -7,4 +7,5 @@ EXPORTS FreeResultsBuffer @4 LoadIndexFromDisk @5 SaveIndexToDisk @6 - GetDriveInfo @7 \ No newline at end of file + GetDriveInfo @7 + ExportIndex @8 \ No newline at end of file diff --git a/FileSearch/FileSearch.vcxproj b/FileSearch/FileSearch.vcxproj index cae9dbb..bf1700a 100644 --- a/FileSearch/FileSearch.vcxproj +++ b/FileSearch/FileSearch.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -28,23 +28,27 @@ DynamicLibrary true Unicode + v120 DynamicLibrary true Unicode + v120 DynamicLibrary false true Unicode + v120 DynamicLibrary false true Unicode + v120 @@ -73,6 +77,7 @@ false + C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib\x64;$(LibraryPath) @@ -144,8 +149,11 @@ + + + @@ -163,12 +171,30 @@ + + NotUsing + NotUsing + NotUsing + NotUsing + + + NotUsing + NotUsing + NotUsing + NotUsing + Create - Create Create + Create Create + + NotUsing + NotUsing + NotUsing + NotUsing + diff --git a/FileSearch/FileSearch.vcxproj.filters b/FileSearch/FileSearch.vcxproj.filters index a3bc8e4..00fe964 100644 --- a/FileSearch/FileSearch.vcxproj.filters +++ b/FileSearch/FileSearch.vcxproj.filters @@ -30,6 +30,15 @@ Headerdateien + + Headerdateien + + + Headerdateien + + + Headerdateien + @@ -41,5 +50,14 @@ Quelldateien + + Quelldateien + + + Quelldateien + + + Quelldateien + \ No newline at end of file diff --git a/FileSearch/FileSearch.vcxproj.user b/FileSearch/FileSearch.vcxproj.user index 1710a9f..9cd3b79 100644 --- a/FileSearch/FileSearch.vcxproj.user +++ b/FileSearch/FileSearch.vcxproj.user @@ -1,15 +1,27 @@  + + C:\Program Files (x86)\AutoHotkey\AutoHotkey.exe + WindowsLocalDebugger + $(OutDir)Exportall.ahk + $(OutDir) + - C:\Program Files\Autohotkey\Autohotkey.exe + C:\Program Files (x86)\AutoHotkey\AutoHotkey.exe WindowsLocalDebugger - $(OutDir)FileSearchTest.ahk + $(OutDir)Exportall.ahk $(OutDir) - - C:\Program Files\Autohotkey\AutoHotkey.exe + + C:\Program Files\AutoHotkey\AutoHotkey.exe + WindowsLocalDebugger + $(OutDir)Exportall.ahk + $(OutDir) + + + C:\Program Files\AutoHotkey\AutoHotkey.exe WindowsLocalDebugger - $(OutDir)FileSearchTest.ahk + $(OutDir)Exportall.ahk $(OutDir) \ No newline at end of file diff --git a/FileSearch/gel.cpp b/FileSearch/gel.cpp new file mode 100644 index 0000000..514b062 --- /dev/null +++ b/FileSearch/gel.cpp @@ -0,0 +1,38 @@ +#include "stxutif.h" +#include + +#ifdef __MINGW32_VERSION +#ifndef _GLIBCXX_USE_WCHAR_T + +namespace std +{ + //instantiate the codecvt facet id for the required types + locale::id codecvt::id; +} + +#endif //_GLIBCXX_USE_WCHAR_T +#endif //__MINGW32_VERSION + + +namespace gel +{namespace stdx +{ +#if 0 + //instantiate the global locale + std::locale utf8_locale(std::locale(), new std::codecvt_utf8()); // VS2010 and further + + // convert wstring to UTF-8 string + std::string wstring_to_utf8 (const std::wstring& str) { + std::wstring_convert> myconv; + return myconv.to_bytes(str); + } +#else + + std::wstring_convert, wchar_t> conversion; + std::string wstring_to_utf8(const std::wstring& str) { + return conversion.to_bytes(str); + } + +#endif + +}} diff --git a/FileSearch/lz4.c b/FileSearch/lz4.c new file mode 100644 index 0000000..7fef165 --- /dev/null +++ b/FileSearch/lz4.c @@ -0,0 +1,819 @@ +/* + LZ4 - Fast LZ compression algorithm + Copyright (C) 2011-2013, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html + - LZ4 source repository : http://code.google.com/p/lz4/ +*/ + +//************************************** +// Tuning parameters +//************************************** +// MEMORY_USAGE : +// Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) +// Increasing memory usage improves compression ratio +// Reduced memory usage can improve speed, due to cache effect +// Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache +#define MEMORY_USAGE 14 + +// HEAPMODE : +// Select how default compression functions will allocate memory for their hash table, +// in memory stack (0:default, fastest), or in memory heap (1:requires memory allocation (malloc)). +#define HEAPMODE 0 + + +//************************************** +// CPU Feature Detection +//************************************** +// 32 or 64 bits ? +#if (defined(__x86_64__) || defined(_M_X64) || defined(_WIN64) \ + || defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) \ + || defined(__64BIT__) || defined(_LP64) || defined(__LP64__) \ + || defined(__ia64) || defined(__itanium__) || defined(_M_IA64) ) // Detects 64 bits mode +# define LZ4_ARCH64 1 +#else +# define LZ4_ARCH64 0 +#endif + +// Little Endian or Big Endian ? +// Overwrite the #define below if you know your architecture endianess +#if defined (__GLIBC__) +# include +# if (__BYTE_ORDER == __BIG_ENDIAN) +# define LZ4_BIG_ENDIAN 1 +# endif +#elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN)) +# define LZ4_BIG_ENDIAN 1 +#elif defined(__sparc) || defined(__sparc__) \ + || defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) \ + || defined(__hpux) || defined(__hppa) \ + || defined(_MIPSEB) || defined(__s390__) +# define LZ4_BIG_ENDIAN 1 +#else +// Little Endian assumed. PDP Endian and other very rare endian format are unsupported. +#endif + +// Unaligned memory access is automatically enabled for "common" CPU, such as x86. +// For others CPU, such as ARM, the compiler may be more cautious, inserting unnecessary extra code to ensure aligned access property +// If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance +#if defined(__ARM_FEATURE_UNALIGNED) +# define LZ4_FORCE_UNALIGNED_ACCESS 1 +#endif + +// Define this parameter if your target system or compiler does not support hardware bit count +#if defined(_MSC_VER) && defined(_WIN32_WCE) // Visual Studio for Windows CE does not support Hardware bit count +# define LZ4_FORCE_SW_BITCOUNT +#endif + +// BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE : +// This option may provide a small boost to performance for some big endian cpu, although probably modest. +// You may set this option to 1 if data will remain within closed environment. +// This option is useless on Little_Endian CPU (such as x86) +//#define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1 + + +//************************************** +// Compiler Options +//************************************** +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) // C99 +/* "restrict" is a known keyword */ +#else +# define restrict // Disable restrict +#endif + +#ifdef _MSC_VER // Visual Studio +# define FORCE_INLINE static __forceinline +# include // For Visual 2005 +# if LZ4_ARCH64 // 64-bits +# pragma intrinsic(_BitScanForward64) // For Visual 2005 +# pragma intrinsic(_BitScanReverse64) // For Visual 2005 +# else // 32-bits +# pragma intrinsic(_BitScanForward) // For Visual 2005 +# pragma intrinsic(_BitScanReverse) // For Visual 2005 +# endif +# pragma warning(disable : 4127) // disable: C4127: conditional expression is constant +#else +# ifdef __GNUC__ +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +#endif + +#ifdef _MSC_VER +# define lz4_bswap16(x) _byteswap_ushort(x) +#else +# define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))) +#endif + +#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + +#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__) +# define expect(expr,value) (__builtin_expect ((expr),(value)) ) +#else +# define expect(expr,value) (expr) +#endif + +#define likely(expr) expect((expr) != 0, 1) +#define unlikely(expr) expect((expr) != 0, 0) + + +//************************************** +// Memory routines +//************************************** +#include // malloc, calloc, free +#define ALLOCATOR(n,s) calloc(n,s) +#define FREEMEM free +#include // memset, memcpy +#define MEM_INIT memset + + +//************************************** +// Includes +//************************************** +#include "lz4.h" + + +//************************************** +// Basic Types +//************************************** +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + +typedef const BYTE* Ptr; + +#if defined(__GNUC__) && !defined(LZ4_FORCE_UNALIGNED_ACCESS) +# define _PACKED __attribute__ ((packed)) +#else +# define _PACKED +#endif + +#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__) +# ifdef __IBMC__ +# pragma pack(1) +# else +# pragma pack(push, 1) +# endif +#endif + +typedef struct { U16 v; } _PACKED U16_S; +typedef struct { U32 v; } _PACKED U32_S; +typedef struct { U64 v; } _PACKED U64_S; +typedef struct {size_t v;} _PACKED size_t_S; + +#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__) +# pragma pack(pop) +#endif + +#define A16(x) (((U16_S *)(x))->v) +#define A32(x) (((U32_S *)(x))->v) +#define A64(x) (((U64_S *)(x))->v) +#define AARCH(x) (((size_t_S *)(x))->v) + + +//************************************** +// Constants +//************************************** +#define LZ4_HASHLOG (MEMORY_USAGE-2) +#define HASHTABLESIZE (1 << MEMORY_USAGE) +#define HASHNBCELLS4 (1 << LZ4_HASHLOG) + +#define MINMATCH 4 + +#define COPYLENGTH 8 +#define LASTLITERALS 5 +#define MFLIMIT (COPYLENGTH+MINMATCH) +const int LZ4_minLength = (MFLIMIT+1); + +#define LZ4_64KLIMIT ((1<<16) + (MFLIMIT-1)) +#define SKIPSTRENGTH 6 // Increasing this value will make the compression run slower on incompressible data + +#define MAXD_LOG 16 +#define MAX_DISTANCE ((1 << MAXD_LOG) - 1) + +#define ML_BITS 4 +#define ML_MASK ((1U<=e; + + +//**************************** +// Private functions +//**************************** +#if LZ4_ARCH64 + +FORCE_INLINE int LZ4_NbCommonBytes (register U64 val) +{ +# if defined(LZ4_BIG_ENDIAN) +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r = 0; + _BitScanReverse64( &r, val ); + return (int)(r>>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_clzll(val) >> 3); +# else + int r; + if (!(val>>32)) { r=4; } else { r=0; val>>=32; } + if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } + r += (!val); + return r; +# endif +# else +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r = 0; + _BitScanForward64( &r, val ); + return (int)(r>>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_ctzll(val) >> 3); +# else + static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; + return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; +# endif +# endif +} + +#else + +FORCE_INLINE int LZ4_NbCommonBytes (register U32 val) +{ +# if defined(LZ4_BIG_ENDIAN) +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r = 0; + _BitScanReverse( &r, val ); + return (int)(r>>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_clz(val) >> 3); +# else + int r; + if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } + r += (!val); + return r; +# endif +# else +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r; + _BitScanForward( &r, val ); + return (int)(r>>3); +# elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_ctz(val) >> 3); +# else + static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; + return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; +# endif +# endif +} + +#endif + + +//**************************** +// Compression functions +//**************************** +FORCE_INLINE int LZ4_hashSequence(U32 sequence, tableType_t tableType) +{ + if (tableType == byU16) + return (((sequence) * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1))); + else + return (((sequence) * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG)); +} + +FORCE_INLINE int LZ4_hashPosition(Ptr p, tableType_t tableType) { return LZ4_hashSequence(A32(p), tableType); } + +FORCE_INLINE void LZ4_putPositionOnHash(Ptr p, U32 h, void* tableBase, tableType_t tableType, Ptr srcBase) +{ + switch (tableType) + { + case byPtr: { Ptr* hashTable = (Ptr*) tableBase; hashTable[h] = p; break; } + case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = p-srcBase; break; } + case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); break; } + } +} + +FORCE_INLINE void LZ4_putPosition(Ptr p, void* tableBase, tableType_t tableType, Ptr srcBase) +{ + U32 h = LZ4_hashPosition(p, tableType); + LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase); +} + +FORCE_INLINE Ptr LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, Ptr srcBase) +{ + if (tableType == byPtr) { Ptr* hashTable = (Ptr*) tableBase; return hashTable[h]; } + if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; } + { U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } // default, to ensure a return +} + +FORCE_INLINE Ptr LZ4_getPosition(Ptr p, void* tableBase, tableType_t tableType, Ptr srcBase) +{ + U32 h = LZ4_hashPosition(p, tableType); + return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); +} + + +FORCE_INLINE int LZ4_compress_generic( + void* ctx, + const char* source, + char* dest, + int inputSize, + int maxOutputSize, + + limitedOutput_directive limitedOutput, + tableType_t tableType, + prefix64k_directive prefix) +{ + Ptr ip = (Ptr) source; + Ptr const base = (prefix==withPrefix) ? ((LZ4_Data_Structure*)ctx)->base : (Ptr) source; + Ptr const lowLimit = ((prefix==withPrefix) ? ((LZ4_Data_Structure*)ctx)->bufferStart : (Ptr)source); + Ptr anchor = (Ptr) source; + Ptr const iend = ip + inputSize; + Ptr const mflimit = iend - MFLIMIT; + Ptr const matchlimit = iend - LASTLITERALS; + + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + maxOutputSize; + + int length; + const int skipStrength = SKIPSTRENGTH; + U32 forwardH; + + // Init conditions + if ((prefix==withPrefix) && (ip != ((LZ4_Data_Structure*)ctx)->nextBlock)) return 0; // must continue from end of previous block + if (prefix==withPrefix) ((LZ4_Data_Structure*)ctx)->nextBlock=iend; // do it now, due to potential early exit + if ((tableType == byU16) && (inputSize>=LZ4_64KLIMIT)) return 0; // Size too large (not within 64K limit) + if (inputSize> skipStrength; + ip = forwardIp; + forwardIp = ip + step; + + if unlikely(forwardIp > mflimit) { goto _last_literals; } + + forwardH = LZ4_hashPosition(forwardIp, tableType); + ref = LZ4_getPositionOnHash(h, ctx, tableType, base); + LZ4_putPositionOnHash(ip, h, ctx, tableType, base); + + } while ((ref + MAX_DISTANCE < ip) || (A32(ref) != A32(ip))); + + // Catch up + while ((ip>anchor) && (ref > lowLimit) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; } + + // Encode Literal length + length = (int)(ip - anchor); + token = op++; + if ((limitedOutput) && unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend)) return 0; // Check output limit + if (length>=(int)RUN_MASK) + { + int len = length-RUN_MASK; + *token=(RUN_MASK<= 255 ; len-=255) *op++ = 255; + *op++ = (BYTE)len; + } + else *token = (BYTE)(length<>8) > oend)) return 0; // Check output limit + if (length>=(int)ML_MASK) + { + *token += ML_MASK; + length -= ML_MASK; + for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; } + if (length >= 255) { length-=255; *op++ = 255; } + *op++ = (BYTE)length; + } + else *token += (BYTE)(length); + + // Test end of chunk + if (ip > mflimit) { anchor = ip; break; } + + // Fill table + LZ4_putPosition(ip-2, ctx, tableType, base); + + // Test next position + ref = LZ4_getPosition(ip, ctx, tableType, base); + LZ4_putPosition(ip, ctx, tableType, base); + if ((ref + MAX_DISTANCE >= ip) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; } + + // Prepare next loop + anchor = ip++; + forwardH = LZ4_hashPosition(ip, tableType); + } + +_last_literals: + // Encode Last Literals + { + int lastRun = (int)(iend - anchor); + if ((limitedOutput) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) return 0; // Check output limit + if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<= 255 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } + else *op++ = (BYTE)(lastRun<hashTable, 0, sizeof(lz4ds->hashTable)); + lz4ds->bufferStart = base; + lz4ds->base = base; + lz4ds->nextBlock = base; +} + + +void* LZ4_create (const char* inputBuffer) +{ + void* lz4ds = ALLOCATOR(1, sizeof(LZ4_Data_Structure)); + LZ4_init ((LZ4_Data_Structure*)lz4ds, (Ptr)inputBuffer); + return lz4ds; +} + + +int LZ4_free (void* LZ4_Data) +{ + FREEMEM(LZ4_Data); + return (0); +} + + +char* LZ4_slideInputBuffer (void* LZ4_Data) +{ + LZ4_Data_Structure* lz4ds = (LZ4_Data_Structure*)LZ4_Data; + size_t delta = lz4ds->nextBlock - (lz4ds->bufferStart + 64 KB); + + if(lz4ds->base - delta > lz4ds->base) // underflow control + { + size_t newBaseDelta = (lz4ds->nextBlock - 64 KB) - lz4ds->base; + int nH; + + for (nH=0; nH < HASHNBCELLS4; nH++) + { + if (lz4ds->hashTable[nH] < (U32)newBaseDelta) lz4ds->hashTable[nH] = 0; + else lz4ds->hashTable[nH] -= newBaseDelta; + } + lz4ds->base += newBaseDelta; + } + memcpy((void*)(lz4ds->bufferStart), (const void*)(lz4ds->nextBlock - 64 KB), 64 KB); + lz4ds->nextBlock -= delta; + lz4ds->base -= delta; + + return (char*)(lz4ds->nextBlock); +} + + +//**************************** +// Decompression functions +//**************************** + +// This generic decompression function cover all use cases. +// It shall be instanciated several times, using different sets of directives +// Note that it is essential this generic function is really inlined, +// in order to remove useless branches during compilation optimisation. +FORCE_INLINE int LZ4_decompress_generic( + const char* source, + char* dest, + int inputSize, // + int outputSize, // OutputSize must be != 0; if endOnInput==endOnInputSize, this value is the max size of Output Buffer. + + int endOnInput, // endOnOutputSize, endOnInputSize + int prefix64k, // noPrefix, withPrefix + int partialDecoding, // full, partial + int targetOutputSize // only used if partialDecoding==partial + ) +{ + // Local Variables + Ptr restrict ip = (Ptr) source; + Ptr ref; + Ptr const iend = ip + inputSize; + + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + outputSize; + BYTE* cpy; + BYTE* oexit = op + targetOutputSize; + + size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0}; +#if LZ4_ARCH64 + size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3}; +#endif + + + // Special cases + if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT; // targetOutputSize too high => decode everything + if ((endOnInput) && unlikely(outputSize==0)) return ((inputSize==1) && (*ip==0)) ? 0 : -1; // Empty output buffer + if ((!endOnInput) && unlikely(outputSize==0)) return (*ip==0?1:-1); + + + // Main Loop + while (1) + { + unsigned token; + size_t length; + + // get runlength + token = *ip++; + if ((length=(token>>ML_BITS)) == RUN_MASK) + { + unsigned s=255; + while (((endOnInput)?ip(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) ) + || ((!endOnInput) && (cpy>oend-COPYLENGTH))) + { + if (partialDecoding) + { + if (cpy > oend) goto _output_error; // Error : write attempt beyond end of output buffer + if ((endOnInput) && (ip+length > iend)) goto _output_error; // Error : read attempt beyond end of input buffer + } + else + { + if ((!endOnInput) && (cpy != oend)) goto _output_error; // Error : block decoding must stop exactly there + if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; // Error : input must be consumed + } + memcpy(op, ip, length); + ip += length; + op += length; + break; // Necessarily EOF, due to parsing restrictions + } + LZ4_WILDCOPY(op, ip, cpy); ip -= (op-cpy); op = cpy; + + // get offset + LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2; + if ((prefix64k==noPrefix) && unlikely(ref < (BYTE* const)dest)) goto _output_error; // Error : offset outside destination buffer + + // get matchlength + if ((length=(token&ML_MASK)) == ML_MASK) + { + while ((!endOnInput) || (ipoend-COPYLENGTH-(STEPSIZE-4)) + { + if (cpy > oend-LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals + LZ4_SECURECOPY(op, ref, (oend-COPYLENGTH)); + while(op The memory position where the next input data block must start is provided as the result of the function. + +Compression can then resume, using LZ4_compress_continue() or LZ4_compress_limitedOutput_continue(), as usual. + +When compression is completed, a call to LZ4_free() will release the memory used by the LZ4 Data Structure. +*/ + + +//**************************** +// Obsolete Functions +//**************************** + +static inline int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); } +static inline int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); } + +/* +These functions are deprecated and should no longer be used. +They are provided here for compatibility with existing user programs. +*/ + + + +#if defined (__cplusplus) +} +#endif diff --git a/FileSearch/stxutif.h b/FileSearch/stxutif.h new file mode 100644 index 0000000..4554f40 --- /dev/null +++ b/FileSearch/stxutif.h @@ -0,0 +1,58 @@ +#ifndef _UTIF_H_ +#define _UTIF_H_ 1 + +#include +#include +#include +#include + +#ifdef __MINGW32_VERSION +#ifndef _GLIBCXX_USE_WCHAR_T + +namespace std +{ + /// declare a coecvt between wchar_t and char + /*! + * If Mingw support for wchar_t is not activated, the following declarations are + * required to properly let the code to be translatable + */ + template<> + class codecvt: + public __codecvt_abstract_base + { + protected: + explicit codecvt(size_t refs=0) + :__codecvt_abstract_base(refs) + {} + public: + static locale::id id; + }; + + // wide types for char dependent templates + typedef basic_ios wios; + typedef basic_streambuf wstreambuf; + typedef basic_istream wistream; + typedef basic_ostream wostream; + typedef basic_iostream wiostream; + typedef basic_stringbuf wstringbuf; + typedef basic_istringstream wistringstream; + typedef basic_ostringstream wostringstream; + typedef basic_stringstream wstringstream; + typedef basic_filebuf wfilebuf; + typedef basic_ifstream wifstream; + typedef basic_ofstream wofstream; + typedef basic_fstream wfstream; +} + +#endif //_GLIBCXX_USE_WCHAR_T +#endif //__MINGW32_VERSION + +namespace gel +{namespace stdx +{ + extern std::locale utf8_locale; ///< global locale with UTF-8 conversion capabilities. + + std::string wstring_to_utf8 (const std::wstring& str); +}} + +#endif diff --git a/FileSearch/tinyxml2.cpp b/FileSearch/tinyxml2.cpp new file mode 100644 index 0000000..7ec259e --- /dev/null +++ b/FileSearch/tinyxml2.cpp @@ -0,0 +1,2101 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#include "tinyxml2.h" + +#include // yes, this one new style header, is in the Android SDK. +# ifdef ANDROID_NDK +# include +#else +# include +#endif + +static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF +static const char LF = LINE_FEED; +static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out +static const char CR = CARRIAGE_RETURN; +static const char SINGLE_QUOTE = '\''; +static const char DOUBLE_QUOTE = '\"'; + +// Bunch of unicode info at: +// http://www.unicode.org/faq/utf_bom.html +// ef bb bf (Microsoft "lead bytes") - designates UTF-8 + +static const unsigned char TIXML_UTF_LEAD_0 = 0xefU; +static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; +static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; + + +#define DELETE_NODE( node ) { \ + if ( node ) { \ + MemPool* pool = node->_memPool; \ + node->~XMLNode(); \ + pool->Free( node ); \ + } \ + } +#define DELETE_ATTRIBUTE( attrib ) { \ + if ( attrib ) { \ + MemPool* pool = attrib->_memPool; \ + attrib->~XMLAttribute(); \ + pool->Free( attrib ); \ + } \ + } + +namespace tinyxml2 +{ + +struct Entity { + const char* pattern; + int length; + char value; +}; + +static const int NUM_ENTITIES = 5; +static const Entity entities[NUM_ENTITIES] = { + { "quot", 4, DOUBLE_QUOTE }, + { "amp", 3, '&' }, + { "apos", 4, SINGLE_QUOTE }, + { "lt", 2, '<' }, + { "gt", 2, '>' } +}; + + +StrPair::~StrPair() +{ + Reset(); +} + + +void StrPair::Reset() +{ + if ( _flags & NEEDS_DELETE ) { + delete [] _start; + } + _flags = 0; + _start = 0; + _end = 0; +} + + +void StrPair::SetStr( const char* str, int flags ) +{ + Reset(); + size_t len = strlen( str ); + _start = new char[ len+1 ]; + memcpy( _start, str, len+1 ); + _end = _start + len; + _flags = flags | NEEDS_DELETE; +} + + +char* StrPair::ParseText( char* p, const char* endTag, int strFlags ) +{ + TIXMLASSERT( endTag && *endTag ); + + char* start = p; // fixme: hides a member + char endChar = *endTag; + size_t length = strlen( endTag ); + + // Inner loop of text parsing. + while ( *p ) { + if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) { + Set( start, p, strFlags ); + return p + length; + } + ++p; + } + return 0; +} + + +char* StrPair::ParseName( char* p ) +{ + char* start = p; + + if ( !start || !(*start) ) { + return 0; + } + + while( *p && ( p == start ? XMLUtil::IsNameStartChar( *p ) : XMLUtil::IsNameChar( *p ) )) { + ++p; + } + + if ( p > start ) { + Set( start, p, 0 ); + return p; + } + return 0; +} + + +void StrPair::CollapseWhitespace() +{ + // Trim leading space. + _start = XMLUtil::SkipWhiteSpace( _start ); + + if ( _start && *_start ) { + char* p = _start; // the read pointer + char* q = _start; // the write pointer + + while( *p ) { + if ( XMLUtil::IsWhiteSpace( *p )) { + p = XMLUtil::SkipWhiteSpace( p ); + if ( *p == 0 ) { + break; // don't write to q; this trims the trailing space. + } + *q = ' '; + ++q; + } + *q = *p; + ++q; + ++p; + } + *q = 0; + } +} + + +const char* StrPair::GetStr() +{ + if ( _flags & NEEDS_FLUSH ) { + *_end = 0; + _flags ^= NEEDS_FLUSH; + + if ( _flags ) { + char* p = _start; // the read pointer + char* q = _start; // the write pointer + + while( p < _end ) { + if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) { + // CR-LF pair becomes LF + // CR alone becomes LF + // LF-CR becomes LF + if ( *(p+1) == LF ) { + p += 2; + } + else { + ++p; + } + *q++ = LF; + } + else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) { + if ( *(p+1) == CR ) { + p += 2; + } + else { + ++p; + } + *q++ = LF; + } + else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) { + // Entities handled by tinyXML2: + // - special entities in the entity table [in/out] + // - numeric character reference [in] + // 中 or 中 + + if ( *(p+1) == '#' ) { + char buf[10] = { 0 }; + int len; + p = const_cast( XMLUtil::GetCharacterRef( p, buf, &len ) ); + for( int i=0; i(p); + // Check for BOM: + if ( *(pu+0) == TIXML_UTF_LEAD_0 + && *(pu+1) == TIXML_UTF_LEAD_1 + && *(pu+2) == TIXML_UTF_LEAD_2 ) { + *bom = true; + p += 3; + } + return p; +} + + +void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ) +{ + const unsigned long BYTE_MASK = 0xBF; + const unsigned long BYTE_MARK = 0x80; + const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + + if (input < 0x80) { + *length = 1; + } + else if ( input < 0x800 ) { + *length = 2; + } + else if ( input < 0x10000 ) { + *length = 3; + } + else if ( input < 0x200000 ) { + *length = 4; + } + else { + *length = 0; // This code won't covert this correctly anyway. + return; + } + + output += *length; + + // Scary scary fall throughs. + switch (*length) { + case 4: + --output; + *output = (char)((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + case 3: + --output; + *output = (char)((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + case 2: + --output; + *output = (char)((input | BYTE_MARK) & BYTE_MASK); + input >>= 6; + case 1: + --output; + *output = (char)(input | FIRST_BYTE_MARK[*length]); + default: + break; + } +} + + +const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length ) +{ + // Presume an entity, and pull it out. + *length = 0; + + if ( *(p+1) == '#' && *(p+2) ) { + unsigned long ucs = 0; + ptrdiff_t delta = 0; + unsigned mult = 1; + + if ( *(p+2) == 'x' ) { + // Hexadecimal. + if ( !*(p+3) ) { + return 0; + } + + const char* q = p+3; + q = strchr( q, ';' ); + + if ( !q || !*q ) { + return 0; + } + + delta = q-p; + --q; + + while ( *q != 'x' ) { + if ( *q >= '0' && *q <= '9' ) { + ucs += mult * (*q - '0'); + } + else if ( *q >= 'a' && *q <= 'f' ) { + ucs += mult * (*q - 'a' + 10); + } + else if ( *q >= 'A' && *q <= 'F' ) { + ucs += mult * (*q - 'A' + 10 ); + } + else { + return 0; + } + mult *= 16; + --q; + } + } + else { + // Decimal. + if ( !*(p+2) ) { + return 0; + } + + const char* q = p+2; + q = strchr( q, ';' ); + + if ( !q || !*q ) { + return 0; + } + + delta = q-p; + --q; + + while ( *q != '#' ) { + if ( *q >= '0' && *q <= '9' ) { + ucs += mult * (*q - '0'); + } + else { + return 0; + } + mult *= 10; + --q; + } + } + // convert the UCS to UTF-8 + ConvertUTF32ToUTF8( ucs, value, length ); + return p + delta + 1; + } + return p+1; +} + + +void XMLUtil::ToStr( int v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%d", v ); +} + + +void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%u", v ); +} + + +void XMLUtil::ToStr( bool v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 ); +} + + +void XMLUtil::ToStr( float v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%f", v ); +} + + +void XMLUtil::ToStr( double v, char* buffer, int bufferSize ) +{ + TIXML_SNPRINTF( buffer, bufferSize, "%f", v ); +} + + +bool XMLUtil::ToInt( const char* str, int* value ) +{ + if ( TIXML_SSCANF( str, "%d", value ) == 1 ) { + return true; + } + return false; +} + +bool XMLUtil::ToUnsigned( const char* str, unsigned *value ) +{ + if ( TIXML_SSCANF( str, "%u", value ) == 1 ) { + return true; + } + return false; +} + +bool XMLUtil::ToBool( const char* str, bool* value ) +{ + int ival = 0; + if ( ToInt( str, &ival )) { + *value = (ival==0) ? false : true; + return true; + } + if ( StringEqual( str, "true" ) ) { + *value = true; + return true; + } + else if ( StringEqual( str, "false" ) ) { + *value = false; + return true; + } + return false; +} + + +bool XMLUtil::ToFloat( const char* str, float* value ) +{ + if ( TIXML_SSCANF( str, "%f", value ) == 1 ) { + return true; + } + return false; +} + +bool XMLUtil::ToDouble( const char* str, double* value ) +{ + if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) { + return true; + } + return false; +} + + +char* XMLDocument::Identify( char* p, XMLNode** node ) +{ + XMLNode* returnNode = 0; + char* start = p; + p = XMLUtil::SkipWhiteSpace( p ); + if( !p || !*p ) { + return p; + } + + // What is this thing? + // - Elements start with a letter or underscore, but xml is reserved. + // - Comments: + // + // With a special case: + // + // + // + // + // Where the closing element (/foo) *must* be the next thing after the opening + // element, and the names must match. BUT the tricky bit is that the closing + // element will be read by the child. + // + // 'endTag' is the end tag for this node, it is returned by a call to a child. + // 'parentEnd' is the end tag for the parent, which is filled in and returned. + + while( p && *p ) { + XMLNode* node = 0; + + p = _document->Identify( p, &node ); + if ( p == 0 || node == 0 ) { + break; + } + + StrPair endTag; + p = node->ParseDeep( p, &endTag ); + if ( !p ) { + DELETE_NODE( node ); + node = 0; + if ( !_document->Error() ) { + _document->SetError( XML_ERROR_PARSING, 0, 0 ); + } + break; + } + + // We read the end tag. Return it to the parent. + if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) { + if ( parentEnd ) { + *parentEnd = static_cast(node)->_value; + } + node->_memPool->SetTracked(); // created and then immediately deleted. + DELETE_NODE( node ); + return p; + } + + // Handle an end tag returned to this level. + // And handle a bunch of annoying errors. + XMLElement* ele = node->ToElement(); + if ( ele ) { + if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) { + _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); + p = 0; + } + else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) { + _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); + p = 0; + } + else if ( !endTag.Empty() ) { + if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) { + _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); + p = 0; + } + } + } + if ( p == 0 ) { + DELETE_NODE( node ); + node = 0; + } + if ( node ) { + this->InsertEndChild( node ); + } + } + return 0; +} + +// --------- XMLText ---------- // +char* XMLText::ParseDeep( char* p, StrPair* ) +{ + const char* start = p; + if ( this->CData() ) { + p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION ); + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 ); + } + return p; + } + else { + int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES; + if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) { + flags |= StrPair::COLLAPSE_WHITESPACE; + } + + p = _value.ParseText( p, "<", flags ); + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 ); + } + if ( p && *p ) { + return p-1; + } + } + return 0; +} + + +XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern? + text->SetCData( this->CData() ); + return text; +} + + +bool XMLText::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() )); +} + + +bool XMLText::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + + +// --------- XMLComment ---------- // + +XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLComment::~XMLComment() +{ +} + + +char* XMLComment::ParseDeep( char* p, StrPair* ) +{ + // Comment parses as text. + const char* start = p; + p = _value.ParseText( p, "-->", StrPair::COMMENT ); + if ( p == 0 ) { + _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 ); + } + return p; +} + + +XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern? + return comment; +} + + +bool XMLComment::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() )); +} + + +bool XMLComment::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + + +// --------- XMLDeclaration ---------- // + +XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLDeclaration::~XMLDeclaration() +{ + //printf( "~XMLDeclaration\n" ); +} + + +char* XMLDeclaration::ParseDeep( char* p, StrPair* ) +{ + // Declaration parses as text. + const char* start = p; + p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION ); + if ( p == 0 ) { + _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 ); + } + return p; +} + + +XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern? + return dec; +} + + +bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() )); +} + + + +bool XMLDeclaration::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + +// --------- XMLUnknown ---------- // + +XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc ) +{ +} + + +XMLUnknown::~XMLUnknown() +{ +} + + +char* XMLUnknown::ParseDeep( char* p, StrPair* ) +{ + // Unknown parses as text. + const char* start = p; + + p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION ); + if ( !p ) { + _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 ); + } + return p; +} + + +XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern? + return text; +} + + +bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const +{ + return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() )); +} + + +bool XMLUnknown::Accept( XMLVisitor* visitor ) const +{ + return visitor->Visit( *this ); +} + +// --------- XMLAttribute ---------- // +char* XMLAttribute::ParseDeep( char* p, bool processEntities ) +{ + // Parse using the name rules: bug fix, was using ParseText before + p = _name.ParseName( p ); + if ( !p || !*p ) { + return 0; + } + + // Skip white space before = + p = XMLUtil::SkipWhiteSpace( p ); + if ( !p || *p != '=' ) { + return 0; + } + + ++p; // move up to opening quote + p = XMLUtil::SkipWhiteSpace( p ); + if ( *p != '\"' && *p != '\'' ) { + return 0; + } + + char endTag[2] = { *p, 0 }; + ++p; // move past opening quote + + p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES ); + return p; +} + + +void XMLAttribute::SetName( const char* n ) +{ + _name.SetStr( n ); +} + + +XMLError XMLAttribute::QueryIntValue( int* value ) const +{ + if ( XMLUtil::ToInt( Value(), value )) { + return XML_NO_ERROR; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const +{ + if ( XMLUtil::ToUnsigned( Value(), value )) { + return XML_NO_ERROR; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryBoolValue( bool* value ) const +{ + if ( XMLUtil::ToBool( Value(), value )) { + return XML_NO_ERROR; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryFloatValue( float* value ) const +{ + if ( XMLUtil::ToFloat( Value(), value )) { + return XML_NO_ERROR; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +XMLError XMLAttribute::QueryDoubleValue( double* value ) const +{ + if ( XMLUtil::ToDouble( Value(), value )) { + return XML_NO_ERROR; + } + return XML_WRONG_ATTRIBUTE_TYPE; +} + + +void XMLAttribute::SetAttribute( const char* v ) +{ + _value.SetStr( v ); +} + + +void XMLAttribute::SetAttribute( int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute( unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +void XMLAttribute::SetAttribute( bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + +void XMLAttribute::SetAttribute( float v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + _value.SetStr( buf ); +} + + +// --------- XMLElement ---------- // +XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), + _closingType( 0 ), + _rootAttribute( 0 ) +{ +} + + +XMLElement::~XMLElement() +{ + while( _rootAttribute ) { + XMLAttribute* next = _rootAttribute->_next; + DELETE_ATTRIBUTE( _rootAttribute ); + _rootAttribute = next; + } +} + + +XMLAttribute* XMLElement::FindAttribute( const char* name ) +{ + XMLAttribute* a = 0; + for( a=_rootAttribute; a; a = a->_next ) { + if ( XMLUtil::StringEqual( a->Name(), name ) ) { + return a; + } + } + return 0; +} + + +const XMLAttribute* XMLElement::FindAttribute( const char* name ) const +{ + XMLAttribute* a = 0; + for( a=_rootAttribute; a; a = a->_next ) { + if ( XMLUtil::StringEqual( a->Name(), name ) ) { + return a; + } + } + return 0; +} + + +const char* XMLElement::Attribute( const char* name, const char* value ) const +{ + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return 0; + } + if ( !value || XMLUtil::StringEqual( a->Value(), value )) { + return a->Value(); + } + return 0; +} + + +const char* XMLElement::GetText() const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + return FirstChild()->ToText()->Value(); + } + return 0; +} + + +XMLError XMLElement::QueryIntText( int* ival ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->ToText()->Value(); + if ( XMLUtil::ToInt( t, ival ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->ToText()->Value(); + if ( XMLUtil::ToUnsigned( t, uval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryBoolText( bool* bval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->ToText()->Value(); + if ( XMLUtil::ToBool( t, bval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryDoubleText( double* dval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->ToText()->Value(); + if ( XMLUtil::ToDouble( t, dval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + +XMLError XMLElement::QueryFloatText( float* fval ) const +{ + if ( FirstChild() && FirstChild()->ToText() ) { + const char* t = FirstChild()->ToText()->Value(); + if ( XMLUtil::ToFloat( t, fval ) ) { + return XML_SUCCESS; + } + return XML_CAN_NOT_CONVERT_TEXT; + } + return XML_NO_TEXT_NODE; +} + + + +XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) +{ + XMLAttribute* last = 0; + XMLAttribute* attrib = 0; + for( attrib = _rootAttribute; + attrib; + last = attrib, attrib = attrib->_next ) { + if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { + break; + } + } + if ( !attrib ) { + attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); + attrib->_memPool = &_document->_attributePool; + if ( last ) { + last->_next = attrib; + } + else { + _rootAttribute = attrib; + } + attrib->SetName( name ); + attrib->_memPool->SetTracked(); // always created and linked. + } + return attrib; +} + + +void XMLElement::DeleteAttribute( const char* name ) +{ + XMLAttribute* prev = 0; + for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) { + if ( XMLUtil::StringEqual( name, a->Name() ) ) { + if ( prev ) { + prev->_next = a->_next; + } + else { + _rootAttribute = a->_next; + } + DELETE_ATTRIBUTE( a ); + break; + } + prev = a; + } +} + + +char* XMLElement::ParseAttributes( char* p ) +{ + const char* start = p; + XMLAttribute* prevAttribute = 0; + + // Read the attributes. + while( p ) { + p = XMLUtil::SkipWhiteSpace( p ); + if ( !p || !(*p) ) { + _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() ); + return 0; + } + + // attribute. + if (XMLUtil::IsNameStartChar( *p ) ) { + XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); + attrib->_memPool = &_document->_attributePool; + attrib->_memPool->SetTracked(); + + p = attrib->ParseDeep( p, _document->ProcessEntities() ); + if ( !p || Attribute( attrib->Name() ) ) { + DELETE_ATTRIBUTE( attrib ); + _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p ); + return 0; + } + // There is a minor bug here: if the attribute in the source xml + // document is duplicated, it will not be detected and the + // attribute will be doubly added. However, tracking the 'prevAttribute' + // avoids re-scanning the attribute list. Preferring performance for + // now, may reconsider in the future. + if ( prevAttribute ) { + prevAttribute->_next = attrib; + } + else { + _rootAttribute = attrib; + } + prevAttribute = attrib; + } + // end of the tag + else if ( *p == '/' && *(p+1) == '>' ) { + _closingType = CLOSED; + return p+2; // done; sealed element. + } + // end of the tag + else if ( *p == '>' ) { + ++p; + break; + } + else { + _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p ); + return 0; + } + } + return p; +} + + +// +// +// foobar +// +char* XMLElement::ParseDeep( char* p, StrPair* strPair ) +{ + // Read the element name. + p = XMLUtil::SkipWhiteSpace( p ); + if ( !p ) { + return 0; + } + + // The closing element is the form. It is + // parsed just like a regular element then deleted from + // the DOM. + if ( *p == '/' ) { + _closingType = CLOSING; + ++p; + } + + p = _value.ParseName( p ); + if ( _value.Empty() ) { + return 0; + } + + p = ParseAttributes( p ); + if ( !p || !*p || _closingType ) { + return p; + } + + p = XMLNode::ParseDeep( p, strPair ); + return p; +} + + + +XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const +{ + if ( !doc ) { + doc = _document; + } + XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern? + for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) { + element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern? + } + return element; +} + + +bool XMLElement::ShallowEqual( const XMLNode* compare ) const +{ + const XMLElement* other = compare->ToElement(); + if ( other && XMLUtil::StringEqual( other->Value(), Value() )) { + + const XMLAttribute* a=FirstAttribute(); + const XMLAttribute* b=other->FirstAttribute(); + + while ( a && b ) { + if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) { + return false; + } + a = a->Next(); + b = b->Next(); + } + if ( a || b ) { + // different count + return false; + } + return true; + } + return false; +} + + +bool XMLElement::Accept( XMLVisitor* visitor ) const +{ + if ( visitor->VisitEnter( *this, _rootAttribute ) ) { + for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { + if ( !node->Accept( visitor ) ) { + break; + } + } + } + return visitor->VisitExit( *this ); +} + + +// --------- XMLDocument ----------- // +XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) : + XMLNode( 0 ), + _writeBOM( false ), + _processEntities( processEntities ), + _errorID( XML_NO_ERROR ), + _whitespace( whitespace ), + _errorStr1( 0 ), + _errorStr2( 0 ), + _charBuffer( 0 ) +{ + _document = this; // avoid warning about 'this' in initializer list +} + + +XMLDocument::~XMLDocument() +{ + DeleteChildren(); + delete [] _charBuffer; + +#if 0 + _textPool.Trace( "text" ); + _elementPool.Trace( "element" ); + _commentPool.Trace( "comment" ); + _attributePool.Trace( "attribute" ); +#endif + +#ifdef DEBUG + if ( Error() == false ) { + TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() ); + TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() ); + TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() ); + TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() ); + } +#endif +} + + +void XMLDocument::Clear() +{ + DeleteChildren(); + + _errorID = XML_NO_ERROR; + _errorStr1 = 0; + _errorStr2 = 0; + + delete [] _charBuffer; + _charBuffer = 0; +} + + +XMLElement* XMLDocument::NewElement( const char* name ) +{ + XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this ); + ele->_memPool = &_elementPool; + ele->SetName( name ); + return ele; +} + + +XMLComment* XMLDocument::NewComment( const char* str ) +{ + XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this ); + comment->_memPool = &_commentPool; + comment->SetValue( str ); + return comment; +} + + +XMLText* XMLDocument::NewText( const char* str ) +{ + XMLText* text = new (_textPool.Alloc()) XMLText( this ); + text->_memPool = &_textPool; + text->SetValue( str ); + return text; +} + + +XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) +{ + XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this ); + dec->_memPool = &_commentPool; + dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" ); + return dec; +} + + +XMLUnknown* XMLDocument::NewUnknown( const char* str ) +{ + XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this ); + unk->_memPool = &_commentPool; + unk->SetValue( str ); + return unk; +} + + +XMLError XMLDocument::LoadFile( const char* filename ) +{ + Clear(); + FILE* fp = 0; + +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) + errno_t err = fopen_s(&fp, filename, "rb" ); + if ( !fp || err) { +#else + fp = fopen( filename, "rb" ); + if ( !fp) { +#endif + SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 ); + return _errorID; + } + LoadFile( fp ); + fclose( fp ); + return _errorID; +} + + +XMLError XMLDocument::LoadFile( FILE* fp ) +{ + Clear(); + + fseek( fp, 0, SEEK_END ); + size_t size = ftell( fp ); + fseek( fp, 0, SEEK_SET ); + + if ( size == 0 ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + + _charBuffer = new char[size+1]; + size_t read = fread( _charBuffer, 1, size, fp ); + if ( read != size ) { + SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); + return _errorID; + } + + _charBuffer[size] = 0; + + const char* p = _charBuffer; + p = XMLUtil::SkipWhiteSpace( p ); + p = XMLUtil::ReadBOM( p, &_writeBOM ); + if ( !p || !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + + ParseDeep( _charBuffer + (p-_charBuffer), 0 ); + return _errorID; +} + + +XMLError XMLDocument::SaveFile( const char* filename, bool compact ) +{ + FILE* fp = 0; +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) + errno_t err = fopen_s(&fp, filename, "w" ); + if ( !fp || err) { +#else + fp = fopen( filename, "w" ); + if ( !fp) { +#endif + SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 ); + return _errorID; + } + SaveFile(fp, compact); + fclose( fp ); + return _errorID; +} + + +XMLError XMLDocument::SaveFile( FILE* fp, bool compact ) +{ + XMLPrinter stream( fp, compact ); + Print( &stream ); + return _errorID; +} + + +XMLError XMLDocument::Parse( const char* p, size_t len ) +{ + const char* start = p; + Clear(); + + if ( !p || !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + if ( len == (size_t)(-1) ) { + len = strlen( p ); + } + _charBuffer = new char[ len+1 ]; + memcpy( _charBuffer, p, len ); + _charBuffer[len] = 0; + + p = XMLUtil::SkipWhiteSpace( p ); + p = XMLUtil::ReadBOM( p, &_writeBOM ); + if ( !p || !*p ) { + SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); + return _errorID; + } + + ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc. + ParseDeep( _charBuffer+delta, 0 ); + return _errorID; +} + + +void XMLDocument::Print( XMLPrinter* streamer ) const +{ + XMLPrinter stdStreamer( stdout ); + if ( !streamer ) { + streamer = &stdStreamer; + } + Accept( streamer ); +} + + +void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 ) +{ + _errorID = error; + _errorStr1 = str1; + _errorStr2 = str2; +} + + +void XMLDocument::PrintError() const +{ + if ( _errorID ) { + static const int LEN = 20; + char buf1[LEN] = { 0 }; + char buf2[LEN] = { 0 }; + + if ( _errorStr1 ) { + TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 ); + } + if ( _errorStr2 ) { + TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 ); + } + + printf( "XMLDocument error id=%d str1=%s str2=%s\n", + _errorID, buf1, buf2 ); + } +} + + +XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) : + _elementJustOpened( false ), + _firstElement( true ), + _fp( file ), + _depth( depth ), + _textDepth( -1 ), + _processEntities( true ), + _compactMode( compact ) +{ + for( int i=0; i'] = true; // not required, but consistency is nice + _buffer.Push( 0 ); +} + + +void XMLPrinter::Print( const char* format, ... ) +{ + va_list va; + va_start( va, format ); + + if ( _fp ) { + vfprintf( _fp, format, va ); + } + else { + // This seems brutally complex. Haven't figured out a better + // way on windows. +#ifdef _MSC_VER + int len = -1; + int expand = 1000; + while ( len < 0 ) { + len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va ); + if ( len < 0 ) { + expand *= 3/2; + _accumulator.PushArr( expand ); + } + } + char* p = _buffer.PushArr( len ) - 1; + memcpy( p, _accumulator.Mem(), len+1 ); +#else + int len = vsnprintf( 0, 0, format, va ); + // Close out and re-start the va-args + va_end( va ); + va_start( va, format ); + char* p = _buffer.PushArr( len ) - 1; + vsnprintf( p, len+1, format, va ); +#endif + } + va_end( va ); +} + + +void XMLPrinter::PrintSpace( int depth ) +{ + for( int i=0; i 0 && *q < ENTITY_RANGE ) { + // Check for entities. If one is found, flush + // the stream up until the entity, write the + // entity, and keep looking. + if ( flag[(unsigned)(*q)] ) { + while ( p < q ) { + Print( "%c", *p ); + ++p; + } + for( int i=0; i 0) ) { + Print( "%s", p ); + } +} + + +void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) +{ + if ( writeBOM ) { + static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 }; + Print( "%s", bom ); + } + if ( writeDec ) { + PushDeclaration( "xml version=\"1.0\"" ); + } +} + + +void XMLPrinter::OpenElement( const char* name ) +{ + if ( _elementJustOpened ) { + SealElement(); + } + _stack.Push( name ); + + if ( _textDepth < 0 && !_firstElement && !_compactMode ) { + Print( "\n" ); + } + if ( !_compactMode ) { + PrintSpace( _depth ); + } + + Print( "<%s", name ); + _elementJustOpened = true; + _firstElement = false; + ++_depth; +} + + +void XMLPrinter::PushAttribute( const char* name, const char* value ) +{ + TIXMLASSERT( _elementJustOpened ); + Print( " %s=\"", name ); + PrintString( value, false ); + Print( "\"" ); +} + + +void XMLPrinter::PushAttribute( const char* name, int v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, unsigned v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, bool v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::PushAttribute( const char* name, double v ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( v, buf, BUF_SIZE ); + PushAttribute( name, buf ); +} + + +void XMLPrinter::CloseElement() +{ + --_depth; + const char* name = _stack.Pop(); + + if ( _elementJustOpened ) { + Print( "/>" ); + } + else { + if ( _textDepth < 0 && !_compactMode) { + Print( "\n" ); + PrintSpace( _depth ); + } + Print( "", name ); + } + + if ( _textDepth == _depth ) { + _textDepth = -1; + } + if ( _depth == 0 && !_compactMode) { + Print( "\n" ); + } + _elementJustOpened = false; +} + + +void XMLPrinter::SealElement() +{ + _elementJustOpened = false; + Print( ">" ); +} + + +void XMLPrinter::PushText( const char* text, bool cdata ) +{ + _textDepth = _depth-1; + + if ( _elementJustOpened ) { + SealElement(); + } + if ( cdata ) { + Print( "" ); + } + else { + PrintString( text, true ); + } +} + +void XMLPrinter::PushText( int value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( unsigned value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( bool value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( float value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushText( double value ) +{ + char buf[BUF_SIZE]; + XMLUtil::ToStr( value, buf, BUF_SIZE ); + PushText( buf, false ); +} + + +void XMLPrinter::PushComment( const char* comment ) +{ + if ( _elementJustOpened ) { + SealElement(); + } + if ( _textDepth < 0 && !_firstElement && !_compactMode) { + Print( "\n" ); + PrintSpace( _depth ); + } + _firstElement = false; + Print( "", comment ); +} + + +void XMLPrinter::PushDeclaration( const char* value ) +{ + if ( _elementJustOpened ) { + SealElement(); + } + if ( _textDepth < 0 && !_firstElement && !_compactMode) { + Print( "\n" ); + PrintSpace( _depth ); + } + _firstElement = false; + Print( "", value ); +} + + +void XMLPrinter::PushUnknown( const char* value ) +{ + if ( _elementJustOpened ) { + SealElement(); + } + if ( _textDepth < 0 && !_firstElement && !_compactMode) { + Print( "\n" ); + PrintSpace( _depth ); + } + _firstElement = false; + Print( "", value ); +} + + +bool XMLPrinter::VisitEnter( const XMLDocument& doc ) +{ + _processEntities = doc.ProcessEntities(); + if ( doc.HasBOM() ) { + PushHeader( true, false ); + } + return true; +} + + +bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) +{ + OpenElement( element.Name() ); + while ( attribute ) { + PushAttribute( attribute->Name(), attribute->Value() ); + attribute = attribute->Next(); + } + return true; +} + + +bool XMLPrinter::VisitExit( const XMLElement& ) +{ + CloseElement(); + return true; +} + + +bool XMLPrinter::Visit( const XMLText& text ) +{ + PushText( text.Value(), text.CData() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLComment& comment ) +{ + PushComment( comment.Value() ); + return true; +} + +bool XMLPrinter::Visit( const XMLDeclaration& declaration ) +{ + PushDeclaration( declaration.Value() ); + return true; +} + + +bool XMLPrinter::Visit( const XMLUnknown& unknown ) +{ + PushUnknown( unknown.Value() ); + return true; +} + +} // namespace tinyxml2 + diff --git a/FileSearch/tinyxml2.h b/FileSearch/tinyxml2.h new file mode 100644 index 0000000..c9b9078 --- /dev/null +++ b/FileSearch/tinyxml2.h @@ -0,0 +1,1987 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#ifndef TINYXML2_INCLUDED +#define TINYXML2_INCLUDED + +#if defined(ANDROID_NDK) || defined(__BORLANDC__) +# include +# include +# include +# include +# include +# include +#else +# include +# include +# include +# include +# include +# include +#endif + +/* + TODO: intern strings instead of allocation. +*/ +/* + gcc: + g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe + + Formatting, Artistic Style: + AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h +*/ + +#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__) +# ifndef DEBUG +# define DEBUG +# endif +#endif + +#ifdef _MSC_VER +# pragma warning(push) +# pragma warning(disable: 4251) +#endif + +#ifdef _WIN32 +# ifdef TINYXML2_EXPORT +# define TINYXML2_LIB __declspec(dllexport) +# elif defined(TINYXML2_IMPORT) +# define TINYXML2_LIB __declspec(dllimport) +# else +# define TINYXML2_LIB +# endif +#else +# define TINYXML2_LIB +#endif + + +#if defined(DEBUG) +# if defined(_MSC_VER) +# define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak() +# elif defined (ANDROID_NDK) +# include +# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); } +# else +# include +# define TIXMLASSERT assert +# endif +# else +# define TIXMLASSERT( x ) {} +#endif + + +#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) +// Microsoft visual studio, version 2005 and higher. +/*int _snprintf_s( + char *buffer, + size_t sizeOfBuffer, + size_t count, + const char *format [, + argument] ... +);*/ +inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) +{ + va_list va; + va_start( va, format ); + int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); + va_end( va ); + return result; +} +#define TIXML_SSCANF sscanf_s +#else +// GCC version 3 and higher +//#warning( "Using sn* functions." ) +#define TIXML_SNPRINTF snprintf +#define TIXML_SSCANF sscanf +#endif + +static const int TIXML2_MAJOR_VERSION = 1; +static const int TIXML2_MINOR_VERSION = 0; +static const int TIXML2_PATCH_VERSION = 11; + +namespace tinyxml2 +{ +class XMLDocument; +class XMLElement; +class XMLAttribute; +class XMLComment; +class XMLText; +class XMLDeclaration; +class XMLUnknown; +class XMLPrinter; + +/* + A class that wraps strings. Normally stores the start and end + pointers into the XML file itself, and will apply normalization + and entity translation if actually read. Can also store (and memory + manage) a traditional char[] +*/ +class StrPair +{ +public: + enum { + NEEDS_ENTITY_PROCESSING = 0x01, + NEEDS_NEWLINE_NORMALIZATION = 0x02, + COLLAPSE_WHITESPACE = 0x04, + + TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_NAME = 0, + ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION, + ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION, + COMMENT = NEEDS_NEWLINE_NORMALIZATION + }; + + StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {} + ~StrPair(); + + void Set( char* start, char* end, int flags ) { + Reset(); + _start = start; + _end = end; + _flags = flags | NEEDS_FLUSH; + } + + const char* GetStr(); + + bool Empty() const { + return _start == _end; + } + + void SetInternedStr( const char* str ) { + Reset(); + _start = const_cast(str); + } + + void SetStr( const char* str, int flags=0 ); + + char* ParseText( char* in, const char* endTag, int strFlags ); + char* ParseName( char* in ); + +private: + void Reset(); + void CollapseWhitespace(); + + enum { + NEEDS_FLUSH = 0x100, + NEEDS_DELETE = 0x200 + }; + + // After parsing, if *_end != 0, it can be set to zero. + int _flags; + char* _start; + char* _end; +}; + + +/* + A dynamic array of Plain Old Data. Doesn't support constructors, etc. + Has a small initial memory pool, so that low or no usage will not + cause a call to new/delete +*/ +template +class DynArray +{ +public: + DynArray< T, INIT >() { + _mem = _pool; + _allocated = INIT; + _size = 0; + } + + ~DynArray() { + if ( _mem != _pool ) { + delete [] _mem; + } + } + + void Push( T t ) { + EnsureCapacity( _size+1 ); + _mem[_size++] = t; + } + + T* PushArr( int count ) { + EnsureCapacity( _size+count ); + T* ret = &_mem[_size]; + _size += count; + return ret; + } + + T Pop() { + return _mem[--_size]; + } + + void PopArr( int count ) { + TIXMLASSERT( _size >= count ); + _size -= count; + } + + bool Empty() const { + return _size == 0; + } + + T& operator[](int i) { + TIXMLASSERT( i>= 0 && i < _size ); + return _mem[i]; + } + + const T& operator[](int i) const { + TIXMLASSERT( i>= 0 && i < _size ); + return _mem[i]; + } + + int Size() const { + return _size; + } + + int Capacity() const { + return _allocated; + } + + const T* Mem() const { + return _mem; + } + + T* Mem() { + return _mem; + } + +private: + void EnsureCapacity( int cap ) { + if ( cap > _allocated ) { + int newAllocated = cap * 2; + T* newMem = new T[newAllocated]; + memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs + if ( _mem != _pool ) { + delete [] _mem; + } + _mem = newMem; + _allocated = newAllocated; + } + } + + T* _mem; + T _pool[INIT]; + int _allocated; // objects allocated + int _size; // number objects in use +}; + + +/* + Parent virtual class of a pool for fast allocation + and deallocation of objects. +*/ +class MemPool +{ +public: + MemPool() {} + virtual ~MemPool() {} + + virtual int ItemSize() const = 0; + virtual void* Alloc() = 0; + virtual void Free( void* ) = 0; + virtual void SetTracked() = 0; +}; + + +/* + Template child class to create pools of the correct type. +*/ +template< int SIZE > +class MemPoolT : public MemPool +{ +public: + MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {} + ~MemPoolT() { + // Delete the blocks. + for( int i=0; i<_blockPtrs.Size(); ++i ) { + delete _blockPtrs[i]; + } + } + + virtual int ItemSize() const { + return SIZE; + } + int CurrentAllocs() const { + return _currentAllocs; + } + + virtual void* Alloc() { + if ( !_root ) { + // Need a new block. + Block* block = new Block(); + _blockPtrs.Push( block ); + + for( int i=0; ichunk[i].next = &block->chunk[i+1]; + } + block->chunk[COUNT-1].next = 0; + _root = block->chunk; + } + void* result = _root; + _root = _root->next; + + ++_currentAllocs; + if ( _currentAllocs > _maxAllocs ) { + _maxAllocs = _currentAllocs; + } + _nAllocs++; + _nUntracked++; + return result; + } + virtual void Free( void* mem ) { + if ( !mem ) { + return; + } + --_currentAllocs; + Chunk* chunk = (Chunk*)mem; +#ifdef DEBUG + memset( chunk, 0xfe, sizeof(Chunk) ); +#endif + chunk->next = _root; + _root = chunk; + } + void Trace( const char* name ) { + printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n", + name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() ); + } + + void SetTracked() { + _nUntracked--; + } + + int Untracked() const { + return _nUntracked; + } + + // This number is perf sensitive. 4k seems like a good tradeoff on my machine. + // The test file is large, 170k. + // Release: VS2010 gcc(no opt) + // 1k: 4000 + // 2k: 4000 + // 4k: 3900 21000 + // 16k: 5200 + // 32k: 4300 + // 64k: 4000 21000 + enum { COUNT = (4*1024)/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private + +private: + union Chunk { + Chunk* next; + char mem[SIZE]; + }; + struct Block { + Chunk chunk[COUNT]; + }; + DynArray< Block*, 10 > _blockPtrs; + Chunk* _root; + + int _currentAllocs; + int _nAllocs; + int _maxAllocs; + int _nUntracked; +}; + + + +/** + Implements the interface to the "Visitor pattern" (see the Accept() method.) + If you call the Accept() method, it requires being passed a XMLVisitor + class to handle callbacks. For nodes that contain other nodes (Document, Element) + you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs + are simply called with Visit(). + + If you return 'true' from a Visit method, recursive parsing will continue. If you return + false, no children of this node or its siblings will be visited. + + All flavors of Visit methods have a default implementation that returns 'true' (continue + visiting). You need to only override methods that are interesting to you. + + Generally Accept() is called on the XMLDocument, although all nodes support visiting. + + You should never change the document from a callback. + + @sa XMLNode::Accept() +*/ +class TINYXML2_LIB XMLVisitor +{ +public: + virtual ~XMLVisitor() {} + + /// Visit a document. + virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { + return true; + } + /// Visit a document. + virtual bool VisitExit( const XMLDocument& /*doc*/ ) { + return true; + } + + /// Visit an element. + virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { + return true; + } + /// Visit an element. + virtual bool VisitExit( const XMLElement& /*element*/ ) { + return true; + } + + /// Visit a declaration. + virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { + return true; + } + /// Visit a text node. + virtual bool Visit( const XMLText& /*text*/ ) { + return true; + } + /// Visit a comment node. + virtual bool Visit( const XMLComment& /*comment*/ ) { + return true; + } + /// Visit an unknown node. + virtual bool Visit( const XMLUnknown& /*unknown*/ ) { + return true; + } +}; + + +/* + Utility functionality. +*/ +class XMLUtil +{ +public: + // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't + // correct, but simple, and usually works. + static const char* SkipWhiteSpace( const char* p ) { + while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast(p) ) ) { + ++p; + } + return p; + } + static char* SkipWhiteSpace( char* p ) { + while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast(p) ) ) { + ++p; + } + return p; + } + static bool IsWhiteSpace( char p ) { + return !IsUTF8Continuation(p) && isspace( static_cast(p) ); + } + + inline static bool IsNameStartChar( unsigned char ch ) { + return ( ( ch < 128 ) ? isalpha( ch ) : 1 ) + || ch == ':' + || ch == '_'; + } + + inline static bool IsNameChar( unsigned char ch ) { + return IsNameStartChar( ch ) + || isdigit( ch ) + || ch == '.' + || ch == '-'; + } + + inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) { + int n = 0; + if ( p == q ) { + return true; + } + while( *p && *q && *p == *q && n(const_cast(this)->FirstChildElement( value )); + } + + /// Get the last child node, or null if none exists. + const XMLNode* LastChild() const { + return _lastChild; + } + + XMLNode* LastChild() { + return const_cast(const_cast(this)->LastChild() ); + } + + /** Get the last child element or optionally the last child + element with the specified name. + */ + const XMLElement* LastChildElement( const char* value=0 ) const; + + XMLElement* LastChildElement( const char* value=0 ) { + return const_cast(const_cast(this)->LastChildElement(value) ); + } + + /// Get the previous (left) sibling node of this node. + const XMLNode* PreviousSibling() const { + return _prev; + } + + XMLNode* PreviousSibling() { + return _prev; + } + + /// Get the previous (left) sibling element of this node, with an optionally supplied name. + const XMLElement* PreviousSiblingElement( const char* value=0 ) const ; + + XMLElement* PreviousSiblingElement( const char* value=0 ) { + return const_cast(const_cast(this)->PreviousSiblingElement( value ) ); + } + + /// Get the next (right) sibling node of this node. + const XMLNode* NextSibling() const { + return _next; + } + + XMLNode* NextSibling() { + return _next; + } + + /// Get the next (right) sibling element of this node, with an optionally supplied name. + const XMLElement* NextSiblingElement( const char* value=0 ) const; + + XMLElement* NextSiblingElement( const char* value=0 ) { + return const_cast(const_cast(this)->NextSiblingElement( value ) ); + } + + /** + Add a child node as the last (right) child. + */ + XMLNode* InsertEndChild( XMLNode* addThis ); + + XMLNode* LinkEndChild( XMLNode* addThis ) { + return InsertEndChild( addThis ); + } + /** + Add a child node as the first (left) child. + */ + XMLNode* InsertFirstChild( XMLNode* addThis ); + /** + Add a node after the specified child node. + */ + XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ); + + /** + Delete all the children of this node. + */ + void DeleteChildren(); + + /** + Delete a child of this node. + */ + void DeleteChild( XMLNode* node ); + + /** + Make a copy of this node, but not its children. + You may pass in a Document pointer that will be + the owner of the new Node. If the 'document' is + null, then the node returned will be allocated + from the current Document. (this->GetDocument()) + + Note: if called on a XMLDocument, this will return null. + */ + virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0; + + /** + Test if 2 nodes are the same, but don't test children. + The 2 nodes do not need to be in the same Document. + + Note: if called on a XMLDocument, this will return false. + */ + virtual bool ShallowEqual( const XMLNode* compare ) const = 0; + + /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the + XML tree will be conditionally visited and the host will be called back + via the XMLVisitor interface. + + This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse + the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this + interface versus any other.) + + The interface has been based on ideas from: + + - http://www.saxproject.org/ + - http://c2.com/cgi/wiki?HierarchicalVisitorPattern + + Which are both good references for "visiting". + + An example of using Accept(): + @verbatim + XMLPrinter printer; + tinyxmlDoc.Accept( &printer ); + const char* xmlcstr = printer.CStr(); + @endverbatim + */ + virtual bool Accept( XMLVisitor* visitor ) const = 0; + + // internal + virtual char* ParseDeep( char*, StrPair* ); + +protected: + XMLNode( XMLDocument* ); + virtual ~XMLNode(); + XMLNode( const XMLNode& ); // not supported + XMLNode& operator=( const XMLNode& ); // not supported + + XMLDocument* _document; + XMLNode* _parent; + mutable StrPair _value; + + XMLNode* _firstChild; + XMLNode* _lastChild; + + XMLNode* _prev; + XMLNode* _next; + +private: + MemPool* _memPool; + void Unlink( XMLNode* child ); +}; + + +/** XML text. + + Note that a text node can have child element nodes, for example: + @verbatim + This is bold + @endverbatim + + A text node can have 2 ways to output the next. "normal" output + and CDATA. It will default to the mode it was parsed from the XML file and + you generally want to leave it alone, but you can change the output mode with + SetCData() and query it with CData(). +*/ +class TINYXML2_LIB XMLText : public XMLNode +{ + friend class XMLBase; + friend class XMLDocument; +public: + virtual bool Accept( XMLVisitor* visitor ) const; + + virtual XMLText* ToText() { + return this; + } + virtual const XMLText* ToText() const { + return this; + } + + /// Declare whether this should be CDATA or standard text. + void SetCData( bool isCData ) { + _isCData = isCData; + } + /// Returns true if this is a CDATA text element. + bool CData() const { + return _isCData; + } + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +protected: + XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {} + virtual ~XMLText() {} + XMLText( const XMLText& ); // not supported + XMLText& operator=( const XMLText& ); // not supported + +private: + bool _isCData; +}; + + +/** An XML Comment. */ +class TINYXML2_LIB XMLComment : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLComment* ToComment() { + return this; + } + virtual const XMLComment* ToComment() const { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const; + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +protected: + XMLComment( XMLDocument* doc ); + virtual ~XMLComment(); + XMLComment( const XMLComment& ); // not supported + XMLComment& operator=( const XMLComment& ); // not supported + +private: +}; + + +/** In correct XML the declaration is the first entry in the file. + @verbatim + + @endverbatim + + TinyXML-2 will happily read or write files without a declaration, + however. + + The text of the declaration isn't interpreted. It is parsed + and written as a string. +*/ +class TINYXML2_LIB XMLDeclaration : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLDeclaration* ToDeclaration() { + return this; + } + virtual const XMLDeclaration* ToDeclaration() const { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const; + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +protected: + XMLDeclaration( XMLDocument* doc ); + virtual ~XMLDeclaration(); + XMLDeclaration( const XMLDeclaration& ); // not supported + XMLDeclaration& operator=( const XMLDeclaration& ); // not supported +}; + + +/** Any tag that TinyXML-2 doesn't recognize is saved as an + unknown. It is a tag of text, but should not be modified. + It will be written back to the XML, unchanged, when the file + is saved. + + DTD tags get thrown into XMLUnknowns. +*/ +class TINYXML2_LIB XMLUnknown : public XMLNode +{ + friend class XMLDocument; +public: + virtual XMLUnknown* ToUnknown() { + return this; + } + virtual const XMLUnknown* ToUnknown() const { + return this; + } + + virtual bool Accept( XMLVisitor* visitor ) const; + + char* ParseDeep( char*, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +protected: + XMLUnknown( XMLDocument* doc ); + virtual ~XMLUnknown(); + XMLUnknown( const XMLUnknown& ); // not supported + XMLUnknown& operator=( const XMLUnknown& ); // not supported +}; + + +enum XMLError { + XML_NO_ERROR = 0, + XML_SUCCESS = 0, + + XML_NO_ATTRIBUTE, + XML_WRONG_ATTRIBUTE_TYPE, + + XML_ERROR_FILE_NOT_FOUND, + XML_ERROR_FILE_COULD_NOT_BE_OPENED, + XML_ERROR_FILE_READ_ERROR, + XML_ERROR_ELEMENT_MISMATCH, + XML_ERROR_PARSING_ELEMENT, + XML_ERROR_PARSING_ATTRIBUTE, + XML_ERROR_IDENTIFYING_TAG, + XML_ERROR_PARSING_TEXT, + XML_ERROR_PARSING_CDATA, + XML_ERROR_PARSING_COMMENT, + XML_ERROR_PARSING_DECLARATION, + XML_ERROR_PARSING_UNKNOWN, + XML_ERROR_EMPTY_DOCUMENT, + XML_ERROR_MISMATCHED_ELEMENT, + XML_ERROR_PARSING, + + XML_CAN_NOT_CONVERT_TEXT, + XML_NO_TEXT_NODE +}; + + +/** An attribute is a name-value pair. Elements have an arbitrary + number of attributes, each with a unique name. + + @note The attributes are not XMLNodes. You may only query the + Next() attribute in a list. +*/ +class TINYXML2_LIB XMLAttribute +{ + friend class XMLElement; +public: + /// The name of the attribute. + const char* Name() const { + return _name.GetStr(); + } + /// The value of the attribute. + const char* Value() const { + return _value.GetStr(); + } + /// The next attribute in the list. + const XMLAttribute* Next() const { + return _next; + } + + /** IntValue interprets the attribute as an integer, and returns the value. + If the value isn't an integer, 0 will be returned. There is no error checking; + use QueryIntValue() if you need error checking. + */ + int IntValue() const { + int i=0; + QueryIntValue( &i ); + return i; + } + /// Query as an unsigned integer. See IntValue() + unsigned UnsignedValue() const { + unsigned i=0; + QueryUnsignedValue( &i ); + return i; + } + /// Query as a boolean. See IntValue() + bool BoolValue() const { + bool b=false; + QueryBoolValue( &b ); + return b; + } + /// Query as a double. See IntValue() + double DoubleValue() const { + double d=0; + QueryDoubleValue( &d ); + return d; + } + /// Query as a float. See IntValue() + float FloatValue() const { + float f=0; + QueryFloatValue( &f ); + return f; + } + + /** QueryIntValue interprets the attribute as an integer, and returns the value + in the provided parameter. The function will return XML_NO_ERROR on success, + and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful. + */ + XMLError QueryIntValue( int* value ) const; + /// See QueryIntValue + XMLError QueryUnsignedValue( unsigned int* value ) const; + /// See QueryIntValue + XMLError QueryBoolValue( bool* value ) const; + /// See QueryIntValue + XMLError QueryDoubleValue( double* value ) const; + /// See QueryIntValue + XMLError QueryFloatValue( float* value ) const; + + /// Set the attribute to a string value. + void SetAttribute( const char* value ); + /// Set the attribute to value. + void SetAttribute( int value ); + /// Set the attribute to value. + void SetAttribute( unsigned value ); + /// Set the attribute to value. + void SetAttribute( bool value ); + /// Set the attribute to value. + void SetAttribute( double value ); + /// Set the attribute to value. + void SetAttribute( float value ); + +private: + enum { BUF_SIZE = 200 }; + + XMLAttribute() : _next( 0 ), _memPool( 0 ) {} + virtual ~XMLAttribute() {} + + XMLAttribute( const XMLAttribute& ); // not supported + void operator=( const XMLAttribute& ); // not supported + void SetName( const char* name ); + + char* ParseDeep( char* p, bool processEntities ); + + mutable StrPair _name; + mutable StrPair _value; + XMLAttribute* _next; + MemPool* _memPool; +}; + + +/** The element is a container class. It has a value, the element name, + and can contain other elements, text, comments, and unknowns. + Elements also contain an arbitrary number of attributes. +*/ +class TINYXML2_LIB XMLElement : public XMLNode +{ + friend class XMLBase; + friend class XMLDocument; +public: + /// Get the name of an element (which is the Value() of the node.) + const char* Name() const { + return Value(); + } + /// Set the name of the element. + void SetName( const char* str, bool staticMem=false ) { + SetValue( str, staticMem ); + } + + virtual XMLElement* ToElement() { + return this; + } + virtual const XMLElement* ToElement() const { + return this; + } + virtual bool Accept( XMLVisitor* visitor ) const; + + /** Given an attribute name, Attribute() returns the value + for the attribute of that name, or null if none + exists. For example: + + @verbatim + const char* value = ele->Attribute( "foo" ); + @endverbatim + + The 'value' parameter is normally null. However, if specified, + the attribute will only be returned if the 'name' and 'value' + match. This allow you to write code: + + @verbatim + if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar(); + @endverbatim + + rather than: + @verbatim + if ( ele->Attribute( "foo" ) ) { + if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar(); + } + @endverbatim + */ + const char* Attribute( const char* name, const char* value=0 ) const; + + /** Given an attribute name, IntAttribute() returns the value + of the attribute interpreted as an integer. 0 will be + returned if there is an error. For a method with error + checking, see QueryIntAttribute() + */ + int IntAttribute( const char* name ) const { + int i=0; + QueryIntAttribute( name, &i ); + return i; + } + /// See IntAttribute() + unsigned UnsignedAttribute( const char* name ) const { + unsigned i=0; + QueryUnsignedAttribute( name, &i ); + return i; + } + /// See IntAttribute() + bool BoolAttribute( const char* name ) const { + bool b=false; + QueryBoolAttribute( name, &b ); + return b; + } + /// See IntAttribute() + double DoubleAttribute( const char* name ) const { + double d=0; + QueryDoubleAttribute( name, &d ); + return d; + } + /// See IntAttribute() + float FloatAttribute( const char* name ) const { + float f=0; + QueryFloatAttribute( name, &f ); + return f; + } + + /** Given an attribute name, QueryIntAttribute() returns + XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion + can't be performed, or XML_NO_ATTRIBUTE if the attribute + doesn't exist. If successful, the result of the conversion + will be written to 'value'. If not successful, nothing will + be written to 'value'. This allows you to provide default + value: + + @verbatim + int value = 10; + QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 + @endverbatim + */ + XMLError QueryIntAttribute( const char* name, int* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryIntValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryUnsignedValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryBoolAttribute( const char* name, bool* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryBoolValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryDoubleAttribute( const char* name, double* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryDoubleValue( value ); + } + /// See QueryIntAttribute() + XMLError QueryFloatAttribute( const char* name, float* value ) const { + const XMLAttribute* a = FindAttribute( name ); + if ( !a ) { + return XML_NO_ATTRIBUTE; + } + return a->QueryFloatValue( value ); + } + + + /** Given an attribute name, QueryAttribute() returns + XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion + can't be performed, or XML_NO_ATTRIBUTE if the attribute + doesn't exist. It is overloaded for the primitive types, + and is a generally more convenient replacement of + QueryIntAttribute() and related functions. + + If successful, the result of the conversion + will be written to 'value'. If not successful, nothing will + be written to 'value'. This allows you to provide default + value: + + @verbatim + int value = 10; + QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10 + @endverbatim + */ + int QueryAttribute( const char* name, int* value ) const { + return QueryIntAttribute( name, value ); + } + + int QueryAttribute( const char* name, unsigned int* value ) const { + return QueryUnsignedAttribute( name, value ); + } + + int QueryAttribute( const char* name, bool* value ) const { + return QueryBoolAttribute( name, value ); + } + + int QueryAttribute( const char* name, double* value ) const { + return QueryDoubleAttribute( name, value ); + } + + int QueryAttribute( const char* name, float* value ) const { + return QueryFloatAttribute( name, value ); + } + + /// Sets the named attribute to value. + void SetAttribute( const char* name, const char* value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, int value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, unsigned value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, bool value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + /// Sets the named attribute to value. + void SetAttribute( const char* name, double value ) { + XMLAttribute* a = FindOrCreateAttribute( name ); + a->SetAttribute( value ); + } + + /** + Delete an attribute. + */ + void DeleteAttribute( const char* name ); + + /// Return the first attribute in the list. + const XMLAttribute* FirstAttribute() const { + return _rootAttribute; + } + /// Query a specific attribute in the list. + const XMLAttribute* FindAttribute( const char* name ) const; + + /** Convenience function for easy access to the text inside an element. Although easy + and concise, GetText() is limited compared to getting the XMLText child + and accessing it directly. + + If the first child of 'this' is a XMLText, the GetText() + returns the character string of the Text node, else null is returned. + + This is a convenient method for getting the text of simple contained text: + @verbatim + This is text + const char* str = fooElement->GetText(); + @endverbatim + + 'str' will be a pointer to "This is text". + + Note that this function can be misleading. If the element foo was created from + this XML: + @verbatim + This is text + @endverbatim + + then the value of str would be null. The first child node isn't a text node, it is + another element. From this XML: + @verbatim + This is text + @endverbatim + GetText() will return "This is ". + */ + const char* GetText() const; + + /** + Convenience method to query the value of a child text node. This is probably best + shown by example. Given you have a document is this form: + @verbatim + + 1 + 1.4 + + @endverbatim + + The QueryIntText() and similar functions provide a safe and easier way to get to the + "value" of x and y. + + @verbatim + int x = 0; + float y = 0; // types of x and y are contrived for example + const XMLElement* xElement = pointElement->FirstChildElement( "x" ); + const XMLElement* yElement = pointElement->FirstChildElement( "y" ); + xElement->QueryIntText( &x ); + yElement->QueryFloatText( &y ); + @endverbatim + + @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted + to the requested type, and XML_NO_TEXT_NODE if there is no child text to query. + + */ + XMLError QueryIntText( int* ival ) const; + /// See QueryIntText() + XMLError QueryUnsignedText( unsigned* uval ) const; + /// See QueryIntText() + XMLError QueryBoolText( bool* bval ) const; + /// See QueryIntText() + XMLError QueryDoubleText( double* dval ) const; + /// See QueryIntText() + XMLError QueryFloatText( float* fval ) const; + + // internal: + enum { + OPEN, // + CLOSED, // + CLOSING // + }; + int ClosingType() const { + return _closingType; + } + char* ParseDeep( char* p, StrPair* endTag ); + virtual XMLNode* ShallowClone( XMLDocument* document ) const; + virtual bool ShallowEqual( const XMLNode* compare ) const; + +private: + XMLElement( XMLDocument* doc ); + virtual ~XMLElement(); + XMLElement( const XMLElement& ); // not supported + void operator=( const XMLElement& ); // not supported + + XMLAttribute* FindAttribute( const char* name ); + XMLAttribute* FindOrCreateAttribute( const char* name ); + //void LinkAttribute( XMLAttribute* attrib ); + char* ParseAttributes( char* p ); + + int _closingType; + // The attribute list is ordered; there is no 'lastAttribute' + // because the list needs to be scanned for dupes before adding + // a new attribute. + XMLAttribute* _rootAttribute; +}; + + +enum Whitespace { + PRESERVE_WHITESPACE, + COLLAPSE_WHITESPACE +}; + + +/** A Document binds together all the functionality. + It can be saved, loaded, and printed to the screen. + All Nodes are connected and allocated to a Document. + If the Document is deleted, all its Nodes are also deleted. +*/ +class TINYXML2_LIB XMLDocument : public XMLNode +{ + friend class XMLElement; +public: + /// constructor + XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE ); + ~XMLDocument(); + + virtual XMLDocument* ToDocument() { + return this; + } + virtual const XMLDocument* ToDocument() const { + return this; + } + + /** + Parse an XML file from a character string. + Returns XML_NO_ERROR (0) on success, or + an errorID. + + You may optionally pass in the 'nBytes', which is + the number of bytes which will be parsed. If not + specified, TinyXML-2 will assume 'xml' points to a + null terminated string. + */ + XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) ); + + /** + Load an XML file from disk. + Returns XML_NO_ERROR (0) on success, or + an errorID. + */ + XMLError LoadFile( const char* filename ); + + /** + Load an XML file from disk. You are responsible + for providing and closing the FILE*. + + Returns XML_NO_ERROR (0) on success, or + an errorID. + */ + XMLError LoadFile( FILE* ); + + /** + Save the XML file to disk. + Returns XML_NO_ERROR (0) on success, or + an errorID. + */ + XMLError SaveFile( const char* filename, bool compact = false ); + + /** + Save the XML file to disk. You are responsible + for providing and closing the FILE*. + + Returns XML_NO_ERROR (0) on success, or + an errorID. + */ + XMLError SaveFile( FILE* fp, bool compact = false ); + + bool ProcessEntities() const { + return _processEntities; + } + Whitespace WhitespaceMode() const { + return _whitespace; + } + + /** + Returns true if this document has a leading Byte Order Mark of UTF8. + */ + bool HasBOM() const { + return _writeBOM; + } + /** Sets whether to write the BOM when writing the file. + */ + void SetBOM( bool useBOM ) { + _writeBOM = useBOM; + } + + /** Return the root element of DOM. Equivalent to FirstChildElement(). + To get the first node, use FirstChild(). + */ + XMLElement* RootElement() { + return FirstChildElement(); + } + const XMLElement* RootElement() const { + return FirstChildElement(); + } + + /** Print the Document. If the Printer is not provided, it will + print to stdout. If you provide Printer, this can print to a file: + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Or you can use a printer to print to memory: + @verbatim + XMLPrinter printer; + doc.Print( &printer ); + // printer.CStr() has a const char* to the XML + @endverbatim + */ + void Print( XMLPrinter* streamer=0 ) const; + virtual bool Accept( XMLVisitor* visitor ) const; + + /** + Create a new Element associated with + this Document. The memory for the Element + is managed by the Document. + */ + XMLElement* NewElement( const char* name ); + /** + Create a new Comment associated with + this Document. The memory for the Comment + is managed by the Document. + */ + XMLComment* NewComment( const char* comment ); + /** + Create a new Text associated with + this Document. The memory for the Text + is managed by the Document. + */ + XMLText* NewText( const char* text ); + /** + Create a new Declaration associated with + this Document. The memory for the object + is managed by the Document. + + If the 'text' param is null, the standard + declaration is used.: + @verbatim + + @endverbatim + */ + XMLDeclaration* NewDeclaration( const char* text=0 ); + /** + Create a new Unknown associated with + this Document. The memory for the object + is managed by the Document. + */ + XMLUnknown* NewUnknown( const char* text ); + + /** + Delete a node associated with this document. + It will be unlinked from the DOM. + */ + void DeleteNode( XMLNode* node ) { + node->_parent->DeleteChild( node ); + } + + void SetError( XMLError error, const char* str1, const char* str2 ); + + /// Return true if there was an error parsing the document. + bool Error() const { + return _errorID != XML_NO_ERROR; + } + /// Return the errorID. + XMLError ErrorID() const { + return _errorID; + } + /// Return a possibly helpful diagnostic location or string. + const char* GetErrorStr1() const { + return _errorStr1; + } + /// Return a possibly helpful secondary diagnostic location or string. + const char* GetErrorStr2() const { + return _errorStr2; + } + /// If there is an error, print it to stdout. + void PrintError() const; + + /// Clear the document, resetting it to the initial state. + void Clear(); + + // internal + char* Identify( char* p, XMLNode** node ); + + virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const { + return 0; + } + virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const { + return false; + } + +private: + XMLDocument( const XMLDocument& ); // not supported + void operator=( const XMLDocument& ); // not supported + + bool _writeBOM; + bool _processEntities; + XMLError _errorID; + Whitespace _whitespace; + const char* _errorStr1; + const char* _errorStr2; + char* _charBuffer; + + MemPoolT< sizeof(XMLElement) > _elementPool; + MemPoolT< sizeof(XMLAttribute) > _attributePool; + MemPoolT< sizeof(XMLText) > _textPool; + MemPoolT< sizeof(XMLComment) > _commentPool; +}; + + +/** + A XMLHandle is a class that wraps a node pointer with null checks; this is + an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2 + DOM structure. It is a separate utility class. + + Take an example: + @verbatim + + + + + + + @endverbatim + + Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very + easy to write a *lot* of code that looks like: + + @verbatim + XMLElement* root = document.FirstChildElement( "Document" ); + if ( root ) + { + XMLElement* element = root->FirstChildElement( "Element" ); + if ( element ) + { + XMLElement* child = element->FirstChildElement( "Child" ); + if ( child ) + { + XMLElement* child2 = child->NextSiblingElement( "Child" ); + if ( child2 ) + { + // Finally do something useful. + @endverbatim + + And that doesn't even cover "else" cases. XMLHandle addresses the verbosity + of such code. A XMLHandle checks for null pointers so it is perfectly safe + and correct to use: + + @verbatim + XMLHandle docHandle( &document ); + XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement(); + if ( child2 ) + { + // do something useful + @endverbatim + + Which is MUCH more concise and useful. + + It is also safe to copy handles - internally they are nothing more than node pointers. + @verbatim + XMLHandle handleCopy = handle; + @endverbatim + + See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects. +*/ +class TINYXML2_LIB XMLHandle +{ +public: + /// Create a handle from any node (at any depth of the tree.) This can be a null pointer. + XMLHandle( XMLNode* node ) { + _node = node; + } + /// Create a handle from a node. + XMLHandle( XMLNode& node ) { + _node = &node; + } + /// Copy constructor + XMLHandle( const XMLHandle& ref ) { + _node = ref._node; + } + /// Assignment + XMLHandle& operator=( const XMLHandle& ref ) { + _node = ref._node; + return *this; + } + + /// Get the first child of this handle. + XMLHandle FirstChild() { + return XMLHandle( _node ? _node->FirstChild() : 0 ); + } + /// Get the first child element of this handle. + XMLHandle FirstChildElement( const char* value=0 ) { + return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 ); + } + /// Get the last child of this handle. + XMLHandle LastChild() { + return XMLHandle( _node ? _node->LastChild() : 0 ); + } + /// Get the last child element of this handle. + XMLHandle LastChildElement( const char* _value=0 ) { + return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 ); + } + /// Get the previous sibling of this handle. + XMLHandle PreviousSibling() { + return XMLHandle( _node ? _node->PreviousSibling() : 0 ); + } + /// Get the previous sibling element of this handle. + XMLHandle PreviousSiblingElement( const char* _value=0 ) { + return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 ); + } + /// Get the next sibling of this handle. + XMLHandle NextSibling() { + return XMLHandle( _node ? _node->NextSibling() : 0 ); + } + /// Get the next sibling element of this handle. + XMLHandle NextSiblingElement( const char* _value=0 ) { + return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 ); + } + + /// Safe cast to XMLNode. This can return null. + XMLNode* ToNode() { + return _node; + } + /// Safe cast to XMLElement. This can return null. + XMLElement* ToElement() { + return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 ); + } + /// Safe cast to XMLText. This can return null. + XMLText* ToText() { + return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 ); + } + /// Safe cast to XMLUnknown. This can return null. + XMLUnknown* ToUnknown() { + return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 ); + } + /// Safe cast to XMLDeclaration. This can return null. + XMLDeclaration* ToDeclaration() { + return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 ); + } + +private: + XMLNode* _node; +}; + + +/** + A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the + same in all regards, except for the 'const' qualifiers. See XMLHandle for API. +*/ +class TINYXML2_LIB XMLConstHandle +{ +public: + XMLConstHandle( const XMLNode* node ) { + _node = node; + } + XMLConstHandle( const XMLNode& node ) { + _node = &node; + } + XMLConstHandle( const XMLConstHandle& ref ) { + _node = ref._node; + } + + XMLConstHandle& operator=( const XMLConstHandle& ref ) { + _node = ref._node; + return *this; + } + + const XMLConstHandle FirstChild() const { + return XMLConstHandle( _node ? _node->FirstChild() : 0 ); + } + const XMLConstHandle FirstChildElement( const char* value=0 ) const { + return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 ); + } + const XMLConstHandle LastChild() const { + return XMLConstHandle( _node ? _node->LastChild() : 0 ); + } + const XMLConstHandle LastChildElement( const char* _value=0 ) const { + return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 ); + } + const XMLConstHandle PreviousSibling() const { + return XMLConstHandle( _node ? _node->PreviousSibling() : 0 ); + } + const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const { + return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 ); + } + const XMLConstHandle NextSibling() const { + return XMLConstHandle( _node ? _node->NextSibling() : 0 ); + } + const XMLConstHandle NextSiblingElement( const char* _value=0 ) const { + return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 ); + } + + + const XMLNode* ToNode() const { + return _node; + } + const XMLElement* ToElement() const { + return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 ); + } + const XMLText* ToText() const { + return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 ); + } + const XMLUnknown* ToUnknown() const { + return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 ); + } + const XMLDeclaration* ToDeclaration() const { + return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 ); + } + +private: + const XMLNode* _node; +}; + + +/** + Printing functionality. The XMLPrinter gives you more + options than the XMLDocument::Print() method. + + It can: + -# Print to memory. + -# Print to a file you provide. + -# Print XML without a XMLDocument. + + Print to Memory + + @verbatim + XMLPrinter printer; + doc.Print( &printer ); + SomeFunction( printer.CStr() ); + @endverbatim + + Print to a File + + You provide the file pointer. + @verbatim + XMLPrinter printer( fp ); + doc.Print( &printer ); + @endverbatim + + Print without a XMLDocument + + When loading, an XML parser is very useful. However, sometimes + when saving, it just gets in the way. The code is often set up + for streaming, and constructing the DOM is just overhead. + + The Printer supports the streaming case. The following code + prints out a trivially simple XML file without ever creating + an XML document. + + @verbatim + XMLPrinter printer( fp ); + printer.OpenElement( "foo" ); + printer.PushAttribute( "foo", "bar" ); + printer.CloseElement(); + @endverbatim +*/ +class TINYXML2_LIB XMLPrinter : public XMLVisitor +{ +public: + /** Construct the printer. If the FILE* is specified, + this will print to the FILE. Else it will print + to memory, and the result is available in CStr(). + If 'compact' is set to true, then output is created + with only required whitespace and newlines. + */ + XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 ); + ~XMLPrinter() {} + + /** If streaming, write the BOM and declaration. */ + void PushHeader( bool writeBOM, bool writeDeclaration ); + /** If streaming, start writing an element. + The element must be closed with CloseElement() + */ + void OpenElement( const char* name ); + /// If streaming, add an attribute to an open element. + void PushAttribute( const char* name, const char* value ); + void PushAttribute( const char* name, int value ); + void PushAttribute( const char* name, unsigned value ); + void PushAttribute( const char* name, bool value ); + void PushAttribute( const char* name, double value ); + /// If streaming, close the Element. + void CloseElement(); + + /// Add a text node. + void PushText( const char* text, bool cdata=false ); + /// Add a text node from an integer. + void PushText( int value ); + /// Add a text node from an unsigned. + void PushText( unsigned value ); + /// Add a text node from a bool. + void PushText( bool value ); + /// Add a text node from a float. + void PushText( float value ); + /// Add a text node from a double. + void PushText( double value ); + + /// Add a comment + void PushComment( const char* comment ); + + void PushDeclaration( const char* value ); + void PushUnknown( const char* value ); + + virtual bool VisitEnter( const XMLDocument& /*doc*/ ); + virtual bool VisitExit( const XMLDocument& /*doc*/ ) { + return true; + } + + virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute ); + virtual bool VisitExit( const XMLElement& element ); + + virtual bool Visit( const XMLText& text ); + virtual bool Visit( const XMLComment& comment ); + virtual bool Visit( const XMLDeclaration& declaration ); + virtual bool Visit( const XMLUnknown& unknown ); + + /** + If in print to memory mode, return a pointer to + the XML file in memory. + */ + const char* CStr() const { + return _buffer.Mem(); + } + /** + If in print to memory mode, return the size + of the XML file in memory. (Note the size returned + includes the terminating null.) + */ + int CStrSize() const { + return _buffer.Size(); + } + +private: + void SealElement(); + void PrintSpace( int depth ); + void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities. + void Print( const char* format, ... ); + + bool _elementJustOpened; + bool _firstElement; + FILE* _fp; + int _depth; + int _textDepth; + bool _processEntities; + bool _compactMode; + + enum { + ENTITY_RANGE = 64, + BUF_SIZE = 200 + }; + bool _entityFlag[ENTITY_RANGE]; + bool _restrictedEntityFlag[ENTITY_RANGE]; + + DynArray< const char*, 10 > _stack; + DynArray< char, 20 > _buffer; +#ifdef _MSC_VER + DynArray< char, 20 > _accumulator; +#endif +}; + + +} // tinyxml2 + +#if defined(_MSC_VER) +# pragma warning(pop) +#endif + +#endif // TINYXML2_INCLUDED diff --git a/Release/Exportall.ahk b/Release/Exportall.ahk new file mode 100644 index 0000000..7fbcf89 --- /dev/null +++ b/Release/Exportall.ahk @@ -0,0 +1,139 @@ +#SingleInstance off +#NoEnv +#Warn +FileEncoding, UTF-8-RAW + +; Name of function in DLL to export a drive's index +DllExportFuncname := "ExportIndex" + +RunAsAdmin() { + global + params := + Loop, %0% ; For each parameter: + params .= A_Space . %A_Index% + local ShellExecute + ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA" + if not A_IsAdmin + { + A_IsCompiled + ? DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1) + : DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1) + ExitApp + } +} + +ToHex(num) +{ + if num is not integer + return num + oldFmt := A_FormatInteger + SetFormat, integer, hex + num := num + 0 + SetFormat, integer,% oldFmt + return num +} +;returns positive hex value of last error +GetLastError() +{ + return ToHex(A_LastError < 0 ? A_LastError & 0xFFFFFFFF : A_LastError) +} + +ErrorFormat(error_id) +{ + VarSetCapacity(msg,500+500*A_IsUnicode,0) + if !len := DllCall("FormatMessage" + ,"UInt",FORMAT_MESSAGE_FROM_SYSTEM := 0x00001000 | FORMAT_MESSAGE_IGNORE_INSERTS := 0x00000200 ;dwflags + ,"Ptr",0 ;lpSource + ,"UInt",error_id ;dwMessageId + ,"UInt",0 ;dwLanguageId + ,"Ptr",&msg ;lpBuffer + ,"UInt",500) ;nSize + return + return strget(&msg,len) +} + +ExitWithMessage(msg) { + MsgBox, 16, % "[Autohotkey message, will auto-close in 30 seconds]", % msg, 30 + ExitApp +} + +Class NtfsFastProc { + Description := "Quickly process NTFS filesystem metadata on a local disk." + LoggedInitError := "" + + Class ErrorModeHelper { + __New() { + ; prevent error mode dialogs from hanging the application + this.oldErrMode := DllCall("SetErrorMode", "UInt", SEM_FAILCRITICALERRORS := 0x0001) + } + __Delete() { + DllCall("SetErrorMode", "UInt", this.oldErrMode) + } + } + + __New(providerDllPath) { + global DllExportFuncname + this.DllPath := providerDllPath + foo := new NtfsFastProc.ErrorModeHelper + this.hModule := DllCall("LoadLibrary", "Str", providerDllPath, "PTR") + if !this.hModule + { + this.LoggedInitError := "Failed to load provider dll: " providerDllPath "`n`nMake sure it is a valid executable and that all runtime dependencies are satisfied.`n`nMessage: " ErrorFormat(GetLastError()) + return this + } + this.QueryProc := DllCall("GetProcAddress", "PTR", this.hModule, "AStr", DllExportFuncname, "PTR") + if !this.QueryProc + { + this.LoggedInitError := "Could not find a '" DllExportFuncname "' export symbol in provider dll: " providerDllPath "`n`nIt may not be a supported dll or the file is corrupt.`n`nMessage: " ErrorFormat(GetLastError()) + return this + } + } + + __Delete() { + if this.hModule + { + DllCall("FreeLibrary", "PTR", this.hModule) + this.hModule := 0 + } + } + + BuildFileDatabase(LoadExisting = true) { + NTFSDrives := this.GetIndexingDrives() + for index, Drive in NTFSDrives + { + ExportPath := A_ScriptDir "\" Drive "_export.xml" + DriveIndex := DllCall(this.DllPath "\CreateIndex", "ushort", NumGet(Drive, "ushort"), "PTR") + ; May be locked with BitLocker if DriveIndex is null + if (DriveIndex) + { + hSrResult := DllCall(this.QueryProc, "PTR", DriveIndex, "wstr", ExportPath, "int", ExportFormatAdcXml := 0, PTR) + SoundPlay, *64 + } +; ExportPath := A_ScriptDir "\" Drive "_export.xmlz4" +; DriveIndex := DllCall(this.DllPath "\CreateIndex", "ushort", NumGet(Drive, "ushort"), "PTR") +; if (DriveIndex) +; { +; hSrResult := DllCall(this.QueryProc, "PTR", DriveIndex, "wstr", ExportPath, "int", ExportFormatAdcXml_LZ4 := 1, PTR) +; SoundPlay, *64 +; } + } + } + + GetIndexingDrives() { + return ["J"] + } +} + + + + +RunAsAdmin() + +DllPath := A_ScriptDir "\FileSearch.dll" + +fastProc := new NtfsFastProc(DllPath) +fastProc.LoggedInitError <> "" ? ExitWithMessage(fastProc.loggedInitError) : + +fastProc.BuildFileDatabase() + + diff --git a/Release/FileSearch.dll b/Release/FileSearch.dll index f2618a9..07dec41 100644 Binary files a/Release/FileSearch.dll and b/Release/FileSearch.dll differ diff --git a/Release/FileSearch.exp b/Release/FileSearch.exp new file mode 100644 index 0000000..e63acc5 Binary files /dev/null and b/Release/FileSearch.exp differ diff --git a/x64/Release/FileSearch.dll b/x64/Release/FileSearch.dll index 2d18dd5..84d284f 100644 Binary files a/x64/Release/FileSearch.dll and b/x64/Release/FileSearch.dll differ