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