//
// main.c
// jnjd
//
// Created by syw on 14-3-14.
// Copyright (c) 2014年 syw. All rights reserved.
//
#include <stdio.h>
#define TRUE 0
#define FALSE 1
//#define MAX_FRAME_NUM 10000
typedef unsigned int UINT32;
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef struct
{
UINT8 * pByte;
UINT8 ucBitPos;
}STRU_POS;
/*
typedef struct
{
UINT16 uhwKey;
UINT8 ucOpCode;
UINT8 ucPara0;
UINT8 ucPara1;
}STRU_FRAME;
typedef struct
{
STRU_FRAME * pstruHostFrame;
STRU_POS struStart;
STRU_POS struEnd;
}STRU_CTRL;
typedef struct
{
UINT32 uwFrameCnt;
STRU_FRAME struFrame[MAX_FRAME_NUM];
}STRU_FRAME_LIST;
STRU_FRAME_LIST gstruFrame;
STRU_CTRL gCtrl;
UINT8 * pgIn;
UINT8 * pgOut;
*/
UINT8 * pgEndBound;
/*************************************************************************
函数功能: 从指定位置 获取 连续bit 字段;
ucBitNum 待获取的bit数,最多32bit;
pstruPos 待获取 的起始位置
返回: 获取到的 字段
**************************************************************************/
UINT32 BitFieldGet(UINT8 ucBitNum,STRU_POS * pstruPos)
{
UINT32 uwVal,uwi,uwTmp;
STRU_POS struPos;
struPos.pByte = pstruPos->pByte;
struPos.ucBitPos = pstruPos->ucBitPos;
uwVal =0;
for(uwi =0;uwi < ucBitNum;uwi++)
{
uwTmp =0x01&((struPos.pByte[0])>>(struPos.ucBitPos));
uwTmp = uwTmp<<uwi;
uwVal = uwVal+uwTmp;
/* Update position; */
if(struPos.ucBitPos <7)
{(struPos.ucBitPos)++;}
else
{
struPos.ucBitPos =0;
(struPos.pByte)++;
}
}
return uwVal;
}
/*************************************************************************
函数功能: 检测 当前位置 是否为有效pattern
ucTotalBitNum 待检测pattern的bit数, 最多32bit;
uwPattern 待检测pattern
pstruPos 待检测 的起始位置
返回: 0 : 检测到 pattern
1: 未检测到pattern
**************************************************************************/
UINT32 PatternCheck(UINT8 ucTotalBitNum,UINT32 uwPattern,STRU_POS * pstruPos)
{
UINT8 aucBit;
UINT32 uwi;
UINT8 ucByte;
STRU_POS struPos;
struPos.pByte = pstruPos->pByte;
struPos.ucBitPos = pstruPos->ucBitPos;
if (ucTotalBitNum >32)
{
return1;
}
for(uwi =0;uwi < ucTotalBitNum;uwi++)
{
ucByte = struPos.pByte[0];
aucBit =0x01&(ucByte>>(struPos.ucBitPos));
if(aucBit != (0x01&(uwPattern>>uwi)))
{return1;}
if(struPos.ucBitPos <7)
{(struPos.ucBitPos)++;}
else
{
struPos.ucBitPos =0;
(struPos.pByte)++;
}
}
return0;
}
/*************************************************************************
函数功能: 在连续的bit流中检测指定图案并记录patern起始 bit和字节位置;
pstruIn 搜索起始位置; 实时修改;
ucTotalBitNum 待搜索pattern的bit数
uwPattern 待搜索pattern
返回: 0: 搜索成功;
1: 搜索失败;
**************************************************************************/
UINT32 PatternSearch(STRU_POS * pstruIn,UINT8 ucTotalBitNum,UINT32 uwPattern)
{
STRU_POS struPos;
UINT32 uwFlag;
UINT32 uwDiff;
UINT8 ucRemainByteNum;
UINT8 ucRemainBitNum;
uwFlag =1;
ucRemainByteNum = ucTotalBitNum/8;
ucRemainBitNum = ucTotalBitNum%8;
while (1 == uwFlag)
{
uwDiff = (UINT32)pgEndBound - (UINT32)(pstruIn->pByte);
if((uwDiff < ucRemainByteNum)
||((ucRemainByteNum == uwDiff)&&(pstruIn->ucBitPos>(8-ucRemainBitNum)))
)
{
returnFALSE;
}
uwFlag =PatternCheck(ucTotalBitNum, uwPattern, pstruIn);
/* Keep the position; */
struPos.pByte = pstruIn->pByte;
struPos.ucBitPos = pstruIn->ucBitPos;
/* Update the position; */
if(pstruIn->ucBitPos <7)
{
(pstruIn->ucBitPos)++;
}
else
{
pstruIn->ucBitPos =0;
(pstruIn->pByte++);
}
}
/* Record the pattern position; */
pstruIn->ucBitPos = struPos.ucBitPos;
pstruIn->pByte = struPos.pByte;
return TRUE;
}
/************************************************************************************************************/
int main(int argc,constchar * argv[])
{
UINT8 aIn[3] = {0x12,0x33,0x58};
STRU_POS struPos;
UINT32 uwVal;
struPos.pByte = &aIn[1];
struPos.ucBitPos =6;
/*
00010010
00110011
01011000
*/
uwVal =BitFieldGet(7, &struPos);
printf("%d\n",uwVal);
//TestCase1();
// insert code here...
printf("Hello, World!\n");
return0;
}