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.14 2003-01-06 08:20:27 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)) <= 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]"
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)) <= 0)
59 fprintf(f, "bad length\n");
64 fprintf(f, "Unexpected end of buffer\n");
71 fprintf(f, " len=%d", ll);
74 fprintf(f, " tl=%d, ll=%d\n", taglen, lenlen);
79 fprintf(f, "Bad length on primitive type.\n");
82 return ll + (b - buf);
86 /* constructed - cycle through children */
87 while ((ll == -1 && len >= 2) || (ll >= 0 && len))
89 if (ll == -1 && *b == 0 && *(b + 1) == 0)
91 if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
93 fprintf(f, "Dump of content element failed.\n");
103 fprintf(f, "Buffer too short in indefinite lenght.\n");
106 return (b - buf) + 2;
111 int odr_dumpBER(FILE *f, const char *buf, int len)
113 return do_dumpBER(f, buf, len, 0, 0);