source: trunk/gsdl/packages/yaz/odr/dumpber.c@ 1343

Last change on this file since 1343 was 1343, checked in by johnmcp, 24 years ago

Added the YAZ toolkit source to the packages directory (for z39.50 stuff)

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/*
2 * Copyright (c) 1995-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
5 *
6 * $Log$
7 * Revision 1.1 2000/08/03 03:11:17 johnmcp
8 * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
9 *
10 * Revision 1.12 2000/02/29 13:44:55 adam
11 * Check for config.h (currently not generated).
12 *
13 * Revision 1.11 1999/11/30 13:47:11 adam
14 * Improved installation. Moved header files to include/yaz.
15 *
16 * Revision 1.10 1999/01/08 11:23:26 adam
17 * Added const modifier to some of the BER/ODR encoding routines.
18 *
19 * Revision 1.9 1998/01/14 09:53:26 quinn
20 * Added a bit more info to dump.
21 *
22 * Revision 1.8 1997/05/14 06:53:57 adam
23 * C++ support.
24 *
25 * Revision 1.7 1996/03/08 14:38:41 quinn
26 * Fixed output.
27 *
28 * Revision 1.6 1996/01/19 15:41:34 quinn
29 * dumpber was ignoring the file argument.
30 *
31 * Revision 1.5 1995/10/18 16:12:55 quinn
32 * Better diagnostics. Added special case in NULL to handle WAIS server.
33 *
34 * Revision 1.4 1995/09/29 17:12:21 quinn
35 * Smallish
36 *
37 * Revision 1.3 1995/09/27 15:02:57 quinn
38 * Modified function heads & prototypes.
39 *
40 * Revision 1.2 1995/06/27 13:20:51 quinn
41 * Fixed sign-clash. Non-fatal warning
42 *
43 * Revision 1.1 1995/06/19 12:38:45 quinn
44 * Added BER dumper.
45 *
46 *
47 */
48#if HAVE_CONFIG_H
49#include <config.h>
50#endif
51
52#include <stdio.h>
53#include <yaz/odr.h>
54
55static int do_dumpBER(FILE *f, const char *buf, int len, int level, int offset)
56{
57 int res, ll, zclass, tag, cons, lenlen, taglen;
58 const char *b = buf, *bp = buf;
59
60 if (!len)
61 return 0;
62 if (!buf[0] && !buf[1])
63 return 0;
64 if ((res = ber_dectag((unsigned char*)b, &zclass, &tag, &cons)) <= 0)
65 return 0;
66 if (res > len)
67 {
68 fprintf(stderr, "Unexpected end of buffer\n");
69 return 0;
70 }
71 fprintf(f, "%5d: %*s", offset, level * 4, "");
72 if (zclass == ODR_UNIVERSAL)
73 {
74 static char *nl[] =
75 {
76 "[Univ 0]", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
77 "NULL", "OID", "OBJECT DESCIPTOR", "EXTERNAL", "REAL",
78 "ENUM", "[UNIV 11]", "[UNIV 12]", "[UNIV 13]", "[UNIV 14]",
79 "[UNIV 15]", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING",
80 "[UNIV 20]", "[UNIV 21]", "[UNIV 22]", "[UNIV 23]", "[UNIV 24]",
81 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "[UNIV 28]"
82 };
83
84 if (tag < 28)
85 fprintf(f, "%s", nl[tag]);
86 else
87 fprintf(f, "[UNIV %d]", tag);
88 }
89 else if (zclass == ODR_CONTEXT)
90 fprintf(f, "[%d]", tag);
91 else
92 fprintf(f, "[%d:%d]", zclass, tag);
93 b += res;
94 taglen = res;
95 len -= res;
96 bp = b;
97 if ((res = ber_declen((unsigned char*)b, &ll)) <= 0)
98 {
99 fprintf(f, "bad length\n");
100 return 0;
101 }
102 if (res > len)
103 {
104 fprintf(f, "Unexpected end of buffer\n");
105 return 0;
106 }
107 lenlen = res;
108 b += res;
109 len -= res;
110 if (ll >= 0)
111 fprintf(f, " len=%d", ll);
112 else
113 fprintf(f, " len=?");
114 fprintf(f, " tl=%d, ll=%d\n", taglen, lenlen);
115 if (!cons)
116 {
117 if (ll < 0)
118 {
119 fprintf(f, "Bad length on primitive type.\n");
120 return 0;
121 }
122 return ll + (b - buf);
123 }
124 if (ll >= 0)
125 len = ll;
126 /* constructed - cycle through children */
127 while ((ll == -1 && len >= 2) || (ll >= 0 && len))
128 {
129 if (ll == -1 && *b == 0 && *(b + 1) == 0)
130 break;
131 if (!(res = do_dumpBER(f, b, len, level + 1, offset + (b - buf))))
132 {
133 fprintf(f, "Dump of content element failed.\n");
134 return 0;
135 }
136 b += res;
137 len -= res;
138 }
139 if (ll == -1)
140 {
141 if (len < 2)
142 {
143 fprintf(f, "Buffer too short in indefinite lenght.\n");
144 return 0;
145 }
146 return (b - buf) + 2;
147 }
148 return b - buf;
149}
150
151int odr_dumpBER(FILE *f, const char *buf, int len)
152{
153 return do_dumpBER(f, buf, len, 0, 0);
154}
Note: See TracBrowser for help on using the repository browser.