Line data Source code
1 : /* run-identify - Helper to run the identify command
2 : * Copyright (C) 2016 g10 Code GmbH
3 : *
4 : * This file is part of GPGME.
5 : *
6 : * GPGME is free software; you can redistribute it and/or modify it
7 : * under the terms of the GNU Lesser General Public License as
8 : * published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * GPGME is distributed in the hope that it will be useful, but
12 : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 : */
19 :
20 : /* We need to include config.h so that we know whether we are building
21 : with large file system (LFS) support. */
22 : #ifdef HAVE_CONFIG_H
23 : #include <config.h>
24 : #endif
25 :
26 : #include <stdlib.h>
27 : #include <stdio.h>
28 : #include <string.h>
29 :
30 : #include <gpgme.h>
31 :
32 : #define PGM "run-identify"
33 :
34 : #include "run-support.h"
35 :
36 :
37 : static int verbose;
38 :
39 :
40 : static const char *
41 0 : data_type_to_string (gpgme_data_type_t dt)
42 : {
43 0 : const char *s = "[?]";
44 :
45 0 : switch (dt)
46 : {
47 0 : case GPGME_DATA_TYPE_INVALID : s = "invalid"; break;
48 0 : case GPGME_DATA_TYPE_UNKNOWN : s = "unknown"; break;
49 0 : case GPGME_DATA_TYPE_PGP_SIGNED : s = "PGP-signed"; break;
50 0 : case GPGME_DATA_TYPE_PGP_SIGNATURE: s = "PGP-signature"; break;
51 0 : case GPGME_DATA_TYPE_PGP_ENCRYPTED: s = "PGP-encrypted"; break;
52 0 : case GPGME_DATA_TYPE_PGP_OTHER : s = "PGP"; break;
53 0 : case GPGME_DATA_TYPE_PGP_KEY : s = "PGP-key"; break;
54 0 : case GPGME_DATA_TYPE_CMS_SIGNED : s = "CMS-signed"; break;
55 0 : case GPGME_DATA_TYPE_CMS_ENCRYPTED: s = "CMS-encrypted"; break;
56 0 : case GPGME_DATA_TYPE_CMS_OTHER : s = "CMS"; break;
57 0 : case GPGME_DATA_TYPE_X509_CERT : s = "X.509"; break;
58 0 : case GPGME_DATA_TYPE_PKCS12 : s = "PKCS12"; break;
59 : }
60 0 : return s;
61 : }
62 :
63 :
64 : static int
65 0 : show_usage (int ex)
66 : {
67 0 : fputs ("usage: " PGM " [options] FILENAMEs\n\n"
68 : "Options:\n"
69 : " --verbose run in verbose mode\n"
70 : , stderr);
71 0 : exit (ex);
72 : }
73 :
74 : int
75 0 : main (int argc, char **argv)
76 : {
77 0 : int last_argc = -1;
78 : gpgme_error_t err;
79 0 : int anyerr = 0;
80 : gpgme_data_t data;
81 : gpgme_data_type_t dt;
82 :
83 0 : if (argc)
84 0 : { argc--; argv++; }
85 0 : while (argc && last_argc != argc )
86 : {
87 0 : last_argc = argc;
88 0 : if (!strcmp (*argv, "--"))
89 : {
90 0 : argc--; argv++;
91 0 : break;
92 : }
93 0 : else if (!strcmp (*argv, "--help"))
94 0 : show_usage (0);
95 0 : else if (!strcmp (*argv, "--verbose"))
96 : {
97 0 : verbose = 1;
98 0 : argc--; argv++;
99 : }
100 0 : else if (!strncmp (*argv, "--", 2))
101 0 : show_usage (1);
102 :
103 : }
104 :
105 0 : init_gpgme_basic ();
106 :
107 0 : for (; argc; argc--, argv++)
108 : {
109 0 : if (verbose)
110 0 : printf ("reading file `%s'\n", *argv);
111 0 : err = gpgme_data_new_from_file (&data, *argv, 1);
112 0 : if (err)
113 : {
114 0 : fprintf (stderr, PGM ": error reading '%s': %s\n",
115 : *argv, gpg_strerror (err));
116 0 : anyerr = 1;
117 : }
118 : else
119 : {
120 0 : dt = gpgme_data_identify (data, 0);
121 0 : if (dt == GPGME_DATA_TYPE_INVALID)
122 0 : anyerr = 1;
123 0 : printf ("%s: %s\n", *argv, data_type_to_string (dt));
124 0 : gpgme_data_release (data);
125 : }
126 : }
127 :
128 0 : return anyerr;
129 : }
|