📄 win32filestore.c
字号:
status = VoltConvertWideToMultiByteAlloc (
path, CP_UTF8, &szConverted, libCtx);
if (status != 0)
break;
#else
status = 0;
szConverted = path;
#endif
if (len < bufLen)
{
Z2Memcpy (temp, szConverted, len);
bufLen -= len;
temp += len;
}
if (VoltageNameLen < bufLen)
Z2Memcpy (temp, VoltageName, VoltageNameLen);
#ifdef UNICODE
if (szConverted != (LPSTR)0)
Z2Free(szConverted);
#endif
} while (0);
return (status);
}
int mIcStoreData (
VoltStorageCtx *prov,
unsigned char *data,
unsigned int dataLen,
unsigned char *extraPassword,
unsigned int extraPasswordLen,
VoltFileCtx *fileCtx,
VoltFileHandle fileHandle
)
{
int status;
BOOL retVal;
DWORD error;
unsigned char *finalPassword = (unsigned char *)0;
VoltLibCtx *libCtx = (VoltLibCtx *)(prov->voltObject.libraryCtx);
unsigned short desc[13] =
{
(unsigned short)'V', (unsigned short)'o', (unsigned short)'l',
(unsigned short)'t', (unsigned short)'a', (unsigned short)'g',
(unsigned short)'e', (unsigned short)'S', (unsigned short)'t',
(unsigned short)'o', (unsigned short)'r', (unsigned short)'e',
(unsigned short)0
};
DATA_BLOB dataIn;
DATA_BLOB dataOut;
DATA_BLOB extraPass;
DATA_BLOB *extra = (DATA_BLOB *)0;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_SYSTEM_ERROR (systemError)
VOLT_DECLARE_ERROR_DESC(description)
VOLT_DECLARE_FNCT_LINE (fnctLine)
Z2Memset (&dataIn, 0, sizeof (DATA_BLOB));
Z2Memset (&dataOut, 0, sizeof (DATA_BLOB));
Z2Memset (&extraPass, 0, sizeof (DATA_BLOB));
do
{
dataIn.pbData = (BYTE *)data;
dataIn.cbData = dataLen;
if ( (extraPassword != (unsigned char *)0) && (extraPasswordLen != 0) )
{
status = VT_ERROR_MEMORY;
finalPassword = (unsigned char *)Z2Malloc (
extraPasswordLen + 1,
VOLT_MEMORY_SENSITIVE);
if (finalPassword == (unsigned char *)0)
break;
Z2Memset (finalPassword, 0, extraPasswordLen + 1);
Z2Memcpy (finalPassword, extraPassword, extraPasswordLen);
extraPass.pbData = (BYTE *)finalPassword;
extraPass.cbData = extraPasswordLen + 1;
extra = &extraPass;
}
/* Call the WinCrypt function that encrypts the data.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_FILE_WRITE;
retVal = CryptProtectData (
&dataIn, (LPCWSTR)&desc, extra, NULL, NULL, 0, &dataOut);
if (retVal == FALSE)
{
error = GetLastError ();
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_SYSTEM | VT_ERROR_TYPE_PRIMARY)
VOLT_SET_SYSTEM_ERROR (systemError, error)
VOLT_SET_ERROR_DESC(description, "System function CryptProtectData failed")
break;
}
/* Store the encrypted data in the file.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = fileCtx->CtxWriteFile (
fileCtx, fileHandle, dataOut.pbData, dataOut.cbData);
} while (0);
/* cleanup any memory we allocated
*/
if (finalPassword != (unsigned char *)0)
Z2Free (finalPassword);
if (dataOut.pbData != NULL)
LocalFree (dataOut.pbData);
VOLT_LOG_ERROR_SYSTEM (
(VtLibCtx)libCtx, status, systemError, errorType,
fnctLine, "mIcStoreData", description)
return (status);
}
int mIcLoadDataAlloc (
VoltStorageCtx *prov,
VoltFileCtx *fileCtx,
VoltFileHandle fileHandle,
unsigned char *extraPassword,
unsigned int extraPasswordLen,
unsigned char **data,
unsigned int *dataLen
)
{
int status;
BOOL retVal;
DWORD error;
unsigned int contentsLen;
VoltFileInt fileSize;
VoltLibCtx *libCtx = (VoltLibCtx *)(prov->voltObject.libraryCtx);
unsigned char *contents = (unsigned char *)0;
unsigned char *finalPassword = (unsigned char *)0;
unsigned char *buffer = (unsigned char *)0;
LPWSTR desc = (LPWSTR)0;
DATA_BLOB dataIn;
DATA_BLOB dataOut;
DATA_BLOB extraPass;
DATA_BLOB *extra = (DATA_BLOB *)0;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_SYSTEM_ERROR (systemError)
VOLT_DECLARE_ERROR_DESC(description)
VOLT_DECLARE_FNCT_LINE (fnctLine)
Z2Memset (&dataIn, 0, sizeof (DATA_BLOB));
Z2Memset (&dataOut, 0, sizeof (DATA_BLOB));
Z2Memset (&extraPass, 0, sizeof (DATA_BLOB));
*data = (unsigned char *)0;
*dataLen = 0;
do
{
/* Get the data out of the file.
* First, how big is the file?
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = fileCtx->CtxGetFileSize (
fileCtx, fileHandle, (char *)0, &fileSize);
if (status != 0)
break;
/* Allocate the buffer to hold the contents.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
contents = (unsigned char *)Z2Malloc (fileSize, 0);
if (contents == (unsigned char *)0)
break;
/* Get the data.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = fileCtx->CtxReadFile (
fileCtx, fileHandle, (unsigned int)fileSize, contents, &contentsLen);
if ( (status != 0) && (status != VT_ERROR_FILE_END_OF_FILE) )
break;
/* Call the WinCrypt function that decrypts the data.
*/
if ( (extraPassword != (unsigned char *)0) && (extraPasswordLen != 0) )
{
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
finalPassword = (unsigned char *)Z2Malloc (
extraPasswordLen + 1, VOLT_MEMORY_SENSITIVE);
if (finalPassword == (unsigned char *)0)
break;
Z2Memset (finalPassword, 0, extraPasswordLen + 1);
Z2Memcpy (finalPassword, extraPassword, extraPasswordLen);
extraPass.pbData = (BYTE *)finalPassword;
extraPass.cbData = extraPasswordLen + 1;
extra = &extraPass;
}
dataIn.pbData = (BYTE *)contents;
dataIn.cbData = contentsLen;
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_FILE_READ;
retVal = CryptUnprotectData (
&dataIn, (LPWSTR *)&desc, extra, NULL, NULL, 0, &dataOut);
if (retVal == FALSE)
{
error = GetLastError ();
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_SYSTEM | VT_ERROR_TYPE_PRIMARY)
VOLT_SET_SYSTEM_ERROR (systemError, error)
VOLT_SET_ERROR_DESC(description, "System function CryptUnprotectData failed")
break;
}
/* Copy the data into a buffer to return to the caller.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
buffer = (unsigned char *)Z2Malloc (dataOut.cbData, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
Z2Memcpy (buffer, dataOut.pbData, dataOut.cbData);
*data = buffer;
*dataLen = dataOut.cbData;
status = 0;
} while (0);
if (finalPassword != (unsigned char *)0)
Z2Free (finalPassword);
if (dataOut.pbData != NULL)
LocalFree (dataOut.pbData);
if (desc != (LPWSTR)0)
LocalFree (desc);
if (contents != (unsigned char *)0)
Z2Free (contents);
/*If no error, we're done.
*/
if (status == 0)
return (0);
/* If there was an error, free what was going to be the return buffer.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR_SYSTEM (
(VtLibCtx)libCtx, status, systemError, errorType,
fnctLine, "mIcLoadDataAlloc", description)
return (status);
}
int VoltGetFileListAlloc (
VoltLibCtx *libCtx,
unsigned char *directoryName,
unsigned int subdirectoriesFlag,
VtFileNameList **nameList
)
{
int status;
BOOL findRet;
DWORD error;
unsigned int index, dirNameLen, subdirNameLen;
unsigned int regExpLen, wstrLen, size, dirExpLen;
VtFileNameList *theList = (VtFileNameList *)0;
Pointer theArray = (unsigned char *)0;
HANDLE findHandle = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW findData;
LPWSTR wide_str = NULL ;
unsigned char *utf8_str = (unsigned char *)0;
unsigned char *newDirName = (unsigned char *)0;
unsigned char *newDir = (unsigned char *)0;
LPCWSTR regExp = L"\\*" ;
char *dirExp = (char *)0;
char *separator = "\\";
VtFileNameList *newNameList = (VtFileNameList *)0;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_SYSTEM_ERROR (systemError)
VOLT_DECLARE_ERROR_DESC(description)
VOLT_DECLARE_FNCT_LINE (fnctLine)
*nameList = (VtFileNameList *)0;
regExpLen = wcslen (regExp);
dirNameLen = Z2Strlen (directoryName);
do
{
/* directory name is a UTF-8 string. Convert it to
* wide char string first before calling any Win32 function
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltConvertMultiByteToWideAlloc (
directoryName, CP_UTF8, &wide_str, libCtx);
if (status != 0)
break;
wstrLen = wcslen (wide_str);
dirExpLen = (wstrLen + regExpLen + 1) * sizeof (WCHAR) ;
/* Append a \* to the directory name
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
dirExp = (char *)Z2Malloc (dirExpLen , 0);
if (dirExp == (char *)0 )
break;
Z2Memcpy (dirExp, wide_str, wstrLen * sizeof(WCHAR) );
Z2Memcpy (
dirExp + wstrLen * sizeof(WCHAR), regExp ,
(regExpLen + 1) * sizeof (WCHAR));
/* The call to FindFirstFile should find the directory.
* If no directory it is still a success.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_FILE_INVALID_HANDLE ;
findHandle = FindFirstFileW ((LPWSTR)dirExp, &findData);
if (findHandle == INVALID_HANDLE_VALUE)
{
error = GetLastError ();
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_SYSTEM | VT_ERROR_TYPE_PRIMARY)
VOLT_SET_SYSTEM_ERROR (systemError, error)
VOLT_SET_ERROR_DESC(description, "System function FindFirstFileW failed")
break;
}
/* Build the list struct, the shell.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
theList = (VtFileNameList *)Z2Malloc (sizeof (VtFileNameList), 0);
if (theList == (VtFileNameList *)0)
break;
Z2Memset (theList, 0, sizeof (VtFileNameList));
/* Build an initial array of size 5.
*/
VOLT_SET_FNCT_LINE (fnctLine)
theArray = (unsigned char *)Z2Malloc (5 * sizeof (char *), 0);
if (theArray == (unsigned char *)0)
break;
Z2Memset (theArray, 0, 5 * sizeof (char *));
theList->nameList = (char **)theArray;
theList->listSize = 5;
status = 0;
} while (0);
/* if status is non-zero it means we can't proceed further.
* return the appropriate value to the caller. We should
* also free any memory and close any handles we allocated.
*/
if (status != 0)
{
if (wide_str != NULL)
Z2Free (wide_str);
if (dirExp != (char *)0)
Z2Free (dirExp);
if (theList != (VtFileNameList *)0)
Z2Free (theList);
if (theArray != (unsigned char *)0 )
Z2Free (theArray);
if (findHandle != INVALID_HANDLE_VALUE)
FindClose (findHandle);
/* if we couldn't find a directory it means a success
*/
if (status == VT_ERROR_FILE_INVALID_HANDLE)
return 0;
return status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -