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 | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <OBJECT_IDENTIFIER.h> |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 7 | #include <limits.h> /* for CHAR_BIT */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | |
| 10 | /* |
| 11 | * OBJECT IDENTIFIER basic type description. |
| 12 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 13 | static ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | (ASN_TAG_CLASS_UNIVERSAL | (6 << 2)) |
| 15 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 16 | asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | "OBJECT IDENTIFIER", |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 18 | "OBJECT_IDENTIFIER", |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 19 | ASN__PRIMITIVE_TYPE_free, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 20 | OBJECT_IDENTIFIER_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | OBJECT_IDENTIFIER_constraint, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 22 | ber_decode_primitive, |
| 23 | der_encode_primitive, |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 24 | OBJECT_IDENTIFIER_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 25 | OBJECT_IDENTIFIER_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 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]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 33 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 34 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 39 | OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 41 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | |
| 43 | if(st && st->buf) { |
| 44 | if(st->size < 1) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 45 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 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__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | } else { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 52 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 53 | "%s: value not given (%s:%d)", |
| 54 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 61 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | int |
Lev Walkin | 8e8078a | 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) { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 64 | unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 65 | uint8_t *arcend = arcbuf + arclen; /* End of arc */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 66 | unsigned int cache = 0; /* No more than 14 significant bits */ |
Lev Walkin | 8e8078a | 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 */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 69 | int inc; /* Return value growth direction */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | |
Lev Walkin | 29a044b | 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. |
Lev Walkin | 0787ff0 | 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. |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 95 | */ |
| 96 | uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f; |
| 97 | if((*arcbuf & mask)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | errno = ERANGE; /* Overflow */ |
| 99 | return -1; |
| 100 | } |
Lev Walkin | 29a044b | 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 | |
Lev Walkin | c4c6196 | 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 | } |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 118 | *(unsigned long *)rvbuf = accum + add; /* alignment OK! */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Lev Walkin | 29a044b | 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 */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 125 | rvbuf += rvsize / CHAR_BIT - 1; |
| 126 | rvstart--; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 127 | inc = -1; /* Descending */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 128 | } else |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 129 | #endif /* !WORDS_BIGENDIAN */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 130 | inc = +1; /* Big endian is known [at compile time] */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 131 | |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 132 | { |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 133 | int bits; /* typically no more than 3-4 bits */ |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 134 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 135 | /* Clear the high unused bits */ |
| 136 | for(bits = rvsize - arclen; |
| 137 | bits > CHAR_BIT; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 138 | rvbuf += inc, bits -= CHAR_BIT) |
| 139 | *rvbuf = 0; |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 140 | |
Lev Walkin | 29a044b | 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; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 147 | *rvbuf = (cache >> bits); |
| 148 | rvbuf += inc; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | if(bits) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 152 | *rvbuf = cache; |
| 153 | rvbuf += inc; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | if(add) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 158 | for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) { |
| 159 | int v = add + *rvbuf; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 160 | if(v & (-1 << CHAR_BIT)) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 161 | *rvbuf = (unsigned char)(v + (1 << CHAR_BIT)); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 162 | add = -1; |
| 163 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 164 | *rvbuf = v; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | if(rvbuf == rvstart) { |
| 169 | /* No space to carry over */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | errno = ERANGE; /* Overflow */ |
| 171 | return -1; |
| 172 | } |
| 173 | } |
| 174 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 178 | ssize_t |
| 179 | OBJECT_IDENTIFIER__dump_arc(uint8_t *arcbuf, int arclen, int add, |
Lev Walkin | f15320b | 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 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 185 | if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add, |
| 186 | &accum, sizeof(accum))) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 187 | return -1; |
| 188 | |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 189 | if(accum) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 190 | ssize_t len; |
| 191 | |
Lev Walkin | 3251b8e | 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) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 195 | *(--p) = (char)(accum % 10) + 0x30; /* Put a digit */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 196 | |
Lev Walkin | a9cc46e | 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; |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 201 | } else { |
| 202 | *scratch = 0x30; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 203 | if(cb(scratch, 1, app_key) < 0) |
| 204 | return -1; |
| 205 | return 1; |
Lev Walkin | 3251b8e | 2004-08-23 09:23:02 +0000 | [diff] [blame] | 206 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | int |
Lev Walkin | a9cc46e | 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; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 222 | int startn; |
| 223 | int add = 0; |
| 224 | int i; |
| 225 | |
Lev Walkin | f15320b | 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; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 237 | if(cb("2", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 238 | } else if(b <= 39) { |
| 239 | add = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 240 | if(cb("0", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 241 | } else if(b < 79) { |
| 242 | add = -40; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 243 | if(cb("1", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | } else { |
| 245 | add = -80; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 246 | if(cb("2", 1, app_key) < 0) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 248 | wrote_len += 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 251 | if(cb(".", 1, app_key) < 0) /* Separate arcs */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 252 | return -1; |
| 253 | |
Lev Walkin | a9cc46e | 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; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 258 | startn = i + 1; |
| 259 | add = 0; |
| 260 | } |
| 261 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 262 | return wrote_len; |
| 263 | } |
| 264 | |
Lev Walkin | 0fab1a6 | 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) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 267 | OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr; |
Lev Walkin | 2c34aad | 2005-03-10 11:50:24 +0000 | [diff] [blame] | 268 | const char *chunk_end = (const char *)chunk_buf + chunk_size; |
| 269 | const char *endptr; |
Lev Walkin | 9230225 | 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 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 275 | (void)td; |
| 276 | |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 277 | arcs_count = OBJECT_IDENTIFIER_parse_arcs( |
Lev Walkin | 0fab1a6 | 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 | } |
Lev Walkin | 2c34aad | 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 | } |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 289 | |
| 290 | if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 291 | arcs = (long *)MALLOC(arcs_count * sizeof(long)); |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 292 | if(!arcs) return XPBD_SYSTEM_FAILURE; |
Lev Walkin | 9230225 | 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) |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 297 | return XPBD_SYSTEM_FAILURE; /* assert?.. */ |
Lev Walkin | 9230225 | 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); |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 304 | if(arcs != s_arcs) FREEMEM(arcs); |
| 305 | |
Lev Walkin | 2c34aad | 2005-03-10 11:50:24 +0000 | [diff] [blame] | 306 | return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED; |
Lev Walkin | 9230225 | 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, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 312 | const void *buf_ptr, size_t size) { |
Lev Walkin | 9230225 | 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 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 319 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 320 | OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 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 | |
Lev Walkin | 4b95ab9 | 2005-11-13 09:19:11 +0000 | [diff] [blame] | 335 | er.structure_ptr = 0; |
| 336 | er.failed_type = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 337 | return er; |
| 338 | } |
| 339 | |
| 340 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 341 | OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 342 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
| 343 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 344 | |
| 345 | (void)td; /* Unused argument */ |
| 346 | (void)ilevel; /* Unused argument */ |
| 347 | |
| 348 | if(!st || !st->buf) |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 349 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 350 | |
| 351 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 352 | if(cb("{ ", 2, app_key) < 0) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 353 | return -1; |
| 354 | |
| 355 | if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) |
| 356 | return -1; |
| 357 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 358 | return (cb(" }", 2, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | int |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 362 | OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, |
| 363 | unsigned int arc_type_size, unsigned int arc_slots) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 364 | void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 365 | int num_arcs = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 366 | int startn = 0; |
| 367 | int add = 0; |
| 368 | int i; |
| 369 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 370 | if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 371 | errno = EINVAL; |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | for(i = 0; i < oid->size; i++) { |
| 376 | uint8_t b = oid->buf[i]; |
| 377 | if((b & 0x80)) /* Continuation expected */ |
| 378 | continue; |
| 379 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 380 | if(num_arcs == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 381 | /* |
| 382 | * First two arcs are encoded through the backdoor. |
| 383 | */ |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 384 | unsigned LE = 1; /* Little endian */ |
| 385 | int first_arc; |
| 386 | num_arcs++; |
| 387 | if(!arc_slots) { num_arcs++; continue; } |
| 388 | |
| 389 | if(i) first_arc = 2; |
| 390 | else if(b <= 39) first_arc = 0; |
| 391 | else if(b < 79) first_arc = 1; |
| 392 | else first_arc = 2; |
| 393 | |
| 394 | add = -40 * first_arc; |
| 395 | memset(arcs, 0, arc_type_size); |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 396 | *(unsigned char *)((char *)arcs |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 397 | + ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 398 | = first_arc; |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 399 | arcs = ((char *)arcs) + arc_type_size; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 402 | /* Decode, if has space */ |
| 403 | if(arcs < arcs_end) { |
| 404 | if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn], |
| 405 | i - startn + 1, add, |
| 406 | arcs, arc_type_size)) |
| 407 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 408 | startn = i + 1; |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 409 | arcs = ((char *)arcs) + arc_type_size; |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 410 | add = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 411 | } |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 412 | num_arcs++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Lev Walkin | 29a044b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 415 | return num_arcs; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 418 | |
| 419 | /* |
| 420 | * Save the single value as an object identifier arc. |
| 421 | */ |
Lev Walkin | 91f5cd0 | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 422 | int |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 423 | OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) { |
| 424 | /* |
| 425 | * The following conditions must hold: |
| 426 | * assert(arcval); |
| 427 | * assert(arcval_size > 0); |
| 428 | * assert(arcbuf); |
| 429 | */ |
| 430 | #ifdef WORDS_BIGENDIAN |
| 431 | const unsigned isLittleEndian = 0; |
| 432 | #else |
| 433 | unsigned LE = 1; |
| 434 | unsigned isLittleEndian = *(char *)&LE; |
| 435 | #endif |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 436 | uint8_t *tp, *tend; |
| 437 | unsigned int cache; |
| 438 | uint8_t *bp = arcbuf; |
| 439 | int bits; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 440 | #ifdef __GNUC__ |
| 441 | uint8_t buffer[arcval_size]; |
| 442 | #else |
| 443 | uint8_t *buffer = alloca(arcval_size); |
Lev Walkin | 7e0d2cb | 2004-08-11 07:41:45 +0000 | [diff] [blame] | 444 | if(!buffer) { errno = ENOMEM; return -1; } |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 445 | #endif |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 446 | |
| 447 | if(isLittleEndian && !prepared_order) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 448 | uint8_t *a = (unsigned char *)arcval + arcval_size - 1; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 449 | uint8_t *aend = (uint8_t *)arcval; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 450 | uint8_t *msb = buffer + arcval_size - 1; |
| 451 | for(tp = buffer; a >= aend; tp++, a--) |
| 452 | if((*tp = *a) && (tp < msb)) |
| 453 | msb = tp; |
| 454 | tend = &buffer[arcval_size]; |
| 455 | tp = msb; /* Most significant non-zero byte */ |
| 456 | } else { |
| 457 | /* Look for most significant non-zero byte */ |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 458 | tend = (unsigned char *)arcval + arcval_size; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 459 | for(tp = (uint8_t *)arcval; tp < tend - 1; tp++) |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 460 | if(*tp) break; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Split the value in 7-bits chunks. |
| 465 | */ |
| 466 | bits = ((tend - tp) * CHAR_BIT) % 7; |
| 467 | if(bits) { |
| 468 | cache = *tp >> (CHAR_BIT - bits); |
| 469 | if(cache) { |
| 470 | *bp++ = cache | 0x80; |
| 471 | cache = *tp++; |
| 472 | bits = CHAR_BIT - bits; |
| 473 | } else { |
| 474 | bits = -bits; |
| 475 | } |
| 476 | } else { |
| 477 | cache = 0; |
| 478 | } |
| 479 | for(; tp < tend; tp++) { |
| 480 | cache = (cache << CHAR_BIT) + *tp; |
| 481 | bits += CHAR_BIT; |
| 482 | while(bits >= 7) { |
| 483 | bits -= 7; |
| 484 | *bp++ = 0x80 | (cache >> bits); |
| 485 | } |
| 486 | } |
| 487 | if(bits) *bp++ = cache; |
| 488 | bp[-1] &= 0x7f; /* Clear the last bit */ |
| 489 | |
| 490 | return bp - arcbuf; |
| 491 | } |
| 492 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 493 | int |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 494 | OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 495 | uint8_t *buf; |
| 496 | uint8_t *bp; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 497 | unsigned LE = 1; /* Little endian (x86) */ |
| 498 | unsigned isLittleEndian = *((char *)&LE); |
| 499 | unsigned int arc0; |
| 500 | unsigned int arc1; |
| 501 | unsigned size; |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 502 | unsigned i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 503 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 504 | if(!oid || !arcs || arc_type_size < 1 || 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): |
| 511 | arc0 = ((unsigned char *)arcs)[0]; |
| 512 | arc1 = ((unsigned char *)arcs)[1]; |
| 513 | break; |
| 514 | case sizeof(short): |
| 515 | arc0 = ((unsigned short *)arcs)[0]; |
| 516 | arc1 = ((unsigned short *)arcs)[1]; |
| 517 | break; |
| 518 | case sizeof(int): |
| 519 | arc0 = ((unsigned int *)arcs)[0]; |
| 520 | arc1 = ((unsigned int *)arcs)[1]; |
| 521 | break; |
| 522 | default: |
| 523 | arc1 = arc0 = 0; |
| 524 | if(isLittleEndian) { /* Little endian (x86) */ |
| 525 | unsigned char *ps, *pe; |
| 526 | /* If more significant bytes are present, |
| 527 | * make them > 255 quick */ |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 528 | for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size; |
| 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; |
| 532 | arc0 = *((unsigned char *)arcs + 0); |
| 533 | arc1 = *((unsigned char *)arcs + arc_type_size); |
| 534 | } else { |
| 535 | unsigned char *ps, *pe; |
| 536 | /* If more significant bytes are present, |
| 537 | * make them > 255 quick */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 538 | for(ps = (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); |
| 540 | arc0 = *((unsigned char *)arcs + arc_type_size - 1); |
| 541 | arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1); |
| 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 | 90fcd44 | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 588 | #ifdef __GNUC__ |
| 589 | uint8_t first_value[1 + arc_type_size]; /* of two arcs */ |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 590 | uint8_t *fv = first_value; |
Lev Walkin | 90fcd44 | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 591 | #else |
| 592 | uint8_t *first_value = alloca(1 + arc_type_size); |
Lev Walkin | 90fcd44 | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 593 | uint8_t *fv = first_value; |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 594 | if(!first_value) { |
| 595 | errno = ENOMEM; |
| 596 | return -1; |
| 597 | } |
| 598 | #endif |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 599 | |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 600 | /* |
| 601 | * Simulate first_value = arc0 * 40 + arc1; |
| 602 | */ |
| 603 | /* Copy the second (1'st) arcs[1] into the first_value */ |
| 604 | *fv++ = 0; |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 605 | arcs = ((char *)arcs) + arc_type_size; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 606 | if(isLittleEndian) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 607 | uint8_t *aend = (unsigned char *)arcs - 1; |
| 608 | uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 609 | for(; a1 > aend; fv++, a1--) *fv = *a1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 610 | } else { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 611 | uint8_t *a1 = (uint8_t *)arcs; |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 612 | uint8_t *aend = a1 + arc_type_size; |
| 613 | for(; a1 < aend; fv++, a1++) *fv = *a1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 614 | } |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 615 | /* Increase the first_value by arc0 */ |
| 616 | arc0 *= 40; /* (0..80) */ |
| 617 | for(tp = first_value + arc_type_size; tp >= first_value; tp--) { |
| 618 | unsigned int v = *tp; |
| 619 | v += arc0; |
| 620 | *tp = v; |
| 621 | if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT; |
| 622 | else break; |
| 623 | } |
| 624 | |
| 625 | assert(tp >= first_value); |
| 626 | |
| 627 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value, |
| 628 | fv - first_value, 1); |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Save the rest of arcs. |
| 633 | */ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 634 | for(arcs = ((char *)arcs) + arc_type_size, i = 2; |
| 635 | i < arc_slots; |
| 636 | i++, arcs = ((char *)arcs) + arc_type_size) { |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 637 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 638 | arcs, arc_type_size, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Lev Walkin | 34b2a93 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 641 | assert((unsigned)(bp - buf) <= size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * Replace buffer. |
| 645 | */ |
Lev Walkin | 0787ff0 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 646 | oid->size = bp - buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 647 | bp = oid->buf; |
| 648 | oid->buf = buf; |
| 649 | if(bp) FREEMEM(bp); |
| 650 | |
| 651 | return 0; |
| 652 | } |
Lev Walkin | c4c6196 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 653 | |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 654 | |
| 655 | int |
| 656 | 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] | 657 | long *arcs, unsigned int arcs_slots, const char **opt_oid_text_end) { |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 658 | unsigned int arcs_count = 0; |
| 659 | const char *oid_end; |
| 660 | long value = 0; |
| 661 | enum { |
| 662 | ST_SKIPSPACE, |
| 663 | ST_WAITDIGITS, /* Next character is expected to be a digit */ |
Lev Walkin | fdb2592 | 2005-07-21 09:32:49 +0000 | [diff] [blame] | 664 | ST_DIGITS |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 665 | } state = ST_SKIPSPACE; |
| 666 | |
| 667 | if(!oid_text || oid_txt_length < -1 || (arcs_slots && !arcs)) { |
Lev Walkin | 093ba2e | 2005-04-04 21:10:06 +0000 | [diff] [blame] | 668 | if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 669 | errno = EINVAL; |
| 670 | return -1; |
| 671 | } |
| 672 | |
| 673 | if(oid_txt_length == -1) |
| 674 | oid_txt_length = strlen(oid_text); |
| 675 | |
| 676 | for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) { |
| 677 | switch(*oid_text) { |
| 678 | case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */ |
| 679 | if(state == ST_SKIPSPACE) { |
| 680 | continue; |
| 681 | } else { |
| 682 | break; /* Finish */ |
| 683 | } |
| 684 | case 0x2e: /* '.' */ |
| 685 | if(state != ST_DIGITS |
| 686 | || (oid_text + 1) == oid_end) { |
| 687 | state = ST_WAITDIGITS; |
| 688 | break; |
| 689 | } |
| 690 | if(arcs_count < arcs_slots) |
| 691 | arcs[arcs_count] = value; |
| 692 | arcs_count++; |
| 693 | state = ST_WAITDIGITS; |
| 694 | continue; |
| 695 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 696 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 697 | if(state != ST_DIGITS) { |
| 698 | state = ST_DIGITS; |
| 699 | value = 0; |
| 700 | } |
| 701 | if(1) { |
| 702 | long new_value = value * 10; |
| 703 | if(new_value / 10 != value |
| 704 | || (value = new_value + (*oid_text - 0x30)) < 0) { |
| 705 | /* Overflow */ |
| 706 | state = ST_WAITDIGITS; |
| 707 | break; |
| 708 | } |
| 709 | continue; |
| 710 | } |
| 711 | default: |
| 712 | /* Unexpected symbols */ |
| 713 | state = ST_WAITDIGITS; |
| 714 | break; |
| 715 | } /* switch() */ |
| 716 | break; |
| 717 | } /* for() */ |
| 718 | |
| 719 | |
Lev Walkin | 093ba2e | 2005-04-04 21:10:06 +0000 | [diff] [blame] | 720 | if(opt_oid_text_end) *opt_oid_text_end = oid_text; |
Lev Walkin | 9230225 | 2004-10-23 10:16:51 +0000 | [diff] [blame] | 721 | |
| 722 | /* Finalize last arc */ |
| 723 | switch(state) { |
| 724 | case ST_WAITDIGITS: |
| 725 | errno = EINVAL; |
| 726 | return -1; |
| 727 | case ST_DIGITS: |
| 728 | if(arcs_count < arcs_slots) |
| 729 | arcs[arcs_count] = value; |
| 730 | arcs_count++; |
| 731 | /* Fall through */ |
| 732 | default: |
| 733 | return arcs_count; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | |