Better diagnostics. Added special case in NULL to handle WAIS server.
[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.5  1995-10-18 16:12:55  quinn
8  * Better diagnostics. Added special case in NULL to handle WAIS server.
9  *
10  * Revision 1.4  1995/09/29  17:12:21  quinn
11  * Smallish
12  *
13  * Revision 1.3  1995/09/27  15:02:57  quinn
14  * Modified function heads & prototypes.
15  *
16  * Revision 1.2  1995/06/27  13:20:51  quinn
17  * Fixed sign-clash. Non-fatal warning
18  *
19  * Revision 1.1  1995/06/19  12:38:45  quinn
20  * Added BER dumper.
21  *
22  *
23  */
24
25 #include <odr.h>
26 #include <stdio.h>
27
28 static int do_dumpBER(FILE *f, char *buf, int len, int level, int offset)
29 {
30     int res, ll, class, tag, cons;
31     char *b = buf;
32     
33     if (!len)
34         return 0;
35     if (!buf[0] && !buf[1])
36         return 0;
37     if ((res = ber_dectag((unsigned char*)b, &class, &tag, &cons)) <= 0)
38         return 0;
39     if (res > len)
40     {
41         fprintf(stderr, "Unexpected end of buffer\n");
42         return 0;
43     }
44     fprintf(stderr, "%5d: %*s", offset, level * 4, "");
45     if (class == ODR_UNIVERSAL)
46     {
47         static char *nl[] =
48         {
49             "Ugh", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
50             "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
51             "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
52             "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
53             "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
54             "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
55         };
56
57         if (tag < 28)
58             fprintf(stderr, "%s", nl[tag]);
59         else
60             fprintf(stderr, "[UNIV %d]", tag);
61     }
62     else if (class == ODR_CONTEXT)
63         fprintf(stderr, "[%d]", tag);
64     else
65         fprintf(stderr, "[%d:%d]", class, tag);
66     b += res;
67     len -= res;
68     if ((res = ber_declen((unsigned char*)b, &ll)) <= 0)
69     {
70         fprintf(stderr, "bad length\n");
71         return 0;
72     }
73     if (res > len)
74     {
75         fprintf(stderr, "Unexpected end of buffer\n");
76         return 0;
77     }
78     b += res;
79     len -= res;
80     if (ll >= 0)
81         fprintf(stderr, " len=%d\n", ll);
82     else
83         fprintf(stderr, " len=?\n");
84     if (!cons)
85     {
86         if (ll < 0)
87         {
88             fprintf(stderr, "Bad length on primitive type.\n");
89             return 0;
90         }
91         return ll + (b - buf);
92     }
93     if (ll >= 0)
94         len = ll;
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(stderr, "Dump of content element failed.\n");
103             return 0;
104         }
105         b += res;
106         len -= res;
107     }
108     if (ll == -1)
109     {
110         if (len < 2)
111         {
112             fprintf(stderr, "Buffer too short in indefinite lenght.\n");
113             return 0;
114         }
115         return (b - buf) + 2;
116     }
117     return b - buf;
118 }
119
120 int odr_dumpBER(FILE *f, char *buf, int len)
121 {
122     return do_dumpBER(f, buf, len, 0, 0);
123 }