1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-10 21:38:50 +01:00
gnupg/g10/plaintext.c

169 lines
3.9 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* 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"
1997-11-24 12:04:11 +01:00
#include "filter.h"
#include "main.h"
1997-11-18 15:06:00 +01:00
/****************
1997-11-24 12:04:11 +01:00
* 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.
1997-11-18 15:06:00 +01:00
*/
int
1997-11-24 12:04:11 +01:00
handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx )
1997-11-18 15:06:00 +01:00
{
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;
}
1997-12-20 18:23:29 +01:00
if( !*fname ) { /* no filename given; write to stdout */
fp = stdout;
1997-11-18 15:06:00 +01:00
}
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;
}
1997-12-03 11:20:03 +01:00
if( mfx->md )
1998-01-12 11:18:17 +01:00
md_putc(mfx->md, c );
1997-11-18 15:06:00 +01:00
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 ) {
1998-01-12 11:18:17 +01:00
if( mfx->md )
md_putc(mfx->md, c );
1997-11-18 15:06:00 +01:00
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;
}
1997-12-03 11:20:03 +01:00
/****************
* Ask for the detached datafile and calculate the digest from it.
* INFILE is the name of the input file.
1997-12-03 11:20:03 +01:00
*/
int
ask_for_detached_datafile( md_filter_context_t *mfx, const char *inname )
1997-12-03 11:20:03 +01:00
{
char *answer = NULL;
IOBUF fp;
1997-12-03 11:20:03 +01:00
int rc = 0;
int c;
fp = open_sigfile( inname ); /* open default file */
1997-12-03 11:20:03 +01:00
if( !fp ) {
int any=0;
tty_printf("Detached signature.\n");
do {
m_free(answer);
answer = tty_get("Please enter name of data file: ");
tty_kill_prompt();
if( any && !*answer ) {
rc = G10ERR_READ_FILE;
goto leave;
}
fp = iobuf_open(answer);
if( !fp && errno == ENOENT ) {
tty_printf("No such file, try again or hit enter to quit.\n");
any++;
}
else if( !fp ) {
log_error("can't open '%s': %s\n", answer, strerror(errno) );
rc = G10ERR_READ_FILE;
goto leave;
}
} while( !fp );
1997-12-03 11:20:03 +01:00
}
while( (c = iobuf_get(fp)) != -1 ) {
1997-12-03 11:20:03 +01:00
if( mfx->md )
1998-01-12 11:18:17 +01:00
md_putc(mfx->md, c );
1997-12-03 11:20:03 +01:00
}
iobuf_close(fp);
1997-12-03 11:20:03 +01:00
leave:
m_free(answer);
return rc;
}