Lev Walkin | f15320b | 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 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 12 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | #include <NativeInteger.h> |
| 14 | #include <INTEGER.h> |
| 15 | #include <assert.h> |
| 16 | |
| 17 | /* |
| 18 | * NativeInteger basic type description. |
| 19 | */ |
| 20 | static ber_tlv_tag_t asn1_DEF_NativeInteger_tags[] = { |
| 21 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 22 | }; |
| 23 | asn1_TYPE_descriptor_t asn1_DEF_NativeInteger = { |
| 24 | "INTEGER", /* The ASN.1 type is still INTEGER */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 25 | NativeInteger_free, |
| 26 | NativeInteger_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | asn_generic_no_constraint, |
| 28 | NativeInteger_decode_ber, |
| 29 | NativeInteger_encode_der, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 30 | 0, /* Not implemented yet */ |
| 31 | NativeInteger_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | 0, /* Use generic outmost tag fetcher */ |
| 33 | asn1_DEF_NativeInteger_tags, |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 34 | sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]), |
| 35 | asn1_DEF_NativeInteger_tags, /* Same as above */ |
| 36 | sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 37 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 38 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * Decode INTEGER type. |
| 43 | */ |
| 44 | ber_dec_rval_t |
| 45 | NativeInteger_decode_ber(asn1_TYPE_descriptor_t *td, |
| 46 | void **int_ptr, void *buf_ptr, size_t size, int tag_mode) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 47 | int *Int = (int *)*int_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | ber_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | ber_tlv_len_t length; |
| 50 | |
| 51 | /* |
| 52 | * If the structure is not there, allocate it. |
| 53 | */ |
| 54 | if(Int == NULL) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 55 | Int = (int *)(*int_ptr = CALLOC(1, sizeof(*Int))); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | if(Int == NULL) { |
| 57 | rval.code = RC_FAIL; |
| 58 | rval.consumed = 0; |
| 59 | return rval; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | ASN_DEBUG("Decoding %s as INTEGER (tm=%d)", |
| 64 | td->name, tag_mode); |
| 65 | |
| 66 | /* |
| 67 | * Check tags. |
| 68 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 69 | rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, 0, &length, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | if(rval.code != RC_OK) |
| 71 | return rval; |
| 72 | |
| 73 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 74 | |
| 75 | /* |
| 76 | * Make sure we have this length. |
| 77 | */ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 78 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | size -= rval.consumed; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 80 | if(length > (ber_tlv_len_t)size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | rval.code = RC_WMORE; |
| 82 | rval.consumed = 0; |
| 83 | return rval; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * ASN.1 encoded INTEGER: buf_ptr, length |
| 88 | * Fill the Int, at the same time checking for overflow. |
| 89 | * If overflow occured, return with RC_FAIL. |
| 90 | */ |
| 91 | { |
| 92 | INTEGER_t tmp; |
| 93 | long l; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 94 | tmp.buf = (uint8_t *)buf_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 95 | tmp.size = length; |
| 96 | |
| 97 | if(asn1_INTEGER2long(&tmp, &l)) { |
| 98 | rval.code = RC_FAIL; |
| 99 | rval.consumed = 0; |
| 100 | return rval; |
| 101 | } |
| 102 | |
| 103 | *Int = l; |
| 104 | |
| 105 | /* |
| 106 | * Note that int might be shorter than long. |
| 107 | * This expression hopefully will be optimized away |
| 108 | * by compiler. |
| 109 | */ |
| 110 | if(sizeof(int) != sizeof(long) && (*Int != l)) { |
| 111 | *Int = 0; /* Safe value */ |
| 112 | rval.code = RC_FAIL; |
| 113 | rval.consumed = 0; |
| 114 | return rval; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | rval.code = RC_OK; |
| 119 | rval.consumed += length; |
| 120 | |
| 121 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)", |
| 122 | (long)rval.consumed, (long)length, td->name, *Int); |
| 123 | |
| 124 | return rval; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Encode the NativeInteger using the standard INTEGER type DER encoder. |
| 129 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 130 | asn_enc_rval_t |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 131 | NativeInteger_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr, |
| 132 | int tag_mode, ber_tlv_tag_t tag, |
| 133 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 134 | unsigned int Int = *(unsigned int *)ptr; /* Disable sign ext. */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 135 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 136 | INTEGER_t tmp; |
| 137 | |
| 138 | #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */ |
| 139 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 140 | tmp.buf = (uint8_t *)∬ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 141 | tmp.size = sizeof(Int); |
| 142 | |
| 143 | #else /* Works even if WORDS_BIGENDIAN is not set where should've been */ |
| 144 | uint8_t buf[sizeof(int)]; |
| 145 | uint8_t *p; |
| 146 | |
| 147 | /* Prepare fake INTEGER */ |
| 148 | for(p = buf + sizeof(buf) - 1; p >= buf; p--, Int >>= 8) |
| 149 | *p = Int & 0xff; |
| 150 | |
| 151 | tmp.buf = buf; |
| 152 | tmp.size = sizeof(buf); |
| 153 | #endif /* WORDS_BIGENDIAN */ |
| 154 | |
| 155 | /* Encode fake INTEGER */ |
| 156 | erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key); |
| 157 | if(erval.encoded == -1) { |
| 158 | assert(erval.structure_ptr == &tmp); |
| 159 | erval.structure_ptr = ptr; |
| 160 | } |
| 161 | return erval; |
| 162 | } |
| 163 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 164 | asn_enc_rval_t |
| 165 | NativeInteger_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, |
| 166 | int ilevel, enum xer_encoder_flags_e flags, |
| 167 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 168 | char scratch[32]; /* Enough for 64-bit int */ |
| 169 | asn_enc_rval_t er; |
| 170 | const int *Int = (const int *)sptr; |
| 171 | |
| 172 | (void)ilevel; |
| 173 | (void)flags; |
| 174 | |
| 175 | if(!Int) _ASN_ENCODE_FAILED; |
| 176 | |
| 177 | er.encoded = snprintf(scratch, sizeof(scratch), "%d", *Int); |
| 178 | if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch) |
| 179 | || cb(scratch, er.encoded, app_key) < 0) |
| 180 | _ASN_ENCODE_FAILED; |
| 181 | |
| 182 | return er; |
| 183 | } |
| 184 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 185 | /* |
| 186 | * INTEGER specific human-readable output. |
| 187 | */ |
| 188 | int |
| 189 | NativeInteger_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 190 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 191 | const int *Int = (const int *)sptr; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 192 | char scratch[32]; /* Enough for 64-bit int */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | int ret; |
| 194 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 195 | (void)td; /* Unused argument */ |
| 196 | (void)ilevel; /* Unused argument */ |
| 197 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 198 | if(Int) { |
| 199 | ret = snprintf(scratch, sizeof(scratch), "%d", *Int); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 200 | assert(ret > 0 && ret < (int)sizeof(scratch)); |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 201 | return (cb(scratch, ret, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 202 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 203 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | NativeInteger_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
| 209 | |
| 210 | if(!td || !ptr) |
| 211 | return; |
| 212 | |
| 213 | ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)", |
| 214 | td->name, contents_only, ptr); |
| 215 | |
| 216 | if(!contents_only) { |
| 217 | FREEMEM(ptr); |
| 218 | } |
| 219 | } |
| 220 | |