2 * Common routines for ASN.1
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include <epan/packet.h>
40 void asn1_ctx_init(asn1_ctx_t *actx, asn1_enc_e encoding, gboolean aligned, packet_info *pinfo) {
41 memset(actx, '\0', sizeof(*actx));
42 actx->signature = ASN1_CTX_SIGNATURE;
43 actx->encoding = encoding;
44 actx->aligned = aligned;
48 gboolean asn1_ctx_check_signature(asn1_ctx_t *actx) {
49 return actx && (actx->signature == ASN1_CTX_SIGNATURE);
52 void asn1_ctx_clean_external(asn1_ctx_t *actx) {
53 memset(&actx->external, '\0', sizeof(actx->external));
54 actx->external.hf_index = -1;
55 actx->external.encoding = -1;
58 void asn1_ctx_clean_epdv(asn1_ctx_t *actx) {
59 memset(&actx->embedded_pdv, '\0', sizeof(actx->embedded_pdv));
60 actx->embedded_pdv.hf_index = -1;
61 actx->embedded_pdv.identification = -1;
65 /*--- stack/parameters ---*/
67 void asn1_stack_frame_push(asn1_ctx_t *actx, const gchar *name) {
68 asn1_stack_frame_t *frame;
70 frame = ep_alloc0(sizeof(asn1_stack_frame_t));
72 frame->next = actx->stack;
76 void asn1_stack_frame_pop(asn1_ctx_t *actx, const gchar *name) {
77 DISSECTOR_ASSERT(actx->stack);
78 DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
79 actx->stack = actx->stack->next;
82 void asn1_stack_frame_check(asn1_ctx_t *actx, const gchar *name, const asn1_par_def_t *par_def) {
83 const asn1_par_def_t *pd = par_def;
86 DISSECTOR_ASSERT(actx->stack);
87 DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
89 par = actx->stack->par;
91 DISSECTOR_ASSERT(par);
92 DISSECTOR_ASSERT((pd->ptype == ASN1_PAR_IRR) || (par->ptype == pd->ptype));
97 DISSECTOR_ASSERT(!par);
100 static asn1_par_t *get_par_by_name(asn1_ctx_t *actx, const gchar *name) {
101 asn1_par_t *par = NULL;
103 DISSECTOR_ASSERT(actx->stack);
104 par = actx->stack->par;
106 if (!strcmp(par->name, name))
113 static asn1_par_t *push_new_par(asn1_ctx_t *actx) {
114 asn1_par_t *par, **pp;
116 DISSECTOR_ASSERT(actx->stack);
118 par = ep_alloc0(sizeof(asn1_par_t));
120 pp = &(actx->stack->par);
128 void asn1_param_push_boolean(asn1_ctx_t *actx, gboolean value) {
131 par = push_new_par(actx);
132 par->ptype = ASN1_PAR_BOOLEAN;
133 par->value.v_boolean = value;
136 void asn1_param_push_integer(asn1_ctx_t *actx, gint32 value) {
139 par = push_new_par(actx);
140 par->ptype = ASN1_PAR_INTEGER;
141 par->value.v_integer = value;
144 gboolean asn1_param_get_boolean(asn1_ctx_t *actx, const gchar *name) {
145 asn1_par_t *par = NULL;
147 par = get_par_by_name(actx, name);
148 DISSECTOR_ASSERT(par);
149 return par->value.v_boolean;
152 gint32 asn1_param_get_integer(asn1_ctx_t *actx, const gchar *name) {
153 asn1_par_t *par = NULL;
155 par = get_par_by_name(actx, name);
156 DISSECTOR_ASSERT(par);
157 return par->value.v_integer;
163 void rose_ctx_init(rose_ctx_t *rctx) {
164 memset(rctx, '\0', sizeof(*rctx));
165 rctx->signature = ROSE_CTX_SIGNATURE;
168 gboolean rose_ctx_check_signature(rose_ctx_t *rctx) {
169 return rctx && (rctx->signature == ROSE_CTX_SIGNATURE);
172 void rose_ctx_clean_data(rose_ctx_t *rctx) {
173 memset(&rctx->d, '\0', sizeof(rctx->d));
177 asn1_ctx_t *get_asn1_ctx(void *ptr) {
178 asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
180 if (!asn1_ctx_check_signature(actx))
186 rose_ctx_t *get_rose_ctx(void *ptr) {
187 rose_ctx_t *rctx = (rose_ctx_t*)ptr;
188 asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
190 if (!asn1_ctx_check_signature(actx))
194 rctx = actx->rose_ctx;
196 if (!rose_ctx_check_signature(rctx))
202 double asn1_get_real(const guint8 *real_ptr, gint real_len) {
208 if (real_len < 1) return val;
212 if (octet & 0x80) { /* binary encoding */
213 } else if (octet & 0x40) { /* SpecialRealValue */
214 switch (octet & 0x3F) {
215 case 0x00: val = HUGE_VAL; break;
216 case 0x01: val = -HUGE_VAL; break;
218 } else { /* decimal encoding */
219 buf = ep_alloc0(real_len + 1);
220 memcpy(buf, p, real_len);