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