155682Smarkm/*
2233294Sstas * Copyright (c) 1997 - 2006 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
755682Smarkm *
8233294Sstas * Redistribution and use in source and binary forms, with or without
9233294Sstas * modification, are permitted provided that the following conditions
10233294Sstas * are met:
1155682Smarkm *
12233294Sstas * 1. Redistributions of source code must retain the above copyright
13233294Sstas *    notice, this list of conditions and the following disclaimer.
1455682Smarkm *
15233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
16233294Sstas *    notice, this list of conditions and the following disclaimer in the
17233294Sstas *    documentation and/or other materials provided with the distribution.
1855682Smarkm *
19233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
20233294Sstas *    may be used to endorse or promote products derived from this software
21233294Sstas *    without specific prior written permission.
22233294Sstas *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33233294Sstas * SUCH DAMAGE.
3455682Smarkm */
3555682Smarkm
3655682Smarkm#include "der_locl.h"
3755682Smarkm
38233294SstasRCSID("$Id$");
3955682Smarkm
4055682Smarkmint
41233294Sstasder_copy_general_string (const heim_general_string *from,
42178825Sdfr			 heim_general_string *to)
4355682Smarkm{
44120945Snectar    *to = strdup(*from);
4555682Smarkm    if(*to == NULL)
4655682Smarkm	return ENOMEM;
4755682Smarkm    return 0;
4855682Smarkm}
4955682Smarkm
5055682Smarkmint
51233294Sstasder_copy_integer (const int *from, int *to)
52233294Sstas{
53233294Sstas    *to = *from;
54233294Sstas    return 0;
55233294Sstas}
56233294Sstas
57233294Sstasint
58233294Sstasder_copy_unsigned (const unsigned *from, unsigned *to)
59233294Sstas{
60233294Sstas    *to = *from;
61233294Sstas    return 0;
62233294Sstas}
63233294Sstas
64233294Sstasint
65233294Sstasder_copy_generalized_time (const time_t *from, time_t *to)
66233294Sstas{
67233294Sstas    *to = *from;
68233294Sstas    return 0;
69233294Sstas}
70233294Sstas
71233294Sstasint
72233294Sstasder_copy_utctime (const time_t *from, time_t *to)
73233294Sstas{
74233294Sstas    *to = *from;
75233294Sstas    return 0;
76233294Sstas}
77233294Sstas
78233294Sstasint
79178825Sdfrder_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
8055682Smarkm{
81178825Sdfr    return der_copy_general_string(from, to);
82178825Sdfr}
83178825Sdfr
84178825Sdfrint
85233294Sstasder_copy_printable_string (const heim_printable_string *from,
86178825Sdfr		       heim_printable_string *to)
87178825Sdfr{
88233294Sstas    to->length = from->length;
89233294Sstas    to->data   = malloc(to->length + 1);
90233294Sstas    if(to->data == NULL)
91233294Sstas	return ENOMEM;
92233294Sstas    memcpy(to->data, from->data, to->length);
93233294Sstas    ((char *)to->data)[to->length] = '\0';
94233294Sstas    return 0;
95178825Sdfr}
96178825Sdfr
97178825Sdfrint
98233294Sstasder_copy_ia5_string (const heim_ia5_string *from,
99233294Sstas		     heim_ia5_string *to)
100178825Sdfr{
101233294Sstas    return der_copy_printable_string(from, to);
102178825Sdfr}
103178825Sdfr
104178825Sdfrint
105178825Sdfrder_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
106178825Sdfr{
10755682Smarkm    to->length = from->length;
108178825Sdfr    to->data   = malloc(to->length * sizeof(to->data[0]));
109178825Sdfr    if(to->length != 0 && to->data == NULL)
110178825Sdfr	return ENOMEM;
111178825Sdfr    memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
112178825Sdfr    return 0;
113178825Sdfr}
114178825Sdfr
115178825Sdfrint
116178825Sdfrder_copy_universal_string (const heim_universal_string *from,
117178825Sdfr			   heim_universal_string *to)
118178825Sdfr{
119178825Sdfr    to->length = from->length;
120178825Sdfr    to->data   = malloc(to->length * sizeof(to->data[0]));
121178825Sdfr    if(to->length != 0 && to->data == NULL)
122178825Sdfr	return ENOMEM;
123178825Sdfr    memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
124178825Sdfr    return 0;
125178825Sdfr}
126178825Sdfr
127178825Sdfrint
128233294Sstasder_copy_visible_string (const heim_visible_string *from,
129178825Sdfr			 heim_visible_string *to)
130178825Sdfr{
131178825Sdfr    return der_copy_general_string(from, to);
132178825Sdfr}
133178825Sdfr
134178825Sdfrint
135178825Sdfrder_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
136178825Sdfr{
137178825Sdfr    to->length = from->length;
13855682Smarkm    to->data   = malloc(to->length);
13955682Smarkm    if(to->length != 0 && to->data == NULL)
14055682Smarkm	return ENOMEM;
14155682Smarkm    memcpy(to->data, from->data, to->length);
14255682Smarkm    return 0;
14355682Smarkm}
14490926Snectar
14590926Snectarint
146178825Sdfrder_copy_heim_integer (const heim_integer *from, heim_integer *to)
14790926Snectar{
148178825Sdfr    to->length = from->length;
149178825Sdfr    to->data   = malloc(to->length);
150178825Sdfr    if(to->length != 0 && to->data == NULL)
151178825Sdfr	return ENOMEM;
152178825Sdfr    memcpy(to->data, from->data, to->length);
153178825Sdfr    to->negative = from->negative;
154178825Sdfr    return 0;
155178825Sdfr}
156178825Sdfr
157178825Sdfrint
158178825Sdfrder_copy_oid (const heim_oid *from, heim_oid *to)
159178825Sdfr{
16090926Snectar    to->length     = from->length;
16190926Snectar    to->components = malloc(to->length * sizeof(*to->components));
16290926Snectar    if (to->length != 0 && to->components == NULL)
16390926Snectar	return ENOMEM;
164178825Sdfr    memcpy(to->components, from->components,
165178825Sdfr	   to->length * sizeof(*to->components));
16690926Snectar    return 0;
16790926Snectar}
168178825Sdfr
169178825Sdfrint
170178825Sdfrder_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
171178825Sdfr{
172178825Sdfr    size_t len;
173178825Sdfr
174178825Sdfr    len = (from->length + 7) / 8;
175178825Sdfr    to->length = from->length;
176178825Sdfr    to->data   = malloc(len);
177178825Sdfr    if(len != 0 && to->data == NULL)
178178825Sdfr	return ENOMEM;
179178825Sdfr    memcpy(to->data, from->data, len);
180178825Sdfr    return 0;
181178825Sdfr}
182