chiark / gitweb /
gpg agent threading bugs: Add some `xxx' comments.
[gnupg2.git] / g10 / dearmor.c
1 /* dearmor.c - Armor utility
2  * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG 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  * GnuPG 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 <https://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
26 #include "gpg.h"
27 #include "status.h"
28 #include "iobuf.h"
29 #include "util.h"
30 #include "filter.h"
31 #include "packet.h"
32 #include "options.h"
33 #include "main.h"
34 #include "i18n.h"
35
36 /****************
37  * Take an armor file and write it out without armor
38  */
39 int
40 dearmor_file( const char *fname )
41 {
42     armor_filter_context_t *afx;
43     IOBUF inp = NULL, out = NULL;
44     int rc = 0;
45     int c;
46
47     afx = new_armor_context ();
48
49     /* prepare iobufs */
50     inp = iobuf_open(fname);
51     if (inp && is_secured_file (iobuf_get_fd (inp)))
52       {
53         iobuf_close (inp);
54         inp = NULL;
55         gpg_err_set_errno (EPERM);
56       }
57     if (!inp) {
58         rc = gpg_error_from_syserror ();
59         log_error(_("can't open '%s': %s\n"), fname? fname: "[stdin]",
60                                         strerror(errno) );
61         goto leave;
62     }
63
64     push_armor_filter ( afx, inp );
65
66     if( (rc = open_outfile (-1, fname, 0, 0, &out)) )
67         goto leave;
68
69     while( (c = iobuf_get(inp)) != -1 )
70         iobuf_put( out, c );
71
72   leave:
73     if( rc )
74         iobuf_cancel(out);
75     else
76         iobuf_close(out);
77     iobuf_close(inp);
78     release_armor_context (afx);
79     return rc;
80 }
81
82
83 /****************
84  * Take file and write it out with armor
85  */
86 int
87 enarmor_file( const char *fname )
88 {
89     armor_filter_context_t *afx;
90     IOBUF inp = NULL, out = NULL;
91     int rc = 0;
92     int c;
93
94     afx = new_armor_context ();
95
96     /* prepare iobufs */
97     inp = iobuf_open(fname);
98     if (inp && is_secured_file (iobuf_get_fd (inp)))
99       {
100         iobuf_close (inp);
101         inp = NULL;
102         gpg_err_set_errno (EPERM);
103       }
104     if (!inp) {
105         rc = gpg_error_from_syserror ();
106         log_error(_("can't open '%s': %s\n"), fname? fname: "[stdin]",
107                   strerror(errno) );
108         goto leave;
109     }
110
111
112     if( (rc = open_outfile (-1, fname, 1, 0, &out )) )
113         goto leave;
114
115     afx->what = 4;
116     afx->hdrlines = "Comment: Use \"gpg --dearmor\" for unpacking\n";
117     push_armor_filter ( afx, out );
118
119     while( (c = iobuf_get(inp)) != -1 )
120         iobuf_put( out, c );
121
122
123   leave:
124     if( rc )
125         iobuf_cancel(out);
126     else
127         iobuf_close(out);
128     iobuf_close(inp);
129     release_armor_context (afx);
130     return rc;
131 }