mirror of
git://git.gnupg.org/gnupg.git
synced 2025-02-23 20:08:04 +01:00
Re-indented.
This commit is contained in:
parent
fafe852c33
commit
b432da7574
503
g10/plaintext.c
503
g10/plaintext.c
@ -1,6 +1,6 @@
|
|||||||
/* plaintext.c - process plaintext packets
|
/* plaintext.c - process plaintext packets
|
||||||
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
||||||
* 2006 Free Software Foundation, Inc.
|
* 2006, 2009 Free Software Foundation, Inc.
|
||||||
*
|
*
|
||||||
* This file is part of GnuPG.
|
* This file is part of GnuPG.
|
||||||
*
|
*
|
||||||
@ -26,7 +26,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#ifdef HAVE_DOSISH_SYSTEM
|
#ifdef HAVE_DOSISH_SYSTEM
|
||||||
#include <fcntl.h> /* for setmode() */
|
# include <fcntl.h> /* for setmode() */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "gpg.h"
|
#include "gpg.h"
|
||||||
@ -40,19 +40,17 @@
|
|||||||
#include "i18n.h"
|
#include "i18n.h"
|
||||||
|
|
||||||
|
|
||||||
/****************
|
/* Handle a plaintext packet. If MFX is not NULL, update the MDs
|
||||||
* Handle a plaintext packet. If MFX is not NULL, update the MDs
|
* Note: We should have used the filter stuff here, but we have to add
|
||||||
* Note: we should use the filter stuff here, but we have to add some
|
* some easy mimic to set a read limit, so we calculate only the bytes
|
||||||
* easy mimic to set a read limit, so we calculate only the
|
* from the plaintext. */
|
||||||
* bytes from the plaintext.
|
|
||||||
*/
|
|
||||||
int
|
int
|
||||||
handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
handle_plaintext (PKT_plaintext * pt, md_filter_context_t * mfx,
|
||||||
int nooutput, int clearsig )
|
int nooutput, int clearsig)
|
||||||
{
|
{
|
||||||
char *fname = NULL;
|
char *fname = NULL;
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
static off_t count=0;
|
static off_t count = 0;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
int c;
|
int c;
|
||||||
int convert = (pt->mode == 't' || pt->mode == 'u');
|
int convert = (pt->mode == 't' || pt->mode == 'u');
|
||||||
@ -61,9 +59,9 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Let people know what the plaintext info is. This allows the
|
/* Let people know what the plaintext info is. This allows the
|
||||||
receiving program to try and do something different based on
|
receiving program to try and do something different based on the
|
||||||
the format code (say, recode UTF-8 to local). */
|
format code (say, recode UTF-8 to local). */
|
||||||
if(!nooutput && is_status_enabled())
|
if (!nooutput && is_status_enabled ())
|
||||||
{
|
{
|
||||||
char status[50];
|
char status[50];
|
||||||
|
|
||||||
@ -73,56 +71,65 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
|||||||
status message. */
|
status message. */
|
||||||
fflush (stdout);
|
fflush (stdout);
|
||||||
|
|
||||||
sprintf(status,"%X %lu ",(byte)pt->mode,(ulong)pt->timestamp);
|
snprintf (status, sizeof status,
|
||||||
write_status_text_and_buffer(STATUS_PLAINTEXT,
|
"%X %lu ", (byte) pt->mode, (ulong) pt->timestamp);
|
||||||
status,pt->name,pt->namelen,0);
|
write_status_text_and_buffer (STATUS_PLAINTEXT,
|
||||||
|
status, pt->name, pt->namelen, 0);
|
||||||
|
|
||||||
if(!pt->is_partial)
|
if (!pt->is_partial)
|
||||||
{
|
{
|
||||||
sprintf(status,"%lu",(ulong)pt->len);
|
sprintf (status, "%lu", (ulong) pt->len);
|
||||||
write_status_text(STATUS_PLAINTEXT_LENGTH,status);
|
write_status_text (STATUS_PLAINTEXT_LENGTH, status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create the filename as C string */
|
/* Create the filename as C string. */
|
||||||
if( nooutput )
|
if (nooutput)
|
||||||
;
|
;
|
||||||
else if( opt.outfile ) {
|
else if (opt.outfile)
|
||||||
fname = xmalloc( strlen( opt.outfile ) + 1);
|
{
|
||||||
strcpy(fname, opt.outfile );
|
fname = xmalloc (strlen (opt.outfile) + 1);
|
||||||
|
strcpy (fname, opt.outfile);
|
||||||
}
|
}
|
||||||
else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) {
|
else if (pt->namelen == 8 && !memcmp (pt->name, "_CONSOLE", 8))
|
||||||
log_info(_("data not saved; use option \"--output\" to save it\n"));
|
{
|
||||||
|
log_info (_("data not saved; use option \"--output\" to save it\n"));
|
||||||
nooutput = 1;
|
nooutput = 1;
|
||||||
}
|
}
|
||||||
else if( !opt.flags.use_embedded_filename ) {
|
else if (!opt.flags.use_embedded_filename)
|
||||||
fname = make_outfile_name( iobuf_get_real_fname(pt->buf) );
|
{
|
||||||
if( !fname )
|
fname = make_outfile_name (iobuf_get_real_fname (pt->buf));
|
||||||
fname = ask_outfile_name( pt->name, pt->namelen );
|
if (!fname)
|
||||||
if( !fname ) {
|
fname = ask_outfile_name (pt->name, pt->namelen);
|
||||||
|
if (!fname)
|
||||||
|
{
|
||||||
rc = gpg_error (GPG_ERR_GENERAL); /* Can't create file. */
|
rc = gpg_error (GPG_ERR_GENERAL); /* Can't create file. */
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
fname=utf8_to_native(pt->name,pt->namelen,0);
|
fname = utf8_to_native (pt->name, pt->namelen, 0);
|
||||||
|
|
||||||
if( nooutput )
|
if (nooutput)
|
||||||
;
|
;
|
||||||
else if ( iobuf_is_pipe_filename (fname) || !*fname)
|
else if (iobuf_is_pipe_filename (fname) || !*fname)
|
||||||
{
|
{
|
||||||
/* No filename or "-" given; write to stdout. */
|
/* No filename or "-" given; write to stdout. */
|
||||||
fp = stdout;
|
fp = stdout;
|
||||||
#ifdef HAVE_DOSISH_SYSTEM
|
#ifdef HAVE_DOSISH_SYSTEM
|
||||||
setmode ( fileno(fp) , O_BINARY );
|
setmode (fileno (fp), O_BINARY);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
while( !overwrite_filep (fname) ) {
|
{
|
||||||
|
while (!overwrite_filep (fname))
|
||||||
|
{
|
||||||
char *tmp = ask_outfile_name (NULL, 0);
|
char *tmp = ask_outfile_name (NULL, 0);
|
||||||
if ( !tmp || !*tmp ) {
|
if (!tmp || !*tmp)
|
||||||
|
{
|
||||||
xfree (tmp);
|
xfree (tmp);
|
||||||
rc = gpg_error (GPG_ERR_GENERAL); /* G10ERR_CREATE_FILE*/
|
/* FIXME: Below used to be G10ERR_CREATE_FILE */
|
||||||
|
rc = gpg_error (GPG_ERR_GENERAL);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
xfree (fname);
|
xfree (fname);
|
||||||
@ -131,257 +138,279 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef __riscos__
|
#ifndef __riscos__
|
||||||
if( fp || nooutput )
|
if (fp || nooutput)
|
||||||
;
|
;
|
||||||
else if (is_secured_filename (fname))
|
else if (is_secured_filename (fname))
|
||||||
{
|
{
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
log_error (_("error creating `%s': %s\n"), fname, strerror (errno));
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
else if( !(fp = fopen(fname,"wb")) ) {
|
else if (!(fp = fopen (fname, "wb")))
|
||||||
|
{
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
log_error (_("error creating `%s': %s\n"), fname, strerror (errno));
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
#else /* __riscos__ */
|
#else /* __riscos__ */
|
||||||
/* If no output filename was given, i.e. we constructed it,
|
/* If no output filename was given, i.e. we constructed it,
|
||||||
convert all '.' in fname to '/' but not vice versa as
|
convert all '.' in fname to '/' but not vice versa as
|
||||||
we don't create directories! */
|
we don't create directories! */
|
||||||
if( !opt.outfile )
|
if (!opt.outfile)
|
||||||
for( c=0; fname[c]; ++c )
|
for (c = 0; fname[c]; ++c)
|
||||||
if( fname[c] == '.' )
|
if (fname[c] == '.')
|
||||||
fname[c] = '/';
|
fname[c] = '/';
|
||||||
|
|
||||||
if( fp || nooutput )
|
if (fp || nooutput)
|
||||||
;
|
;
|
||||||
else {
|
else
|
||||||
fp = fopen(fname,"wb");
|
{
|
||||||
if( !fp ) {
|
fp = fopen (fname, "wb");
|
||||||
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
|
if (!fp)
|
||||||
|
{
|
||||||
|
log_error (_("error creating `%s': %s\n"), fname, strerror (errno));
|
||||||
rc = G10ERR_CREATE_FILE;
|
rc = G10ERR_CREATE_FILE;
|
||||||
if (errno == 106)
|
if (errno == 106)
|
||||||
log_info("Do output file and input file have the same name?\n");
|
log_info ("Do output file and input file have the same name?\n");
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If there's a ,xxx extension in the embedded filename,
|
/* If there's a ,xxx extension in the embedded filename,
|
||||||
use that, else check whether the user input (in fname)
|
use that, else check whether the user input (in fname)
|
||||||
has a ,xxx appended, then use that in preference */
|
has a ,xxx appended, then use that in preference */
|
||||||
if( (c = riscos_get_filetype_from_string( pt->name,
|
if ((c = riscos_get_filetype_from_string (pt->name, pt->namelen)) != -1)
|
||||||
pt->namelen )) != -1 )
|
|
||||||
filetype = c;
|
filetype = c;
|
||||||
if( (c = riscos_get_filetype_from_string( fname,
|
if ((c = riscos_get_filetype_from_string (fname, strlen (fname))) != -1)
|
||||||
strlen(fname) )) != -1 )
|
|
||||||
filetype = c;
|
filetype = c;
|
||||||
riscos_set_filetype_by_number(fname, filetype);
|
riscos_set_filetype_by_number (fname, filetype);
|
||||||
}
|
}
|
||||||
#endif /* __riscos__ */
|
#endif /* __riscos__ */
|
||||||
|
|
||||||
if( !pt->is_partial ) {
|
if (!pt->is_partial)
|
||||||
|
{
|
||||||
/* We have an actual length (which might be zero). */
|
/* We have an actual length (which might be zero). */
|
||||||
|
|
||||||
if (clearsig) {
|
if (clearsig)
|
||||||
|
{
|
||||||
log_error ("clearsig encountered while not expected\n");
|
log_error ("clearsig encountered while not expected\n");
|
||||||
rc = G10ERR_UNEXPECTED;
|
rc = G10ERR_UNEXPECTED;
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( convert ) { /* text mode */
|
if (convert) /* Text mode. */
|
||||||
for( ; pt->len; pt->len-- ) {
|
{
|
||||||
if( (c = iobuf_get(pt->buf)) == -1 ) {
|
for (; pt->len; pt->len--)
|
||||||
|
{
|
||||||
|
if ((c = iobuf_get (pt->buf)) == -1)
|
||||||
|
{
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
log_error ("problem reading source (%u bytes remaining)\n",
|
log_error ("problem reading source (%u bytes remaining)\n",
|
||||||
(unsigned)pt->len);
|
(unsigned) pt->len);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
if( mfx->md )
|
if (mfx->md)
|
||||||
gcry_md_putc (mfx->md, c );
|
gcry_md_putc (mfx->md, c);
|
||||||
#ifndef HAVE_DOSISH_SYSTEM
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
if( c == '\r' ) /* convert to native line ending */
|
if (c == '\r') /* convert to native line ending */
|
||||||
continue; /* fixme: this hack might be too simple */
|
continue; /* fixme: this hack might be too simple */
|
||||||
#endif
|
#endif
|
||||||
if( fp )
|
if (fp)
|
||||||
{
|
{
|
||||||
if(opt.max_output && (++count)>opt.max_output)
|
if (opt.max_output && (++count) > opt.max_output)
|
||||||
{
|
{
|
||||||
log_error ("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname,"exceeded --max-output limit\n");
|
fname, "exceeded --max-output limit\n");
|
||||||
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
else if( putc( c, fp ) == EOF )
|
else if (putc (c, fp) == EOF)
|
||||||
{
|
{
|
||||||
if (ferror (fp))
|
if (ferror (fp))
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
else
|
else
|
||||||
rc = gpg_error (GPG_ERR_EOF);
|
rc = gpg_error (GPG_ERR_EOF);
|
||||||
log_error ("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname, strerror(errno) );
|
fname, strerror (errno));
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* binary mode */
|
else /* Binary mode. */
|
||||||
byte *buffer = xmalloc( 32768 );
|
{
|
||||||
while( pt->len ) {
|
byte *buffer = xmalloc (32768);
|
||||||
|
while (pt->len)
|
||||||
|
{
|
||||||
int len = pt->len > 32768 ? 32768 : pt->len;
|
int len = pt->len > 32768 ? 32768 : pt->len;
|
||||||
len = iobuf_read( pt->buf, buffer, len );
|
len = iobuf_read (pt->buf, buffer, len);
|
||||||
if( len == -1 ) {
|
if (len == -1)
|
||||||
|
{
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
log_error ("problem reading source (%u bytes remaining)\n",
|
log_error ("problem reading source (%u bytes remaining)\n",
|
||||||
(unsigned)pt->len);
|
(unsigned) pt->len);
|
||||||
xfree( buffer );
|
xfree (buffer);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
if( mfx->md )
|
if (mfx->md)
|
||||||
gcry_md_write ( mfx->md, buffer, len );
|
gcry_md_write (mfx->md, buffer, len);
|
||||||
if( fp )
|
if (fp)
|
||||||
{
|
{
|
||||||
if(opt.max_output && (count+=len)>opt.max_output)
|
if (opt.max_output && (count += len) > opt.max_output)
|
||||||
{
|
{
|
||||||
log_error ("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname,"exceeded --max-output limit\n");
|
fname, "exceeded --max-output limit\n");
|
||||||
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
||||||
xfree( buffer );
|
xfree (buffer);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
else if( fwrite( buffer, 1, len, fp ) != len )
|
else if (fwrite (buffer, 1, len, fp) != len)
|
||||||
{
|
{
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
log_error ("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname, strerror(errno) );
|
fname, strerror (errno));
|
||||||
xfree( buffer );
|
xfree (buffer);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pt->len -= len;
|
pt->len -= len;
|
||||||
}
|
}
|
||||||
xfree( buffer );
|
xfree (buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if( !clearsig ) {
|
else if (!clearsig)
|
||||||
if( convert ) { /* text mode */
|
{
|
||||||
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
if (convert)
|
||||||
if( mfx->md )
|
{ /* text mode */
|
||||||
gcry_md_putc (mfx->md, c );
|
while ((c = iobuf_get (pt->buf)) != -1)
|
||||||
|
{
|
||||||
|
if (mfx->md)
|
||||||
|
gcry_md_putc (mfx->md, c);
|
||||||
#ifndef HAVE_DOSISH_SYSTEM
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
if( convert && c == '\r' )
|
if (convert && c == '\r')
|
||||||
continue; /* fixme: this hack might be too simple */
|
continue; /* fixme: this hack might be too simple */
|
||||||
#endif
|
#endif
|
||||||
if( fp )
|
if (fp)
|
||||||
{
|
{
|
||||||
if(opt.max_output && (++count)>opt.max_output)
|
if (opt.max_output && (++count) > opt.max_output)
|
||||||
{
|
{
|
||||||
log_error("Error writing to `%s': %s\n",
|
log_error ("Error writing to `%s': %s\n",
|
||||||
fname,"exceeded --max-output limit\n");
|
fname, "exceeded --max-output limit\n");
|
||||||
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
else if( putc( c, fp ) == EOF )
|
else if (putc (c, fp) == EOF)
|
||||||
{
|
{
|
||||||
if ( ferror (fp ) )
|
if (ferror (fp))
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
else
|
else
|
||||||
rc = gpg_error (GPG_ERR_EOF);
|
rc = gpg_error (GPG_ERR_EOF);
|
||||||
log_error("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname, strerror(errno) );
|
fname, strerror (errno));
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* binary mode */
|
else
|
||||||
byte *buffer = xmalloc( 32768 );
|
{ /* binary mode */
|
||||||
|
byte *buffer = xmalloc (32768);
|
||||||
int eof_seen = 0;
|
int eof_seen = 0;
|
||||||
|
|
||||||
while ( !eof_seen ) {
|
while (!eof_seen)
|
||||||
|
{
|
||||||
/* Why do we check for len < 32768:
|
/* Why do we check for len < 32768:
|
||||||
* If we won't, we would practically read 2 EOFs but
|
* If we won't, we would practically read 2 EOFs but
|
||||||
* the first one has already popped the block_filter
|
* the first one has already popped the block_filter
|
||||||
* off and therefore we don't catch the boundary.
|
* off and therefore we don't catch the boundary.
|
||||||
* So, always assume EOF if iobuf_read returns less bytes
|
* So, always assume EOF if iobuf_read returns less bytes
|
||||||
* then requested */
|
* then requested */
|
||||||
int len = iobuf_read( pt->buf, buffer, 32768 );
|
int len = iobuf_read (pt->buf, buffer, 32768);
|
||||||
if( len == -1 )
|
if (len == -1)
|
||||||
break;
|
break;
|
||||||
if( len < 32768 )
|
if (len < 32768)
|
||||||
eof_seen = 1;
|
eof_seen = 1;
|
||||||
if( mfx->md )
|
if (mfx->md)
|
||||||
gcry_md_write ( mfx->md, buffer, len );
|
gcry_md_write (mfx->md, buffer, len);
|
||||||
if( fp )
|
if (fp)
|
||||||
{
|
{
|
||||||
if(opt.max_output && (count+=len)>opt.max_output)
|
if (opt.max_output && (count += len) > opt.max_output)
|
||||||
{
|
{
|
||||||
log_error("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname,"exceeded --max-output limit\n");
|
fname, "exceeded --max-output limit\n");
|
||||||
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
||||||
xfree( buffer );
|
xfree (buffer);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
else if( fwrite( buffer, 1, len, fp ) != len ) {
|
else if (fwrite (buffer, 1, len, fp) != len)
|
||||||
rc = (errno? gpg_error_from_syserror ()
|
{
|
||||||
|
rc = (errno ? gpg_error_from_syserror ()
|
||||||
: gpg_error (GPG_ERR_INTERNAL));
|
: gpg_error (GPG_ERR_INTERNAL));
|
||||||
log_error ("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname, strerror(errno) );
|
fname, strerror (errno));
|
||||||
xfree( buffer );
|
xfree (buffer);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xfree( buffer );
|
xfree (buffer);
|
||||||
}
|
}
|
||||||
pt->buf = NULL;
|
pt->buf = NULL;
|
||||||
}
|
}
|
||||||
else { /* clear text signature - don't hash the last cr,lf */
|
else /* Clear text signature - don't hash the last CR,LF. */
|
||||||
|
{
|
||||||
int state = 0;
|
int state = 0;
|
||||||
|
|
||||||
while( (c = iobuf_get(pt->buf)) != -1 ) {
|
while ((c = iobuf_get (pt->buf)) != -1)
|
||||||
if( fp )
|
|
||||||
{
|
{
|
||||||
if(opt.max_output && (++count)>opt.max_output)
|
if (fp)
|
||||||
|
{
|
||||||
|
if (opt.max_output && (++count) > opt.max_output)
|
||||||
{
|
{
|
||||||
log_error ("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname,"exceeded --max-output limit\n");
|
fname, "exceeded --max-output limit\n");
|
||||||
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
rc = gpg_error (GPG_ERR_TOO_LARGE);
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
else if( putc( c, fp ) == EOF )
|
else if (putc (c, fp) == EOF)
|
||||||
{
|
{
|
||||||
rc = (errno? gpg_error_from_syserror ()
|
rc = (errno ? gpg_error_from_syserror ()
|
||||||
: gpg_error (GPG_ERR_INTERNAL));
|
: gpg_error (GPG_ERR_INTERNAL));
|
||||||
log_error ("error writing to `%s': %s\n",
|
log_error ("error writing to `%s': %s\n",
|
||||||
fname, strerror(errno) );
|
fname, strerror (errno));
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( !mfx->md )
|
if (!mfx->md)
|
||||||
continue;
|
continue;
|
||||||
if( state == 2 ) {
|
if (state == 2)
|
||||||
gcry_md_putc (mfx->md, '\r' );
|
{
|
||||||
gcry_md_putc (mfx->md, '\n' );
|
gcry_md_putc (mfx->md, '\r');
|
||||||
|
gcry_md_putc (mfx->md, '\n');
|
||||||
state = 0;
|
state = 0;
|
||||||
}
|
}
|
||||||
if( !state ) {
|
if (!state)
|
||||||
if( c == '\r' )
|
{
|
||||||
|
if (c == '\r')
|
||||||
state = 1;
|
state = 1;
|
||||||
else if( c == '\n' )
|
else if (c == '\n')
|
||||||
state = 2;
|
state = 2;
|
||||||
else
|
else
|
||||||
gcry_md_putc(mfx->md, c );
|
gcry_md_putc (mfx->md, c);
|
||||||
}
|
}
|
||||||
else if( state == 1 ) {
|
else if (state == 1)
|
||||||
if( c == '\n' )
|
{
|
||||||
|
if (c == '\n')
|
||||||
state = 2;
|
state = 2;
|
||||||
else {
|
else
|
||||||
gcry_md_putc(mfx->md, '\r' );
|
{
|
||||||
if( c == '\r' )
|
gcry_md_putc (mfx->md, '\r');
|
||||||
|
if (c == '\r')
|
||||||
state = 1;
|
state = 1;
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
state = 0;
|
state = 0;
|
||||||
gcry_md_putc(mfx->md, c );
|
gcry_md_putc (mfx->md, c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -389,63 +418,72 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
|
|||||||
pt->buf = NULL;
|
pt->buf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( fp && fp != stdout && fclose(fp) ) {
|
if (fp && fp != stdout && fclose (fp))
|
||||||
rc = (errno? gpg_error_from_syserror ()
|
{
|
||||||
|
rc = (errno ? gpg_error_from_syserror ()
|
||||||
: gpg_error (GPG_ERR_INTERNAL));
|
: gpg_error (GPG_ERR_INTERNAL));
|
||||||
log_error ("error closing `%s': %s\n", fname, strerror(errno) );
|
log_error ("error closing `%s': %s\n", fname, strerror (errno));
|
||||||
fp = NULL;
|
fp = NULL;
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
fp = NULL;
|
fp = NULL;
|
||||||
|
|
||||||
leave:
|
leave:
|
||||||
/* Make sure that stdout gets flushed after the plaintext has
|
/* Make sure that stdout gets flushed after the plaintext has been
|
||||||
been handled. This is for extra security as we do a
|
handled. This is for extra security as we do a flush anyway
|
||||||
flush anyway before checking the signature. */
|
before checking the signature. */
|
||||||
fflush (stdout);
|
fflush (stdout);
|
||||||
|
|
||||||
if( fp && fp != stdout )
|
if (fp && fp != stdout)
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
xfree(fname);
|
xfree (fname);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
do_hash( gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode )
|
do_hash (gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode)
|
||||||
{
|
{
|
||||||
text_filter_context_t tfx;
|
text_filter_context_t tfx;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
if( textmode ) {
|
if (textmode)
|
||||||
memset( &tfx, 0, sizeof tfx);
|
{
|
||||||
iobuf_push_filter( fp, text_filter, &tfx );
|
memset (&tfx, 0, sizeof tfx);
|
||||||
|
iobuf_push_filter (fp, text_filter, &tfx);
|
||||||
}
|
}
|
||||||
if( md2 ) { /* work around a strange behaviour in pgp2 */
|
if (md2)
|
||||||
|
{ /* work around a strange behaviour in pgp2 */
|
||||||
/* It seems that at least PGP5 converts a single CR to a CR,LF too */
|
/* It seems that at least PGP5 converts a single CR to a CR,LF too */
|
||||||
int lc = -1;
|
int lc = -1;
|
||||||
while( (c = iobuf_get(fp)) != -1 ) {
|
while ((c = iobuf_get (fp)) != -1)
|
||||||
if( c == '\n' && lc == '\r' )
|
{
|
||||||
|
if (c == '\n' && lc == '\r')
|
||||||
gcry_md_putc (md2, c);
|
gcry_md_putc (md2, c);
|
||||||
else if( c == '\n' ) {
|
else if (c == '\n')
|
||||||
|
{
|
||||||
gcry_md_putc (md2, '\r');
|
gcry_md_putc (md2, '\r');
|
||||||
gcry_md_putc (md2, c);
|
gcry_md_putc (md2, c);
|
||||||
}
|
}
|
||||||
else if( c != '\n' && lc == '\r' ) {
|
else if (c != '\n' && lc == '\r')
|
||||||
|
{
|
||||||
gcry_md_putc (md2, '\n');
|
gcry_md_putc (md2, '\n');
|
||||||
gcry_md_putc (md2, c);
|
gcry_md_putc (md2, c);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
gcry_md_putc (md2, c);
|
gcry_md_putc (md2, c);
|
||||||
|
|
||||||
if( md )
|
if (md)
|
||||||
gcry_md_putc (md, c );
|
gcry_md_putc (md, c);
|
||||||
lc = c;
|
lc = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
while( (c = iobuf_get(fp)) != -1 ) {
|
{
|
||||||
if( md )
|
while ((c = iobuf_get (fp)) != -1)
|
||||||
gcry_md_putc (md, c );
|
{
|
||||||
|
if (md)
|
||||||
|
gcry_md_putc (md, c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -457,7 +495,7 @@ do_hash( gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode )
|
|||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
|
ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
|
||||||
const char *inname, int textmode )
|
const char *inname, int textmode)
|
||||||
{
|
{
|
||||||
progress_filter_context_t *pfx;
|
progress_filter_context_t *pfx;
|
||||||
char *answer = NULL;
|
char *answer = NULL;
|
||||||
@ -465,58 +503,65 @@ ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
|
|||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
pfx = new_progress_context ();
|
pfx = new_progress_context ();
|
||||||
fp = open_sigfile ( inname, pfx ); /* Open default file. */
|
fp = open_sigfile (inname, pfx); /* Open default file. */
|
||||||
|
|
||||||
if( !fp && !opt.batch ) {
|
if (!fp && !opt.batch)
|
||||||
int any=0;
|
{
|
||||||
tty_printf(_("Detached signature.\n"));
|
int any = 0;
|
||||||
do {
|
tty_printf (_("Detached signature.\n"));
|
||||||
|
do
|
||||||
|
{
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
xfree(answer);
|
xfree (answer);
|
||||||
tty_enable_completion(NULL);
|
tty_enable_completion (NULL);
|
||||||
name = cpr_get("detached_signature.filename",
|
name = cpr_get ("detached_signature.filename",
|
||||||
_("Please enter name of data file: "));
|
_("Please enter name of data file: "));
|
||||||
tty_disable_completion();
|
tty_disable_completion ();
|
||||||
cpr_kill_prompt();
|
cpr_kill_prompt ();
|
||||||
answer=make_filename(name,(void *)NULL);
|
answer = make_filename (name, (void *) NULL);
|
||||||
xfree(name);
|
xfree (name);
|
||||||
|
|
||||||
if( any && !*answer ) {
|
if (any && !*answer)
|
||||||
rc = gpg_error (GPG_ERR_GENERAL); /*G10ERR_READ_FILE*/
|
{
|
||||||
|
rc = gpg_error (GPG_ERR_GENERAL); /*G10ERR_READ_FILE */
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
fp = iobuf_open(answer);
|
fp = iobuf_open (answer);
|
||||||
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
||||||
{
|
{
|
||||||
iobuf_close (fp);
|
iobuf_close (fp);
|
||||||
fp = NULL;
|
fp = NULL;
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
}
|
}
|
||||||
if( !fp && errno == ENOENT ) {
|
if (!fp && errno == ENOENT)
|
||||||
tty_printf("No such file, try again or hit enter to quit.\n");
|
{
|
||||||
|
tty_printf ("No such file, try again or hit enter to quit.\n");
|
||||||
any++;
|
any++;
|
||||||
}
|
}
|
||||||
else if( !fp )
|
else if (!fp)
|
||||||
{
|
{
|
||||||
rc = gpg_error_from_syserror ();
|
rc = gpg_error_from_syserror ();
|
||||||
log_error(_("can't open `%s': %s\n"), answer, strerror(errno));
|
log_error (_("can't open `%s': %s\n"), answer,
|
||||||
|
strerror (errno));
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
} while( !fp );
|
}
|
||||||
|
while (!fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !fp ) {
|
if (!fp)
|
||||||
if( opt.verbose )
|
{
|
||||||
log_info(_("reading stdin ...\n"));
|
if (opt.verbose)
|
||||||
fp = iobuf_open( NULL );
|
log_info (_("reading stdin ...\n"));
|
||||||
assert(fp);
|
fp = iobuf_open (NULL);
|
||||||
|
assert (fp);
|
||||||
}
|
}
|
||||||
do_hash( md, md2, fp, textmode );
|
do_hash (md, md2, fp, textmode);
|
||||||
iobuf_close(fp);
|
iobuf_close (fp);
|
||||||
|
|
||||||
leave:
|
leave:
|
||||||
xfree(answer);
|
xfree (answer);
|
||||||
release_progress_context (pfx);
|
release_progress_context (pfx);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@ -528,8 +573,8 @@ ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
|
|||||||
* If FILES is NULL, hash stdin.
|
* If FILES is NULL, hash stdin.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
|
hash_datafiles (gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
|
||||||
const char *sigfilename, int textmode )
|
const char *sigfilename, int textmode)
|
||||||
{
|
{
|
||||||
progress_filter_context_t *pfx;
|
progress_filter_context_t *pfx;
|
||||||
IOBUF fp;
|
IOBUF fp;
|
||||||
@ -537,12 +582,14 @@ hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
|
|||||||
|
|
||||||
pfx = new_progress_context ();
|
pfx = new_progress_context ();
|
||||||
|
|
||||||
if( !files ) {
|
if (!files)
|
||||||
|
{
|
||||||
/* check whether we can open the signed material */
|
/* check whether we can open the signed material */
|
||||||
fp = open_sigfile( sigfilename, pfx );
|
fp = open_sigfile (sigfilename, pfx);
|
||||||
if( fp ) {
|
if (fp)
|
||||||
do_hash( md, md2, fp, textmode );
|
{
|
||||||
iobuf_close(fp);
|
do_hash (md, md2, fp, textmode);
|
||||||
|
iobuf_close (fp);
|
||||||
release_progress_context (pfx);
|
release_progress_context (pfx);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -552,24 +599,26 @@ hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (sl=files; sl; sl = sl->next ) {
|
for (sl = files; sl; sl = sl->next)
|
||||||
fp = iobuf_open( sl->d );
|
{
|
||||||
|
fp = iobuf_open (sl->d);
|
||||||
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
if (fp && is_secured_file (iobuf_get_fd (fp)))
|
||||||
{
|
{
|
||||||
iobuf_close (fp);
|
iobuf_close (fp);
|
||||||
fp = NULL;
|
fp = NULL;
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
}
|
}
|
||||||
if( !fp ) {
|
if (!fp)
|
||||||
|
{
|
||||||
int rc = gpg_error_from_syserror ();
|
int rc = gpg_error_from_syserror ();
|
||||||
log_error(_("can't open signed data `%s'\n"),
|
log_error (_("can't open signed data `%s'\n"),
|
||||||
print_fname_stdin(sl->d));
|
print_fname_stdin (sl->d));
|
||||||
release_progress_context (pfx);
|
release_progress_context (pfx);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
handle_progress (pfx, fp, sl->d);
|
handle_progress (pfx, fp, sl->d);
|
||||||
do_hash( md, md2, fp, textmode );
|
do_hash (md, md2, fp, textmode);
|
||||||
iobuf_close(fp);
|
iobuf_close (fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
release_progress_context (pfx);
|
release_progress_context (pfx);
|
||||||
@ -580,8 +629,8 @@ hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
|
|||||||
/* Hash the data from file descriptor DATA_FD and append the hash to hash
|
/* Hash the data from file descriptor DATA_FD and append the hash to hash
|
||||||
contexts MD and MD2. */
|
contexts MD and MD2. */
|
||||||
int
|
int
|
||||||
hash_datafile_by_fd ( gcry_md_hd_t md, gcry_md_hd_t md2, int data_fd,
|
hash_datafile_by_fd (gcry_md_hd_t md, gcry_md_hd_t md2, int data_fd,
|
||||||
int textmode )
|
int textmode)
|
||||||
{
|
{
|
||||||
progress_filter_context_t *pfx = new_progress_context ();
|
progress_filter_context_t *pfx = new_progress_context ();
|
||||||
iobuf_t fp;
|
iobuf_t fp;
|
||||||
@ -593,10 +642,10 @@ hash_datafile_by_fd ( gcry_md_hd_t md, gcry_md_hd_t md2, int data_fd,
|
|||||||
fp = NULL;
|
fp = NULL;
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
}
|
}
|
||||||
if ( !fp )
|
if (!fp)
|
||||||
{
|
{
|
||||||
int rc = gpg_error_from_syserror ();
|
int rc = gpg_error_from_syserror ();
|
||||||
log_error ( _("can't open signed data fd=%d: %s\n"),
|
log_error (_("can't open signed data fd=%d: %s\n"),
|
||||||
data_fd, strerror (errno));
|
data_fd, strerror (errno));
|
||||||
release_progress_context (pfx);
|
release_progress_context (pfx);
|
||||||
return rc;
|
return rc;
|
||||||
@ -604,9 +653,9 @@ hash_datafile_by_fd ( gcry_md_hd_t md, gcry_md_hd_t md2, int data_fd,
|
|||||||
|
|
||||||
handle_progress (pfx, fp, NULL);
|
handle_progress (pfx, fp, NULL);
|
||||||
|
|
||||||
do_hash ( md, md2, fp, textmode);
|
do_hash (md, md2, fp, textmode);
|
||||||
|
|
||||||
iobuf_close(fp);
|
iobuf_close (fp);
|
||||||
|
|
||||||
release_progress_context (pfx);
|
release_progress_context (pfx);
|
||||||
return 0;
|
return 0;
|
||||||
@ -619,26 +668,26 @@ hash_datafile_by_fd ( gcry_md_hd_t md, gcry_md_hd_t md2, int data_fd,
|
|||||||
filenames at all, set the field empty. */
|
filenames at all, set the field empty. */
|
||||||
|
|
||||||
PKT_plaintext *
|
PKT_plaintext *
|
||||||
setup_plaintext_name(const char *filename,IOBUF iobuf)
|
setup_plaintext_name (const char *filename, IOBUF iobuf)
|
||||||
{
|
{
|
||||||
PKT_plaintext *pt;
|
PKT_plaintext *pt;
|
||||||
|
|
||||||
if(filename || opt.set_filename)
|
if (filename || opt.set_filename)
|
||||||
{
|
{
|
||||||
char *s;
|
char *s;
|
||||||
|
|
||||||
if(opt.set_filename)
|
if (opt.set_filename)
|
||||||
s=make_basename(opt.set_filename,iobuf_get_real_fname(iobuf));
|
s = make_basename (opt.set_filename, iobuf_get_real_fname (iobuf));
|
||||||
else if(filename && !opt.flags.utf8_filename)
|
else if (filename && !opt.flags.utf8_filename)
|
||||||
{
|
{
|
||||||
char *tmp=native_to_utf8(filename);
|
char *tmp = native_to_utf8 (filename);
|
||||||
s=make_basename(tmp,iobuf_get_real_fname(iobuf));
|
s = make_basename (tmp, iobuf_get_real_fname (iobuf));
|
||||||
xfree(tmp);
|
xfree (tmp);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
s=make_basename(filename,iobuf_get_real_fname(iobuf));
|
s = make_basename (filename, iobuf_get_real_fname (iobuf));
|
||||||
|
|
||||||
pt = xmalloc (sizeof *pt + strlen(s) - 1);
|
pt = xmalloc (sizeof *pt + strlen (s) - 1);
|
||||||
pt->namelen = strlen (s);
|
pt->namelen = strlen (s);
|
||||||
memcpy (pt->name, s, pt->namelen);
|
memcpy (pt->name, s, pt->namelen);
|
||||||
xfree (s);
|
xfree (s);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user