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 master 98b443d9 Lines: 73 122 59.8 %
Date: 2024-05-31 13:13:24 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    18097291 : der_copy_general_string (const heim_general_string *from,
      42             :                          heim_general_string *to)
      43             : {
      44    18097291 :     *to = strdup(*from);
      45    18097291 :     if(*to == NULL)
      46           0 :         return ENOMEM;
      47    17478159 :     return 0;
      48             : }
      49             : 
      50             : int ASN1CALL
      51    26684739 : der_copy_integer (const int *from, int *to)
      52             : {
      53    26684739 :     *to = *from;
      54    26684739 :     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       30843 : der_copy_unsigned (const unsigned *from, unsigned *to)
      66             : {
      67       30843 :     *to = *from;
      68       30843 :     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     5397459 : der_copy_generalized_time (const time_t *from, time_t *to)
      80             : {
      81     5397459 :     *to = *from;
      82     5397459 :     return 0;
      83             : }
      84             : 
      85             : int ASN1CALL
      86         646 : der_copy_utctime (const time_t *from, time_t *to)
      87             : {
      88         646 :     *to = *from;
      89         646 :     return 0;
      90             : }
      91             : 
      92             : int ASN1CALL
      93        4026 : der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
      94             : {
      95        4026 :     return der_copy_general_string(from, to);
      96             : }
      97             : 
      98             : int ASN1CALL
      99        3143 : der_copy_printable_string (const heim_printable_string *from,
     100             :                        heim_printable_string *to)
     101             : {
     102        3143 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     103        3143 :     to->data = malloc(from->length + 1);
     104        3143 :     if (to->data == NULL) {
     105           0 :         to->length = 0;
     106           0 :         return ENOMEM;
     107             :     }
     108        3143 :     to->length = from->length;
     109        3143 :     if (to->length > 0)
     110        3143 :         memcpy(to->data, from->data, to->length);
     111        3143 :     ((char *)to->data)[to->length] = '\0';
     112        3143 :     return 0;
     113             : }
     114             : 
     115             : int ASN1CALL
     116        1417 : der_copy_ia5_string (const heim_ia5_string *from,
     117             :                      heim_ia5_string *to)
     118             : {
     119        1417 :     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    13486331 : der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
     168             : {
     169    13486331 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     170    13486331 :     if (from->length == 0) {
     171     3154603 :         if (from->data == NULL) {
     172     1951060 :             *to = *from;
     173     1951060 :             return 0;
     174             :         }
     175     1203543 :         to->data = calloc(1, 1);
     176             :     } else
     177    10331728 :         to->data = malloc(from->length);
     178    11535271 :     if (to->data == NULL) {
     179           0 :         to->length = 0;
     180           0 :         return ENOMEM;
     181             :     }
     182    11535271 :     to->length = from->length;
     183    11535271 :     if (to->length > 0)
     184    10331728 :         memcpy(to->data, from->data, to->length);
     185    11127420 :     return 0;
     186             : }
     187             : 
     188             : int ASN1CALL
     189         903 : der_copy_heim_integer (const heim_integer *from, heim_integer *to)
     190             : {
     191         903 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     192         903 :     if (from->length == 0)
     193           6 :         to->data = calloc(1, 1);
     194             :     else
     195         897 :         to->data = malloc(from->length);
     196         903 :     if (to->data == NULL) {
     197           0 :         to->length = 0;
     198           0 :         return ENOMEM;
     199             :     }
     200         903 :     to->length = from->length;
     201         903 :     if (to->length > 0)
     202         897 :         memcpy(to->data, from->data, to->length);
     203         903 :     to->negative = from->negative;
     204         903 :     return 0;
     205             : }
     206             : 
     207             : int ASN1CALL
     208       13985 : der_copy_oid (const heim_oid *from, heim_oid *to)
     209             : {
     210       13985 :     if (from->length == 0) {
     211           0 :         to->length = 0;
     212           0 :         to->components = calloc(1, sizeof(*from->components));
     213           0 :         if (to->components == NULL)
     214           0 :             return ENOMEM;
     215           0 :         return 0;
     216             :     }
     217       13985 :     assert(from->components != NULL);
     218       13985 :     to->components = malloc(from->length * sizeof(*from->components));
     219       13985 :     if (to->components == NULL) {
     220           0 :         to->length = 0;
     221           0 :         return ENOMEM;
     222             :     }
     223       13985 :     to->length = from->length;
     224       13985 :     memcpy(to->components, from->components,
     225       13529 :            to->length * sizeof(*to->components));
     226       13985 :     return 0;
     227             : }
     228             : 
     229             : int ASN1CALL
     230         646 : der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
     231             : {
     232          32 :     size_t len;
     233             : 
     234         646 :     assert(from->length == 0 || (from->length > 0 && from->data != NULL));
     235             : 
     236         646 :     len = (from->length + 7) / 8;
     237         646 :     if (len == 0)
     238           0 :         to->data = calloc(1, 1);
     239             :     else
     240         646 :         to->data = malloc(len);
     241         646 :     if (to->data == NULL) {
     242           0 :         to->length = 0;
     243           0 :         return ENOMEM;
     244             :     }
     245         646 :     to->length = from->length;
     246         646 :     if (len > 0)
     247         646 :         memcpy(to->data, from->data, len);
     248         614 :     return 0;
     249             : }

Generated by: LCOV version 1.14