MemCopyBits, C function, Copies a set of bits from one memory location to another.

Mon, 29 Jun 2009 01:25:45 -0400 - Author:

/*
* Copies a set of bits 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 Bit index within the destination buffer where the
* source bits will be received.
* @param src Source byte buffer. Returns FALSE if this parameter
* is NULL.
* @param iSrc Bit index within the source buffer where the
* source bits will be read.
* @param count Number of bits to copy
* @param bitorder If TRUE, the nibbles are copied most-significant-bit
* first. If FALSE, they're copied least-significant-bit 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 MemCopyBits(
LPBYTE dst,
DWORD iDst,
LPBYTE src,
DWORD iSrc,
DWORD count,
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>0xFFFFFFF8)return FALSE;
if(dst==src&&iDst==iSrc)return TRUE;
sp=src+(iSrc>>3);
dp=dst+(iDst>>3);
if(!(iDst&7)&&!(iSrc&7)&&!(count&7)){
MoveMemory(dp,sp,count>>3);
return TRUE;
}
if(sp LONG srcend=((iSrc&7)+count-1);
LONG dstend=((iDst&7)+count-1);
sp+=srcend>>3;
dp+=dstend>>3;
sbyteinc=-1;
if(bitorder==1){
sshift=7-(srcend&7);
dshift=7-(dstend&7);
sstart=0;
send=7;
sinc=1;
} else {
sshift=(srcend&7);
dshift=(dstend&7);
sstart=7;
send=0;
sinc=-1;
}
} else {
sbyteinc=1;
if(bitorder==1){
sshift=7-(iSrc&7);
dshift=7-(iDst&7);
sstart=7;
send=0;
sinc=-1;
} else {
sshift=(iSrc&7);
dshift=(iDst&7);
sstart=0;
send=7;
sinc=1;
}
}
for(i=0;i if(getnewbyte){
spTemp=*sp;
getnewbyte=0;
}
v=(BYTE)((spTemp>>sshift)&0x01);
*dp&=(BYTE)((0x7F7F>>(7-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;
}