prop_number.c revision 1.5 1 1.5 thorpej /* $NetBSD: prop_number.c,v 1.5 2006/08/22 21:21:23 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej #include <prop/prop_number.h>
40 1.1 thorpej #include "prop_object_impl.h"
41 1.1 thorpej
42 1.1 thorpej #if defined(_KERNEL)
43 1.1 thorpej #include <sys/inttypes.h>
44 1.1 thorpej #include <sys/systm.h>
45 1.1 thorpej #elif defined(_STANDALONE)
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <lib/libkern/libkern.h>
48 1.1 thorpej #else
49 1.1 thorpej #include <errno.h>
50 1.1 thorpej #include <stdlib.h>
51 1.1 thorpej #include <inttypes.h>
52 1.1 thorpej #endif
53 1.1 thorpej
54 1.1 thorpej struct _prop_number {
55 1.1 thorpej struct _prop_object pn_obj;
56 1.3 thorpej uint64_t pn_number;
57 1.1 thorpej };
58 1.1 thorpej
59 1.1 thorpej _PROP_POOL_INIT(_prop_number_pool, sizeof(struct _prop_number), "propnmbr")
60 1.1 thorpej
61 1.2 thorpej static void _prop_number_free(void *);
62 1.2 thorpej static boolean_t _prop_number_externalize(
63 1.2 thorpej struct _prop_object_externalize_context *,
64 1.2 thorpej void *);
65 1.2 thorpej static boolean_t _prop_number_equals(void *, void *);
66 1.2 thorpej
67 1.2 thorpej static const struct _prop_object_type _prop_object_type_number = {
68 1.2 thorpej .pot_type = PROP_TYPE_NUMBER,
69 1.2 thorpej .pot_free = _prop_number_free,
70 1.2 thorpej .pot_extern = _prop_number_externalize,
71 1.2 thorpej .pot_equals = _prop_number_equals,
72 1.2 thorpej };
73 1.2 thorpej
74 1.2 thorpej #define prop_object_is_number(x) \
75 1.5 thorpej ((x) != NULL && (x)->pn_obj.po_type == &_prop_object_type_number)
76 1.1 thorpej
77 1.1 thorpej static void
78 1.1 thorpej _prop_number_free(void *v)
79 1.1 thorpej {
80 1.1 thorpej
81 1.1 thorpej _PROP_POOL_PUT(_prop_number_pool, v);
82 1.1 thorpej }
83 1.1 thorpej
84 1.1 thorpej static boolean_t
85 1.1 thorpej _prop_number_externalize(struct _prop_object_externalize_context *ctx,
86 1.1 thorpej void *v)
87 1.1 thorpej {
88 1.1 thorpej prop_number_t pn = v;
89 1.1 thorpej char tmpstr[32];
90 1.1 thorpej
91 1.3 thorpej sprintf(tmpstr, "0x%" PRIx64, pn->pn_number);
92 1.1 thorpej
93 1.1 thorpej if (_prop_object_externalize_start_tag(ctx, "integer") == FALSE ||
94 1.1 thorpej _prop_object_externalize_append_cstring(ctx, tmpstr) == FALSE ||
95 1.1 thorpej _prop_object_externalize_end_tag(ctx, "integer") == FALSE)
96 1.1 thorpej return (FALSE);
97 1.1 thorpej
98 1.1 thorpej return (TRUE);
99 1.1 thorpej }
100 1.1 thorpej
101 1.2 thorpej static boolean_t
102 1.2 thorpej _prop_number_equals(void *v1, void *v2)
103 1.2 thorpej {
104 1.2 thorpej prop_number_t num1 = v1;
105 1.2 thorpej prop_number_t num2 = v2;
106 1.2 thorpej
107 1.4 thorpej if (! (prop_object_is_number(num1) &&
108 1.4 thorpej prop_object_is_number(num2)))
109 1.4 thorpej return (FALSE);
110 1.4 thorpej
111 1.2 thorpej return (num1->pn_number == num2->pn_number);
112 1.2 thorpej }
113 1.2 thorpej
114 1.1 thorpej static prop_number_t
115 1.3 thorpej _prop_number_alloc(uint64_t val)
116 1.1 thorpej {
117 1.1 thorpej prop_number_t pn;
118 1.1 thorpej
119 1.1 thorpej pn = _PROP_POOL_GET(_prop_number_pool);
120 1.1 thorpej if (pn != NULL) {
121 1.2 thorpej _prop_object_init(&pn->pn_obj, &_prop_object_type_number);
122 1.1 thorpej
123 1.1 thorpej pn->pn_number = val;
124 1.1 thorpej }
125 1.1 thorpej
126 1.1 thorpej return (pn);
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej /*
130 1.1 thorpej * prop_number_create_integer --
131 1.1 thorpej * Create a prop_number_t and initialize it with the
132 1.1 thorpej * provided integer value.
133 1.1 thorpej */
134 1.1 thorpej prop_number_t
135 1.3 thorpej prop_number_create_integer(uint64_t val)
136 1.1 thorpej {
137 1.1 thorpej
138 1.1 thorpej return (_prop_number_alloc(val));
139 1.1 thorpej }
140 1.1 thorpej
141 1.1 thorpej /*
142 1.1 thorpej * prop_number_copy --
143 1.1 thorpej * Copy a prop_number_t.
144 1.1 thorpej */
145 1.1 thorpej prop_number_t
146 1.1 thorpej prop_number_copy(prop_number_t opn)
147 1.1 thorpej {
148 1.1 thorpej
149 1.4 thorpej if (! prop_object_is_number(opn))
150 1.4 thorpej return (NULL);
151 1.1 thorpej
152 1.1 thorpej return (_prop_number_alloc(opn->pn_number));
153 1.1 thorpej }
154 1.1 thorpej
155 1.1 thorpej /*
156 1.1 thorpej * prop_number_size --
157 1.1 thorpej * Return the size, in bits, required to hold the value of
158 1.1 thorpej * the specified number.
159 1.1 thorpej */
160 1.1 thorpej int
161 1.1 thorpej prop_number_size(prop_number_t pn)
162 1.1 thorpej {
163 1.1 thorpej
164 1.4 thorpej if (! prop_object_is_number(pn))
165 1.4 thorpej return (0);
166 1.4 thorpej
167 1.1 thorpej if (pn->pn_number > UINT32_MAX)
168 1.1 thorpej return (64);
169 1.1 thorpej if (pn->pn_number > UINT16_MAX)
170 1.1 thorpej return (32);
171 1.1 thorpej if (pn->pn_number > UINT8_MAX)
172 1.1 thorpej return (16);
173 1.1 thorpej return (8);
174 1.1 thorpej }
175 1.1 thorpej
176 1.1 thorpej /*
177 1.1 thorpej * prop_number_integer_value --
178 1.1 thorpej * Get the integer value of a prop_number_t.
179 1.1 thorpej */
180 1.3 thorpej uint64_t
181 1.1 thorpej prop_number_integer_value(prop_number_t pn)
182 1.1 thorpej {
183 1.1 thorpej
184 1.4 thorpej /*
185 1.4 thorpej * XXX Impossible to distinguish between "not a prop_number_t"
186 1.4 thorpej * XXX and "prop_number_t has a value of 0".
187 1.4 thorpej */
188 1.4 thorpej if (! prop_object_is_number(pn))
189 1.4 thorpej return (0);
190 1.4 thorpej
191 1.1 thorpej return (pn->pn_number);
192 1.1 thorpej }
193 1.1 thorpej
194 1.1 thorpej /*
195 1.1 thorpej * prop_number_equals --
196 1.1 thorpej * Return TRUE if two numbers are equivalent.
197 1.1 thorpej */
198 1.1 thorpej boolean_t
199 1.1 thorpej prop_number_equals(prop_number_t num1, prop_number_t num2)
200 1.1 thorpej {
201 1.1 thorpej
202 1.2 thorpej return (_prop_number_equals(num1, num2));
203 1.1 thorpej }
204 1.1 thorpej
205 1.1 thorpej /*
206 1.1 thorpej * prop_number_equals_integer --
207 1.1 thorpej * Return TRUE if the number is equivalent to the specified integer.
208 1.1 thorpej */
209 1.1 thorpej boolean_t
210 1.3 thorpej prop_number_equals_integer(prop_number_t pn, uint64_t val)
211 1.1 thorpej {
212 1.1 thorpej
213 1.4 thorpej if (! prop_object_is_number(pn))
214 1.4 thorpej return (FALSE);
215 1.4 thorpej
216 1.1 thorpej return (pn->pn_number == val);
217 1.1 thorpej }
218 1.1 thorpej
219 1.1 thorpej /*
220 1.1 thorpej * _prop_number_internalize --
221 1.1 thorpej * Parse a <number>...</number> and return the object created from
222 1.1 thorpej * the external representation.
223 1.1 thorpej */
224 1.1 thorpej prop_object_t
225 1.1 thorpej _prop_number_internalize(struct _prop_object_internalize_context *ctx)
226 1.1 thorpej {
227 1.3 thorpej uint64_t val = 0;
228 1.1 thorpej char *cp;
229 1.1 thorpej
230 1.1 thorpej /* No attributes, no empty elements. */
231 1.1 thorpej if (ctx->poic_tagattr != NULL || ctx->poic_is_empty_element)
232 1.1 thorpej return (NULL);
233 1.1 thorpej
234 1.1 thorpej #ifndef _KERNEL
235 1.1 thorpej errno = 0;
236 1.1 thorpej #endif
237 1.1 thorpej val = strtoumax(ctx->poic_cp, &cp, 0);
238 1.1 thorpej #ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
239 1.1 thorpej if (val == UINTMAX_MAX && errno == ERANGE)
240 1.1 thorpej return (NULL);
241 1.1 thorpej #endif
242 1.1 thorpej ctx->poic_cp = cp;
243 1.1 thorpej
244 1.1 thorpej if (_prop_object_internalize_find_tag(ctx, "integer",
245 1.1 thorpej _PROP_TAG_TYPE_END) == FALSE)
246 1.1 thorpej return (NULL);
247 1.1 thorpej
248 1.1 thorpej return (_prop_number_alloc(val));
249 1.1 thorpej }
250