1997-12-23 17:30:18 +00:00
|
|
|
/* textfilter.c
|
2004-12-10 05:35:54 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
|
1997-12-23 17:30:18 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-12-23 17:30:18 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-12-23 17:30:18 +00:00
|
|
|
* 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.
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-12-23 17:30:18 +00:00
|
|
|
* 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
|
2005-05-31 08:39:18 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
1997-12-23 17:30:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "errors.h"
|
|
|
|
#include "iobuf.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
#include "memory.h"
|
1997-12-23 17:30:18 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "filter.h"
|
1999-01-17 10:06:03 +00:00
|
|
|
#include "i18n.h"
|
2000-07-14 17:34:53 +00:00
|
|
|
#include "options.h"
|
1997-12-23 17:30:18 +00:00
|
|
|
|
2000-07-14 17:34:53 +00:00
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
* armor.c, g10.c, kbnode.c, misc.c, pkclist.c, sign.c, build-packet.c,
getkey.c, keydb.c, openfile.c, plaintext.c, status.c, gpgv.c, keygen.c,
options.h, sig-check.c, tdbio.h, encode.c, mainproc.c, parse-packet.c,
signal.c, textfilter.c: Edit all preprocessor instructions to remove
whitespace before the '#'. This is not required by C89, but there are some
compilers out there that don't like it.
2003-05-24 21:50:33 +00:00
|
|
|
#define LF "\r\n"
|
2000-07-14 17:34:53 +00:00
|
|
|
#else
|
* armor.c, g10.c, kbnode.c, misc.c, pkclist.c, sign.c, build-packet.c,
getkey.c, keydb.c, openfile.c, plaintext.c, status.c, gpgv.c, keygen.c,
options.h, sig-check.c, tdbio.h, encode.c, mainproc.c, parse-packet.c,
signal.c, textfilter.c: Edit all preprocessor instructions to remove
whitespace before the '#'. This is not required by C89, but there are some
compilers out there that don't like it.
2003-05-24 21:50:33 +00:00
|
|
|
#define LF "\n"
|
2000-07-14 17:34:53 +00:00
|
|
|
#endif
|
1997-12-23 17:30:18 +00:00
|
|
|
|
1999-01-20 18:10:35 +00:00
|
|
|
#define MAX_LINELEN 19995 /* a little bit smaller than in armor.c */
|
|
|
|
/* to make sure that a warning is displayed while */
|
|
|
|
/* creating a message */
|
|
|
|
|
1999-05-20 12:11:41 +00:00
|
|
|
static unsigned
|
|
|
|
len_without_trailing_chars( byte *line, unsigned len, const char *trimchars )
|
1999-01-20 18:10:35 +00:00
|
|
|
{
|
|
|
|
byte *p, *mark;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
|
1999-05-20 12:11:41 +00:00
|
|
|
if( strchr( trimchars, *p ) ) {
|
1999-01-20 18:10:35 +00:00
|
|
|
if( !mark )
|
|
|
|
mark = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mark = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mark? (mark - line) : len;
|
|
|
|
}
|
|
|
|
|
2004-12-11 04:47:33 +00:00
|
|
|
|
1999-01-20 18:10:35 +00:00
|
|
|
static int
|
|
|
|
standard( text_filter_context_t *tfx, IOBUF a,
|
|
|
|
byte *buf, size_t size, size_t *ret_len)
|
|
|
|
{
|
|
|
|
int rc=0;
|
|
|
|
size_t len = 0;
|
|
|
|
unsigned maxlen;
|
|
|
|
|
|
|
|
assert( size > 10 );
|
|
|
|
size -= 2; /* reserve 2 bytes to append CR,LF */
|
|
|
|
while( !rc && len < size ) {
|
|
|
|
int lf_seen;
|
|
|
|
|
|
|
|
while( len < size && tfx->buffer_pos < tfx->buffer_len )
|
|
|
|
buf[len++] = tfx->buffer[tfx->buffer_pos++];
|
|
|
|
if( len >= size )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* read the next line */
|
|
|
|
maxlen = MAX_LINELEN;
|
|
|
|
tfx->buffer_pos = 0;
|
|
|
|
tfx->buffer_len = iobuf_read_line( a, &tfx->buffer,
|
|
|
|
&tfx->buffer_size, &maxlen );
|
|
|
|
if( !maxlen )
|
|
|
|
tfx->truncated++;
|
|
|
|
if( !tfx->buffer_len ) {
|
|
|
|
if( !len )
|
|
|
|
rc = -1; /* eof */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lf_seen = tfx->buffer[tfx->buffer_len-1] == '\n';
|
2004-12-10 05:35:54 +00:00
|
|
|
|
|
|
|
/* The story behind this is that 2440 says that textmode
|
|
|
|
hashes should canonicalize line endings to CRLF and remove
|
|
|
|
spaces and tabs. 2440bis-12 says to just canonicalize to
|
2004-12-20 05:19:09 +00:00
|
|
|
CRLF. 1.4.0 was released using the bis-12 behavior, but it
|
|
|
|
was discovered that many mail clients do not canonicalize
|
|
|
|
PGP/MIME signature text appropriately (and were relying on
|
|
|
|
GnuPG to handle trailing spaces). So, we default to the
|
|
|
|
2440 behavior, but use the 2440bis-12 behavior if the user
|
|
|
|
specifies --no-rfc2440-text. The default will be changed
|
|
|
|
at some point in the future when the mail clients have been
|
|
|
|
upgraded. Aside from PGP/MIME and broken mail clients,
|
|
|
|
this makes no difference to any signatures in the real
|
|
|
|
world except for a textmode detached signature. PGP always
|
|
|
|
used the 2440bis-12 behavior (ignoring 2440 itself), so
|
|
|
|
this actually makes us compatible with PGP textmode
|
|
|
|
detached signatures for the first time. */
|
|
|
|
if(opt.rfc2440_text)
|
2004-12-10 05:35:54 +00:00
|
|
|
tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len,
|
|
|
|
" \t\r\n");
|
|
|
|
else
|
|
|
|
tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len,
|
|
|
|
"\r\n");
|
|
|
|
|
1999-01-20 18:10:35 +00:00
|
|
|
if( lf_seen ) {
|
|
|
|
tfx->buffer[tfx->buffer_len++] = '\r';
|
|
|
|
tfx->buffer[tfx->buffer_len++] = '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ret_len = len;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-23 17:30:18 +00:00
|
|
|
/****************
|
|
|
|
* The filter is used to make canonical text: Lines are terminated by
|
|
|
|
* CR, LF, trailing white spaces are removed.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
text_filter( void *opaque, int control,
|
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
|
|
|
size_t size = *ret_len;
|
|
|
|
text_filter_context_t *tfx = opaque;
|
1998-02-04 18:54:31 +00:00
|
|
|
int rc=0;
|
1997-12-23 17:30:18 +00:00
|
|
|
|
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) {
|
1999-03-02 15:48:37 +00:00
|
|
|
rc = standard( tfx, a, buf, size, ret_len );
|
1997-12-23 17:30:18 +00:00
|
|
|
}
|
1999-01-17 10:06:03 +00:00
|
|
|
else if( control == IOBUFCTRL_FREE ) {
|
|
|
|
if( tfx->truncated )
|
|
|
|
log_error(_("can't handle text lines longer than %d characters\n"),
|
|
|
|
MAX_LINELEN );
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free( tfx->buffer );
|
1999-01-17 10:06:03 +00:00
|
|
|
tfx->buffer = NULL;
|
|
|
|
}
|
1999-01-20 18:10:35 +00:00
|
|
|
else if( control == IOBUFCTRL_DESC )
|
|
|
|
*(char**)buf = "text_filter";
|
1997-12-23 17:30:18 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-02 15:48:37 +00:00
|
|
|
/****************
|
|
|
|
* Copy data from INP to OUT and do some escaping if requested.
|
|
|
|
* md is updated as required by rfc2440
|
|
|
|
*/
|
|
|
|
int
|
2002-06-29 13:46:34 +00:00
|
|
|
copy_clearsig_text( IOBUF out, IOBUF inp, MD_HANDLE md,
|
1999-05-20 12:11:41 +00:00
|
|
|
int escape_dash, int escape_from, int pgp2mode )
|
1999-03-02 15:48:37 +00:00
|
|
|
{
|
|
|
|
unsigned maxlen;
|
|
|
|
byte *buffer = NULL; /* malloced buffer */
|
|
|
|
unsigned bufsize; /* and size of this buffer */
|
|
|
|
unsigned n;
|
|
|
|
int truncated = 0;
|
|
|
|
int pending_lf = 0;
|
|
|
|
|
2000-07-14 17:34:53 +00:00
|
|
|
if( !opt.pgp2_workarounds )
|
|
|
|
pgp2mode = 0;
|
|
|
|
|
1999-03-02 15:48:37 +00:00
|
|
|
if( !escape_dash )
|
|
|
|
escape_from = 0;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
maxlen = MAX_LINELEN;
|
|
|
|
n = iobuf_read_line( inp, &buffer, &bufsize, &maxlen );
|
|
|
|
if( !maxlen )
|
|
|
|
truncated++;
|
|
|
|
|
|
|
|
if( !n )
|
|
|
|
break; /* read_line has returned eof */
|
|
|
|
|
|
|
|
/* update the message digest */
|
|
|
|
if( escape_dash ) {
|
|
|
|
if( pending_lf ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc( md, '\r' );
|
|
|
|
md_putc( md, '\n' );
|
1999-03-02 15:48:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( md, buffer,
|
1999-05-20 12:11:41 +00:00
|
|
|
len_without_trailing_chars( buffer, n,
|
|
|
|
pgp2mode? " \r\n":" \t\r\n"));
|
1999-03-02 15:48:37 +00:00
|
|
|
}
|
|
|
|
else
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( md, buffer, n );
|
1999-03-02 15:48:37 +00:00
|
|
|
pending_lf = buffer[n-1] == '\n';
|
|
|
|
|
|
|
|
/* write the output */
|
|
|
|
if( ( escape_dash && *buffer == '-')
|
|
|
|
|| ( escape_from && n > 4 && !memcmp(buffer, "From ", 5 ) ) ) {
|
|
|
|
iobuf_put( out, '-' );
|
|
|
|
iobuf_put( out, ' ' );
|
|
|
|
}
|
2000-07-14 17:34:53 +00:00
|
|
|
|
* armor.c, g10.c, kbnode.c, misc.c, pkclist.c, sign.c, build-packet.c,
getkey.c, keydb.c, openfile.c, plaintext.c, status.c, gpgv.c, keygen.c,
options.h, sig-check.c, tdbio.h, encode.c, mainproc.c, parse-packet.c,
signal.c, textfilter.c: Edit all preprocessor instructions to remove
whitespace before the '#'. This is not required by C89, but there are some
compilers out there that don't like it.
2003-05-24 21:50:33 +00:00
|
|
|
#if 0 /*defined(HAVE_DOSISH_SYSTEM)*/
|
2000-07-14 17:34:53 +00:00
|
|
|
/* We don't use this anymore because my interpretation of rfc2440 7.1
|
|
|
|
* is that there is no conversion needed. If one decides to
|
|
|
|
* clearsign a unix file on a DOS box he will get a mixed line endings.
|
|
|
|
* If at some point it turns out, that a conversion is a nice feature
|
|
|
|
* we can make an option out of it.
|
|
|
|
*/
|
|
|
|
/* make sure the lines do end in CR,LF */
|
|
|
|
if( n > 1 && ( (buffer[n-2] == '\r' && buffer[n-1] == '\n' )
|
|
|
|
|| (buffer[n-2] == '\n' && buffer[n-1] == '\r'))) {
|
|
|
|
iobuf_write( out, buffer, n-2 );
|
|
|
|
iobuf_put( out, '\r');
|
|
|
|
iobuf_put( out, '\n');
|
|
|
|
}
|
|
|
|
else if( n && buffer[n-1] == '\n' ) {
|
|
|
|
iobuf_write( out, buffer, n-1 );
|
|
|
|
iobuf_put( out, '\r');
|
|
|
|
iobuf_put( out, '\n');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
iobuf_write( out, buffer, n );
|
|
|
|
|
* armor.c, g10.c, kbnode.c, misc.c, pkclist.c, sign.c, build-packet.c,
getkey.c, keydb.c, openfile.c, plaintext.c, status.c, gpgv.c, keygen.c,
options.h, sig-check.c, tdbio.h, encode.c, mainproc.c, parse-packet.c,
signal.c, textfilter.c: Edit all preprocessor instructions to remove
whitespace before the '#'. This is not required by C89, but there are some
compilers out there that don't like it.
2003-05-24 21:50:33 +00:00
|
|
|
#else
|
1999-03-02 15:48:37 +00:00
|
|
|
iobuf_write( out, buffer, n );
|
* armor.c, g10.c, kbnode.c, misc.c, pkclist.c, sign.c, build-packet.c,
getkey.c, keydb.c, openfile.c, plaintext.c, status.c, gpgv.c, keygen.c,
options.h, sig-check.c, tdbio.h, encode.c, mainproc.c, parse-packet.c,
signal.c, textfilter.c: Edit all preprocessor instructions to remove
whitespace before the '#'. This is not required by C89, but there are some
compilers out there that don't like it.
2003-05-24 21:50:33 +00:00
|
|
|
#endif
|
1999-03-02 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* at eof */
|
|
|
|
if( !pending_lf ) { /* make sure that the file ends with a LF */
|
2000-07-14 17:34:53 +00:00
|
|
|
iobuf_writestr( out, LF );
|
1999-03-02 15:48:37 +00:00
|
|
|
if( !escape_dash )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc( md, '\n' );
|
1999-03-02 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( truncated )
|
|
|
|
log_info(_("input line longer than %d characters\n"), MAX_LINELEN );
|
|
|
|
|
|
|
|
return 0; /* okay */
|
|
|
|
}
|