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