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