X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=odr%2Fdumpber.c;h=b332f9ab9aaa31c7c7e57f825909cbb2247e1dfd;hb=006c3df264a95c03bfc557b2004595fc6cc3ee14;hp=7acc3aa1f013a25b27126be5ebf96c79317bb9bd;hpb=3111fa3db35d0d3b76ba0f70a2c9ed83d5114c11;p=yaz-moved-to-github.git diff --git a/odr/dumpber.c b/odr/dumpber.c index 7acc3aa..b332f9a 100644 --- a/odr/dumpber.c +++ b/odr/dumpber.c @@ -1,46 +1,27 @@ /* - * Copyright (c) 1995, Index Data. + * Copyright (c) 1995-2003, Index Data. * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Log: dumpber.c,v $ - * Revision 1.7 1996-03-08 14:38:41 quinn - * Fixed output. - * - * Revision 1.6 1996/01/19 15:41:34 quinn - * dumpber was ignoring the file argument. - * - * Revision 1.5 1995/10/18 16:12:55 quinn - * Better diagnostics. Added special case in NULL to handle WAIS server. - * - * Revision 1.4 1995/09/29 17:12:21 quinn - * Smallish - * - * Revision 1.3 1995/09/27 15:02:57 quinn - * Modified function heads & prototypes. - * - * Revision 1.2 1995/06/27 13:20:51 quinn - * Fixed sign-clash. Non-fatal warning - * - * Revision 1.1 1995/06/19 12:38:45 quinn - * Added BER dumper. - * - * + * $Id: dumpber.c,v 1.16 2003-04-24 12:48:47 adam Exp $ */ +#if HAVE_CONFIG_H +#include +#endif -#include #include +#include "odr-priv.h" -static int do_dumpBER(FILE *f, char *buf, int len, int level, int offset) +static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset) { - int res, ll, class, tag, cons; - char *b = buf; + int res, ll, zclass, tag, cons, lenlen, taglen; + const char *b = buf, *bp = buf; if (!len) return 0; if (!buf[0] && !buf[1]) return 0; - if ((res = ber_dectag((unsigned char*)b, &class, &tag, &cons)) <= 0) + if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons, len)) <= 0) return 0; if (res > len) { @@ -48,7 +29,7 @@ static int do_dumpBER(FILE *f, char *buf, int len, int level, int offset) return 0; } fprintf(f, "%5d: %*s", offset, level * 4, ""); - if (class == ODR_UNIVERSAL) + if (zclass == ODR_UNIVERSAL) { static char *nl[] = { @@ -60,44 +41,52 @@ static int do_dumpBER(FILE *f, char *buf, int len, int level, int offset) "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]" }; - if (tag < 28) + if (tag >= 0 && tag < 28) fprintf(f, "%s", nl[tag]); else fprintf(f, "[UNIV %d]", tag); } - else if (class == ODR_CONTEXT) + else if (zclass == ODR_CONTEXT) fprintf(f, "[%d]", tag); else - fprintf(f, "[%d:%d]", class, tag); + fprintf(f, "[%d:%d]", zclass, tag); b += res; + taglen = res; len -= res; - if ((res = ber_declen((unsigned char*)b, &ll)) <= 0) + bp = b; + if ((res = ber_declen((unsigned char*)b, &ll, len)) <= 0) { - fprintf(f, "bad length\n"); - return 0; - } - if (res > len) - { - fprintf(f, "Unexpected end of buffer\n"); + fprintf(f, "\n%*sBad length\n", level*4+5, ""); return 0; } + lenlen = res; b += res; len -= res; if (ll >= 0) - fprintf(f, " len=%d\n", ll); + fprintf(f, " len=%d", ll); else - fprintf(f, " len=?\n"); + fprintf(f, " len=?"); + fprintf(f, " tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons); if (!cons) { - if (ll < 0) + if (ll < 0 || ll > len) { - fprintf(f, "Bad length on primitive type.\n"); + fprintf(f, "%*sBad length on primitive type. ll=%d len=%d\n", + level*4+5, "", ll, len); return 0; } return ll + (b - buf); } if (ll >= 0) + { + if (ll > len) + { + fprintf(f, "%*sBad length of constructed type ll=%d len=%d.\n", + level*4+5, "", ll, len); + return 0; + } len = ll; + } /* constructed - cycle through children */ while ((ll == -1 && len >= 2) || (ll >= 0 && len)) { @@ -105,17 +94,23 @@ static int do_dumpBER(FILE *f, char *buf, int len, int level, int offset) break; if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf)))) { - fprintf(f, "Dump of content element failed.\n"); + fprintf(f, "%*sDump of content element failed.\n", level*4+5, ""); return 0; } b += res; len -= res; + if (len < 0) + { + fprintf(f, "%*sBad length\n", level*4+5, ""); + return 0; + } } if (ll == -1) { if (len < 2) { - fprintf(f, "Buffer too short in indefinite lenght.\n"); + fprintf(f, "%*sBuffer too short in indefinite length.\n", + level*4+5, ""); return 0; } return (b - buf) + 2; @@ -123,7 +118,7 @@ static int do_dumpBER(FILE *f, char *buf, int len, int level, int offset) return b - buf; } -int odr_dumpBER(FILE *f, char *buf, int len) +int odr_dumpBER(FILE *f, const char *buf, int len) { return do_dumpBER(f, buf, len, 0, 0); }