sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
def.c
Go to the documentation of this file.
1
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)
75u16_t
76lwip_htons(u16_t n)
77{
78 return PP_HTONS(n);
79}
80#endif /* lwip_htons */
81
82#if !defined(lwip_htonl)
89u32_t
90lwip_htonl(u32_t n)
91{
92 return PP_HTONL(n);
93}
94#endif /* lwip_htonl */
95
96#endif /* BYTE_ORDER == LITTLE_ENDIAN */
97
98#ifndef lwip_strnstr
104char *
105lwip_strnstr(const char *buffer, const char *token, size_t n)
106{
107 const char *p;
108 size_t tokenlen = strlen(token);
109 if (tokenlen == 0) {
110 return LWIP_CONST_CAST(char *, buffer);
111 }
112 for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
113 if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
114 return LWIP_CONST_CAST(char *, p);
115 }
116 }
117 return NULL;
118}
119#endif
120
121#ifndef lwip_strnistr
127char *
128lwip_strnistr(const char *buffer, const char *token, size_t n)
129{
130 const char *p;
131 size_t tokenlen = strlen(token);
132 if (tokenlen == 0) {
133 return LWIP_CONST_CAST(char *, buffer);
134 }
135 for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
136 if (lwip_strnicmp(p, token, tokenlen) == 0) {
137 return LWIP_CONST_CAST(char *, p);
138 }
139 }
140 return NULL;
141}
142#endif
143
144#ifndef lwip_stricmp
150int
151lwip_stricmp(const char *str1, const char *str2)
152{
153 char c1, c2;
154
155 do {
156 c1 = *str1++;
157 c2 = *str2++;
158 if (c1 != c2) {
159 char c1_upc = c1 | 0x20;
160 if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
161 /* characters are not equal an one is in the alphabet range:
162 downcase both chars and check again */
163 char c2_upc = c2 | 0x20;
164 if (c1_upc != c2_upc) {
165 /* still not equal */
166 /* don't care for < or > */
167 return 1;
168 }
169 } else {
170 /* characters are not equal but none is in the alphabet range */
171 return 1;
172 }
173 }
174 } while (c1 != 0);
175 return 0;
176}
177#endif
178
179#ifndef lwip_strnicmp
185int
186lwip_strnicmp(const char *str1, const char *str2, size_t len)
187{
188 char c1, c2;
189
190 do {
191 c1 = *str1++;
192 c2 = *str2++;
193 if (c1 != c2) {
194 char c1_upc = c1 | 0x20;
195 if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
196 /* characters are not equal an one is in the alphabet range:
197 downcase both chars and check again */
198 char c2_upc = c2 | 0x20;
199 if (c1_upc != c2_upc) {
200 /* still not equal */
201 /* don't care for < or > */
202 return 1;
203 }
204 } else {
205 /* characters are not equal but none is in the alphabet range */
206 return 1;
207 }
208 }
209 len--;
210 } while ((len != 0) && (c1 != 0));
211 return 0;
212}
213#endif
214
215#ifndef lwip_itoa
221void
222lwip_itoa(char *result, size_t bufsize, int number)
223{
224 char *res = result;
225 char *tmp = result + bufsize - 1;
226 int n = (number >= 0) ? number : -number;
227
228 /* handle invalid bufsize */
229 if (bufsize < 2) {
230 if (bufsize == 1) {
231 *result = 0;
232 }
233 return;
234 }
235
236 /* First, add sign */
237 if (number < 0) {
238 *res++ = '-';
239 }
240 /* Then create the string from the end and stop if buffer full,
241 and ensure output string is zero terminated */
242 *tmp = 0;
243 while ((n != 0) && (tmp > res)) {
244 char val = (char)('0' + (n % 10));
245 tmp--;
246 *tmp = val;
247 n = n / 10;
248 }
249 if (n) {
250 /* buffer is too small */
251 *result = 0;
252 return;
253 }
254 if (*tmp == 0) {
255 /* Nothing added? */
256 *res++ = '0';
257 *res++ = 0;
258 return;
259 }
260 /* move from temporary buffer to output buffer (sign is not moved) */
261 memmove(res, tmp, (size_t)((result + bufsize) - tmp));
262}
263#endif
264
265#ifndef lwip_memcmp_consttime
274int lwip_memcmp_consttime(const void* s1, const void* s2, size_t len)
275{
276 size_t i;
277 const unsigned char* a1 = (const unsigned char*)s1;
278 const unsigned char* a2 = (const unsigned char*)s2;
279 unsigned char ret = 0;
280
281 for (i = 0; i < len; i++) {
282 ret |= a1[i] ^ a2[i];
283 }
284 return ret;
285}
286#endif
#define LWIP_CONST_CAST(target_type, val)
Definition arch.h:240
int lwip_stricmp(const char *str1, const char *str2)
Definition def.c:151
int lwip_memcmp_consttime(const void *s1, const void *s2, size_t len)
Definition def.c:274
int lwip_strnicmp(const char *str1, const char *str2, size_t len)
Definition def.c:186
char * lwip_strnstr(const char *buffer, const char *token, size_t n)
Definition def.c:105
char * lwip_strnistr(const char *buffer, const char *token, size_t n)
Definition def.c:128
void lwip_itoa(char *result, size_t bufsize, int number)
Definition def.c:222