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