def.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @file
  3. * Common functions used throughout the stack.
  4. *
  5. * These are reference implementations of the byte swapping functions.
  6. * Again with the aim of being simple, correct and fully portable.
  7. * Byte swapping is the second thing you would want to optimize. You will
  8. * need to port it to your architecture and in your cc.h:
  9. *
  10. * \#define lwip_htons(x) your_htons
  11. * \#define lwip_htonl(x) your_htonl
  12. *
  13. * Note lwip_ntohs() and lwip_ntohl() are merely references to the htonx counterparts.
  14. *
  15. * If you \#define them to htons() and htonl(), you should
  16. * \#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS to prevent lwIP from
  17. * defining htonx/ntohx compatibility macros.
  18. * @defgroup sys_nonstandard Non-standard functions
  19. * @ingroup sys_layer
  20. * lwIP provides default implementations for non-standard functions.
  21. * These can be mapped to OS functions to reduce code footprint if desired.
  22. * All defines related to this section must not be placed in lwipopts.h,
  23. * but in arch/cc.h!
  24. * These options cannot be \#defined in lwipopts.h since they are not options
  25. * of lwIP itself, but options of the lwIP port to your system.
  26. */
  27. /*
  28. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  29. * All rights reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without modification,
  32. * are permitted provided that the following conditions are met:
  33. *
  34. * 1. Redistributions of source code must retain the above copyright notice,
  35. * this list of conditions and the following disclaimer.
  36. * 2. Redistributions in binary form must reproduce the above copyright notice,
  37. * this list of conditions and the following disclaimer in the documentation
  38. * and/or other materials provided with the distribution.
  39. * 3. The name of the author may not be used to endorse or promote products
  40. * derived from this software without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  43. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  44. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  45. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  46. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  47. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  49. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  50. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  51. * OF SUCH DAMAGE.
  52. *
  53. * This file is part of the lwIP TCP/IP stack.
  54. *
  55. * Author: Simon Goldschmidt
  56. *
  57. */
  58. #include "lwip/opt.h"
  59. #include "lwip/def.h"
  60. #include <string.h>
  61. #if BYTE_ORDER == LITTLE_ENDIAN
  62. #if !defined(lwip_htons)
  63. /**
  64. * Convert an u16_t from host- to network byte order.
  65. *
  66. * @param n u16_t in host byte order
  67. * @return n in network byte order
  68. */
  69. u16_t
  70. lwip_htons(u16_t n)
  71. {
  72. return PP_HTONS(n);
  73. }
  74. #endif /* lwip_htons */
  75. #if !defined(lwip_htonl)
  76. /**
  77. * Convert an u32_t from host- to network byte order.
  78. *
  79. * @param n u32_t in host byte order
  80. * @return n in network byte order
  81. */
  82. u32_t
  83. lwip_htonl(u32_t n)
  84. {
  85. return PP_HTONL(n);
  86. }
  87. #endif /* lwip_htonl */
  88. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  89. #ifndef lwip_strnstr
  90. /**
  91. * @ingroup sys_nonstandard
  92. * lwIP default implementation for strnstr() non-standard function.
  93. * This can be \#defined to strnstr() depending on your platform port.
  94. */
  95. char *
  96. lwip_strnstr(const char *buffer, const char *token, size_t n)
  97. {
  98. const char *p;
  99. size_t tokenlen = strlen(token);
  100. if (tokenlen == 0) {
  101. return LWIP_CONST_CAST(char *, buffer);
  102. }
  103. for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
  104. if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
  105. return LWIP_CONST_CAST(char *, p);
  106. }
  107. }
  108. return NULL;
  109. }
  110. #endif
  111. #ifndef lwip_strnistr
  112. /**
  113. * @ingroup sys_nonstandard
  114. * lwIP default implementation for strnistr() non-standard function.
  115. * This can be \#defined to strnistr() depending on your platform port.
  116. */
  117. char *
  118. lwip_strnistr(const char *buffer, const char *token, size_t n)
  119. {
  120. const char *p;
  121. size_t tokenlen = strlen(token);
  122. if (tokenlen == 0) {
  123. return LWIP_CONST_CAST(char *, buffer);
  124. }
  125. for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
  126. if (lwip_strnicmp(p, token, tokenlen) == 0) {
  127. return LWIP_CONST_CAST(char *, p);
  128. }
  129. }
  130. return NULL;
  131. }
  132. #endif
  133. #ifndef lwip_stricmp
  134. /**
  135. * @ingroup sys_nonstandard
  136. * lwIP default implementation for stricmp() non-standard function.
  137. * This can be \#defined to stricmp() depending on your platform port.
  138. */
  139. int
  140. lwip_stricmp(const char *str1, const char *str2)
  141. {
  142. char c1, c2;
  143. do {
  144. c1 = *str1++;
  145. c2 = *str2++;
  146. if (c1 != c2) {
  147. char c1_upc = c1 | 0x20;
  148. if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
  149. /* characters are not equal an one is in the alphabet range:
  150. downcase both chars and check again */
  151. char c2_upc = c2 | 0x20;
  152. if (c1_upc != c2_upc) {
  153. /* still not equal */
  154. /* don't care for < or > */
  155. return 1;
  156. }
  157. } else {
  158. /* characters are not equal but none is in the alphabet range */
  159. return 1;
  160. }
  161. }
  162. } while (c1 != 0);
  163. return 0;
  164. }
  165. #endif
  166. #ifndef lwip_strnicmp
  167. /**
  168. * @ingroup sys_nonstandard
  169. * lwIP default implementation for strnicmp() non-standard function.
  170. * This can be \#defined to strnicmp() depending on your platform port.
  171. */
  172. int
  173. lwip_strnicmp(const char *str1, const char *str2, size_t len)
  174. {
  175. char c1, c2;
  176. do {
  177. c1 = *str1++;
  178. c2 = *str2++;
  179. if (c1 != c2) {
  180. char c1_upc = c1 | 0x20;
  181. if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
  182. /* characters are not equal an one is in the alphabet range:
  183. downcase both chars and check again */
  184. char c2_upc = c2 | 0x20;
  185. if (c1_upc != c2_upc) {
  186. /* still not equal */
  187. /* don't care for < or > */
  188. return 1;
  189. }
  190. } else {
  191. /* characters are not equal but none is in the alphabet range */
  192. return 1;
  193. }
  194. }
  195. len--;
  196. } while ((len != 0) && (c1 != 0));
  197. return 0;
  198. }
  199. #endif
  200. #ifndef lwip_itoa
  201. /**
  202. * @ingroup sys_nonstandard
  203. * lwIP default implementation for itoa() non-standard function.
  204. * This can be \#defined to itoa() or snprintf(result, bufsize, "%d", number) depending on your platform port.
  205. */
  206. void
  207. lwip_itoa(char *result, size_t bufsize, int number)
  208. {
  209. char *res = result;
  210. char *tmp = result + bufsize - 1;
  211. int n = (number >= 0) ? number : -number;
  212. /* handle invalid bufsize */
  213. if (bufsize < 2) {
  214. if (bufsize == 1) {
  215. *result = 0;
  216. }
  217. return;
  218. }
  219. /* First, add sign */
  220. if (number < 0) {
  221. *res++ = '-';
  222. }
  223. /* Then create the string from the end and stop if buffer full,
  224. and ensure output string is zero terminated */
  225. *tmp = 0;
  226. while ((n != 0) && (tmp > res)) {
  227. char val = (char)('0' + (n % 10));
  228. tmp--;
  229. *tmp = val;
  230. n = n / 10;
  231. }
  232. if (n) {
  233. /* buffer is too small */
  234. *result = 0;
  235. return;
  236. }
  237. if (*tmp == 0) {
  238. /* Nothing added? */
  239. *res++ = '0';
  240. *res++ = 0;
  241. return;
  242. }
  243. /* move from temporary buffer to output buffer (sign is not moved) */
  244. memmove(res, tmp, (size_t)((result + bufsize) - tmp));
  245. }
  246. #endif
  247. #ifndef lwip_memcmp_consttime
  248. /**
  249. * @ingroup sys_nonstandard
  250. * The goal of this function is to compare memory with constant runtime in order to prevent
  251. * timing attacks to various parts in the stack.
  252. * To do that, in contrast to memcmp(), it only returns:
  253. * 0: equal
  254. * != 0: not equal
  255. */
  256. int lwip_memcmp_consttime(const void* s1, const void* s2, size_t len)
  257. {
  258. size_t i;
  259. const unsigned char* a1 = s1;
  260. const unsigned char* a2 = s2;
  261. unsigned char ret = 0;
  262. for (i = 0; i < len; i++) {
  263. ret |= a1[i] ^ a2[i];
  264. }
  265. return ret;
  266. }
  267. #endif