vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | /* |
| 6 | * Please read the NativeInteger.h for the explanation wrt. differences between |
| 7 | * INTEGER and NativeInteger. |
| 8 | * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this |
| 9 | * implementation deals with the standard (machine-specific) representation |
| 10 | * of them instead of using the platform-independent buffer. |
| 11 | */ |
| 12 | #include <NativeInteger.h> |
| 13 | #include <INTEGER.h> |
| 14 | #include <assert.h> |
| 15 | |
| 16 | /* |
| 17 | * NativeInteger basic type description. |
| 18 | */ |
| 19 | static ber_tlv_tag_t asn1_DEF_NativeInteger_tags[] = { |
| 20 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 21 | }; |
| 22 | asn1_TYPE_descriptor_t asn1_DEF_NativeInteger = { |
| 23 | "INTEGER", /* The ASN.1 type is still INTEGER */ |
| 24 | asn_generic_no_constraint, |
| 25 | NativeInteger_decode_ber, |
| 26 | NativeInteger_encode_der, |
| 27 | NativeInteger_print, |
| 28 | NativeInteger_free, |
| 29 | 0, /* Use generic outmost tag fetcher */ |
| 30 | asn1_DEF_NativeInteger_tags, |
| 31 | sizeof(asn1_DEF_NativeInteger_tags)/sizeof(asn1_DEF_NativeInteger_tags[0]), |
| 32 | 1, /* Single UNIVERSAL tag may be implicitly overriden */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 33 | 0, /* Always in primitive form */ |
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 | /* |
| 39 | * Decode INTEGER type. |
| 40 | */ |
| 41 | ber_dec_rval_t |
| 42 | NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td, |
| 43 | void **int_ptr, void *buf_ptr, size_t size, int tag_mode) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 44 | int *Int = (int *)*int_ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | ber_dec_rval_t rval; |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 46 | ber_dec_ctx_t ctx = { 0, 0, 0, 0 }; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | ber_tlv_len_t length; |
| 48 | |
| 49 | /* |
| 50 | * If the structure is not there, allocate it. |
| 51 | */ |
| 52 | if(Int == NULL) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 53 | (void *)Int = *int_ptr = CALLOC(1, sizeof(*Int)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 54 | if(Int == NULL) { |
| 55 | rval.code = RC_FAIL; |
| 56 | rval.consumed = 0; |
| 57 | return rval; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | ASN_DEBUG("Decoding %s as INTEGER (tm=%d)", |
| 62 | td->name, tag_mode); |
| 63 | |
| 64 | /* |
| 65 | * Check tags. |
| 66 | */ |
| 67 | rval = ber_check_tags(td, &ctx, |
| 68 | buf_ptr, size, tag_mode, &length, 0); |
| 69 | if(rval.code != RC_OK) |
| 70 | return rval; |
| 71 | |
| 72 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 73 | |
| 74 | /* |
| 75 | * Make sure we have this length. |
| 76 | */ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 77 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 78 | size -= rval.consumed; |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 79 | if(length > (ber_tlv_len_t)size) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | rval.code = RC_WMORE; |
| 81 | rval.consumed = 0; |
| 82 | return rval; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * ASN.1 encoded INTEGER: buf_ptr, length |
| 87 | * Fill the Int, at the same time checking for overflow. |
| 88 | * If overflow occured, return with RC_FAIL. |
| 89 | */ |
| 90 | { |
| 91 | INTEGER_t tmp; |
| 92 | long l; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 93 | tmp.buf = (uint8_t *)buf_ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | tmp.size = length; |
| 95 | |
| 96 | if(asn1_INTEGER2long(&tmp, &l)) { |
| 97 | rval.code = RC_FAIL; |
| 98 | rval.consumed = 0; |
| 99 | return rval; |
| 100 | } |
| 101 | |
| 102 | *Int = l; |
| 103 | |
| 104 | /* |
| 105 | * Note that int might be shorter than long. |
| 106 | * This expression hopefully will be optimized away |
| 107 | * by compiler. |
| 108 | */ |
| 109 | if(sizeof(int) != sizeof(long) && (*Int != l)) { |
| 110 | *Int = 0; /* Safe value */ |
| 111 | rval.code = RC_FAIL; |
| 112 | rval.consumed = 0; |
| 113 | return rval; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | rval.code = RC_OK; |
| 118 | rval.consumed += length; |
| 119 | |
| 120 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)", |
| 121 | (long)rval.consumed, (long)length, td->name, *Int); |
| 122 | |
| 123 | return rval; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Encode the NativeInteger using the standard INTEGER type DER encoder. |
| 128 | */ |
| 129 | der_enc_rval_t |
| 130 | NativeInteger_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr, |
| 131 | int tag_mode, ber_tlv_tag_t tag, |
| 132 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 133 | unsigned int Int = *(unsigned int *)ptr; /* Disable sign ext. */ |
| 134 | der_enc_rval_t erval; |
| 135 | INTEGER_t tmp; |
| 136 | |
| 137 | #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */ |
| 138 | |
| 139 | tmp.buf = ∬ |
| 140 | tmp.size = sizeof(Int); |
| 141 | |
| 142 | #else /* Works even if WORDS_BIGENDIAN is not set where should've been */ |
| 143 | uint8_t buf[sizeof(int)]; |
| 144 | uint8_t *p; |
| 145 | |
| 146 | /* Prepare fake INTEGER */ |
| 147 | for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8) |
| 148 | *p = Int & 0xff; |
| 149 | |
| 150 | tmp.buf = buf; |
| 151 | tmp.size = sizeof(buf); |
| 152 | #endif /* WORDS_BIGENDIAN */ |
| 153 | |
| 154 | /* Encode fake INTEGER */ |
| 155 | erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key); |
| 156 | if(erval.encoded == -1) { |
| 157 | assert(erval.structure_ptr == &tmp); |
| 158 | erval.structure_ptr = ptr; |
| 159 | } |
| 160 | return erval; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * INTEGER specific human-readable output. |
| 165 | */ |
| 166 | int |
| 167 | NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 168 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 169 | const int *Int = (const int *)sptr; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 170 | char scratch[32]; /* Enough for 64-bit int */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 171 | int ret; |
| 172 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 173 | (void)td; /* Unused argument */ |
| 174 | (void)ilevel; /* Unused argument */ |
| 175 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | if(Int) { |
| 177 | ret = snprintf(scratch, sizeof(scratch), "%d", *Int); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 178 | assert(ret > 0 && ret < (int)sizeof(scratch)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 179 | return cb(scratch, ret, app_key); |
| 180 | } else { |
| 181 | return cb("<absent>", 8, app_key); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | NativeInteger_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
| 187 | |
| 188 | if(!td || !ptr) |
| 189 | return; |
| 190 | |
| 191 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
| 192 | td->name, contents_only, ptr); |
| 193 | |
| 194 | if(!contents_only) { |
| 195 | FREEMEM(ptr); |
| 196 | } |
| 197 | } |
| 198 | |