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