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