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 */
24
25 /*
26 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without modification,
30 * are permitted provided that the following conditions are met:
31 *
32 * 1. Redistributions of source code must retain the above copyright notice,
33 * this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright notice,
35 * this list of conditions and the following disclaimer in the documentation
36 * and/or other materials provided with the distribution.
37 * 3. The name of the author may not be used to endorse or promote products
38 * derived from this software without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
43 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
45 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
48 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
49 * OF SUCH DAMAGE.
50 *
51 * This file is part of the lwIP TCP/IP stack.
52 *
53 * Author: Simon Goldschmidt
54 *
55 */
56
57 #include "lwip/opt.h"
58 #include "lwip/def.h"
59 #include "aos/kernel.h"
60
61
62 #include <string.h>
63
64 #if BYTE_ORDER == LITTLE_ENDIAN
65
66 #if !defined(lwip_htons)
67 /**
68 * Convert an u16_t from host- to network byte order.
69 *
70 * @param n u16_t in host byte order
71 * @return n in network byte order
72 */
73 u16_t
lwip_htons(u16_t n)74 lwip_htons(u16_t n)
75 {
76 return (u16_t)PP_HTONS(n);
77 }
78
79 #endif /* lwip_htons */
80
81 #if !defined(lwip_htonl)
82 /**
83 * Convert an u32_t from host- to network byte order.
84 *
85 * @param n u32_t in host byte order
86 * @return n in network byte order
87 */
88 u32_t
lwip_htonl(u32_t n)89 lwip_htonl(u32_t n)
90 {
91 return (u32_t)PP_HTONL(n);
92 }
93
94 #endif /* lwip_htonl */
95
96 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
97
98 #ifndef lwip_strnstr
99 /**
100 * @ingroup sys_nonstandard
101 * lwIP default implementation for strnstr() non-standard function.
102 * This can be \#defined to strnstr() depending on your platform port.
103 */
104 char*
lwip_strnstr(const char * buffer,const char * token,size_t n)105 lwip_strnstr(const char* buffer, const char* token, size_t n)
106 {
107 const char* p;
108 int tokenlen = (int)strlen(token);
109 if (tokenlen == 0) {
110 return (char *)(size_t)buffer;
111 }
112 for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
113 if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
114 return (char *)(size_t)p;
115 }
116 }
117 return NULL;
118 }
119 #endif
120
121 #ifndef lwip_stricmp
122 /**
123 * @ingroup sys_nonstandard
124 * lwIP default implementation for stricmp() non-standard function.
125 * This can be \#defined to stricmp() depending on your platform port.
126 */
127 int
lwip_stricmp(const char * str1,const char * str2)128 lwip_stricmp(const char* str1, const char* str2)
129 {
130 char c1, c2;
131
132 do {
133 c1 = *str1++;
134 c2 = *str2++;
135 if (c1 != c2) {
136 char c1_upc = c1 | 0x20;
137 if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
138 /* characters are not equal an one is in the alphabet range:
139 downcase both chars and check again */
140 char c2_upc = c2 | 0x20;
141 if (c1_upc != c2_upc) {
142 /* still not equal */
143 /* don't care for < or > */
144 return 1;
145 }
146 } else {
147 /* characters are not equal but none is in the alphabet range */
148 return 1;
149 }
150 }
151 } while (c1 != 0);
152 return 0;
153 }
154 #endif
155
156 #ifndef lwip_strnicmp
157 /**
158 * @ingroup sys_nonstandard
159 * lwIP default implementation for strnicmp() non-standard function.
160 * This can be \#defined to strnicmp() depending on your platform port.
161 */
162 int
lwip_strnicmp(const char * str1,const char * str2,size_t len)163 lwip_strnicmp(const char* str1, const char* str2, size_t len)
164 {
165 char c1, c2;
166
167 do {
168 c1 = *str1++;
169 c2 = *str2++;
170 if (c1 != c2) {
171 char c1_upc = c1 | 0x20;
172 if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
173 /* characters are not equal an one is in the alphabet range:
174 downcase both chars and check again */
175 char c2_upc = c2 | 0x20;
176 if (c1_upc != c2_upc) {
177 /* still not equal */
178 /* don't care for < or > */
179 return 1;
180 }
181 } else {
182 /* characters are not equal but none is in the alphabet range */
183 return 1;
184 }
185 }
186 } while (len-- && c1 != 0);
187 return 0;
188 }
189 #endif
190
191 #ifndef lwip_itoa
192 /**
193 * @ingroup sys_nonstandard
194 * lwIP default implementation for itoa() non-standard function.
195 * This can be \#defined to itoa() or snprintf(result, bufsize, "%d", number) depending on your platform port.
196 */
197 void
lwip_itoa(char * result,size_t bufsize,int number)198 lwip_itoa(char* result, size_t bufsize, int number)
199 {
200 const int base = 10;
201 char* ptr = result, *ptr1 = result, tmp_char;
202 int tmp_value;
203 LWIP_UNUSED_ARG(bufsize);
204
205 do {
206 tmp_value = number;
207 number /= base;
208 *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - number * base)];
209 } while(number);
210
211 /* Apply negative sign */
212 if (tmp_value < 0) {
213 *ptr++ = '-';
214 }
215 *ptr-- = '\0';
216 while(ptr1 < ptr) {
217 tmp_char = *ptr;
218 *ptr--= *ptr1;
219 *ptr1++ = tmp_char;
220 }
221 }
222 #endif
223