value.c revision 15a44745412679c30a6d022733925af70a38b715
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley/*
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * Copyright (C) 1996-2000 Internet Software Consortium.
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley *
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * Permission to use, copy, modify, and distribute this software for any
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * purpose with or without fee is hereby granted, provided that the above
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * copyright notice and this permission notice appear in all copies.
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley *
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley */
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley/* $Id: value.c,v 1.8 2000/07/27 09:54:26 tale Exp $ */
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley/* Principal Author: Ted Lemon */
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley#include <config.h>
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley#include <isc/mem.h>
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley#include <isc/string.h>
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley#include <isc/util.h>
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley#include <omapi/private.h>
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyisc_result_t
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_create(omapi_value_t **d) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_value_t *new;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley new = isc_mem_get(omapi_mctx, sizeof(*new));
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (new == NULL)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (ISC_R_NOMEMORY);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley memset(new, 0, sizeof *new);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_value_reference(d, new);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (ISC_R_SUCCESS);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyvoid
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_reference(omapi_value_t **r, omapi_value_t *h) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE(r != NULL && h != NULL);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE(*r == NULL);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley *r = h;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley h->refcnt++;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyvoid
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_dereference(omapi_value_t **h) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE(h != NULL && *h != NULL);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE((*h)->refcnt > 0);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (--((*h)->refcnt) <= 0) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if ((*h)->name != NULL)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_string_dereference(&(*h)->name);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if ((*h)->value != NULL)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_data_dereference(&(*h)->value);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley isc_mem_put(omapi_mctx, *h, sizeof(omapi_value_t));
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley }
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley *h = NULL;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyisc_result_t
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_storedata(omapi_value_t **vp, omapi_string_t *name,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_data_t *value)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley{
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley isc_result_t result;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_value_create(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_string_reference(&(*vp)->name, name);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (value != NULL)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_data_reference(&(*vp)->value, value);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyisc_result_t
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_storemem(omapi_value_t **vp, omapi_string_t *name,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley const unsigned char *value, unsigned int len)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley{
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley isc_result_t result;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_value_create(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_string_reference(&(*vp)->name, name);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (value != NULL) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_data_create(&(*vp)->value,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_datatype_data, len);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result == ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley memcpy((*vp)->value->u.buffer.value, value, len);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley }
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_value_dereference(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyisc_result_t
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_storeint(omapi_value_t **vp, omapi_string_t *name, int value)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley{
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley isc_result_t result;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_value_create(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_string_reference(&(*vp)->name, name);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (value != 0) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_data_create(&(*vp)->value, omapi_datatype_int);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result == ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley (*vp)->value->u.integer = value;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley }
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_value_dereference(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyisc_result_t
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_storeobject(omapi_value_t **vp, omapi_string_t *name,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_object_t *value)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley{
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley isc_result_t result;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_value_create(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_string_reference(&(*vp)->name, name);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (value != NULL) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_data_create(&(*vp)->value, omapi_datatype_int);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result == ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = object_gethandle((omapi_handle_t *)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley &(*vp)->value->u.integer,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley value);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley }
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_value_dereference(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyisc_result_t
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_storestr(omapi_value_t **vp, omapi_string_t *name, char *value)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley{
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley isc_result_t result;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_value_create(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_string_reference(&(*vp)->name, name);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (value != NULL)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley result = omapi_data_create(&(*vp)->value,
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_datatype_string, value);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley if (result != ISC_R_SUCCESS)
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley omapi_value_dereference(vp);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (result);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyint
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_getint(omapi_value_t *value) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE(value != NULL && value->value != NULL);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE(value->value->type == omapi_datatype_int ||
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley ((value->value->type == omapi_datatype_data ||
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley (value->value->type == omapi_datatype_string)) &&
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley value->value->u.buffer.len == sizeof(isc_uint32_t)));
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley return (omapi_data_getint(value->value));
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley/*
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * WARNING: The region is valid only as long as the value pointer
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * is valid. See omapi.h.
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley */
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyvoid
c7ddab7655021d96211a26f99d9f694396c53284Bob Halleyomapi_value_getregion(omapi_value_t *value, isc_region_t *region) {
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE(value != NULL && value->value != NULL);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley REQUIRE(value->value->type == omapi_datatype_data ||
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley value->value->type == omapi_datatype_string);
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley /*
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * Boy, the word "value" appears a lot. Almost like a Smurf song.
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley * La la la la la la, la la la la la.
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley */
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley region->base = value->value->u.buffer.value;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley region->length = value->value->u.buffer.len;
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley}
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley
c7ddab7655021d96211a26f99d9f694396c53284Bob Halley