Reinsert initialiser for __UNUSED_loglevel
[yaz-moved-to-github.git] / src / dumpber.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: dumpber.c,v 1.4 2005-06-25 15:46:04 adam Exp $
6  */
7
8 /**
9  * \file dumpber.c
10  * \brief Implements BER dumping
11  */
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <stdio.h>
18 #include "odr-priv.h"
19
20 static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
21 {
22     int res, ll, zclass, tag, cons, lenlen, taglen;
23     const char *b = buf, *bp = buf;
24     
25     if (!len)
26         return 0;
27     if (!buf[0] && !buf[1])
28         return 0;
29     if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons, len)) <= 0)
30         return 0;
31     if (res > len)
32     {
33         fprintf(stderr, "Unexpected end of buffer\n");
34         return 0;
35     }
36     fprintf(f, "%5d: %*s", offset, level * 4, "");
37     if (zclass == ODR_UNIVERSAL)
38     {
39         static char *nl[] =
40         {
41             "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
42             "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
43             "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
44             "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
45             "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
46             "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
47         };
48
49         if (tag >= 0 && tag < 28)
50             fprintf(f, "%s", nl[tag]);
51         else
52             fprintf(f, "[UNIV %d]", tag);
53     }
54     else if (zclass == ODR_CONTEXT)
55         fprintf(f, "[%d]", tag);
56     else
57         fprintf(f, "[%d:%d]", zclass, tag);
58     b += res;
59     taglen = res;
60     len -= res;
61     bp = b;
62     if ((res = ber_declen((unsigned char*)b, &ll, len)) <= 0)
63     {
64         fprintf(f, "\n%*sBad length\n", level*4+5, "");
65         return 0;
66     }
67     lenlen = res;
68     b += res;
69     len -= res;
70     if (ll >= 0)
71         fprintf(f, " len=%d", ll);
72     else
73         fprintf(f, " len=?");
74     fprintf(f, "       tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
75     if (!cons)
76     {
77         if (ll < 0 || ll > len)
78         {
79             fprintf(f, "%*sBad length on primitive type. ll=%d len=%d\n",
80                     level*4+5, "", ll, len);
81             return 0;
82         }
83         return ll + (b - buf);
84     }
85     if (ll >= 0)
86     {
87         if (ll > len)
88         {
89             fprintf(f, "%*sBad length of constructed type ll=%d len=%d.\n",
90                     level*4+5, "", ll, len);
91             return 0;
92         }
93         len = ll;
94     }
95     /* constructed - cycle through children */
96     while ((ll == -1 && len >= 2) || (ll >= 0 && len))
97     {
98         if (ll == -1 && *b == 0 && *(b + 1) == 0)
99             break;
100         if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
101         {
102             fprintf(f, "%*sDump of content element failed.\n", level*4+5, "");
103             return 0;
104         }
105         b += res;
106         len -= res;
107         if (len < 0)
108         {
109             fprintf(f, "%*sBad length\n", level*4+5, "");
110             return 0;
111         }
112     }
113     if (ll == -1)
114     {
115         if (len < 2)
116         {
117             fprintf(f, "%*sBuffer too short in indefinite length.\n",
118                     level*4+5, "");
119             return 0;
120         }
121         return (b - buf) + 2;
122     }
123     return b - buf;
124 }
125
126 int odr_dumpBER(FILE *f, const char *buf, int len)
127 {
128     return do_dumpBER(f, buf, len, 0, 0);
129 }
130 /*
131  * Local variables:
132  * c-basic-offset: 4
133  * indent-tabs-mode: nil
134  * End:
135  * vim: shiftwidth=4 tabstop=8 expandtab
136  */
137