summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/krb/copy_addrs.c
blob: 04042f68a8e5b50de14dc38e9300befee422714a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <krb5/mit-copyright.h>.
 *
 * krb5_copy_addresses()
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_copy_addrs_c[] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/copyright.h>
#include <krb5/krb5.h>
#include <errno.h>

#include <krb5/ext-proto.h>

static krb5_error_code
krb5_copy_addr(inad, outad)
const krb5_address *inad;
krb5_address **outad;
{
    krb5_address *tmpad;

    if (!(tmpad = (krb5_address *)malloc(sizeof(*tmpad))))
	return ENOMEM;
    *tmpad = *inad;
    if (!(tmpad->contents = (krb5_octet *)malloc(inad->length))) {
	xfree(tmpad);
	return ENOMEM;
    }
    bcopy((char *)inad->contents, (char *)tmpad->contents, inad->length);
    *outad = tmpad;
    return 0;
}

/*
 * Copy an address array, with fresh allocation.
 */
krb5_error_code
krb5_copy_addresses(inaddr, outaddr)
krb5_address * const * inaddr;
krb5_address ***outaddr;
{
    krb5_error_code retval;
    krb5_address ** tempaddr;
    register int nelems;

    for (nelems = 0; inaddr[nelems]; nelems++);

    /* one more for a null terminated list */
    if (!(tempaddr = (krb5_address **) calloc(nelems+1, sizeof(*tempaddr))))
	return ENOMEM;

    for (nelems = 0; inaddr[nelems]; nelems++)
	if (retval = krb5_copy_addr(inaddr[nelems], &tempaddr[nelems])) {
	    krb5_free_address(tempaddr);
	    return retval;
	}

    *outaddr = tempaddr;
    return 0;
}