2002-04-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / plaintext.c
blob555dd163660b77985ac27f801f6ac606bad77875
1 /* plaintext.c - process an plaintext packet
2 * Copyright (C) 1998, 1999, 2000 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
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
27 #ifdef HAVE_DOSISH_SYSTEM
28 #include <fcntl.h> /* for setmode() */
29 #endif
31 #include "util.h"
32 #include <gcrypt.h>
33 #include "options.h"
34 #include "packet.h"
35 #include "ttyio.h"
36 #include "filter.h"
37 #include "main.h"
38 #include "status.h"
39 #include "i18n.h"
43 /****************
44 * Handle a plaintext packet. If MFX is not NULL, update the MDs
45 * Note: we should use the filter stuff here, but we have to add some
46 * easy mimic to set a read limit, so we calculate only the
47 * bytes from the plaintext.
49 int
50 handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
51 int nooutput, int clearsig )
53 char *fname = NULL;
54 FILE *fp = NULL;
55 int rc = 0;
56 int c;
57 int convert = pt->mode == 't';
59 /* create the filename as C string */
60 if( nooutput )
62 else if( opt.outfile ) {
63 fname = gcry_xmalloc( strlen( opt.outfile ) + 1);
64 strcpy(fname, opt.outfile );
66 else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) {
67 log_info(_("data not saved; use option \"--output\" to save it\n"));
68 nooutput = 1;
70 else if( !opt.use_embedded_filename ) {
71 fname = make_outfile_name( iobuf_get_real_fname(pt->buf) );
72 if( !fname )
73 fname = ask_outfile_name( pt->name, pt->namelen );
74 if( !fname ) {
75 rc = GPGERR_CREATE_FILE;
76 goto leave;
79 else {
80 fname = make_printable_string( pt->name, pt->namelen, 0 );
83 if( nooutput )
85 else if( !*fname || (*fname=='-' && !fname[1])) {
86 /* no filename or "-" given; write to stdout */
87 fp = stdout;
88 #ifdef HAVE_DOSISH_SYSTEM
89 setmode ( fileno(fp) , O_BINARY );
90 #endif
92 else if( !overwrite_filep( fname ) ) {
93 rc = GPGERR_CREATE_FILE;
94 goto leave;
97 if( fp || nooutput )
99 else if( !(fp = fopen(fname,"wb")) ) {
100 log_error("Error creating `%s': %s\n", fname, strerror(errno) );
101 rc = GPGERR_CREATE_FILE;
102 goto leave;
105 if( pt->len ) {
106 assert( !clearsig );
107 if( convert ) { /* text mode */
108 for( ; pt->len; pt->len-- ) {
109 if( (c = iobuf_get(pt->buf)) == -1 ) {
110 log_error("Problem reading source (%u bytes remaining)\n",
111 (unsigned)pt->len);
112 rc = GPGERR_READ_FILE;
113 goto leave;
115 if( mfx->md )
116 gcry_md_putc(mfx->md, c );
117 #ifndef HAVE_DOSISH_SYSTEM
118 if( c == '\r' ) /* convert to native line ending */
119 continue; /* fixme: this hack might be too simple */
120 #endif
121 if( fp ) {
122 if( putc( c, fp ) == EOF ) {
123 log_error("Error writing to `%s': %s\n",
124 fname, strerror(errno) );
125 rc = GPGERR_WRITE_FILE;
126 goto leave;
131 else { /* binary mode */
132 byte *buffer = gcry_xmalloc( 32768 );
133 while( pt->len ) {
134 int len = pt->len > 32768 ? 32768 : pt->len;
135 len = iobuf_read( pt->buf, buffer, len );
136 if( len == -1 ) {
137 log_error("Problem reading source (%u bytes remaining)\n",
138 (unsigned)pt->len);
139 rc = GPGERR_READ_FILE;
140 gcry_free( buffer );
141 goto leave;
143 if( mfx->md )
144 gcry_md_write( mfx->md, buffer, len );
145 if( fp ) {
146 if( fwrite( buffer, 1, len, fp ) != len ) {
147 log_error("Error writing to `%s': %s\n",
148 fname, strerror(errno) );
149 rc = GPGERR_WRITE_FILE;
150 gcry_free( buffer );
151 goto leave;
154 pt->len -= len;
156 gcry_free( buffer );
159 else if( !clearsig ) {
160 if( convert ) { /* text mode */
161 while( (c = iobuf_get(pt->buf)) != -1 ) {
162 if( mfx->md )
163 gcry_md_putc(mfx->md, c );
164 #ifndef HAVE_DOSISH_SYSTEM
165 if( convert && c == '\r' )
166 continue; /* fixme: this hack might be too simple */
167 #endif
168 if( fp ) {
169 if( putc( c, fp ) == EOF ) {
170 log_error("Error writing to `%s': %s\n",
171 fname, strerror(errno) );
172 rc = GPGERR_WRITE_FILE;
173 goto leave;
178 else { /* binary mode */
179 byte *buffer = gcry_xmalloc( 32768 );
180 int eof;
181 for( eof=0; !eof; ) {
182 /* Why do we check for len < 32768:
183 * If we won't, we would practically read 2 EOFs but
184 * the first one has already popped the block_filter
185 * off and therefore we don't catch the boundary.
186 * So, always assume EOF if iobuf_read returns less bytes
187 * then requested */
188 int len = iobuf_read( pt->buf, buffer, 32768 );
189 if( len == -1 )
190 break;
191 if( len < 32768 )
192 eof = 1;
193 if( mfx->md )
194 gcry_md_write( mfx->md, buffer, len );
195 if( fp ) {
196 if( fwrite( buffer, 1, len, fp ) != len ) {
197 log_error("Error writing to `%s': %s\n",
198 fname, strerror(errno) );
199 rc = GPGERR_WRITE_FILE;
200 gcry_free( buffer );
201 goto leave;
205 gcry_free( buffer );
207 pt->buf = NULL;
209 else { /* clear text signature - don't hash the last cr,lf */
210 int state = 0;
212 while( (c = iobuf_get(pt->buf)) != -1 ) {
213 if( fp ) {
214 if( putc( c, fp ) == EOF ) {
215 log_error("Error writing to `%s': %s\n",
216 fname, strerror(errno) );
217 rc = GPGERR_WRITE_FILE;
218 goto leave;
221 if( !mfx->md )
222 continue;
223 if( state == 2 ) {
224 gcry_md_putc(mfx->md, '\r' );
225 gcry_md_putc(mfx->md, '\n' );
226 state = 0;
228 if( !state ) {
229 if( c == '\r' )
230 state = 1;
231 else if( c == '\n' )
232 state = 2;
233 else
234 gcry_md_putc(mfx->md, c );
236 else if( state == 1 ) {
237 if( c == '\n' )
238 state = 2;
239 else {
240 gcry_md_putc(mfx->md, '\r' );
241 if( c == '\r' )
242 state = 1;
243 else {
244 state = 0;
245 gcry_md_putc(mfx->md, c );
250 pt->buf = NULL;
253 if( fp && fp != stdout && fclose(fp) ) {
254 log_error("Error closing `%s': %s\n", fname, strerror(errno) );
255 fp = NULL;
256 rc = GPGERR_WRITE_FILE;
257 goto leave;
259 fp = NULL;
261 leave:
262 if( fp && fp != stdout )
263 fclose(fp);
264 gcry_free(fname);
265 return rc;
268 static void
269 do_hash( GCRY_MD_HD md, GCRY_MD_HD md2, IOBUF fp, int textmode )
271 text_filter_context_t tfx;
272 int c;
274 if( textmode ) {
275 memset( &tfx, 0, sizeof tfx);
276 iobuf_push_filter( fp, text_filter, &tfx );
278 if( md2 ) { /* work around a strange behaviour in pgp2 */
279 /* It seems that at least PGP5 converts a single CR to a CR,LF too */
280 int lc = -1;
281 while( (c = iobuf_get(fp)) != -1 ) {
282 if( c == '\n' && lc == '\r' )
283 gcry_md_putc(md2, c);
284 else if( c == '\n' ) {
285 gcry_md_putc(md2, '\r');
286 gcry_md_putc(md2, c);
288 else if( c != '\n' && lc == '\r' ) {
289 gcry_md_putc(md2, '\n');
290 gcry_md_putc(md2, c);
292 else
293 gcry_md_putc(md2, c);
295 if( md )
296 gcry_md_putc(md, c );
297 lc = c;
300 else {
301 while( (c = iobuf_get(fp)) != -1 ) {
302 if( md )
303 gcry_md_putc(md, c );
309 /****************
310 * Ask for the detached datafile and calculate the digest from it.
311 * INFILE is the name of the input file.
314 ask_for_detached_datafile( GCRY_MD_HD md, GCRY_MD_HD md2,
315 const char *inname, int textmode )
317 char *answer = NULL;
318 IOBUF fp;
319 int rc = 0;
321 fp = open_sigfile( inname ); /* open default file */
322 if( !fp && !opt.batch ) {
323 int any=0;
324 tty_printf(_("Detached signature.\n"));
325 do {
326 gcry_free(answer);
327 answer = cpr_get("detached_signature.filename",
328 _("Please enter name of data file: "));
329 cpr_kill_prompt();
330 if( any && !*answer ) {
331 rc = GPGERR_READ_FILE;
332 goto leave;
334 fp = iobuf_open(answer);
335 if( !fp && errno == ENOENT ) {
336 tty_printf("No such file, try again or hit enter to quit.\n");
337 any++;
339 else if( !fp ) {
340 log_error("can't open `%s': %s\n", answer, strerror(errno) );
341 rc = GPGERR_READ_FILE;
342 goto leave;
344 } while( !fp );
347 if( !fp ) {
348 if( opt.verbose )
349 log_info(_("reading stdin ...\n"));
350 fp = iobuf_open( NULL );
351 assert(fp);
353 do_hash( md, md2, fp, textmode );
354 iobuf_close(fp);
357 leave:
358 gcry_free(answer);
359 return rc;
364 /****************
365 * Hash the given files and append the hash to hash context md.
366 * If FILES is NULL, hash stdin.
369 hash_datafiles( GCRY_MD_HD md, GCRY_MD_HD md2, STRLIST files,
370 const char *sigfilename, int textmode )
372 IOBUF fp;
373 STRLIST sl=NULL;
375 if( !files ) {
376 /* check whether we can open the signed material */
377 fp = open_sigfile( sigfilename );
378 if( fp ) {
379 do_hash( md, md2, fp, textmode );
380 iobuf_close(fp);
381 return 0;
383 /* no we can't (no sigfile) - read signed stuff from stdin */
384 add_to_strlist( &sl, "-");
386 else
387 sl = files;
389 for( ; sl; sl = sl->next ) {
390 fp = iobuf_open( sl->d );
391 if( !fp ) {
392 log_error(_("can't open signed data `%s'\n"),
393 print_fname_stdin(sl->d));
394 if( !files )
395 free_strlist(sl);
396 return GPGERR_OPEN_FILE;
398 do_hash( md, md2, fp, textmode );
399 iobuf_close(fp);
402 if( !files )
403 free_strlist(sl);
404 return 0;