#pragma once

#ifndef INLINE_EXECUTE_H
#define INLINE_EXECUTE_H

#include "analizador.h"
#include <windows.h>

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__x86_64__) || defined(_WIN64)
#define PREPENDSYMBOLVALUE "__imp_"
#else
#define PREPENDSYMBOLVALUE "__imp__"
#endif

#define STR_EQUALS(str, substr) (strncmp(str, substr, strlen(substr)) == 0)

// COFF relocation types (from winnt.h)
#define IMAGE_REL_AMD64_ADDR64      0x0001
#define IMAGE_REL_AMD64_ADDR32NB    0x0003
#define IMAGE_REL_AMD64_REL32       0x0004
#define IMAGE_FILE_MACHINE_AMD64    0x8664

// COFF File Header
typedef struct coff_file_header {
    UINT16 Machine;
    UINT16 NumberOfSections;
    UINT32 TimeDateStamp;
    UINT32 PointerToSymbolTable;
    UINT32 NumberOfSymbols;
    UINT16 SizeOfOptionalHeader;
    UINT16 Characteristics;
} FileHeader_t;

#pragma pack(push,1)

// COFF Section Table
typedef struct coff_sections_table {
    char Name[8];
    UINT32 VirtualSize;
    UINT32 VirtualAddress;
    UINT32 SizeOfRawData;
    UINT32 PointerToRawData;
    UINT32 PointerToRelocations;
    UINT32 PointerToLineNumbers;
    UINT16 NumberOfRelocations;
    UINT16 NumberOfLinenumbers;
    UINT32 Characteristics;
} Section_t;

// COFF Relocations
typedef struct coff_relocations {
    UINT32 VirtualAddress;
    UINT32 SymbolTableIndex;
    UINT16 Type;
} Relocation_t;

// COFF Symbols Table
typedef struct coff_symbols_table {
    union {
        char Name[8];
        UINT32 value[2];
    } first;
    UINT32 Value;
    UINT16 SectionNumber;
    UINT16 Type;
    UINT8 StorageClass;
    UINT8 NumberOfAuxSymbols;
} Symbol_t;

#pragma pack(pop)

// COFF structure
typedef struct COFF {
    char* FileBase;
    FileHeader_t* FileHeader;
    Relocation_t* Relocation;
    Symbol_t* SymbolTable;
    void* RawTextData;
    char* RelocationsTextPTR;
    int RelocationsCount;
    int FunctionMappingCount;
} COFF_t;

// BOF Upload structure
typedef struct _BOF_UPLOAD {
    CHAR fileUuid[37];          // File UUID (36 + 1 for null terminator)
    UINT32 totalChunks;          // Total number of chunks
    UINT32 currentChunk;        // Current chunk number
    SIZE_T size;                // Size of the file
    PVOID buffer;               // Pointer to BOF buffer data
} BOF_UPLOAD, *PBOF_UPLOAD;

// Function declarations
VOID InlineExecuteHandler(PAnalizador argumentos);
BOOL RunCOFF(char* FileData, DWORD* DataSize, char* EntryName, char* argumentdata, unsigned long argumentsize);
DWORD GetFileBytesFromMythic(PCHAR taskUuid, PCHAR fileId, PBOF_UPLOAD bof);

#ifdef __cplusplus
}
#endif

#endif // INLINE_EXECUTE_H

