/* api.c : part of The Bastard disassembly environment ========================G
 *
 * Contains  the bulk of the internal code -- specifically, the API routines
 * provided for internal and scripting use. Functions should be in the same
 * order as in api.h
 */
#include <bastard.h>
#include <sys/types.h>

/* API Sections:
 *     3. Sections				:: Creating/manipulating file SECTION objects
 * /

/* ---------------------------------------------------------------Sections */
   int CreateSection(char *name, long start, int size, long pa, int type){
      struct section section;
      int ret = 1;
   
      if ( d_keyfind(SECTION_NAME, &name) == S_OKAY) {
         SetLastError(4810);
         return(0);
      } 
      else if (db_status == S_NOTFOUND ) {
         DefineAddr(start, size,  pa, ADDR_SECTION);
         strncpy(section.name, name, 32);
         section.rva = start;
         section.size = size;
         section.flags = type;
         if ( d_fillnew(SECTION, &section) != S_OKAY) {
            dbErrorMsg();
            ret = 0;
         }
      } 
      else {
         dbError();
         ret = 0;
      }
      disasm_env.flags |= DB_MOD;
      return(ret);
   }

   int DeleteSection(char *name){
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_delete();
         return(1);
      }
      return(SetLastError(4820));
   }

   long GetSectionStart(char *name){
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_recread(&s);
         return(s.rva);
      }
      return(SetLastError(4820));
   }

   long GetSectionEnd(char *name){
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_recread(&s);
         return(s.rva + s.size - 1);
      }
      return(SetLastError(4820));
   }

   int  GetSectionName(long rva, char* buf){
      struct section s;
   
      d_keyfrst(SECTION_NAME);
      while (db_status == S_OKAY){
         d_recread(&s);	
         if ( rva >= s.rva && rva < (s.rva + s.size)){
            strncpy(buf, s.name, 32);
            return(1);
         }
      }
      return(SetLastError(4820));
   }

   int  GetSectionFlags(char *name){
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_recread(&s);
         return(s.flags);
      }
      return(SetLastError(4820));
   }

   int  SetSectionStart(char *name, long start) {
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_recread(&s);
         s.rva = start;
         if ( d_recwrite(&s) == S_OKAY) 
            return(0);
         else 
            return(db_status);
      }
      return(SetLastError(4820));
   }

   int  SetSectionEnd(char *name, long end){
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_recread(&s);
         s.size =  end - s.rva;
         if ( d_recwrite(&s) == S_OKAY) 
            return(0);
         else 
            return(db_status);
      }
      return(SetLastError(4820));
   }

   int  SetSectionName(char *name, char *new_name){
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_recread(&s);
         strcpy(s.name, new_name);
         if ( d_recwrite(&s) == S_OKAY) 
            return(0);
         else 
            return(db_status);
      }
      return(SetLastError(4820));
   }

   int  SetSectionFlags(char *name, int flags){
      struct section s;
   
      strcpy( s.name, name);
      if ( d_keyfind(SECTION_NAME, &s.name) == S_OKAY){ 
         d_recread(&s);
         s.flags = flags;	
         if ( d_recwrite(&s) == S_OKAY) 
            return(0);
         else 
            return(db_status);
      }
      return(SetLastError(4820));
   }

