def.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @file
  3. * various utility macros
  4. */
  5. /*
  6. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Adam Dunkels <adam@sics.se>
  34. *
  35. */
  36. /**
  37. * @defgroup perf Performance measurement
  38. * @ingroup sys_layer
  39. * All defines related to this section must not be placed in lwipopts.h,
  40. * but in arch/perf.h!
  41. * Measurement calls made throughout lwip, these can be defined to nothing.
  42. * - PERF_START: start measuring something.
  43. * - PERF_STOP(x): stop measuring something, and record the result.
  44. */
  45. #ifndef LWIP_HDR_DEF_H
  46. #define LWIP_HDR_DEF_H
  47. /* arch.h might define NULL already */
  48. #include "lwip/arch.h"
  49. #include "lwip/opt.h"
  50. #if LWIP_PERF
  51. #include "arch/perf.h"
  52. #else /* LWIP_PERF */
  53. #define PERF_START /* null definition */
  54. #define PERF_STOP(x) /* null definition */
  55. #endif /* LWIP_PERF */
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. #define LWIP_MAX(x , y) (((x) > (y)) ? (x) : (y))
  60. #define LWIP_MIN(x , y) (((x) < (y)) ? (x) : (y))
  61. /* Get the number of entries in an array ('x' must NOT be a pointer!) */
  62. #define LWIP_ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0]))
  63. /** Create u32_t value from bytes */
  64. #define LWIP_MAKEU32(a,b,c,d) (((u32_t)((a) & 0xff) << 24) | \
  65. ((u32_t)((b) & 0xff) << 16) | \
  66. ((u32_t)((c) & 0xff) << 8) | \
  67. (u32_t)((d) & 0xff))
  68. #ifndef NULL
  69. #ifdef __cplusplus
  70. #define NULL 0
  71. #else
  72. #define NULL ((void *)0)
  73. #endif
  74. #endif
  75. #if BYTE_ORDER == BIG_ENDIAN
  76. #define lwip_htons(x) ((u16_t)(x))
  77. #define lwip_ntohs(x) ((u16_t)(x))
  78. #define lwip_htonl(x) ((u32_t)(x))
  79. #define lwip_ntohl(x) ((u32_t)(x))
  80. #define PP_HTONS(x) ((u16_t)(x))
  81. #define PP_NTOHS(x) ((u16_t)(x))
  82. #define PP_HTONL(x) ((u32_t)(x))
  83. #define PP_NTOHL(x) ((u32_t)(x))
  84. #else /* BYTE_ORDER != BIG_ENDIAN */
  85. #ifndef lwip_htons
  86. u16_t lwip_htons(u16_t x);
  87. #endif
  88. #define lwip_ntohs(x) lwip_htons(x)
  89. #ifndef lwip_htonl
  90. u32_t lwip_htonl(u32_t x);
  91. #endif
  92. #define lwip_ntohl(x) lwip_htonl(x)
  93. /* These macros should be calculated by the preprocessor and are used
  94. with compile-time constants only (so that there is no little-endian
  95. overhead at runtime). */
  96. #define PP_HTONS(x) ((u16_t)((((x) & (u16_t)0x00ffU) << 8) | (((x) & (u16_t)0xff00U) >> 8)))
  97. #define PP_NTOHS(x) PP_HTONS(x)
  98. #define PP_HTONL(x) ((((x) & (u32_t)0x000000ffUL) << 24) | \
  99. (((x) & (u32_t)0x0000ff00UL) << 8) | \
  100. (((x) & (u32_t)0x00ff0000UL) >> 8) | \
  101. (((x) & (u32_t)0xff000000UL) >> 24))
  102. #define PP_NTOHL(x) PP_HTONL(x)
  103. #endif /* BYTE_ORDER == BIG_ENDIAN */
  104. /* Provide usual function names as macros for users, but this can be turned off */
  105. #ifndef LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS
  106. #define htons(x) lwip_htons(x)
  107. #define ntohs(x) lwip_ntohs(x)
  108. #define htonl(x) lwip_htonl(x)
  109. #define ntohl(x) lwip_ntohl(x)
  110. #endif
  111. /* Functions that are not available as standard implementations.
  112. * In cc.h, you can #define these to implementations available on
  113. * your platform to save some code bytes if you use these functions
  114. * in your application, too.
  115. */
  116. #ifndef lwip_itoa
  117. /* This can be #defined to itoa() or snprintf(result, bufsize, "%d", number) depending on your platform */
  118. void lwip_itoa(char* result, size_t bufsize, int number);
  119. #endif
  120. #ifndef lwip_strnicmp
  121. /* This can be #defined to strnicmp() or strncasecmp() depending on your platform */
  122. int lwip_strnicmp(const char* str1, const char* str2, size_t len);
  123. #endif
  124. #ifndef lwip_stricmp
  125. /* This can be #defined to stricmp() or strcasecmp() depending on your platform */
  126. int lwip_stricmp(const char* str1, const char* str2);
  127. #endif
  128. #ifndef lwip_strnstr
  129. /* This can be #defined to strnstr() depending on your platform */
  130. char* lwip_strnstr(const char* buffer, const char* token, size_t n);
  131. #endif
  132. #ifndef lwip_strnistr
  133. /* This can be #defined to strnistr() depending on your platform */
  134. char* lwip_strnistr(const char* buffer, const char* token, size_t n);
  135. #endif
  136. #ifndef lwip_memcmp_consttime
  137. /* This could be #defined to something existing on your platform
  138. * The goal of this function is to compare memory with constant runtime in order to prevent
  139. * timing attacks to various parts in the stack.
  140. * To do that, in contrast to memcmp(), it only returns:
  141. * 0: equal
  142. * != 0: not equal
  143. */
  144. int lwip_memcmp_consttime(const void* s1, const void* s2, size_t len);
  145. #endif
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif /* LWIP_HDR_DEF_H */