C++ support.
[yaz-moved-to-github.git] / odr / dumpber.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dumpber.c,v $
7  * Revision 1.8  1997-05-14 06:53:57  adam
8  * C++ support.
9  *
10  * Revision 1.7  1996/03/08 14:38:41  quinn
11  * Fixed output.
12  *
13  * Revision 1.6  1996/01/19  15:41:34  quinn
14  * dumpber was ignoring the file argument.
15  *
16  * Revision 1.5  1995/10/18  16:12:55  quinn
17  * Better diagnostics. Added special case in NULL to handle WAIS server.
18  *
19  * Revision 1.4  1995/09/29  17:12:21  quinn
20  * Smallish
21  *
22  * Revision 1.3  1995/09/27  15:02:57  quinn
23  * Modified function heads & prototypes.
24  *
25  * Revision 1.2  1995/06/27  13:20:51  quinn
26  * Fixed sign-clash. Non-fatal warning
27  *
28  * Revision 1.1  1995/06/19  12:38:45  quinn
29  * Added BER dumper.
30  *
31  *
32  */
33
34 #include <odr.h>
35 #include <stdio.h>
36
37 static int do_dumpBER(FILE *f, char *buf, int len, int level, int offset)
38 {
39     int res, ll, zclass, tag, cons;
40     char *b = buf;
41     
42     if (!len)
43         return 0;
44     if (!buf[0] && !buf[1])
45         return 0;
46     if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons)) <= 0)
47         return 0;
48     if (res > len)
49     {
50         fprintf(stderr, "Unexpected end of buffer\n");
51         return 0;
52     }
53     fprintf(f, "%5d: %*s", offset, level * 4, "");
54     if (zclass == ODR_UNIVERSAL)
55     {
56         static char *nl[] =
57         {
58             "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
59             "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
60             "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
61             "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
62             "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
63             "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
64         };
65
66         if (tag < 28)
67             fprintf(f, "%s", nl[tag]);
68         else
69             fprintf(f, "[UNIV %d]", tag);
70     }
71     else if (zclass == ODR_CONTEXT)
72         fprintf(f, "[%d]", tag);
73     else
74         fprintf(f, "[%d:%d]", zclass, tag);
75     b += res;
76     len -= res;
77     if ((res = ber_declen((unsigned char*)b, &ll)) <= 0)
78     {
79         fprintf(f, "bad length\n");
80         return 0;
81     }
82     if (res > len)
83     {
84         fprintf(f, "Unexpected end of buffer\n");
85         return 0;
86     }
87     b += res;
88     len -= res;
89     if (ll >= 0)
90         fprintf(f, " len=%d\n", ll);
91     else
92         fprintf(f, " len=?\n");
93     if (!cons)
94     {
95         if (ll < 0)
96         {
97             fprintf(f, "Bad length on primitive type.\n");
98             return 0;
99         }
100         return ll + (b - buf);
101     }
102     if (ll >= 0)
103         len = ll;
104     /* constructed - cycle through children */
105     while ((ll == -1 && len >= 2) || (ll >= 0 && len))
106     {
107         if (ll == -1 && *b == 0 && *(b + 1) == 0)
108             break;
109         if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
110         {
111             fprintf(f, "Dump of content element failed.\n");
112             return 0;
113         }
114         b += res;
115         len -= res;
116     }
117     if (ll == -1)
118     {
119         if (len < 2)
120         {
121             fprintf(f, "Buffer too short in indefinite lenght.\n");
122             return 0;
123         }
124         return (b - buf) + 2;
125     }
126     return b - buf;
127 }
128
129 int odr_dumpBER(FILE *f, char *buf, int len)
130 {
131     return do_dumpBER(f, buf, len, 0, 0);
132 }