Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 6 | #include <INTEGER.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <OBJECT_IDENTIFIER.h> |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 8 | #include <OCTET_STRING.h> |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 9 | #include <limits.h> /* for CHAR_BIT */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | |
| 12 | /* |
| 13 | * OBJECT IDENTIFIER basic type description. |
| 14 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 15 | static const ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | (ASN_TAG_CLASS_UNIVERSAL | (6 << 2)) |
| 17 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 18 | asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | "OBJECT IDENTIFIER", |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 20 | "OBJECT_IDENTIFIER", |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 21 | ASN__PRIMITIVE_TYPE_free, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 22 | OBJECT_IDENTIFIER_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 23 | OCTET_STRING_compare, /* Implemented in terms of a string comparison */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | OBJECT_IDENTIFIER_constraint, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 25 | ber_decode_primitive, |
| 26 | der_encode_primitive, |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 27 | OBJECT_IDENTIFIER_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 28 | OBJECT_IDENTIFIER_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 29 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 30 | 0, |
| 31 | 0, |
| 32 | #else |
| 33 | 0, |
| 34 | 0, |
| 35 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 36 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 37 | 0, |
| 38 | 0, |
| 39 | #else |
| 40 | OCTET_STRING_decode_uper, |
| 41 | OCTET_STRING_encode_uper, |
| 42 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 44 | asn_DEF_OBJECT_IDENTIFIER_tags, |
| 45 | sizeof(asn_DEF_OBJECT_IDENTIFIER_tags) |
| 46 | / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]), |
| 47 | asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */ |
| 48 | sizeof(asn_DEF_OBJECT_IDENTIFIER_tags) |
| 49 | / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]), |
Lev Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 50 | 0, /* No OER visible constraints */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 51 | 0, /* No PER visible constraints */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 52 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 53 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 57 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 58 | OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 59 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 60 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 61 | |
| 62 | if(st && st->buf) { |
| 63 | if(st->size < 1) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 64 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 65 | "%s: at least one numerical value " |
| 66 | "expected (%s:%d)", |
| 67 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 68 | return -1; |
| 69 | } |
| 70 | } else { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 71 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 72 | "%s: value not given (%s:%d)", |
| 73 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 80 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | int |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 82 | OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) { |
Lev Walkin | 50b0f6c | 2006-10-16 12:01:36 +0000 | [diff] [blame] | 83 | unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */ |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 84 | const uint8_t *arcend = arcbuf + arclen; /* End of arc */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 85 | unsigned int cache = 0; /* No more than 14 significant bits */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 86 | unsigned char *rvbuf = (unsigned char *)rvbufp; |
| 87 | unsigned char *rvstart = rvbuf; /* Original start of the value buffer */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 88 | int inc; /* Return value growth direction */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 89 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 90 | rvsize *= CHAR_BIT; /* bytes to bits */ |
| 91 | arclen *= 7; /* bytes to bits */ |
| 92 | |
| 93 | /* |
| 94 | * The arc has the number of bits |
| 95 | * cannot be represented using supplied return value type. |
| 96 | */ |
| 97 | if(arclen > rvsize) { |
| 98 | if(arclen > (rvsize + CHAR_BIT)) { |
| 99 | errno = ERANGE; /* Overflow */ |
| 100 | return -1; |
| 101 | } else { |
| 102 | /* |
| 103 | * Even if the number of bits in the arc representation |
| 104 | * is higher than the width of supplied * return value |
| 105 | * type, there is still possible to fit it when there |
| 106 | * are few unused high bits in the arc value |
| 107 | * representaion. |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 108 | * |
| 109 | * Moreover, there is a possibility that the |
| 110 | * number could actually fit the arc space, given |
| 111 | * that add is negative, but we don't handle |
| 112 | * such "temporary lack of precision" situation here. |
| 113 | * May be considered as a bug. |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 114 | */ |
| 115 | uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f; |
| 116 | if((*arcbuf & mask)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | errno = ERANGE; /* Overflow */ |
| 118 | return -1; |
| 119 | } |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 120 | /* Fool the routine computing unused bits */ |
| 121 | arclen -= 7; |
| 122 | cache = *arcbuf & 0x7f; |
| 123 | arcbuf++; |
| 124 | } |
| 125 | } |
| 126 | |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 127 | /* Faster path for common size */ |
| 128 | if(rvsize == (CHAR_BIT * sizeof(unsigned long))) { |
| 129 | unsigned long accum; |
| 130 | /* Gather all bits into the accumulator */ |
| 131 | for(accum = cache; arcbuf < arcend; arcbuf++) |
| 132 | accum = (accum << 7) | (*arcbuf & ~0x80); |
| 133 | if(accum < (unsigned)-add) { |
| 134 | errno = ERANGE; /* Overflow */ |
| 135 | return -1; |
| 136 | } |
Lev Walkin | 70732a2 | 2013-12-07 10:55:14 -0800 | [diff] [blame] | 137 | *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 141 | #ifndef WORDS_BIGENDIAN |
| 142 | if(*(unsigned char *)&LE) { /* Little endian (x86) */ |
| 143 | /* "Convert" to big endian */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 144 | rvbuf += rvsize / CHAR_BIT - 1; |
| 145 | rvstart--; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 146 | inc = -1; /* Descending */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 147 | } else |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 148 | #endif /* !WORDS_BIGENDIAN */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 149 | inc = +1; /* Big endian is known [at compile time] */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 150 | |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 151 | { |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 152 | int bits; /* typically no more than 3-4 bits */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 153 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 154 | /* Clear the high unused bits */ |
| 155 | for(bits = rvsize - arclen; |
| 156 | bits > CHAR_BIT; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 157 | rvbuf += inc, bits -= CHAR_BIT) |
| 158 | *rvbuf = 0; |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 159 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 160 | /* Fill the body of a value */ |
| 161 | for(; arcbuf < arcend; arcbuf++) { |
| 162 | cache = (cache << 7) | (*arcbuf & 0x7f); |
| 163 | bits += 7; |
| 164 | if(bits >= CHAR_BIT) { |
| 165 | bits -= CHAR_BIT; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 166 | *rvbuf = (cache >> bits); |
| 167 | rvbuf += inc; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | if(bits) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 171 | *rvbuf = cache; |
| 172 | rvbuf += inc; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | if(add) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 177 | for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) { |
| 178 | int v = add + *rvbuf; |
Lev Walkin | 74757c8 | 2016-07-02 23:02:59 -0700 | [diff] [blame] | 179 | if(v & ((unsigned)~0 << CHAR_BIT)) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 180 | *rvbuf = (unsigned char)(v + (1 << CHAR_BIT)); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 181 | add = -1; |
| 182 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 183 | *rvbuf = v; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if(rvbuf == rvstart) { |
| 188 | /* No space to carry over */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | errno = ERANGE; /* Overflow */ |
| 190 | return -1; |
| 191 | } |
| 192 | } |
| 193 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 197 | ssize_t |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 198 | OBJECT_IDENTIFIER__dump_arc(const uint8_t *arcbuf, int arclen, int add, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 199 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 200 | char scratch[64]; /* Conservative estimate */ |
| 201 | unsigned long accum; /* Bits accumulator */ |
| 202 | char *p; /* Position in the scratch buffer */ |
| 203 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 204 | if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add, |
| 205 | &accum, sizeof(accum))) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | return -1; |
| 207 | |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 208 | if(accum) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 209 | ssize_t len; |
| 210 | |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 211 | /* Fill the scratch buffer in reverse. */ |
| 212 | p = scratch + sizeof(scratch); |
| 213 | for(; accum; accum /= 10) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 214 | *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 215 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 216 | len = sizeof(scratch) - (p - scratch); |
| 217 | if(cb(p, len, app_key) < 0) |
| 218 | return -1; |
| 219 | return len; |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 220 | } else { |
| 221 | *scratch = 0x30; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 222 | if(cb(scratch, 1, app_key) < 0) |
| 223 | return -1; |
| 224 | return 1; |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 225 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 229 | OBJECT_IDENTIFIER_print_arc(const uint8_t *arcbuf, int arclen, int add, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 230 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 231 | |
| 232 | if(OBJECT_IDENTIFIER__dump_arc(arcbuf, arclen, add, cb, app_key) < 0) |
| 233 | return -1; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static ssize_t |
| 239 | OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st, asn_app_consume_bytes_f *cb, void *app_key) { |
| 240 | ssize_t wrote_len = 0; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 241 | size_t startn; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 242 | int add = 0; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 243 | size_t i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 245 | for(i = 0, startn = 0; i < st->size; i++) { |
| 246 | uint8_t b = st->buf[i]; |
| 247 | if((b & 0x80)) /* Continuation expected */ |
| 248 | continue; |
| 249 | |
| 250 | if(startn == 0) { |
| 251 | /* |
| 252 | * First two arcs are encoded through the backdoor. |
| 253 | */ |
| 254 | if(i) { |
| 255 | add = -80; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 256 | if(cb("2", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 257 | } else if(b <= 39) { |
| 258 | add = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 259 | if(cb("0", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 260 | } else if(b < 79) { |
| 261 | add = -40; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 262 | if(cb("1", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 263 | } else { |
| 264 | add = -80; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 265 | if(cb("2", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 266 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 267 | wrote_len += 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 270 | if(cb(".", 1, app_key) < 0) /* Separate arcs */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 271 | return -1; |
| 272 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 273 | add = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn], |
| 274 | i - startn + 1, add, cb, app_key); |
| 275 | if(add < 0) return -1; |
| 276 | wrote_len += 1 + add; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 277 | startn = i + 1; |
| 278 | add = 0; |
| 279 | } |
| 280 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 281 | return wrote_len; |
| 282 | } |
| 283 | |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 284 | static enum xer_pbd_rval |
| 285 | OBJECT_IDENTIFIER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 286 | OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr; |
Lev Walkin | 2c34aad | 2005-03-10 11:50:24 +0000 | [diff] [blame] | 287 | const char *chunk_end = (const char *)chunk_buf + chunk_size; |
| 288 | const char *endptr; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 289 | long s_arcs[10]; |
| 290 | long *arcs = s_arcs; |
| 291 | int arcs_count; |
| 292 | int ret; |
| 293 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 294 | (void)td; |
| 295 | |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 296 | arcs_count = OBJECT_IDENTIFIER_parse_arcs( |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 297 | (const char *)chunk_buf, chunk_size, arcs, |
| 298 | sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr); |
Lev Walkin | aed43c8 | 2012-09-04 14:56:27 -0700 | [diff] [blame] | 299 | if(arcs_count < 0) { |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 300 | /* Expecting more than zero arcs */ |
| 301 | return XPBD_BROKEN_ENCODING; |
Lev Walkin | aed43c8 | 2012-09-04 14:56:27 -0700 | [diff] [blame] | 302 | } else if(arcs_count == 0) { |
| 303 | return XPBD_NOT_BODY_IGNORE; |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 304 | } |
Lev Walkin | aed43c8 | 2012-09-04 14:56:27 -0700 | [diff] [blame] | 305 | assert(endptr == chunk_end); |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 306 | |
| 307 | if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 308 | arcs = (long *)MALLOC(arcs_count * sizeof(long)); |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 309 | if(!arcs) return XPBD_SYSTEM_FAILURE; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 310 | ret = OBJECT_IDENTIFIER_parse_arcs( |
| 311 | (const char *)chunk_buf, chunk_size, |
| 312 | arcs, arcs_count, &endptr); |
| 313 | if(ret != arcs_count) |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 314 | return XPBD_SYSTEM_FAILURE; /* assert?.. */ |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Convert arcs into BER representation. |
| 319 | */ |
| 320 | ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, sizeof(*arcs), arcs_count); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 321 | if(arcs != s_arcs) FREEMEM(arcs); |
| 322 | |
Lev Walkin | 2c34aad | 2005-03-10 11:50:24 +0000 | [diff] [blame] | 323 | return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | asn_dec_rval_t |
| 327 | OBJECT_IDENTIFIER_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 328 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 329 | const void *buf_ptr, size_t size) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 330 | |
| 331 | return xer_decode_primitive(opt_codec_ctx, td, |
| 332 | sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname, |
| 333 | buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode); |
| 334 | } |
| 335 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 336 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 337 | OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 338 | int ilevel, enum xer_encoder_flags_e flags, |
| 339 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 340 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 341 | asn_enc_rval_t er; |
| 342 | |
| 343 | (void)ilevel; |
| 344 | (void)flags; |
| 345 | |
| 346 | if(!st || !st->buf) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 347 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 348 | |
| 349 | er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 350 | if(er.encoded < 0) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 351 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 352 | ASN__ENCODED_OK(er); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 356 | OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 357 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
| 358 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 359 | |
| 360 | (void)td; /* Unused argument */ |
| 361 | (void)ilevel; /* Unused argument */ |
| 362 | |
| 363 | if(!st || !st->buf) |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 364 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 365 | |
| 366 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 367 | if(cb("{ ", 2, app_key) < 0) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 368 | return -1; |
| 369 | |
| 370 | if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) |
| 371 | return -1; |
| 372 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 373 | return (cb(" }", 2, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | int |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 377 | OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, void *arcs, |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 378 | unsigned int arc_type_size, unsigned int arc_slots) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 379 | void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 380 | int num_arcs = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 381 | int startn = 0; |
| 382 | int add = 0; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 383 | size_t i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 384 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 385 | if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 386 | errno = EINVAL; |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | for(i = 0; i < oid->size; i++) { |
| 391 | uint8_t b = oid->buf[i]; |
| 392 | if((b & 0x80)) /* Continuation expected */ |
| 393 | continue; |
| 394 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 395 | if(num_arcs == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 396 | /* |
| 397 | * First two arcs are encoded through the backdoor. |
| 398 | */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 399 | unsigned LE = 1; /* Little endian */ |
| 400 | int first_arc; |
| 401 | num_arcs++; |
| 402 | if(!arc_slots) { num_arcs++; continue; } |
| 403 | |
| 404 | if(i) first_arc = 2; |
| 405 | else if(b <= 39) first_arc = 0; |
| 406 | else if(b < 79) first_arc = 1; |
| 407 | else first_arc = 2; |
| 408 | |
| 409 | add = -40 * first_arc; |
| 410 | memset(arcs, 0, arc_type_size); |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 411 | *(unsigned char *)((char *)arcs |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 412 | + ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 413 | = first_arc; |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 414 | arcs = ((char *)arcs) + arc_type_size; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 417 | /* Decode, if has space */ |
| 418 | if(arcs < arcs_end) { |
| 419 | if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn], |
| 420 | i - startn + 1, add, |
| 421 | arcs, arc_type_size)) |
| 422 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 423 | startn = i + 1; |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 424 | arcs = ((char *)arcs) + arc_type_size; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 425 | add = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 426 | } |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 427 | num_arcs++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 430 | return num_arcs; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * Save the single value as an object identifier arc. |
| 436 | */ |
Lev Walkin | 91f5cd0 | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 437 | int |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 438 | OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, const void *arcval, unsigned int arcval_size, int prepared_order) { |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 439 | /* |
| 440 | * The following conditions must hold: |
| 441 | * assert(arcval); |
| 442 | * assert(arcval_size > 0); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 443 | * assert(arcval_size <= 16); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 444 | * assert(arcbuf); |
| 445 | */ |
| 446 | #ifdef WORDS_BIGENDIAN |
| 447 | const unsigned isLittleEndian = 0; |
| 448 | #else |
| 449 | unsigned LE = 1; |
| 450 | unsigned isLittleEndian = *(char *)&LE; |
| 451 | #endif |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 452 | const uint8_t *tend, *tp; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 453 | unsigned int cache; |
| 454 | uint8_t *bp = arcbuf; |
| 455 | int bits; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 456 | uint8_t buffer[16]; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 457 | |
| 458 | if(isLittleEndian && !prepared_order) { |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 459 | const uint8_t *a = (const unsigned char *)arcval + arcval_size - 1; |
| 460 | const uint8_t *aend = (const uint8_t *)arcval; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 461 | uint8_t *msb = buffer + arcval_size - 1; |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 462 | uint8_t *tb; |
| 463 | for(tb = buffer; a >= aend; tb++, a--) |
| 464 | if((*tb = *a) && (tb < msb)) |
| 465 | msb = tb; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 466 | tend = &buffer[arcval_size]; |
| 467 | tp = msb; /* Most significant non-zero byte */ |
| 468 | } else { |
| 469 | /* Look for most significant non-zero byte */ |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 470 | tend = (const unsigned char *)arcval + arcval_size; |
| 471 | for(tp = (const uint8_t *)arcval; tp < tend - 1; tp++) |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 472 | if(*tp) break; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Split the value in 7-bits chunks. |
| 477 | */ |
| 478 | bits = ((tend - tp) * CHAR_BIT) % 7; |
| 479 | if(bits) { |
| 480 | cache = *tp >> (CHAR_BIT - bits); |
| 481 | if(cache) { |
| 482 | *bp++ = cache | 0x80; |
| 483 | cache = *tp++; |
| 484 | bits = CHAR_BIT - bits; |
| 485 | } else { |
| 486 | bits = -bits; |
| 487 | } |
| 488 | } else { |
| 489 | cache = 0; |
| 490 | } |
| 491 | for(; tp < tend; tp++) { |
| 492 | cache = (cache << CHAR_BIT) + *tp; |
| 493 | bits += CHAR_BIT; |
| 494 | while(bits >= 7) { |
| 495 | bits -= 7; |
| 496 | *bp++ = 0x80 | (cache >> bits); |
| 497 | } |
| 498 | } |
| 499 | if(bits) *bp++ = cache; |
| 500 | bp[-1] &= 0x7f; /* Clear the last bit */ |
| 501 | |
| 502 | return bp - arcbuf; |
| 503 | } |
| 504 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 505 | int |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 506 | OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, const void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 507 | uint8_t *buf; |
| 508 | uint8_t *bp; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 509 | unsigned LE = 1; /* Little endian (x86) */ |
| 510 | unsigned isLittleEndian = *((char *)&LE); |
| 511 | unsigned int arc0; |
| 512 | unsigned int arc1; |
| 513 | unsigned size; |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 514 | unsigned i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 515 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 516 | if(!oid || !arcs || arc_type_size < 1 |
| 517 | || arc_type_size > 16 |
| 518 | || arc_slots < 2) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 519 | errno = EINVAL; |
| 520 | return -1; |
| 521 | } |
| 522 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 523 | switch(arc_type_size) { |
| 524 | case sizeof(char): |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 525 | arc0 = ((const unsigned char *)arcs)[0]; |
| 526 | arc1 = ((const unsigned char *)arcs)[1]; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 527 | break; |
| 528 | case sizeof(short): |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 529 | arc0 = ((const unsigned short *)arcs)[0]; |
| 530 | arc1 = ((const unsigned short *)arcs)[1]; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 531 | break; |
| 532 | case sizeof(int): |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 533 | arc0 = ((const unsigned int *)arcs)[0]; |
| 534 | arc1 = ((const unsigned int *)arcs)[1]; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 535 | break; |
| 536 | default: |
| 537 | arc1 = arc0 = 0; |
| 538 | if(isLittleEndian) { /* Little endian (x86) */ |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 539 | const unsigned char *ps, *pe; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 540 | /* If more significant bytes are present, |
| 541 | * make them > 255 quick */ |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 542 | for(ps = (const unsigned char *)arcs + 1, pe = ps+arc_type_size; |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 543 | ps < pe; ps++) |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 544 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 545 | arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT; |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 546 | arc0 = *((const unsigned char *)arcs + 0); |
| 547 | arc1 = *((const unsigned char *)arcs + arc_type_size); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 548 | } else { |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 549 | const unsigned char *ps, *pe; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 550 | /* If more significant bytes are present, |
| 551 | * make them > 255 quick */ |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 552 | for(ps = (const unsigned char *)arcs, pe = ps+arc_type_size - 1; ps < pe; ps++) |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 553 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 554 | arc0 = *((const unsigned char *)arcs + arc_type_size - 1); |
| 555 | arc1 = *((const unsigned char *)arcs +(arc_type_size<< 1)-1); |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * The previous chapter left us with the first and the second arcs. |
| 561 | * The values are not precise (that is, they are valid only if |
| 562 | * they're less than 255), but OK for the purposes of making |
| 563 | * the sanity test below. |
| 564 | */ |
| 565 | if(arc0 <= 1) { |
| 566 | if(arc1 >= 39) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 567 | /* 8.19.4: At most 39 subsequent values (including 0) */ |
| 568 | errno = ERANGE; |
| 569 | return -1; |
| 570 | } |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 571 | } else if(arc0 > 2) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 572 | /* 8.19.4: Only three values are allocated from the root node */ |
| 573 | errno = ERANGE; |
| 574 | return -1; |
| 575 | } |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 576 | /* |
| 577 | * After above tests it is known that the value of arc0 is completely |
| 578 | * trustworthy (0..2). However, the arc1's value is still meaningless. |
| 579 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * Roughly estimate the maximum size necessary to encode these arcs. |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 583 | * This estimation implicitly takes in account the following facts, |
| 584 | * that cancel each other: |
| 585 | * * the first two arcs are encoded in a single value. |
| 586 | * * the first value may require more space (+1 byte) |
| 587 | * * the value of the first arc which is in range (0..2) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 588 | */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 589 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 590 | bp = buf = (uint8_t *)MALLOC(size + 1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 591 | if(!buf) { |
| 592 | /* ENOMEM */ |
| 593 | return -1; |
| 594 | } |
| 595 | |
| 596 | /* |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 597 | * Encode the first two arcs. |
| 598 | * These require special treatment. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 599 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 600 | { |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 601 | uint8_t *tp; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 602 | uint8_t first_value[1 + 16]; /* of two arcs */ |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 603 | uint8_t *fv = first_value; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 604 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 605 | /* |
| 606 | * Simulate first_value = arc0 * 40 + arc1; |
| 607 | */ |
| 608 | /* Copy the second (1'st) arcs[1] into the first_value */ |
| 609 | *fv++ = 0; |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 610 | arcs = ((const char *)arcs) + arc_type_size; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 611 | if(isLittleEndian) { |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 612 | const uint8_t *aend = (const unsigned char *)arcs - 1; |
| 613 | const uint8_t *a1 = (const unsigned char *)arcs + arc_type_size - 1; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 614 | for(; a1 > aend; fv++, a1--) *fv = *a1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 615 | } else { |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 616 | const uint8_t *a1 = (const uint8_t *)arcs; |
| 617 | const uint8_t *aend = a1 + arc_type_size; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 618 | for(; a1 < aend; fv++, a1++) *fv = *a1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 619 | } |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 620 | /* Increase the first_value by arc0 */ |
| 621 | arc0 *= 40; /* (0..80) */ |
| 622 | for(tp = first_value + arc_type_size; tp >= first_value; tp--) { |
| 623 | unsigned int v = *tp; |
| 624 | v += arc0; |
| 625 | *tp = v; |
| 626 | if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT; |
| 627 | else break; |
| 628 | } |
| 629 | |
| 630 | assert(tp >= first_value); |
| 631 | |
| 632 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value, |
| 633 | fv - first_value, 1); |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Save the rest of arcs. |
| 638 | */ |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 639 | for(arcs = ((const char *)arcs) + arc_type_size, i = 2; |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 640 | i < arc_slots; |
Lev Walkin | b1a1555 | 2005-12-22 21:39:44 +0000 | [diff] [blame] | 641 | i++, arcs = ((const char *)arcs) + arc_type_size) { |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 642 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 643 | arcs, arc_type_size, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Lev Walkin | 34b2a93 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 646 | assert((unsigned)(bp - buf) <= size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * Replace buffer. |
| 650 | */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 651 | oid->size = bp - buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 652 | bp = oid->buf; |
| 653 | oid->buf = buf; |
| 654 | if(bp) FREEMEM(bp); |
| 655 | |
| 656 | return 0; |
| 657 | } |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 658 | |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 659 | |
| 660 | int |
| 661 | OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length, |
Lev Walkin | 093ba2e | 2005-04-04 21:10:06 +0000 | [diff] [blame] | 662 | long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 663 | unsigned int arcs_count = 0; |
| 664 | const char *oid_end; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 665 | enum { |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 666 | ST_LEADSPACE, |
| 667 | ST_TAILSPACE, |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 668 | ST_AFTERVALUE, /* Next character ought to be '.' or a space */ |
Lev Walkin | 104af19 | 2016-01-24 22:13:27 -0800 | [diff] [blame] | 669 | ST_WAITDIGITS /* Next character is expected to be a digit */ |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 670 | } state = ST_LEADSPACE; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 671 | |
| 672 | if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) { |
Lev Walkin | 093ba2e | 2005-04-04 21:10:06 +0000 | [diff] [blame] | 673 | if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 674 | errno = EINVAL; |
| 675 | return -1; |
| 676 | } |
| 677 | |
| 678 | if(oid_txt_length == -1) |
| 679 | oid_txt_length = strlen(oid_text); |
| 680 | |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 681 | #define _OID_CAPTURE_ARC(oid_text, oid_end) do { \ |
| 682 | const char *endp = oid_end; \ |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 683 | long value; \ |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 684 | switch(asn_strtol_lim(oid_text, &endp, &value)) { \ |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 685 | case ASN_STRTOX_EXTRA_DATA: \ |
| 686 | case ASN_STRTOX_OK: \ |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 687 | if(arcs_count < arcs_slots) \ |
| 688 | arcs[arcs_count] = value; \ |
| 689 | arcs_count++; \ |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 690 | oid_text = endp - 1; \ |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 691 | break; \ |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 692 | case ASN_STRTOX_ERROR_RANGE: \ |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 693 | if(opt_oid_text_end) \ |
| 694 | *opt_oid_text_end = oid_text; \ |
| 695 | errno = ERANGE; \ |
| 696 | return -1; \ |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 697 | case ASN_STRTOX_ERROR_INVAL: \ |
| 698 | case ASN_STRTOX_EXPECT_MORE: \ |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 699 | if(opt_oid_text_end) \ |
| 700 | *opt_oid_text_end = oid_text; \ |
| 701 | errno = EINVAL; \ |
| 702 | return -1; \ |
| 703 | } \ |
| 704 | } while(0) |
| 705 | |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 706 | for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) { |
| 707 | switch(*oid_text) { |
| 708 | case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */ |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 709 | switch(state) { |
| 710 | case ST_LEADSPACE: |
| 711 | case ST_TAILSPACE: |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 712 | continue; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 713 | case ST_AFTERVALUE: |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 714 | state = ST_TAILSPACE; |
| 715 | continue; |
| 716 | case ST_WAITDIGITS: |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 717 | break; /* Digits expected after ".", got whitespace */ |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 718 | } |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 719 | break; |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 720 | case 0x2e: /* '.' */ |
| 721 | switch(state) { |
| 722 | case ST_LEADSPACE: |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 723 | case ST_TAILSPACE: |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 724 | case ST_WAITDIGITS: |
| 725 | if(opt_oid_text_end) |
| 726 | *opt_oid_text_end = oid_text; |
| 727 | errno = EINVAL; /* Broken OID */ |
| 728 | return -1; |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 729 | break; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 730 | case ST_AFTERVALUE: |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 731 | state = ST_WAITDIGITS; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 732 | continue; |
| 733 | } |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 734 | break; |
| 735 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 736 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 737 | switch(state) { |
| 738 | case ST_TAILSPACE: |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 739 | case ST_AFTERVALUE: |
| 740 | if(opt_oid_text_end) |
| 741 | *opt_oid_text_end = oid_text; |
| 742 | errno = EINVAL; /* "1. 1" => broken OID */ |
| 743 | return -1; |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 744 | case ST_LEADSPACE: |
| 745 | case ST_WAITDIGITS: |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 746 | _OID_CAPTURE_ARC(oid_text, oid_end); |
| 747 | state = ST_AFTERVALUE; |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 748 | continue; |
| 749 | } |
| 750 | break; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 751 | default: |
| 752 | /* Unexpected symbols */ |
| 753 | state = ST_WAITDIGITS; |
| 754 | break; |
| 755 | } /* switch() */ |
| 756 | break; |
| 757 | } /* for() */ |
| 758 | |
| 759 | |
Lev Walkin | 093ba2e | 2005-04-04 21:10:06 +0000 | [diff] [blame] | 760 | if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 761 | |
| 762 | /* Finalize last arc */ |
| 763 | switch(state) { |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 764 | case ST_LEADSPACE: |
Lev Walkin | aed43c8 | 2012-09-04 14:56:27 -0700 | [diff] [blame] | 765 | return 0; /* No OID found in input data */ |
Lev Walkin | aed43c8 | 2012-09-04 14:56:27 -0700 | [diff] [blame] | 766 | case ST_WAITDIGITS: |
| 767 | errno = EINVAL; /* Broken OID */ |
| 768 | return -1; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 769 | case ST_AFTERVALUE: |
Lev Walkin | f592711 | 2012-09-03 00:48:45 -0700 | [diff] [blame] | 770 | case ST_TAILSPACE: |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 771 | return arcs_count; |
| 772 | } |
Lev Walkin | aed43c8 | 2012-09-04 14:56:27 -0700 | [diff] [blame] | 773 | |
| 774 | errno = EINVAL; /* Broken OID */ |
| 775 | return -1; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | |