link_string.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. /**
  35. * DATE AUTHOR INSTRUCTION
  36. * 2019-04-29 09:50 zhangqianfu The first version
  37. *
  38. */
  39. #include <stdint.h>
  40. #include <stdlib.h>
  41. #include <stddef.h>
  42. #include <string.h>
  43. // #include <osal.h> 修改
  44. //this function is used to format the char string to the argc mode
  45. //this function will changed the original string, used it carefully
  46. //return how many arguments has been
  47. int string_to_arg(int *argc, const char *argv[],char *string)
  48. {
  49. int argvlen = 0;
  50. int paramnum = 0;
  51. char *tmp = NULL;
  52. char bak;
  53. int len;
  54. argvlen = *argc;
  55. *argc = paramnum;
  56. if(NULL == string)
  57. {
  58. return paramnum;
  59. }
  60. //use the '\0' to replace the ' '
  61. len = strlen(string);
  62. tmp = string;
  63. while(tmp < (string + len))
  64. {
  65. if(*tmp == ' ')
  66. {
  67. *tmp = '\0';
  68. }
  69. tmp++;
  70. }
  71. bak = '\0';
  72. tmp = string;
  73. while(tmp < (string + len))
  74. {
  75. if((*tmp != '\0')&&(bak =='\0'))
  76. {
  77. if(paramnum < argvlen)
  78. {
  79. argv[paramnum] = tmp;
  80. paramnum++;
  81. }
  82. }
  83. bak = *tmp;
  84. tmp++;
  85. }
  86. *argc = paramnum;
  87. return paramnum;
  88. }
  89. char *osal_strdup(const char *ch)
  90. {
  91. char *copy;
  92. size_t length;
  93. if(NULL == ch)
  94. return NULL;
  95. length = strlen(ch);
  96. copy = (char *)malloc(length + 1); //修改
  97. if(NULL == copy)
  98. return NULL;
  99. (void) strncpy(copy, ch, length);
  100. copy[length] = '\0';
  101. return copy;
  102. }
  103. char *osal_strcat(char *str[])
  104. {
  105. char *ret = NULL;
  106. int str_tnum = 0;
  107. int str_tlen = 0;
  108. int str_off = 0;
  109. int i = 0;
  110. if(NULL == str)
  111. {
  112. return ret;
  113. }
  114. while(NULL != str[i])
  115. {
  116. str_tlen += strlen(str[i]);
  117. str_tnum++;
  118. i++;
  119. }
  120. ret = malloc(str_tlen); //修改
  121. if(NULL != ret)
  122. {
  123. for(i =0;i< str_tnum;i++)
  124. {
  125. (void) memcpy(ret + str_off,str[i],strlen(str[i]));
  126. str_off += strlen(str[i]);
  127. }
  128. ret[str_off] = '\0';
  129. }
  130. return ret;
  131. }
  132. /*
  133. int hexstr2byte(const char *bufin, int len, char *bufout)
  134. {
  135. int i = 0;
  136. unsigned char tmp2 = 0x0;
  137. unsigned int tmp = 0;
  138. char cc;
  139. if (NULL == bufin || len <= 0 || NULL == bufout)
  140. {
  141. return -1;
  142. }
  143. for(i = 0; i < len; i = i+2)
  144. {
  145. tmp2 = (unsigned char )bufin[i];
  146. tmp2 = tmp2 <= '9'?(unsigned char)(unsigned int)(tmp2-0x30):(unsigned char)(unsigned int)(tmp2-0x37);
  147. cc = bufin[i+1];
  148. tmp = (unsigned int)(unsigned char)cc;
  149. tmp = tmp <= '9'?(unsigned int)(tmp-0x30):(unsigned int)(tmp-0x37);
  150. bufout[i/2] =(tmp2<<4)|(tmp&0x0F);
  151. }
  152. return 0;
  153. }
  154. */
  155. int hexstr2byte(const char *buf, int len, char *bufout)
  156. {
  157. int ret = -1;
  158. int i = 0;
  159. uint8_t low;
  160. uint8_t high;
  161. if (NULL == buf || len <= 0 || NULL == bufout)
  162. {
  163. return ret;
  164. }
  165. ret = 0;
  166. for(i = 0; i < len; i = i+2)
  167. {
  168. if(((buf[i]) >= '0') && (buf[i] <= '9'))
  169. {
  170. high = (uint8_t)( buf[i] - '0');
  171. }
  172. else if((buf[i] >= 'A') && (buf[i] <= 'F'))
  173. {
  174. high = (uint8_t)( buf[i] - 'A') + 10;
  175. }
  176. else if((buf[i] >= 'a') && (buf[i] <= 'f'))
  177. {
  178. high = (uint8_t)( buf[i] - 'a') + 10;
  179. }
  180. else
  181. {
  182. ret = -1;
  183. break;
  184. }
  185. if(((buf[i+1]) >= '0') && (buf[i+1] <= '9'))
  186. {
  187. low = (uint8_t)( buf[i+1] - '0');
  188. }
  189. else if((buf[i+1] >= 'A') && (buf[i+1] <= 'F'))
  190. {
  191. low = (uint8_t)( buf[i+1] - 'A') + 10;
  192. }
  193. else if((buf[i+1] >= 'a') && (buf[i+1] <= 'f'))
  194. {
  195. low = (uint8_t)( buf[i+1] - 'a') + 10;
  196. }
  197. else
  198. {
  199. ret = -1;
  200. break;
  201. }
  202. bufout[i/2] = (char)((high<<4)|(low&0x0F));
  203. }
  204. return ret;
  205. }
  206. //make a byte to 2 ascii hex
  207. int byte2hexstr(uint8_t *bufin, int len, char *bufout)
  208. {
  209. int i = 0;
  210. uint8_t tmp_l = 0x0;
  211. uint8_t tmp_h = 0;
  212. if ((NULL == bufin )|| (len <= 0 )||( NULL == bufout))
  213. {
  214. return -1;
  215. }
  216. for(i = 0; i < len; i++)
  217. {
  218. tmp_h = (bufin[i]>>4)&0X0F;
  219. tmp_l = bufin[i] &0x0F;
  220. bufout[2*i] = (tmp_h > 9)? (tmp_h - 10 + 'a'):(tmp_h +'0');
  221. bufout[2*i + 1] = (tmp_l > 9)? (tmp_l - 10 + 'a'):(tmp_l +'0');
  222. }
  223. bufout[2*len] = '\0';
  224. return 0;
  225. }