1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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;
25 sprintf(level_str, "level=%-6d%*s", level, 18, "");
27 sprintf(level_str, "%*s", level * 2, "");
31 if (!buf[0] && !buf[1])
33 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
37 fprintf(f, "%5d: %s : Unexpected enf of buffer\n", offset, level_str);
40 fprintf(f, "%5d: %s", offset, level_str);
41 if (zclass == ODR_UNIVERSAL)
45 "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
46 "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
47 "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
48 "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
49 "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
50 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
53 if (tag >= 0 && tag < 28)
54 fprintf(f, "%s", nl[tag]);
56 fprintf(f, "[UNIV %d]", tag);
58 else if (zclass == ODR_CONTEXT)
59 fprintf(f, "[%d]", tag);
61 fprintf(f, "[%d:%d]", zclass, tag);
65 if ((res = ber_declen(b, &ll, len)) <= 0)
67 fprintf(f, "\n%sBad length\n", level_str);
74 fprintf(f, " len=%d", ll);
77 fprintf(f, " tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
80 if (ll < 0 || ll > len)
82 fprintf(f, "%sBad length on primitive type. ll=%d len=%d\n",
86 return ll + (b - buf);
92 fprintf(f, "%sBad length of constructed type ll=%d len=%d\n",
98 /* constructed - cycle through children */
99 while ((ll == -1 && len >= 2) || (ll >= 0 && len))
101 if (ll == -1 && *b == 0 && *(b + 1) == 0)
103 if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
105 fprintf(f, "%s Dump of content element failed\n", level_str);
112 fprintf(f, "%sBad length\n", level_str);
120 fprintf(f, "%sBuffer too short in indefinite length\n",
124 return (b - buf) + 2;
129 int odr_dumpBER(FILE *f, const char *buf, int len)
131 return do_dumpBER(f, buf, len, 0, 0);
136 * c-file-style: "Stroustrup"
137 * indent-tabs-mode: nil
139 * vim: shiftwidth=4 tabstop=8 expandtab