Make dissection of AVP: 3GPP-User-Location-Info(22) l=15 f=V-- vnd=TGPP val=303231...
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include <math.h>
35
36 #include <epan/packet.h>
37
38 #include "asn1.h"
39
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;
45   actx->pinfo = pinfo;
46 }
47
48 gboolean asn1_ctx_check_signature(asn1_ctx_t *actx) {
49   return actx && (actx->signature == ASN1_CTX_SIGNATURE);
50 }
51
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;
56 }
57
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;
62 }
63
64
65 /*--- stack/parameters ---*/
66
67 void asn1_stack_frame_push(asn1_ctx_t *actx, const gchar *name) {
68   asn1_stack_frame_t *frame;
69
70   frame = ep_alloc0(sizeof(asn1_stack_frame_t));
71   frame->name = name;
72   frame->next = actx->stack;
73   actx->stack = frame;
74 }
75
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;
80 }
81
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;
84   asn1_par_t *par;
85
86   DISSECTOR_ASSERT(actx->stack);
87   DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
88
89   par = actx->stack->par;
90   while (pd->name) {
91     DISSECTOR_ASSERT(par);
92     DISSECTOR_ASSERT((pd->ptype == ASN1_PAR_IRR) || (par->ptype == pd->ptype));
93     par->name = pd->name;
94     pd++;
95     par = par->next;
96   }
97   DISSECTOR_ASSERT(!par);
98 }
99
100 static asn1_par_t *get_par_by_name(asn1_ctx_t *actx, const gchar *name) {
101   asn1_par_t *par = NULL;
102
103   DISSECTOR_ASSERT(actx->stack);
104   par = actx->stack->par;
105   while (par) {
106     if (!strcmp(par->name, name))
107       return par;
108     par = par->next;
109   }
110   return par;
111 }
112
113 static asn1_par_t *push_new_par(asn1_ctx_t *actx) {
114   asn1_par_t *par, **pp;
115
116   DISSECTOR_ASSERT(actx->stack);
117
118   par = ep_alloc0(sizeof(asn1_par_t));
119
120   pp = &(actx->stack->par);
121   while (*pp)
122     pp = &((*pp)->next);
123   *pp = par;
124
125   return par;
126 }
127
128 void asn1_param_push_boolean(asn1_ctx_t *actx, gboolean value) {
129   asn1_par_t *par;
130
131   par = push_new_par(actx);
132   par->ptype = ASN1_PAR_BOOLEAN;
133   par->value.v_boolean = value;
134 }
135
136 void asn1_param_push_integer(asn1_ctx_t *actx, gint32 value) {
137   asn1_par_t *par;
138
139   par = push_new_par(actx);
140   par->ptype = ASN1_PAR_INTEGER;
141   par->value.v_integer = value;
142 }
143
144 gboolean asn1_param_get_boolean(asn1_ctx_t *actx, const gchar *name) {
145   asn1_par_t *par = NULL;
146
147   par = get_par_by_name(actx, name);
148   DISSECTOR_ASSERT(par);
149   return par->value.v_boolean;
150 }
151
152 gint32 asn1_param_get_integer(asn1_ctx_t *actx, const gchar *name) {
153   asn1_par_t *par = NULL;
154
155   par = get_par_by_name(actx, name);
156   DISSECTOR_ASSERT(par);
157   return par->value.v_integer;
158 }
159
160
161 /*--- ROSE ---*/
162
163 void rose_ctx_init(rose_ctx_t *rctx) {
164   memset(rctx, '\0', sizeof(*rctx));
165   rctx->signature = ROSE_CTX_SIGNATURE;
166 }
167
168 gboolean rose_ctx_check_signature(rose_ctx_t *rctx) {
169   return rctx && (rctx->signature == ROSE_CTX_SIGNATURE);
170 }
171
172 void rose_ctx_clean_data(rose_ctx_t *rctx) {
173   memset(&rctx->d, '\0', sizeof(rctx->d));
174   rctx->d.code = -1;
175 }
176
177 asn1_ctx_t *get_asn1_ctx(void *ptr) {
178   asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
179
180   if (!asn1_ctx_check_signature(actx)) 
181     actx = NULL;
182
183   return actx;
184 }
185
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;
189
190   if (!asn1_ctx_check_signature(actx)) 
191     actx = NULL;
192
193   if (actx)
194     rctx = actx->rose_ctx;
195
196   if (!rose_ctx_check_signature(rctx)) 
197     rctx = NULL;
198
199   return rctx;
200 }
201
202 double asn1_get_real(const guint8 *real_ptr, gint real_len) {
203   guint8 octet;
204   const guint8 *p;
205   guint8 *buf;
206   double val = 0;
207
208   if (real_len < 1) return val;
209   octet = real_ptr[0];
210   p = real_ptr + 1;
211   real_len -= 1;
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;
217     }
218   } else {  /* decimal encoding */
219     buf = ep_strndup(p, real_len);
220     val = atof(buf);
221   }
222
223   return val;
224 }