/* bastard api -- these should be called from the macro language and the prog */

/* DB Interface
   ======================================================================== */
/* These are located in db.c, but are included here for the convenience of
 * the script writers who shouldn't really be using them, but probably will
 * regardless */
/* ---- Low-level DB access ---- */
/*   These expect *dest to be pre-allocated */
int DBIndexFirst(int index, void *dest);
int DBIndexNext(int index, void *dest);
int DBIndexPrev(int index, void *dest);
int DBIndexLast(int index, void *dest);
int DBIndexFind(int index, void *value, void *dest);
int DBTableFirst(int table, void *dest);
int DBTableNext(int table, void *dest);
int DBTablePrev(int table, void *dest);
int DBTableLast(int table, void *dest);
int DBRecordInsert(int table, void *src);
int DBRecordUpdate(int index, void *value, void *src);
int DBRecordDelete(int index, void *value);
/* ---- generic 'object' access ---- */
/*   These calloc a struct and fill it; it is up to the caller to free */
struct address * GetAddressObject(long rva);
struct code * GetCodeObject(long rva);
struct section * GetSectionObject(char *name);
struct xref * GetXrefObject(int id);
struct int_code * GetIntCodeObject(int id);
struct fin_code * GetFinCodeObject(int id);
struct export * GetExportObject(long rva);
struct import * GetImportObject(long rva);
struct string * GetStringObject(long rva);
struct name * GetNameObject(long rva);
struct comment * GetCommentObject(int id);
struct function * GetFunctionObject(int id);
struct func_param * GetFuncParamObject(int id);
struct func_local * GetFuncLocalObject(int id);
struct f_inline * GetFInlineObject(int id);
struct inline_type * GetInlineTypeObject(int id);
struct structure * GetStructureTypeObject(int id);
struct struct_member * GetStructMemberObject(int id);
struct data_type * GetDataTypeObject(int id);
struct constant * GetConstantObject(int id);
struct seer_macro * GetSeerMacroObject(int id);
/* ---- Low level Disassembler access ---- */
int DisPrologue(struct code **c);
int DisEpilogue(struct code **c);
int DisByteOrder();
int DisAddrSize();
int DisByteSize();
int DisWordSize();
int DisDwordSize();
char* DisGetSP();
char* DisGetIP();



/* Loading
   ======================================================================== */
/*  LoadTarget : Open target file for disassenbly
 * 	Return Value:	0 -- Success 		-1 -- Failure
 *  	Arguments:		char *target -- full name/path of file to open */
int LoadTarget( char *target );
/* LoadBDB : Open previously disassembled file 
 * 	Return Value:	0 -- Success 		-1 -- Failure
 * 	Arguments:		char *dbname -- full name/path of .bdb file */
int LoadBDB( char *dbname );
/* SetFileFormat : Specify Format of Target [e.g. "ELF"]
 * 	Return Value:	0 -- Success 		-1 -- Failure
 * 	Arguments:		char *file_format -- name of format
 * 	Notes:		Valid formats can be listed with GetValidFormats(); */
int SetFileFormat( char *file_format );
/* SetFileArch : Specify Processor Target was compiled for [e.g. "i386"]
 * 	Return Value:	0 -- Success 		-1 -- Failure
 * 	Arguments:		char *file_arch -- name of architecture
 * 	Notes:		Valid architectures are listed with GetValidArchs(); */
int SetFileArch( char *file_arch );

/* Saving
   ======================================================================== */
/* SaveDB : Save current DB as .bdb file
 * 	Notes : Uses ./$target.bdb for filename */
void SaveDB( );
/* SaveDBBak : Save current DB to a unique 'temp' filename
 * 	Notes: Uses ./$target.$timestamp.bdb as filename */
void SaveDBBak( );
/* CloseDB : Close the current DB and target [i.e. before loading a new one] */
void CloseDB( );
/* SaveDBAs : Save DB as the specified filename
 * 	Arguments:		char *filename -- name of file to save DB as */
