1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-01 22:28:02 +02:00

Re-indented.

This commit is contained in:
Werner Koch 2009-10-01 09:56:51 +00:00
parent fafe852c33
commit b432da7574

View File

@ -1,6 +1,6 @@
/* plaintext.c - process plaintext packets
* 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.
*
@ -40,12 +40,10 @@
#include "i18n.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.
*/
/* 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
* 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,
int nooutput, int clearsig)
@ -61,8 +59,8 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
#endif
/* Let people know what the plaintext info is. This allows the
receiving program to try and do something different based on
the format code (say, recode UTF-8 to local). */
receiving program to try and do something different based on the
format code (say, recode UTF-8 to local). */
if (!nooutput && is_status_enabled ())
{
char status[50];
@ -73,7 +71,8 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
status message. */
fflush (stdout);
sprintf(status,"%X %lu ",(byte)pt->mode,(ulong)pt->timestamp);
snprintf (status, sizeof status,
"%X %lu ", (byte) pt->mode, (ulong) pt->timestamp);
write_status_text_and_buffer (STATUS_PLAINTEXT,
status, pt->name, pt->namelen, 0);
@ -84,22 +83,26 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
}
}
/* create the filename as C string */
/* Create the filename as C string. */
if (nooutput)
;
else if( opt.outfile ) {
else if (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"));
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 = ask_outfile_name (pt->name, pt->namelen);
if( !fname ) {
if (!fname)
{
rc = gpg_error (GPG_ERR_GENERAL); /* Can't create file. */
goto leave;
}
@ -117,12 +120,16 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
setmode (fileno (fp), O_BINARY);
#endif
}
else {
while( !overwrite_filep (fname) ) {
else
{
while (!overwrite_filep (fname))
{
char *tmp = ask_outfile_name (NULL, 0);
if ( !tmp || !*tmp ) {
if (!tmp || !*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;
}
xfree (fname);
@ -140,7 +147,8 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
log_error (_("error creating `%s': %s\n"), fname, strerror (errno));
goto leave;
}
else if( !(fp = fopen(fname,"wb")) ) {
else if (!(fp = fopen (fname, "wb")))
{
rc = gpg_error_from_syserror ();
log_error (_("error creating `%s': %s\n"), fname, strerror (errno));
goto leave;
@ -156,9 +164,11 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
if (fp || nooutput)
;
else {
else
{
fp = fopen (fname, "wb");
if( !fp ) {
if (!fp)
{
log_error (_("error creating `%s': %s\n"), fname, strerror (errno));
rc = G10ERR_CREATE_FILE;
if (errno == 106)
@ -169,28 +179,31 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
/* If there's a ,xxx extension in the embedded filename,
use that, else check whether the user input (in fname)
has a ,xxx appended, then use that in preference */
if( (c = riscos_get_filetype_from_string( pt->name,
pt->namelen )) != -1 )
if ((c = riscos_get_filetype_from_string (pt->name, pt->namelen)) != -1)
filetype = c;
if( (c = riscos_get_filetype_from_string( fname,
strlen(fname) )) != -1 )
if ((c = riscos_get_filetype_from_string (fname, strlen (fname))) != -1)
filetype = c;
riscos_set_filetype_by_number (fname, filetype);
}
#endif /* __riscos__ */
if( !pt->is_partial ) {
if (!pt->is_partial)
{
/* We have an actual length (which might be zero). */
if (clearsig) {
if (clearsig)
{
log_error ("clearsig encountered while not expected\n");
rc = G10ERR_UNEXPECTED;
goto leave;
}
if( convert ) { /* text mode */
for( ; pt->len; pt->len-- ) {
if( (c = iobuf_get(pt->buf)) == -1 ) {
if (convert) /* Text mode. */
{
for (; pt->len; pt->len--)
{
if ((c = iobuf_get (pt->buf)) == -1)
{
rc = gpg_error_from_syserror ();
log_error ("problem reading source (%u bytes remaining)\n",
(unsigned) pt->len);
@ -224,12 +237,15 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
}
}
}
else { /* binary mode */
else /* Binary mode. */
{
byte *buffer = xmalloc (32768);
while( pt->len ) {
while (pt->len)
{
int len = pt->len > 32768 ? 32768 : pt->len;
len = iobuf_read (pt->buf, buffer, len);
if( len == -1 ) {
if (len == -1)
{
rc = gpg_error_from_syserror ();
log_error ("problem reading source (%u bytes remaining)\n",
(unsigned) pt->len);
@ -262,9 +278,12 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
xfree (buffer);
}
}
else if( !clearsig ) {
if( convert ) { /* text mode */
while( (c = iobuf_get(pt->buf)) != -1 ) {
else if (!clearsig)
{
if (convert)
{ /* text mode */
while ((c = iobuf_get (pt->buf)) != -1)
{
if (mfx->md)
gcry_md_putc (mfx->md, c);
#ifndef HAVE_DOSISH_SYSTEM
@ -293,11 +312,13 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
}
}
}
else { /* binary mode */
else
{ /* binary mode */
byte *buffer = xmalloc (32768);
int eof_seen = 0;
while ( !eof_seen ) {
while (!eof_seen)
{
/* Why do we check for len < 32768:
* If we won't, we would practically read 2 EOFs but
* the first one has already popped the block_filter
@ -321,7 +342,8 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
xfree (buffer);
goto leave;
}
else if( fwrite( buffer, 1, len, fp ) != len ) {
else if (fwrite (buffer, 1, len, fp) != len)
{
rc = (errno ? gpg_error_from_syserror ()
: gpg_error (GPG_ERR_INTERNAL));
log_error ("error writing to `%s': %s\n",
@ -335,10 +357,12 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
}
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;
while( (c = iobuf_get(pt->buf)) != -1 ) {
while ((c = iobuf_get (pt->buf)) != -1)
{
if (fp)
{
if (opt.max_output && (++count) > opt.max_output)
@ -359,12 +383,14 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
}
if (!mfx->md)
continue;
if( state == 2 ) {
if (state == 2)
{
gcry_md_putc (mfx->md, '\r');
gcry_md_putc (mfx->md, '\n');
state = 0;
}
if( !state ) {
if (!state)
{
if (c == '\r')
state = 1;
else if (c == '\n')
@ -372,14 +398,17 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
else
gcry_md_putc (mfx->md, c);
}
else if( state == 1 ) {
else if (state == 1)
{
if (c == '\n')
state = 2;
else {
else
{
gcry_md_putc (mfx->md, '\r');
if (c == '\r')
state = 1;
else {
else
{
state = 0;
gcry_md_putc (mfx->md, c);
}
@ -389,7 +418,8 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
pt->buf = NULL;
}
if( fp && fp != stdout && fclose(fp) ) {
if (fp && fp != stdout && fclose (fp))
{
rc = (errno ? gpg_error_from_syserror ()
: gpg_error (GPG_ERR_INTERNAL));
log_error ("error closing `%s': %s\n", fname, strerror (errno));
@ -399,9 +429,9 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
fp = NULL;
leave:
/* Make sure that stdout gets flushed after the plaintext has
been handled. This is for extra security as we do a
flush anyway before checking the signature. */
/* Make sure that stdout gets flushed after the plaintext has been
handled. This is for extra security as we do a flush anyway
before checking the signature. */
fflush (stdout);
if (fp && fp != stdout)
@ -410,27 +440,33 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
return rc;
}
static void
do_hash (gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode)
{
text_filter_context_t tfx;
int c;
if( textmode ) {
if (textmode)
{
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 */
int lc = -1;
while( (c = iobuf_get(fp)) != -1 ) {
while ((c = iobuf_get (fp)) != -1)
{
if (c == '\n' && lc == '\r')
gcry_md_putc (md2, c);
else if( c == '\n' ) {
else if (c == '\n')
{
gcry_md_putc (md2, '\r');
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, c);
}
@ -442,8 +478,10 @@ do_hash( gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode )
lc = c;
}
}
else {
while( (c = iobuf_get(fp)) != -1 ) {
else
{
while ((c = iobuf_get (fp)) != -1)
{
if (md)
gcry_md_putc (md, c);
}
@ -467,10 +505,12 @@ ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
pfx = new_progress_context ();
fp = open_sigfile (inname, pfx); /* Open default file. */
if( !fp && !opt.batch ) {
if (!fp && !opt.batch)
{
int any = 0;
tty_printf (_("Detached signature.\n"));
do {
do
{
char *name;
xfree (answer);
@ -482,7 +522,8 @@ ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
answer = make_filename (name, (void *) NULL);
xfree (name);
if( any && !*answer ) {
if (any && !*answer)
{
rc = gpg_error (GPG_ERR_GENERAL); /*G10ERR_READ_FILE */
goto leave;
}
@ -493,20 +534,24 @@ ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
fp = NULL;
errno = EPERM;
}
if( !fp && errno == ENOENT ) {
if (!fp && errno == ENOENT)
{
tty_printf ("No such file, try again or hit enter to quit.\n");
any++;
}
else if (!fp)
{
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;
}
} while( !fp );
}
while (!fp);
}
if( !fp ) {
if (!fp)
{
if (opt.verbose)
log_info (_("reading stdin ...\n"));
fp = iobuf_open (NULL);
@ -537,10 +582,12 @@ hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
pfx = new_progress_context ();
if( !files ) {
if (!files)
{
/* check whether we can open the signed material */
fp = open_sigfile (sigfilename, pfx);
if( fp ) {
if (fp)
{
do_hash (md, md2, fp, textmode);
iobuf_close (fp);
release_progress_context (pfx);
@ -552,7 +599,8 @@ 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);
if (fp && is_secured_file (iobuf_get_fd (fp)))
{
@ -560,7 +608,8 @@ hash_datafiles( gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
fp = NULL;
errno = EPERM;
}
if( !fp ) {
if (!fp)
{
int rc = gpg_error_from_syserror ();
log_error (_("can't open signed data `%s'\n"),
print_fname_stdin (sl->d));