aboutsummaryrefslogtreecommitdiffstats
path: root/g10/plaintext.c
blob: d317fd7899de0e0177cc2b2bd12f2c05fe9df03e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* plaintext.c -  process an plaintext packet
 *	Copyright (c) 1997 by Werner Koch (dd9jn)
 *
 * This file is part of G10.
 *
 * G10 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * G10 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "util.h"
#include "memory.h"
#include "options.h"
#include "packet.h"
#include "ttyio.h"
#include "filter.h"


/****************
 * Handle a plaintext packet.  If MFX is not NULL, update the MDs
 * Note: we should use the filter stuff here, but we have to add some
 *	 easy mimic to set a read limit, so we calculate only the
 *	 bytes from the plaintext.
 */
int
handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx )
{
    char *fname;
    FILE *fp = NULL;
    int rc = 0;
    int c;

    /* create the filename as C string */
    if( opt.outfile ) {
	fname = m_alloc( strlen( opt.outfile ) + 1);
	strcpy(fname, opt.outfile );
    }
    else {
	fname = m_alloc( pt->namelen +1 );
	memcpy( fname, pt->name, pt->namelen );
	fname[pt->namelen] = 0;
    }

    if( !*fname ) { /* no filename given */
	if( opt.outfile_is_stdout )
	    fp = stdout;
	else {
	    log_error("no outputfile given\n");
	    goto leave;
	}
    }
    else if( overwrite_filep( fname ) )
	goto leave;

    if( fp )
	;
    else if( !(fp = fopen(fname,"wb")) ) {
	log_error("Error creating '%s': %s\n", fname, strerror(errno) );
	rc = G10ERR_WRITE_FILE;
	goto leave;
    }

    if( pt->len ) {
	for( ; pt->len; pt->len-- ) {
	    if( (c = iobuf_get(pt->buf)) == -1 ) {
		log_error("Problem reading source\n");
		rc = G10ERR_READ_FILE;
		goto leave;
	    }
	    if( mfx->rmd160 )
		rmd160_putchar(mfx->rmd160, c );
	    if( mfx->md5 )
		md5_putchar(mfx->md5, c );
	    if( putc( c, fp ) == EOF ) {
		log_error("Error writing to '%s': %s\n", fname, strerror(errno) );
		rc = G10ERR_WRITE_FILE;
		goto leave;
	    }
	}
    }
    else {
	while( (c = iobuf_get(pt->buf)) != -1 ) {
	    if( mfx->rmd160 )
		rmd160_putchar(mfx->rmd160, c );
	    if( mfx->md5 )
		md5_putchar(mfx->md5, c );
	    if( putc( c, fp ) == EOF ) {
		log_error("Error writing to '%s': %s\n",
					    fname, strerror(errno) );
		rc = G10ERR_WRITE_FILE;
		goto leave;
	    }
	}
	iobuf_clear_eof(pt->buf);
    }

    if( fp && fp != stdout && fclose(fp) ) {
	log_error("Error closing '%s': %s\n", fname, strerror(errno) );
	fp = NULL;
	rc = G10ERR_WRITE_FILE;
	goto leave;
    }
    fp = NULL;

  leave:
    if( fp && fp != stdout )
	fclose(fp);
    m_free(fname);
    return rc;
}