| 1 | /* $NetBSD: prop_array_util.c,v 1.5 2016/05/31 09:29:25 pgoyette Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 2006 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Jason R. Thorpe. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Utility routines to make it more convenient to work with values |
| 34 | * stored in array. |
| 35 | * |
| 36 | * Note: There is no special magic going on here. We use the standard |
| 37 | * proplib(3) APIs to do all of this work. Any application could do |
| 38 | * exactly what we're doing here. |
| 39 | */ |
| 40 | |
| 41 | #include "prop_object_impl.h" /* hide kernel vs. not-kernel vs. standalone */ |
| 42 | #include <prop/proplib.h> |
| 43 | |
| 44 | bool |
| 45 | prop_array_get_bool(prop_array_t array, |
| 46 | unsigned int indx, |
| 47 | bool *valp) |
| 48 | { |
| 49 | prop_bool_t b; |
| 50 | |
| 51 | b = prop_array_get(array, indx); |
| 52 | if (prop_object_type(b) != PROP_TYPE_BOOL) |
| 53 | return (false); |
| 54 | |
| 55 | *valp = prop_bool_true(b); |
| 56 | |
| 57 | return (true); |
| 58 | } |
| 59 | |
| 60 | bool |
| 61 | prop_array_set_bool(prop_array_t array, |
| 62 | unsigned int indx, |
| 63 | bool val) |
| 64 | { |
| 65 | prop_bool_t b; |
| 66 | int rv; |
| 67 | |
| 68 | b = prop_bool_create(val); |
| 69 | if (b == NULL) |
| 70 | return (false); |
| 71 | rv = prop_array_set(array, indx, b); |
| 72 | prop_object_release(b); |
| 73 | |
| 74 | return (rv); |
| 75 | } |
| 76 | |
| 77 | #define TEMPLATE(size) \ |
| 78 | bool \ |
| 79 | prop_array_get_int ## size (prop_array_t array, \ |
| 80 | unsigned int indx, \ |
| 81 | int ## size ## _t *valp) \ |
| 82 | { \ |
| 83 | prop_number_t num; \ |
| 84 | \ |
| 85 | num = prop_array_get(array, indx); \ |
| 86 | if (prop_object_type(num) != PROP_TYPE_NUMBER) \ |
| 87 | return (false); \ |
| 88 | \ |
| 89 | if (prop_number_unsigned(num) && \ |
| 90 | prop_number_unsigned_integer_value(num) > \ |
| 91 | /*CONSTCOND*/((size) == 8 ? INT8_MAX : \ |
| 92 | (size) == 16 ? INT16_MAX : \ |
| 93 | (size) == 32 ? INT32_MAX : INT64_MAX)) { \ |
| 94 | return (false); \ |
| 95 | } \ |
| 96 | \ |
| 97 | if (prop_number_size(num) > (size)) \ |
| 98 | return (false); \ |
| 99 | \ |
| 100 | *valp = (int ## size ## _t) prop_number_integer_value(num); \ |
| 101 | \ |
| 102 | return (true); \ |
| 103 | } \ |
| 104 | \ |
| 105 | bool \ |
| 106 | prop_array_get_uint ## size (prop_array_t array, \ |
| 107 | unsigned int indx, \ |
| 108 | uint ## size ## _t *valp) \ |
| 109 | { \ |
| 110 | prop_number_t num; \ |
| 111 | \ |
| 112 | num = prop_array_get(array, indx); \ |
| 113 | if (prop_object_type(num) != PROP_TYPE_NUMBER) \ |
| 114 | return (false); \ |
| 115 | \ |
| 116 | if (prop_number_unsigned(num) == false && \ |
| 117 | prop_number_integer_value(num) < 0) { \ |
| 118 | return (false); \ |
| 119 | } \ |
| 120 | \ |
| 121 | if (prop_number_size(num) > (size)) \ |
| 122 | return (false); \ |
| 123 | \ |
| 124 | *valp = (uint ## size ## _t) \ |
| 125 | prop_number_unsigned_integer_value(num); \ |
| 126 | \ |
| 127 | return (true); \ |
| 128 | } \ |
| 129 | \ |
| 130 | bool \ |
| 131 | prop_array_set_int ## size (prop_array_t array, \ |
| 132 | unsigned int indx, \ |
| 133 | int ## size ## _t val) \ |
| 134 | { \ |
| 135 | prop_number_t num; \ |
| 136 | int rv; \ |
| 137 | \ |
| 138 | num = prop_number_create_integer((int64_t) val); \ |
| 139 | if (num == NULL) \ |
| 140 | return (false); \ |
| 141 | rv = prop_array_set(array, indx, num); \ |
| 142 | prop_object_release(num); \ |
| 143 | \ |
| 144 | return (rv); \ |
| 145 | } \ |
| 146 | \ |
| 147 | bool \ |
| 148 | prop_array_set_uint ## size (prop_array_t array, \ |
| 149 | unsigned int indx, \ |
| 150 | uint ## size ## _t val) \ |
| 151 | { \ |
| 152 | prop_number_t num; \ |
| 153 | int rv; \ |
| 154 | \ |
| 155 | num = prop_number_create_unsigned_integer((uint64_t) val); \ |
| 156 | if (num == NULL) \ |
| 157 | return (false); \ |
| 158 | rv = prop_array_set(array, indx, num); \ |
| 159 | prop_object_release(num); \ |
| 160 | \ |
| 161 | return (rv); \ |
| 162 | } \ |
| 163 | \ |
| 164 | bool \ |
| 165 | prop_array_add_int ## size (prop_array_t array, \ |
| 166 | int ## size ## _t val) \ |
| 167 | { \ |
| 168 | prop_number_t num; \ |
| 169 | int rv; \ |
| 170 | \ |
| 171 | num = prop_number_create_integer((int64_t) val); \ |
| 172 | if (num == NULL) \ |
| 173 | return (false); \ |
| 174 | rv = prop_array_add(array, num); \ |
| 175 | prop_object_release(num); \ |
| 176 | \ |
| 177 | return (rv); \ |
| 178 | } \ |
| 179 | \ |
| 180 | bool \ |
| 181 | prop_array_add_uint ## size (prop_array_t array, \ |
| 182 | uint ## size ## _t val) \ |
| 183 | { \ |
| 184 | prop_number_t num; \ |
| 185 | int rv; \ |
| 186 | \ |
| 187 | num = prop_number_create_integer((int64_t) val); \ |
| 188 | if (num == NULL) \ |
| 189 | return (false); \ |
| 190 | rv = prop_array_add(array, num); \ |
| 191 | prop_object_release(num); \ |
| 192 | \ |
| 193 | return (rv); \ |
| 194 | } |
| 195 | |
| 196 | TEMPLATE(8) |
| 197 | TEMPLATE(16) |
| 198 | TEMPLATE(32) |
| 199 | TEMPLATE(64) |
| 200 | |
| 201 | #undef TEMPLATE |
| 202 | |
| 203 | #define TEMPLATE(variant, qualifier) \ |
| 204 | bool \ |
| 205 | prop_array_add_cstring ## variant (prop_array_t array, \ |
| 206 | const char *cp) \ |
| 207 | { \ |
| 208 | prop_string_t str; \ |
| 209 | bool rv; \ |
| 210 | \ |
| 211 | str = prop_string_create_cstring ## variant (cp); \ |
| 212 | if (str == NULL) \ |
| 213 | return false; \ |
| 214 | rv = prop_array_add(array, str); \ |
| 215 | prop_object_release(str); \ |
| 216 | return rv; \ |
| 217 | } \ |
| 218 | \ |
| 219 | bool \ |
| 220 | prop_array_get_cstring ## variant (prop_array_t array, \ |
| 221 | unsigned int indx, \ |
| 222 | qualifier char **cpp) \ |
| 223 | { \ |
| 224 | prop_string_t str; \ |
| 225 | \ |
| 226 | str = prop_array_get(array, indx); \ |
| 227 | if (prop_object_type(str) != PROP_TYPE_STRING) \ |
| 228 | return (false); \ |
| 229 | \ |
| 230 | *cpp = prop_string_cstring ## variant (str); \ |
| 231 | \ |
| 232 | return (*cpp == NULL ? false : true); \ |
| 233 | } \ |
| 234 | \ |
| 235 | bool \ |
| 236 | prop_array_set_cstring ## variant (prop_array_t array, \ |
| 237 | unsigned int indx, \ |
| 238 | const char *cp) \ |
| 239 | { \ |
| 240 | prop_string_t str; \ |
| 241 | int rv; \ |
| 242 | \ |
| 243 | str = prop_string_create_cstring ## variant (cp); \ |
| 244 | if (str == NULL) \ |
| 245 | return (false); \ |
| 246 | rv = prop_array_set(array, indx, str); \ |
| 247 | prop_object_release(str); \ |
| 248 | \ |
| 249 | return (rv); \ |
| 250 | } |
| 251 | |
| 252 | TEMPLATE(,) |
| 253 | TEMPLATE(_nocopy,const) |
| 254 | |
| 255 | #undef TEMPLATE |
| 256 | |
| 257 | bool |
| 258 | prop_array_add_and_rel(prop_array_t array, prop_object_t po) |
| 259 | { |
| 260 | bool ret; |
| 261 | if (po == NULL) |
| 262 | return false; |
| 263 | ret = prop_array_add(array, po); |
| 264 | prop_object_release(po); |
| 265 | return ret; |
| 266 | } |
| 267 | |