1/* ********************************************************************* 2 * Broadcom Common Firmware Environment (CFE) 3 * 4 * Misc. library routines File: lib_misc.c 5 * 6 * Miscellaneous library routines. 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#include "lib_types.h" 49#include "lib_malloc.h" 50#define _LIB_NO_MACROS_ 51#include "lib_string.h" 52 53 54/* ********************************************************************* 55 * lib_parseipaddr(ipaddr,dest) 56 * 57 * Parse an IP address. 58 * 59 * Input parameters: 60 * ipaddr - string of IP address 61 * dest - pointer to 4 bytes to receive binary IP address 62 * 63 * Return value: 64 * 0 if ok 65 * -1 if ip address is invalid 66 ********************************************************************* */ 67 68int lib_parseipaddr(const char *ipaddr,uint8_t *dest) 69{ 70 int a,b,c,d; 71 char *x; 72 73 /* make sure it's all digits and dots. */ 74 x = (char *) ipaddr; 75 while (*x) { 76 if ((*x == '.') || ((*x >= '0') && (*x <= '9'))) { 77 x++; 78 continue; 79 } 80 return -1; 81 } 82 83 x = (char *) ipaddr; 84 a = lib_atoi(ipaddr); 85 x = lib_strchr(x,'.'); 86 if (!x) return -1; 87 b = lib_atoi(x+1); 88 x = lib_strchr(x+1,'.'); 89 if (!x) return -1; 90 c = lib_atoi(x+1); 91 x = lib_strchr(x+1,'.'); 92 if (!x) return -1; 93 d = lib_atoi(x+1); 94 95 if ((a < 0) || (a > 255)) return -1; 96 if ((b < 0) || (b > 255)) return -1; 97 if ((c < 0) || (c > 255)) return -1; 98 if ((d < 0) || (d > 255)) return -1; 99 100 dest[0] = (uint8_t) a; 101 dest[1] = (uint8_t) b; 102 dest[2] = (uint8_t) c; 103 dest[3] = (uint8_t) d; 104 105 return 0; 106} 107 108 109/* ********************************************************************* 110 * lib_lookup(list,str) 111 * 112 * Look up an element on a {string,value} list. 113 * 114 * Input parameters: 115 * list - list to search 116 * str - string to find on the list 117 * 118 * Return value: 119 * 0 if string was not found 120 * else number associated with this string 121 ********************************************************************* */ 122 123int lib_lookup(const cons_t *list,char *str) 124{ 125 while (list->str) { 126 if (lib_strcmp(list->str,str) == 0) return list->num; 127 list++; 128 } 129 130 return 0; 131 132} 133 134/* ********************************************************************* 135 * lib_findinlist(list,str) 136 * 137 * Like lib_lookup but returns cons structure instead of value 138 * 139 * Input parameters: 140 * list - list of associations 141 * str - what to find 142 * 143 * Return value: 144 * cons_t or null if not found 145 ********************************************************************* */ 146 147static const cons_t *lib_findinlist(const cons_t *list,char *str) 148{ 149 while (list->str) { 150 if (lib_strcmp(list->str,str) == 0) return list; 151 list++; 152 } 153 return NULL; 154} 155 156 157/* ********************************************************************* 158 * lib_setoptions(list,str,flags) 159 * 160 * Set or reset one or more bits in a flags variable based 161 * on the list of valid bits and a string containing what 162 * to change. flags starts off as a default value. 163 * 164 * The input string is a comma-separated list of options, 165 * optionally prefixed by "no_" or "no" to invert the 166 * sense of the option. negative values in the table 167 * remove options, positive add options (you can't use 168 * bit 31 as an option for this reason). 169 * 170 * Input parameters: 171 * list - list of valid options 172 * str - options to parse 173 * flags - pointer to variable to be modified 174 * 175 * Return value: 176 * number of options we did not understand, 0=ok 177 ********************************************************************* */ 178 179int lib_setoptions(const cons_t *list,char *str,unsigned int *flags) 180{ 181 char *dupstr; 182 char *x; 183 char *ptr; 184 const cons_t *val; 185 int newbits; 186 int errors = 0; 187 188 if (!list || !str || !flags) return 0; 189 190 dupstr = lib_strdup(str); 191 if (!dupstr) return 0; 192 193 ptr = dupstr; 194 195 while (*ptr) { 196 if ((x = lib_strchr(ptr,','))) { 197 *x = '\0'; 198 } 199 200 val = lib_findinlist(list,ptr); 201 newbits = 0; 202 if (!val) { 203 if (lib_memcmp(ptr,"no_",3) == 0) { 204 val = lib_findinlist(list,ptr+3); 205 } 206 else if (lib_memcmp(ptr,"no",2) == 0) { 207 val = lib_findinlist(list,ptr+2); 208 } 209 if (val) newbits = ~((unsigned int) (val->num)); 210 else errors++; 211 } 212 else { 213 newbits = (val->num); 214 } 215 216 /* if new bits are negative, it's an AND mask 217 otherwise it's an OR mask */ 218 219 if (newbits < 0) *flags &= (unsigned int) newbits; 220 else *flags |= (unsigned int) newbits; 221 222 if (x) ptr = x+1; 223 else break; 224 } 225 226 KFREE(dupstr); 227 228 return errors; 229} 230