Slightly different fix for https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9117 :
[metze/wireshark/wip.git] / epan / asn1.c
1 /* asn1.c
2  * Common routines for ASN.1
3  * 2007  Anders Broman
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <math.h>
33
34 #include <epan/packet.h>
35
36 #include "asn1.h"
37
38 void asn1_ctx_init(asn1_ctx_t *actx, asn1_enc_e encoding, gboolean aligned, packet_info *pinfo) {
39   memset(actx, '\0', sizeof(*actx));
40   actx->signature = ASN1_CTX_SIGNATURE;
41   actx->encoding = encoding;
42   actx->aligned = aligned;
43   actx->pinfo = pinfo;
44 }
45
46 gboolean asn1_ctx_check_signature(asn1_ctx_t *actx) {
47   return actx && (actx->signature == ASN1_CTX_SIGNATURE);
48 }
49
50 void asn1_ctx_clean_external(asn1_ctx_t *actx) {
51   memset(&actx->external, '\0', sizeof(actx->external));
52   actx->external.hf_index = -1;
53   actx->external.encoding = -1;
54 }
55
56 void asn1_ctx_clean_epdv(asn1_ctx_t *actx) {
57   memset(&actx->embedded_pdv, '\0', sizeof(actx->embedded_pdv));
58   actx->embedded_pdv.hf_index = -1;
59   actx->embedded_pdv.identification = -1;
60 }
61
62
63 /*--- stack/parameters ---*/
64
65 void asn1_stack_frame_push(asn1_ctx_t *actx, const gchar *name) {
66   asn1_stack_frame_t *frame;
67
68   frame = ep_new0(asn1_stack_frame_t);
69   frame->name = name;
70   frame->next = actx->stack;
71   actx->stack = frame;
72 }
73
74 void asn1_stack_frame_pop(asn1_ctx_t *actx, const gchar *name) {
75   DISSECTOR_ASSERT(actx->stack);
76   DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
77   actx->stack = actx->stack->next;
78 }
79
80 void asn1_stack_frame_check(asn1_ctx_t *actx, const gchar *name, const asn1_par_def_t *par_def) {
81   const asn1_par_def_t *pd = par_def;
82   asn1_par_t *par;
83
84   DISSECTOR_ASSERT(actx->stack);
85   DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
86
87   par = actx->stack->par;
88   while (pd->name) {
89     DISSECTOR_ASSERT(par);
90     DISSECTOR_ASSERT((pd->ptype == ASN1_PAR_IRR) || (par->ptype == pd->ptype));
91     par->name = pd->name;
92     pd++;
93     par = par->next;
94   }
95   DISSECTOR_ASSERT(!par);
96 }
97
98 static asn1_par_t *get_par_by_name(asn1_ctx_t *actx, const gchar *name) {
99   asn1_par_t *par = NULL;
100
101   DISSECTOR_ASSERT(actx->stack);
102   par = actx->stack->par;
103   while (par) {
104     if (!strcmp(par->name, name))
105       return par;
106     par = par->next;
107   }
108   return par;
109 }
110
111 static asn1_par_t *push_new_par(asn1_ctx_t *actx) {
112   asn1_par_t *par, **pp;
113
114   DISSECTOR_ASSERT(actx->stack);
115
116   par = ep_new0(asn1_par_t);
117
118   pp = &(actx->stack->par);
119   while (*pp)
120     pp = &((*pp)->next);
121   *pp = par;
122
123   return par;
124 }
125
126 void asn1_param_push_boolean(asn1_ctx_t *actx, gboolean value) {
127   asn1_par_t *par;
128
129   par = push_new_par(actx);
130   par->ptype = ASN1_PAR_BOOLEAN;
131   par->value.v_boolean = value;
132 }
133
134 void asn1_param_push_integer(asn1_ctx_t *actx, gint32 value) {
135   asn1_par_t *par;
136
137   par = push_new_par(actx);
138   par->ptype = ASN1_PAR_INTEGER;
139   par->value.v_integer = value;
140 }
141
142 gboolean asn1_param_get_boolean(asn1_ctx_t *actx, const gchar *name) {
143   asn1_par_t *par = NULL;
144
145   par = get_par_by_name(actx, name);
146   DISSECTOR_ASSERT(par);
147   return par->value.v_boolean;
148 }
149
150 gint32 asn1_param_get_integer(asn1_ctx_t *actx, const gchar *name) {
151   asn1_par_t *par = NULL;
152
153   par = get_par_by_name(actx, name);
154   DISSECTOR_ASSERT(par);
155   return par->value.v_integer;
156 }
157
158
159 /*--- ROSE ---*/
160
161 void rose_ctx_init(rose_ctx_t *rctx) {
162   memset(rctx, '\0', sizeof(*rctx));
163   rctx->signature = ROSE_CTX_SIGNATURE;
164 }
165
166 gboolean rose_ctx_check_signature(rose_ctx_t *rctx) {
167   return rctx && (rctx->signature == ROSE_CTX_SIGNATURE);
168 }
169
170 void rose_ctx_clean_data(rose_ctx_t *rctx) {
171   memset(&rctx->d, '\0', sizeof(rctx->d));
172   rctx->d.code = -1;
173 }
174
175 asn1_ctx_t *get_asn1_ctx(void *ptr) {
176   asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
177
178   if (!asn1_ctx_check_signature(actx)) 
179     actx = NULL;
180
181   return actx;
182 }
183
184 rose_ctx_t *get_rose_ctx(void *ptr) {
185   rose_ctx_t *rctx = (rose_ctx_t*)ptr;
186   asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
187
188   if (!asn1_ctx_check_signature(actx)) 
189     actx = NULL;
190
191   if (actx)
192     rctx = actx->rose_ctx;
193
194   if (!rose_ctx_check_signature(rctx)) 
195     rctx = NULL;
196
197   return rctx;
198 }
199
200 double asn1_get_real(const guint8 *real_ptr, gint real_len) {
201   guint8 octet;
202   const guint8 *p;
203   guint8 *buf;
204   double val = 0;
205
206   if (real_len < 1) return val;
207   octet = real_ptr[0];
208   p = real_ptr + 1;
209   real_len -= 1;
210   if (octet & 0x80) {  /* binary encoding */
211   } else if (octet & 0x40) {  /* SpecialRealValue */
212     switch (octet & 0x3F) {
213       case 0x00: val = HUGE_VAL; break;
214       case 0x01: val = -HUGE_VAL; break;
215     }
216   } else {  /* decimal encoding */
217     buf = ep_strndup(p, real_len);
218     val = atof(buf);
219   }
220
221   return val;
222 }