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