1 #pragma ident "%Z%%M% %I% %E% SMI" 2 /* 3 * lib/krb5/krb/copy_addrs.c 4 * 5 * Copyright 1990 by the Massachusetts Institute of Technology. 6 * All Rights Reserved. 7 * 8 * Export of this software from the United States of America may 9 * require a specific license from the United States Government. 10 * It is the responsibility of any person or organization contemplating 11 * export to obtain such a license before exporting. 12 * 13 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 14 * distribute this software and its documentation for any purpose and 15 * without fee is hereby granted, provided that the above copyright 16 * notice appear in all copies and that both that copyright notice and 17 * this permission notice appear in supporting documentation, and that 18 * the name of M.I.T. not be used in advertising or publicity pertaining 19 * to distribution of the software without specific, written prior 20 * permission. Furthermore if you modify this software you must label 21 * your software as modified software and not distribute it in such a 22 * fashion that it might be confused with the original M.I.T. software. 23 * M.I.T. makes no representations about the suitability of 24 * this software for any purpose. It is provided "as is" without express 25 * or implied warranty. 26 * 27 * 28 * krb5_copy_addresses() 29 */ 30 31 #include <k5-int.h> 32 33 /*ARGSUSED*/ 34 krb5_error_code KRB5_CALLCONV 35 krb5_copy_addr(krb5_context context, const krb5_address *inad, krb5_address **outad) 36 { 37 krb5_address *tmpad; 38 39 if (!(tmpad = (krb5_address *)malloc(sizeof(*tmpad)))) 40 return ENOMEM; 41 #ifdef HAVE_C_STRUCTURE_ASSIGNMENT 42 *tmpad = *inad; 43 #else 44 memcpy(tmpad, inad, sizeof(krb5_address)); 45 #endif 46 if (!(tmpad->contents = (krb5_octet *)malloc(inad->length))) { 47 krb5_xfree(tmpad); 48 return ENOMEM; 49 } 50 memcpy((char *)tmpad->contents, (char *)inad->contents, inad->length); 51 *outad = tmpad; 52 return 0; 53 } 54 55 /* 56 * Copy an address array, with fresh allocation. 57 */ 58 krb5_error_code KRB5_CALLCONV 59 krb5_copy_addresses(krb5_context context, krb5_address *const *inaddr, krb5_address ***outaddr) 60 { 61 krb5_error_code retval; 62 krb5_address ** tempaddr; 63 register unsigned int nelems = 0; 64 65 if (!inaddr) { 66 *outaddr = 0; 67 return 0; 68 } 69 70 while (inaddr[nelems]) nelems++; 71 72 /* one more for a null terminated list */ 73 if (!(tempaddr = (krb5_address **) calloc(nelems+1, sizeof(*tempaddr)))) 74 return ENOMEM; 75 76 for (nelems = 0; inaddr[nelems]; nelems++) { 77 retval = krb5_copy_addr(context, inaddr[nelems], &tempaddr[nelems]); 78 if (retval) { 79 krb5_free_addresses(context, tempaddr); 80 return retval; 81 } 82 } 83 84 *outaddr = tempaddr; 85 return 0; 86 } 87 88 #if 0 89 /* 90 * Append an address array, to another address array, with fresh allocation. 91 * Note that this function may change the value of *outaddr even if it 92 * returns failure, but it will not change the contents of the list. 93 */ 94 krb5_error_code 95 krb5_append_addresses(context, inaddr, outaddr) 96 krb5_context context; 97 krb5_address * const * inaddr; 98 krb5_address ***outaddr; 99 { 100 krb5_error_code retval; 101 krb5_address ** tempaddr; 102 krb5_address ** tempaddr2; 103 register unsigned int nelems = 0; 104 register int norigelems = 0; 105 106 if (!inaddr) 107 return 0; 108 109 tempaddr2 = *outaddr; 110 111 while (inaddr[nelems]) nelems++; 112 while (tempaddr2[norigelems]) norigelems++; 113 114 tempaddr = (krb5_address **) realloc((char *)*outaddr, 115 (nelems + norigelems + 1) * sizeof(*tempaddr)); 116 if (!tempaddr) 117 return ENOMEM; 118 119 /* The old storage has been freed. */ 120 *outaddr = tempaddr; 121 122 123 for (nelems = 0; inaddr[nelems]; nelems++) { 124 retval = krb5_copy_addr(context, inaddr[nelems], 125 &tempaddr[norigelems + nelems]); 126 if (retval) 127 goto cleanup; 128 } 129 130 tempaddr[norigelems + nelems] = 0; 131 return 0; 132 133 cleanup: 134 while (--nelems >= 0) 135 krb5_free_address(context, tempaddr[norigelems + nelems]); 136 137 /* Try to allocate a smaller amount of memory for *outaddr. */ 138 tempaddr = (krb5_address **) realloc((char *)tempaddr, 139 (norigelems + 1) * sizeof(*tempaddr)); 140 if (tempaddr) 141 *outaddr = tempaddr; 142 return retval; 143 } 144 #endif 145 146