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