Debian Codegraph @

src/ber-dump.c

raw
1 /* ber-dump.c - Tool to dump BER encoded data
2 * Copyright (C) 2001 g10 Code GmbH
3 *
4 * This file is part of KSBA.
5 *
6 * KSBA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * KSBA is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdarg.h>
26
27 #include "visibility.h"
28 #include "ksba.h"
29 #include "ber-decoder.h"
30
31 #define PGMNAME "ber-dump"
32
33 #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
34 # define ATTR_PRINTF(a,b) __attribute__ ((format (printf,a,b)))
35 #else
36 # define ATTR_PRINTF(a,b)
37 #endif
38
39 /* keep track of parsing error */
40 static int error_counter;
41
42
43 static void print_error (const char *fmt, ... ) ATTR_PRINTF(1,2);
44
45
46
47 static void
48 print_error (const char *fmt, ... )
49 {
50 va_list arg_ptr ;
51
52 va_start (arg_ptr, fmt);
53 fputs (PGMNAME ": ", stderr);
54 vfprintf (stderr, fmt, arg_ptr);
55 va_end (arg_ptr);
56 error_counter++;
57 }
58
59 static void
60 fatal (const char *fmt, ... )
61 {
62 va_list arg_ptr ;
63
64 va_start (arg_ptr, fmt);
65 fputs (PGMNAME ": fatal: ", stderr);
66 vfprintf (stderr, fmt, arg_ptr);
67 va_end (arg_ptr);
68 exit (1);
69 }
70
71
72 static void
73 one_file (FILE *fp, const char *fname, ksba_asn_tree_t asn_tree)
74 {
75 gpg_error_t err;
76 ksba_reader_t r;
77 BerDecoder d;
78
79 (void)fname; /* Not yet used in error messages. */
80
81 err = ksba_reader_new (&r);
82 if (err)
83 fatal ("out of core\n");
84 err = ksba_reader_set_file (r, fp);
85 if (err)
86 fatal ("ksba_reader_set_file failed: rc=%d\n", err);
87
88 d = _ksba_ber_decoder_new ();
89 if (!d)
90 fatal ("out of core\n");
91 err = _ksba_ber_decoder_set_reader (d, r);
92 if (err)
93 fatal ("ksba_ber_decoder_set_reader failed: rc=%d\n", err);
94
95 if (asn_tree)
96 {
97 err = _ksba_ber_decoder_set_module (d, asn_tree);
98 if (err)
99 fatal ("ksba_ber_decoder_set_module failed: rc=%d\n", err);
100 }
101
102 err = _ksba_ber_decoder_dump (d, stdout);
103 if (err)
104 print_error ("ksba_ber_decoder_dump failed: rc=%d\n", err);
105
106 _ksba_ber_decoder_release (d);
107 ksba_reader_release (r);
108 }
109
110
111 static void
112 usage (int exitcode)
113 {
114 fputs ("usage: ber-dump [--module asnfile] [files]\n", stderr);
115 exit (exitcode);
116 }
117
118 int
119 main (int argc, char **argv)
120 {
121 const char *asnfile = NULL;
122 ksba_asn_tree_t asn_tree = NULL;
123 int rc;
124
125 if (!argc || (argc > 1 &&
126 (!strcmp (argv[1],"--help") || !strcmp (argv[1],"-h"))) )
127 usage (0);
128
129 argc--; argv++;
130 if (argc && !strcmp (*argv,"--module"))
131 {
132 argc--; argv++;
133 if (!argc)
134 usage (1);
135 asnfile = *argv;
136 argc--; argv++;
137 }
138
139 if (asnfile)
140 {
141 rc = ksba_asn_parse_file (asnfile, &asn_tree, 0);
142 if (rc)
143 {
144 print_error ("parsing `%s' failed: rc=%d\n", asnfile, rc);
145 exit (1);
146 }
147 }
148
149
150 if (!argc)
151 one_file (stdin, "-", asn_tree);
152 else
153 {
154 for (; argc; argc--, argv++)
155 {
156 FILE *fp;
157
158 fp = fopen (*argv, "r");
159 if (!fp)
160 print_error ("can't open `%s': %s\n", *argv, strerror (errno));
161 else
162 {
163 one_file (fp, *argv, asn_tree);
164 fclose (fp);
165 }
166 }
167 }
168
169 ksba_asn_tree_release (asn_tree);
170
171 return error_counter? 1:0;
172 }