Amxmodx uses such a macro to read data:
#define DATAREAD(addr, itemsize, itemcount) \
if (fread((addr), (itemsize), (itemcount), (m_pFile)) != (itemcount)) \
{ \
if (feof(m_pFile)) \
m_Status = Err_FileInvalid; \
else \
m_Status = Err_FileRead; \
fclose(m_pFile); \
m_pFile = NULL; \
return; \
}
Read data like this:
mint32_t magic;
DATAREAD(&magic, sizeof(magic), 1);
m_OldFile = false;
if (magic == 0x524C4542)
{
//we have an invalid, old, RLEB file
m_Status = Err_OldFile;
fclose(m_pFile);
m_pFile = NULL;
return;
}
else if (magic == MAGIC_HEADER2)
{
DATAREAD(&m_Bh.version, sizeof(int16_t), 1);
if (m_Bh.version > MAGIC_VERSION)
{
m_Status = Err_OldFile;
fclose(m_pFile);
m_pFile = NULL;
return;
}
m_AmxxFile = true;
DATAREAD(&m_Bh.numPlugins, sizeof(mint8_t), 1);
m_Bh.plugins = new PluginEntry[m_Bh.numPlugins];
PluginEntry *pe;
m_SectionHdrOffset = 0;
m_Entry = -1;
for (mint8_t i = 0; i < m_Bh.numPlugins; i++)
{
pe = &(m_Bh.plugins[i]);
DATAREAD(&pe->cellsize, sizeof(mint8_t), 1);
DATAREAD(&pe->disksize, sizeof(int32_t), 1);
DATAREAD(&pe->imagesize, sizeof(int32_t), 1);
DATAREAD(&pe->memsize, sizeof(int32_t), 1);
DATAREAD(&pe->offs, sizeof(int32_t), 1);
}
For complete code, you can get reference from this link https://github.com/alliedmodders/amxmodx/blob/master/amxmodx/amxxfile.cpp
I want to encrypt an amxx file. I use my own algorithm to decrypt the file, but I don't know how to make amxmodx read the encrypted file correctly.
My decryptor uses a function like DecryptData(bufferin, bufferout, key)
to decrypt this file completely into bufferout
. How can I use fread() to read data from bufferout
without outputting the decrypted file?