2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: dumpber.c,v 1.4 2005-06-25 15:46:04 adam Exp $
10 * \brief Implements BER dumping
20 static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
22 int res, ll, zclass, tag, cons, lenlen, taglen;
23 const char *b = buf, *bp = buf;
27 if (!buf[0] && !buf[1])
29 if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons, len)) <= 0)
33 fprintf(stderr, "Unexpected end of buffer\n");
36 fprintf(f, "%5d: %*s", offset, level * 4, "");
37 if (zclass == ODR_UNIVERSAL)
41 "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
42 "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
43 "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
44 "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
45 "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
46 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
49 if (tag >= 0 && tag < 28)
50 fprintf(f, "%s", nl[tag]);
52 fprintf(f, "[UNIV %d]", tag);
54 else if (zclass == ODR_CONTEXT)
55 fprintf(f, "[%d]", tag);
57 fprintf(f, "[%d:%d]", zclass, tag);
62 if ((res = ber_declen((unsigned char*)b, &ll, len)) <= 0)
64 fprintf(f, "\n%*sBad length\n", level*4+5, "");
71 fprintf(f, " len=%d", ll);
74 fprintf(f, " tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
77 if (ll < 0 || ll > len)
79 fprintf(f, "%*sBad length on primitive type. ll=%d len=%d\n",
80 level*4+5, "", ll, len);
83 return ll + (b - buf);
89 fprintf(f, "%*sBad length of constructed type ll=%d len=%d.\n",
90 level*4+5, "", ll, len);
95 /* constructed - cycle through children */
96 while ((ll == -1 && len >= 2) || (ll >= 0 && len))
98 if (ll == -1 && *b == 0 && *(b + 1) == 0)
100 if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
102 fprintf(f, "%*sDump of content element failed.\n", level*4+5, "");
109 fprintf(f, "%*sBad length\n", level*4+5, "");
117 fprintf(f, "%*sBuffer too short in indefinite length.\n",
121 return (b - buf) + 2;
126 int odr_dumpBER(FILE *f, const char *buf, int len)
128 return do_dumpBER(f, buf, len, 0, 0);
133 * indent-tabs-mode: nil
135 * vim: shiftwidth=4 tabstop=8 expandtab