1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 /*
3  * lib/krb5/krb/copy_data.c
4  *
5  * Copyright 1990,1991 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_data()
29  */
30 
31 #include <k5-int.h>
32 
33 /*
34  * Copy a data structure, with fresh allocation.
35  */
36 /*ARGSUSED*/
37 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
38 krb5_copy_data(context, indata, outdata)
39     krb5_context context;
40     const krb5_data FAR *indata;
41     krb5_data FAR * FAR *outdata;
42 {
43     krb5_data *tempdata;
44 
45     if (!indata) {
46 	*outdata = 0;
47 	return 0;
48     }
49 
50     if (!(tempdata = (krb5_data *)malloc(sizeof(*tempdata))))
51 	return ENOMEM;
52 
53     tempdata->length = indata->length;
54     if (tempdata->length) {
55 	if (!(tempdata->data = malloc(tempdata->length))) {
56 	    krb5_xfree(tempdata);
57 	    return ENOMEM;
58 	}
59 	memcpy((char *)tempdata->data, (char *)indata->data, tempdata->length);
60     } else
61 	tempdata->data = 0;
62     tempdata->magic = KV5M_DATA;
63     *outdata = tempdata;
64     return 0;
65 }
66 
67 krb5_error_code
68 krb5int_copy_data_contents(krb5_context context, const krb5_data *indata, krb5_data *outdata)
69 {
70     if (!indata) {
71 	return EINVAL;
72     }
73 
74     outdata->length = indata->length;
75     if (outdata->length) {
76 	if (!(outdata->data = malloc(outdata->length))) {
77             krb5_xfree(outdata);
78             return ENOMEM;
79 	}
80         memcpy((char *)outdata->data, (char *)indata->data, outdata->length);
81     } else
82         outdata->data = 0;
83     outdata->magic = KV5M_DATA;
84 
85     return 0;
86 }
87 
88