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