2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-05 17:15:31 +00:00
Files
bind/lib/omapi/data.c

195 lines
4.5 KiB
C
Raw Normal View History

2000-01-04 20:04:42 +00:00
/*
* Copyright (C) 1996, 1997, 1998, 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* $Id: data.c,v 1.7 2000/02/01 23:18:51 marka Exp $ */
2000-01-04 20:04:42 +00:00
/* Principal Author: Ted Lemon */
/*
* Functions supporting memory allocation for the object management protocol.
*/
#include <stdlib.h> /* abort() */
2000-01-04 20:04:42 +00:00
#include <string.h> /* memset */
#include <isc/assertions.h>
#include <isc/error.h>
#include <omapi/private.h>
isc_result_t
2000-01-22 00:18:05 +00:00
omapi_data_create(omapi_data_t **t, omapi_datatype_t type, ...) {
2000-01-04 20:04:42 +00:00
va_list l;
2000-01-22 00:18:05 +00:00
omapi_data_t *new;
2000-01-04 20:04:42 +00:00
unsigned int len;
unsigned int val;
int intval;
char *s;
REQUIRE(type == omapi_datatype_int ||
type == omapi_datatype_data ||
2000-01-22 00:18:05 +00:00
type == omapi_datatype_string ||
2000-01-04 20:04:42 +00:00
type == omapi_datatype_object);
va_start(l, type);
/*
* Quiet bogus "might be used uninitialized in this function" warnings.
*/
val = 0;
intval = 0;
s = NULL;
switch (type) {
2000-01-13 06:13:26 +00:00
case omapi_datatype_int:
2000-01-22 00:18:05 +00:00
len = OMAPI_DATA_INT_LEN;
2000-01-04 20:04:42 +00:00
intval = va_arg(l, int);
break;
2000-01-13 06:13:26 +00:00
case omapi_datatype_string:
2000-01-04 20:04:42 +00:00
s = va_arg(l, char *);
val = strlen(s);
2000-01-22 00:18:05 +00:00
len = OMAPI_DATA_NOBUFFER_LEN + val;
2000-01-04 20:04:42 +00:00
break;
2000-01-13 06:13:26 +00:00
case omapi_datatype_data:
2000-01-04 20:04:42 +00:00
val = va_arg(l, unsigned int);
2000-01-22 00:18:05 +00:00
len = OMAPI_DATA_NOBUFFER_LEN + val;
2000-01-04 20:04:42 +00:00
break;
2000-01-13 06:13:26 +00:00
case omapi_datatype_object:
2000-01-22 00:18:05 +00:00
len = OMAPI_DATA_OBJECT_LEN;
2000-01-04 20:04:42 +00:00
break;
2000-01-13 06:13:26 +00:00
default:
2000-01-04 20:04:42 +00:00
UNEXPECTED_ERROR(__FILE__, __LINE__,
2000-01-22 00:18:05 +00:00
"unknown type in omapi_data_create: %d\n",
2000-01-04 20:04:42 +00:00
type);
return (ISC_R_UNEXPECTED);
}
new = isc_mem_get(omapi_mctx, len);
if (new == NULL)
return (ISC_R_NOMEMORY);
memset(new, 0, len);
switch (type) {
case omapi_datatype_int:
new->u.integer = intval;
break;
case omapi_datatype_string:
memcpy(new->u.buffer.value, s, val);
new->u.buffer.len = val;
break;
case omapi_datatype_data:
new->u.buffer.len = val;
break;
case omapi_datatype_object:
2000-01-17 18:02:11 +00:00
OBJECT_REF(&new->u.object, va_arg(l, omapi_object_t *));
2000-01-04 20:04:42 +00:00
break;
}
new->type = type;
2000-01-22 00:18:05 +00:00
omapi_data_reference(t, new);
2000-01-04 20:04:42 +00:00
return (ISC_R_SUCCESS);
}
void
2000-01-22 00:18:05 +00:00
omapi_data_reference(omapi_data_t **r, omapi_data_t *h) {
2000-01-04 20:04:42 +00:00
REQUIRE(r != NULL && h != NULL);
2000-01-13 06:13:26 +00:00
REQUIRE(*r == NULL);
2000-01-04 20:04:42 +00:00
*r = h;
h->refcnt++;
}
void
2000-01-22 00:18:05 +00:00
omapi_data_dereference(omapi_data_t **h) {
2000-01-13 06:13:26 +00:00
int length = 0;
2000-01-04 20:04:42 +00:00
REQUIRE(h != NULL && *h != NULL);
REQUIRE((*h)->refcnt > 0);
2000-01-22 00:18:05 +00:00
if (--((*h)->refcnt) == 0) {
2000-01-04 20:04:42 +00:00
switch ((*h)->type) {
2000-01-13 06:13:26 +00:00
case omapi_datatype_int:
2000-01-22 00:18:05 +00:00
length = OMAPI_DATA_INT_LEN;
2000-01-04 20:04:42 +00:00
break;
2000-01-13 06:13:26 +00:00
case omapi_datatype_string:
2000-01-22 00:18:05 +00:00
length = OMAPI_DATA_NOBUFFER_LEN + (*h)->u.buffer.len;
2000-01-13 06:13:26 +00:00
break;
case omapi_datatype_data:
2000-01-22 00:18:05 +00:00
length = OMAPI_DATA_NOBUFFER_LEN + (*h)->u.buffer.len;
2000-01-13 06:13:26 +00:00
break;
case omapi_datatype_object:
2000-01-17 18:02:11 +00:00
OBJECT_DEREF(&(*h)->u.object);
2000-01-22 00:18:05 +00:00
length = OMAPI_DATA_OBJECT_LEN;
2000-01-04 20:04:42 +00:00
break;
2000-01-13 06:13:26 +00:00
default:
FATAL_ERROR(__FILE__, __LINE__,
"unknown datatype in "
"omapi_data_dereference: %d\n",
(*h)->type);
/* NOTREACHED */
2000-01-22 00:18:05 +00:00
break;
2000-01-04 20:04:42 +00:00
}
2000-01-13 06:13:26 +00:00
isc_mem_put(omapi_mctx, *h, length);
2000-01-04 20:04:42 +00:00
}
2000-01-13 06:13:26 +00:00
2000-01-04 20:04:42 +00:00
*h = NULL;
}
2000-01-22 00:18:05 +00:00
int
omapi_data_strcmp(omapi_data_t *s1, const char *s2) {
unsigned int len, slen;
int order;
2000-01-04 20:04:42 +00:00
2000-01-22 00:18:05 +00:00
REQUIRE(s1->type == omapi_datatype_data ||
s1->type == omapi_datatype_string);
2000-01-04 20:04:42 +00:00
2000-01-22 00:18:05 +00:00
slen = strlen(s2);
if (slen > s1->u.buffer.len)
len = s1->u.buffer.len;
else
len = slen;
2000-01-04 20:04:42 +00:00
2000-01-22 00:18:05 +00:00
order = memcmp(s1->u.buffer.value, s2, len);
if (order == 0) {
2000-01-22 00:18:05 +00:00
if (s1->u.buffer.len > slen)
order = 1;
else if (s1->u.buffer.len < slen)
order = -1;
}
2000-01-04 20:04:42 +00:00
2000-01-22 00:18:05 +00:00
return (order);
2000-01-04 20:04:42 +00:00
}
2000-01-31 14:38:01 +00:00
int
omapi_data_getint(omapi_data_t *t) {
isc_uint32_t stored_value; /* Stored in network byte order. */
REQUIRE(t != NULL);
REQUIRE(t->type == omapi_datatype_int ||
((t->type == omapi_datatype_data ||
(t->type == omapi_datatype_string)) &&
t->u.buffer.len == sizeof(stored_value)));
if (t->type == omapi_datatype_int)
return (t->u.integer);
memcpy(&stored_value, t->u.buffer.value, sizeof(stored_value));
return (ntohl(stored_value));
}