UbixOS  2.0
def.c
Go to the documentation of this file.
1 
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 "net/opt.h"
62 #include "net/def.h"
63 
64 #include <string.h>
65 
66 #if BYTE_ORDER == LITTLE_ENDIAN
67 
68 #if !defined(lwip_htons)
69 
76  return ((u16_t) PP_HTONS(n));
77 }
78 #endif /* lwip_htons */
79 
80 #if !defined(lwip_htonl)
81 
87 u32_t
89 {
90  return ((u32_t) PP_HTONL(n));
91 }
92 #endif /* lwip_htonl */
93 
94 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
95 
96 #ifndef lwip_strnstr
97 
102 char*
103 lwip_strnstr(const char* buffer, const char* token, size_t n)
104 {
105  const char* p;
106  size_t tokenlen = strlen(token);
107  if (tokenlen == 0) {
108  return LWIP_CONST_CAST(char *, buffer);
109  }
110  for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
111  if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
112  return LWIP_CONST_CAST(char *, p);
113  }
114  }
115  return (NULL);
116 }
117 #endif
118 
119 #ifndef lwip_stricmp
120 
125 int
126 lwip_stricmp(const char* str1, const char* str2)
127 {
128  char c1, c2;
129 
130  do {
131  c1 = *str1++;
132  c2 = *str2++;
133  if (c1 != c2) {
134  char c1_upc = c1 | 0x20;
135  if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
136  /* characters are not equal an one is in the alphabet range:
137  downcase both chars and check again */
138  char c2_upc = c2 | 0x20;
139  if (c1_upc != c2_upc) {
140  /* still not equal */
141  /* don't care for < or > */
142  return (1);
143  }
144  } else {
145  /* characters are not equal but none is in the alphabet range */
146  return (1);
147  }
148  }
149  } while (c1 != 0);
150  return (0);
151 }
152 #endif
153 
154 #ifndef lwip_strnicmp
155 
160 int
161 lwip_strnicmp(const char* str1, const char* str2, size_t len)
162 {
163  char c1, c2;
164 
165  do {
166  c1 = *str1++;
167  c2 = *str2++;
168  if (c1 != c2) {
169  char c1_upc = c1 | 0x20;
170  if ((c1_upc >= 'a') && (c1_upc <= 'z')) {
171  /* characters are not equal an one is in the alphabet range:
172  downcase both chars and check again */
173  char c2_upc = c2 | 0x20;
174  if (c1_upc != c2_upc) {
175  /* still not equal */
176  /* don't care for < or > */
177  return (1);
178  }
179  } else {
180  /* characters are not equal but none is in the alphabet range */
181  return (1);
182  }
183  }
184  } while (len-- && c1 != 0);
185  return (0);
186 }
187 #endif
188 
189 #ifndef lwip_itoa
190 
195 void
196 lwip_itoa(char* result, size_t bufsize, int number)
197 {
198  const int base = 10;
199  char* ptr = result, *ptr1 = result, tmp_char;
200  int tmp_value;
201  LWIP_UNUSED_ARG(bufsize);
202 
203  do {
204  tmp_value = number;
205  number /= base;
206  *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - number * base)];
207  } while(number);
208 
209  /* Apply negative sign */
210  if (tmp_value < 0) {
211  *ptr++ = '-';
212  }
213  *ptr-- = '\0';
214  while(ptr1 < ptr) {
215  tmp_char = *ptr;
216  *ptr--= *ptr1;
217  *ptr1++ = tmp_char;
218  }
219 }
220 #endif
lwip_strnicmp
int lwip_strnicmp(const char *str1, const char *str2, size_t len)
Definition: def.c:161
opt.h
pbuf::len
u16_t len
Definition: pbuf.h:159
def.h
buffer
char * buffer
Definition: shell.c:47
u16_t
uint16_t u16_t
Definition: arch.h:124
string.h
u32_t
uint32_t u32_t
Definition: arch.h:126
strlen
int strlen(const char *str)
Definition: strlen.c:55
lwip_strnstr
char * lwip_strnstr(const char *buffer, const char *token, size_t n)
Definition: def.c:103
LWIP_UNUSED_ARG
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:315
lwip_htonl
u32_t lwip_htonl(u32_t n)
Definition: def.c:88
lwip_htons
u16_t lwip_htons(u16_t n)
Definition: def.c:75
lwip_itoa
void lwip_itoa(char *result, size_t bufsize, int number)
Definition: def.c:196
lwip_stricmp
int lwip_stricmp(const char *str1, const char *str2)
Definition: def.c:126
strncmp
int strncmp(const char *str1, const char *str2, int len)
PP_HTONL
#define PP_HTONL(x)
Definition: def.h:81
LWIP_CONST_CAST
#define LWIP_CONST_CAST(target_type, val)
Definition: arch.h:187
PP_HTONS
#define PP_HTONS(x)
Definition: def.h:79
NULL
#define NULL
Definition: fat_string.h:17