1/* ********************************************************************* 2 * Broadcom Common Firmware Environment (CFE) 3 * 4 * String routines File: lib_string.c 5 * 6 * Some standard routines for messing with strings. 7 * 8 * Author: Mitch Lichtenberg 9 * 10 ********************************************************************* 11 * 12 * Copyright 2000,2001,2002,2003 13 * Broadcom Corporation. All rights reserved. 14 * 15 * This software is furnished under license and may be used and 16 * copied only in accordance with the following terms and 17 * conditions. Subject to these conditions, you may download, 18 * copy, install, use, modify and distribute modified or unmodified 19 * copies of this software in source and/or binary form. No title 20 * or ownership is transferred hereby. 21 * 22 * 1) Any source code used, modified or distributed must reproduce 23 * and retain this copyright notice and list of conditions 24 * as they appear in the source file. 25 * 26 * 2) No right is granted to use any trade name, trademark, or 27 * logo of Broadcom Corporation. The "Broadcom Corporation" 28 * name may not be used to endorse or promote products derived 29 * from this software without the prior written permission of 30 * Broadcom Corporation. 31 * 32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR 33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED 34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT, 38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 44 * THE POSSIBILITY OF SUCH DAMAGE. 45 ********************************************************************* */ 46 47 48 49 50#include "lib_types.h" 51#define _LIB_NO_MACROS_ 52#include "lib_string.h" 53 54char *lib_strcpy(char *dest,const char *src) 55{ 56 char *ptr = dest; 57 58 while (*src) *ptr++ = *src++; 59 *ptr = '\0'; 60 61 return dest; 62} 63 64char *lib_strncpy(char *dest,const char *src,size_t cnt) 65{ 66 char *ptr = dest; 67 68 while (*src && (cnt > 0)) { 69 *ptr++ = *src++; 70 cnt--; 71 } 72 73 while (cnt > 0) { 74 *ptr++ = 0; 75 cnt--; 76 } 77 78 return dest; 79} 80 81 82size_t lib_xstrncpy(char *dest,const char *src,size_t cnt) 83{ 84 char *ptr = dest; 85 size_t copied = 0; 86 87 while (*src && (cnt > 1)) { 88 *ptr++ = *src++; 89 cnt--; 90 copied++; 91 } 92 *ptr = '\0'; 93 94 return copied; 95} 96 97size_t lib_strlen(const char *str) 98{ 99 size_t cnt = 0; 100 101 while (*str) { 102 str++; 103 cnt++; 104 } 105 106 return cnt; 107} 108 109 110int lib_strcmp(const char *dest,const char *src) 111{ 112 while (*src && *dest) { 113 if (*dest < *src) return -1; 114 if (*dest > *src) return 1; 115 dest++; 116 src++; 117 } 118 119 if (*dest && !*src) return 1; 120 if (!*dest && *src) return -1; 121 return 0; 122} 123 124int lib_strcmpi(const char *dest,const char *src) 125{ 126 char dc,sc; 127 128 while (*src && *dest) { 129 dc = lib_toupper(*dest); 130 sc = lib_toupper(*src); 131 if (dc < sc) return -1; 132 if (dc > sc) return 1; 133 dest++; 134 src++; 135 } 136 137 if (*dest && !*src) return 1; 138 if (!*dest && *src) return -1; 139 return 0; 140} 141 142 143char *lib_strchr(const char *dest,int c) 144{ 145 while (*dest) { 146 if (*dest == c) return (char *) dest; 147 dest++; 148 } 149 return NULL; 150} 151 152char *lib_strnchr(const char *dest,int c,size_t cnt) 153{ 154 while (*dest && (cnt > 0)) { 155 if (*dest == c) return (char *) dest; 156 dest++; 157 cnt--; 158 } 159 return NULL; 160} 161 162char *lib_strrchr(const char *dest,int c) 163{ 164 char *ret = NULL; 165 166 while (*dest) { 167 if (*dest == c) ret = (char *) dest; 168 dest++; 169 } 170 171 return ret; 172} 173 174 175int lib_memcmp(const void *dest,const void *src,size_t cnt) 176{ 177 const unsigned char *d; 178 const unsigned char *s; 179 180 d = (const unsigned char *) dest; 181 s = (const unsigned char *) src; 182 183 while (cnt) { 184 if (*d < *s) return -1; 185 if (*d > *s) return 1; 186 d++; s++; cnt--; 187 } 188 189 return 0; 190} 191 192void *lib_memcpy(void *dest,const void *src,size_t cnt) 193{ 194 unsigned char *d; 195 const unsigned char *s; 196 197 d = (unsigned char *) dest; 198 s = (const unsigned char *) src; 199 200 while (cnt) { 201 *d++ = *s++; 202 cnt--; 203 } 204 205 return dest; 206} 207 208void *lib_memset(void *dest,int c,size_t cnt) 209{ 210 unsigned char *d; 211 212 d = dest; 213 214 while (cnt) { 215 *d++ = (unsigned char) c; 216 cnt--; 217 } 218 219 return d; 220} 221 222char lib_toupper(char c) 223{ 224 if ((c >= 'a') && (c <= 'z')) c -= 32; 225 return c; 226} 227 228void lib_strupr(char *str) 229{ 230 while (*str) { 231 *str = lib_toupper(*str); 232 str++; 233 } 234} 235 236char *lib_strcat(char *dest,const char *src) 237{ 238 char *ptr = dest; 239 240 while (*ptr) ptr++; 241 while (*src) *ptr++ = *src++; 242 *ptr = '\0'; 243 244 return dest; 245} 246 247#define isspace(x) (((x) == ' ') || ((x) == '\t')) 248 249char *lib_gettoken(char **ptr) 250{ 251 char *p = *ptr; 252 char *ret; 253 254 /* skip white space */ 255 256 while (*p && isspace(*p)) p++; 257 ret = p; 258 259 /* check for end of string */ 260 261 if (!*p) { 262 *ptr = p; 263 return NULL; 264 } 265 266 /* skip non-whitespace */ 267 268 while (*p) { 269 if (isspace(*p)) break; 270 271 /* do quoted strings */ 272 273 if (*p == '"') { 274 p++; 275 ret = p; 276 while (*p && (*p != '"')) p++; 277 if (*p == '"') *p = '\0'; 278 } 279 280 p++; 281 282 } 283 284 if (*p) { 285 *p++ = '\0'; 286 } 287 *ptr = p; 288 289 return ret; 290} 291 292 293int lib_atoi(const char *dest) 294{ 295 int x = 0; 296 int digit; 297 298 if ((*dest == '0') && (*(dest+1) == 'x')) { 299 return lib_xtoi(dest+2); 300 } 301 302 while (*dest) { 303 if ((*dest >= '0') && (*dest <= '9')) { 304 digit = *dest - '0'; 305 } 306 else { 307 break; 308 } 309 x *= 10; 310 x += digit; 311 dest++; 312 } 313 314 return x; 315} 316 317uint64_t lib_xtoq(const char *dest) 318{ 319 uint64_t x = 0; 320 unsigned int digit; 321 322 if ((*dest == '0') && (*(dest+1) == 'x')) dest += 2; 323 324 while (*dest) { 325 if ((*dest >= '0') && (*dest <= '9')) { 326 digit = *dest - '0'; 327 } 328 else if ((*dest >= 'A') && (*dest <= 'F')) { 329 digit = 10 + *dest - 'A'; 330 } 331 else if ((*dest >= 'a') && (*dest <= 'f')) { 332 digit = 10 + *dest - 'a'; 333 } 334 else { 335 break; 336 } 337 x *= 16; 338 x += digit; 339 dest++; 340 } 341 342 return x; 343} 344 345int lib_xtoi(const char *dest) 346{ 347 int x = 0; 348 int digit; 349 350 if ((*dest == '0') && (*(dest+1) == 'x')) dest += 2; 351 352 while (*dest) { 353 if ((*dest >= '0') && (*dest <= '9')) { 354 digit = *dest - '0'; 355 } 356 else if ((*dest >= 'A') && (*dest <= 'F')) { 357 digit = 10 + *dest - 'A'; 358 } 359 else if ((*dest >= 'a') && (*dest <= 'f')) { 360 digit = 10 + *dest - 'a'; 361 } 362 else { 363 break; 364 } 365 x *= 16; 366 x += digit; 367 dest++; 368 } 369 370 return x; 371} 372