📄 cbits.h
字号:
/*-------------数据流处理与侦察基本类库 -----------------
适用:Sco Openserver 5.0.X C++
Win9X C++
WinNT/Win2000/XP C++
-------------------------------
VisualStudio 6.0
VisualStudio .NET
------------------------------
注意事项:
所提供的测试环境为PIII800,Win2000
所有数据处理类除非特别声明,默认为低位在前.
版权所有(C)
1995-2003 Meylin Corp.
1998-2003 Anybit Studio
更新历史:
2003.06 增加了排队模版CSortNameList CSortValueList
-----------------------------------------------------
在 UNIX 上使用请定义宏 UNIX
---------------------------------------------------*/
#ifndef _CJG_CBITS_CLASS
#define _CJG_CBITS_CLASS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <assert.h>
#include <time.h>
#include <fcntl.h>
#include <stdarg.h>
//#define UNIX
#define _INTEL_CPU
/*-------------------------修改历史-----------------
Version 1.1 2001/05/21
最新版 Version2.0 2003.06.05
-----------------------------------------------------*/
#define BUG_MAGIC 0x12345678
#define FIFO_MAGIC 'FIFO'
typedef unsigned char u_char;
typedef unsigned int u_int;
typedef unsigned char BYTE;
//
//--------------------------------------------
//
#define SAFEDELETE(pp) if(pp){delete pp;pp=NULL;}
#define NOUSE(aa) {aa;}
#define Meg(a) ((a)*1024*1024)
#define Kilo(a) ((a)*1024)
#define REVERSE(a) (a)=!(a)
extern void INIT_HDLC(bool);
extern int (*CRC)(unsigned char *,int len);
extern u_char crc7[]; //校验
extern int QuickHDLCList[8*256];
extern int QuickHDLCNext[8*256];
extern u_int fcstab16[256];
extern u_int fcstab32[256];
extern u_char REV8[256];
extern u_char Normal8[256];
extern int WEIGHT8[256];
extern int HexTable[256];
void rev8(BYTE *pch,int size);
#define IsHexChar(cc) (HexTable[(unsigned char)(cc)]<16)
#define Hex2Val(cc) HexTable[(unsigned char)(cc)]
#define IsDecChar(cc) (HexTable[(unsigned char)(cc)]<10)
#ifdef UNIX
//unix system-----------------
#define volatile
#define PARTING '/'
typedef unsigned int u_int;
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
typedef int HANDLE;
#define INVALID_HANDLE_VALUE (-1L)
typedef void* LPVOID;
#else
//mircosoft windows system
//#include <windows.h>
#define PARTING '\\'
#endif
//
//----------------------------------------
//
int warning(char *fmt, ...);
//
//基本的位运算内联定义
#define GetShort(pch,pos) \
((*(unsigned int*)(pch+pos/8)>>(pos&7))&0xffff)
inline int GetBit(int a,unsigned int n)
{
return ((a>>n)&1);
}
inline int GetBit(unsigned char a,unsigned int n)
{
return ((a>>n)&1);
}
inline int GetBit(unsigned char *pch,unsigned int n)
{
return ((pch[n>>3]>>(n&7))&1);
}
inline int GetBit(unsigned int *pch,unsigned int n)
{
return ((pch[n>>5]>>(n&0x1f))&1);
}
inline int GetBitS(unsigned char *pch,unsigned int pos,
int bits)
{
assert(bits<25);
return (*(unsigned int *)(pch+pos/8)>>(pos%8))&((1<<bits)-1);
}
//
//------------------------------------------
//
inline int GetBitH(unsigned char *pch,int n)
{
return ((pch[n>>3]>>(7-(n&7)))&1);
}
inline int GetBitH(int *pch,int n)
{
return ((pch[n>>5]>>(31-(n&0x1f)))&1);
}
//
//--------------------------------------------
//
inline BYTE GetByte(unsigned char * p,int position)
{
p+=position/8;
position%=8;
return (BYTE)((*(p)>>position)^(*(p+1)<<(8-position)));
}
inline void swapc(BYTE &c1,BYTE &c2)
{
BYTE tmp;
tmp=c1;
c1=c2;
c2=tmp;
}
//
//转化为可显示字符
//
inline unsigned char toAlpha(unsigned char c)
{
if(c>=' ' && c<127)
return c;
else return '.';
}
inline bool isBlank(unsigned char c)
{
if(c==0x20 || c==0x9 || c==0xa || c==0xd)
return true;
return false;
}
//
//用于缓冲区溢出保护
//
#ifdef _DEBUG
class CBug
{
public:
unsigned int magic;
CBug(){magic=BUG_MAGIC;};
~CBug(){
assert(magic==BUG_MAGIC);
}
void check()
{
assert(magic==BUG_MAGIC);
}
};
#else
class CBug
{
};
#endif
#define SETBUGTRAP(aaa) CBug aaa;
//=========================================
class FIFO_CMD_PARA
{
public:
int m_id;
unsigned char * commandstr;
FIFO_CMD_PARA(int id){
m_id=id;
commandstr=NULL;
};
};
#ifdef FIFO_STANDARD
class FIFO_PARAM
{
void *m_content;
public:
enum DATATYPE
{
DATA_INT=1,
DATA_POINTER=2,
};
FIFO_PARAM(){};
FIFO_PARAM(int a)
{
m_content=(void *)a;
}
operator=(int a){m_content=(void *)a;};
operator int() {return (int)m_content;};
};
#else
typedef int FIFO_PARAM;
#endif
//
// ======================适合数据流处理的先进先出类
//
#define FIFO_INPUT virtual int input(BYTE *ibuf,int isize,\
FIFO_PARAM iparam=0)
#define IMPL_INPUT(clsname) int clsname::input(BYTE *ibuf,int isize,FIFO_PARAM iparam)
#define OK_INPUT 0
class FIFO
{
u_int magic;
FIFO *m_next; //横向数据链节点
u_int m_errorcode;
public:
FIFO *m_LastNext;
u_int m_ID;
//--兼容已有的其他程序-----------
#ifdef USE_OLD_CBITS
u_int m_LID;
char m_name[256];
bool FilterHttp;
int TypeId;
_int64 m_OKNumber;
_int64 m_Errors;
_int64 m_DumpNumber;
int m_rate; //数据速率
int m_runoff; //数据流量
u_int m_channel_id;
#endif
//-----------------------------------
public:
bool m_AutoDelete;
BYTE *m_buf; //公用缓冲区
int m_pos;
int m_BufSize;
int bbsize;
//-----------------------------------
int m_counter; //辅助计数器
int m_type;
u_int m_DataType;
int m_monitor;
u_int preLost;
//------------------------------------
enum data_type
{
ASCII=2,
HEX=4,
REVORDER=8,
OF_HDLC=16,
APPEND=32,
BIT01=64,
};
public:
bool isok() {return m_errorcode!=0;};
void SetLastError(u_int err){m_errorcode=err;};
void clear()
{
m_next=NULL;
m_LastNext=NULL;
m_ID=0x0;
m_buf=NULL;
m_pos=0;
m_BufSize=0;
m_counter=0;
m_AutoDelete=false;
m_errorcode=0;
preLost=0;
m_monitor=0;
}
FIFO()
{
magic=FIFO_MAGIC;
clear();
};
FIFO(u_int id)
{
clear();
m_ID=id;
}
void AssertValid()
{
assert(magic==FIFO_MAGIC);
}
virtual int GetCount(){return 0;};
bool DirectLoadFile(const char *name);
virtual void GetItem(int,int,char *st)
{
st[0]=0;
};
virtual void flush()
{
if(!m_buf)return;
if(m_pos)
InputNext(m_buf,m_pos,0);
m_pos=0;
}
inline bool FlushAll()
{
for(FIFO *nn=this;nn!=NULL;nn=nn->m_next)
nn->flush();
return true;
}
virtual ~FIFO()
{
//bug flush();
if(m_next!=NULL)
{
m_next->m_LastNext=NULL;
if(m_next->m_AutoDelete)delete m_next;
}
if(m_LastNext) m_LastNext->SetNext(m_next);
m_LastNext=NULL;
m_next=NULL;
if(m_buf)delete m_buf;
m_buf=NULL;
}
virtual bool canSend(int size=0){
NOUSE(size);
return true;
};
FIFO_INPUT
{
return InputNext(ibuf,isize,iparam);
}
inline int InputNext(BYTE *pch,int size,FIFO_PARAM restbit=0)
{
if(m_next)
return m_next->input(pch,size,restbit);
return 0;
}
inline bool SetNext(FIFO* next)
{
m_next=next;
if(next)next->m_LastNext=this;
return true;
}
FIFO& operator>>(FIFO& next)
{
SetNext(&next);
return next;
}
FIFO* GetNext(){return m_next;};
FIFO* GetLast() {return m_LastNext;};
};
inline void DeleteFIFO(FIFO *head)
{
FIFO *next;
while(head)
{
next=head->GetNext();
head->SetNext(NULL);
if(next)next->m_LastNext=NULL;
delete head;
head=next;
}
}
class CRev8:public FIFO
{
public:
CRev8() {};
~CRev8() {};
FIFO_INPUT
{
rev8(ibuf,isize);
InputNext(ibuf,isize);
return 0;
}
};
/**********************************
HDLC类如果用于高位在前的数据在使用前必须调用
HDLC::SetHighBit()
**********************************/
class HDLC:public FIFO
{
private:
int c8;
int bit8;
int fcs;
int last1yc;
int pos;
int crcflag;
int showmode;
bool crcok;
public:
unsigned char frame[8192]; //存放帧内容
int framelen; //帧字节长度
int restbit; //帧剩余的比特数
void SetHighBit(bool xx=true);
int CRC();
int IsIP();
void setCRCOK(bool flag){crcok=flag;};
void clear()
{
bit8=0;
c8=0;
pos=0;
last1yc=0;
fcs=0xffff;
showmode=0;
crcflag=-1;
framelen=0;
crcok=true;
}
HDLC(){
INIT_HDLC(true);
clear();
};
inline void outframec(){
if(pos<8000) frame[pos++]=(BYTE)c8;
bit8=0;
}
FIFO_INPUT;
void show(char *format="hex");
HDLC *operator<<(int flag);
};
//----------------------------------
// 2002.9.21 改变为虚基类
//
class CacheBuf:virtual public FIFO
{
public:
unsigned int m_media;
int m_displace;
int m_framenum;
unsigned long m_curpos;
CacheBuf()
{
m_displace=0;
m_framenum=0;
m_media=0;
m_curpos=0;
m_buf=NULL;
};
CacheBuf(int bytesize) //字节数
{
m_displace=0;
m_framenum=0;
m_media=0;
m_curpos=0;
Resize(bytesize);
};
virtual ~CacheBuf() {};
FIFO_INPUT;
virtual bool IsFull(unsigned char *ibuf,int isize=0)
{
int flag=InputNext(ibuf,isize);
LShift();
return flag!=0;
}
bool LShift(int pos=0,unsigned char *out=NULL);
bool Resize(int);
};
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -