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