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 | 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 | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | 0, /* Not implemented yet */ |
| 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 | |
| 265 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 266 | OBJECT_IDENTIFIER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 267 | int ilevel, enum xer_encoder_flags_e flags, |
| 268 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 269 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 270 | asn_enc_rval_t er; |
| 271 | |
| 272 | (void)ilevel; |
| 273 | (void)flags; |
| 274 | |
| 275 | if(!st || !st->buf) |
| 276 | _ASN_ENCODE_FAILED; |
| 277 | |
| 278 | er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key); |
| 279 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 280 | |
| 281 | return er; |
| 282 | } |
| 283 | |
| 284 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 285 | OBJECT_IDENTIFIER_print(asn_TYPE_descriptor_t *td, const void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 286 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
| 287 | const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr; |
| 288 | |
| 289 | (void)td; /* Unused argument */ |
| 290 | (void)ilevel; /* Unused argument */ |
| 291 | |
| 292 | if(!st || !st->buf) |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 293 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 294 | |
| 295 | /* Dump preamble */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 296 | if(cb("{ ", 2, app_key) < 0) |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 297 | return -1; |
| 298 | |
| 299 | if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) |
| 300 | return -1; |
| 301 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 302 | return (cb(" }", 2, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | int |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 306 | OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, |
| 307 | unsigned int arc_type_size, unsigned int arc_slots) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 308 | void *arcs_end = (char *)arcs + (arc_type_size * arc_slots); |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 309 | int num_arcs = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 310 | int startn = 0; |
| 311 | int add = 0; |
| 312 | int i; |
| 313 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 314 | if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 315 | errno = EINVAL; |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | for(i = 0; i < oid->size; i++) { |
| 320 | uint8_t b = oid->buf[i]; |
| 321 | if((b & 0x80)) /* Continuation expected */ |
| 322 | continue; |
| 323 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 324 | if(num_arcs == 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 325 | /* |
| 326 | * First two arcs are encoded through the backdoor. |
| 327 | */ |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 328 | unsigned LE = 1; /* Little endian */ |
| 329 | int first_arc; |
| 330 | num_arcs++; |
| 331 | if(!arc_slots) { num_arcs++; continue; } |
| 332 | |
| 333 | if(i) first_arc = 2; |
| 334 | else if(b <= 39) first_arc = 0; |
| 335 | else if(b < 79) first_arc = 1; |
| 336 | else first_arc = 2; |
| 337 | |
| 338 | add = -40 * first_arc; |
| 339 | memset(arcs, 0, arc_type_size); |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 340 | *(unsigned char *)((char *)arcs |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 341 | + ((*(char *)&LE)?0:(arc_type_size - 1))) |
| 342 | = first_arc; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 343 | arcs = ((char *)arcs) + arc_type_size; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 344 | } |
| 345 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 346 | /* Decode, if has space */ |
| 347 | if(arcs < arcs_end) { |
| 348 | if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn], |
| 349 | i - startn + 1, add, |
| 350 | arcs, arc_type_size)) |
| 351 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | startn = i + 1; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 353 | arcs = ((char *)arcs) + arc_type_size; |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 354 | add = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 355 | } |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 356 | num_arcs++; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 357 | } |
| 358 | |
vlm | 2e3dd3b | 2004-06-14 07:24:36 +0000 | [diff] [blame] | 359 | return num_arcs; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 360 | } |
| 361 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * Save the single value as an object identifier arc. |
| 365 | */ |
vlm | e55716a | 2004-08-11 09:10:59 +0000 | [diff] [blame] | 366 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 367 | OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) { |
| 368 | /* |
| 369 | * The following conditions must hold: |
| 370 | * assert(arcval); |
| 371 | * assert(arcval_size > 0); |
| 372 | * assert(arcbuf); |
| 373 | */ |
| 374 | #ifdef WORDS_BIGENDIAN |
| 375 | const unsigned isLittleEndian = 0; |
| 376 | #else |
| 377 | unsigned LE = 1; |
| 378 | unsigned isLittleEndian = *(char *)&LE; |
| 379 | #endif |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 380 | uint8_t *tp, *tend; |
| 381 | unsigned int cache; |
| 382 | uint8_t *bp = arcbuf; |
| 383 | int bits; |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 384 | #ifdef __GNUC__ |
| 385 | uint8_t buffer[arcval_size]; |
| 386 | #else |
| 387 | uint8_t *buffer = alloca(arcval_size); |
vlm | 7a6a60e | 2004-08-11 07:41:45 +0000 | [diff] [blame] | 388 | if(!buffer) { errno = ENOMEM; return -1; } |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 389 | #endif |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 390 | |
| 391 | if(isLittleEndian && !prepared_order) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 392 | uint8_t *a = (unsigned char *)arcval + arcval_size - 1; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 393 | uint8_t *aend = (uint8_t *)arcval; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 394 | uint8_t *msb = buffer + arcval_size - 1; |
| 395 | for(tp = buffer; a >= aend; tp++, a--) |
| 396 | if((*tp = *a) && (tp < msb)) |
| 397 | msb = tp; |
| 398 | tend = &buffer[arcval_size]; |
| 399 | tp = msb; /* Most significant non-zero byte */ |
| 400 | } else { |
| 401 | /* Look for most significant non-zero byte */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 402 | tend = (unsigned char *)arcval + arcval_size; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 403 | for(tp = (uint8_t *)arcval; tp < tend - 1; tp++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 404 | if(*tp) break; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Split the value in 7-bits chunks. |
| 409 | */ |
| 410 | bits = ((tend - tp) * CHAR_BIT) % 7; |
| 411 | if(bits) { |
| 412 | cache = *tp >> (CHAR_BIT - bits); |
| 413 | if(cache) { |
| 414 | *bp++ = cache | 0x80; |
| 415 | cache = *tp++; |
| 416 | bits = CHAR_BIT - bits; |
| 417 | } else { |
| 418 | bits = -bits; |
| 419 | } |
| 420 | } else { |
| 421 | cache = 0; |
| 422 | } |
| 423 | for(; tp < tend; tp++) { |
| 424 | cache = (cache << CHAR_BIT) + *tp; |
| 425 | bits += CHAR_BIT; |
| 426 | while(bits >= 7) { |
| 427 | bits -= 7; |
| 428 | *bp++ = 0x80 | (cache >> bits); |
| 429 | } |
| 430 | } |
| 431 | if(bits) *bp++ = cache; |
| 432 | bp[-1] &= 0x7f; /* Clear the last bit */ |
| 433 | |
| 434 | return bp - arcbuf; |
| 435 | } |
| 436 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 437 | int |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 438 | 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] | 439 | uint8_t *buf; |
| 440 | uint8_t *bp; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 441 | unsigned LE = 1; /* Little endian (x86) */ |
| 442 | unsigned isLittleEndian = *((char *)&LE); |
| 443 | unsigned int arc0; |
| 444 | unsigned int arc1; |
| 445 | unsigned size; |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 446 | unsigned i; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 447 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 448 | if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 449 | errno = EINVAL; |
| 450 | return -1; |
| 451 | } |
| 452 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 453 | switch(arc_type_size) { |
| 454 | case sizeof(char): |
| 455 | arc0 = ((unsigned char *)arcs)[0]; |
| 456 | arc1 = ((unsigned char *)arcs)[1]; |
| 457 | break; |
| 458 | case sizeof(short): |
| 459 | arc0 = ((unsigned short *)arcs)[0]; |
| 460 | arc1 = ((unsigned short *)arcs)[1]; |
| 461 | break; |
| 462 | case sizeof(int): |
| 463 | arc0 = ((unsigned int *)arcs)[0]; |
| 464 | arc1 = ((unsigned int *)arcs)[1]; |
| 465 | break; |
| 466 | default: |
| 467 | arc1 = arc0 = 0; |
| 468 | if(isLittleEndian) { /* Little endian (x86) */ |
| 469 | unsigned char *ps, *pe; |
| 470 | /* If more significant bytes are present, |
| 471 | * make them > 255 quick */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 472 | for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size; |
| 473 | ps < pe; ps++) |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 474 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 475 | arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT; |
| 476 | arc0 = *((unsigned char *)arcs + 0); |
| 477 | arc1 = *((unsigned char *)arcs + arc_type_size); |
| 478 | } else { |
| 479 | unsigned char *ps, *pe; |
| 480 | /* If more significant bytes are present, |
| 481 | * make them > 255 quick */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 482 | 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] | 483 | arc0 |= *ps, arc1 |= *(ps + arc_type_size); |
| 484 | arc0 = *((unsigned char *)arcs + arc_type_size - 1); |
| 485 | arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * The previous chapter left us with the first and the second arcs. |
| 491 | * The values are not precise (that is, they are valid only if |
| 492 | * they're less than 255), but OK for the purposes of making |
| 493 | * the sanity test below. |
| 494 | */ |
| 495 | if(arc0 <= 1) { |
| 496 | if(arc1 >= 39) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 497 | /* 8.19.4: At most 39 subsequent values (including 0) */ |
| 498 | errno = ERANGE; |
| 499 | return -1; |
| 500 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 501 | } else if(arc0 > 2) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 502 | /* 8.19.4: Only three values are allocated from the root node */ |
| 503 | errno = ERANGE; |
| 504 | return -1; |
| 505 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 506 | /* |
| 507 | * After above tests it is known that the value of arc0 is completely |
| 508 | * trustworthy (0..2). However, the arc1's value is still meaningless. |
| 509 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * Roughly estimate the maximum size necessary to encode these arcs. |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 513 | * This estimation implicitly takes in account the following facts, |
| 514 | * that cancel each other: |
| 515 | * * the first two arcs are encoded in a single value. |
| 516 | * * the first value may require more space (+1 byte) |
| 517 | * * the value of the first arc which is in range (0..2) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 518 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 519 | size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 520 | bp = buf = (uint8_t *)MALLOC(size + 1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 521 | if(!buf) { |
| 522 | /* ENOMEM */ |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | /* |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 527 | * Encode the first two arcs. |
| 528 | * These require special treatment. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 529 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 530 | { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 531 | uint8_t *tp; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 532 | #ifdef __GNUC__ |
| 533 | uint8_t first_value[1 + arc_type_size]; /* of two arcs */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 534 | uint8_t *fv = first_value; |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 535 | #else |
| 536 | uint8_t *first_value = alloca(1 + arc_type_size); |
vlm | a51f7ce | 2004-08-11 07:48:19 +0000 | [diff] [blame] | 537 | uint8_t *fv = first_value; |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 538 | if(!first_value) { |
| 539 | errno = ENOMEM; |
| 540 | return -1; |
| 541 | } |
| 542 | #endif |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 543 | |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 544 | /* |
| 545 | * Simulate first_value = arc0 * 40 + arc1; |
| 546 | */ |
| 547 | /* Copy the second (1'st) arcs[1] into the first_value */ |
| 548 | *fv++ = 0; |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 549 | arcs = ((char *)arcs) + arc_type_size; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 550 | if(isLittleEndian) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 551 | uint8_t *aend = (unsigned char *)arcs - 1; |
| 552 | uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 553 | for(; a1 > aend; fv++, a1--) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 554 | } else { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 555 | uint8_t *a1 = (uint8_t *)arcs; |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 556 | uint8_t *aend = a1 + arc_type_size; |
| 557 | for(; a1 < aend; fv++, a1++) *fv = *a1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 558 | } |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 559 | /* Increase the first_value by arc0 */ |
| 560 | arc0 *= 40; /* (0..80) */ |
| 561 | for(tp = first_value + arc_type_size; tp >= first_value; tp--) { |
| 562 | unsigned int v = *tp; |
| 563 | v += arc0; |
| 564 | *tp = v; |
| 565 | if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT; |
| 566 | else break; |
| 567 | } |
| 568 | |
| 569 | assert(tp >= first_value); |
| 570 | |
| 571 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value, |
| 572 | fv - first_value, 1); |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Save the rest of arcs. |
| 577 | */ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 578 | for(arcs = ((char *)arcs) + arc_type_size, i = 2; |
| 579 | i < arc_slots; |
| 580 | i++, arcs = ((char *)arcs) + arc_type_size) { |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 581 | bp += OBJECT_IDENTIFIER_set_single_arc(bp, |
| 582 | arcs, arc_type_size, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 583 | } |
| 584 | |
vlm | a63e029 | 2004-06-17 23:46:45 +0000 | [diff] [blame] | 585 | assert((unsigned)(bp - buf) <= size); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * Replace buffer. |
| 589 | */ |
vlm | 1255771 | 2004-06-17 23:43:39 +0000 | [diff] [blame] | 590 | oid->size = bp - buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 591 | bp = oid->buf; |
| 592 | oid->buf = buf; |
| 593 | if(bp) FREEMEM(bp); |
| 594 | |
| 595 | return 0; |
| 596 | } |
vlm | 3717fb3 | 2004-06-14 08:17:27 +0000 | [diff] [blame] | 597 | |