Line data Source code
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 0 : print_error (const char *fmt, ... )
49 : {
50 : va_list arg_ptr ;
51 :
52 0 : va_start (arg_ptr, fmt);
53 0 : fputs (PGMNAME ": ", stderr);
54 0 : vfprintf (stderr, fmt, arg_ptr);
55 0 : va_end (arg_ptr);
56 0 : error_counter++;
57 0 : }
58 :
59 : static void
60 0 : fatal (const char *fmt, ... )
61 : {
62 : va_list arg_ptr ;
63 :
64 0 : va_start (arg_ptr, fmt);
65 0 : fputs (PGMNAME ": fatal: ", stderr);
66 0 : vfprintf (stderr, fmt, arg_ptr);
67 0 : va_end (arg_ptr);
68 0 : exit (1);
69 : }
70 :
71 :
72 : static void
73 0 : 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 0 : err = ksba_reader_new (&r);
82 0 : if (err)
83 0 : fatal ("out of core\n");
84 0 : err = ksba_reader_set_file (r, fp);
85 0 : if (err)
86 0 : fatal ("ksba_reader_set_file failed: rc=%d\n", err);
87 :
88 0 : d = _ksba_ber_decoder_new ();
89 0 : if (!d)
90 0 : fatal ("out of core\n");
91 0 : err = _ksba_ber_decoder_set_reader (d, r);
92 0 : if (err)
93 0 : fatal ("ksba_ber_decoder_set_reader failed: rc=%d\n", err);
94 :
95 0 : if (asn_tree)
96 : {
97 0 : err = _ksba_ber_decoder_set_module (d, asn_tree);
98 0 : if (err)
99 0 : fatal ("ksba_ber_decoder_set_module failed: rc=%d\n", err);
100 : }
101 :
102 0 : err = _ksba_ber_decoder_dump (d, stdout);
103 0 : if (err)
104 0 : print_error ("ksba_ber_decoder_dump failed: rc=%d\n", err);
105 :
106 0 : _ksba_ber_decoder_release (d);
107 0 : ksba_reader_release (r);
108 0 : }
109 :
110 :
111 : static void
112 0 : usage (int exitcode)
113 : {
114 0 : fputs ("usage: ber-dump [--module asnfile] [files]\n", stderr);
115 0 : exit (exitcode);
116 : }
117 :
118 : int
119 0 : main (int argc, char **argv)
120 : {
121 0 : const char *asnfile = NULL;
122 0 : ksba_asn_tree_t asn_tree = NULL;
123 : int rc;
124 :
125 0 : if (!argc || (argc > 1 &&
126 0 : (!strcmp (argv[1],"--help") || !strcmp (argv[1],"-h"))) )
127 0 : usage (0);
128 :
129 0 : argc--; argv++;
130 0 : if (argc && !strcmp (*argv,"--module"))
131 : {
132 0 : argc--; argv++;
133 0 : if (!argc)
134 0 : usage (1);
135 0 : asnfile = *argv;
136 0 : argc--; argv++;
137 : }
138 :
139 0 : if (asnfile)
140 : {
141 0 : rc = ksba_asn_parse_file (asnfile, &asn_tree, 0);
142 0 : if (rc)
143 : {
144 0 : print_error ("parsing `%s' failed: rc=%d\n", asnfile, rc);
145 0 : exit (1);
146 : }
147 : }
148 :
149 :
150 0 : if (!argc)
151 0 : one_file (stdin, "-", asn_tree);
152 : else
153 : {
154 0 : for (; argc; argc--, argv++)
155 : {
156 : FILE *fp;
157 :
158 0 : fp = fopen (*argv, "r");
159 0 : if (!fp)
160 0 : print_error ("can't open `%s': %s\n", *argv, strerror (errno));
161 : else
162 : {
163 0 : one_file (fp, *argv, asn_tree);
164 0 : fclose (fp);
165 : }
166 : }
167 : }
168 :
169 0 : ksba_asn_tree_release (asn_tree);
170 :
171 0 : return error_counter? 1:0;
172 : }
|