2 * Copyright (C) 1998, 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
35 #ifdef HAVE_DOSISH_SYSTEM
41 #define MAX_LINELEN 19995 /* a little bit smaller than in armor.c */
42 /* to make sure that a warning is displayed while */
43 /* creating a message */
46 len_without_trailing_chars( byte *line, unsigned len, const char *trimchars )
51 for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
52 if( strchr( trimchars, *p ) ) {
60 return mark? (mark - line) : len;
65 standard( text_filter_context_t *tfx, IOBUF a,
66 byte *buf, size_t size, size_t *ret_len)
72 log_assert( size > 10 );
73 size -= 2; /* reserve 2 bytes to append CR,LF */
74 while( !rc && len < size ) {
77 while( len < size && tfx->buffer_pos < tfx->buffer_len )
78 buf[len++] = tfx->buffer[tfx->buffer_pos++];
82 /* read the next line */
85 tfx->buffer_len = iobuf_read_line( a, &tfx->buffer,
86 &tfx->buffer_size, &maxlen );
89 if( !tfx->buffer_len ) {
94 lf_seen = tfx->buffer[tfx->buffer_len-1] == '\n';
96 /* The story behind this is that 2440 says that textmode
97 hashes should canonicalize line endings to CRLF and remove
98 spaces and tabs. 2440bis-12 says to just canonicalize to
99 CRLF. 1.4.0 was released using the bis-12 behavior, but it
100 was discovered that many mail clients do not canonicalize
101 PGP/MIME signature text appropriately (and were relying on
102 GnuPG to handle trailing spaces). So, we default to the
103 2440 behavior, but use the 2440bis-12 behavior if the user
104 specifies --no-rfc2440-text. The default will be changed
105 at some point in the future when the mail clients have been
106 upgraded. Aside from PGP/MIME and broken mail clients,
107 this makes no difference to any signatures in the real
108 world except for a textmode detached signature. PGP always
109 used the 2440bis-12 behavior (ignoring 2440 itself), so
110 this actually makes us compatible with PGP textmode
111 detached signatures for the first time. */
113 tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len,
116 tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len,
120 tfx->buffer[tfx->buffer_len++] = '\r';
121 tfx->buffer[tfx->buffer_len++] = '\n';
130 * The filter is used to make canonical text: Lines are terminated by
131 * CR, LF, trailing white spaces are removed.
134 text_filter( void *opaque, int control,
135 IOBUF a, byte *buf, size_t *ret_len)
137 size_t size = *ret_len;
138 text_filter_context_t *tfx = opaque;
141 if( control == IOBUFCTRL_UNDERFLOW ) {
142 rc = standard( tfx, a, buf, size, ret_len );
144 else if( control == IOBUFCTRL_FREE ) {
146 log_error(_("can't handle text lines longer than %d characters\n"),
148 xfree( tfx->buffer );
151 else if( control == IOBUFCTRL_DESC )
152 mem2str (buf, "text_filter", *ret_len);
158 * Copy data from INP to OUT and do some escaping if requested.
159 * md is updated as required by rfc2440
162 copy_clearsig_text( IOBUF out, IOBUF inp, gcry_md_hd_t md,
163 int escape_dash, int escape_from)
166 byte *buffer = NULL; /* malloced buffer */
167 unsigned int bufsize; /* and size of this buffer */
175 write_status_begin_signing (md);
178 maxlen = MAX_LINELEN;
179 n = iobuf_read_line( inp, &buffer, &bufsize, &maxlen );
184 break; /* read_line has returned eof */
186 /* update the message digest */
189 gcry_md_putc ( md, '\r' );
190 gcry_md_putc ( md, '\n' );
192 gcry_md_write ( md, buffer,
193 len_without_trailing_chars (buffer, n, " \t\r\n"));
196 gcry_md_write ( md, buffer, n );
197 pending_lf = buffer[n-1] == '\n';
199 /* write the output */
200 if( ( escape_dash && *buffer == '-')
201 || ( escape_from && n > 4 && !memcmp(buffer, "From ", 5 ) ) ) {
202 iobuf_put( out, '-' );
203 iobuf_put( out, ' ' );
206 #if 0 /*defined(HAVE_DOSISH_SYSTEM)*/
207 /* We don't use this anymore because my interpretation of rfc2440 7.1
208 * is that there is no conversion needed. If one decides to
209 * clearsign a unix file on a DOS box he will get a mixed line endings.
210 * If at some point it turns out, that a conversion is a nice feature
211 * we can make an option out of it.
213 /* make sure the lines do end in CR,LF */
214 if( n > 1 && ( (buffer[n-2] == '\r' && buffer[n-1] == '\n' )
215 || (buffer[n-2] == '\n' && buffer[n-1] == '\r'))) {
216 iobuf_write( out, buffer, n-2 );
217 iobuf_put( out, '\r');
218 iobuf_put( out, '\n');
220 else if( n && buffer[n-1] == '\n' ) {
221 iobuf_write( out, buffer, n-1 );
222 iobuf_put( out, '\r');
223 iobuf_put( out, '\n');
226 iobuf_write( out, buffer, n );
229 iobuf_write( out, buffer, n );
234 if( !pending_lf ) { /* make sure that the file ends with a LF */
235 iobuf_writestr( out, LF );
237 gcry_md_putc( md, '\n' );
241 log_info(_("input line longer than %d characters\n"), MAX_LINELEN );