1 /* plaintext.c - process an plaintext packet
2 * Copyright (C) 1998 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
38 * Defer the last CR,LF
41 special_md_putc( MD_HANDLE md, int c, int *state )
43 if( c == -1 ) { /* flush */
79 * Handle a plaintext packet. If MFX is not NULL, update the MDs
80 * Note: we should use the filter stuff here, but we have to add some
81 * easy mimic to set a read limit, so we calculate only the
82 * bytes from the plaintext.
85 handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
86 int nooutput, int clearsig )
92 int convert = pt->mode == 't';
93 int special_state = 0;
95 /* create the filename as C string */
98 else if( opt.outfile ) {
99 fname = m_alloc( strlen( opt.outfile ) + 1);
100 strcpy(fname, opt.outfile );
102 else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) {
103 log_info(_("data not saved; use option \"--output\" to save it\n"));
107 fname = m_alloc( pt->namelen +1 );
108 memcpy( fname, pt->name, pt->namelen );
109 fname[pt->namelen] = 0;
114 else if( !*fname || (*fname=='-' && !fname[1])) {
115 /* no filename or "-" given; write to stdout */
118 else if( !overwrite_filep( fname ) ) {
119 rc = G10ERR_CREATE_FILE;
125 else if( !(fp = fopen(fname,"wb")) ) {
126 log_error("Error creating '%s': %s\n", fname, strerror(errno) );
127 rc = G10ERR_CREATE_FILE;
132 for( ; pt->len; pt->len-- ) {
133 if( (c = iobuf_get(pt->buf)) == -1 ) {
134 log_error("Problem reading source (%u bytes remaining)\n",
136 rc = G10ERR_READ_FILE;
140 if( convert && clearsig )
141 special_md_putc(mfx->md, c, &special_state );
143 md_putc(mfx->md, c );
145 if( convert && !clearsig && c == '\r' )
146 continue; /* fixme: this hack might be too simple */
148 if( putc( c, fp ) == EOF ) {
149 log_error("Error writing to '%s': %s\n",
150 fname, strerror(errno) );
151 rc = G10ERR_WRITE_FILE;
158 while( (c = iobuf_get(pt->buf)) != -1 ) {
160 if( convert && clearsig )
161 special_md_putc(mfx->md, c, &special_state );
163 md_putc(mfx->md, c );
165 if( convert && !clearsig && c == '\r' )
166 continue; /* fixme: this hack might be too simple */
168 if( putc( c, fp ) == EOF ) {
169 log_error("Error writing to '%s': %s\n",
170 fname, strerror(errno) );
171 rc = G10ERR_WRITE_FILE;
176 iobuf_clear_eof(pt->buf);
178 if( mfx->md && convert && clearsig )
179 special_md_putc(mfx->md, -1, &special_state ); /* flush */
181 if( fp && fp != stdout && fclose(fp) ) {
182 log_error("Error closing '%s': %s\n", fname, strerror(errno) );
184 rc = G10ERR_WRITE_FILE;
190 if( fp && fp != stdout )
198 * Ask for the detached datafile and calculate the digest from it.
199 * INFILE is the name of the input file.
202 ask_for_detached_datafile( md_filter_context_t *mfx, const char *inname )
209 fp = open_sigfile( inname ); /* open default file */
210 if( !fp && !opt.batch ) {
212 tty_printf("Detached signature.\n");
215 answer = cpr_get("detached_signature.filename",
216 _("Please enter name of data file: "));
218 if( any && !*answer ) {
219 rc = G10ERR_READ_FILE;
222 fp = iobuf_open(answer);
223 if( !fp && errno == ENOENT ) {
224 tty_printf("No such file, try again or hit enter to quit.\n");
228 log_error("can't open '%s': %s\n", answer, strerror(errno) );
229 rc = G10ERR_READ_FILE;
237 log_info(_("reading stdin ...\n"));
238 while( (c = getchar()) != EOF ) {
240 md_putc(mfx->md, c );
244 while( (c = iobuf_get(fp)) != -1 ) {
246 md_putc(mfx->md, c );
258 do_hash( MD_HANDLE md, IOBUF fp, int textmode )
260 text_filter_context_t tfx;
264 memset( &tfx, 0, sizeof tfx);
265 iobuf_push_filter( fp, text_filter, &tfx );
267 while( (c = iobuf_get(fp)) != -1 )
273 * Hash the given files and append the hash to hash context md.
274 * If FILES is NULL, hash stdin.
277 hash_datafiles( MD_HANDLE md, STRLIST files,
278 const char *sigfilename, int textmode )
284 /* check whether we can opne the signed material */
285 fp = open_sigfile( sigfilename );
287 do_hash( md, fp, textmode );
291 /* no we can't (no sigfile) - read signed stuff from stdin */
292 add_to_strlist( &sl, "-");
297 for( ; sl; sl = sl->next ) {
298 fp = iobuf_open( sl->d );
300 log_error(_("can't open signed data '%s'\n"),
301 print_fname_stdin(sl->d));
304 return G10ERR_OPEN_FILE;
306 do_hash( md, fp, textmode );