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_error_code KRB5_CALLCONV 38 krb5_copy_data(krb5_context context, const krb5_data *indata, krb5_data **outdata) 39 { 40 krb5_data *tempdata; 41 42 if (!indata) { 43 *outdata = 0; 44 return 0; 45 } 46 47 if (!(tempdata = (krb5_data *)malloc(sizeof(*tempdata)))) 48 return ENOMEM; 49 50 tempdata->length = indata->length; 51 if (tempdata->length) { 52 if (!(tempdata->data = malloc(tempdata->length))) { 53 krb5_xfree(tempdata); 54 return ENOMEM; 55 } 56 memcpy((char *)tempdata->data, (char *)indata->data, tempdata->length); 57 } else 58 tempdata->data = 0; 59 tempdata->magic = KV5M_DATA; 60 *outdata = tempdata; 61 return 0; 62 } 63 64 krb5_error_code 65 krb5int_copy_data_contents(krb5_context context, const krb5_data *indata, krb5_data *outdata) 66 { 67 if (!indata) { 68 return EINVAL; 69 } 70 71 outdata->length = indata->length; 72 if (outdata->length) { 73 if (!(outdata->data = malloc(outdata->length))) { 74 krb5_xfree(outdata); 75 return ENOMEM; 76 } 77 memcpy((char *)outdata->data, (char *)indata->data, outdata->length); 78 } else 79 outdata->data = 0; 80 outdata->magic = KV5M_DATA; 81 82 return 0; 83 } 84 85