2006-06-09 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / dearmor.c
blob7f69a1bba7f2a0c1a57126c980e3aa615bb8cb2d
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
29 #include "gpg.h"
30 #include "errors.h"
31 #include "iobuf.h"
32 #include "util.h"
33 #include "filter.h"
34 #include "packet.h"
35 #include "options.h"
36 #include "main.h"
37 #include "i18n.h"
39 /****************
40 * Take an armor file and write it out without armor
42 int
43 dearmor_file( const char *fname )
45 armor_filter_context_t afx;
46 IOBUF inp = NULL, out = NULL;
47 int rc = 0;
48 int c;
50 memset( &afx, 0, sizeof afx);
52 /* prepare iobufs */
53 inp = iobuf_open(fname);
54 if (inp && is_secured_file (iobuf_get_fd (inp)))
56 iobuf_close (inp);
57 inp = NULL;
58 errno = EPERM;
60 if (!inp) {
61 rc = gpg_error_from_errno (errno);
62 log_error(_("can't open `%s': %s\n"), fname? fname: "[stdin]",
63 strerror(errno) );
64 goto leave;
67 iobuf_push_filter( inp, armor_filter, &afx );
69 if( (rc = open_outfile( fname, 0, &out )) )
70 goto leave;
74 while( (c = iobuf_get(inp)) != -1 )
75 iobuf_put( out, c );
78 leave:
79 if( rc )
80 iobuf_cancel(out);
81 else
82 iobuf_close(out);
83 iobuf_close(inp);
84 return rc;
88 /****************
89 * Take file and write it out with armor
91 int
92 enarmor_file( const char *fname )
94 armor_filter_context_t afx;
95 IOBUF inp = NULL, out = NULL;
96 int rc = 0;
97 int c;
99 memset( &afx, 0, sizeof afx);
101 /* prepare iobufs */
102 inp = iobuf_open(fname);
103 if (inp && is_secured_file (iobuf_get_fd (inp)))
105 iobuf_close (inp);
106 inp = NULL;
107 errno = EPERM;
109 if (!inp) {
110 rc = gpg_error_from_errno (errno);
111 log_error(_("can't open `%s': %s\n"), fname? fname: "[stdin]",
112 strerror(errno) );
113 goto leave;
117 if( (rc = open_outfile( fname, 1, &out )) )
118 goto leave;
120 afx.what = 4;
121 afx.hdrlines = "Comment: Use \"gpg --dearmor\" for unpacking\n";
122 iobuf_push_filter( out, armor_filter, &afx );
124 while( (c = iobuf_get(inp)) != -1 )
125 iobuf_put( out, c );
128 leave:
129 if( rc )
130 iobuf_cancel(out);
131 else
132 iobuf_close(out);
133 iobuf_close(inp);
134 return rc;