123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #include "common.h"
- #include <string.h>
- #include <stdio.h>
- /*
- 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<len;i++){
- if(src[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<len/2;i++){
- temp[0]=src[2*i];
- temp[1]=src[2*i+1];
- for(k=0;k<2;k++){
- if(temp[k]>='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<len;i++){
- sprintf(buf, "%02x",data[i]);
- strcat(src, buf);
- }
- strcat(src,"\r\n");
- }
- /*
- MakeStringToUpperCap
- 遍历data,将非大写字母转成大写字母
- */
- void MakeStringToUpperCap(T_UINT8 *data, T_UINT16 len){
- T_UINT16 i;
- for(i=0;i<len;i++)
- {
- if(data[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<listnum;i++)
- {
- cmdPtr=(char *)cmdlist[i];
- pSrc=data;
- flag=0;
- while(*cmdPtr){
- if(*cmdPtr != *pSrc){
- flag=1;
- break;
- }
- cmdPtr++;
- pSrc++;
- }
- if(flag==0) break;
- }
- if(i==listnum) i=-1;
- return i;
- }
- /*******************************************************************
- fitStrForIndex
- 从Str中查找Para串
- return:-1 未找到目标串
- return:>=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;i<len;i++){
- if(src[i]==target){
- if(++num==index) return i+1;
- }
- }
- return -1;
- }
- /*12-->0x12*/
- 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<len;i++){
- if(info[i]<'0' ||
- (info[i]>'9' && info[i]<'A') ||
- (info[i]>'F' && info[i]<'a') ||
- info[i]>'f') return 2;
- }
- return 0;
- }
|