1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements BER dumping
18 static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
20 int res, ll, zclass, tag, cons, lenlen, taglen;
21 const char *b = buf, *bp = buf;
25 if (!buf[0] && !buf[1])
27 if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons, len)) <= 0)
31 fprintf(stderr, "Unexpected end of buffer\n");
34 fprintf(f, "%5d: %*s", offset, level * 4, "");
35 if (zclass == ODR_UNIVERSAL)
39 "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
40 "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
41 "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
42 "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
43 "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
44 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
47 if (tag >= 0 && tag < 28)
48 fprintf(f, "%s", nl[tag]);
50 fprintf(f, "[UNIV %d]", tag);
52 else if (zclass == ODR_CONTEXT)
53 fprintf(f, "[%d]", tag);
55 fprintf(f, "[%d:%d]", zclass, tag);
60 if ((res = ber_declen((unsigned char*)b, &ll, len)) <= 0)
62 fprintf(f, "\n%*sBad length\n", level*4+5, "");
69 fprintf(f, " len=%d", ll);
72 fprintf(f, " tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
75 if (ll < 0 || ll > len)
77 fprintf(f, "%*sBad length on primitive type. ll=%d len=%d\n",
78 level*4+5, "", ll, len);
81 return ll + (b - buf);
87 fprintf(f, "%*sBad length of constructed type ll=%d len=%d.\n",
88 level*4+5, "", ll, len);
93 /* constructed - cycle through children */
94 while ((ll == -1 && len >= 2) || (ll >= 0 && len))
96 if (ll == -1 && *b == 0 && *(b + 1) == 0)
98 if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
100 fprintf(f, "%*sDump of content element failed.\n", level*4+5, "");
107 fprintf(f, "%*sBad length\n", level*4+5, "");
115 fprintf(f, "%*sBuffer too short in indefinite length.\n",
119 return (b - buf) + 2;
124 int odr_dumpBER(FILE *f, const char *buf, int len)
126 return do_dumpBER(f, buf, len, 0, 0);
131 * c-file-style: "Stroustrup"
132 * indent-tabs-mode: nil
134 * vim: shiftwidth=4 tabstop=8 expandtab