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