blob: c4d06e16d9c1cd1cbb8d5cbaf62919745da20741 [file] [log] [blame]
Lev Walkine7318792004-09-26 13:11:01 +00001/*-
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 */
13ber_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000014ber_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
15 asn_TYPE_descriptor_t *td,
Lev Walkine7318792004-09-26 13:11:01 +000016 void **sptr, void *buf_ptr, size_t size, int tag_mode) {
17 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
18 ber_dec_rval_t rval;
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 */
Lev Walkin5e033762004-09-29 13:26:15 +000039 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
40 tag_mode, 0, &length, 0);
Lev Walkine7318792004-09-26 13:11:01 +000041 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 */
82asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000083der_encode_primitive(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkine7318792004-09-26 13:11:01 +000084 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
117void
Lev Walkin5e033762004-09-29 13:26:15 +0000118ASN__PRIMITIVE_TYPE_free(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkine7318792004-09-26 13:11:01 +0000119 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