#include "common.h" #include #include /* checkIpDomain 检测地址是IP还是域名,并且提取到ipDomain中 IP/域名类型提取到type 成功则返回true */ T_BOOL checkIpDomain(T_INT8 *src, T_UINT16 len, T_INT8 * ipDomain, IPD_ENUM *type){ //www.baidu.com //120.77.66.129 T_UINT8 i,j,k,flag; j=0;k=0;flag=0; *type=IPD_IP; for(i=0;i= '0' && src[i] <= '9'){ if(k<=3){ j++; if(j>3) flag=1; } }else if(src[i] == '.'){ k++; j=0; if(k>3){ //get 4 '.', we condisider it is domain *type = IPD_DOMAIN; } }else if(src[i] == ':') break; else{//it is domain *type = IPD_DOMAIN; } } if(k==3 && flag != 0) return FALSE; // only 3 digit max it is ip , or it is invalid if(len > IP_DOMAIN_SIZE) return FALSE; else{ if(ipDomain != NULL){ memset(ipDomain, 0, IP_DOMAIN_SIZE); memcpy(ipDomain, src, i); } return TRUE; } } /* restoreDataFormatByHex "313233"=>{0x31,0x32,0x33} */ T_INT8 restoreDataFormatByHex(T_UINT8 *src, T_UINT16 len) { T_UINT16 i,j,k; T_UINT8 temp[2]; if(NULL==src) return 1; j=0; for(i=0;i='0' && temp[k]<='9') temp[k] -= 0x30; else if(temp[k]>='a' && temp[k]<='f') temp[k] -= 0x57; else if(temp[k]>='A' && temp[k]<='F') temp[k] -= 0x37; else return 2; } temp[0] <<= 4; temp[0] &= 0xF0; src[j++] = temp[0] | temp[1]; } return 0; } /* fillByHexFormat {0x31,0x32,0x33}=>"313233"=>src */ void fillByHexFormat(T_INT8 *src, T_INT8 *data, T_INT32 len){ T_INT32 i; T_INT8 buf[3]; for(i=0;i= 'a' && data[i] <= 'z') data[i] -= 0x20; } } T_INT16 matchCmdList(const char **cmdlist,T_UINT8 *data){ T_INT8 *cmdPtr; T_UINT8 *pSrc,flag; T_INT16 i; T_UINT16 listnum; i=0; while(*cmdlist[i++]); listnum=i-1; for(i=0;i=0 找到目标串返回目标在源串的索引值 ********************************************************************/ T_INT16 fitStrForIndex(T_INT8 *Str,T_INT8 *Para){ T_INT8 *targetP; targetP=strstr(Str, Para); if(targetP==0) return -1; else{ return (targetP-Str); } } /* 从长度为len的src缓存中查找第index个target,并返回下一个字节的索引 src:缓存 len:缓存长度 target:要匹配的字节内容 index:第几个target, 1,2,3,4..... return: < 0 失败,else, target后面一个字节的索引 */ int findByteFromStr(unsigned char *src, int len,unsigned char target,unsigned char index){ int i; unsigned char num=0; for(i=0;i0x12*/ unsigned char decToBCD(unsigned char value){ unsigned char h,l; h=value%100/10; l=value%10; h <<= 4; return (h&0xf0|l); } /*0x12-->12*/ unsigned char bcdToDec(unsigned char value){ unsigned char h,l; h=(value>>4)&0x0f; l=value&0x0f; return (h*10+l); } /*"04"-->0x04 "1F"-->0x1F*/ /*检测是否是16进制的字符段的合法数据*/ char isBytesAreHex(unsigned char *info, int len){ int i; if(len%2) return 1;//要为偶数 for(i=0;i'9' && info[i]<'A') || (info[i]>'F' && info[i]<'a') || info[i]>'f') return 2; } return 0; }