common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "common.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. /*
  5. checkIpDomain
  6. 检测地址是IP还是域名,并且提取到ipDomain中
  7. IP/域名类型提取到type
  8. 成功则返回true
  9. */
  10. T_BOOL checkIpDomain(T_INT8 *src, T_UINT16 len, T_INT8 * ipDomain, IPD_ENUM *type){
  11. //www.baidu.com
  12. //120.77.66.129
  13. T_UINT8 i,j,k,flag;
  14. j=0;k=0;flag=0;
  15. *type=IPD_IP;
  16. for(i=0;i<len;i++){
  17. if(src[i] >= '0' && src[i] <= '9'){
  18. if(k<=3){
  19. j++;
  20. if(j>3) flag=1;
  21. }
  22. }else if(src[i] == '.'){
  23. k++;
  24. j=0;
  25. if(k>3){
  26. //get 4 '.', we condisider it is domain
  27. *type = IPD_DOMAIN;
  28. }
  29. }else if(src[i] == ':') break;
  30. else{//it is domain
  31. *type = IPD_DOMAIN;
  32. }
  33. }
  34. if(k==3 && flag != 0) return FALSE; // only 3 digit max it is ip , or it is invalid
  35. if(len > IP_DOMAIN_SIZE) return FALSE;
  36. else{
  37. if(ipDomain != NULL){
  38. memset(ipDomain, 0, IP_DOMAIN_SIZE);
  39. memcpy(ipDomain, src, i);
  40. }
  41. return TRUE;
  42. }
  43. }
  44. /*
  45. restoreDataFormatByHex
  46. "313233"=>{0x31,0x32,0x33}
  47. */
  48. T_INT8 restoreDataFormatByHex(T_UINT8 *src, T_UINT16 len)
  49. {
  50. T_UINT16 i,j,k;
  51. T_UINT8 temp[2];
  52. if(NULL==src) return 1;
  53. j=0;
  54. for(i=0;i<len/2;i++){
  55. temp[0]=src[2*i];
  56. temp[1]=src[2*i+1];
  57. for(k=0;k<2;k++){
  58. if(temp[k]>='0' && temp[k]<='9') temp[k] -= 0x30;
  59. else if(temp[k]>='a' && temp[k]<='f') temp[k] -= 0x57;
  60. else if(temp[k]>='A' && temp[k]<='F') temp[k] -= 0x37;
  61. else return 2;
  62. }
  63. temp[0] <<= 4;
  64. temp[0] &= 0xF0;
  65. src[j++] = temp[0] | temp[1];
  66. }
  67. return 0;
  68. }
  69. /*
  70. fillByHexFormat
  71. {0x31,0x32,0x33}=>"313233"=>src
  72. */
  73. void fillByHexFormat(T_INT8 *src, T_INT8 *data, T_INT32 len){
  74. T_INT32 i;
  75. T_INT8 buf[3];
  76. for(i=0;i<len;i++){
  77. sprintf(buf, "%02x",data[i]);
  78. strcat(src, buf);
  79. }
  80. strcat(src,"\r\n");
  81. }
  82. /*
  83. MakeStringToUpperCap
  84. 遍历data,将非大写字母转成大写字母
  85. */
  86. void MakeStringToUpperCap(T_UINT8 *data, T_UINT16 len){
  87. T_UINT16 i;
  88. for(i=0;i<len;i++)
  89. {
  90. if(data[i] >= 'a' && data[i] <= 'z')
  91. data[i] -= 0x20;
  92. }
  93. }
  94. T_INT16 matchCmdList(const char **cmdlist,T_UINT8 *data){
  95. T_INT8 *cmdPtr;
  96. T_UINT8 *pSrc,flag;
  97. T_INT16 i;
  98. T_UINT16 listnum;
  99. i=0;
  100. while(*cmdlist[i++]);
  101. listnum=i-1;
  102. for(i=0;i<listnum;i++)
  103. {
  104. cmdPtr=(char *)cmdlist[i];
  105. pSrc=data;
  106. flag=0;
  107. while(*cmdPtr){
  108. if(*cmdPtr != *pSrc){
  109. flag=1;
  110. break;
  111. }
  112. cmdPtr++;
  113. pSrc++;
  114. }
  115. if(flag==0) break;
  116. }
  117. if(i==listnum) i=-1;
  118. return i;
  119. }
  120. /*******************************************************************
  121. fitStrForIndex
  122. 从Str中查找Para串
  123. return:-1 未找到目标串
  124. return:>=0 找到目标串返回目标在源串的索引值
  125. ********************************************************************/
  126. T_INT16 fitStrForIndex(T_INT8 *Str,T_INT8 *Para){
  127. T_INT8 *targetP;
  128. targetP=strstr(Str, Para);
  129. if(targetP==0) return -1;
  130. else{
  131. return (targetP-Str);
  132. }
  133. }
  134. /*
  135. 从长度为len的src缓存中查找第index个target,并返回下一个字节的索引
  136. src:缓存
  137. len:缓存长度
  138. target:要匹配的字节内容
  139. index:第几个target, 1,2,3,4.....
  140. return: < 0 失败,else, target后面一个字节的索引
  141. */
  142. int findByteFromStr(unsigned char *src, int len,unsigned char target,unsigned char index){
  143. int i;
  144. unsigned char num=0;
  145. for(i=0;i<len;i++){
  146. if(src[i]==target){
  147. if(++num==index) return i+1;
  148. }
  149. }
  150. return -1;
  151. }
  152. /*12-->0x12*/
  153. unsigned char decToBCD(unsigned char value){
  154. unsigned char h,l;
  155. h=value%100/10;
  156. l=value%10;
  157. h <<= 4;
  158. return (h&0xf0|l);
  159. }
  160. /*0x12-->12*/
  161. unsigned char bcdToDec(unsigned char value){
  162. unsigned char h,l;
  163. h=(value>>4)&0x0f;
  164. l=value&0x0f;
  165. return (h*10+l);
  166. }
  167. /*"04"-->0x04 "1F"-->0x1F*/
  168. /*检测是否是16进制的字符段的合法数据*/
  169. char isBytesAreHex(unsigned char *info, int len){
  170. int i;
  171. if(len%2) return 1;//要为偶数
  172. for(i=0;i<len;i++){
  173. if(info[i]<'0' ||
  174. (info[i]>'9' && info[i]<'A') ||
  175. (info[i]>'F' && info[i]<'a') ||
  176. info[i]>'f') return 2;
  177. }
  178. return 0;
  179. }