2 * Copyright (c) 1995-2003, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: dumpber.c,v 1.1 2003-10-27 12:21:30 adam Exp $
15 static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
17 int res, ll, zclass, tag, cons, lenlen, taglen;
18 const char *b = buf, *bp = buf;
22 if (!buf[0] && !buf[1])
24 if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons, len)) <= 0)
28 fprintf(stderr, "Unexpected end of buffer\n");
31 fprintf(f, "%5d: %*s", offset, level * 4, "");
32 if (zclass == ODR_UNIVERSAL)
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]"
44 if (tag >= 0 && tag < 28)
45 fprintf(f, "%s", nl[tag]);
47 fprintf(f, "[UNIV %d]", tag);
49 else if (zclass == ODR_CONTEXT)
50 fprintf(f, "[%d]", tag);
52 fprintf(f, "[%d:%d]", zclass, tag);
57 if ((res = ber_declen((unsigned char*)b, &ll, len)) <= 0)
59 fprintf(f, "\n%*sBad length\n", level*4+5, "");
66 fprintf(f, " len=%d", ll);
69 fprintf(f, " tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
72 if (ll < 0 || ll > len)
74 fprintf(f, "%*sBad length on primitive type. ll=%d len=%d\n",
75 level*4+5, "", ll, len);
78 return ll + (b - buf);
84 fprintf(f, "%*sBad length of constructed type ll=%d len=%d.\n",
85 level*4+5, "", ll, len);
90 /* constructed - cycle through children */
91 while ((ll == -1 && len >= 2) || (ll >= 0 && len))
93 if (ll == -1 && *b == 0 && *(b + 1) == 0)
95 if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
97 fprintf(f, "%*sDump of content element failed.\n", level*4+5, "");
104 fprintf(f, "%*sBad length\n", level*4+5, "");
112 fprintf(f, "%*sBuffer too short in indefinite length.\n",
116 return (b - buf) + 2;
121 int odr_dumpBER(FILE *f, const char *buf, int len)
123 return do_dumpBER(f, buf, len, 0, 0);