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