Line data Source code
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 36 : dearmor_file( const char *fname )
41 : {
42 : armor_filter_context_t *afx;
43 36 : IOBUF inp = NULL, out = NULL;
44 36 : int rc = 0;
45 : int c;
46 :
47 36 : afx = new_armor_context ();
48 :
49 : /* prepare iobufs */
50 36 : inp = iobuf_open(fname);
51 36 : if (inp && is_secured_file (iobuf_get_fd (inp)))
52 : {
53 0 : iobuf_close (inp);
54 0 : inp = NULL;
55 0 : gpg_err_set_errno (EPERM);
56 : }
57 36 : if (!inp) {
58 0 : rc = gpg_error_from_syserror ();
59 0 : log_error(_("can't open '%s': %s\n"), fname? fname: "[stdin]",
60 0 : strerror(errno) );
61 0 : goto leave;
62 : }
63 :
64 36 : push_armor_filter ( afx, inp );
65 :
66 36 : if( (rc = open_outfile (-1, fname, 0, 0, &out)) )
67 0 : goto leave;
68 :
69 270808 : while( (c = iobuf_get(inp)) != -1 )
70 270736 : iobuf_put( out, c );
71 :
72 : leave:
73 36 : if( rc )
74 0 : iobuf_cancel(out);
75 : else
76 36 : iobuf_close(out);
77 36 : iobuf_close(inp);
78 36 : release_armor_context (afx);
79 36 : return rc;
80 : }
81 :
82 :
83 : /****************
84 : * Take file and write it out with armor
85 : */
86 : int
87 0 : enarmor_file( const char *fname )
88 : {
89 : armor_filter_context_t *afx;
90 0 : IOBUF inp = NULL, out = NULL;
91 0 : int rc = 0;
92 : int c;
93 :
94 0 : afx = new_armor_context ();
95 :
96 : /* prepare iobufs */
97 0 : inp = iobuf_open(fname);
98 0 : if (inp && is_secured_file (iobuf_get_fd (inp)))
99 : {
100 0 : iobuf_close (inp);
101 0 : inp = NULL;
102 0 : gpg_err_set_errno (EPERM);
103 : }
104 0 : if (!inp) {
105 0 : rc = gpg_error_from_syserror ();
106 0 : log_error(_("can't open '%s': %s\n"), fname? fname: "[stdin]",
107 0 : strerror(errno) );
108 0 : goto leave;
109 : }
110 :
111 :
112 0 : if( (rc = open_outfile (-1, fname, 1, 0, &out )) )
113 0 : goto leave;
114 :
115 0 : afx->what = 4;
116 0 : afx->hdrlines = "Comment: Use \"gpg --dearmor\" for unpacking\n";
117 0 : push_armor_filter ( afx, out );
118 :
119 0 : while( (c = iobuf_get(inp)) != -1 )
120 0 : iobuf_put( out, c );
121 :
122 :
123 : leave:
124 0 : if( rc )
125 0 : iobuf_cancel(out);
126 : else
127 0 : iobuf_close(out);
128 0 : iobuf_close(inp);
129 0 : release_armor_context (afx);
130 0 : return rc;
131 : }
|