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