#include "k5-int.h"
krb5_error_code KRB5_CALLCONV
krb5_copy_principal(krb5_context context, krb5_const_principal inprinc, krb5_principal *outprinc)
{
register krb5_principal tempprinc;
register int i, nelems;
tempprinc = (krb5_principal)malloc(sizeof(krb5_principal_data));
if (tempprinc == 0)
return ENOMEM;
#ifdef HAVE_C_STRUCTURE_ASSIGNMENT
*tempprinc = *inprinc;
#else
memcpy(tempprinc, inprinc, sizeof(krb5_principal_data));
#endif
nelems = (int) krb5_princ_size(context, inprinc);
tempprinc->data = malloc(nelems * sizeof(krb5_data));
if (tempprinc->data == 0) {
free((char *)tempprinc);
return ENOMEM;
}
for (i = 0; i < nelems; i++) {
unsigned int len = krb5_princ_component(context, inprinc, i)->length;
krb5_princ_component(context, tempprinc, i)->length = len;
if (len) {
if (((krb5_princ_component(context, tempprinc, i)->data =
malloc(len)) == 0)) {
while (--i >= 0)
free(krb5_princ_component(context, tempprinc, i)->data);
free (tempprinc->data);
free (tempprinc);
return ENOMEM;
}
memcpy(krb5_princ_component(context, tempprinc, i)->data,
krb5_princ_component(context, inprinc, i)->data, len);
} else
krb5_princ_component(context, tempprinc, i)->data = 0;
}
if (tempprinc->realm.length) {
tempprinc->realm.data =
malloc(tempprinc->realm.length = inprinc->realm.length);
if (!tempprinc->realm.data) {
for (i = 0; i < nelems; i++)
free(krb5_princ_component(context, tempprinc, i)->data);
free(tempprinc->data);
free(tempprinc);
return ENOMEM;
}
memcpy(tempprinc->realm.data, inprinc->realm.data,
inprinc->realm.length);
} else
tempprinc->realm.data = 0;
*outprinc = tempprinc;
return 0;
}