2014-04-06 18:31:53 +00:00
|
|
|
/* Benjamin DELPY `gentilkiwi`
|
2020-09-17 01:17:11 +00:00
|
|
|
https://blog.gentilkiwi.com
|
2014-04-06 18:31:53 +00:00
|
|
|
benjamin@gentilkiwi.com
|
2015-08-25 09:19:01 +00:00
|
|
|
Licence : https://creativecommons.org/licenses/by/4.0/
|
2014-04-06 18:31:53 +00:00
|
|
|
*/
|
|
|
|
#include "kull_m_file.h"
|
|
|
|
|
2016-12-22 23:21:08 +00:00
|
|
|
BOOL isBase64InterceptOutput = FALSE, isBase64InterceptInput = FALSE;
|
2014-04-23 20:00:29 +00:00
|
|
|
|
2014-04-06 18:31:53 +00:00
|
|
|
BOOL kull_m_file_getCurrentDirectory(wchar_t ** ppDirName)
|
|
|
|
{
|
|
|
|
BOOL reussite = FALSE;
|
|
|
|
DWORD tailleRequise = GetCurrentDirectory(0, NULL);
|
2014-09-07 16:40:32 +00:00
|
|
|
if(*ppDirName = (wchar_t *) LocalAlloc(LPTR, tailleRequise * sizeof(wchar_t)))
|
|
|
|
if(!(reussite = (tailleRequise > 0 && (GetCurrentDirectory(tailleRequise, *ppDirName) == tailleRequise - 1))))
|
|
|
|
LocalFree(*ppDirName);
|
2014-04-06 18:31:53 +00:00
|
|
|
|
|
|
|
return reussite;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:16:46 +00:00
|
|
|
BOOL kull_m_file_getAbsolutePathOf(PCWCHAR thisData, wchar_t ** reponse)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
|
|
|
BOOL reussite = FALSE;
|
|
|
|
wchar_t *monRep;
|
2018-02-05 23:16:51 +00:00
|
|
|
*reponse = (wchar_t *) LocalAlloc(LPTR, MAX_PATH * sizeof(wchar_t));
|
2014-04-06 18:31:53 +00:00
|
|
|
|
|
|
|
if(PathIsRelative(thisData))
|
|
|
|
{
|
|
|
|
if(kull_m_file_getCurrentDirectory(&monRep))
|
|
|
|
{
|
|
|
|
reussite = (PathCombine(*reponse , monRep, thisData) != NULL);
|
|
|
|
LocalFree(monRep);
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 23:16:51 +00:00
|
|
|
else reussite = PathCanonicalize(*reponse, thisData);
|
2014-04-06 18:31:53 +00:00
|
|
|
|
|
|
|
if(!reussite)
|
|
|
|
LocalFree(*reponse);
|
|
|
|
|
|
|
|
return reussite;
|
|
|
|
}
|
|
|
|
|
2016-01-12 02:13:12 +00:00
|
|
|
BOOL kull_m_file_isFileExist(PCWCHAR fileName)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
|
|
|
BOOL reussite = FALSE;
|
2016-01-12 02:13:12 +00:00
|
|
|
HANDLE hFile = NULL;
|
2014-04-06 18:31:53 +00:00
|
|
|
|
2016-01-31 15:01:45 +00:00
|
|
|
reussite = ((hFile = CreateFile(fileName, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) && hFile != INVALID_HANDLE_VALUE);
|
|
|
|
if(reussite)
|
|
|
|
CloseHandle(hFile);
|
2014-04-06 18:31:53 +00:00
|
|
|
return reussite;
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:08:23 +00:00
|
|
|
BOOL kull_m_file_writeData(PCWCHAR fileName, LPCVOID data, DWORD lenght)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
|
|
|
BOOL reussite = FALSE;
|
2014-04-23 20:00:29 +00:00
|
|
|
DWORD dwBytesWritten = 0, i;
|
|
|
|
HANDLE hFile = NULL;
|
|
|
|
LPWSTR base64;
|
2016-01-12 02:13:12 +00:00
|
|
|
|
2016-12-22 23:21:08 +00:00
|
|
|
if(isBase64InterceptOutput)
|
2014-04-23 20:00:29 +00:00
|
|
|
{
|
2016-01-31 15:01:45 +00:00
|
|
|
if(CryptBinaryToString((const BYTE *) data, lenght, CRYPT_STRING_BASE64, NULL, &dwBytesWritten))
|
2014-04-23 20:00:29 +00:00
|
|
|
{
|
2016-01-31 15:01:45 +00:00
|
|
|
if(base64 = (LPWSTR) LocalAlloc(LPTR, dwBytesWritten * sizeof(wchar_t)))
|
2014-04-23 20:00:29 +00:00
|
|
|
{
|
2016-01-31 15:01:45 +00:00
|
|
|
if(reussite = CryptBinaryToString((const BYTE *) data, lenght, CRYPT_STRING_BASE64, base64, &dwBytesWritten))
|
2014-04-23 20:00:29 +00:00
|
|
|
{
|
2016-01-31 15:01:45 +00:00
|
|
|
kprintf(L"\n====================\nBase64 of file : %s\n====================\n", fileName);
|
|
|
|
for(i = 0; i < dwBytesWritten; i++)
|
|
|
|
kprintf(L"%c", base64[i]);
|
|
|
|
kprintf(L"====================\n");
|
2014-04-23 20:00:29 +00:00
|
|
|
}
|
2016-01-31 15:01:45 +00:00
|
|
|
LocalFree(base64);
|
2014-04-23 20:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 15:01:45 +00:00
|
|
|
}
|
|
|
|
else if((hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) && hFile != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if(WriteFile(hFile, data, lenght, &dwBytesWritten, NULL) && (lenght == dwBytesWritten))
|
|
|
|
reussite = FlushFileBuffers(hFile);
|
|
|
|
CloseHandle(hFile);
|
2014-04-06 18:31:53 +00:00
|
|
|
}
|
|
|
|
return reussite;
|
|
|
|
}
|
|
|
|
|
2016-01-12 02:13:12 +00:00
|
|
|
BOOL kull_m_file_readData(PCWCHAR fileName, PBYTE * data, PDWORD lenght) // for ""little"" files !
|
2020-08-04 12:06:21 +00:00
|
|
|
{
|
|
|
|
return kull_m_file_readGeneric(fileName, data, lenght, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL kull_m_file_readGeneric(PCWCHAR fileName, PBYTE * data, PDWORD lenght, DWORD flags)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
|
|
|
BOOL reussite = FALSE;
|
|
|
|
DWORD dwBytesReaded;
|
|
|
|
LARGE_INTEGER filesize;
|
2016-01-12 02:13:12 +00:00
|
|
|
HANDLE hFile = NULL;
|
2016-01-31 15:01:45 +00:00
|
|
|
|
2016-12-22 23:21:08 +00:00
|
|
|
if(isBase64InterceptInput)
|
|
|
|
{
|
|
|
|
if(!(reussite = kull_m_string_quick_base64_to_Binary(fileName, data, lenght)))
|
|
|
|
PRINT_ERROR_AUTO(L"kull_m_string_quick_base64_to_Binary");
|
|
|
|
}
|
2020-08-04 12:06:21 +00:00
|
|
|
else if((hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, flags, NULL)) && hFile != INVALID_HANDLE_VALUE)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
2016-01-31 15:01:45 +00:00
|
|
|
if(GetFileSizeEx(hFile, &filesize) && !filesize.HighPart)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
2016-01-31 15:01:45 +00:00
|
|
|
*lenght = filesize.LowPart;
|
|
|
|
if(*data = (PBYTE) LocalAlloc(LPTR, *lenght))
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
2016-01-31 15:01:45 +00:00
|
|
|
if(!(reussite = ReadFile(hFile, *data, *lenght, &dwBytesReaded, NULL) && (*lenght == dwBytesReaded)))
|
|
|
|
LocalFree(*data);
|
2014-04-06 18:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 15:01:45 +00:00
|
|
|
CloseHandle(hFile);
|
2014-04-06 18:31:53 +00:00
|
|
|
}
|
|
|
|
return reussite;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t kull_m_file_forbiddenChars[] = {L'\\', L'/', L':', L'*', L'?', L'\"', L'<', L'>', L'|'};
|
2016-01-12 02:13:12 +00:00
|
|
|
void kull_m_file_cleanFilename(PWCHAR fileName)
|
2014-04-06 18:31:53 +00:00
|
|
|
{
|
|
|
|
DWORD i, j;
|
|
|
|
for(i = 0; fileName[i]; i++)
|
2014-05-28 16:00:36 +00:00
|
|
|
for(j = 0; j < ARRAYSIZE(kull_m_file_forbiddenChars); j++)
|
2014-04-06 18:31:53 +00:00
|
|
|
if(fileName[i] == kull_m_file_forbiddenChars[j])
|
|
|
|
fileName[i] = L'~';
|
2016-01-12 02:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PWCHAR kull_m_file_fullPath(PCWCHAR fileName)
|
|
|
|
{
|
|
|
|
PWCHAR buffer = NULL;
|
|
|
|
DWORD bufferLen;
|
|
|
|
if(fileName)
|
|
|
|
if(bufferLen = ExpandEnvironmentStrings(fileName, NULL, 0))
|
|
|
|
if(buffer = (PWCHAR) LocalAlloc(LPTR, bufferLen * sizeof(wchar_t)))
|
|
|
|
if(bufferLen != ExpandEnvironmentStrings(fileName, buffer, bufferLen))
|
|
|
|
buffer = (PWCHAR) LocalFree(buffer);
|
|
|
|
return buffer;
|
2016-01-31 15:01:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 04:11:42 +00:00
|
|
|
BOOL kull_m_file_Find(PCWCHAR directory, PCWCHAR filter, BOOL isRecursive /*TODO*/, DWORD level, BOOL isPrintInfos, BOOL isWithDir, PKULL_M_FILE_FIND_CALLBACK callback, PVOID pvArg)
|
2016-01-31 15:01:45 +00:00
|
|
|
{
|
2017-06-07 00:37:32 +00:00
|
|
|
BOOL status = FALSE;
|
2016-01-31 15:01:45 +00:00
|
|
|
DWORD dwAttrib;
|
|
|
|
HANDLE hFind;
|
|
|
|
WIN32_FIND_DATA fData;
|
|
|
|
PWCHAR fullpath;
|
|
|
|
|
|
|
|
dwAttrib = GetFileAttributes(directory);
|
|
|
|
if((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
{
|
|
|
|
if(isPrintInfos && !level)
|
|
|
|
{
|
|
|
|
kprintf(L"%*s" L"Directory \'%s\'", level << 1, L"", directory);
|
|
|
|
if(filter)
|
|
|
|
kprintf(L" (%s)", filter);
|
|
|
|
kprintf(L"\n");
|
|
|
|
}
|
|
|
|
if(fullpath = (wchar_t *) LocalAlloc(LPTR, MAX_PATH * sizeof(wchar_t)))
|
|
|
|
{
|
|
|
|
if(wcscpy_s(fullpath, MAX_PATH, directory) == 0)
|
|
|
|
{
|
|
|
|
if(wcscat_s(fullpath, MAX_PATH, L"\\") == 0)
|
|
|
|
{
|
|
|
|
if(wcscat_s(fullpath, MAX_PATH, filter ? filter : L"*") == 0)
|
|
|
|
{
|
|
|
|
hFind = FindFirstFile(fullpath, &fData);
|
|
|
|
if(hFind != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(_wcsicmp(fData.cFileName, L".") && _wcsicmp(fData.cFileName, L".."))
|
|
|
|
{
|
|
|
|
if(wcscpy_s(fullpath, MAX_PATH, directory) == 0)
|
|
|
|
{
|
|
|
|
if(wcscat_s(fullpath, MAX_PATH, L"\\") == 0)
|
|
|
|
{
|
|
|
|
dwAttrib = (DWORD) wcslen(fullpath);
|
|
|
|
if(wcscat_s(fullpath, MAX_PATH, fData.cFileName) == 0)
|
|
|
|
{
|
|
|
|
if(isPrintInfos)
|
|
|
|
kprintf(L"%*s" L"%3u %c|'%s\'\n", level << 1, L"", level, (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? L'D' : L'F' , fData.cFileName);
|
2020-08-24 04:11:42 +00:00
|
|
|
if(isWithDir || !(fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
2016-01-31 15:01:45 +00:00
|
|
|
{
|
|
|
|
if(callback)
|
2017-06-07 00:37:32 +00:00
|
|
|
status = callback(level, fullpath, fullpath + dwAttrib, pvArg);
|
2016-01-31 15:01:45 +00:00
|
|
|
}
|
|
|
|
else if(isRecursive && fData.cFileName)
|
2020-08-24 04:11:42 +00:00
|
|
|
status = kull_m_file_Find(fullpath, filter, TRUE, level + 1, isPrintInfos, isWithDir, callback, pvArg);
|
2016-01-31 15:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 00:37:32 +00:00
|
|
|
} while(!status && FindNextFile(hFind, &fData));
|
2016-01-31 15:01:45 +00:00
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 04:11:42 +00:00
|
|
|
LocalFree(fullpath);
|
2016-01-31 15:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
2014-04-06 18:31:53 +00:00
|
|
|
}
|