1/*
2 * Copyright (c) 2011-12 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
25 * (Royal Institute of Technology, Stockholm, Sweden).
26 * All rights reserved.
27 *
28 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 *
34 * 1. Redistributions of source code must retain the above copyright
35 *    notice, this list of conditions and the following disclaimer.
36 *
37 * 2. Redistributions in binary form must reproduce the above copyright
38 *    notice, this list of conditions and the following disclaimer in the
39 *    documentation and/or other materials provided with the distribution.
40 *
41 * 3. Neither the name of the Institute nor the names of its contributors
42 *    may be used to endorse or promote products derived from this software
43 *    without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58#include "asn1-der_locl.h"
59
60RCSID("$Id$");
61
62int
63der_copy_general_string (const heim_general_string *from,
64			 heim_general_string *to)
65{
66    *to = strdup(*from);
67    if(*to == NULL)
68	return ENOMEM;
69    return 0;
70}
71
72int
73der_copy_integer (const int *from, int *to)
74{
75    *to = *from;
76    return 0;
77}
78
79int
80der_copy_unsigned (const unsigned *from, unsigned *to)
81{
82    *to = *from;
83    return 0;
84}
85
86int
87der_copy_generalized_time (const time_t *from, time_t *to)
88{
89    *to = *from;
90    return 0;
91}
92
93int
94der_copy_utctime (const time_t *from, time_t *to)
95{
96    *to = *from;
97    return 0;
98}
99
100int
101der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
102{
103    return der_copy_general_string(from, to);
104}
105
106int
107der_copy_printable_string (const heim_printable_string *from,
108		       heim_printable_string *to)
109{
110    to->length = from->length;
111    to->data   = malloc(to->length + 1);
112    if(to->data == NULL)
113	return ENOMEM;
114    memcpy(to->data, from->data, to->length);
115    ((char *)to->data)[to->length] = '\0';
116    return 0;
117}
118
119int
120der_copy_ia5_string (const heim_ia5_string *from,
121		     heim_ia5_string *to)
122{
123    return der_copy_printable_string(from, to);
124}
125
126int
127der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
128{
129    to->length = from->length;
130    to->data   = malloc(to->length * sizeof(to->data[0]));
131    if(to->length != 0 && to->data == NULL)
132	return ENOMEM;
133    memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
134    return 0;
135}
136
137int
138der_copy_universal_string (const heim_universal_string *from,
139			   heim_universal_string *to)
140{
141    to->length = from->length;
142    to->data   = malloc(to->length * sizeof(to->data[0]));
143    if(to->length != 0 && to->data == NULL)
144	return ENOMEM;
145    memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
146    return 0;
147}
148
149int
150der_copy_visible_string (const heim_visible_string *from,
151			 heim_visible_string *to)
152{
153    return der_copy_general_string(from, to);
154}
155
156int
157der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
158{
159    to->length = from->length;
160    to->data   = malloc(to->length);
161    if(to->length != 0 && to->data == NULL)
162	return ENOMEM;
163    memcpy(to->data, from->data, to->length);
164    return 0;
165}
166
167int
168der_copy_heim_integer (const heim_integer *from, heim_integer *to)
169{
170    to->length = from->length;
171    to->data   = malloc(to->length);
172    if(to->length != 0 && to->data == NULL)
173	return ENOMEM;
174    memcpy(to->data, from->data, to->length);
175    to->negative = from->negative;
176    return 0;
177}
178
179int
180der_copy_oid (const heim_oid *from, heim_oid *to)
181{
182    to->length     = from->length;
183    to->components = malloc(to->length * sizeof(*to->components));
184    if (to->length != 0 && to->components == NULL)
185	return ENOMEM;
186    memcpy(to->components, from->components,
187	   to->length * sizeof(*to->components));
188    return 0;
189}
190
191int
192der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
193{
194    size_t len;
195
196    len = (from->length + 7) / 8;
197    to->length = from->length;
198    to->data   = malloc(len);
199    if(len != 0 && to->data == NULL)
200	return ENOMEM;
201    memcpy(to->data, from->data, len);
202    return 0;
203}
204