void SaveDBAs( char *filename );
/* SaveAsASM : Save a disassembled listing of the file
 * 	Return Value :	0 -- Success 		-1 -- Failure
 * 	Arguments :		char *filename -- name of file to save listing as 
 * 	Notes : 		Will change to SaveAsLST and make new routine for 
 * 					GAS and NASM asm files */
int SaveAsASM( char *filename );
/* SaveAsDump : Save a  hex dump of the target
 * 	Return Value :	0 -- Success 		-1 -- Failure
 * 	Arguments :		char *filename -- name of file to save dump as  */
int SaveAsDump( char *filename );
/* SaveAsDiff : Create a binary diff [patch] between the original target and
 *              the current DB image of the target. Useless if you have not
 *              patched the target inside the disassembler.
 * 	Return Value :	0 -- Success 		-1 -- Failure
 * 	Arguments :		char *filename -- name of file to save diff as  */
int SaveAsDiff( char *filename );
/* SaveAsBinary : Create a new binary image of the target, using the DB image
 * 					of the target. Used to 'recreate' a missing or patched 
 * 					target.
 * 	Return Value :	0 -- Success 		-1 -- Failure
 * 	Arguments :		char *filename -- name of file to save target as  */
int SaveAsBinary( char *filename );

/* Sections
   ======================================================================== */
/*  CreateSection : 
 * 	Return Value:	
 *  	Arguments:		 */
int CreateSection( char *name, long start, int size, long pa, int type );
/*  DeleteSection : 
 * 	Return Value:	
 *  	Arguments:		 */
    /* FIX: This routine must check that start/end are less than other */
int DeleteSection( char *name );
/*  GetSectionStart : 
 * 	Return Value:	
 *  	Arguments:		 */
 	/* FIX: should increase the end addr of the prev section thru curr->end */
long GetSectionStart( char *name );
/*  GetSectionEnd : 
 * 	Return Value:	
 *  	Arguments:		 */
long GetSectionEnd( char *name );
/*  GetSectionName : 
 * 	Return Value:	
 *  	Arguments:		 */
int  GetSectionName( long rva, char* buf );
/*  GetSectionFlags : 
 * 	Return Value:	
 *  	Arguments:		 */
int  GetSectionFlags( char *name );
/*  SetSectionStart : 
 * 	Return Value:	
 *  	Arguments:		 */
int  SetSectionStart( char *name, long start );
/*  SetSectionEnd : 
 * 	Return Value:	
 *  	Arguments:		 */
int  SetSectionEnd( char *name, long end );
/*  SetSectionName : 
 * 	Return Value:	
 *  	Arguments:		 */
int  SetSectionName( char *name, char *new_name );
/*  SetSectionFlags : 
 * 	Return Value:	
 *  	Arguments:		 */
int  SetSectionFlags( char *name, int flags );

/* Disassembly
   ======================================================================== */
/*  CreateHeader : 
 * 	Return Value:	
 *  	Arguments:		 */
int CreateHeader( char *header );
/*  PrintHeader : 
 * 	Return Value:	
 *  	Arguments:		 */
void PrintHeader( );
/*  InitDisassembler : 
 * 	Return Value:	
 *  	Arguments:		 */
int InitDisassembler( char *disasm );
/*  DisassembleFull : 
 * 	Return Value:	
 *  	Arguments:		 */
int DisassembleFull( );
/*  DisassembleDumb : 
 * 	Return Value:	
 *  	Arguments:		 */
int DisassembleDumb( );
/*  DisassembleSection : 
 * 	Return Value:	
 *  	Arguments:		 */
int DisassembleSection( char *name );
/*  DisassembleRange : 
 * 	Return Value:	
 *  	Arguments:		 */
int DisassembleRange( long rva, int size );
/*  DisassembleForward : 
 * 	Return Value:	
 *  	Arguments:		 */
int DisassembleForward( long rva );
/*  DisassemblePass : 
 * 	Return Value:	
 *  	Arguments:		 */
int DisassemblePass( int pass );
/*  DisassembleAllPasses : 
 * 	Return Value:	
 *  	Arguments:		 */
