LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/asn1 - der_copy.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 69 118 58.5 %
Date: 2024-06-13 04:01:37 Functions: 12 17 70.6 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
       7             :  *
       8             :  * Redistribution and use in source and binary forms, with or without
       9             :  * modification, are permitted provided that the following conditions
      10             :  * are met:
      11             :  *
      12             :  * 1. Redistributions of source code must retain the above copyright
      13             :  *    notice, this list of conditions and the following disclaimer.
      14             :  *
      15             :  * 2. Redistributions in binary form must reproduce the above copyright
      16             :  *    notice, this list of conditions and the following disclaimer in the
      17             :  *    documentation and/or other materials provided with the distribution.
      18             :  *
      19             :  * 3. Neither the name of the Institute nor the names of its contributors
      20             :  *    may be used to endorse or promote products derived from this software
      21             :  *    without specific prior written permission.
      22             :  *
      23             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      24             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      25             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      26             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      27             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      28             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      29             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      30             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      31             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      32             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      33             :  * SUCH DAMAGE.
      34             :  */
      35             : 
      36             : #include "der_locl.h"
      37             : 
      38             : RCSID("$Id$");
      39             : 
      40             : int ASN1CALL
      41    13668125 : der_copy_general_string (const heim_general_string *from,
      42             :                          heim_general_string *to)
      43             : {
      44    13668125 :     *to = strdup(*from);
      45    13668125 :     if(*to == NULL)
      46           0 :         return ENOMEM;
      47    13668125 :     return 0;
      48             : }
      49             : 
      50             : int ASN1CALL
      51    19995991 : der_copy_integer (const int *from, int *to)
      52             : {
      53    19995991 :     *to = *from;
      54    19995991 :     return 0;
      55             : }
      56             : 
      57             : int ASN1CALL
      58           0 : der_copy_integer64 (const int64_t *from, int64_t *to)
      59             : {
      60           0 :     *to = *from;
      61           0 :     return 0;
      62             : }
      63             : 
      64             : int ASN1CALL
      65       21153 : der_copy_unsigned (const unsigned *from, unsigned *to)
      66             : {
      67       21153 :     *to = *from;
      68       21153 :     return 0;
      69             : }
      70             : 
      71             : int ASN1CALL
      72           0 : der_copy_unsigned64 (const uint64_t *from, uint64_t *to)
      73             : {
      74           0 :     *to = *from;
      75           0 :     return 0;
      76             : }
      77             : 
      78             : int ASN1CALL
      79     4166462 : der_copy_generalized_time (const time_t *from, time_t *to)
      80             : {
      81     4166462 :     *to = *from;
      82     4166462 :     return 0;
      83             : }
      84             : 
      85             : int ASN1CALL
      86         302 : der_copy_utctime (const time_t *from, time_t *to)
      87             : {
      88         302 :     *to = *from;
      89         302 :     return 0;
      90             : }
      91             : 
      92             : int ASN1CALL
      93        1322 : der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
      94             : {
      95        1322 :     return der_copy_general_string(from, to);
      96             : }
      97             : 
      98             : int ASN1CALL
      99        1609 : der_copy_printable_string (const heim_printable_string *from,
     100             :                        heim_printable_string *to)
     101             : {
     102        1609 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     103        1609 :     to->data = malloc(from->length + 1);
     104        1609 :     if (to->data == NULL) {
     105           0 :         to->length = 0;
     106           0 :         return ENOMEM;
     107             :     }
     108        1609 :     to->length = from->length;
     109        1609 :     if (to->length > 0)
     110        1609 :         memcpy(to->data, from->data, to->length);
     111        1609 :     ((char *)to->data)[to->length] = '\0';
     112        1609 :     return 0;
     113             : }
     114             : 
     115             : int ASN1CALL
     116         572 : der_copy_ia5_string (const heim_ia5_string *from,
     117             :                      heim_ia5_string *to)
     118             : {
     119         572 :     return der_copy_printable_string(from, to);
     120             : }
     121             : 
     122             : int ASN1CALL
     123           0 : der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
     124             : {
     125           0 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     126           0 :     if (from->length == 0)
     127           0 :         to->data = calloc(1, sizeof(from->data[0]));
     128             :     else
     129           0 :         to->data = malloc(from->length * sizeof(from->data[0]));
     130           0 :     if (to->data == NULL) {
     131           0 :         to->length = 0;
     132           0 :         return ENOMEM;
     133             :     }
     134           0 :     to->length = from->length;
     135           0 :     if (to->length > 0)
     136           0 :         memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
     137           0 :     return 0;
     138             : }
     139             : 
     140             : int ASN1CALL
     141           0 : der_copy_universal_string (const heim_universal_string *from,
     142             :                            heim_universal_string *to)
     143             : {
     144           0 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     145           0 :     if (from->length == 0)
     146           0 :         to->data = calloc(1, sizeof(from->data[0]));
     147             :     else
     148           0 :         to->data = malloc(from->length * sizeof(from->data[0]));
     149           0 :     if (to->data == NULL) {
     150           0 :         to->length = 0;
     151           0 :         return ENOMEM;
     152             :     }
     153           0 :     to->length = from->length;
     154           0 :     if (to->length > 0)
     155           0 :         memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
     156           0 :     return 0;
     157             : }
     158             : 
     159             : int ASN1CALL
     160           0 : der_copy_visible_string (const heim_visible_string *from,
     161             :                          heim_visible_string *to)
     162             : {
     163           0 :     return der_copy_general_string(from, to);
     164             : }
     165             : 
     166             : int ASN1CALL
     167     9911256 : der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
     168             : {
     169     9911256 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     170     9911256 :     if (from->length == 0)
     171     2405470 :         to->data = calloc(1, 1);
     172             :     else
     173     7505786 :         to->data = malloc(from->length);
     174     9911256 :     if (to->data == NULL) {
     175           0 :         to->length = 0;
     176           0 :         return ENOMEM;
     177             :     }
     178     9911256 :     to->length = from->length;
     179     9911256 :     if (to->length > 0)
     180     7505786 :         memcpy(to->data, from->data, to->length);
     181     9911256 :     return 0;
     182             : }
     183             : 
     184             : int ASN1CALL
     185         454 : der_copy_heim_integer (const heim_integer *from, heim_integer *to)
     186             : {
     187         454 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     188         454 :     if (from->length == 0)
     189           6 :         to->data = calloc(1, 1);
     190             :     else
     191         448 :         to->data = malloc(from->length);
     192         454 :     if (to->data == NULL) {
     193           0 :         to->length = 0;
     194           0 :         return ENOMEM;
     195             :     }
     196         454 :     to->length = from->length;
     197         454 :     if (to->length > 0)
     198         448 :         memcpy(to->data, from->data, to->length);
     199         454 :     to->negative = from->negative;
     200         454 :     return 0;
     201             : }
     202             : 
     203             : int ASN1CALL
     204        5804 : der_copy_oid (const heim_oid *from, heim_oid *to)
     205             : {
     206        5804 :     if (from->length == 0) {
     207           0 :         to->length = 0;
     208           0 :         to->components = calloc(1, sizeof(*from->components));
     209           0 :         if (to->components == NULL)
     210           0 :             return ENOMEM;
     211           0 :         return 0;
     212             :     }
     213        5804 :     assert(from->components != NULL);
     214        5804 :     to->components = malloc(from->length * sizeof(*from->components));
     215        5804 :     if (to->components == NULL) {
     216           0 :         to->length = 0;
     217           0 :         return ENOMEM;
     218             :     }
     219        5804 :     to->length = from->length;
     220        5804 :     memcpy(to->components, from->components,
     221        5804 :            to->length * sizeof(*to->components));
     222        5804 :     return 0;
     223             : }
     224             : 
     225             : int ASN1CALL
     226         302 : der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
     227             : {
     228             :     size_t len;
     229             : 
     230         302 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     231             : 
     232         302 :     len = (from->length + 7) / 8;
     233         302 :     if (len == 0)
     234           0 :         to->data = calloc(1, 1);
     235             :     else
     236         302 :         to->data = malloc(len);
     237         302 :     if (to->data == NULL) {
     238           0 :         to->length = 0;
     239           0 :         return ENOMEM;
     240             :     }
     241         302 :     to->length = from->length;
     242         302 :     if (len > 0)
     243         302 :         memcpy(to->data, from->data, len);
     244         302 :     return 0;
     245             : }

Generated by: LCOV version 1.13