From 3f99ce7af5b93ca8abe98287cd4c208a2dcca46b Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 1 Aug 2005 08:47:54 +0000 Subject: [PATCH] New plugin file format --- compiler/amxxpc/Binary.cpp | 143 ++++++++++++++++++++++++++++++++++ compiler/amxxpc/Binary.h | 53 +++++++++++++ compiler/amxxpc/amxxpc.cpp | 118 ++++++++++++++++++---------- compiler/amxxpc/amxxpc.h | 19 +++++ compiler/amxxpc/amxxpc.vcproj | 7 ++ 5 files changed, 301 insertions(+), 39 deletions(-) create mode 100755 compiler/amxxpc/Binary.cpp create mode 100755 compiler/amxxpc/Binary.h diff --git a/compiler/amxxpc/Binary.cpp b/compiler/amxxpc/Binary.cpp new file mode 100755 index 00000000..9d029dca --- /dev/null +++ b/compiler/amxxpc/Binary.cpp @@ -0,0 +1,143 @@ +#include "Binary.h" + +BinaryWriter::BinaryWriter(FILE *fp) +{ + m_Fp = fp; +} + +bool BinaryWriter::WriteAddr(void *buffer, size_t size) +{ + if (fwrite(buffer, size, 1, m_Fp) != 1) + return false; + + return true; +} + +void BinaryWriter::WriteUInt32(uint32_t num) +{ + if ( !WriteAddr(&num, sizeof(uint32_t)) ) + throw -1; +} + +void BinaryWriter::WriteInt32(int32_t num) +{ + if ( !WriteAddr(&num, sizeof(int32_t)) ) + throw -1; +} + +void BinaryWriter::WriteUInt16(uint16_t num) +{ + if ( !WriteAddr(&num, sizeof(uint16_t)) ) + throw -1; +} + +void BinaryWriter::WriteInt16(int16_t num) +{ + if ( !WriteAddr(&num, sizeof(int16_t)) ) + throw -1; +} + +void BinaryWriter::WriteUInt8(uint8_t num) +{ + if ( !WriteAddr(&num, sizeof(uint8_t)) ) + throw -1; +} + +void BinaryWriter::WriteInt8(int8_t num) +{ + if ( !WriteAddr(&num, sizeof(int8_t)) ) + throw -1; +} + +void BinaryWriter::WriteChars(const char buffer[], size_t chars) +{ + if (!chars) + return; + + if (fwrite(buffer, sizeof(char), chars, m_Fp) != chars) + throw -1; +} + +BinaryReader::BinaryReader(FILE *fp) +{ + m_Fp = fp; +} + +bool BinaryReader::ReadAddr(void *buffer, size_t size) +{ + if (fread(buffer, size, 1, m_Fp) != 1) + return false; + + return true; +} + +uint32_t BinaryReader::ReadUInt32() +{ + uint32_t num; + + if ( !ReadAddr(&num, sizeof(uint32_t)) ) + throw -1; + + return num; +} + +int32_t BinaryReader::ReadInt32() +{ + int32_t num; + + if ( !ReadAddr(&num, sizeof(int32_t)) ) + throw -1; + + return num; +} + +uint16_t BinaryReader::ReadUInt16() +{ + uint16_t num; + + if ( !ReadAddr(&num, sizeof(uint16_t)) ) + throw -1; + + return num; +} + +int16_t BinaryReader::ReadInt16() +{ + int16_t num; + + if ( !ReadAddr(&num, sizeof(int16_t)) ) + throw -1; + + return num; +} + +uint8_t BinaryReader::ReadUInt8() +{ + uint8_t num; + + if ( !ReadAddr(&num, sizeof(uint8_t)) ) + throw -1; + + return num; +} + +int8_t BinaryReader::ReadInt8() +{ + int8_t num; + + if ( !ReadAddr(&num, sizeof(int8_t)) ) + throw -1; + + return num; +} + +char *BinaryReader::ReadChars(char buffer[], size_t chars) +{ + if (!chars) + return buffer; + + if (fread(buffer, sizeof(char), chars, m_Fp) != chars) + throw -1; + + return buffer; +} diff --git a/compiler/amxxpc/Binary.h b/compiler/amxxpc/Binary.h new file mode 100755 index 00000000..9270bd86 --- /dev/null +++ b/compiler/amxxpc/Binary.h @@ -0,0 +1,53 @@ +#ifndef _INCLUDE_BINARY_H +#define _INCLUDE_BINARY_H + +#include +#include "amx.h" + +#ifdef WIN32 + typedef __int8 int8_t; + typedef unsigned __int8 uint8_t; + typedef __int16 int16_t; + typedef unsigned __int16 uint16_t; +#else +#include +#endif + +class BinaryReader +{ +public: + BinaryReader(FILE *fp); + //~BinaryReader(); +public: + uint32_t ReadUInt32(); + int32_t ReadInt32(); + uint16_t ReadUInt16(); + int16_t ReadInt16(); + uint8_t ReadUInt8(); + int8_t ReadInt8(); + char *ReadChars(char buffer[], size_t chars); +private: + bool ReadAddr(void *buffer, size_t size); +private: + FILE *m_Fp; +}; + +class BinaryWriter +{ +public: + BinaryWriter(FILE *fp); +public: + void WriteUInt32(uint32_t num); + void WriteInt32(int32_t num); + void WriteUInt16(uint16_t num); + void WriteInt16(int16_t num); + void WriteUInt8(uint8_t num); + void WriteInt8(int8_t num); + void WriteChars(const char buffer[], size_t chars); +private: + bool WriteAddr(void *buffer, size_t size); +private: + FILE *m_Fp; +}; + +#endif //_INCLUDE_BINARY_H \ No newline at end of file diff --git a/compiler/amxxpc/amxxpc.cpp b/compiler/amxxpc/amxxpc.cpp index 3a1c60f8..bd622ba8 100755 --- a/compiler/amxxpc/amxxpc.cpp +++ b/compiler/amxxpc/amxxpc.cpp @@ -10,8 +10,14 @@ #include "amx.h" #include "amxdbg.h" #include "amxxpc.h" +#include "Binary.h" + +static PRINTF pc_printf = NULL; void ReadFileIntoPl(abl *pl, FILE *fp); +bool CompressPl(abl *pl); +void Pl2Bh(abl *pl, BinPlugin *bh); +void WriteBh(BinaryWriter *bw, BinPlugin *bh); int main(int argc, char **argv) { @@ -39,7 +45,7 @@ int main(int argc, char **argv) } COMPILER sc32 = (COMPILER)dlsym(lib, "Compile32"); - PRINTF pc_printf = (PRINTF)dlsym(lib, "pc_printf"); + pc_printf = (PRINTF)dlsym(lib, "pc_printf"); if (!sc32 || !pc_printf) { @@ -143,27 +149,9 @@ int main(int argc, char **argv) // COMPRSSION ///////////// - int err; + CompressPl(&pl32); - pl32.cmpsize = compressBound(pl32.size); - pl32.cmp = new char[pl32.cmpsize]; - err = compress((Bytef *)pl32.cmp, (uLongf *)&(pl32.cmpsize), (const Bytef*)pl32.data, pl32.size); - - if (err != Z_OK) - { - pc_printf("internal error - compression failed on first pass: %d\n", err); - exit(0); - } - - pl64.cmpsize = compressBound(pl64.size); - pl64.cmp = new char[pl64.cmpsize]; - err = compress((Bytef *)pl64.cmp, (uLongf *)&(pl64.cmpsize), (const Bytef*)pl64.data, pl64.size); - - if (err != Z_OK) - { - pc_printf("internal error - compression failed on second pass: %d\n", err); - exit(0); - } + CompressPl(&pl64); char *newfile = new char[strlen(file)+3]; strcpy(newfile, file); @@ -177,25 +165,41 @@ int main(int argc, char **argv) exit(0); } - //magic + archn - int hdrsize = sizeof(long) + sizeof(char); - int entry = sizeof(long) + sizeof(long) + sizeof(char); - int offset1 = hdrsize + (entry * 2); - int offset2 = offset1 + pl32.cmpsize; - - int magic = MAGIC_HEADER; - fwrite((void *)&magic, sizeof(int), 1, fp); - char n = 2; - fwrite((void *)&n, sizeof(char), 1, fp); + BinPlugin bh32, bh64; - fwrite((void *)&(pl32.cellsize), sizeof(char), 1, fp); - fwrite((void *)&(pl32.stp), sizeof(long), 1, fp); - fwrite((void *)&(offset1), sizeof(long), 1, fp); - fwrite((void *)&(pl64.cellsize), sizeof(char), 1, fp); - fwrite((void *)&(pl64.stp), sizeof(long), 1, fp); - fwrite((void *)&(offset2), sizeof(long), 1, fp); - fwrite(pl32.cmp, sizeof(char), pl32.cmpsize, fp); - fwrite(pl64.cmp, sizeof(char), pl64.cmpsize, fp); + Pl2Bh(&pl32, &bh32); + Pl2Bh(&pl64, &bh64); + + try + { + + BinaryWriter bw(fp); + + bw.WriteUInt32(MAGIC_HEADER2); + bw.WriteUInt16(MAGIC_VERSION); + bw.WriteUInt8(2); + + //base header + int baseaddr = sizeof(int32_t) + sizeof(int16_t) + sizeof(int8_t); + //entry is 4 ints and a byte + int entrysize = (sizeof(int32_t) * 4) + sizeof(int8_t); + //extend this by the two entries we have + baseaddr += entrysize * 2; + + bh32.offs = baseaddr; + bh64.offs = bh32.offs + bh32.disksize; + + WriteBh(&bw, &bh32); + WriteBh(&bw, &bh64); + bw.WriteChars(pl32.cmp, pl32.cmpsize); + bw.WriteChars(pl64.cmp, pl64.cmpsize); + } catch (...) { + fclose(fp); + unlink(file); + pc_printf("Error, failed to write binary\n"); + dlclose(lib); + exit(0); + } fclose(fp); @@ -208,6 +212,42 @@ int main(int argc, char **argv) exit(0); } +void WriteBh(BinaryWriter *bw, BinPlugin *bh) +{ + bw->WriteUInt8(bh->cellsize); + bw->WriteUInt32(bh->disksize); + bw->WriteUInt32(bh->imagesize); + bw->WriteUInt32(bh->memsize); + bw->WriteUInt32(bh->offs); +} + +void Pl2Bh(abl *pl, BinPlugin *bh) +{ + bh->cellsize = pl->cellsize; + bh->disksize = pl->cmpsize; + bh->imagesize = pl->size; + bh->memsize = pl->stp; +} + +bool CompressPl(abl *pl) +{ + pl->cmpsize = compressBound(pl->size); + pl->cmp = new char[pl->cmpsize]; + + int err = compress((Bytef *)(pl->cmp), (uLongf *)&(pl->cmpsize), (const Bytef *)(pl->data), pl->size); + + delete [] pl->data; + pl->data = NULL; + + if (err != Z_OK) + { + pc_printf("internal error - compression failed on first pass: %d\n", err); + exit(0); + } + + return true; +} + //As of Small 3.0, there's extra debug info in the file we need to get out. //Sadly this is placed somewhere really inconvenient and I'm mad. void ReadFileIntoPl(abl *pl, FILE *fp) diff --git a/compiler/amxxpc/amxxpc.h b/compiler/amxxpc/amxxpc.h index cec83117..c1e33d73 100755 --- a/compiler/amxxpc/amxxpc.h +++ b/compiler/amxxpc/amxxpc.h @@ -4,6 +4,8 @@ #define VERSION_STRING "1.50-300" #define VERSION 03000 #define MAGIC_HEADER 0x414D5842 +#define MAGIC_HEADER2 0x414D5858 +#define MAGIC_VERSION 0x0300 #ifdef __linux__ # include @@ -23,6 +25,7 @@ #endif #include "zlib.h" +#include "Binary.h" typedef int (*COMPILER)(int argc, char **argv); typedef int (*PRINTF)(const char *message, ...); @@ -48,4 +51,20 @@ struct abl char *cmp; }; +struct BinHeader +{ + int32_t magic; + int16_t version; + int8_t plugins; +}; + +struct BinPlugin +{ + int8_t cellsize; //cell size + int32_t imagesize; //uncompressed image size + int32_t disksize; //compressed image size + int32_t memsize; //memory image size + int32_t offs; //file offset +}; + #endif //_AMXXSC_INCLUDE_H diff --git a/compiler/amxxpc/amxxpc.vcproj b/compiler/amxxpc/amxxpc.vcproj index e9a590bb..c35d82bd 100755 --- a/compiler/amxxpc/amxxpc.vcproj +++ b/compiler/amxxpc/amxxpc.vcproj @@ -69,6 +69,7 @@ Name="VCCLCompilerTool" PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" RuntimeLibrary="4" + StructMemberAlignment="0" UsePrecompiledHeader="0" WarningLevel="3" Detect64BitPortabilityProblems="FALSE" @@ -120,6 +121,9 @@ + + + +