Add Zthes tag-set -- where was it?!
[yaz-moved-to-github.git] / odr / dumpber.c
1 /*
2  * Copyright (c) 1995-2002, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: dumpber.c,v 1.13 2002-07-25 12:51:08 adam Exp $
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include "odr-priv.h"
14
15 static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
16 {
17     int res, ll, zclass, tag, cons, lenlen, taglen;
18     const char *b = buf, *bp = buf;
19     
20     if (!len)
21         return 0;
22     if (!buf[0] && !buf[1])
23         return 0;
24     if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons)) <= 0)
25         return 0;
26     if (res > len)
27     {
28         fprintf(stderr, "Unexpected end of buffer\n");
29         return 0;
30     }
31     fprintf(f, "%5d: %*s", offset, level * 4, "");
32     if (zclass == ODR_UNIVERSAL)
33     {
34         static char *nl[] =
35         {
36             "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
37             "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
38             "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
39             "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
40             "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
41             "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
42         };
43
44         if (tag < 28)
45             fprintf(f, "%s", nl[tag]);
46         else
47             fprintf(f, "[UNIV %d]", tag);
48     }
49     else if (zclass == ODR_CONTEXT)
50         fprintf(f, "[%d]", tag);
51     else
52         fprintf(f, "[%d:%d]", zclass, tag);
53     b += res;
54     taglen = res;
55     len -= res;
56     bp = b;
57     if ((res = ber_declen((unsigned char*)b, &ll)) <= 0)
58     {
59         fprintf(f, "bad length\n");
60         return 0;
61     }
62     if (res > len)
63     {
64         fprintf(f, "Unexpected end of buffer\n");
65         return 0;
66     }
67     lenlen = res;
68     b += res;
69     len -= res;
70     if (ll >= 0)
71         fprintf(f, " len=%d", ll);
72     else
73         fprintf(f, " len=?");
74     fprintf(f, "       tl=%d, ll=%d\n", taglen, lenlen);
75     if (!cons)
76     {
77         if (ll < 0)
78         {
79             fprintf(f, "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, offset + (b - buf))))
92         {
93             fprintf(f, "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(f, "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, const char *buf, int len)
112 {
113     return do_dumpBER(f, buf, len, 0, 0);
114 }