📄 bitop.c
字号:
/*
* Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
* All rights reserved.
*
* This software is copyrighted by and is the sole property of
* VIA Networking Technologies, Inc. This software may only be used
* in accordance with the corresponding license agreement. Any unauthorized
* use, duplication, transmission, distribution, or disclosure of this
* software is expressly forbidden.
*
* This software is provided by VIA Networking Technologies, Inc. "as is"
* and any express or implied warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed. In no event shall VIA Networking Technologies, Inc.
* be liable for any direct, indirect, incidental, special, exemplary, or
* consequential damages.
*
*
* File: bitop.c
*
* Purpose: Bit operation in any range of byte array
*
* Author: Tevin Chen
*
* Date: Jan 08, 2002
*
* Functions:
*
* Revision History:
*
*/
#if !defined(__UASSERT_H__)
#include "uassert.h"
#endif
#if !defined(__BITOP_H__)
#include "bitop.h"
#endif
/*--------------------- Static Definitions ------------------------*/
/*--------------------- Static Types ------------------------------*/
/*--------------------- Static Macros -----------------------------*/
/*--------------------- Static Classes ----------------------------*/
/*--------------------- Static Variables --------------------------*/
/*--------------------- Static Functions --------------------------*/
static void s_vExtractBits (PBYTE abyData, BYTE byStartBit, BYTE byEndBit, PBYTE abyResult) DIRECT_FUNTYPE_REENT;
static void s_vModifyBits (PBYTE abyDataBuf, BYTE byStartBit, BYTE byEndBit, PBYTE abyModiPtn) DIRECT_FUNTYPE_REENT;
/*--------------------- Export Variables --------------------------*/
void BITvEndianConvert (PBYTE pbyDataBuf, BYTE byBufSize) DIRECT_FUNTYPE_REENT
{
BYTE bySwapTmp, uu;
// byBufSize must be even, cannot be odd
DBG_ASSERT((byBufSize % 2) == 0);
for (uu = 0; uu < (byBufSize / 2); uu++) {
bySwapTmp = pbyDataBuf[uu];
pbyDataBuf[uu] = pbyDataBuf[byBufSize - 1 - uu];
pbyDataBuf[byBufSize - 1 - uu] = bySwapTmp;
}
}
void BITvExtractBits(PBYTE abyData, BYTE byStartBit, BYTE byEndBit, PBYTE abyResult) DIRECT_FUNTYPE_REENT
{
#if defined(__BIG_ENDIAN)
BYTE byByteCount;
#endif
// Extract operation
s_vExtractBits(abyData, byStartBit, byEndBit, abyResult);
#if defined(__BIG_ENDIAN)
// Decide how many bytes to perform endian convertion, ceiling value
byByteCount = (byEndBit - byStartBit) / 8 + (((byEndBit - byStartBit) % 8) != 0);
// No 3 byte data type => must be double word
if (byByteCount == 3)
byByteCount = 4;
// Swap abyResult for big-endian platforms
BITvEndianConvert(abyResult, byByteCount);
#endif
}
void BITvModifyBits(PBYTE abyDataBuf, BYTE byStartBit, BYTE byEndBit, PBYTE abyModiPtn) DIRECT_FUNTYPE_REENT
{
#if defined(__BIG_ENDIAN)
BYTE byByteCount;
// Decide how many bytes to perform endian convertion, ceiling value
byByteCount = (byEndBit - byStartBit) / 8 + (((byEndBit - byStartBit) % 8) != 0);
// No 3 byte data type => must be double word
if (byByteCount == 3)
byByteCount = 4;
// Swap abyModiPtn for big-endian platforms
BITvEndianConvert(abyModiPtn, byByteCount);
#endif
// Modify operation
s_vModifyBits(abyDataBuf, byStartBit, byEndBit, abyModiPtn);
#if defined(__BIG_ENDIAN)
// Swap back abyModiPtn for big-endian platforms
BITvEndianConvert(abyModiPtn, byByteCount);
#endif
}
static void s_vExtractBits(PBYTE abyData, BYTE byStartBit, BYTE byEndBit, PBYTE abyResult) DIRECT_FUNTYPE_REENT
{
BYTE byByteCount;
BYTE uu, byBitMask = 0xFF;
// Decide how many bytes, ceiling value
byByteCount = (byEndBit - byStartBit) / 8 + (((byEndBit - byStartBit) % 8) != 0);
// Clear result buffer
for (uu = 0; uu < byByteCount; uu++)
abyResult[uu] = 0;
uu = 0;
while (1) {
// Extract 8 succesive bits from StartBit to result
abyResult[uu] = ( (abyData[byStartBit / 8 + 1] << (8 - byStartBit % 8)) |
(abyData[byStartBit / 8] >> (byStartBit % 8)) );
// Adjust bit pattern in highest byte
if (byEndBit - byStartBit < 8) {
byBitMask >>= 8 - (byEndBit - byStartBit + 1);
abyResult[uu] &= byBitMask;
break;
}
// Adjust start bit and array pointer
byStartBit += 8;
uu++;
}
}
static void s_vModifyBits(PBYTE abyDataBuf, BYTE byStartBit, BYTE byEndBit, PBYTE abyModiPtn) DIRECT_FUNTYPE_REENT
{
BYTE byBitOffset, byBufIndx, byPtnIndx = 0, byBitMask = 0xFF;
// Initial config
byBufIndx = byStartBit / 8;
byBitOffset = byStartBit % 8;
// Handle first byte of DataBuf
if ((byEndBit / 8) == (byStartBit / 8)) {
// Clear modified bits of data buffer
abyDataBuf[byBufIndx] &= ~( (byBitMask >> (7 - byEndBit + byStartBit))
<< byBitOffset);
// Write modified pattern into data buffer
abyDataBuf[byBufIndx] |= ( (abyModiPtn[byPtnIndx] &
(byBitMask >> (7 - byEndBit + byStartBit)))
<< byBitOffset );
return;
}
else {
// Clear modified bits of data buffer
abyDataBuf[byBufIndx] &= ~(byBitMask << byBitOffset);
// Write modified pattern into data buffer
abyDataBuf[byBufIndx] |= (abyModiPtn[byPtnIndx] << byBitOffset);
// Adjust indexs
byStartBit += 8 - byBitOffset;
byBufIndx++;
}
while(1) {
// Check new byte position
if (byStartBit > byEndBit) {
return;
}
// Handle highest byte of DataBuf
else if ((byEndBit - byStartBit) < 8) {
// Clear modified bits of data buffer
abyDataBuf[byBufIndx] &= (byBitMask << (byEndBit % 8 + 1));
// Write modified pattern into data buffer
abyDataBuf[byBufIndx] |= ( (abyModiPtn[byPtnIndx] >> (8 - byBitOffset)) |
(abyModiPtn[byPtnIndx + 1] << byBitOffset) )
& ~(byBitMask << (byEndBit % 8 + 1));
return;
}
// Handle middle bytes of DataBuf
else {
// Clear modified bits of data buffer
abyDataBuf[byBufIndx] = 0;
// Write modified pattern into data buffer
abyDataBuf[byBufIndx] |= ((abyModiPtn[byPtnIndx] >> (8 - byBitOffset)) |
(abyModiPtn[byPtnIndx + 1] << byBitOffset));
// Adjust start bit and byte index
byPtnIndx++;
byBufIndx++;
byStartBit += 8;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -