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