1 /* $NetBSD: prop_object_impl.h,v 1.41 2025/05/14 03:25:46 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2006, 2020, 2025 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 #ifndef _PROPLIB_PROP_OBJECT_IMPL_H_ 33 #define _PROPLIB_PROP_OBJECT_IMPL_H_ 34 35 #if defined(HAVE_NBTOOL_CONFIG_H) 36 #include "nbtool_config.h" 37 #endif 38 39 #if defined(_KERNEL) || defined(_STANDALONE) 40 #include <lib/libkern/libkern.h> 41 #else 42 #include <inttypes.h> 43 #endif 44 45 #include "prop_stack.h" 46 47 struct _prop_object; 48 49 struct _prop_object_externalize_context { 50 char * poec_buf; /* string buffer */ 51 size_t poec_capacity; /* capacity of buffer */ 52 size_t poec_len; /* current length of string */ 53 unsigned int poec_depth; /* nesting depth */ 54 prop_format_t poec_format; /* output format */ 55 }; 56 57 struct _prop_object_type_tags { 58 const char *xml_tag; 59 const char *json_open_tag; 60 const char *json_close_tag; 61 const char *json_empty_sep; 62 }; 63 64 bool _prop_extern_append_char( 65 struct _prop_object_externalize_context *, 66 unsigned char); 67 bool _prop_extern_append_cstring( 68 struct _prop_object_externalize_context *, 69 const char *); 70 bool _prop_extern_start_line( 71 struct _prop_object_externalize_context *); 72 bool _prop_extern_end_line( 73 struct _prop_object_externalize_context *, 74 const char *); 75 76 bool _prop_extern_append_start_tag( 77 struct _prop_object_externalize_context *, 78 const struct _prop_object_type_tags *, 79 const char *); 80 bool _prop_extern_append_end_tag( 81 struct _prop_object_externalize_context *, 82 const struct _prop_object_type_tags *); 83 bool _prop_extern_append_empty_tag( 84 struct _prop_object_externalize_context *, 85 const struct _prop_object_type_tags *); 86 87 bool _prop_extern_append_encoded_cstring( 88 struct _prop_object_externalize_context *, 89 const char *); 90 91 bool _prop_object_externalize_to_file(struct _prop_object *, 92 const char *, prop_format_t); 93 char * _prop_object_externalize(struct _prop_object *, 94 prop_format_t fmt); 95 96 typedef enum { 97 _PROP_TAG_TYPE_START, /* e.g. <dict> */ 98 _PROP_TAG_TYPE_END, /* e.g. </dict> */ 99 _PROP_TAG_TYPE_EITHER 100 } _prop_tag_type_t; 101 102 struct _prop_object_internalize_context { 103 prop_format_t poic_format; 104 105 const char *poic_data; 106 const char *poic_cp; 107 108 const char *poic_tag_start; 109 110 const char *poic_tagname; 111 size_t poic_tagname_len; 112 const char *poic_tagattr; 113 size_t poic_tagattr_len; 114 const char *poic_tagattrval; 115 size_t poic_tagattrval_len; 116 117 bool poic_is_empty_element; 118 _prop_tag_type_t poic_tag_type; 119 }; 120 121 typedef enum { 122 _PROP_OBJECT_FREE_DONE, 123 _PROP_OBJECT_FREE_RECURSE, 124 _PROP_OBJECT_FREE_FAILED 125 } _prop_object_free_rv_t; 126 127 typedef enum { 128 _PROP_OBJECT_EQUALS_FALSE, 129 _PROP_OBJECT_EQUALS_TRUE, 130 _PROP_OBJECT_EQUALS_RECURSE 131 } _prop_object_equals_rv_t; 132 133 #define _PROP_EOF(c) ((c) == '\0') 134 #define _PROP_ISSPACE(c) \ 135 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r') 136 137 #define _PROP_TAG_MATCH(ctx, t) \ 138 _prop_intern_match((ctx)->poic_tagname, \ 139 (ctx)->poic_tagname_len, \ 140 (t), strlen(t)) 141 142 #define _PROP_TAGATTR_MATCH(ctx, a) \ 143 _prop_intern_match((ctx)->poic_tagattr, \ 144 (ctx)->poic_tagattr_len, \ 145 (a), strlen(a)) 146 147 #define _PROP_TAGATTRVAL_MATCH(ctx, a) \ 148 _prop_intern_match((ctx)->poic_tagattrval, \ 149 (ctx)->poic_tagattrval_len, \ 150 (a), strlen(a)) 151 152 const char * _prop_intern_skip_whitespace(const char *); 153 bool _prop_intern_match(const char *, size_t, const char *, size_t); 154 155 bool _prop_intern_decode_string( 156 struct _prop_object_internalize_context *, 157 char *, size_t, size_t *, const char **); 158 159 bool _prop_xml_intern_find_tag( 160 struct _prop_object_internalize_context *, 161 const char *, _prop_tag_type_t); 162 163 prop_object_t _prop_object_internalize(const char *, 164 const struct _prop_object_type_tags *); 165 prop_object_t _prop_object_internalize_from_file(const char *, 166 const struct _prop_object_type_tags *); 167 168 typedef bool (*prop_object_internalizer_t)(prop_stack_t, prop_object_t *, 169 struct _prop_object_internalize_context *); 170 typedef bool (*prop_object_internalizer_continue_t)(prop_stack_t, 171 prop_object_t *, 172 struct _prop_object_internalize_context *, 173 void *, prop_object_t); 174 175 /* These are here because they're required by shared code. */ 176 bool _prop_array_internalize(prop_stack_t, prop_object_t *, 177 struct _prop_object_internalize_context *); 178 bool _prop_bool_internalize(prop_stack_t, prop_object_t *, 179 struct _prop_object_internalize_context *); 180 bool _prop_data_internalize(prop_stack_t, prop_object_t *, 181 struct _prop_object_internalize_context *); 182 bool _prop_dictionary_internalize(prop_stack_t, prop_object_t *, 183 struct _prop_object_internalize_context *); 184 bool _prop_number_internalize(prop_stack_t, prop_object_t *, 185 struct _prop_object_internalize_context *); 186 bool _prop_string_internalize(prop_stack_t, prop_object_t *, 187 struct _prop_object_internalize_context *); 188 189 bool _prop_string_externalize_internal( 190 struct _prop_object_externalize_context *, 191 const struct _prop_object_type_tags *, 192 const char *); 193 194 struct _prop_object_type { 195 /* type indicator */ 196 uint32_t pot_type; 197 /* func to free object */ 198 _prop_object_free_rv_t 199 (*pot_free)(prop_stack_t, prop_object_t *); 200 /* 201 * func to free the child returned by pot_free with stack == NULL. 202 * 203 * Must be implemented if pot_free can return anything other than 204 * _PROP_OBJECT_FREE_DONE. 205 */ 206 void (*pot_emergency_free)(prop_object_t); 207 /* func to externalize object */ 208 bool (*pot_extern)(struct _prop_object_externalize_context *, 209 void *); 210 /* func to test quality */ 211 _prop_object_equals_rv_t 212 (*pot_equals)(prop_object_t, prop_object_t, 213 void **, void **, 214 prop_object_t *, prop_object_t *); 215 /* 216 * func to finish equality iteration. 217 * 218 * Must be implemented if pot_equals can return 219 * _PROP_OBJECT_EQUALS_RECURSE 220 */ 221 void (*pot_equals_finish)(prop_object_t, prop_object_t); 222 void (*pot_lock)(void); 223 void (*pot_unlock)(void); 224 }; 225 226 struct _prop_object { 227 const struct _prop_object_type *po_type;/* type descriptor */ 228 uint32_t po_refcnt; /* reference count */ 229 }; 230 231 void _prop_object_init(struct _prop_object *, 232 const struct _prop_object_type *); 233 void _prop_object_fini(struct _prop_object *); 234 235 struct _prop_object_iterator { 236 prop_object_t (*pi_next_object)(void *); 237 void (*pi_reset)(void *); 238 prop_object_t pi_obj; 239 uint32_t pi_version; 240 }; 241 242 #define _PROP_NOTHREAD_ONCE_DECL(x) static bool x = false; 243 #define _PROP_NOTHREAD_ONCE_RUN(x,f) \ 244 do { \ 245 if ((x) == false) { \ 246 f(); \ 247 x = true; \ 248 } \ 249 } while (/*CONSTCOND*/0) 250 251 #if defined(_KERNEL) 252 253 /* 254 * proplib in the kernel... 255 */ 256 257 #include <sys/param.h> 258 #include <sys/malloc.h> 259 #include <sys/pool.h> 260 #include <sys/systm.h> 261 #include <sys/rwlock.h> 262 #include <sys/once.h> 263 264 #define _PROP_ASSERT(x) KASSERT(x) 265 266 #define _PROP_MALLOC(s, t) malloc((s), (t), M_WAITOK) 267 #define _PROP_CALLOC(s, t) malloc((s), (t), M_WAITOK | M_ZERO) 268 #define _PROP_REALLOC(v, s, t) realloc((v), (s), (t), M_WAITOK) 269 #define _PROP_FREE(v, t) free((v), (t)) 270 271 #define _PROP_POOL_GET(p) pool_get(&(p), PR_WAITOK) 272 #define _PROP_POOL_PUT(p, v) pool_put(&(p), (v)) 273 274 struct prop_pool_init { 275 struct pool *pp; 276 size_t size; 277 const char *wchan; 278 }; 279 #define _PROP_POOL_INIT(pp, size, wchan) \ 280 struct pool pp; \ 281 static const struct prop_pool_init _link_ ## pp[1] = { \ 282 { &pp, size, wchan } \ 283 }; \ 284 __link_set_add_rodata(prop_linkpools, _link_ ## pp); 285 286 #define _PROP_MALLOC_DEFINE(t, s, l) \ 287 MALLOC_DEFINE(t, s, l); 288 289 #define _PROP_MUTEX_DECL_STATIC(x) static kmutex_t x; 290 #define _PROP_MUTEX_INIT(x) mutex_init(&(x),MUTEX_DEFAULT,IPL_NONE) 291 #define _PROP_MUTEX_LOCK(x) mutex_enter(&(x)) 292 #define _PROP_MUTEX_UNLOCK(x) mutex_exit(&(x)) 293 294 #define _PROP_RWLOCK_DECL(x) krwlock_t x ; 295 #define _PROP_RWLOCK_INIT(x) rw_init(&(x)) 296 #define _PROP_RWLOCK_RDLOCK(x) rw_enter(&(x), RW_READER) 297 #define _PROP_RWLOCK_WRLOCK(x) rw_enter(&(x), RW_WRITER) 298 #define _PROP_RWLOCK_UNLOCK(x) rw_exit(&(x)) 299 #define _PROP_RWLOCK_DESTROY(x) rw_destroy(&(x)) 300 301 #define _PROP_ONCE_DECL(x) static ONCE_DECL(x); 302 #define _PROP_ONCE_RUN(x,f) RUN_ONCE(&(x), f) 303 304 #include <sys/atomic.h> 305 306 #define _PROP_ATOMIC_LOAD(x) atomic_load_relaxed(x) 307 #define _PROP_ATOMIC_INC32(x) atomic_inc_32(x) 308 #define _PROP_ATOMIC_DEC32(x) atomic_dec_32(x) 309 #define _PROP_ATOMIC_INC32_NV(x, v) v = atomic_inc_32_nv(x) 310 #define _PROP_ATOMIC_DEC32_NV(x, v) v = atomic_dec_32_nv(x) 311 312 #elif defined(_STANDALONE) 313 314 /* 315 * proplib in a standalone environment... 316 */ 317 318 #include <lib/libsa/stand.h> 319 320 void * _prop_standalone_calloc(size_t); 321 void * _prop_standalone_realloc(void *, size_t); 322 323 #define _PROP_ASSERT(x) /* nothing */ 324 325 #define _PROP_MALLOC(s, t) alloc((s)) 326 #define _PROP_CALLOC(s, t) _prop_standalone_calloc((s)) 327 #define _PROP_REALLOC(v, s, t) _prop_standalone_realloc((v), (s)) 328 #define _PROP_FREE(v, t) dealloc((v), 0) /* XXX */ 329 330 #define _PROP_POOL_GET(p) alloc((p)) 331 #define _PROP_POOL_PUT(p, v) dealloc((v), (p)) 332 333 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s; 334 335 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ 336 337 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ 338 #define _PROP_MUTEX_INIT(x) /* nothing */ 339 #define _PROP_MUTEX_LOCK(x) /* nothing */ 340 #define _PROP_MUTEX_UNLOCK(x) /* nothing */ 341 342 #define _PROP_RWLOCK_DECL(x) /* nothing */ 343 #define _PROP_RWLOCK_INIT(x) /* nothing */ 344 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */ 345 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */ 346 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */ 347 #define _PROP_RWLOCK_DESTROY(x) /* nothing */ 348 349 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) 350 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) 351 352 #define _PROP_ATOMIC_LOAD(x) *(x) 353 #define _PROP_ATOMIC_INC32(x) ++*(x) 354 #define _PROP_ATOMIC_DEC32(x) --*(x) 355 #define _PROP_ATOMIC_INC32_NV(x, v) v = ++*(x) 356 #define _PROP_ATOMIC_DEC32_NV(x, v) v = --*(x) 357 358 #else 359 360 /* 361 * proplib in user space... 362 */ 363 364 #include <assert.h> 365 #include <string.h> 366 #include <stdio.h> 367 #include <stdlib.h> 368 #include <stddef.h> 369 370 #define _PROP_ASSERT(x) /*LINTED*/assert(x) 371 372 #define _PROP_MALLOC(s, t) malloc((s)) 373 #define _PROP_CALLOC(s, t) calloc(1, (s)) 374 #define _PROP_REALLOC(v, s, t) realloc((v), (s)) 375 #define _PROP_FREE(v, t) free((v)) 376 377 #define _PROP_POOL_GET(p) malloc((p)) 378 #define _PROP_POOL_PUT(p, v) free((v)) 379 380 #define _PROP_POOL_INIT(p, s, d) static const size_t p = s; 381 382 #define _PROP_MALLOC_DEFINE(t, s, l) /* nothing */ 383 384 #if defined(__NetBSD__) && defined(_LIBPROP) 385 /* 386 * Use the same mechanism as libc; we get pthread mutexes for threaded 387 * programs and do-nothing stubs for non-threaded programs. 388 */ 389 #include <sys/atomic.h> 390 #include "reentrant.h" 391 #define _PROP_MUTEX_DECL_STATIC(x) static mutex_t x; 392 #define _PROP_MUTEX_INIT(x) mutex_init(&(x), NULL) 393 #define _PROP_MUTEX_LOCK(x) mutex_lock(&(x)) 394 #define _PROP_MUTEX_UNLOCK(x) mutex_unlock(&(x)) 395 396 #define _PROP_RWLOCK_DECL(x) rwlock_t x ; 397 #define _PROP_RWLOCK_INIT(x) rwlock_init(&(x), NULL) 398 #define _PROP_RWLOCK_RDLOCK(x) rwlock_rdlock(&(x)) 399 #define _PROP_RWLOCK_WRLOCK(x) rwlock_wrlock(&(x)) 400 #define _PROP_RWLOCK_UNLOCK(x) rwlock_unlock(&(x)) 401 #define _PROP_RWLOCK_DESTROY(x) rwlock_destroy(&(x)) 402 403 #define _PROP_ONCE_DECL(x) \ 404 static pthread_once_t x = PTHREAD_ONCE_INIT; 405 #define _PROP_ONCE_RUN(x,f) thr_once(&(x), (void(*)(void))f); 406 407 #define _PROP_ATOMIC_LOAD(x) *(x) 408 #define _PROP_ATOMIC_INC32(x) atomic_inc_32(x) 409 #define _PROP_ATOMIC_DEC32(x) atomic_dec_32(x) 410 #define _PROP_ATOMIC_INC32_NV(x, v) v = atomic_inc_32_nv(x) 411 #define _PROP_ATOMIC_DEC32_NV(x, v) v = atomic_dec_32_nv(x) 412 413 #define _PROP_EXPORT __attribute__((visibility("default"))) 414 415 #elif defined(HAVE_NBTOOL_CONFIG_H) 416 /* 417 * None of NetBSD's build tools are multi-threaded. 418 */ 419 #define _PROP_MUTEX_DECL_STATIC(x) /* nothing */ 420 #define _PROP_MUTEX_INIT(x) /* nothing */ 421 #define _PROP_MUTEX_LOCK(x) /* nothing */ 422 #define _PROP_MUTEX_UNLOCK(x) /* nothing */ 423 424 #define _PROP_RWLOCK_DECL(x) /* nothing */ 425 #define _PROP_RWLOCK_INIT(x) /* nothing */ 426 #define _PROP_RWLOCK_RDLOCK(x) /* nothing */ 427 #define _PROP_RWLOCK_WRLOCK(x) /* nothing */ 428 #define _PROP_RWLOCK_UNLOCK(x) /* nothing */ 429 #define _PROP_RWLOCK_DESTROY(x) /* nothing */ 430 431 #define _PROP_ONCE_DECL(x) _PROP_NOTHREAD_ONCE_DECL(x) 432 #define _PROP_ONCE_RUN(x,f) _PROP_NOTHREAD_ONCE_RUN(x,f) 433 434 #define _PROP_ATOMIC_LOAD(x) *(x) 435 #define _PROP_ATOMIC_INC32(x) ++*(x) 436 #define _PROP_ATOMIC_DEC32(x) --*(x) 437 #define _PROP_ATOMIC_INC32_NV(x, v) v = ++*(x) 438 #define _PROP_ATOMIC_DEC32_NV(x, v) v = --*(x) 439 440 #else 441 /* 442 * Use pthread mutexes everywhere else. 443 */ 444 #include <pthread.h> 445 #define _PROP_MUTEX_DECL_STATIC(x) static pthread_mutex_t x; 446 #define _PROP_MUTEX_INIT(x) pthread_mutex_init(&(x), NULL) 447 #define _PROP_MUTEX_LOCK(x) pthread_mutex_lock(&(x)) 448 #define _PROP_MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) 449 450 #define _PROP_RWLOCK_DECL(x) pthread_rwlock_t x ; 451 #define _PROP_RWLOCK_INIT(x) pthread_rwlock_init(&(x), NULL) 452 #define _PROP_RWLOCK_RDLOCK(x) pthread_rwlock_rdlock(&(x)) 453 #define _PROP_RWLOCK_WRLOCK(x) pthread_rwlock_wrlock(&(x)) 454 #define _PROP_RWLOCK_UNLOCK(x) pthread_rwlock_unlock(&(x)) 455 #define _PROP_RWLOCK_DESTROY(x) pthread_rwlock_destroy(&(x)) 456 457 #define _PROP_ONCE_DECL(x) \ 458 static pthread_once_t x = PTHREAD_ONCE_INIT; 459 #define _PROP_ONCE_RUN(x,f) pthread_once(&(x),(void(*)(void))f) 460 461 #define _PROP_NEED_REFCNT_MTX 462 463 #define _PROP_ATOMIC_LOAD(x) *(x) 464 465 #define _PROP_ATOMIC_INC32(x) \ 466 do { \ 467 pthread_mutex_lock(&_prop_refcnt_mtx); \ 468 (*(x))++; \ 469 pthread_mutex_unlock(&_prop_refcnt_mtx); \ 470 } while (/*CONSTCOND*/0) 471 472 #define _PROP_ATOMIC_DEC32(x) \ 473 do { \ 474 pthread_mutex_lock(&_prop_refcnt_mtx); \ 475 (*(x))--; \ 476 pthread_mutex_unlock(&_prop_refcnt_mtx); \ 477 } while (/*CONSTCOND*/0) 478 479 #define _PROP_ATOMIC_INC32_NV(x, v) \ 480 do { \ 481 pthread_mutex_lock(&_prop_refcnt_mtx); \ 482 v = ++(*(x)); \ 483 pthread_mutex_unlock(&_prop_refcnt_mtx); \ 484 } while (/*CONSTCOND*/0) 485 486 #define _PROP_ATOMIC_DEC32_NV(x, v) \ 487 do { \ 488 pthread_mutex_lock(&_prop_refcnt_mtx); \ 489 v = --(*(x)); \ 490 pthread_mutex_unlock(&_prop_refcnt_mtx); \ 491 } while (/*CONSTCOND*/0) 492 493 #endif 494 #endif /* _KERNEL */ 495 496 #ifndef _PROP_EXPORT 497 #define _PROP_EXPORT /* nothing */ 498 #endif 499 500 /* 501 * Language features. 502 */ 503 #if defined(__NetBSD__) 504 #include <sys/cdefs.h> 505 #define _PROP_ARG_UNUSED __unused 506 #if defined(__clang__) 507 #define _PROP_DEPRECATED(s, m) /* delete */ 508 #else /* ! __clang__ */ 509 #define _PROP_DEPRECATED(s, m) __warn_references(s, m) 510 #endif /* __clang__ */ 511 #define _PROP_UNCONST(x) __UNCONST(x) 512 #else 513 #define _PROP_ARG_UNUSED /* delete */ 514 #define _PROP_DEPRECATED(s, m) /* delete */ 515 #define _PROP_UNCONST(x) ((void *)(unsigned long)(const void *)(x)) 516 #endif /* __NetBSD__ */ 517 518 #endif /* _PROPLIB_PROP_OBJECT_IMPL_H_ */ 519