int DisassembleAllPasses();

/* Addresses
   ======================================================================== */
/*  AddrExists : 
 * 	Return Value:	
 *  	Arguments:		 */
int AddrExists( long addr );
/*  FindClosestAddr : 
 * 	Return Value:	
 *  	Arguments:		 */
long FindClosestAddr( long addr );
/*  DefineAddr : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineAddr( long rva, int size, long pa, int flags );
/*  UndefineAddr : 
 * 	Return Value:	
 *  	Arguments:		 */
int UndefineAddr( long addr );
/*  PrintAddress : 
 * 	Return Value:	
 *  	Arguments:		 */
int PrintAddress( long rva );
/*  MakeCode : 
 * 	Return Value:	
 *  	Arguments:		 */
int MakeCode( long addr );
/*  MakeData : 
 * 	Return Value:	
 *  	Arguments:		 */
int MakeData( long addr );
/*  GetPA : 
 * 	Return Value:	
 *  	Arguments:		 */
long GetPA( long rva );
/*  GetRVA : 
 * 	Return Value:	
 *  	Arguments:		 */
long GetRVA( long pa );
/*  GetAddrType : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAddrType( long rva );
/*  GetAddrSize : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAddrSize( long rva );
/*  GetFlags : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetFlags( long addr );
/*  GetAddrBytes : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAddrBytes( long rva, char *buf );
/*  GetNextAddr : 
 * 	Return Value:	
 *  	Arguments:		 */
long GetNextAddr( long rva );
/*  GetPrevAddr : 
 * 	Return Value:	
 *  	Arguments:		 */
long GetPrevAddr( long rva );
/*  SetFlags : 
 * 	Return Value:	
 *  	Arguments:		 */
int SetFlags( long rva, int flags );
/*  SetName : 
 * 	Return Value:	
 *  	Arguments:		 */
int SetName( long rva, char* name, int type );
/*  GetName : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetName( long rva, char* buffer );
int GetNameType(long addr);
long GetAddrByName( char *name );
/*  DefineAddrComment : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineAddrComment( long rva, char *buf );
/*  GetAddrComment : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAddrComment( long rva );
/*  GetAddrConstant : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAddrConstant( long rva );
/*  GetAddrStruct : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAddrStruct( long rva );
/*  SetAddrComment : 
 * 	Return Value:	
 *  	Arguments:		 */
int SetAddrComment( long rva, int id );
/*  SetAddrConstant : 
 * 	Return Value:	
 *  	Arguments:		 */
int SetAddrConstant( long rva, int id );
/*  SetAddrStruct : 
 * 	Return Value:	
 *  	Arguments:		 */
int SetAddrStruct( long rva, int id );
 /*
  MakeData( addr, size );
 	Note that MakeData provides a size attr -- it is up to the front-end to
	display this as a byte, word, dword, or array
 GetName( addr, char* );
 	returns size, 0 [no name], or -1 [no addr]
 JumpTo( addr );
 	use this to follow xrefs
 GetBranchCode( addr );
 	Gets next code item from a JMP/JCC
 GetPrevData( addr );
 GetPrevCode( addr );
*/

