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