def.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 (u16_t)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 (u32_t)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_stricmp
  112. /**
  113. * @ingroup sys_nonstandard
  114. * lwIP default implementation for stricmp() non-standard function.
  115. * This can be \#defined to stricmp() depending on your platform port.
  116. */
  117. int
  118. lwip_stricmp(const char* str1, const char* str2)
  119. {
  120. char c1, c2;
  121. do {
  122. c1 = *str1++;
  123. c2 = *str2++;
  124. if (c1 != c2) {
  125. char c1_upc = c1 | 0x20;
  126. if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
  127. /* characters are not equal an one is in the alphabet range:
  128. downcase both chars and check again */
  129. char c2_upc = c2 | 0x20;
  130. if (c1_upc != c2_upc) {
  131. /* still not equal */
  132. /* don't care for < or > */
  133. return 1;
  134. }
  135. } else {
  136. /* characters are not equal but none is in the alphabet range */
  137. return 1;
  138. }
  139. }
  140. } while (c1 != 0);
  141. return 0;
  142. }
  143. #endif
  144. #ifndef lwip_strnicmp
  145. /**
  146. * @ingroup sys_nonstandard
  147. * lwIP default implementation for strnicmp() non-standard function.
  148. * This can be \#defined to strnicmp() depending on your platform port.
  149. */
  150. int
  151. lwip_strnicmp(const char* str1, const char* str2, size_t len)
  152. {
  153. char c1, c2;
  154. do {
  155. c1 = *str1++;
  156. c2 = *str2++;
  157. if (c1 != c2) {
  158. char c1_upc = c1 | 0x20;
  159. if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
  160. /* characters are not equal an one is in the alphabet range:
  161. downcase both chars and check again */
  162. char c2_upc = c2 | 0x20;
  163. if (c1_upc != c2_upc) {
  164. /* still not equal */
  165. /* don't care for < or > */
  166. return 1;
  167. }
  168. } else {
  169. /* characters are not equal but none is in the alphabet range */
  170. return 1;
  171. }
  172. }
  173. } while (len-- && c1 != 0);
  174. return 0;
  175. }
  176. #endif
  177. #ifndef lwip_itoa
  178. /**
  179. * @ingroup sys_nonstandard
  180. * lwIP default implementation for itoa() non-standard function.
  181. * This can be \#defined to itoa() or snprintf(result, bufsize, "%d", number) depending on your platform port.
  182. */
  183. void
  184. lwip_itoa(char* result, size_t bufsize, int number)
  185. {
  186. const int base = 10;
  187. char* ptr = result, *ptr1 = result, tmp_char;
  188. int tmp_value;
  189. LWIP_UNUSED_ARG(bufsize);
  190. do {
  191. tmp_value = number;
  192. number /= base;
  193. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - number * base)];
  194. } while(number);
  195. /* Apply negative sign */
  196. if (tmp_value < 0) {
  197. *ptr++ = '-';
  198. }
  199. *ptr-- = '\0';
  200. while(ptr1 < ptr) {
  201. tmp_char = *ptr;
  202. *ptr--= *ptr1;
  203. *ptr1++ = tmp_char;
  204. }
  205. }
  206. #endif