der_length.c revision 178826
1/* 2 * Copyright (c) 1997-2005 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include "der_locl.h" 35 36RCSID("$Id: der_length.c 19539 2006-12-28 17:15:05Z lha $"); 37 38size_t 39_heim_len_unsigned (unsigned val) 40{ 41 size_t ret = 0; 42 int last_val_gt_128; 43 44 do { 45 ++ret; 46 last_val_gt_128 = (val >= 128); 47 val /= 256; 48 } while (val); 49 50 if(last_val_gt_128) 51 ret++; 52 53 return ret; 54} 55 56size_t 57_heim_len_int (int val) 58{ 59 unsigned char q; 60 size_t ret = 0; 61 62 if (val >= 0) { 63 do { 64 q = val % 256; 65 ret++; 66 val /= 256; 67 } while(val); 68 if(q >= 128) 69 ret++; 70 } else { 71 val = ~val; 72 do { 73 q = ~(val % 256); 74 ret++; 75 val /= 256; 76 } while(val); 77 if(q < 128) 78 ret++; 79 } 80 return ret; 81} 82 83static size_t 84len_oid (const heim_oid *oid) 85{ 86 size_t ret = 1; 87 int n; 88 89 for (n = 2; n < oid->length; ++n) { 90 unsigned u = oid->components[n]; 91 92 do { 93 ++ret; 94 u /= 128; 95 } while(u > 0); 96 } 97 return ret; 98} 99 100size_t 101der_length_len (size_t len) 102{ 103 if (len < 128) 104 return 1; 105 else { 106 int ret = 0; 107 do { 108 ++ret; 109 len /= 256; 110 } while (len); 111 return ret + 1; 112 } 113} 114 115size_t 116der_length_integer (const int *data) 117{ 118 return _heim_len_int (*data); 119} 120 121size_t 122der_length_unsigned (const unsigned *data) 123{ 124 return _heim_len_unsigned(*data); 125} 126 127size_t 128der_length_enumerated (const unsigned *data) 129{ 130 return _heim_len_int (*data); 131} 132 133size_t 134der_length_general_string (const heim_general_string *data) 135{ 136 return strlen(*data); 137} 138 139size_t 140der_length_utf8string (const heim_utf8_string *data) 141{ 142 return strlen(*data); 143} 144 145size_t 146der_length_printable_string (const heim_printable_string *data) 147{ 148 return strlen(*data); 149} 150 151size_t 152der_length_ia5_string (const heim_ia5_string *data) 153{ 154 return strlen(*data); 155} 156 157size_t 158der_length_bmp_string (const heim_bmp_string *data) 159{ 160 return data->length * 2; 161} 162 163size_t 164der_length_universal_string (const heim_universal_string *data) 165{ 166 return data->length * 4; 167} 168 169size_t 170der_length_visible_string (const heim_visible_string *data) 171{ 172 return strlen(*data); 173} 174 175size_t 176der_length_octet_string (const heim_octet_string *k) 177{ 178 return k->length; 179} 180 181size_t 182der_length_heim_integer (const heim_integer *k) 183{ 184 if (k->length == 0) 185 return 1; 186 if (k->negative) 187 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1); 188 else 189 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0); 190} 191 192size_t 193der_length_oid (const heim_oid *k) 194{ 195 return len_oid (k); 196} 197 198size_t 199der_length_generalized_time (const time_t *t) 200{ 201 heim_octet_string k; 202 size_t ret; 203 204 _heim_time2generalizedtime (*t, &k, 1); 205 ret = k.length; 206 free(k.data); 207 return ret; 208} 209 210size_t 211der_length_utctime (const time_t *t) 212{ 213 heim_octet_string k; 214 size_t ret; 215 216 _heim_time2generalizedtime (*t, &k, 0); 217 ret = k.length; 218 free(k.data); 219 return ret; 220} 221 222size_t 223der_length_boolean (const int *k) 224{ 225 return 1; 226} 227 228size_t 229der_length_bit_string (const heim_bit_string *k) 230{ 231 return (k->length + 7) / 8 + 1; 232} 233