The Pedigree Project 0.1
def.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
48/*
49 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
50 * All rights reserved.
51 *
52 * Redistribution and use in source and binary forms, with or without modification,
53 * are permitted provided that the following conditions are met:
54 *
55 * 1. Redistributions of source code must retain the above copyright notice,
56 * this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright notice,
58 * this list of conditions and the following disclaimer in the documentation
59 * and/or other materials provided with the distribution.
60 * 3. The name of the author may not be used to endorse or promote products
61 * derived from this software without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
64 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
65 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
66 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
68 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
71 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
72 * OF SUCH DAMAGE.
73 *
74 * This file is part of the lwIP TCP/IP stack.
75 *
76 * Author: Simon Goldschmidt
77 *
78 */
79
80#include "lwip/opt.h"
81#include "lwip/def.h"
82
83#include <string.h>
84
85#if BYTE_ORDER == LITTLE_ENDIAN
86
87#if !defined(lwip_htons)
94u16_t
95lwip_htons(u16_t n)
96{
97 return (u16_t)PP_HTONS(n);
98}
99#endif /* lwip_htons */
100
101#if !defined(lwip_htonl)
108u32_t
109lwip_htonl(u32_t n)
110{
111 return (u32_t)PP_HTONL(n);
112}
113#endif /* lwip_htonl */
114
115#endif /* BYTE_ORDER == LITTLE_ENDIAN */
116
117#ifndef lwip_strnstr
123char*
124lwip_strnstr(const char* buffer, const char* token, size_t n)
125{
126 const char* p;
127 size_t tokenlen = strlen(token);
128 if (tokenlen == 0) {
129 return LWIP_CONST_CAST(char *, buffer);
130 }
131 for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
132 if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
133 return LWIP_CONST_CAST(char *, p);
134 }
135 }
136 return NULL;
137}
138#endif
139
140#ifndef lwip_stricmp
146int
147lwip_stricmp(const char* str1, const char* str2)
148{
149 char c1, c2;
150
151 do {
152 c1 = *str1++;
153 c2 = *str2++;
154 if (c1 != c2) {
155 char c1_upc = c1 | 0x20;
156 if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
157 /* characters are not equal an one is in the alphabet range:
158 downcase both chars and check again */
159 char c2_upc = c2 | 0x20;
160 if (c1_upc != c2_upc) {
161 /* still not equal */
162 /* don't care for < or > */
163 return 1;
164 }
165 } else {
166 /* characters are not equal but none is in the alphabet range */
167 return 1;
168 }
169 }
170 } while (c1 != 0);
171 return 0;
172}
173#endif
174
175#ifndef lwip_strnicmp
181int
182lwip_strnicmp(const char* str1, const char* str2, size_t len)
183{
184 char c1, c2;
185
186 do {
187 c1 = *str1++;
188 c2 = *str2++;
189 if (c1 != c2) {
190 char c1_upc = c1 | 0x20;
191 if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
192 /* characters are not equal an one is in the alphabet range:
193 downcase both chars and check again */
194 char c2_upc = c2 | 0x20;
195 if (c1_upc != c2_upc) {
196 /* still not equal */
197 /* don't care for < or > */
198 return 1;
199 }
200 } else {
201 /* characters are not equal but none is in the alphabet range */
202 return 1;
203 }
204 }
205 } while (len-- && c1 != 0);
206 return 0;
207}
208#endif
209
210#ifndef lwip_itoa
216void
217lwip_itoa(char* result, size_t bufsize, int number)
218{
219 const int base = 10;
220 char* ptr = result, *ptr1 = result, tmp_char;
221 int tmp_value;
222 LWIP_UNUSED_ARG(bufsize);
223
224 do {
225 tmp_value = number;
226 number /= base;
227 *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - number * base)];
228 } while(number);
229
230 /* Apply negative sign */
231 if (tmp_value < 0) {
232 *ptr++ = '-';
233 }
234 *ptr-- = '\0';
235 while(ptr1 < ptr) {
236 tmp_char = *ptr;
237 *ptr--= *ptr1;
238 *ptr1++ = tmp_char;
239 }
240}
241#endif
#define LWIP_UNUSED_ARG(x)
Definition arch.h:327
#define LWIP_CONST_CAST(target_type, val)
Definition arch.h:199
int lwip_stricmp(const char *str1, const char *str2)
Definition def.c:147
int lwip_strnicmp(const char *str1, const char *str2, size_t len)
Definition def.c:182
char * lwip_strnstr(const char *buffer, const char *token, size_t n)
Definition def.c:124
void lwip_itoa(char *result, size_t bufsize, int number)
Definition def.c:217