MemCopyNibbles, C function, Copies a set of nibbles (half-bytes) from one memory location to another.
Mon, 29 Jun 2009 05:23:57 +0000 - Author: Peter O.
/*
* Copies a set of nibbles (half-bytes) from one memory location to
* another. The memory locations can overlap.
*
* @param dst Destination byte buffer. Returns FALSE if this parameter
* is NULL.
* @param iDst Nibble index within the destination buffer where the
* source nibbles will be received.
* @param src Source byte buffer. Returns FALSE if this parameter
* is NULL.
* @param iSrc Nibble index within the source buffer where the
* source nibbles will be read.
* @param count Number of nibbles to copy
* @param bitorder If TRUE, the nibbles are copied most-significant-nibble
* first. If FALSE, they're copied least-significant-nibble first.
* @return TRUE if successful, FALSE otherwise.
*/
/*
To use this function, either include "user.h" (included in the C Files
download of the projects section of this site) or define the following
macros (assumes that int and long are 32 bits long):
#define BOOL signed int
#define DWORD unsigned long
#define LPBYTE unsigned char *
#define BYTE unsigned char
#define LONG signed int
#define TRUE 1
#define FALSE 0
#define MoveMemory(a,b,c) memmove(a,b,c)
*/
BOOL MemCopyNibbles(
LPBYTE dst,// Destination byte buffer.
DWORD iDst,// Nibble index of the destination buffer
LPBYTE src,
DWORD iSrc,
DWORD count,// Number of nibbles (half-bytes) to copy
BOOL bitorder//if 1, MSB first
){
LPBYTE sp,dp;
int sshift,dshift;
int sstart,send,sinc,sbyteinc;
BYTE v;
DWORD i;
BYTE spTemp;
int getnewbyte=1;
if(!src||!dst)return FALSE;
if(!count||count>0xFFFFFFFE)return FALSE;
if(dst==src&&iDst==iSrc)return TRUE;
sp=src+(iSrc>>1);
dp=dst+(iDst>>1);
if(!(iDst&1)&&!(iSrc&1)&&!(count&1)){
MoveMemory(dp,sp,count>>1);
return TRUE;
}
if(spLONG srcend=((iSrc&1)+count-1);
LONG dstend=((iDst&1)+count-1);
sp+=srcend>>1;
dp+=dstend>>1;
sbyteinc=-1;
if(bitorder==1){
sshift=(1-(srcend&1))<<2;
dshift=(1-(dstend&1))<<2;
sstart=0;
send=4;
sinc=4;
} else {
sshift=(srcend&1)<<2;
dshift=(dstend&1)<<2;
sstart=4;
send=0;
sinc=-4;
}
} else {
sbyteinc=1;
if(bitorder==1){
sshift=(1-(iSrc&1))<<2;
dshift=(1-(iDst&1))<<2;
sstart=4;
send=0;
sinc=-4;
} else {
sshift=(iSrc&1)<<2;
dshift=(iDst&1)<<2;
sstart=0;
send=4;
sinc=4;
}
}
for(i=0;iif(getnewbyte){
spTemp=*sp;
getnewbyte=0;
}
v=(BYTE)((spTemp>>sshift)&0x0F);
*dp&=(BYTE)((0x0F0F>>(4-dshift))&0xFF);
*dp|=(BYTE)(v<if(dshift==send){
dshift=sstart;dp+=sbyteinc;
} else dshift+=sinc;
if(sshift==send){
sshift=sstart;sp+=sbyteinc;
getnewbyte=1;
} else sshift+=sinc;
}
return TRUE;
}