#include <api/api_main.h>

extern struct DISASM_TGT target;
extern struct DISASM_ENV disasm_env;
extern struct DISASM_PREFS disasm_prefs;

/* API sections:
 *    14. Xrefs               :: Address cross-references
 */


/* ---------------------------------------------------------------XRefs */
   long GetRefTo(long rva, int type) {
      /* returns first xref */
      struct xref x;
      
      if (d_keyfind(XREF_TO_RVA, &rva) == S_OKAY){
         d_recread(&x);
         while (type && !(x.type & type) ){
            if ( d_keynext(XREF_TO_RVA) != S_OKAY) return(0);
            d_recread(&x);
         }
         return(x.from_rva);
      }
     return(SetLastError(5320));
   }
   
   long GetNextRefTo(int type) {
      struct xref x;
      
      if (d_keynext(XREF_TO_RVA) == S_OKAY){
         d_recread(&x);
         while (type && !(x.type & type) ){
            if ( d_keynext(XREF_TO_RVA) != S_OKAY) return(0);
            d_recread(&x);
         }
         return(x.from_rva);
      }
      return(0);
   }

   long GetRefFrom(long rva, int type) {
      struct xref x;
      
      if (d_keyfind(XREF_FROM_RVA, &rva) == S_OKAY){
         d_recread(&x);
         while (type && !(x.type & type) ){
            if ( d_keynext(XREF_FROM_RVA) != S_OKAY) return(0);
            d_recread(&x);
         }
         return(x.to_rva);
      }
     return(SetLastError(5300));
   }
   
   long GetNextRefFrom(int type) {
      struct xref x;
      
      if (d_keynext(XREF_FROM_RVA) == S_OKAY){
         d_recread(&x);
         while (type && !(x.type & type) ){
            if ( d_keynext(XREF_FROM_RVA) != S_OKAY) return(0);
            d_recread(&x);
         }
         return(x.to_rva);
      }
      return(0);
   }

   int DefineRef(long from, long to, int type){
      struct xref x;
   
      x.from_rva = from;
      x.to_rva = to;
      x.type = type;
      x.id   = ++seq.xref;
   /* add sequence here for ID */
      d_fillnew(XREF, &x);
      if (db_status != S_OKAY) 
         return(SetLastError(dbError()));
      return(1);
   }
