0

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?

missing
  • 1
  • 1
  • Are you writing in C or in C++? Choose *one* to tag, unless you're asking for a comparison between the two. – John Bollinger Apr 25 '20 at 19:31
  • Well, I removed C + + tag. But it doesn't matter whether C or C + +. – missing Apr 25 '20 at 19:40
  • 1
    It *does* matter, at least if you care to receive an answer that you can actually use. Answers for C may differ significantly from answers for C++. Moreover, if your existing code is in C, then it would be most unwise to (try to) compile it with a C++ compiler. The two are distinct languages. – John Bollinger Apr 25 '20 at 19:43
  • @JohnBollinger Is trying really unwise? That makes it sound like it would compile but then do something bad at runtime. Unless you're doing something really weird like changing behavior based on `sizeof('x')`, wouldn't it either fail to compile or work just fine? – Joseph Sible-Reinstate Monica Apr 26 '20 at 01:18
  • @JosephSible-ReinstateMonica, it *could* compile successfully and do something bad at runtime. There are expressions and syntax that are valid in both languages but have different meaning to each. There is at least on Q&A on that topic around SO somewhere. – John Bollinger Apr 26 '20 at 12:21
  • @JohnBollinger I assume you mean [Can code that is valid in both C and C++ produce different behavior when compiled in each language?](https://stackoverflow.com/q/12887700/7509065) Most of those seem unlikely in "real" code. – Joseph Sible-Reinstate Monica Apr 26 '20 at 19:47

0 Answers0