static int dict_array_unpack(DBusMessageIter *iter, uint32_t *num_dicts_out, struct dict **dicts_out) { int ret; DBusMessageIter array_iter; uint32_t num_dicts; struct dict *dicts = NULL; if (cdbus_type_check(iter, DBUS_TYPE_ARRAY) < 0) { return -EINVAL; } dbus_message_iter_recurse(iter, &array_iter); num_dicts = 0; while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) { num_dicts++; dicts = realloc(dicts, sizeof(struct dict) * num_dicts); if (dicts == NULL) abort(); ret = dict_unpack(&array_iter, &dicts[num_dicts - 1]); if (ret < 0) { free(dicts); return ret; } dbus_message_iter_next(&array_iter); } *num_dicts_out = num_dicts; *dicts_out = dicts; return 0; }
/* * Enter a key-value pair in a dictionary. * See idict.h for the possible return values. */ int dict_put(ref * pdref /* t_dictionary */ , const ref * pkey, const ref * pvalue, dict_stack_t *pds) { dict *pdict = pdref->value.pdict; gs_ref_memory_t *mem = dict_memory(pdict); gs_memory_t *pmem = dict_mem(pdict); int rcode = 0; int code; ref *pvslot, kname; /* Check the value. */ store_check_dest(pdref, pvalue); top:if ((code = dict_find(pdref, pkey, &pvslot)) <= 0) { /* not found *//* Check for overflow */ uint index; switch (code) { case 0: break; case gs_error_dictfull: if (!pmem->gs_lib_ctx->dict_auto_expand) return_error(gs_error_dictfull); code = dict_grow(pdref, pds); if (code < 0) return code; goto top; /* keep things simple */ default: /* gs_error_typecheck */ return code; } index = pvslot - pdict->values.value.refs; /* If the key is a string, convert it to a name. */ if (r_has_type(pkey, t_string)) { int code; if (!r_has_attr(pkey, a_read)) return_error(gs_error_invalidaccess); code = name_from_string(pmem, pkey, &kname); if (code < 0) return code; pkey = &kname; } if (dict_is_packed(pdict)) { ref_packed *kp; if (!r_has_type(pkey, t_name) || name_index(pmem, pkey) > packed_name_max_index ) { /* Change to unpacked representation. */ int code = dict_unpack(pdref, pds); if (code < 0) return code; goto top; } kp = pdict->keys.value.writable_packed + index; if (ref_must_save_in(mem, &pdict->keys)) { /* See initial comment for why it is safe */ /* not to save the change if the keys */ /* array itself is new. */ ref_do_save_in(mem, &pdict->keys, kp, "dict_put(key)"); } *kp = pt_tag(pt_literal_name) + name_index(pmem, pkey); } else { ref *kp = pdict->keys.value.refs + index; if_debug2m('d', (const gs_memory_t *)mem, "[d]0x%lx: fill key at 0x%lx\n", (ulong) pdict, (ulong) kp); store_check_dest(pdref, pkey); ref_assign_old_in(mem, &pdict->keys, kp, pkey, "dict_put(key)"); /* set key of pair */ } ref_save_in(mem, pdref, &pdict->count, "dict_put(count)"); pdict->count.value.intval++; /* If the key is a name, update its 1-element cache. */ if (r_has_type(pkey, t_name)) { name *pname = pkey->value.pname; if (pname->pvalue == pv_no_defn && CAN_SET_PVALUE_CACHE(pds, pdref, mem) ) { /* Set the cache. */ if_debug0m('d', (const gs_memory_t *)mem, "[d]set cache\n"); pname->pvalue = pvslot; } else { /* The cache can't be used. */ if_debug0m('d', (const gs_memory_t *)mem, "[d]no cache\n"); pname->pvalue = pv_other; } } rcode = 1; } if_debug8m('d', (const gs_memory_t *)mem, "[d]0x%lx: put key 0x%lx 0x%lx\n value at 0x%lx: old 0x%lx 0x%lx, new 0x%lx 0x%lx\n", (ulong) pdref->value.pdict, ((const ulong *)pkey)[0], ((const ulong *)pkey)[1], (ulong) pvslot, ((const ulong *)pvslot)[0], ((const ulong *)pvslot)[1], ((const ulong *)pvalue)[0], ((const ulong *)pvalue)[1]); ref_assign_old_in(mem, &pdref->value.pdict->values, pvslot, pvalue, "dict_put(value)"); return rcode; }