update wavesaudio backend, now supports Windows (ASIO) as well as OS X (CoreAudio)
[ardour.git] / libs / backends / wavesaudio / portmidi / src / pm_mac / readbinaryplist.c
index bccd09518310cace8b6fbba688d133639181ef39..d8ed8fbabc8e342deab86c66f09384928e41ced0 100644 (file)
-/*\r
-\r
-readbinaryplist.c -- Roger B. Dannenberg, Jun 2008\r
-Based on ReadBinaryPList.m by Jens Ayton, 2007\r
-\r
-Note that this code is intended to read preference files and has an upper\r
-bound on file size (currently 100MB) and assumes in some places that 32 bit\r
-offsets are sufficient.\r
-\r
-Here are his comments:\r
-\r
-Reader for binary property list files (version 00).\r
-\r
-This has been found to work on all 566 binary plists in my ~/Library/Preferences/\r
-and /Library/Preferences/ directories. This probably does not provide full\r
-test coverage. It has also been found to provide different data to Apple's\r
-implementation when presented with a key-value archive. This is because Apple's\r
-implementation produces undocumented CFKeyArchiverUID objects. My implementation\r
-produces dictionaries instead, matching the in-file representation used in XML\r
-and OpenStep plists. See extract_uid().\r
-\r
-Full disclosure: in implementing this software, I read one comment and one\r
-struct defintion in CFLite, Apple's implementation, which is under the APSL\r
-license. I also deduced the information about CFKeyArchiverUID from that code.\r
-However, none of the implementation was copied.\r
-\r
-Copyright (C) 2007 Jens Ayton\r
-\r
-Permission is hereby granted, free of charge, to any person obtaining a copy\r
-of this software and associated documentation files (the "Software"), to deal\r
-in the Software without restriction, including without limitation the rights\r
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-copies of the Software, and to permit persons to whom the Software is\r
-furnished to do so, subject to the following conditions:\r
-\r
-The above copyright notice and this permission notice shall be included in all\r
-copies or substantial portions of the Software.\r
-\r
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
-SOFTWARE.\r
-\r
-*/\r
-\r
-/* A note about memory management:\r
-Strings and possibly other values are unique and because the values\r
-associated with IDs are cached, you end up with a directed graph rather\r
-than a tree. It is tricky to free the data because if you do a simple\r
-depth-first search to free nodes, you will free nodes twice. I decided\r
-to allocate memory from blocks of 1024 bytes and keep the blocks in a\r
-list associated with but private to this module. So the user should\r
-access this module by calling:\r
-    bplist_read_file() or bplist_read_user_pref() or \r
-    bplist_read_system_pref()\r
-which returns a value. When you are done with the value, call\r
-    bplist_free_data()\r
-This will of course free the value_ptr returned by bplist_read_*()\r
-\r
-To deal with memory exhaustion (what happens when malloc returns\r
-NULL?), use setjmp/longjmp -- a single setjmp protects the whole\r
-parser, and allocate uses longjmp to abort. After abort, memory\r
-is freed and NULL is returned to caller. There is not much here\r
-in the way of error reporting.\r
-\r
-Memory is obtained by calling allocate which either returns the\r
-memory requested or calls longjmp, so callers don't have to check.\r
-\r
-*/\r
-\r
-#include <sys/types.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <assert.h>\r
-#include <stdio.h>\r
-#include <sys/stat.h>\r
-#include "readbinaryplist.h"\r
-#include <Carbon/Carbon.h>\r
-\r
-#define NO 0\r
-#define YES 1\r
-#define BOOL int\r
-\r
-#define MAXPATHLEN 256\r
-\r
-/* there are 2 levels of error logging/printing:\r
- *   BPLIST_LOG and BPLIST_LOG_VERBOSE\r
- * either or both can be set to non-zero to turn on\r
- * If BPLIST_LOG_VERBOSE is true, then BPLIST_LOG \r
- * is also true.\r
- * \r
- * In the code, logging is done by calling either\r
- * bplist_log() or bplist_log_verbose(), which take\r
- * parameters like printf but might be a no-op.\r
- */\r
\r
-/* #define BPLIST_LOG_VERBOSE 1 */\r
-\r
-#if BPLIST_LOG_VERBOSE\r
-    #ifndef BPLIST_LOG\r
-        #define BPLIST_LOG 1\r
-    #endif\r
-#endif\r
-\r
-#if BPLIST_LOG\r
-    #define bplist_log printf\r
-#else\r
-    #define bplist_log(...)\r
-#endif\r
-\r
-#if BPLIST_LOG_VERBOSE\r
-    #define bplist_log_verbose bplist_log\r
-#else\r
-    #define bplist_log_verbose(...)\r
-#endif\r
-\r
-\r
-/********* MEMORY MANAGEMENT ********/\r
-#define BLOCK_SIZE 1024\r
-// memory is aligned to multiples of this; assume malloc automatically\r
-// aligns to this number and assume this number is > sizeof(void *)\r
-#define ALIGNMENT 8\r
-static void *block_list = NULL;\r
-static char *free_ptr = NULL;\r
-static char *end_ptr = NULL;\r
-static jmp_buf abort_parsing;\r
-\r
-static void *allocate(size_t size)\r
-{\r
-    void *result;\r
-    if (free_ptr + size > end_ptr) {\r
-        size_t how_much = BLOCK_SIZE;\r
-        // align everything to 8 bytes\r
-        if (size > BLOCK_SIZE - ALIGNMENT) {\r
-            how_much = size + ALIGNMENT;\r
-        }\r
-        result = malloc(how_much);\r
-        if (result == NULL) {\r
-            /* serious problem */\r
-            longjmp(abort_parsing, 1);\r
-        }\r
-        *((void **)result) = block_list;\r
-        block_list = result;\r
-        free_ptr = ((char *) result) + ALIGNMENT;\r
-        end_ptr = ((char *) result) + how_much;\r
-    }\r
-    // now, there is enough rooom at free_ptr\r
-    result = free_ptr;\r
-    free_ptr += size;\r
-    return result;\r
-}\r
-\r
-void bplist_free_data()\r
-{\r
-    while (block_list) {\r
-        void *next = *(void **)block_list;\r
-        free(block_list);\r
-        block_list = next;\r
-    }\r
-    free_ptr = NULL;\r
-    end_ptr = NULL;\r
-}\r
-\r
-// layout of trailer -- last 32 bytes in plist data\r
-    uint8_t unused[6];\r
-    uint8_t offset_int_size;\r
-    uint8_t object_ref_size;\r
-    uint64_t object_count;\r
-    uint64_t top_level_object;\r
-    uint64_t offset_table_offset;\r
-\r
-\r
-enum\r
-{\r
-    kHEADER_SIZE = 8,\r
-    kTRAILER_SIZE = 32, //sizeof(bplist_trailer_node),\r
-    kMINIMUM_SANE_SIZE = kHEADER_SIZE + kTRAILER_SIZE\r
-};\r
-\r
-\r
-static const char kHEADER_BYTES[kHEADER_SIZE] = "bplist00";\r
-\r
-// map from UID key to previously parsed value\r
-typedef struct cache_struct {\r
-    uint64_t key;\r
-    value_ptr value;\r
-    struct cache_struct *next;\r
-} cache_node, *cache_ptr;\r
-\r
-\r
-typedef struct bplist_info\r
-{\r
-    uint64_t object_count;\r
-    const uint8_t *data_bytes;\r
-    uint64_t length;\r
-    uint64_t offset_table_offset;\r
-    uint8_t offset_int_size;\r
-    uint8_t object_ref_size;\r
-    cache_ptr cache;\r
-} bplist_info_node, *bplist_info_ptr;\r
-\r
-\r
-static value_ptr bplist_read_pldata(pldata_ptr data);\r
-static value_ptr bplist_read_pref(char *filename, OSType folder_type);\r
-static uint64_t read_sized_int(bplist_info_ptr bplist, uint64_t offset, uint8_t size);\r
-static uint64_t read_offset(bplist_info_ptr bplist, uint64_t index);\r
-static BOOL read_self_sized_int(bplist_info_ptr bplist, uint64_t offset, uint64_t *outValue, size_t *outSize);\r
-\r
-static value_ptr extract_object(bplist_info_ptr bplist, uint64_t objectRef);\r
-static value_ptr extract_simple(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_int(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_real(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_date(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_data(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_ascii_string(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_unicode_string(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_uid(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_array(bplist_info_ptr bplist, uint64_t offset);\r
-static value_ptr extract_dictionary(bplist_info_ptr bplist, uint64_t offset);\r
-\r
-\r
-value_ptr value_create()\r
-{\r
-    value_ptr value = (value_ptr) allocate(sizeof(value_node));\r
-    return value;\r
-}\r
-\r
-\r
-void value_set_integer(value_ptr v, int64_t i) {\r
-    v->tag = kTAG_INT; v->integer = i;\r
-}\r
-\r
-void value_set_real(value_ptr v, double d) {\r
-    v->tag = kTAG_REAL; v->real = d;\r
-}\r
-\r
-// d is seconds since 1 January 2001\r
-void value_set_date(value_ptr v, double d) {\r
-    v->tag = kTAG_DATE; v->real = d;\r
-}\r
-\r
-void value_set_ascii_string(value_ptr v, const uint8_t *s, size_t len) {\r
-    v->tag = kTAG_ASCIISTRING;\r
-    v->string = (char *) allocate(len + 1);\r
-    memcpy(v->string, s, len);\r
-    v->string[len] = 0;\r
-}\r
-\r
-void value_set_unicode_string(value_ptr v, const uint8_t *s, size_t len) {\r
-    v->tag = kTAG_UNICODESTRING;\r
-    v->string = (char *) allocate(len + 1);\r
-    memcpy(v->string, s, len);\r
-    v->string[len] = 0;\r
-}\r
-\r
-void value_set_uid(value_ptr v, uint64_t uid)\r
-{\r
-    v->tag = kTAG_UID; v->uinteger = uid;\r
-}\r
-\r
-// v->data points to a pldata that points to the actual bytes\r
-// the bytes are copied, so caller must free byte source (*data)\r
-void value_set_data(value_ptr v, const uint8_t *data, size_t len) {\r
-    v->tag = kTAG_DATA;\r
-    pldata_ptr pldata = (pldata_ptr) allocate(sizeof(pldata_node));\r
-    pldata->data = (uint8_t *) allocate(len);\r
-    memcpy(pldata->data, data, len);\r
-    pldata->len = len;\r
-    v->data = pldata;\r
-    printf("value at %p gets data at %p\n", v, pldata);\r
-}\r
-\r
-// caller releases ownership of array to value_ptr v\r
-void value_set_array(value_ptr v, value_ptr *array, size_t length) {\r
-    array_ptr a = (array_ptr) allocate(sizeof(array_node));\r
-    a->array = array;\r
-    a->length = length;\r
-    v->tag = kTAG_ARRAY;\r
-    v->array = a;\r
-}\r
-\r
-// caller releases ownership of dict to value_ptr v\r
-void value_set_dict(value_ptr v, dict_ptr dict) {\r
-    v->tag = kTAG_DICTIONARY;\r
-    v->dict = dict;\r
-}\r
-\r
-\r
-// look up an objectref in the cache, a ref->value_ptr mapping\r
-value_ptr cache_lookup(cache_ptr cache, uint64_t ref)\r
-{\r
-    while (cache) {\r
-        if (cache->key == ref) {\r
-            return cache->value;\r
-        }\r
-        cache = cache->next;\r
-    }\r
-    return NULL;\r
-}\r
-\r
-\r
-// insert an objectref and value in the cache\r
-void cache_insert(cache_ptr *cache, uint64_t ref, value_ptr value)\r
-{\r
-    cache_ptr c = (cache_ptr) allocate(sizeof(cache_node));\r
-    c->key = ref;\r
-    c->value = value;\r
-    c->next = *cache;\r
-    *cache = c;\r
-}\r
-\r
-\r
-// insert an objectref and value in a dictionary\r
-void dict_insert(dict_ptr *dict, value_ptr key, value_ptr value)\r
-{\r
-    dict_ptr d = (dict_ptr) allocate(sizeof(dict_node));\r
-    d->key = key;\r
-    d->value = value;\r
-    d->next = *dict;\r
-    *dict = d;\r
-}\r
-\r
-\r
-BOOL is_binary_plist(pldata_ptr data)\r
-{\r
-    if (data->len < kMINIMUM_SANE_SIZE)  return NO;\r
-    return memcmp(data->data, kHEADER_BYTES, kHEADER_SIZE) == 0;\r
-}\r
-\r
-\r
-value_ptr bplist_read_file(char *filename)\r
-{\r
-    struct stat stbuf;\r
-    pldata_node pldata;\r
-    FILE *file;\r
-    size_t n;\r
-    value_ptr value;\r
-    int rslt = stat(filename, &stbuf);\r
-    if (rslt) {\r
-        #if BPLIST_LOG\r
-            perror("in stat");\r
-        #endif\r
-        bplist_log("Could not stat %s, error %d\n", filename, rslt);\r
-        return NULL;\r
-    }\r
-    // if file is >100MB, assume it is not a preferences file and give up\r
-    if (stbuf.st_size > 100000000) {\r
-        bplist_log("Large file %s encountered (%llu bytes) -- not read\n",\r
-                   filename, stbuf.st_size);\r
-        return NULL;\r
-    }\r
-    pldata.len = (size_t) stbuf.st_size;\r
-    // note: this is supposed to be malloc, not allocate. It is separate\r
-    // from the graph structure, large, and easy to free right after\r
-    // parsing.\r
-    pldata.data = (uint8_t *) malloc(pldata.len);\r
-    if (!pldata.data) {\r
-        bplist_log("Could not allocate %lu bytes for %s\n",\r
-                   (unsigned long) pldata.len, filename);\r
-        return NULL;\r
-    }\r
-    file = fopen(filename, "rb");\r
-    if (!file) {\r
-        bplist_log("Could not open %s\n", filename);\r
-        return NULL;\r
-    }\r
-    n = fread(pldata.data, 1, pldata.len, file);\r
-    if (n != pldata.len) {\r
-        bplist_log("Error reading from %s\n", filename);\r
-        return NULL;\r
-    }\r
-    value = bplist_read_pldata(&pldata);\r
-    free(pldata.data);\r
-    return value;\r
-}\r
-\r
-\r
-value_ptr bplist_read_pref(char *filename, OSType folder_type)\r
-{\r
-    FSRef prefdir;\r
-    char cstr[MAXPATHLEN];\r
-\r
-    OSErr err = FSFindFolder(kOnAppropriateDisk, folder_type,\r
-                             FALSE, &prefdir);\r
-    if (err) {\r
-        bplist_log("Error finding preferences folder: %d\n", err);\r
-        return NULL;\r
-    }\r
-    err = FSRefMakePath(&prefdir, (UInt8 *) cstr, (UInt32) (MAXPATHLEN - 1));\r
-    if (err) {\r
-        bplist_log("Error making path name for preferences folder: %d\n", err);\r
-        return NULL;\r
-    }\r
-    strlcat(cstr, "/", MAXPATHLEN);\r
-    strlcat(cstr, filename, MAXPATHLEN);\r
-    return bplist_read_file(cstr);\r
-}\r
-\r
-\r
-value_ptr bplist_read_system_pref(char *filename) {\r
-    return bplist_read_pref(filename, kSystemPreferencesFolderType);\r
-}\r
-\r
-\r
-value_ptr bplist_read_user_pref(char *filename) {\r
-    return bplist_read_pref(filename, kPreferencesFolderType);\r
-}\r
-\r
-\r
-// data is stored with high-order bytes first.\r
-// read from plist data in a machine-independent fashion\r
-//\r
-uint64_t convert_uint64(uint8_t *ptr)\r
-{\r
-    uint64_t rslt = 0;\r
-    int i;\r
-    // shift in bytes, high-order first\r
-    for (i = 0; i < sizeof(uint64_t); i++) {\r
-        rslt <<= 8;\r
-        rslt += ptr[i];\r
-    }\r
-    return rslt;\r
-}\r
-\r
-\r
-value_ptr bplist_read_pldata(pldata_ptr data)\r
-{\r
-    value_ptr result = NULL;\r
-    bplist_info_node bplist;\r
-    uint8_t *ptr;\r
-    uint64_t top_level_object;\r
-    int i;\r
-\r
-    if (data == NULL)  return NULL;\r
-    if (!is_binary_plist(data)) {\r
-        bplist_log("Bad binary plist: too short or invalid header.\n");\r
-        return NULL;\r
-    }\r
-        \r
-    // read trailer\r
-    ptr = (uint8_t *) (data->data + data->len - kTRAILER_SIZE);\r
-    bplist.offset_int_size = ptr[6];\r
-    bplist.object_ref_size = ptr[7];\r
-    bplist.object_count = convert_uint64(ptr + 8);\r
-    top_level_object = convert_uint64(ptr + 16);\r
-    bplist.offset_table_offset = convert_uint64(ptr + 24);\r
-        \r
-    // Basic sanity checks\r
-    if (bplist.offset_int_size < 1 || bplist.offset_int_size > 8 ||\r
-        bplist.object_ref_size < 1 || bplist.object_ref_size > 8 ||\r
-        bplist.offset_table_offset < kHEADER_SIZE) {\r
-        bplist_log("Bad binary plist: trailer declared insane.\n");\r
-        return NULL;                \r
-    }\r
-        \r
-    // Ensure offset table is inside file\r
-    uint64_t offsetTableSize = bplist.offset_int_size * bplist.object_count;\r
-    if (offsetTableSize + bplist.offset_table_offset + kTRAILER_SIZE > \r
-        data->len) {\r
-        bplist_log("Bad binary plist: offset table overlaps end of container.\n");\r
-        return NULL;\r
-    }\r
-        \r
-    bplist.data_bytes = data->data;\r
-    bplist.length = data->len;\r
-    bplist.cache = NULL; /* dictionary is empty */\r
-\r
-    bplist_log_verbose("Got a sane bplist with %llu items, offset_int_size: %u, object_ref_size: %u\n", \r
-                      bplist.object_count, bplist.offset_int_size, \r
-                      bplist.object_ref_size);\r
-    /* at this point, we are ready to do some parsing which allocates\r
-        memory for the result data structure. If memory allocation (using\r
-        allocate fails, a longjmp will return to here and we simply give up\r
-     */\r
-    i = setjmp(abort_parsing);\r
-    if (i == 0) {\r
-        result = extract_object(&bplist, top_level_object);\r
-    } else {\r
-        bplist_log("allocate() failed to allocate memory. Giving up.\n");\r
-        result = NULL;\r
-    }\r
-    if (!result) {\r
-        bplist_free_data();\r
-    }\r
-    return result;\r
-}\r
-\r
-\r
-static value_ptr extract_object(bplist_info_ptr bplist, uint64_t objectRef)\r
-{\r
-    uint64_t offset;\r
-    value_ptr result = NULL;\r
-    uint8_t objectTag;\r
-    \r
-    if (objectRef >= bplist->object_count) {\r
-        // Out-of-range object reference.\r
-        bplist_log("Bad binary plist: object index is out of range.\n");\r
-        return NULL;\r
-    }\r
-        \r
-    // Use cached object if it exists\r
-    result = cache_lookup(bplist->cache, objectRef);\r
-    if (result != NULL)  return result;\r
-        \r
-    // Otherwise, find object in file.\r
-    offset = read_offset(bplist, objectRef);\r
-    if (offset > bplist->length) {\r
-        // Out-of-range offset.\r
-        bplist_log("Bad binary plist: object outside container.\n");\r
-        return NULL;\r
-    }\r
-    objectTag = *(bplist->data_bytes + offset);\r
-    switch (objectTag & 0xF0) {\r
-    case kTAG_SIMPLE:\r
-        result = extract_simple(bplist, offset);\r
-        break;\r
-                \r
-    case kTAG_INT:\r
-        result = extract_int(bplist, offset);\r
-        break;\r
-                        \r
-    case kTAG_REAL:\r
-        result = extract_real(bplist, offset);\r
-        break;\r
-                        \r
-    case kTAG_DATE:\r
-        result = extract_date(bplist, offset);\r
-        break;\r
-                        \r
-    case kTAG_DATA:\r
-        result = extract_data(bplist, offset);\r
-        break;\r
-                        \r
-    case kTAG_ASCIISTRING:\r
-        result = extract_ascii_string(bplist, offset);\r
-        break;\r
-                        \r
-    case kTAG_UNICODESTRING:\r
-        result = extract_unicode_string(bplist, offset);\r
-        break;\r
-        \r
-    case kTAG_UID:\r
-        result = extract_uid(bplist, offset);\r
-        break;\r
-        \r
-    case kTAG_ARRAY:\r
-        result = extract_array(bplist, offset);\r
-        break;\r
-        \r
-    case kTAG_DICTIONARY:\r
-        result = extract_dictionary(bplist, offset);\r
-        break;\r
-        \r
-    default:\r
-        // Unknown tag.\r
-        bplist_log("Bad binary plist: unknown tag 0x%X.\n", \r
-                   (objectTag & 0x0F) >> 4);\r
-        result = NULL;\r
-    }\r
-    \r
-    // Cache and return result.\r
-    if (result != NULL)  \r
-        cache_insert(&bplist->cache, objectRef, result);\r
-    return result;\r
-}\r
-\r
-\r
-static uint64_t read_sized_int(bplist_info_ptr bplist, uint64_t offset, \r
-                               uint8_t size)\r
-{\r
-    assert(bplist->data_bytes != NULL && size >= 1 && size <= 8 && \r
-           offset + size <= bplist->length);\r
-        \r
-    uint64_t result = 0;\r
-    const uint8_t *byte = bplist->data_bytes + offset;\r
-        \r
-    do {\r
-        // note that ints seem to be high-order first\r
-        result = (result << 8) | *byte++;\r
-    } while (--size);\r
-        \r
-    return result;\r
-}\r
-\r
-\r
-static uint64_t read_offset(bplist_info_ptr bplist, uint64_t index)\r
-{\r
-    assert(index < bplist->object_count);\r
-        \r
-    return read_sized_int(bplist, \r
-            bplist->offset_table_offset + bplist->offset_int_size * index, \r
-            bplist->offset_int_size);\r
-}\r
-\r
-\r
-static BOOL read_self_sized_int(bplist_info_ptr bplist, uint64_t offset, \r
-                             uint64_t *outValue, size_t *outSize)\r
-{\r
-    uint32_t size;\r
-    int64_t value;\r
-        \r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-        \r
-    size = 1 << (bplist->data_bytes[offset] & 0x0F);\r
-    if (size > 8) {\r
-        // Maximum allowable size in this implementation is 1<<3 = 8 bytes.\r
-        // This also happens to be the biggest we can handle.\r
-        return NO;\r
-    }\r
-        \r
-    if (offset + 1 + size > bplist->length) {\r
-        // Out of range.\r
-        return NO;\r
-    }\r
-        \r
-    value = read_sized_int(bplist, offset + 1, size);\r
-    \r
-    if (outValue != NULL) *outValue = value;\r
-    if (outSize != NULL) *outSize = size + 1; // +1 for tag byte.\r
-    return YES;\r
-}\r
-\r
-\r
-static value_ptr extract_simple(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-    value_ptr value = value_create();\r
-        \r
-    switch (bplist->data_bytes[offset]) {\r
-    case kVALUE_NULL:\r
-        value->tag = kVALUE_NULL;\r
-        return value;\r
-        \r
-    case kVALUE_TRUE:\r
-        value->tag = kVALUE_TRUE;\r
-        return value;\r
-                        \r
-    case kVALUE_FALSE:\r
-        value->tag = kVALUE_FALSE;\r
-        return value;\r
-    }\r
-        \r
-    // Note: kVALUE_FILLER is treated as invalid, because it, er, is.\r
-    bplist_log("Bad binary plist: invalid atom.\n");\r
-    free(value);\r
-    return NULL;\r
-}\r
-\r
-\r
-static value_ptr extract_int(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    value_ptr value = value_create();\r
-    value->tag = kTAG_INT;\r
-\r
-    if (!read_self_sized_int(bplist, offset, &value->uinteger, NULL)) {\r
-        bplist_log("Bad binary plist: invalid integer object.\n");\r
-    }\r
-        \r
-    /* NOTE: originally, I sign-extended here. This was the wrong thing; it\r
-       turns out that negative ints are always stored as 64-bit, and smaller\r
-       ints are unsigned.\r
-    */\r
-    return value;\r
-}\r
-\r
-\r
-static value_ptr extract_real(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    value_ptr value = value_create();\r
-    uint32_t size;\r
-        \r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-    \r
-    size = 1 << (bplist->data_bytes[offset] & 0x0F);\r
-        \r
-    // FIXME: what to do if faced with other sizes for float/double?\r
-    assert (sizeof (float) == sizeof (uint32_t) && \r
-            sizeof (double) == sizeof (uint64_t));\r
-        \r
-    if (offset + 1 + size > bplist->length) {\r
-        bplist_log("Bad binary plist: %s object overlaps end of container.\n", \r
-                  "floating-point number");\r
-        free(value);\r
-        return NULL;\r
-    }\r
-        \r
-    if (size == sizeof (float)) {\r
-        // cast is ok because we know size is 4 bytes\r
-        uint32_t i = (uint32_t) read_sized_int(bplist, offset + 1, size); \r
-        // Note that this handles byte swapping.\r
-        value_set_real(value, *(float *)&i);\r
-        return value;\r
-    } else if (size == sizeof (double)) {\r
-        uint64_t i = read_sized_int(bplist, offset + 1, size);\r
-        // Note that this handles byte swapping.\r
-        value_set_real(value, *(double *)&i);\r
-        return value;\r
-    } else {\r
-        // Can't handle floats of other sizes.\r
-        bplist_log("Bad binary plist: can't handle %u-byte float.\n", size);\r
-        free(value);\r
-        return NULL;\r
-    }\r
-}\r
-\r
-\r
-static value_ptr extract_date(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    value_ptr value;\r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-        \r
-    // Data has size code like int and real, but only 3 (meaning 8 bytes) is valid.\r
-    if (bplist->data_bytes[offset] != kVALUE_FULLDATETAG) {\r
-        bplist_log("Bad binary plist: invalid size for date object.\n");\r
-        return NULL;\r
-    }\r
-        \r
-    if (offset + 1 + sizeof (double) > bplist->length) {\r
-        bplist_log("Bad binary plist: %s object overlaps end of container.\n", \r
-                  "date");\r
-        return NULL;\r
-    }\r
-        \r
-    // FIXME: what to do if faced with other sizes for double?\r
-    assert (sizeof (double) == sizeof (uint64_t));\r
-        \r
-    uint64_t date = read_sized_int(bplist, offset + 1, sizeof(double));\r
-    // Note that this handles byte swapping.\r
-    value = value_create();\r
-    value_set_date(value, *(double *)&date);\r
-    return value;\r
-}\r
-\r
-\r
-uint64_t bplist_get_a_size(bplist_info_ptr bplist, \r
-                           uint64_t *offset_ptr, char *msg)\r
-{\r
-    uint64_t size = bplist->data_bytes[*offset_ptr] & 0x0F;\r
-    (*offset_ptr)++;\r
-    if (size == 0x0F) {\r
-        // 0x0F means separate int size follows. \r
-        // Smaller values are used for short data.\r
-        size_t extra; // the length of the data size we are about to read\r
-        if ((bplist->data_bytes[*offset_ptr] & 0xF0) != kTAG_INT) {\r
-            // Bad data, mistagged size int\r
-            bplist_log("Bad binary plist: %s object size is not tagged as int.\n",\r
-                       msg);\r
-            return UINT64_MAX; // error\r
-        }\r
-                \r
-        // read integer data as size, extra tells how many bytes to skip\r
-        if (!read_self_sized_int(bplist, *offset_ptr, &size, &extra)) {\r
-            bplist_log("Bad binary plist: invalid %s object size tag.\n", \r
-                      "data");\r
-            return UINT64_MAX; // error\r
-        }\r
-        (*offset_ptr) += extra;\r
-    }\r
-\r
-    if (*offset_ptr + size > bplist->length) {\r
-        bplist_log("Bad binary plist: %s object overlaps end of container.\n", \r
-                  "data");\r
-        return UINT64_MAX; // error\r
-    }\r
-    return size;\r
-}\r
-\r
-\r
-static value_ptr extract_data(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    uint64_t size;\r
-    value_ptr value;\r
-        \r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-        \r
-    if ((size = bplist_get_a_size(bplist, &offset, "data")) == UINT64_MAX) \r
-        return NULL;\r
-        \r
-    value = value_create();\r
-    // cast is ok because we only allow files up to 100MB:\r
-    value_set_data(value, bplist->data_bytes + (size_t) offset, (size_t) size);\r
-    return value;\r
-}\r
-\r
-\r
-static value_ptr extract_ascii_string(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    uint64_t size;\r
-    value_ptr value; // return value\r
-        \r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-        \r
-    if ((size = bplist_get_a_size(bplist, &offset, "ascii string")) ==\r
-        UINT64_MAX) \r
-        return NULL;\r
-\r
-    value = value_create();\r
-    // cast is ok because we only allow 100MB files\r
-    value_set_ascii_string(value, bplist->data_bytes + (size_t) offset, \r
-                           (size_t) size);\r
-    return value;\r
-}\r
-\r
-\r
-static value_ptr extract_unicode_string(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    uint64_t size;\r
-    value_ptr value;\r
-        \r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-        \r
-    if ((size = bplist_get_a_size(bplist, &offset, "unicode string")) == \r
-        UINT64_MAX)\r
-        return NULL;\r
-        \r
-    value = value_create();\r
-    // cast is ok because we only allow 100MB files\r
-    value_set_unicode_string(value, bplist->data_bytes + (size_t) offset, \r
-                             (size_t) size);\r
-    return value;\r
-}\r
-\r
-\r
-static value_ptr extract_uid(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    /* UIDs are used by Cocoa's key-value coder.\r
-       When writing other plist formats, they are expanded to dictionaries of\r
-       the form <dict><key>CF$UID</key><integer>value</integer></dict>, so we\r
-       do the same here on reading. This results in plists identical to what\r
-       running plutil -convert xml1 gives us. However, this is not the same\r
-       result as [Core]Foundation's plist parser, which extracts them as un-\r
-       introspectable CF objects. In fact, it even seems to convert the CF$UID\r
-       dictionaries from XML plists on the fly.\r
-    */\r
-        \r
-    value_ptr value;\r
-    uint64_t uid;\r
-        \r
-    if (!read_self_sized_int(bplist, offset, &uid, NULL)) {\r
-        bplist_log("Bad binary plist: invalid UID object.\n");\r
-        return NULL;\r
-    }\r
-        \r
-    // assert(NO); // original code suggests using a string for a key\r
-    // but our dictionaries all use big ints for keys, so I don't know\r
-    // what to do here\r
-    \r
-    // In practice, I believe this code is never executed by PortMidi.\r
-    // I changed it to do something and not raise compiler warnings, but\r
-    // not sure what the code should do.\r
-\r
-    value = value_create();\r
-    value_set_uid(value, uid);\r
-    // return [NSDictionary dictionaryWithObject:\r
-    //         [NSNumber numberWithUnsignedLongLong:value] \r
-    //         forKey:"CF$UID"];\r
-    return value;\r
-}\r
-\r
-\r
-static value_ptr extract_array(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    uint64_t i, count;\r
-    uint64_t size;\r
-    uint64_t elementID;\r
-    value_ptr element = NULL;\r
-    value_ptr *array = NULL;\r
-    value_ptr value = NULL;\r
-    BOOL ok = YES;\r
-        \r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-        \r
-    if ((count = bplist_get_a_size(bplist, &offset, "array")) == UINT64_MAX)\r
-        return NULL;\r
-        \r
-    if (count > UINT64_MAX / bplist->object_ref_size - offset) {\r
-        // Offset overflow.\r
-        bplist_log("Bad binary plist: %s object overlaps end of container.\n", \r
-                   "array");\r
-        return NULL;\r
-    }\r
-        \r
-    size = bplist->object_ref_size * count;\r
-    if (size + offset > bplist->length) {\r
-        bplist_log("Bad binary plist: %s object overlaps end of container.\n", \r
-                   "array");\r
-        return NULL;\r
-    }\r
-        \r
-    // got count, the number of array elements\r
-\r
-    value = value_create();\r
-    assert(value);\r
-\r
-    if (count == 0) {\r
-        // count must be size_t or smaller because max file size is 100MB\r
-        value_set_array(value, array, (size_t) count);\r
-        return value;\r
-    }\r
-        \r
-    array = allocate(sizeof(value_ptr) * (size_t) count);\r
-        \r
-    for (i = 0; i != count; ++i) {\r
-        bplist_log_verbose("[%u]\n", i);\r
-        elementID = read_sized_int(bplist, offset + i * bplist->object_ref_size, \r
-                                 bplist->object_ref_size);\r
-        element = extract_object(bplist, elementID);\r
-        if (element != NULL) {\r
-            array[i] = element;\r
-        } else {\r
-            ok = NO;\r
-            break;\r
-        }\r
-    }\r
-    if (ok) { // count is smaller than size_t max because of 100MB file limit\r
-        value_set_array(value, array, (size_t) count);\r
-    }\r
-\r
-    return value;\r
-}\r
-\r
-\r
-static value_ptr extract_dictionary(bplist_info_ptr bplist, uint64_t offset)\r
-{\r
-    uint64_t i, count;\r
-    uint64_t size;\r
-    uint64_t elementID;\r
-    value_ptr value = NULL;\r
-    dict_ptr dict = NULL;\r
-    BOOL ok = YES;\r
-        \r
-    assert(bplist->data_bytes != NULL && offset < bplist->length);\r
-        \r
-        \r
-    if ((count = bplist_get_a_size(bplist, &offset, "array")) == UINT64_MAX)\r
-        return NULL;\r
-\r
-    if (count > UINT64_MAX / (bplist->object_ref_size * 2) - offset) {\r
-        // Offset overflow.\r
-        bplist_log("Bad binary plist: %s object overlaps end of container.\n", \r
-                   "dictionary");\r
-        return NULL;\r
-    }\r
-    \r
-    size = bplist->object_ref_size * count * 2;\r
-    if (size + offset > bplist->length) {\r
-        bplist_log("Bad binary plist: %s object overlaps end of container.\n", \r
-                   "dictionary");\r
-        return NULL;\r
-    }\r
-    \r
-    value = value_create();\r
-    if (count == 0) {\r
-        value_set_dict(value, NULL);\r
-        return value;\r
-    }\r
-\r
-    for (i = 0; i != count; ++i) {\r
-        value_ptr key;\r
-        value_ptr val;\r
-        elementID = read_sized_int(bplist, offset + i * bplist->object_ref_size, \r
-                                 bplist->object_ref_size);\r
-        key = extract_object(bplist, elementID);\r
-        if (key != NULL) {\r
-            bplist_log_verbose("key: %p\n", key);\r
-        } else {\r
-            ok = NO;\r
-            break;\r
-        }\r
-                    \r
-        elementID = read_sized_int(bplist, \r
-                            offset + (i + count) * bplist->object_ref_size, \r
-                            bplist->object_ref_size);\r
-        val = extract_object(bplist, elementID);\r
-        if (val != NULL) {\r
-            dict_insert(&dict, key, val);\r
-        } else {\r
-            ok = NO;\r
-            break;\r
-        }\r
-    }\r
-    if (ok) {\r
-        value_set_dict(value, dict);\r
-    }\r
-    \r
-    return value;\r
-}\r
-\r
-/*************** functions for accessing values ****************/\r
-\r
-\r
-char *value_get_asciistring(value_ptr v)\r
-{\r
-    if (v->tag != kTAG_ASCIISTRING) return NULL;\r
-    return v->string;\r
-}\r
-\r
-\r
-value_ptr value_dict_lookup_using_string(value_ptr v, char *key)\r
-{\r
-    dict_ptr dict;\r
-    if (v->tag != kTAG_DICTIONARY) return NULL; // not a dictionary\r
-    dict = v->dict;\r
-    /* search for key */\r
-    while (dict) {\r
-        if (dict->key && dict->key->tag == kTAG_ASCIISTRING &&\r
-            strcmp(key, dict->key->string) == 0) { // found it\r
-            return dict->value;\r
-        }\r
-        dict = dict->next;\r
-    }\r
-    return NULL; /* not found */\r
-}\r
-\r
-value_ptr value_dict_lookup_using_path(value_ptr v, char *path)\r
-{\r
-    char key[MAX_KEY_SIZE];\r
-    while (*path) { /* more to the path */\r
-        int i = 0;\r
-        while (i < MAX_KEY_SIZE - 1) {\r
-            key[i] = *path++;\r
-            if (key[i] == '/') { /* end of entry in path */\r
-                key[i + 1] = 0;\r
-                break;\r
-            }\r
-            if (!key[i]) {\r
-                path--; /* back up to end of string char */\r
-                break;  /* this will cause outer loop to exit */\r
-            }\r
-            i++;\r
-        }\r
-        if (!v || v->tag != kTAG_DICTIONARY) return NULL;\r
-        /* now, look up the key to get next value */\r
-        v = value_dict_lookup_using_string(v, key);\r
-        if (v == NULL) return NULL;\r
-    }\r
-    return v;\r
-}\r
-                \r
-\r
-/*************** functions for debugging ***************/\r
-\r
-void plist_print(value_ptr v)\r
-{\r
-    size_t i;\r
-    int comma_needed;\r
-    dict_ptr dict;\r
-    if (!v) {\r
-        printf("NULL");\r
-        return;\r
-    }\r
-    switch (v->tag & 0xF0) {\r
-    case kTAG_SIMPLE:\r
-        switch (v->tag) {\r
-        case kVALUE_NULL: \r
-            printf("NULL@%p", v); break;\r
-        case kVALUE_FALSE: \r
-            printf("FALSE@%p", v); break;\r
-        case kVALUE_TRUE:\r
-            printf("TRUE@%p", v); break;\r
-        default:\r
-            printf("UNKNOWN tag=%x@%p", v->tag, v); break;\r
-        }\r
-        break;\r
-    case kTAG_INT:\r
-        printf("%lld@%p", v->integer, v); break;\r
-    case kTAG_REAL:\r
-        printf("%g@%p", v->real, v); break;\r
-    case kTAG_DATE:\r
-        printf("date:%g@%p", v->real, v); break;\r
-    case kTAG_DATA:\r
-        printf("data@%p->%p:[%p:", v, v->data, v->data->data);\r
-        for (i = 0; i < v->data->len; i++) {\r
-            printf(" %2x", v->data->data[i]);\r
-        }\r
-        printf("]"); break;\r
-    case kTAG_ASCIISTRING:\r
-        printf("%p:\"%s\"@%p", v->string, v->string, v); break;\r
-    case kTAG_UNICODESTRING:\r
-        printf("unicode:%p:\"%s\"@%p", v->string, v->string, v); break;\r
-    case kTAG_UID:\r
-        printf("UID:%llu@%p", v->uinteger, v); break;\r
-    case kTAG_ARRAY:\r
-        comma_needed = FALSE;\r
-        printf("%p->%p:[%p:", v, v->array, v->array->array);\r
-        for (i = 0; i < v->array->length; i++) {\r
-            if (comma_needed) printf(", ");\r
-            plist_print(v->array->array[i]);\r
-            comma_needed = TRUE;\r
-        }\r
-        printf("]"); break;\r
-    case kTAG_DICTIONARY:\r
-        comma_needed = FALSE;\r
-        printf("%p:[", v);\r
-        dict = v->dict;\r
-        while (dict) {\r
-            if (comma_needed) printf(", ");\r
-            printf("%p:", dict);\r
-            plist_print(dict->key);\r
-            printf("->");\r
-            plist_print(dict->value);\r
-            comma_needed = TRUE;\r
-            dict = dict->next;\r
-        }\r
-        printf("]"); break;\r
-    default:\r
-        printf("UNKNOWN tag=%x", v->tag);\r
-        break;\r
-    }\r
-}\r
-\r
-            \r
+/*
+
+readbinaryplist.c -- Roger B. Dannenberg, Jun 2008
+Based on ReadBinaryPList.m by Jens Ayton, 2007
+
+Note that this code is intended to read preference files and has an upper
+bound on file size (currently 100MB) and assumes in some places that 32 bit
+offsets are sufficient.
+
+Here are his comments:
+
+Reader for binary property list files (version 00).
+
+This has been found to work on all 566 binary plists in my ~/Library/Preferences/
+and /Library/Preferences/ directories. This probably does not provide full
+test coverage. It has also been found to provide different data to Apple's
+implementation when presented with a key-value archive. This is because Apple's
+implementation produces undocumented CFKeyArchiverUID objects. My implementation
+produces dictionaries instead, matching the in-file representation used in XML
+and OpenStep plists. See extract_uid().
+
+Full disclosure: in implementing this software, I read one comment and one
+struct defintion in CFLite, Apple's implementation, which is under the APSL
+license. I also deduced the information about CFKeyArchiverUID from that code.
+However, none of the implementation was copied.
+
+Copyright (C) 2007 Jens Ayton
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+*/
+
+/* A note about memory management:
+Strings and possibly other values are unique and because the values
+associated with IDs are cached, you end up with a directed graph rather
+than a tree. It is tricky to free the data because if you do a simple
+depth-first search to free nodes, you will free nodes twice. I decided
+to allocate memory from blocks of 1024 bytes and keep the blocks in a
+list associated with but private to this module. So the user should
+access this module by calling:
+    bplist_read_file() or bplist_read_user_pref() or 
+    bplist_read_system_pref()
+which returns a value. When you are done with the value, call
+    bplist_free_data()
+This will of course free the value_ptr returned by bplist_read_*()
+
+To deal with memory exhaustion (what happens when malloc returns
+NULL?), use setjmp/longjmp -- a single setjmp protects the whole
+parser, and allocate uses longjmp to abort. After abort, memory
+is freed and NULL is returned to caller. There is not much here
+in the way of error reporting.
+
+Memory is obtained by calling allocate which either returns the
+memory requested or calls longjmp, so callers don't have to check.
+
+*/
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include "readbinaryplist.h"
+#include <Carbon/Carbon.h>
+
+#define NO 0
+#define YES 1
+#define BOOL int
+
+#define MAXPATHLEN 256
+
+/* there are 2 levels of error logging/printing:
+ *   BPLIST_LOG and BPLIST_LOG_VERBOSE
+ * either or both can be set to non-zero to turn on
+ * If BPLIST_LOG_VERBOSE is true, then BPLIST_LOG 
+ * is also true.
+ * 
+ * In the code, logging is done by calling either
+ * bplist_log() or bplist_log_verbose(), which take
+ * parameters like printf but might be a no-op.
+ */
+/* #define BPLIST_LOG_VERBOSE 1 */
+
+#if BPLIST_LOG_VERBOSE
+    #ifndef BPLIST_LOG
+        #define BPLIST_LOG 1
+    #endif
+#endif
+
+#if BPLIST_LOG
+    #define bplist_log printf
+#else
+    #define bplist_log(...)
+#endif
+
+#if BPLIST_LOG_VERBOSE
+    #define bplist_log_verbose bplist_log
+#else
+    #define bplist_log_verbose(...)
+#endif
+
+
+/********* MEMORY MANAGEMENT ********/
+#define BLOCK_SIZE 1024
+// memory is aligned to multiples of this; assume malloc automatically
+// aligns to this number and assume this number is > sizeof(void *)
+#define ALIGNMENT 8
+static void *block_list = NULL;
+static char *free_ptr = NULL;
+static char *end_ptr = NULL;
+static jmp_buf abort_parsing;
+
+static void *allocate(size_t size)
+{
+    void *result;
+    if (free_ptr + size > end_ptr) {
+        size_t how_much = BLOCK_SIZE;
+        // align everything to 8 bytes
+        if (size > BLOCK_SIZE - ALIGNMENT) {
+            how_much = size + ALIGNMENT;
+        }
+        result = malloc(how_much);
+        if (result == NULL) {
+            /* serious problem */
+            longjmp(abort_parsing, 1);
+        }
+        *((void **)result) = block_list;
+        block_list = result;
+        free_ptr = ((char *) result) + ALIGNMENT;
+        end_ptr = ((char *) result) + how_much;
+    }
+    // now, there is enough rooom at free_ptr
+    result = free_ptr;
+    free_ptr += size;
+    return result;
+}
+
+void bplist_free_data()
+{
+    while (block_list) {
+        void *next = *(void **)block_list;
+        free(block_list);
+        block_list = next;
+    }
+    free_ptr = NULL;
+    end_ptr = NULL;
+}
+
+// layout of trailer -- last 32 bytes in plist data
+    uint8_t unused[6];
+    uint8_t offset_int_size;
+    uint8_t object_ref_size;
+    uint64_t object_count;
+    uint64_t top_level_object;
+    uint64_t offset_table_offset;
+
+
+enum
+{
+    kHEADER_SIZE = 8,
+    kTRAILER_SIZE = 32, //sizeof(bplist_trailer_node),
+    kMINIMUM_SANE_SIZE = kHEADER_SIZE + kTRAILER_SIZE
+};
+
+
+static const char kHEADER_BYTES[kHEADER_SIZE] = "bplist00";
+
+// map from UID key to previously parsed value
+typedef struct cache_struct {
+    uint64_t key;
+    value_ptr value;
+    struct cache_struct *next;
+} cache_node, *cache_ptr;
+
+
+typedef struct bplist_info
+{
+    uint64_t object_count;
+    const uint8_t *data_bytes;
+    uint64_t length;
+    uint64_t offset_table_offset;
+    uint8_t offset_int_size;
+    uint8_t object_ref_size;
+    cache_ptr cache;
+} bplist_info_node, *bplist_info_ptr;
+
+
+static value_ptr bplist_read_pldata(pldata_ptr data);
+static value_ptr bplist_read_pref(char *filename, OSType folder_type);
+static uint64_t read_sized_int(bplist_info_ptr bplist, uint64_t offset, uint8_t size);
+static uint64_t read_offset(bplist_info_ptr bplist, uint64_t index);
+static BOOL read_self_sized_int(bplist_info_ptr bplist, uint64_t offset, uint64_t *outValue, size_t *outSize);
+
+static value_ptr extract_object(bplist_info_ptr bplist, uint64_t objectRef);
+static value_ptr extract_simple(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_int(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_real(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_date(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_data(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_ascii_string(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_unicode_string(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_uid(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_array(bplist_info_ptr bplist, uint64_t offset);
+static value_ptr extract_dictionary(bplist_info_ptr bplist, uint64_t offset);
+
+
+value_ptr value_create()
+{
+    value_ptr value = (value_ptr) allocate(sizeof(value_node));
+    return value;
+}
+
+
+void value_set_integer(value_ptr v, int64_t i) {
+    v->tag = kTAG_INT; v->integer = i;
+}
+
+void value_set_real(value_ptr v, double d) {
+    v->tag = kTAG_REAL; v->real = d;
+}
+
+// d is seconds since 1 January 2001
+void value_set_date(value_ptr v, double d) {
+    v->tag = kTAG_DATE; v->real = d;
+}
+
+void value_set_ascii_string(value_ptr v, const uint8_t *s, size_t len) {
+    v->tag = kTAG_ASCIISTRING;
+    v->string = (char *) allocate(len + 1);
+    memcpy(v->string, s, len);
+    v->string[len] = 0;
+}
+
+void value_set_unicode_string(value_ptr v, const uint8_t *s, size_t len) {
+    v->tag = kTAG_UNICODESTRING;
+    v->string = (char *) allocate(len + 1);
+    memcpy(v->string, s, len);
+    v->string[len] = 0;
+}
+
+void value_set_uid(value_ptr v, uint64_t uid)
+{
+    v->tag = kTAG_UID; v->uinteger = uid;
+}
+
+// v->data points to a pldata that points to the actual bytes
+// the bytes are copied, so caller must free byte source (*data)
+void value_set_data(value_ptr v, const uint8_t *data, size_t len) {
+    v->tag = kTAG_DATA;
+    pldata_ptr pldata = (pldata_ptr) allocate(sizeof(pldata_node));
+    pldata->data = (uint8_t *) allocate(len);
+    memcpy(pldata->data, data, len);
+    pldata->len = len;
+    v->data = pldata;
+    printf("value at %p gets data at %p\n", v, pldata);
+}
+
+// caller releases ownership of array to value_ptr v
+void value_set_array(value_ptr v, value_ptr *array, size_t length) {
+    array_ptr a = (array_ptr) allocate(sizeof(array_node));
+    a->array = array;
+    a->length = length;
+    v->tag = kTAG_ARRAY;
+    v->array = a;
+}
+
+// caller releases ownership of dict to value_ptr v
+void value_set_dict(value_ptr v, dict_ptr dict) {
+    v->tag = kTAG_DICTIONARY;
+    v->dict = dict;
+}
+
+
+// look up an objectref in the cache, a ref->value_ptr mapping
+value_ptr cache_lookup(cache_ptr cache, uint64_t ref)
+{
+    while (cache) {
+        if (cache->key == ref) {
+            return cache->value;
+        }
+        cache = cache->next;
+    }
+    return NULL;
+}
+
+
+// insert an objectref and value in the cache
+void cache_insert(cache_ptr *cache, uint64_t ref, value_ptr value)
+{
+    cache_ptr c = (cache_ptr) allocate(sizeof(cache_node));
+    c->key = ref;
+    c->value = value;
+    c->next = *cache;
+    *cache = c;
+}
+
+
+// insert an objectref and value in a dictionary
+void dict_insert(dict_ptr *dict, value_ptr key, value_ptr value)
+{
+    dict_ptr d = (dict_ptr) allocate(sizeof(dict_node));
+    d->key = key;
+    d->value = value;
+    d->next = *dict;
+    *dict = d;
+}
+
+
+BOOL is_binary_plist(pldata_ptr data)
+{
+    if (data->len < kMINIMUM_SANE_SIZE)  return NO;
+    return memcmp(data->data, kHEADER_BYTES, kHEADER_SIZE) == 0;
+}
+
+
+value_ptr bplist_read_file(char *filename)
+{
+    struct stat stbuf;
+    pldata_node pldata;
+    FILE *file;
+    size_t n;
+    value_ptr value;
+    int rslt = stat(filename, &stbuf);
+    if (rslt) {
+        #if BPLIST_LOG
+            perror("in stat");
+        #endif
+        bplist_log("Could not stat %s, error %d\n", filename, rslt);
+        return NULL;
+    }
+    // if file is >100MB, assume it is not a preferences file and give up
+    if (stbuf.st_size > 100000000) {
+        bplist_log("Large file %s encountered (%llu bytes) -- not read\n",
+                   filename, stbuf.st_size);
+        return NULL;
+    }
+    pldata.len = (size_t) stbuf.st_size;
+    // note: this is supposed to be malloc, not allocate. It is separate
+    // from the graph structure, large, and easy to free right after
+    // parsing.
+    pldata.data = (uint8_t *) malloc(pldata.len);
+    if (!pldata.data) {
+        bplist_log("Could not allocate %lu bytes for %s\n",
+                   (unsigned long) pldata.len, filename);
+        return NULL;
+    }
+    file = fopen(filename, "rb");
+    if (!file) {
+        bplist_log("Could not open %s\n", filename);
+        return NULL;
+    }
+    n = fread(pldata.data, 1, pldata.len, file);
+    if (n != pldata.len) {
+        bplist_log("Error reading from %s\n", filename);
+        return NULL;
+    }
+    value = bplist_read_pldata(&pldata);
+    free(pldata.data);
+    return value;
+}
+
+
+value_ptr bplist_read_pref(char *filename, OSType folder_type)
+{
+    FSRef prefdir;
+    char cstr[MAXPATHLEN];
+
+    OSErr err = FSFindFolder(kOnAppropriateDisk, folder_type,
+                             FALSE, &prefdir);
+    if (err) {
+        bplist_log("Error finding preferences folder: %d\n", err);
+        return NULL;
+    }
+    err = FSRefMakePath(&prefdir, (UInt8 *) cstr, (UInt32) (MAXPATHLEN - 1));
+    if (err) {
+        bplist_log("Error making path name for preferences folder: %d\n", err);
+        return NULL;
+    }
+    strlcat(cstr, "/", MAXPATHLEN);
+    strlcat(cstr, filename, MAXPATHLEN);
+    return bplist_read_file(cstr);
+}
+
+
+value_ptr bplist_read_system_pref(char *filename) {
+    return bplist_read_pref(filename, kSystemPreferencesFolderType);
+}
+
+
+value_ptr bplist_read_user_pref(char *filename) {
+    return bplist_read_pref(filename, kPreferencesFolderType);
+}
+
+
+// data is stored with high-order bytes first.
+// read from plist data in a machine-independent fashion
+//
+uint64_t convert_uint64(uint8_t *ptr)
+{
+    uint64_t rslt = 0;
+    int i;
+    // shift in bytes, high-order first
+    for (i = 0; i < sizeof(uint64_t); i++) {
+        rslt <<= 8;
+        rslt += ptr[i];
+    }
+    return rslt;
+}
+
+
+value_ptr bplist_read_pldata(pldata_ptr data)
+{
+    value_ptr result = NULL;
+    bplist_info_node bplist;
+    uint8_t *ptr;
+    uint64_t top_level_object;
+    int i;
+
+    if (data == NULL)  return NULL;
+    if (!is_binary_plist(data)) {
+        bplist_log("Bad binary plist: too short or invalid header.\n");
+        return NULL;
+    }
+        
+    // read trailer
+    ptr = (uint8_t *) (data->data + data->len - kTRAILER_SIZE);
+    bplist.offset_int_size = ptr[6];
+    bplist.object_ref_size = ptr[7];
+    bplist.object_count = convert_uint64(ptr + 8);
+    top_level_object = convert_uint64(ptr + 16);
+    bplist.offset_table_offset = convert_uint64(ptr + 24);
+        
+    // Basic sanity checks
+    if (bplist.offset_int_size < 1 || bplist.offset_int_size > 8 ||
+        bplist.object_ref_size < 1 || bplist.object_ref_size > 8 ||
+        bplist.offset_table_offset < kHEADER_SIZE) {
+        bplist_log("Bad binary plist: trailer declared insane.\n");
+        return NULL;                
+    }
+        
+    // Ensure offset table is inside file
+    uint64_t offsetTableSize = bplist.offset_int_size * bplist.object_count;
+    if (offsetTableSize + bplist.offset_table_offset + kTRAILER_SIZE > 
+        data->len) {
+        bplist_log("Bad binary plist: offset table overlaps end of container.\n");
+        return NULL;
+    }
+        
+    bplist.data_bytes = data->data;
+    bplist.length = data->len;
+    bplist.cache = NULL; /* dictionary is empty */
+
+    bplist_log_verbose("Got a sane bplist with %llu items, offset_int_size: %u, object_ref_size: %u\n", 
+                      bplist.object_count, bplist.offset_int_size, 
+                      bplist.object_ref_size);
+    /* at this point, we are ready to do some parsing which allocates
+        memory for the result data structure. If memory allocation (using
+        allocate fails, a longjmp will return to here and we simply give up
+     */
+    i = setjmp(abort_parsing);
+    if (i == 0) {
+        result = extract_object(&bplist, top_level_object);
+    } else {
+        bplist_log("allocate() failed to allocate memory. Giving up.\n");
+        result = NULL;
+    }
+    if (!result) {
+        bplist_free_data();
+    }
+    return result;
+}
+
+
+static value_ptr extract_object(bplist_info_ptr bplist, uint64_t objectRef)
+{
+    uint64_t offset;
+    value_ptr result = NULL;
+    uint8_t objectTag;
+    
+    if (objectRef >= bplist->object_count) {
+        // Out-of-range object reference.
+        bplist_log("Bad binary plist: object index is out of range.\n");
+        return NULL;
+    }
+        
+    // Use cached object if it exists
+    result = cache_lookup(bplist->cache, objectRef);
+    if (result != NULL)  return result;
+        
+    // Otherwise, find object in file.
+    offset = read_offset(bplist, objectRef);
+    if (offset > bplist->length) {
+        // Out-of-range offset.
+        bplist_log("Bad binary plist: object outside container.\n");
+        return NULL;
+    }
+    objectTag = *(bplist->data_bytes + offset);
+    switch (objectTag & 0xF0) {
+    case kTAG_SIMPLE:
+        result = extract_simple(bplist, offset);
+        break;
+                
+    case kTAG_INT:
+        result = extract_int(bplist, offset);
+        break;
+                        
+    case kTAG_REAL:
+        result = extract_real(bplist, offset);
+        break;
+                        
+    case kTAG_DATE:
+        result = extract_date(bplist, offset);
+        break;
+                        
+    case kTAG_DATA:
+        result = extract_data(bplist, offset);
+        break;
+                        
+    case kTAG_ASCIISTRING:
+        result = extract_ascii_string(bplist, offset);
+        break;
+                        
+    case kTAG_UNICODESTRING:
+        result = extract_unicode_string(bplist, offset);
+        break;
+        
+    case kTAG_UID:
+        result = extract_uid(bplist, offset);
+        break;
+        
+    case kTAG_ARRAY:
+        result = extract_array(bplist, offset);
+        break;
+        
+    case kTAG_DICTIONARY:
+        result = extract_dictionary(bplist, offset);
+        break;
+        
+    default:
+        // Unknown tag.
+        bplist_log("Bad binary plist: unknown tag 0x%X.\n", 
+                   (objectTag & 0x0F) >> 4);
+        result = NULL;
+    }
+    
+    // Cache and return result.
+    if (result != NULL)  
+        cache_insert(&bplist->cache, objectRef, result);
+    return result;
+}
+
+
+static uint64_t read_sized_int(bplist_info_ptr bplist, uint64_t offset, 
+                               uint8_t size)
+{
+    assert(bplist->data_bytes != NULL && size >= 1 && size <= 8 && 
+           offset + size <= bplist->length);
+        
+    uint64_t result = 0;
+    const uint8_t *byte = bplist->data_bytes + offset;
+        
+    do {
+        // note that ints seem to be high-order first
+        result = (result << 8) | *byte++;
+    } while (--size);
+        
+    return result;
+}
+
+
+static uint64_t read_offset(bplist_info_ptr bplist, uint64_t index)
+{
+    assert(index < bplist->object_count);
+        
+    return read_sized_int(bplist, 
+            bplist->offset_table_offset + bplist->offset_int_size * index, 
+            bplist->offset_int_size);
+}
+
+
+static BOOL read_self_sized_int(bplist_info_ptr bplist, uint64_t offset, 
+                             uint64_t *outValue, size_t *outSize)
+{
+    uint32_t size;
+    int64_t value;
+        
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+        
+    size = 1 << (bplist->data_bytes[offset] & 0x0F);
+    if (size > 8) {
+        // Maximum allowable size in this implementation is 1<<3 = 8 bytes.
+        // This also happens to be the biggest we can handle.
+        return NO;
+    }
+        
+    if (offset + 1 + size > bplist->length) {
+        // Out of range.
+        return NO;
+    }
+        
+    value = read_sized_int(bplist, offset + 1, size);
+    
+    if (outValue != NULL) *outValue = value;
+    if (outSize != NULL) *outSize = size + 1; // +1 for tag byte.
+    return YES;
+}
+
+
+static value_ptr extract_simple(bplist_info_ptr bplist, uint64_t offset)
+{
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+    value_ptr value = value_create();
+        
+    switch (bplist->data_bytes[offset]) {
+    case kVALUE_NULL:
+        value->tag = kVALUE_NULL;
+        return value;
+        
+    case kVALUE_TRUE:
+        value->tag = kVALUE_TRUE;
+        return value;
+                        
+    case kVALUE_FALSE:
+        value->tag = kVALUE_FALSE;
+        return value;
+    }
+        
+    // Note: kVALUE_FILLER is treated as invalid, because it, er, is.
+    bplist_log("Bad binary plist: invalid atom.\n");
+    free(value);
+    return NULL;
+}
+
+
+static value_ptr extract_int(bplist_info_ptr bplist, uint64_t offset)
+{
+    value_ptr value = value_create();
+    value->tag = kTAG_INT;
+
+    if (!read_self_sized_int(bplist, offset, &value->uinteger, NULL)) {
+        bplist_log("Bad binary plist: invalid integer object.\n");
+    }
+        
+    /* NOTE: originally, I sign-extended here. This was the wrong thing; it
+       turns out that negative ints are always stored as 64-bit, and smaller
+       ints are unsigned.
+    */
+    return value;
+}
+
+
+static value_ptr extract_real(bplist_info_ptr bplist, uint64_t offset)
+{
+    value_ptr value = value_create();
+    uint32_t size;
+        
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+    
+    size = 1 << (bplist->data_bytes[offset] & 0x0F);
+        
+    // FIXME: what to do if faced with other sizes for float/double?
+    assert (sizeof (float) == sizeof (uint32_t) && 
+            sizeof (double) == sizeof (uint64_t));
+        
+    if (offset + 1 + size > bplist->length) {
+        bplist_log("Bad binary plist: %s object overlaps end of container.\n", 
+                  "floating-point number");
+        free(value);
+        return NULL;
+    }
+        
+    if (size == sizeof (float)) {
+        // cast is ok because we know size is 4 bytes
+        uint32_t i = (uint32_t) read_sized_int(bplist, offset + 1, size); 
+        // Note that this handles byte swapping.
+        value_set_real(value, *(float *)&i);
+        return value;
+    } else if (size == sizeof (double)) {
+        uint64_t i = read_sized_int(bplist, offset + 1, size);
+        // Note that this handles byte swapping.
+        value_set_real(value, *(double *)&i);
+        return value;
+    } else {
+        // Can't handle floats of other sizes.
+        bplist_log("Bad binary plist: can't handle %u-byte float.\n", size);
+        free(value);
+        return NULL;
+    }
+}
+
+
+static value_ptr extract_date(bplist_info_ptr bplist, uint64_t offset)
+{
+    value_ptr value;
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+        
+    // Data has size code like int and real, but only 3 (meaning 8 bytes) is valid.
+    if (bplist->data_bytes[offset] != kVALUE_FULLDATETAG) {
+        bplist_log("Bad binary plist: invalid size for date object.\n");
+        return NULL;
+    }
+        
+    if (offset + 1 + sizeof (double) > bplist->length) {
+        bplist_log("Bad binary plist: %s object overlaps end of container.\n", 
+                  "date");
+        return NULL;
+    }
+        
+    // FIXME: what to do if faced with other sizes for double?
+    assert (sizeof (double) == sizeof (uint64_t));
+        
+    uint64_t date = read_sized_int(bplist, offset + 1, sizeof(double));
+    // Note that this handles byte swapping.
+    value = value_create();
+    value_set_date(value, *(double *)&date);
+    return value;
+}
+
+
+uint64_t bplist_get_a_size(bplist_info_ptr bplist, 
+                           uint64_t *offset_ptr, char *msg)
+{
+    uint64_t size = bplist->data_bytes[*offset_ptr] & 0x0F;
+    (*offset_ptr)++;
+    if (size == 0x0F) {
+        // 0x0F means separate int size follows. 
+        // Smaller values are used for short data.
+        size_t extra; // the length of the data size we are about to read
+        if ((bplist->data_bytes[*offset_ptr] & 0xF0) != kTAG_INT) {
+            // Bad data, mistagged size int
+            bplist_log("Bad binary plist: %s object size is not tagged as int.\n",
+                       msg);
+            return UINT64_MAX; // error
+        }
+                
+        // read integer data as size, extra tells how many bytes to skip
+        if (!read_self_sized_int(bplist, *offset_ptr, &size, &extra)) {
+            bplist_log("Bad binary plist: invalid %s object size tag.\n", 
+                      "data");
+            return UINT64_MAX; // error
+        }
+        (*offset_ptr) += extra;
+    }
+
+    if (*offset_ptr + size > bplist->length) {
+        bplist_log("Bad binary plist: %s object overlaps end of container.\n", 
+                  "data");
+        return UINT64_MAX; // error
+    }
+    return size;
+}
+
+
+static value_ptr extract_data(bplist_info_ptr bplist, uint64_t offset)
+{
+    uint64_t size;
+    value_ptr value;
+        
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+        
+    if ((size = bplist_get_a_size(bplist, &offset, "data")) == UINT64_MAX) 
+        return NULL;
+        
+    value = value_create();
+    // cast is ok because we only allow files up to 100MB:
+    value_set_data(value, bplist->data_bytes + (size_t) offset, (size_t) size);
+    return value;
+}
+
+
+static value_ptr extract_ascii_string(bplist_info_ptr bplist, uint64_t offset)
+{
+    uint64_t size;
+    value_ptr value; // return value
+        
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+        
+    if ((size = bplist_get_a_size(bplist, &offset, "ascii string")) ==
+        UINT64_MAX) 
+        return NULL;
+
+    value = value_create();
+    // cast is ok because we only allow 100MB files
+    value_set_ascii_string(value, bplist->data_bytes + (size_t) offset, 
+                           (size_t) size);
+    return value;
+}
+
+
+static value_ptr extract_unicode_string(bplist_info_ptr bplist, uint64_t offset)
+{
+    uint64_t size;
+    value_ptr value;
+        
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+        
+    if ((size = bplist_get_a_size(bplist, &offset, "unicode string")) == 
+        UINT64_MAX)
+        return NULL;
+        
+    value = value_create();
+    // cast is ok because we only allow 100MB files
+    value_set_unicode_string(value, bplist->data_bytes + (size_t) offset, 
+                             (size_t) size);
+    return value;
+}
+
+
+static value_ptr extract_uid(bplist_info_ptr bplist, uint64_t offset)
+{
+    /* UIDs are used by Cocoa's key-value coder.
+       When writing other plist formats, they are expanded to dictionaries of
+       the form <dict><key>CF$UID</key><integer>value</integer></dict>, so we
+       do the same here on reading. This results in plists identical to what
+       running plutil -convert xml1 gives us. However, this is not the same
+       result as [Core]Foundation's plist parser, which extracts them as un-
+       introspectable CF objects. In fact, it even seems to convert the CF$UID
+       dictionaries from XML plists on the fly.
+    */
+        
+    value_ptr value;
+    uint64_t uid;
+        
+    if (!read_self_sized_int(bplist, offset, &uid, NULL)) {
+        bplist_log("Bad binary plist: invalid UID object.\n");
+        return NULL;
+    }
+        
+    // assert(NO); // original code suggests using a string for a key
+    // but our dictionaries all use big ints for keys, so I don't know
+    // what to do here
+    
+    // In practice, I believe this code is never executed by PortMidi.
+    // I changed it to do something and not raise compiler warnings, but
+    // not sure what the code should do.
+
+    value = value_create();
+    value_set_uid(value, uid);
+    // return [NSDictionary dictionaryWithObject:
+    //         [NSNumber numberWithUnsignedLongLong:value] 
+    //         forKey:"CF$UID"];
+    return value;
+}
+
+
+static value_ptr extract_array(bplist_info_ptr bplist, uint64_t offset)
+{
+    uint64_t i, count;
+    uint64_t size;
+    uint64_t elementID;
+    value_ptr element = NULL;
+    value_ptr *array = NULL;
+    value_ptr value = NULL;
+    BOOL ok = YES;
+        
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+        
+    if ((count = bplist_get_a_size(bplist, &offset, "array")) == UINT64_MAX)
+        return NULL;
+        
+    if (count > UINT64_MAX / bplist->object_ref_size - offset) {
+        // Offset overflow.
+        bplist_log("Bad binary plist: %s object overlaps end of container.\n", 
+                   "array");
+        return NULL;
+    }
+        
+    size = bplist->object_ref_size * count;
+    if (size + offset > bplist->length) {
+        bplist_log("Bad binary plist: %s object overlaps end of container.\n", 
+                   "array");
+        return NULL;
+    }
+        
+    // got count, the number of array elements
+
+    value = value_create();
+    assert(value);
+
+    if (count == 0) {
+        // count must be size_t or smaller because max file size is 100MB
+        value_set_array(value, array, (size_t) count);
+        return value;
+    }
+        
+    array = allocate(sizeof(value_ptr) * (size_t) count);
+        
+    for (i = 0; i != count; ++i) {
+        bplist_log_verbose("[%u]\n", i);
+        elementID = read_sized_int(bplist, offset + i * bplist->object_ref_size, 
+                                 bplist->object_ref_size);
+        element = extract_object(bplist, elementID);
+        if (element != NULL) {
+            array[i] = element;
+        } else {
+            ok = NO;
+            break;
+        }
+    }
+    if (ok) { // count is smaller than size_t max because of 100MB file limit
+        value_set_array(value, array, (size_t) count);
+    }
+
+    return value;
+}
+
+
+static value_ptr extract_dictionary(bplist_info_ptr bplist, uint64_t offset)
+{
+    uint64_t i, count;
+    uint64_t size;
+    uint64_t elementID;
+    value_ptr value = NULL;
+    dict_ptr dict = NULL;
+    BOOL ok = YES;
+        
+    assert(bplist->data_bytes != NULL && offset < bplist->length);
+        
+        
+    if ((count = bplist_get_a_size(bplist, &offset, "array")) == UINT64_MAX)
+        return NULL;
+
+    if (count > UINT64_MAX / (bplist->object_ref_size * 2) - offset) {
+        // Offset overflow.
+        bplist_log("Bad binary plist: %s object overlaps end of container.\n", 
+                   "dictionary");
+        return NULL;
+    }
+    
+    size = bplist->object_ref_size * count * 2;
+    if (size + offset > bplist->length) {
+        bplist_log("Bad binary plist: %s object overlaps end of container.\n", 
+                   "dictionary");
+        return NULL;
+    }
+    
+    value = value_create();
+    if (count == 0) {
+        value_set_dict(value, NULL);
+        return value;
+    }
+
+    for (i = 0; i != count; ++i) {
+        value_ptr key;
+        value_ptr val;
+        elementID = read_sized_int(bplist, offset + i * bplist->object_ref_size, 
+                                 bplist->object_ref_size);
+        key = extract_object(bplist, elementID);
+        if (key != NULL) {
+            bplist_log_verbose("key: %p\n", key);
+        } else {
+            ok = NO;
+            break;
+        }
+                    
+        elementID = read_sized_int(bplist, 
+                            offset + (i + count) * bplist->object_ref_size, 
+                            bplist->object_ref_size);
+        val = extract_object(bplist, elementID);
+        if (val != NULL) {
+            dict_insert(&dict, key, val);
+        } else {
+            ok = NO;
+            break;
+        }
+    }
+    if (ok) {
+        value_set_dict(value, dict);
+    }
+    
+    return value;
+}
+
+/*************** functions for accessing values ****************/
+
+
+char *value_get_asciistring(value_ptr v)
+{
+    if (v->tag != kTAG_ASCIISTRING) return NULL;
+    return v->string;
+}
+
+
+value_ptr value_dict_lookup_using_string(value_ptr v, char *key)
+{
+    dict_ptr dict;
+    if (v->tag != kTAG_DICTIONARY) return NULL; // not a dictionary
+    dict = v->dict;
+    /* search for key */
+    while (dict) {
+        if (dict->key && dict->key->tag == kTAG_ASCIISTRING &&
+            strcmp(key, dict->key->string) == 0) { // found it
+            return dict->value;
+        }
+        dict = dict->next;
+    }
+    return NULL; /* not found */
+}
+
+value_ptr value_dict_lookup_using_path(value_ptr v, char *path)
+{
+    char key[MAX_KEY_SIZE];
+    while (*path) { /* more to the path */
+        int i = 0;
+        while (i < MAX_KEY_SIZE - 1) {
+            key[i] = *path++;
+            if (key[i] == '/') { /* end of entry in path */
+                key[i + 1] = 0;
+                break;
+            }
+            if (!key[i]) {
+                path--; /* back up to end of string char */
+                break;  /* this will cause outer loop to exit */
+            }
+            i++;
+        }
+        if (!v || v->tag != kTAG_DICTIONARY) return NULL;
+        /* now, look up the key to get next value */
+        v = value_dict_lookup_using_string(v, key);
+        if (v == NULL) return NULL;
+    }
+    return v;
+}
+                
+
+/*************** functions for debugging ***************/
+
+void plist_print(value_ptr v)
+{
+    size_t i;
+    int comma_needed;
+    dict_ptr dict;
+    if (!v) {
+        printf("NULL");
+        return;
+    }
+    switch (v->tag & 0xF0) {
+    case kTAG_SIMPLE:
+        switch (v->tag) {
+        case kVALUE_NULL: 
+            printf("NULL@%p", v); break;
+        case kVALUE_FALSE: 
+            printf("FALSE@%p", v); break;
+        case kVALUE_TRUE:
+            printf("TRUE@%p", v); break;
+        default:
+            printf("UNKNOWN tag=%x@%p", v->tag, v); break;
+        }
+        break;
+    case kTAG_INT:
+        printf("%lld@%p", v->integer, v); break;
+    case kTAG_REAL:
+        printf("%g@%p", v->real, v); break;
+    case kTAG_DATE:
+        printf("date:%g@%p", v->real, v); break;
+    case kTAG_DATA:
+        printf("data@%p->%p:[%p:", v, v->data, v->data->data);
+        for (i = 0; i < v->data->len; i++) {
+            printf(" %2x", v->data->data[i]);
+        }
+        printf("]"); break;
+    case kTAG_ASCIISTRING:
+        printf("%p:\"%s\"@%p", v->string, v->string, v); break;
+    case kTAG_UNICODESTRING:
+        printf("unicode:%p:\"%s\"@%p", v->string, v->string, v); break;
+    case kTAG_UID:
+        printf("UID:%llu@%p", v->uinteger, v); break;
+    case kTAG_ARRAY:
+        comma_needed = FALSE;
+        printf("%p->%p:[%p:", v, v->array, v->array->array);
+        for (i = 0; i < v->array->length; i++) {
+            if (comma_needed) printf(", ");
+            plist_print(v->array->array[i]);
+            comma_needed = TRUE;
+        }
+        printf("]"); break;
+    case kTAG_DICTIONARY:
+        comma_needed = FALSE;
+        printf("%p:[", v);
+        dict = v->dict;
+        while (dict) {
+            if (comma_needed) printf(", ");
+            printf("%p:", dict);
+            plist_print(dict->key);
+            printf("->");
+            plist_print(dict->value);
+            comma_needed = TRUE;
+            dict = dict->next;
+        }
+        printf("]"); break;
+    default:
+        printf("UNKNOWN tag=%x", v->tag);
+        break;
+    }
+}
+
+