vlm | 7fc571a | 2004-09-26 13:11:01 +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 | */ |
| 5 | #include <asn_internal.h> |
| 6 | #include <ber_codec_prim.h> |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | |
| 10 | /* |
| 11 | * Decode an always-primitive type. |
| 12 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame^] | 13 | asn_dec_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 14 | ber_decode_primitive(asn_codec_ctx_t *opt_codec_ctx, |
| 15 | asn_TYPE_descriptor_t *td, |
vlm | 7fc571a | 2004-09-26 13:11:01 +0000 | [diff] [blame] | 16 | void **sptr, void *buf_ptr, size_t size, int tag_mode) { |
| 17 | ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame^] | 18 | asn_dec_rval_t rval; |
vlm | 7fc571a | 2004-09-26 13:11:01 +0000 | [diff] [blame] | 19 | ber_tlv_len_t length; |
| 20 | |
| 21 | /* |
| 22 | * If the structure is not there, allocate it. |
| 23 | */ |
| 24 | if(st == NULL) { |
| 25 | (void *)st = *sptr = CALLOC(1, sizeof(*st)); |
| 26 | if(st == NULL) { |
| 27 | rval.code = RC_FAIL; |
| 28 | rval.consumed = 0; |
| 29 | return rval; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | ASN_DEBUG("Decoding %s as plain primitive (tm=%d)", |
| 34 | td->name, tag_mode); |
| 35 | |
| 36 | /* |
| 37 | * Check tags and extract value length. |
| 38 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 39 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 40 | tag_mode, 0, &length, 0); |
vlm | 7fc571a | 2004-09-26 13:11:01 +0000 | [diff] [blame] | 41 | if(rval.code != RC_OK) |
| 42 | return rval; |
| 43 | |
| 44 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 45 | |
| 46 | /* |
| 47 | * Make sure we have this length. |
| 48 | */ |
| 49 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
| 50 | size -= rval.consumed; |
| 51 | if(length > (ber_tlv_len_t)size) { |
| 52 | rval.code = RC_WMORE; |
| 53 | rval.consumed = 0; |
| 54 | return rval; |
| 55 | } |
| 56 | |
| 57 | st->buf = (uint8_t *)MALLOC(length + 1); |
| 58 | if(st->buf) { |
| 59 | st->size = length; |
| 60 | } else { |
| 61 | rval.code = RC_FAIL; |
| 62 | rval.consumed = 0; |
| 63 | return rval; |
| 64 | } |
| 65 | |
| 66 | memcpy(st->buf, buf_ptr, length); |
| 67 | st->buf[length] = '\0'; /* Just in case */ |
| 68 | |
| 69 | rval.code = RC_OK; |
| 70 | rval.consumed += length; |
| 71 | |
| 72 | ASN_DEBUG("Took %ld/%ld bytes to encode %s", |
| 73 | (long)rval.consumed, |
| 74 | (long)length, td->name); |
| 75 | |
| 76 | return rval; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Encode an always-primitive type using DER. |
| 81 | */ |
| 82 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 83 | der_encode_primitive(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 7fc571a | 2004-09-26 13:11:01 +0000 | [diff] [blame] | 84 | int tag_mode, ber_tlv_tag_t tag, |
| 85 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 86 | asn_enc_rval_t erval; |
| 87 | ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr; |
| 88 | |
| 89 | ASN_DEBUG("%s %s as a primitive type (tm=%d)", |
| 90 | cb?"Encoding":"Estimating", td->name, tag_mode); |
| 91 | |
| 92 | erval.encoded = der_write_tags(td, st->size, tag_mode, 0, tag, |
| 93 | cb, app_key); |
| 94 | ASN_DEBUG("%s wrote tags %d", td->name, (int)erval.encoded); |
| 95 | if(erval.encoded == -1) { |
| 96 | erval.failed_type = td; |
| 97 | erval.structure_ptr = sptr; |
| 98 | return erval; |
| 99 | } |
| 100 | |
| 101 | if(cb && st->buf) { |
| 102 | if(cb(st->buf, st->size, app_key) < 0) { |
| 103 | erval.encoded = -1; |
| 104 | erval.failed_type = td; |
| 105 | erval.structure_ptr = sptr; |
| 106 | return erval; |
| 107 | } |
| 108 | } else { |
| 109 | assert(st->buf || st->size == 0); |
| 110 | } |
| 111 | |
| 112 | erval.encoded += st->size; |
| 113 | |
| 114 | return erval; |
| 115 | } |
| 116 | |
| 117 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 118 | ASN__PRIMITIVE_TYPE_free(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 7fc571a | 2004-09-26 13:11:01 +0000 | [diff] [blame] | 119 | int contents_only) { |
| 120 | ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr; |
| 121 | |
| 122 | if(!td || !sptr) |
| 123 | return; |
| 124 | |
| 125 | ASN_DEBUG("Freeing %s as a primitive type", td->name); |
| 126 | |
| 127 | if(st->buf) |
| 128 | FREEMEM(st->buf); |
| 129 | |
| 130 | if(!contents_only) |
| 131 | FREEMEM(st); |
| 132 | } |
| 133 | |