New plugin file format

This commit is contained in:
David Anderson 2005-08-01 08:47:54 +00:00
parent 4451691aa2
commit 3f99ce7af5
5 changed files with 301 additions and 39 deletions

143
compiler/amxxpc/Binary.cpp Executable file
View File

@ -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;
}

53
compiler/amxxpc/Binary.h Executable file
View File

@ -0,0 +1,53 @@
#ifndef _INCLUDE_BINARY_H
#define _INCLUDE_BINARY_H
#include <stdio.h>
#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 <stdint.h>
#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

View File

@ -10,8 +10,14 @@
#include "amx.h" #include "amx.h"
#include "amxdbg.h" #include "amxdbg.h"
#include "amxxpc.h" #include "amxxpc.h"
#include "Binary.h"
static PRINTF pc_printf = NULL;
void ReadFileIntoPl(abl *pl, FILE *fp); 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) int main(int argc, char **argv)
{ {
@ -39,7 +45,7 @@ int main(int argc, char **argv)
} }
COMPILER sc32 = (COMPILER)dlsym(lib, "Compile32"); 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) if (!sc32 || !pc_printf)
{ {
@ -143,27 +149,9 @@ int main(int argc, char **argv)
// COMPRSSION // COMPRSSION
///////////// /////////////
int err; CompressPl(&pl32);
pl32.cmpsize = compressBound(pl32.size); CompressPl(&pl64);
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);
}
char *newfile = new char[strlen(file)+3]; char *newfile = new char[strlen(file)+3];
strcpy(newfile, file); strcpy(newfile, file);
@ -177,25 +165,41 @@ int main(int argc, char **argv)
exit(0); exit(0);
} }
//magic + archn BinPlugin bh32, bh64;
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);
fwrite((void *)&(pl32.cellsize), sizeof(char), 1, fp); Pl2Bh(&pl32, &bh32);
fwrite((void *)&(pl32.stp), sizeof(long), 1, fp); Pl2Bh(&pl64, &bh64);
fwrite((void *)&(offset1), sizeof(long), 1, fp);
fwrite((void *)&(pl64.cellsize), sizeof(char), 1, fp); try
fwrite((void *)&(pl64.stp), sizeof(long), 1, fp); {
fwrite((void *)&(offset2), sizeof(long), 1, fp);
fwrite(pl32.cmp, sizeof(char), pl32.cmpsize, fp); BinaryWriter bw(fp);
fwrite(pl64.cmp, sizeof(char), pl64.cmpsize, 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); fclose(fp);
@ -208,6 +212,42 @@ int main(int argc, char **argv)
exit(0); 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. //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. //Sadly this is placed somewhere really inconvenient and I'm mad.
void ReadFileIntoPl(abl *pl, FILE *fp) void ReadFileIntoPl(abl *pl, FILE *fp)

View File

@ -4,6 +4,8 @@
#define VERSION_STRING "1.50-300" #define VERSION_STRING "1.50-300"
#define VERSION 03000 #define VERSION 03000
#define MAGIC_HEADER 0x414D5842 #define MAGIC_HEADER 0x414D5842
#define MAGIC_HEADER2 0x414D5858
#define MAGIC_VERSION 0x0300
#ifdef __linux__ #ifdef __linux__
# include <dlfcn.h> # include <dlfcn.h>
@ -23,6 +25,7 @@
#endif #endif
#include "zlib.h" #include "zlib.h"
#include "Binary.h"
typedef int (*COMPILER)(int argc, char **argv); typedef int (*COMPILER)(int argc, char **argv);
typedef int (*PRINTF)(const char *message, ...); typedef int (*PRINTF)(const char *message, ...);
@ -48,4 +51,20 @@ struct abl
char *cmp; 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 #endif //_AMXXSC_INCLUDE_H

View File

@ -69,6 +69,7 @@
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="4" RuntimeLibrary="4"
StructMemberAlignment="0"
UsePrecompiledHeader="0" UsePrecompiledHeader="0"
WarningLevel="3" WarningLevel="3"
Detect64BitPortabilityProblems="FALSE" Detect64BitPortabilityProblems="FALSE"
@ -120,6 +121,9 @@
<File <File
RelativePath=".\amxxpc.cpp"> RelativePath=".\amxxpc.cpp">
</File> </File>
<File
RelativePath=".\Binary.cpp">
</File>
</Filter> </Filter>
<Filter <Filter
Name="Header Files" Name="Header Files"
@ -134,6 +138,9 @@
<File <File
RelativePath=".\amxxpc.h"> RelativePath=".\amxxpc.h">
</File> </File>
<File
RelativePath=".\Binary.h">
</File>
<File <File
RelativePath=".\osdefs.h"> RelativePath=".\osdefs.h">
</File> </File>