1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * Misc. library routines File: lib_misc.c
6 * Miscellaneous library routines.
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
10 *********************************************************************
12 * Copyright 2000,2001,2002,2003
13 * Broadcom Corporation. All rights reserved.
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.
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.
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.
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 ********************************************************************* */
48 #include "lib_types.h"
49 #include "lib_malloc.h"
50 #define _LIB_NO_MACROS_
51 #include "lib_string.h"
54 /* *********************************************************************
55 * lib_parseipaddr(ipaddr,dest)
57 * Parse an IP address.
60 * ipaddr - string of IP address
61 * dest - pointer to 4 bytes to receive binary IP address
65 * -1 if ip address is invalid
66 ********************************************************************* */
68 int lib_parseipaddr(const char *ipaddr
,uint8_t *dest
)
73 /* make sure it's all digits and dots. */
76 if ((*x
== '.') || ((*x
>= '0') && (*x
<= '9'))) {
85 x
= lib_strchr(x
,'.');
88 x
= lib_strchr(x
+1,'.');
91 x
= lib_strchr(x
+1,'.');
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;
100 dest
[0] = (uint8_t) a
;
101 dest
[1] = (uint8_t) b
;
102 dest
[2] = (uint8_t) c
;
103 dest
[3] = (uint8_t) d
;
109 /* *********************************************************************
110 * lib_lookup(list,str)
112 * Look up an element on a {string,value} list.
115 * list - list to search
116 * str - string to find on the list
119 * 0 if string was not found
120 * else number associated with this string
121 ********************************************************************* */
123 int lib_lookup(const cons_t
*list
,char *str
)
126 if (lib_strcmp(list
->str
,str
) == 0) return list
->num
;
134 /* *********************************************************************
135 * lib_findinlist(list,str)
137 * Like lib_lookup but returns cons structure instead of value
140 * list - list of associations
144 * cons_t or null if not found
145 ********************************************************************* */
147 static const cons_t
*lib_findinlist(const cons_t
*list
,char *str
)
150 if (lib_strcmp(list
->str
,str
) == 0) return list
;
157 /* *********************************************************************
158 * lib_setoptions(list,str,flags)
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.
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).
171 * list - list of valid options
172 * str - options to parse
173 * flags - pointer to variable to be modified
176 * number of options we did not understand, 0=ok
177 ********************************************************************* */
179 int lib_setoptions(const cons_t
*list
,char *str
,unsigned int *flags
)
188 if (!list
|| !str
|| !flags
) return 0;
190 dupstr
= lib_strdup(str
);
191 if (!dupstr
) return 0;
196 if ((x
= lib_strchr(ptr
,','))) {
200 val
= lib_findinlist(list
,ptr
);
203 if (lib_memcmp(ptr
,"no_",3) == 0) {
204 val
= lib_findinlist(list
,ptr
+3);
206 else if (lib_memcmp(ptr
,"no",2) == 0) {
207 val
= lib_findinlist(list
,ptr
+2);
209 if (val
) newbits
= ~((unsigned int) (val
->num
));
213 newbits
= (val
->num
);
216 /* if new bits are negative, it's an AND mask
217 otherwise it's an OR mask */
219 if (newbits
< 0) *flags
&= (unsigned int) newbits
;
220 else *flags
|= (unsigned int) newbits
;