Avoid catch-22 with README.main not being distributed but having the
[gnupg.git] / g10 / dearmor.c
blobda888ad14b0b98f5250eac72b473d05510fc798d
1 /* dearmor.c - Armor utility
2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
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.
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.
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/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
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"
37 /****************
38 * Take an armor file and write it out without armor
40 int
41 dearmor_file( const char *fname )
43 armor_filter_context_t *afx;
44 IOBUF inp = NULL, out = NULL;
45 int rc = 0;
46 int c;
48 afx = new_armor_context ();
50 /* prepare iobufs */
51 inp = iobuf_open(fname);
52 if (inp && is_secured_file (iobuf_get_fd (inp)))
54 iobuf_close (inp);
55 inp = NULL;
56 errno = EPERM;
58 if (!inp) {
59 rc = gpg_error_from_syserror ();
60 log_error(_("can't open `%s': %s\n"), fname? fname: "[stdin]",
61 strerror(errno) );
62 goto leave;
65 push_armor_filter ( afx, inp );
67 if( (rc = open_outfile( fname, 0, &out )) )
68 goto leave;
70 while( (c = iobuf_get(inp)) != -1 )
71 iobuf_put( out, c );
73 leave:
74 if( rc )
75 iobuf_cancel(out);
76 else
77 iobuf_close(out);
78 iobuf_close(inp);
79 release_armor_context (afx);
80 return rc;
84 /****************
85 * Take file and write it out with armor
87 int
88 enarmor_file( const char *fname )
90 armor_filter_context_t *afx;
91 IOBUF inp = NULL, out = NULL;
92 int rc = 0;
93 int c;
95 afx = new_armor_context ();
97 /* prepare iobufs */
98 inp = iobuf_open(fname);
99 if (inp && is_secured_file (iobuf_get_fd (inp)))
101 iobuf_close (inp);
102 inp = NULL;
103 errno = EPERM;
105 if (!inp) {
106 rc = gpg_error_from_syserror ();
107 log_error(_("can't open `%s': %s\n"), fname? fname: "[stdin]",
108 strerror(errno) );
109 goto leave;
113 if( (rc = open_outfile( fname, 1, &out )) )
114 goto leave;
116 afx->what = 4;
117 afx->hdrlines = "Comment: Use \"gpg --dearmor\" for unpacking\n";
118 push_armor_filter ( afx, out );
120 while( (c = iobuf_get(inp)) != -1 )
121 iobuf_put( out, c );
124 leave:
125 if( rc )
126 iobuf_cancel(out);
127 else
128 iobuf_close(out);
129 iobuf_close(inp);
130 release_armor_context (afx);
131 return rc;