2 * Common routines for ASN.1
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
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.
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.
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.
32 #include <epan/emem.h>
33 #include <epan/packet.h>
37 void asn1_ctx_init(asn1_ctx_t *actx, asn1_enc_e encoding, gboolean aligned, packet_info *pinfo) {
38 memset(actx, '\0', sizeof(*actx));
39 actx->signature = ASN1_CTX_SIGNATURE;
40 actx->encoding = encoding;
41 actx->aligned = aligned;
45 gboolean asn1_ctx_check_signature(asn1_ctx_t *actx) {
46 return actx && (actx->signature == ASN1_CTX_SIGNATURE);
49 void asn1_ctx_clean_external(asn1_ctx_t *actx) {
50 memset(&actx->external, '\0', sizeof(actx->external));
51 actx->external.hf_index = -1;
52 actx->external.encoding = -1;
55 void asn1_ctx_clean_epdv(asn1_ctx_t *actx) {
56 memset(&actx->embedded_pdv, '\0', sizeof(actx->embedded_pdv));
57 actx->embedded_pdv.hf_index = -1;
58 actx->embedded_pdv.identification = -1;
62 /*--- stack/parameters ---*/
64 void asn1_stack_frame_push(asn1_ctx_t *actx, const gchar *name) {
65 asn1_stack_frame_t *frame;
67 frame = wmem_new0(wmem_packet_scope(), asn1_stack_frame_t);
69 frame->next = actx->stack;
73 void asn1_stack_frame_pop(asn1_ctx_t *actx, const gchar *name) {
74 DISSECTOR_ASSERT(actx->stack);
75 DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
76 actx->stack = actx->stack->next;
79 void asn1_stack_frame_check(asn1_ctx_t *actx, const gchar *name, const asn1_par_def_t *par_def) {
80 const asn1_par_def_t *pd = par_def;
83 DISSECTOR_ASSERT(actx->stack);
84 DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
86 par = actx->stack->par;
88 DISSECTOR_ASSERT(par);
89 DISSECTOR_ASSERT((pd->ptype == ASN1_PAR_IRR) || (par->ptype == pd->ptype));
94 DISSECTOR_ASSERT(!par);
97 static asn1_par_t *get_par_by_name(asn1_ctx_t *actx, const gchar *name) {
98 asn1_par_t *par = NULL;
100 DISSECTOR_ASSERT(actx->stack);
101 par = actx->stack->par;
103 if (!strcmp(par->name, name))
110 static asn1_par_t *push_new_par(asn1_ctx_t *actx) {
111 asn1_par_t *par, **pp;
113 DISSECTOR_ASSERT(actx->stack);
115 par = wmem_new0(wmem_packet_scope(), asn1_par_t);
117 pp = &(actx->stack->par);
125 void asn1_param_push_boolean(asn1_ctx_t *actx, gboolean value) {
128 par = push_new_par(actx);
129 par->ptype = ASN1_PAR_BOOLEAN;
130 par->value.v_boolean = value;
133 void asn1_param_push_integer(asn1_ctx_t *actx, gint32 value) {
136 par = push_new_par(actx);
137 par->ptype = ASN1_PAR_INTEGER;
138 par->value.v_integer = value;
141 gboolean asn1_param_get_boolean(asn1_ctx_t *actx, const gchar *name) {
142 asn1_par_t *par = NULL;
144 par = get_par_by_name(actx, name);
145 DISSECTOR_ASSERT(par);
146 return par->value.v_boolean;
149 gint32 asn1_param_get_integer(asn1_ctx_t *actx, const gchar *name) {
150 asn1_par_t *par = NULL;
152 par = get_par_by_name(actx, name);
153 DISSECTOR_ASSERT(par);
154 return par->value.v_integer;
160 void rose_ctx_init(rose_ctx_t *rctx) {
161 memset(rctx, '\0', sizeof(*rctx));
162 rctx->signature = ROSE_CTX_SIGNATURE;
165 gboolean rose_ctx_check_signature(rose_ctx_t *rctx) {
166 return rctx && (rctx->signature == ROSE_CTX_SIGNATURE);
169 void rose_ctx_clean_data(rose_ctx_t *rctx) {
170 memset(&rctx->d, '\0', sizeof(rctx->d));
174 asn1_ctx_t *get_asn1_ctx(void *ptr) {
175 asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
177 if (!asn1_ctx_check_signature(actx))
183 rose_ctx_t *get_rose_ctx(void *ptr) {
184 rose_ctx_t *rctx = (rose_ctx_t*)ptr;
185 asn1_ctx_t *actx = (asn1_ctx_t*)ptr;
187 if (!asn1_ctx_check_signature(actx))
191 rctx = actx->rose_ctx;
193 if (!rose_ctx_check_signature(rctx))
199 /** Only tested for BER */
200 double asn1_get_real(const guint8 *real_ptr, gint len) {
206 /* 8.5.2 If the real value is the value zero,
207 * there shall be no contents octets in the encoding.
209 if (len < 1) return val;
214 if (octet & 0x80) { /* binary encoding */
219 guint8 F; /* scaling Factor */
220 gint32 E = 0; /* Exponent (supported max 3 octets/24 bit) */
221 guint64 N = 0; /* N (supported max 8 octets/64 bit) */
225 if(octet & 0x40) S = -1; else S = 1;
226 switch(octet & 0x30) {
227 case 0x00: B = 2; break;
228 case 0x10: B = 8; break;
229 case 0x20: B = 16; break;
230 case 0x30: /* Reserved */
232 /* TODO Add some warning in tree about reserved value for Base */
235 F = (octet & 0x0c) >> 2;
237 /* 8.5.6.4 Exponent length */
238 lenE = (octet & 0x3) + 1;
241 /* we can't handle exponents > 24 bits */
242 /* TODO Next octet(s) define length of exponent */
243 DISSECTOR_ASSERT_NOT_REACHED();
246 Eneg = (*p) & 0x80 ? TRUE : FALSE;
247 for (i = 0; i < lenE; i++) {
249 /* 2's complement: inverse bits */
250 E = (E<<8) | ((guint8) ~(*p));
257 /* 2's complement: ... and add 1 (and make negative of course) */
264 /* we can't handle integers > 64 bits */
265 DISSECTOR_ASSERT_NOT_REACHED();
267 for (i=0; i<lenN; i++) {
271 val = (double) S * N * pow(2, F) * pow(B, E);
273 printf("S = %d, N = %lu, F = %u, B = %u, E = %d -> %f\n", S, N, F, B, E, Eneg, val);
275 } else if (octet & 0x40) { /* SpecialRealValue */
276 switch (octet & 0x3F) {
277 case 0x00: val = HUGE_VAL; break;
278 case 0x01: val = -HUGE_VAL; break;
280 } else { /* decimal encoding */
281 buf = g_strndup(p, len);
290 * Editor modelines - http://www.wireshark.org/tools/modelines.html
295 * indent-tabs-mode: nil
298 * ex: set shiftwidth=2 tabstop=8 expandtab:
299 * :indentSize=2:tabSize=8:noTabs=true: