1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
static const char rcsid[] = "$Header: c:/mctex/imagen/rcs/bset.c 1.1 91/08/05 23:41:24 ROOT_DOS Exp Locker: ROOT_DOS $";
/* Copyright (c) 1993 Neal Becker
* All rights reserved.
* Permission to copy for any purpose is hereby granted
* so long as this copyright notice remains intact.
*/
/*
$Log: bset.c $
* Revision 1.1 91/08/05 23:41:24 ROOT_DOS
* Initial revision
*
*/
static const int BitMask[ 8 ] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
inline int min( int a, int b ) {
return ( a > b ) ? b : a;
}
inline unsigned char TopPart( char S, int Pos ) {
return ((unsigned char)S) >> Pos;
}
inline unsigned char BottomPart( char S, int Pos ) {
return S << ( 8 - Pos );
}
static void MemOr( char* D, char* S, int count ) {
for(; count != 0; count--, D++, S++ )
*D |= *S;
}
void Bset( char* S, int Slen, char* D, int Dlen, int Pos ) {
char* Dest = D + ( Pos / 8 );
int BitPos = Pos % 8;
char* Dend = D + Dlen - 1;
int BytesToMove = min( Slen, Dlen - ( Pos / 8 ) );
if( BitPos == 0 ) {
MemOr( Dest, S, BytesToMove );
}
else {
for( ; BytesToMove != 0; BytesToMove--, Dest++, S++ ) {
*Dest |= TopPart( *S, BitPos );
if( Dest + 1 > Dend ) break;
*( Dest + 1 ) |= BottomPart( *S, BitPos );
}
}
}
void SetBits( int Slen, char* D, int Dlen, int Pos ) {
char* Dest = D + ( Pos / 8 );
int BitPos = 7 - (Pos % 8);
int BitsToMove = min( Slen, Dlen - Pos );
for( ; BitsToMove > 0 ; BitsToMove-- ) {
*Dest |= BitMask[ BitPos-- ];
if( BitPos < 0 ) {
Dest++;
BitPos = 7;
}
}
}
|