(Temporarily) Allow DCE/RPC dissector table to have duplicates.
[metze/wireshark/wip.git] / epan / asn1.c
1 /* asn1.c
2  * Common routines for ASN.1
3  * 2007  Anders Broman
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <glib.h>
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <math.h>
31 #ifdef DEBUG
32 #include <stdio.h>
33 #endif
34
35 #include <epan/packet.h>
36
37 #include "asn1.h"
38
39 void asn1_ctx_init(asn1_ctx_t *actx, asn1_enc_e encoding, gboolean aligned, packet_info *pinfo) {
40   memset(actx, '\0', sizeof(*actx));
41   actx->signature = ASN1_CTX_SIGNATURE;
42   actx->encoding = encoding;
43   actx->aligned = aligned;
44   actx->pinfo = pinfo;
45 }
46
47 gboolean asn1_ctx_check_signature(asn1_ctx_t *actx) {
48   return actx && (actx->signature == ASN1_CTX_SIGNATURE);
49 }
50
51 void asn1_ctx_clean_external(asn1_ctx_t *actx) {
52   memset(&actx->external, '\0', sizeof(actx->external));
53   actx->external.hf_index = -1;
54   actx->external.encoding = -1;
55 }
56
57 void asn1_ctx_clean_epdv(asn1_ctx_t *actx) {
58   memset(&actx->embedded_pdv, '\0', sizeof(actx->embedded_pdv));
59   actx->embedded_pdv.hf_index = -1;
60   actx->embedded_pdv.identification = -1;
61 }
62
63
64 /*--- stack/parameters ---*/
65
66 void asn1_stack_frame_push(asn1_ctx_t *actx, const gchar *name) {
67   asn1_stack_frame_t *frame;
68
69   frame = wmem_new0(wmem_packet_scope(), asn1_stack_frame_t);
70   frame->name = name;
71   frame->next = actx->stack;
72   actx->stack = frame;
73 }
74
75 void asn1_stack_frame_pop(asn1_ctx_t *actx, const gchar *name) {
76   DISSECTOR_ASSERT(actx->stack);
77   DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
78   actx->stack = actx->stack->next;
79 }
80
81 void asn1_stack_frame_check(asn1_ctx_t *actx, const gchar *name, const asn1_par_def_t *par_def) {
82   const asn1_par_def_t *pd = par_def;
83   asn1_par_t *par;
84
85   DISSECTOR_ASSERT(actx->stack);
86   DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
87
88   par = actx->stack->par;
89   while (pd->name) {
90     DISSECTOR_ASSERT(par);
91     DISSECTOR_ASSERT((pd->ptype == ASN1_PAR_IRR) || (par->ptype == pd->ptype));
92     par->name = pd->name;
93     pd++;
94     par = par->next;
95   }
96   DISSECTOR_ASSERT(!par);
97 }
98
99 static asn1_par_t *get_par_by_name(asn1_ctx_t *actx, const gchar *name) {
100   asn1_par_t *par = NULL;
101
102   DISSECTOR_ASSERT(actx->stack);
103   par = actx->stack->par;
104   while (par) {
105     if (!strcmp(par->name, name))
106       return par;
107     par = par->next;
108   }
109   return par;
110 }
111
112 static asn1_par_t *push_new_par(asn1_ctx_t *actx) {
113   asn1_par_t *par, **pp;
114
115   DISSECTOR_ASSERT(actx->stack);
116
117   par = wmem_new0(wmem_packet_scope(), asn1_par_t);
118
119   pp = &(actx->stack->par);
120   while (*pp)
121     pp = &((*pp)->next);
122   *pp = par;
123
124   return par;
125 }
126
127 void asn1_param_push_boolean(asn1_ctx_t *actx, gboolean value) {
128   asn1_par_t *par;
129
130   par = push_new_par(actx);
131   par->ptype = ASN1_PAR_BOOLEAN;
132   par->value.v_boolean = value;
133 }
134
135 void asn1_param_push_integer(asn1_ctx_t *actx, gint32 value) {
136   asn1_par_t *par;
137
138   par = push_new_par(actx);
139   par->ptype = ASN1_PAR_INTEGER;
140   par->value.v_integer = value;
141 }
142
143 gboolean asn1_param_get_boolean(asn1_ctx_t *actx, const gchar *name) {
144   asn1_par_t *par = NULL;
145
146   par = get_par_by_name(actx, name);
147   DISSECTOR_ASSERT(par);
148   return par->value.v_boolean;
149 }
150
151 gint32 asn1_param_get_integer(asn1_ctx_t *actx, const gchar *name) {
152   asn1_par_t *par = NULL;
153
154   par = get_par_by_name(actx, name);
155   DISSECTOR_ASSERT(par);
156   return par->value.v_integer;
157 }
158
159
160 /*--- ROSE ---*/
161
162 void rose_ctx_init(rose_ctx_t *rctx) {
163   memset(rctx, '\0', sizeof(*rctx));
164   rctx->signature = ROSE_CTX_SIGNATURE;
165 }
166
167 gboolean rose_ctx_check_signature(rose_ctx_t *rctx) {
168   return rctx && (rctx->signature == ROSE_CTX_SIGNATURE);
169 }
170
171 void rose_ctx_clean_data(rose_ctx_t *rctx) {
172   memset(&rctx->d, '\0', sizeof(rctx->d));
173   rctx->d.code = -1;
174 }
175
176 asn1_ctx_t *get_asn1_ctx(void *ptr) {
177   asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
178
179   if (!asn1_ctx_check_signature(actx))
180     actx = NULL;
181
182   return actx;
183 }
184
185 rose_ctx_t *get_rose_ctx(void *ptr) {
186   rose_ctx_t *rctx = (rose_ctx_t*)ptr;
187   asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
188
189   if (!asn1_ctx_check_signature(actx))
190     actx = NULL;
191
192   if (actx)
193     rctx = actx->rose_ctx;
194
195   if (!rose_ctx_check_signature(rctx))
196     rctx = NULL;
197
198   return rctx;
199 }
200
201 /** Only tested for BER */
202 double asn1_get_real(const guint8 *real_ptr, gint len) {
203   guint8 octet;
204   const guint8 *p;
205   guint8 *buf;
206   double val = 0;
207
208   /* 8.5.2    If the real value is the value zero,
209    *          there shall be no contents octets in the encoding.
210    */
211   if (len < 1) return val;
212
213   octet = real_ptr[0];
214   p = real_ptr + 1;
215   len -= 1;
216   if (octet & 0x80) {  /* binary encoding */
217     int i;
218     gboolean Eneg;
219     gint8 S; /* Sign */
220     guint8 B; /* Base */
221     guint8 F; /* scaling Factor */
222     gint32 E = 0; /* Exponent (supported max 3 octets/24 bit) */
223     guint64 N = 0; /* N (supported max 8 octets/64 bit) */
224
225     guint8 lenE, lenN;
226
227     if(octet & 0x40) S = -1; else S = 1;
228     switch(octet & 0x30) {
229       case 0x00: B = 2; break;
230       case 0x10: B = 8; break;
231       case 0x20: B = 16; break;
232       case 0x30: /* Reserved */
233       default:
234         /* TODO Add some warning in tree about reserved value for Base */
235         return 0;
236     }
237     F = (octet & 0x0c) >> 2;
238
239     /* 8.5.6.4 Exponent length */
240     lenE = (octet & 0x3) + 1;
241     if(lenE == 4)
242     {
243       /* we can't handle exponents > 24 bits */
244       /* TODO Next octet(s) define length of exponent */
245       DISSECTOR_ASSERT_NOT_REACHED();
246     }
247
248     Eneg = (*p) & 0x80 ? TRUE : FALSE;
249     for (i = 0; i < lenE; i++) {
250       if(Eneg) {
251         /* 2's complement: inverse bits */
252         E = (E<<8) | ((guint8) ~(*p));
253       } else {
254         E = (E<<8) | *p;
255       }
256       p++;
257     }
258     if(Eneg) {
259       /* 2's complement: ... and add 1 (and make negative of course) */
260       E = -(E + 1);
261     }
262
263     lenN = len - lenE;
264     if(lenN > 8)
265     {
266       /* we can't handle integers > 64 bits */
267       DISSECTOR_ASSERT_NOT_REACHED();
268     }
269     for (i=0; i<lenN; i++) {
270       N = (N<<8) | *p;
271       p++;
272     }
273     val = (double) S * N * pow(2, F) * pow(B, E);
274 #ifdef DEBUG
275     printf("S = %d, N = %lu, F = %u, B = %u, E = %d -> %f\n", S, N, F, B, E, val);
276 #endif
277   } else if (octet & 0x40) {  /* SpecialRealValue */
278     switch (octet & 0x3F) {
279       case 0x00: val = HUGE_VAL; break;
280       case 0x01: val = -HUGE_VAL; break;
281     }
282   } else {  /* decimal encoding */
283     buf = g_strndup(p, len);
284     val = g_ascii_strtod(buf, NULL);
285     g_free(buf);
286   }
287
288   return val;
289 }
290
291 /*
292  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
293  *
294  * Local Variables:
295  * c-basic-offset: 2
296  * tab-width: 8
297  * indent-tabs-mode: nil
298  * End:
299  *
300  * ex: set shiftwidth=2 tabstop=8 expandtab:
301  * :indentSize=2:tabSize=8:noTabs=true:
302  */