value.c revision 499b34cea04a46823d003d4c0520c8b03e8513cb
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt/*
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * Copyright (C) 1996-2001 Internet Software Consortium.
45c5f403619029a363cf089e0a4b1bb44425dd84Tinderbox User *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
ecfedec0e0b7ae3dc3cbbf9b59f63732146c1387Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews/* $Id: value.c,v 1.11 2001/01/09 22:00:15 bwelling Exp $ */
677f507de7c546c187c1505c48bc7b440545485cMark Andrews
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews/* Principal Author: Ted Lemon */
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews#include <config.h>
677f507de7c546c187c1505c48bc7b440545485cMark Andrews
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt#include <isc/mem.h>
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt#include <isc/string.h>
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt#include <isc/util.h>
677f507de7c546c187c1505c48bc7b440545485cMark Andrews
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews#include <omapi/private.h>
094672b313d840bd55c30ac3b46371e173475bc0Evan Hunt
0d6328ce5f6b799f8e7c6cbbb3b965cf29bfb7baMark Andrewsisc_result_t
0d6328ce5f6b799f8e7c6cbbb3b965cf29bfb7baMark Andrewsomapi_value_create(omapi_value_t **d) {
677f507de7c546c187c1505c48bc7b440545485cMark Andrews omapi_value_t *new;
e01ef6f01c7e8f80122cd80a2e011425a0135489Mark Andrews
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews new = isc_mem_get(omapi_mctx, sizeof(*new));
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews if (new == NULL)
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (ISC_R_NOMEMORY);
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews memset(new, 0, sizeof *new);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt omapi_value_reference(d, new);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (ISC_R_SUCCESS);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntvoid
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrewsomapi_value_reference(omapi_value_t **r, omapi_value_t *h) {
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews REQUIRE(r != NULL && h != NULL);
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews REQUIRE(*r == NULL);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews *r = h;
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews h->refcnt++;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntvoid
677f507de7c546c187c1505c48bc7b440545485cMark Andrewsomapi_value_dereference(omapi_value_t **h) {
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt REQUIRE(h != NULL && *h != NULL);
e939674d53a127ddeeaf4b41fd72933f0b493308Mark Andrews REQUIRE((*h)->refcnt > 0);
9478de25bb86e8942e35cf50462cf108154958e9Mark Andrews
677f507de7c546c187c1505c48bc7b440545485cMark Andrews if (--((*h)->refcnt) <= 0) {
677f507de7c546c187c1505c48bc7b440545485cMark Andrews if ((*h)->name != NULL)
omapi_string_dereference(&(*h)->name);
if ((*h)->value != NULL)
omapi_data_dereference(&(*h)->value);
isc_mem_put(omapi_mctx, *h, sizeof(omapi_value_t));
}
*h = NULL;
}
isc_result_t
omapi_value_storedata(omapi_value_t **vp, omapi_string_t *name,
omapi_data_t *value)
{
isc_result_t result;
result = omapi_value_create(vp);
if (result != ISC_R_SUCCESS)
return (result);
omapi_string_reference(&(*vp)->name, name);
if (value != NULL)
omapi_data_reference(&(*vp)->value, value);
return (result);
}
isc_result_t
omapi_value_storemem(omapi_value_t **vp, omapi_string_t *name,
const unsigned char *value, unsigned int len)
{
isc_result_t result;
result = omapi_value_create(vp);
if (result != ISC_R_SUCCESS)
return (result);
omapi_string_reference(&(*vp)->name, name);
if (value != NULL) {
result = omapi_data_create(&(*vp)->value,
omapi_datatype_data, len);
if (result == ISC_R_SUCCESS)
memcpy((*vp)->value->u.buffer.value, value, len);
}
if (result != ISC_R_SUCCESS)
omapi_value_dereference(vp);
return (result);
}
isc_result_t
omapi_value_storeint(omapi_value_t **vp, omapi_string_t *name, int value) {
isc_result_t result;
result = omapi_value_create(vp);
if (result != ISC_R_SUCCESS)
return (result);
omapi_string_reference(&(*vp)->name, name);
result = omapi_data_create(&(*vp)->value, omapi_datatype_int);
if (result == ISC_R_SUCCESS)
(*vp)->value->u.integer = value;
if (result != ISC_R_SUCCESS)
omapi_value_dereference(vp);
return (result);
}
isc_result_t
omapi_value_storeobject(omapi_value_t **vp, omapi_string_t *name,
omapi_object_t *value)
{
isc_result_t result;
result = omapi_value_create(vp);
if (result != ISC_R_SUCCESS)
return (result);
omapi_string_reference(&(*vp)->name, name);
if (value != NULL) {
result = omapi_data_create(&(*vp)->value, omapi_datatype_int);
if (result == ISC_R_SUCCESS)
result = object_gethandle((omapi_handle_t *)
&(*vp)->value->u.integer,
value);
}
if (result != ISC_R_SUCCESS)
omapi_value_dereference(vp);
return (result);
}
isc_result_t
omapi_value_storestr(omapi_value_t **vp, omapi_string_t *name, char *value)
{
isc_result_t result;
result = omapi_value_create(vp);
if (result != ISC_R_SUCCESS)
return (result);
omapi_string_reference(&(*vp)->name, name);
if (value != NULL)
result = omapi_data_create(&(*vp)->value,
omapi_datatype_string, value);
if (result != ISC_R_SUCCESS)
omapi_value_dereference(vp);
return (result);
}
int
omapi_value_getint(omapi_value_t *value) {
REQUIRE(value != NULL && value->value != NULL);
REQUIRE(value->value->type == omapi_datatype_int ||
((value->value->type == omapi_datatype_data ||
(value->value->type == omapi_datatype_string)) &&
value->value->u.buffer.len == sizeof(isc_uint32_t)));
return (omapi_data_getint(value->value));
}
/*
* WARNING: The region is valid only as long as the value pointer
* is valid. See omapi.h.
*/
void
omapi_value_getregion(omapi_value_t *value, isc_region_t *region) {
REQUIRE(value != NULL && value->value != NULL);
REQUIRE(value->value->type == omapi_datatype_data ||
value->value->type == omapi_datatype_string);
/*
* Boy, the word "value" appears a lot. Almost like a Smurf song.
* La la la la la la, la la la la la.
*/
region->base = value->value->u.buffer.value;
region->length = value->value->u.buffer.len;
}