YAZ 5.37.0
dumpber.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5
10
11#if HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15#include <stdio.h>
16#include <yaz/snprintf.h>
17#include "odr-priv.h"
18
19static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
20{
21 int res, ll, zclass, tag, cons, lenlen, taglen;
22 const char *b = buf;
23 char level_str[80];
24
25 if (level >= 15)
26 yaz_snprintf(level_str, sizeof level_str, "level=%-6d%*s", level, 18, "");
27 else
28 yaz_snprintf(level_str, sizeof level_str, "%*s", level * 2, "");
29
30 if (!len)
31 return 0;
32 if (!buf[0] && !buf[1])
33 return 0;
34 if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
35 return 0;
36 if (res > len)
37 {
38 fprintf(f, "%5d: %s : Unexpected end of buffer\n", offset, level_str);
39 return 0;
40 }
41 fprintf(f, "%5d: %s", offset, level_str);
42 if (zclass == ODR_UNIVERSAL)
43 {
44 static char *nl[] =
45 {
46 "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
47 "NULL", "OID", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
48 "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
49 "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
50 "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
51 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
52 };
53
54 if (tag >= 0 && tag < 28)
55 fprintf(f, "%s", nl[tag]);
56 else
57 fprintf(f, "[UNIV %d]", tag);
58 }
59 else if (zclass == ODR_CONTEXT)
60 fprintf(f, "[%d]", tag);
61 else
62 fprintf(f, "[%d:%d]", zclass, tag);
63 b += res;
64 taglen = res;
65 len -= res;
66 if ((res = ber_declen(b, &ll, len)) <= 0)
67 {
68 fprintf(f, "\n%sBad length\n", level_str);
69 return 0;
70 }
71 lenlen = res;
72 b += res;
73 len -= res;
74 if (ll >= 0)
75 fprintf(f, " len=%d", ll);
76 else
77 fprintf(f, " len=?");
78 fprintf(f, " tl=%d, ll=%d cons=%d\n", taglen, lenlen, cons);
79 if (!cons)
80 {
81 if (ll < 0 || ll > len)
82 {
83 fprintf(f, "%sBad length on primitive type. ll=%d len=%d\n",
84 level_str, ll, len);
85 return 0;
86 }
87 return ll + (b - buf);
88 }
89 if (ll >= 0)
90 {
91 if (ll > len)
92 {
93 fprintf(f, "%sBad length of constructed type ll=%d len=%d\n",
94 level_str, ll, len);
95 return 0;
96 }
97 len = ll;
98 }
99 /* constructed - cycle through children */
100 while ((ll == -1 && len >= 2) || (ll >= 0 && len))
101 {
102 if (ll == -1 && *b == 0 && *(b + 1) == 0)
103 break;
104 if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
105 {
106 fprintf(f, "%s Dump of content element failed\n", level_str);
107 return 0;
108 }
109 b += res;
110 len -= res;
111 if (len < 0)
112 {
113 fprintf(f, "%sBad length\n", level_str);
114 return 0;
115 }
116 }
117 if (ll == -1)
118 {
119 if (len < 2)
120 {
121 fprintf(f, "%sBuffer too short in indefinite length\n",
122 level_str);
123 return 0;
124 }
125 return (b - buf) + 2;
126 }
127 return b - buf;
128}
129
130int odr_dumpBER(FILE *f, const char *buf, int len)
131{
132 return do_dumpBER(f, buf, len, 0, 0);
133}
134/*
135 * Local variables:
136 * c-basic-offset: 4
137 * c-file-style: "Stroustrup"
138 * indent-tabs-mode: nil
139 * End:
140 * vim: shiftwidth=4 tabstop=8 expandtab
141 */
142
int ber_declen(const char *buf, int *len, int max)
Definition ber_len.c:93
int ber_dectag(const char *cp, int *zclass, int *tag, int *constructed, int max)
Decodes BER identifier octets.
Definition ber_tag.c:165
static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
Definition dumpber.c:19
int odr_dumpBER(FILE *f, const char *buf, int len)
Definition dumpber.c:130
Internal ODR definitions.
#define ODR_UNIVERSAL
Definition odr.h:65
#define ODR_CONTEXT
Definition odr.h:67
void yaz_snprintf(char *buf, size_t size, const char *fmt,...)
Definition snprintf.c:31
Header for config file reading utilities.