I write BYTE* to bytea field in postgres with function, which returns bytea:
...
PG_RETURN_BYTEA_P(pbBuffer);
These bytes look in file like:
Later Im trying read this bytea field and write to other file with other function like this:
bytea* data = PG_GETARG_BYTEA_P(0);
char filePath[] = "C:\\pg\\11.txt";
HANDLE h = CreateFile(filePath, FILE_WRITE_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dw = sizeof(data);
WriteFile(h, data, dw, &dw, NULL);
CloseHandle(h);
In file It looks like:
What should I do to get the same result as in begin?
first function works:
FILE* log = AllocateFile("C:\\pg\\log.txt", PG_BINARY_A);
char* dataToEncrypt = PG_GETARG_CSTRING(0);
FILE* tempFile = AllocateFile("C:\\pg\\openText.txt", PG_BINARY_A);
fprintf(tempFile, "%s", dataToEncrypt);
FreeFile(tempFile);
HANDLE hSourceFile = INVALID_HANDLE_VALUE;
HANDLE hDestinationFile = INVALID_HANDLE_VALUE;
HCRYPTPROV hCryptProv = NULL;
HCRYPTKEY hKey = NULL;
HCRYPTKEY hXchgKey = NULL;
HCRYPTHASH hHash = NULL;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
PBYTE pbBuffer = NULL;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
bool fEOF = FALSE;
char pszPassword[] = "key";
char s[] = "C:\\pg\\openText.txt";
char d[] = "C:\\pg\\cryptoText.txt";
hSourceFile = CreateFile(s, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hSourceFile)
{
fprintf(log, "%u %s\n", GetLastError(), "CreateFile s");
goto Exit_MyEncryptFile;
}
hDestinationFile = CreateFile(d, FILE_WRITE_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hDestinationFile)
{
fprintf(log, "%u %s\n", GetLastError(), "CreateFile d");
goto Exit_MyEncryptFile;
}
if (!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
fprintf(log, "%u %s\n", GetLastError(), "CryptAcquireContext");
goto Exit_MyEncryptFile;
}
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
{
fprintf(log, "%u %s\n", GetLastError(), "CryptCreateHash");
goto Exit_MyEncryptFile;
}
if (!CryptHashData(hHash, (BYTE*)pszPassword, lstrlen(pszPassword), 0))
{
fprintf(log, "%u %s\n", GetLastError(), "CryptHashData");
goto Exit_MyEncryptFile;
}
if (!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
{
fprintf(log, "%u %s\n", GetLastError(), "CryptDeriveKey");
goto Exit_MyEncryptFile;
}
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
if (ENCRYPT_BLOCK_SIZE > 1)
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
else
dwBufferLen = dwBlockLen;
pbBuffer = (BYTE*)malloc(dwBufferLen);
do
{
if (!ReadFile(hSourceFile, pbBuffer, dwBlockLen, &dwCount, NULL))
{
fprintf(log, "%u %s\n", GetLastError(), "ReadFile");
goto Exit_MyEncryptFile;
}
if (dwCount < dwBlockLen)
fEOF = TRUE;
if (!CryptEncrypt(hKey, NULL, fEOF, 0, pbBuffer, &dwCount, dwBufferLen))
{
fprintf(log, "%u %s\n", GetLastError(), "CryptEncrypt");
goto Exit_MyEncryptFile;
}
if (!WriteFile(hDestinationFile, pbBuffer, dwCount, &dwCount, NULL))
{
fprintf(log, "%u %s\n", GetLastError(), "WriteFile");
goto Exit_MyEncryptFile;
}
} while (!fEOF);
DWORD dw = sizeof(data);This cannot possibly be correct;sizeof(data)is the size of the pointer variable, not the size of thebyteadatum you retrieved from the database. I don't know what the correct way to get the length of the datum is (which is why this is not an answer) but I'm pretty sure this is the cause of your problem.