1 /* $NetBSD: array.c,v 1.1.1.2 2014/07/12 11:57:58 spz Exp $ */ 2 /* listener.c 3 4 Subroutines that support the omapi extensible array type. */ 5 6 /* 7 * Copyright (c) 2004-2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC") 8 * Copyright (c) 2001-2003 by Internet Software Consortium 9 * 10 * Permission to use, copy, modify, and distribute this software for any 11 * purpose with or without fee is hereby granted, provided that the above 12 * copyright notice and this permission notice appear in all copies. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21 * 22 * Internet Systems Consortium, Inc. 23 * 950 Charter Street 24 * Redwood City, CA 94063 25 * <info@isc.org> 26 * https://www.isc.org/ 27 * 28 */ 29 30 #include <sys/cdefs.h> 31 __RCSID("$NetBSD: array.c,v 1.1.1.2 2014/07/12 11:57:58 spz Exp $"); 32 33 #include "dhcpd.h" 34 35 #include <omapip/omapip_p.h> 36 37 /* Allocate a new extensible array. */ 38 39 isc_result_t omapi_array_allocate (omapi_array_t **array, 40 omapi_array_ref_t ref, 41 omapi_array_deref_t deref, 42 const char *file, int line) 43 { 44 omapi_array_t *aptr; 45 46 if (!array || *array) 47 return DHCP_R_INVALIDARG; 48 aptr = dmalloc (sizeof (omapi_array_t),file, line); 49 if (!aptr) 50 return ISC_R_NOMEMORY; 51 *array = aptr; 52 aptr -> ref = ref; 53 aptr -> deref = deref; 54 return ISC_R_SUCCESS; 55 } 56 57 isc_result_t omapi_array_free (omapi_array_t **array, 58 const char *file, int line) 59 { 60 omapi_array_t *aptr; 61 int i; 62 63 if (!array || !*array) 64 return DHCP_R_INVALIDARG; 65 aptr = *array; 66 for (i = 0; i < aptr -> count; i++) 67 if (aptr -> data [i] && aptr -> deref) 68 (*aptr -> deref) (&aptr -> data [i], file, line); 69 dfree (aptr -> data, MDL); 70 dfree (aptr, MDL); 71 *array = (omapi_array_t *)0; 72 return ISC_R_SUCCESS; 73 } 74 75 /* Extend the size of the array by one entry (we may allocate more than that) 76 and store the specified value in the new array element. */ 77 78 isc_result_t omapi_array_extend (omapi_array_t *array, char *ptr, 79 int *index, const char *file, int line) 80 { 81 isc_result_t status; 82 int new = array -> count; 83 status = omapi_array_set (array, ptr, new, file, line); 84 if (index && status == ISC_R_SUCCESS) 85 *index = new; 86 return status; 87 } 88 89 /* Set a value in the specified array, extending it if necessary. */ 90 91 isc_result_t omapi_array_set (omapi_array_t *array, void *ptr, int index, 92 const char *file, int line) 93 { 94 char **newbuf; 95 int delta; 96 isc_result_t status; 97 98 if (!array) 99 return DHCP_R_INVALIDARG; 100 if (!ptr) 101 return DHCP_R_INVALIDARG; 102 if (index < 0) 103 return DHCP_R_INVALIDARG; 104 105 /* If the proposed index is larger than the current available 106 space in the array, make more space in the array. */ 107 if (array -> max <= index) { 108 delta = index - array -> max + 10; 109 newbuf = dmalloc ((array -> max + delta) * sizeof (char *), 110 file, line); 111 if (!newbuf) 112 return ISC_R_NOMEMORY; 113 /* Zero the new elements. */ 114 memset (&newbuf [array -> max], 0, (sizeof (char *)) * delta); 115 array -> max += delta; 116 /* Copy the old array data into the new buffer. */ 117 if (array -> data) { 118 memcpy (newbuf, 119 array -> data, array -> count * sizeof (char *)); 120 dfree (array -> data, file, line); 121 } 122 array -> data = newbuf; 123 } else { 124 /* If there's already data there, and this is an array 125 of references, dereference what's there. */ 126 if (array -> data [index]) { 127 status = ((*array -> deref) (&array -> data [index], 128 file, line)); 129 130 if (status != ISC_R_SUCCESS) 131 return status; 132 } 133 } 134 135 /* Store the pointer using the referencer function. We have 136 either just memset this to zero or dereferenced what was 137 there previously, so there is no need to do anything if the 138 pointer we have been asked to store is null. */ 139 if (ptr) { 140 status = (*array -> ref) (&array -> data [index], ptr, 141 file, line); 142 if (status != ISC_R_SUCCESS) 143 return status; 144 } 145 if (index >= array -> count) 146 array -> count = index + 1; 147 return ISC_R_SUCCESS; 148 } 149 150 isc_result_t omapi_array_lookup (char **ptr, omapi_array_t *array, int index, 151 const char *file, int line) 152 { 153 if (!array || !ptr || *ptr || index < 0 || index >= array -> count) 154 return DHCP_R_INVALIDARG; 155 if (array -> data [index]) 156 return (*array -> ref) (ptr, 157 array -> data [index], file, line); 158 return ISC_R_NOTFOUND; 159 } 160 161