/* Code
   ======================================================================== */
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
long GetNextCode( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
long GetNextData( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetCodeRep( char *buf, char *fmt, long rva ) ;
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetMnemonic( long rva, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetSrc( long rva, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetDest( long rva, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAux( long rva, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetMnemType( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetSrcType( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetDestType( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetAuxType( long rva );
	/* 
	GetCodeRep( addr );
		Returns a basic [INTEL fmt] representation of the line of code
	GetFlags( code );
*/

/*	Strings
   ======================================================================== */
int GetString( long rva, char *buf, int len );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineString( long rva, char *text );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DumpStrings( ); /* print all strings/addresses to stdout */

/* Imports
   ======================================================================== */
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineImport( long rva, char *lib, char* name, int type );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetImportName( long rva, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetImportLib( long rva, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetImportType( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DumpImports( );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineLibrary( char *name, int v_hi, int v_lo );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DumpLibs( );	/* print all libraries to stdout */

/* Exports
   ======================================================================== */
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineExport( long rva, char *name );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetExport( char *name );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DumpExports( ); /* list all entry points */

/* XRefs
   ======================================================================== */
/*  : 
 * 	Return Value: from_rva or 0 if no matches	
 *  	Arguments:	  rva  -- address to find references to
 *  	          	  type -- type of xref to match [OP_R | OP_W | OP_X] or 0 */
long GetRefTo( long rva, int type ) ; 		//get first
/*  : 
 * 	Return Value:	from_rva or 0 if no more matches	
 *  	Arguments:	  type -- type of xref to match [OP_R | OP_W | OP_X] or 0 */
long GetNextRefTo( int type ) ; //get array of structs; return num
/*  : 
 * 	Return Value: to_rva or 0 if no matches	
 *  	Arguments:	  rva  -- address to find references from
 *  	          	  type -- type of xref to match [OP_R | OP_W | OP_X] or 0 */
long GetRefFrom( long rva, int type ) ;
/*  : 
 * 	Return Value: to_rva or 0 if no more matches	
 *  	Arguments:	  type -- type of xref to match [OP_R | OP_W | OP_X] or 0 */
long GetNextRefFrom( int type ) ;
/*  : 
 * 	Return Value: 1 on success, 0 on error 
 *  	Arguments:		 */
int DefineRef( long from, long to, int type );

/* Structures
   ======================================================================== */
	/*
	CreateStructure( char*, name);
	   Prob isn't necessary -- can be provided by the UI
	ApplyStructure( name, addr );*/

/* Functions
   ======================================================================== */
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineFunc( long rva, char * name, int size );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetFuncName( long rva, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetFuncStart( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetFuncEnd( long rva );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetFuncByName( char *name );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int SetFuncName( long rva, char *name );
int SetFuncSize( long rva, int size );
 /*
 MakeFunction();
 SetFunctionComment();
 GetFunctionComment();
 GetFunctionPrototype();
 	A type of comment
 SetFunctionPrototype();
 MakeInlineFunction( addr, name );
 ApplyInlineFunction( name );
 	consider these to be 'structs' for code...*/

/* Comments
   ======================================================================== */
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int GetComment( int id, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DefineComment( char *buf, int type );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int SetComment( int id, char *buf );
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int DelComment( int id ) ;

 /* Constants
   ======================================================================== */
// *buf is buf[32]
int DefineConstant( char *name, int value);
int GetConstant(int value, char buf[32]);
int GetConstantNext( char buf[32]);
int GetConstantName( unsigned long id, char buf[32]);
unsigned long GetConstantID( char *name);
int DelConstant( unsigned long id);

/* Macros
   ======================================================================== */
/*  : 
 * 	Return Value:	
 *  	Arguments:		 */
int RunScript( char *name );
int RunScriptText( char *script );
int RunCompiledScript(char *name);
int CompileScript(char *name);
int DefineMacro( char *name, char *macro );
int UndefineMacro( char *name );
int RunMacro( char *name );
int RecordMacro( char *name );

/* Misc Search Functions
   ======================================================================== */
int FindBytes( unsigned char *bytes, int len, int **search);
int FindOperand( char *str, int **search);
int FindMnemonic( char *str, int **search);
int FindConstant( long value, int **search);
int FreeSearchHandle( int **search );

/* System Interaction
   ======================================================================== */
 /*
  SetMode( int mode, bool state);
 	e.g. quiet on/off #define MODE_QUIET 0x7ffff or sumthin
 */
void Message( const char *str ) ;
void ErrorMsg( int num, const char *str ) ;
void Quit( ) ;
int Exec( char *args );
int Debug( );
int Core( ); /* core dump the bastard */
int Panic( ); /* cause a kernel panic */
int GetValidArchs( );
int GetValidFormats( char *buf );
int GetEnvFlag( int flag );
int SetEnvFlag( int flag );
void SetTtyFmt( char *fmt );
void SetAsmFmt( char *fmt );
void SetLprFmt( char *fmt );
int SetLastError( int errnum );
int GetLastError( );
char* GetLastErrorString( );
void PrintErrorMsg(int errnum);
void SprintErrorMsg(char *buf, int errnum);

 /* -------------- definitions ----------------- */
 // maybe a FlagToChar() routine?
 /* SECTION types and permissions */
#define SECTION_HDR		0x1000
#define SECTION_CODE	0x1100
#define SECTION_DATA	0x1200
#define SECTION_RSRC	0x1300
#define SECTION_IMPORT	0x1400
#define SECTION_EXPORT	0x1500
#define SECTION_DEBUG	0x1600
#define SECTION_CMT		0x1700
#define SECTION_RELOC	0x1800
#define SECTION_EXECUTE	0x01
#define SECTION_WRITE	0x02
#define SECTION_READ	0x04
 /* ADDRESS types and permissions */
#define ADDR_CODE		0x01
#define ADDR_DATA		0x02
#define ADDR_IMPORT		0x10
#define ADDR_EXPORT		0x20
#define ADDR_SECTION	0x40
#define ADDR_STRUCT		0x100
#define ADDR_STRING		0x200
#define ADDR_FUNCTION	0x300
#define ADDR_INLINE		0x400
#define ADDR_NAME		0x1000
#define ADDR_COMMENT	0x2000
#define ADDR_SYMBOL	0x4000

 /* Operand and instruction types */
#define OP_R			0x001		/* operand is READ */
#define OP_W			0x002		/* operand is WRITTEN */
#define OP_X			0x004		/* operand is EXECUTED */
#define OP_REG			0x100		/* register */
#define OP_IMM			0x200		/* immediate value */
#define OP_IND			0x300		/* indirect memory reference */
#define OP_REL			0x400		/* relative Address [offset] */
#define OP_BPTR		0x500		/* BYTE Pointer */
#define OP_WPTR		0x600		/* WORD Pointer */
#define OP_DPTR		0x700		/* DWORD Pointer */
#define OP_STRPTR		0x800		/* STRING Pointer */
#define OP_EXPR		0x900		/* Address Expression [e.g. SIB byte] */
#define OP_UNK			0xA00		
#define OP_SIGNED    0x1000   /* operand is signed */
#define OP_UNSIGNED  0x2000   /* operand is unsigned */
#define OP_CONST     0x4000   /* operans is a constant */

#define INS_BRANCH   0x01 	 	/* Unconditional branch */
#define INS_COND     0x02  	/* Conditional branch */
#define INS_SUB      0x04  	/* Jump to subroutine */
#define INS_RET      0x08  	/* Return from subroutine */
    /* modify ( 'w' ) instructions */
#define INS_ARITH    0x10 		/* Arithmetic inst */
#define INS_LOGIC    0x20 		/* logical inst */
#define INS_FPU      0x40 		/* Floating Point inst */
#define INS_FLAG     0x80 		/* Modify flags */
    /* misc Instruction Types */
#define INS_MOVE     0x0100
#define INS_ARRAY    0x0200   /* String and XLAT ops */
#define INS_PTR      0x0400   /* Load EA/pointer */
#define INS_STACK    0x1000   /* PUSH, POP, etc */
#define INS_FRAME    0x2000   /* ENTER, LEAVE, etc */
#define INS_SYSTEM   0x4000   /* CPUID, WBINVD, etc */
 	/* Xref Types */
#define XREF_READ		0x01
#define XREF_WRITE	0x02
#define XREF_EXEC		0x04
	/* Name Types */
#define NAME_AUTO		0x01
#define NAME_SUB		0x02
#define NAME_USER		0x10
#define NAME_SYMBOL	0x20
   /* Comment Types */
#define CMT_AUTO		0x01
#define CMT_USER		0x02
	/* Macro Types */
#define SEERC_CODE	0x01
#define SEERC_FILE	0x02

 
 /* Other info */
#define BIG_ENDIAN_ORD 		0
#define LITTLE_ENDIAN_ORD 	1

