
#include <bastard.h>

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


/* ---------------------------------------------------------------XRefs */
   long GetRefTo(long rva, int type) {
		/* returns first xref */
		struct xref x;
		
		if (d_keyfind(TO_RVA, &rva) == S_OKAY){
			d_recread(&x);
			while (type && !(x.type & type) ){
				if ( d_keynext(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(TO_RVA) == S_OKAY){
			d_recread(&x);
			while (type && !(x.type & type) ){
				if ( d_keynext(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(FROM_RVA, &rva) == S_OKAY){
			d_recread(&x);
			while (type && !(x.type & type) ){
				if ( d_keynext(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(FROM_RVA) == S_OKAY){
			d_recread(&x);
			while (type && !(x.type & type) ){
				if ( d_keynext(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);
   }
