| 1 |
/* |
| 2 |
* Copyright (c) 2010 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Portions Copyright (c) 2010 Apple Inc. All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* |
| 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 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 20 |
* may be used to endorse or promote products derived from this software |
| 21 |
* without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 24 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 |
* SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
|
| 36 |
#ifndef HEIM_BASE_H |
| 37 |
#define HEIM_BASE_H 1 |
| 38 |
|
| 39 |
#include <sys/types.h> |
| 40 |
#if !defined(WIN32) && !defined(HAVE_DISPATCH_DISPATCH_H) && defined(ENABLE_PTHREAD_SUPPORT) |
| 41 |
#include <pthread.h> |
| 42 |
#endif |
| 43 |
#include <krb5-types.h> |
| 44 |
#include <stdarg.h> |
| 45 |
#ifdef HAVE_STDBOOL_H |
| 46 |
#include <stdbool.h> |
| 47 |
#else |
| 48 |
#ifndef false |
| 49 |
#define false 0 |
| 50 |
#endif |
| 51 |
#ifndef true |
| 52 |
#define true 1 |
| 53 |
#endif |
| 54 |
#endif |
| 55 |
|
| 56 |
#define HEIM_BASE_API_VERSION 20130210 |
| 57 |
|
| 58 |
typedef void * heim_object_t; |
| 59 |
typedef unsigned int heim_tid_t; |
| 60 |
typedef heim_object_t heim_bool_t; |
| 61 |
typedef heim_object_t heim_null_t; |
| 62 |
#ifdef WIN32 |
| 63 |
typedef LONG heim_base_once_t; |
| 64 |
#define HEIM_BASE_ONCE_INIT 0 |
| 65 |
#elif defined(HAVE_DISPATCH_DISPATCH_H) |
| 66 |
typedef long heim_base_once_t; /* XXX arch dependant */ |
| 67 |
#define HEIM_BASE_ONCE_INIT 0 |
| 68 |
#elif defined(ENABLE_PTHREAD_SUPPORT) |
| 69 |
typedef pthread_once_t heim_base_once_t; |
| 70 |
#define HEIM_BASE_ONCE_INIT PTHREAD_ONCE_INIT |
| 71 |
#else |
| 72 |
typedef long heim_base_once_t; /* XXX arch dependant */ |
| 73 |
#define HEIM_BASE_ONCE_INIT 0 |
| 74 |
#endif |
| 75 |
|
| 76 |
#if !defined(__has_extension) |
| 77 |
#define __has_extension(x) 0 |
| 78 |
#endif |
| 79 |
|
| 80 |
#define HEIM_REQUIRE_GNUC(m,n,p) \ |
| 81 |
(((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) >= \ |
| 82 |
(((m) * 10000) + ((n) * 100) + (p))) |
| 83 |
|
| 84 |
|
| 85 |
#if __has_extension(__builtin_expect) || HEIM_REQUIRE_GNUC(3,0,0) |
| 86 |
#define heim_builtin_expect(_op,_res) __builtin_expect(_op,_res) |
| 87 |
#else |
| 88 |
#define heim_builtin_expect(_op,_res) (_op) |
| 89 |
#endif |
| 90 |
|
| 91 |
|
| 92 |
void * heim_retain(heim_object_t); |
| 93 |
void heim_release(heim_object_t); |
| 94 |
|
| 95 |
void heim_show(heim_object_t); |
| 96 |
|
| 97 |
typedef void (*heim_type_dealloc)(void *); |
| 98 |
|
| 99 |
void * |
| 100 |
heim_alloc(size_t size, const char *name, heim_type_dealloc dealloc); |
| 101 |
|
| 102 |
heim_tid_t |
| 103 |
heim_get_tid(heim_object_t object); |
| 104 |
|
| 105 |
int |
| 106 |
heim_cmp(heim_object_t a, heim_object_t b); |
| 107 |
|
| 108 |
unsigned long |
| 109 |
heim_get_hash(heim_object_t ptr); |
| 110 |
|
| 111 |
void |
| 112 |
heim_base_once_f(heim_base_once_t *, void *, void (*)(void *)); |
| 113 |
|
| 114 |
void |
| 115 |
heim_abort(const char *fmt, ...) |
| 116 |
HEIMDAL_NORETURN_ATTRIBUTE |
| 117 |
HEIMDAL_PRINTF_ATTRIBUTE((__printf__, 1, 2)); |
| 118 |
|
| 119 |
void |
| 120 |
heim_abortv(const char *fmt, va_list ap) |
| 121 |
HEIMDAL_NORETURN_ATTRIBUTE |
| 122 |
HEIMDAL_PRINTF_ATTRIBUTE((__printf__, 1, 0)); |
| 123 |
|
| 124 |
#define heim_assert(e,t) \ |
| 125 |
(heim_builtin_expect(!(e), 0) ? heim_abort(t ":" #e) : (void)0) |
| 126 |
|
| 127 |
/* |
| 128 |
* |
| 129 |
*/ |
| 130 |
|
| 131 |
heim_null_t |
| 132 |
heim_null_create(void); |
| 133 |
|
| 134 |
heim_bool_t |
| 135 |
heim_bool_create(int); |
| 136 |
|
| 137 |
int |
| 138 |
heim_bool_val(heim_bool_t); |
| 139 |
|
| 140 |
/* |
| 141 |
* Array |
| 142 |
*/ |
| 143 |
|
| 144 |
typedef struct heim_array_data *heim_array_t; |
| 145 |
|
| 146 |
heim_array_t heim_array_create(void); |
| 147 |
heim_tid_t heim_array_get_type_id(void); |
| 148 |
|
| 149 |
typedef void (*heim_array_iterator_f_t)(heim_object_t, void *, int *); |
| 150 |
typedef int (*heim_array_filter_f_t)(heim_object_t, void *); |
| 151 |
|
| 152 |
int heim_array_append_value(heim_array_t, heim_object_t); |
| 153 |
int heim_array_insert_value(heim_array_t, size_t idx, heim_object_t); |
| 154 |
void heim_array_iterate_f(heim_array_t, void *, heim_array_iterator_f_t); |
| 155 |
void heim_array_iterate_reverse_f(heim_array_t, void *, heim_array_iterator_f_t); |
| 156 |
#ifdef __BLOCKS__ |
| 157 |
void heim_array_iterate(heim_array_t, void (^)(heim_object_t, int *)); |
| 158 |
void heim_array_iterate_reverse(heim_array_t, void (^)(heim_object_t, int *)); |
| 159 |
#endif |
| 160 |
size_t heim_array_get_length(heim_array_t); |
| 161 |
heim_object_t |
| 162 |
heim_array_get_value(heim_array_t, size_t); |
| 163 |
heim_object_t |
| 164 |
heim_array_copy_value(heim_array_t, size_t); |
| 165 |
void heim_array_set_value(heim_array_t, size_t, heim_object_t); |
| 166 |
void heim_array_delete_value(heim_array_t, size_t); |
| 167 |
void heim_array_filter_f(heim_array_t, void *, heim_array_filter_f_t); |
| 168 |
#ifdef __BLOCKS__ |
| 169 |
void heim_array_filter(heim_array_t, int (^)(heim_object_t)); |
| 170 |
#endif |
| 171 |
|
| 172 |
/* |
| 173 |
* Dict |
| 174 |
*/ |
| 175 |
|
| 176 |
typedef struct heim_dict_data *heim_dict_t; |
| 177 |
|
| 178 |
heim_dict_t heim_dict_create(size_t size); |
| 179 |
heim_tid_t heim_dict_get_type_id(void); |
| 180 |
|
| 181 |
typedef void (*heim_dict_iterator_f_t)(heim_object_t, heim_object_t, void *); |
| 182 |
|
| 183 |
int heim_dict_set_value(heim_dict_t, heim_object_t, heim_object_t); |
| 184 |
void heim_dict_iterate_f(heim_dict_t, void *, heim_dict_iterator_f_t); |
| 185 |
#ifdef __BLOCKS__ |
| 186 |
void heim_dict_iterate(heim_dict_t, void (^)(heim_object_t, heim_object_t)); |
| 187 |
#endif |
| 188 |
|
| 189 |
heim_object_t |
| 190 |
heim_dict_get_value(heim_dict_t, heim_object_t); |
| 191 |
heim_object_t |
| 192 |
heim_dict_copy_value(heim_dict_t, heim_object_t); |
| 193 |
void heim_dict_delete_key(heim_dict_t, heim_object_t); |
| 194 |
|
| 195 |
/* |
| 196 |
* String |
| 197 |
*/ |
| 198 |
|
| 199 |
typedef struct heim_string_data *heim_string_t; |
| 200 |
typedef void (*heim_string_free_f_t)(void *); |
| 201 |
|
| 202 |
heim_string_t heim_string_create(const char *); |
| 203 |
heim_string_t heim_string_ref_create(const char *, heim_string_free_f_t); |
| 204 |
heim_string_t heim_string_create_with_bytes(const void *, size_t); |
| 205 |
heim_string_t heim_string_ref_create_with_bytes(const void *, size_t, |
| 206 |
heim_string_free_f_t); |
| 207 |
heim_string_t heim_string_create_with_format(const char *, ...); |
| 208 |
heim_tid_t heim_string_get_type_id(void); |
| 209 |
const char * heim_string_get_utf8(heim_string_t); |
| 210 |
|
| 211 |
#define HSTR(_str) (__heim_string_constant("" _str "")) |
| 212 |
heim_string_t __heim_string_constant(const char *); |
| 213 |
|
| 214 |
/* |
| 215 |
* Errors |
| 216 |
*/ |
| 217 |
|
| 218 |
typedef struct heim_error * heim_error_t; |
| 219 |
|
| 220 |
heim_error_t heim_error_create_enomem(void); |
| 221 |
|
| 222 |
heim_error_t heim_error_create(int, const char *, ...) |
| 223 |
HEIMDAL_PRINTF_ATTRIBUTE((__printf__, 2, 3)); |
| 224 |
|
| 225 |
void heim_error_create_opt(heim_error_t *error, int error_code, const char *fmt, ...) |
| 226 |
HEIMDAL_PRINTF_ATTRIBUTE((__printf__, 3, 4)); |
| 227 |
|
| 228 |
heim_error_t heim_error_createv(int, const char *, va_list) |
| 229 |
HEIMDAL_PRINTF_ATTRIBUTE((__printf__, 2, 0)); |
| 230 |
|
| 231 |
heim_string_t heim_error_copy_string(heim_error_t); |
| 232 |
int heim_error_get_code(heim_error_t); |
| 233 |
|
| 234 |
heim_error_t heim_error_append(heim_error_t, heim_error_t); |
| 235 |
|
| 236 |
/* |
| 237 |
* Path |
| 238 |
*/ |
| 239 |
|
| 240 |
heim_object_t heim_path_get(heim_object_t ptr, heim_error_t *error, ...); |
| 241 |
heim_object_t heim_path_copy(heim_object_t ptr, heim_error_t *error, ...); |
| 242 |
heim_object_t heim_path_vget(heim_object_t ptr, heim_error_t *error, |
| 243 |
va_list ap); |
| 244 |
heim_object_t heim_path_vcopy(heim_object_t ptr, heim_error_t *error, |
| 245 |
va_list ap); |
| 246 |
|
| 247 |
int heim_path_vcreate(heim_object_t ptr, size_t size, heim_object_t leaf, |
| 248 |
heim_error_t *error, va_list ap); |
| 249 |
int heim_path_create(heim_object_t ptr, size_t size, heim_object_t leaf, |
| 250 |
heim_error_t *error, ...); |
| 251 |
|
| 252 |
void heim_path_vdelete(heim_object_t ptr, heim_error_t *error, va_list ap); |
| 253 |
void heim_path_delete(heim_object_t ptr, heim_error_t *error, ...); |
| 254 |
|
| 255 |
/* |
| 256 |
* Data (octet strings) |
| 257 |
*/ |
| 258 |
|
| 259 |
#ifndef __HEIM_BASE_DATA__ |
| 260 |
#define __HEIM_BASE_DATA__ |
| 261 |
struct heim_base_data { |
| 262 |
size_t length; |
| 263 |
void *data; |
| 264 |
}; |
| 265 |
typedef struct heim_base_data heim_octet_string; |
| 266 |
#endif |
| 267 |
|
| 268 |
typedef struct heim_base_data * heim_data_t; |
| 269 |
typedef void (*heim_data_free_f_t)(void *); |
| 270 |
|
| 271 |
heim_data_t heim_data_create(const void *, size_t); |
| 272 |
heim_data_t heim_data_ref_create(const void *, size_t, heim_data_free_f_t); |
| 273 |
heim_tid_t heim_data_get_type_id(void); |
| 274 |
const heim_octet_string * |
| 275 |
heim_data_get_data(heim_data_t); |
| 276 |
const void * heim_data_get_ptr(heim_data_t); |
| 277 |
size_t heim_data_get_length(heim_data_t); |
| 278 |
|
| 279 |
/* |
| 280 |
* DB |
| 281 |
*/ |
| 282 |
|
| 283 |
typedef struct heim_db_data *heim_db_t; |
| 284 |
|
| 285 |
typedef void (*heim_db_iterator_f_t)(heim_data_t, heim_data_t, void *); |
| 286 |
|
| 287 |
typedef int (*heim_db_plug_open_f_t)(void *, const char *, const char *, |
| 288 |
heim_dict_t, void **, heim_error_t *); |
| 289 |
typedef int (*heim_db_plug_clone_f_t)(void *, void **, heim_error_t *); |
| 290 |
typedef int (*heim_db_plug_close_f_t)(void *, heim_error_t *); |
| 291 |
typedef int (*heim_db_plug_lock_f_t)(void *, int, heim_error_t *); |
| 292 |
typedef int (*heim_db_plug_unlock_f_t)(void *, heim_error_t *); |
| 293 |
typedef int (*heim_db_plug_sync_f_t)(void *, heim_error_t *); |
| 294 |
typedef int (*heim_db_plug_begin_f_t)(void *, int, heim_error_t *); |
| 295 |
typedef int (*heim_db_plug_commit_f_t)(void *, heim_error_t *); |
| 296 |
typedef int (*heim_db_plug_rollback_f_t)(void *, heim_error_t *); |
| 297 |
typedef heim_data_t (*heim_db_plug_copy_value_f_t)(void *, heim_string_t, |
| 298 |
heim_data_t, |
| 299 |
heim_error_t *); |
| 300 |
typedef int (*heim_db_plug_set_value_f_t)(void *, heim_string_t, heim_data_t, |
| 301 |
heim_data_t, heim_error_t *); |
| 302 |
typedef int (*heim_db_plug_del_key_f_t)(void *, heim_string_t, heim_data_t, |
| 303 |
heim_error_t *); |
| 304 |
typedef void (*heim_db_plug_iter_f_t)(void *, heim_string_t, void *, |
| 305 |
heim_db_iterator_f_t, heim_error_t *); |
| 306 |
|
| 307 |
struct heim_db_type { |
| 308 |
int version; |
| 309 |
heim_db_plug_open_f_t openf; |
| 310 |
heim_db_plug_clone_f_t clonef; |
| 311 |
heim_db_plug_close_f_t closef; |
| 312 |
heim_db_plug_lock_f_t lockf; |
| 313 |
heim_db_plug_unlock_f_t unlockf; |
| 314 |
heim_db_plug_sync_f_t syncf; |
| 315 |
heim_db_plug_begin_f_t beginf; |
| 316 |
heim_db_plug_commit_f_t commitf; |
| 317 |
heim_db_plug_rollback_f_t rollbackf; |
| 318 |
heim_db_plug_copy_value_f_t copyf; |
| 319 |
heim_db_plug_set_value_f_t setf; |
| 320 |
heim_db_plug_del_key_f_t delf; |
| 321 |
heim_db_plug_iter_f_t iterf; |
| 322 |
}; |
| 323 |
|
| 324 |
extern struct heim_db_type heim_sorted_text_file_dbtype; |
| 325 |
|
| 326 |
#define HEIM_DB_TYPE_VERSION_01 1 |
| 327 |
|
| 328 |
int heim_db_register(const char *dbtype, |
| 329 |
void *data, |
| 330 |
struct heim_db_type *plugin); |
| 331 |
|
| 332 |
heim_db_t heim_db_create(const char *dbtype, const char *dbname, |
| 333 |
heim_dict_t options, heim_error_t *error); |
| 334 |
heim_db_t heim_db_clone(heim_db_t, heim_error_t *); |
| 335 |
int heim_db_begin(heim_db_t, int, heim_error_t *); |
| 336 |
int heim_db_commit(heim_db_t, heim_error_t *); |
| 337 |
int heim_db_rollback(heim_db_t, heim_error_t *); |
| 338 |
heim_tid_t heim_db_get_type_id(void); |
| 339 |
|
| 340 |
int heim_db_set_value(heim_db_t, heim_string_t, heim_data_t, heim_data_t, |
| 341 |
heim_error_t *); |
| 342 |
heim_data_t heim_db_copy_value(heim_db_t, heim_string_t, heim_data_t, |
| 343 |
heim_error_t *); |
| 344 |
int heim_db_delete_key(heim_db_t, heim_string_t, heim_data_t, |
| 345 |
heim_error_t *); |
| 346 |
void heim_db_iterate_f(heim_db_t, heim_string_t, void *, |
| 347 |
heim_db_iterator_f_t, heim_error_t *); |
| 348 |
#ifdef __BLOCKS__ |
| 349 |
void heim_db_iterate(heim_db_t, heim_string_t, |
| 350 |
void (^)(heim_data_t, heim_data_t), heim_error_t *); |
| 351 |
#endif |
| 352 |
|
| 353 |
|
| 354 |
/* |
| 355 |
* Number |
| 356 |
*/ |
| 357 |
|
| 358 |
typedef struct heim_number_data *heim_number_t; |
| 359 |
|
| 360 |
heim_number_t heim_number_create(int); |
| 361 |
heim_tid_t heim_number_get_type_id(void); |
| 362 |
int heim_number_get_int(heim_number_t); |
| 363 |
|
| 364 |
/* |
| 365 |
* |
| 366 |
*/ |
| 367 |
|
| 368 |
typedef struct heim_auto_release * heim_auto_release_t; |
| 369 |
|
| 370 |
heim_auto_release_t heim_auto_release_create(void); |
| 371 |
void heim_auto_release_drain(heim_auto_release_t); |
| 372 |
heim_object_t heim_auto_release(heim_object_t); |
| 373 |
|
| 374 |
/* |
| 375 |
* JSON |
| 376 |
*/ |
| 377 |
typedef enum heim_json_flags { |
| 378 |
HEIM_JSON_F_NO_C_NULL = 1, |
| 379 |
HEIM_JSON_F_STRICT_STRINGS = 2, |
| 380 |
HEIM_JSON_F_NO_DATA = 4, |
| 381 |
HEIM_JSON_F_NO_DATA_DICT = 8, |
| 382 |
HEIM_JSON_F_STRICT_DICT = 16, |
| 383 |
HEIM_JSON_F_STRICT = 31, |
| 384 |
HEIM_JSON_F_CNULL2JSNULL = 32, |
| 385 |
HEIM_JSON_F_TRY_DECODE_DATA = 64, |
| 386 |
HEIM_JSON_F_ONE_LINE = 128 |
| 387 |
} heim_json_flags_t; |
| 388 |
|
| 389 |
heim_object_t heim_json_create(const char *, size_t, heim_json_flags_t, |
| 390 |
heim_error_t *); |
| 391 |
heim_object_t heim_json_create_with_bytes(const void *, size_t, size_t, |
| 392 |
heim_json_flags_t, |
| 393 |
heim_error_t *); |
| 394 |
heim_string_t heim_json_copy_serialize(heim_object_t, heim_json_flags_t, |
| 395 |
heim_error_t *); |
| 396 |
|
| 397 |
|
| 398 |
/* |
| 399 |
* Debug |
| 400 |
*/ |
| 401 |
|
| 402 |
heim_string_t |
| 403 |
heim_description(heim_object_t ptr); |
| 404 |
|
| 405 |
/* |
| 406 |
* Binary search. |
| 407 |
* |
| 408 |
* Note: these are private until integrated into the heimbase object system. |
| 409 |
*/ |
| 410 |
typedef struct bsearch_file_handle *bsearch_file_handle; |
| 411 |
int _bsearch_text(const char *buf, size_t buf_sz, const char *key, |
| 412 |
char **value, size_t *location, size_t *loops); |
| 413 |
int _bsearch_file_open(const char *fname, size_t max_sz, size_t page_sz, |
| 414 |
bsearch_file_handle *bfh, size_t *reads); |
| 415 |
int _bsearch_file(bsearch_file_handle bfh, const char *key, char **value, |
| 416 |
size_t *location, size_t *loops, size_t *reads); |
| 417 |
void _bsearch_file_info(bsearch_file_handle bfh, size_t *page_sz, |
| 418 |
size_t *max_sz, int *blockwise); |
| 419 |
void _bsearch_file_close(bsearch_file_handle *bfh); |
| 420 |
|
| 421 |
/* |
| 422 |
* Thread-specific keys |
| 423 |
*/ |
| 424 |
|
| 425 |
int heim_w32_key_create(unsigned long *, void (*)(void *)); |
| 426 |
int heim_w32_delete_key(unsigned long); |
| 427 |
int heim_w32_setspecific(unsigned long, void *); |
| 428 |
void *heim_w32_getspecific(unsigned long); |
| 429 |
void heim_w32_service_thread_detach(void *); |
| 430 |
|
| 431 |
#endif /* HEIM_BASE_H */ |