Fix sample PQF
[yaz-moved-to-github.git] / odr / dumpber.c
1 /*
2  * Copyright (c) 1995-2003, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: dumpber.c,v 1.16 2003-04-24 12:48:47 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, len)) <= 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 >= 0 && 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, len)) <= 0)
58     {
59         fprintf(f, "\n%*sBad length\n", level*4+5, "");
60         return 0;
61     }
62     lenlen = res;
63     b += res;
64     len -= res;
65     if (ll >= 0)
66         fprintf(f, " len=%d", ll);
67     else
68         fprintf(f, " len=?");
69     fprintf(f, "       tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
70     if (!cons)
71     {
72         if (ll < 0 || ll > len)
73         {
74             fprintf(f, "%*sBad length on primitive type. ll=%d len=%d\n",
75                     level*4+5, "", ll, len);
76             return 0;
77         }
78         return ll + (b - buf);
79     }
80     if (ll >= 0)
81     {
82         if (ll > len)
83         {
84             fprintf(f, "%*sBad length of constructed type ll=%d len=%d.\n",
85                     level*4+5, "", ll, len);
86             return 0;
87         }
88         len = ll;
89     }
90     /* constructed - cycle through children */
91     while ((ll == -1 && len >= 2) || (ll >= 0 && len))
92     {
93         if (ll == -1 && *b == 0 && *(b + 1) == 0)
94             break;
95         if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
96         {
97             fprintf(f, "%*sDump of content element failed.\n", level*4+5, "");
98             return 0;
99         }
100         b += res;
101         len -= res;
102         if (len < 0)
103         {
104             fprintf(f, "%*sBad length\n", level*4+5, "");
105             return 0;
106         }
107     }
108     if (ll == -1)
109     {
110         if (len < 2)
111         {
112             fprintf(f, "%*sBuffer too short in indefinite length.\n",
113                     level*4+5, "");
114             return 0;
115         }
116         return (b - buf) + 2;
117     }
118     return b - buf;
119 }
120
121 int odr_dumpBER(FILE *f, const char *buf, int len)
122 {
123     return do_dumpBER(f, buf, len, 0, 0);
124 }