LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/asn1 - der_length.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 91 143 63.6 %
Date: 2024-04-21 15:09:00 Functions: 18 26 69.2 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997-2005 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             : size_t
      41       97946 : _heim_len_unsigned (unsigned val)
      42             : {
      43       97946 :     size_t ret = 0;
      44       14650 :     int last_val_gt_128;
      45             : 
      46       14650 :     do {
      47      390746 :         ++ret;
      48      390746 :         last_val_gt_128 = (val >= 128);
      49      390746 :         val /= 256;
      50      390746 :     } while (val);
      51             : 
      52       97946 :     if(last_val_gt_128)
      53       22504 :         ret++;
      54             : 
      55       97946 :     return ret;
      56             : }
      57             : 
      58             : size_t
      59           0 : _heim_len_unsigned64 (uint64_t val)
      60             : {
      61           0 :     size_t ret = 0;
      62           0 :     int last_val_gt_128;
      63             : 
      64           0 :     do {
      65           0 :         ++ret;
      66           0 :         last_val_gt_128 = (val >= 128);
      67           0 :         val /= 256;
      68           0 :     } while (val);
      69             : 
      70           0 :     if(last_val_gt_128)
      71           0 :         ret++;
      72             : 
      73           0 :     return ret;
      74             : }
      75             : 
      76             : size_t
      77     6485556 : _heim_len_int (int val)
      78             : {
      79      232477 :     unsigned char q;
      80     6485556 :     size_t ret = 0;
      81             : 
      82     6485556 :     if (val >= 0) {
      83      259194 :         do {
      84     7254958 :             q = val % 256;
      85     7254958 :             ret++;
      86     7254958 :             val /= 256;
      87     7254958 :         } while(val);
      88     6377682 :         if(q >= 128)
      89      400923 :             ret++;
      90             :     } else {
      91      107874 :         val = ~val;
      92       13652 :         do {
      93      415951 :             q = ~(val % 256);
      94      415951 :             ret++;
      95      415951 :             val /= 256;
      96      415951 :         } while(val);
      97      107874 :         if(q < 128)
      98        5332 :             ret++;
      99             :     }
     100     6485556 :     return ret;
     101             : }
     102             : 
     103             : size_t
     104           0 : _heim_len_int64 (int64_t val)
     105             : {
     106           0 :     unsigned char q;
     107           0 :     size_t ret = 0;
     108             : 
     109           0 :     if (val >= 0) {
     110           0 :         do {
     111           0 :             q = val % 256;
     112           0 :             ret++;
     113           0 :             val /= 256;
     114           0 :         } while(val);
     115           0 :         if(q >= 128)
     116           0 :             ret++;
     117             :     } else {
     118           0 :         val = ~val;
     119           0 :         do {
     120           0 :             q = ~(val % 256);
     121           0 :             ret++;
     122           0 :             val /= 256;
     123           0 :         } while(val);
     124           0 :         if(q < 128)
     125           0 :             ret++;
     126             :     }
     127           0 :     return ret;
     128             : }
     129             : 
     130             : static size_t
     131        7716 : len_oid (const heim_oid *oid)
     132             : {
     133        7716 :     size_t ret = 1;
     134           0 :     size_t n;
     135             : 
     136       38618 :     for (n = 2; n < oid->length; ++n) {
     137       30902 :         unsigned u = oid->components[n];
     138             : 
     139           0 :         do {
     140       41540 :             ++ret;
     141       41540 :             u /= 128;
     142       41540 :         } while(u > 0);
     143             :     }
     144        7716 :     return ret;
     145             : }
     146             : 
     147             : size_t ASN1CALL
     148    36886814 : der_length_len (size_t len)
     149             : {
     150    36886814 :     if (len < 128)
     151    28871782 :         return 1;
     152             :     else {
     153     6701419 :         int ret = 0;
     154      432138 :         do {
     155    11871456 :             ++ret;
     156    11871456 :             len /= 256;
     157    11871456 :         } while (len);
     158     6951366 :         return ret + 1;
     159             :     }
     160             : }
     161             : 
     162             : size_t ASN1CALL
     163    36680273 : der_length_tag(unsigned int tag)
     164             : {
     165    36680273 :     size_t len = 0;
     166             : 
     167    36680273 :     if(tag <= 30)
     168    35370096 :         return 1;
     169           0 :     while(tag) {
     170           0 :         tag /= 128;
     171           0 :         len++;
     172             :     }
     173           0 :     return len + 1;
     174             : }
     175             : 
     176             : size_t ASN1CALL
     177     6485556 : der_length_integer (const int *data)
     178             : {
     179     6485556 :     return _heim_len_int (*data);
     180             : }
     181             : 
     182             : size_t ASN1CALL
     183           0 : der_length_integer64 (const int64_t *data)
     184             : {
     185           0 :     return _heim_len_int64 (*data);
     186             : }
     187             : 
     188             : size_t ASN1CALL
     189       97946 : der_length_unsigned (const unsigned *data)
     190             : {
     191       97946 :     return _heim_len_unsigned(*data);
     192             : }
     193             : 
     194             : size_t ASN1CALL
     195           0 : der_length_unsigned64 (const uint64_t *data)
     196             : {
     197           0 :     return _heim_len_unsigned64(*data);
     198             : }
     199             : 
     200             : size_t ASN1CALL
     201           0 : der_length_enumerated (const unsigned *data)
     202             : {
     203           0 :   return _heim_len_int (*data);
     204             : }
     205             : 
     206             : size_t ASN1CALL
     207     2938860 : der_length_general_string (const heim_general_string *data)
     208             : {
     209     2938860 :     return strlen(*data);
     210             : }
     211             : 
     212             : size_t ASN1CALL
     213        1790 : der_length_utf8string (const heim_utf8_string *data)
     214             : {
     215        1790 :     return strlen(*data);
     216             : }
     217             : 
     218             : size_t ASN1CALL
     219         738 : der_length_printable_string (const heim_printable_string *data)
     220             : {
     221         738 :     return data->length;
     222             : }
     223             : 
     224             : size_t ASN1CALL
     225         384 : der_length_ia5_string (const heim_ia5_string *data)
     226             : {
     227         384 :     return data->length;
     228             : }
     229             : 
     230             : size_t ASN1CALL
     231           0 : der_length_bmp_string (const heim_bmp_string *data)
     232             : {
     233           0 :     return data->length * 2;
     234             : }
     235             : 
     236             : size_t ASN1CALL
     237           0 : der_length_universal_string (const heim_universal_string *data)
     238             : {
     239           0 :     return data->length * 4;
     240             : }
     241             : 
     242             : size_t ASN1CALL
     243           0 : der_length_visible_string (const heim_visible_string *data)
     244             : {
     245           0 :     return strlen(*data);
     246             : }
     247             : 
     248             : size_t ASN1CALL
     249     2132716 : der_length_octet_string (const heim_octet_string *k)
     250             : {
     251     2132716 :     return k->length;
     252             : }
     253             : 
     254             : size_t ASN1CALL
     255         830 : der_length_heim_integer (const heim_integer *k)
     256             : {
     257         830 :     if (k->length == 0)
     258         124 :         return 1;
     259         706 :     if (k->negative && k->length == 1 && ((unsigned char *)k->data)[0] == 1)
     260           0 :         return 1;
     261         706 :     else if (k->negative)
     262          59 :         return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
     263             :     else
     264         647 :         return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
     265             : }
     266             : 
     267             : size_t ASN1CALL
     268        7716 : der_length_oid (const heim_oid *k)
     269             : {
     270        7716 :     return len_oid (k);
     271             : }
     272             : 
     273             : size_t ASN1CALL
     274     1267166 : der_length_generalized_time (const time_t *t)
     275             : {
     276       43707 :     heim_octet_string k;
     277       43707 :     size_t ret;
     278             : 
     279     1267166 :     _heim_time2generalizedtime (*t, &k, 1);
     280     1267166 :     ret = k.length;
     281     1267166 :     free(k.data);
     282     1267166 :     return ret;
     283             : }
     284             : 
     285             : size_t ASN1CALL
     286         130 : der_length_utctime (const time_t *t)
     287             : {
     288           0 :     heim_octet_string k;
     289           0 :     size_t ret;
     290             : 
     291         130 :     _heim_time2generalizedtime (*t, &k, 0);
     292         130 :     ret = k.length;
     293         130 :     free(k.data);
     294         130 :     return ret;
     295             : }
     296             : 
     297             : size_t ASN1CALL
     298        1671 : der_length_boolean (const int *k)
     299             : {
     300        1671 :     return 1;
     301             : }
     302             : 
     303             : size_t ASN1CALL
     304         278 : der_length_bit_string (const heim_bit_string *k)
     305             : {
     306         278 :     return (k->length + 7) / 8 + 1;
     307             : }

Generated by: LCOV version 1.14