2008-02-01 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / textfilter.c
blob14bf69962b5d301c48f9b4207d0d227c46e5fd2c
1 /* textfilter.c
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 <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
27 #include "gpg.h"
28 #include "status.h"
29 #include "iobuf.h"
30 #include "util.h"
31 #include "filter.h"
32 #include "i18n.h"
33 #include "options.h"
34 #include "status.h"
36 #ifdef HAVE_DOSISH_SYSTEM
37 #define LF "\r\n"
38 #else
39 #define LF "\n"
40 #endif
42 #define MAX_LINELEN 19995 /* a little bit smaller than in armor.c */
43 /* to make sure that a warning is displayed while */
44 /* creating a message */
46 static unsigned
47 len_without_trailing_chars( byte *line, unsigned len, const char *trimchars )
49 byte *p, *mark;
50 unsigned n;
52 for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
53 if( strchr( trimchars, *p ) ) {
54 if( !mark )
55 mark = p;
57 else
58 mark = NULL;
61 return mark? (mark - line) : len;
65 static int
66 standard( text_filter_context_t *tfx, IOBUF a,
67 byte *buf, size_t size, size_t *ret_len)
69 int rc=0;
70 size_t len = 0;
71 unsigned maxlen;
73 assert( size > 10 );
74 size -= 2; /* reserve 2 bytes to append CR,LF */
75 while( !rc && len < size ) {
76 int lf_seen;
78 while( len < size && tfx->buffer_pos < tfx->buffer_len )
79 buf[len++] = tfx->buffer[tfx->buffer_pos++];
80 if( len >= size )
81 continue;
83 /* read the next line */
84 maxlen = MAX_LINELEN;
85 tfx->buffer_pos = 0;
86 tfx->buffer_len = iobuf_read_line( a, &tfx->buffer,
87 &tfx->buffer_size, &maxlen );
88 if( !maxlen )
89 tfx->truncated++;
90 if( !tfx->buffer_len ) {
91 if( !len )
92 rc = -1; /* eof */
93 break;
95 lf_seen = tfx->buffer[tfx->buffer_len-1] == '\n';
97 /* The story behind this is that 2440 says that textmode
98 hashes should canonicalize line endings to CRLF and remove
99 spaces and tabs. 2440bis-12 says to just canonicalize to
100 CRLF. 1.4.0 was released using the bis-12 behavior, but it
101 was discovered that many mail clients do not canonicalize
102 PGP/MIME signature text appropriately (and were relying on
103 GnuPG to handle trailing spaces). So, we default to the
104 2440 behavior, but use the 2440bis-12 behavior if the user
105 specifies --no-rfc2440-text. The default will be changed
106 at some point in the future when the mail clients have been
107 upgraded. Aside from PGP/MIME and broken mail clients,
108 this makes no difference to any signatures in the real
109 world except for a textmode detached signature. PGP always
110 used the 2440bis-12 behavior (ignoring 2440 itself), so
111 this actually makes us compatible with PGP textmode
112 detached signatures for the first time. */
113 if(opt.rfc2440_text)
114 tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len,
115 " \t\r\n");
116 else
117 tfx->buffer_len=trim_trailing_chars(tfx->buffer,tfx->buffer_len,
118 "\r\n");
120 if( lf_seen ) {
121 tfx->buffer[tfx->buffer_len++] = '\r';
122 tfx->buffer[tfx->buffer_len++] = '\n';
125 *ret_len = len;
126 return rc;
130 /****************
131 * The filter is used to make canonical text: Lines are terminated by
132 * CR, LF, trailing white spaces are removed.
135 text_filter( void *opaque, int control,
136 IOBUF a, byte *buf, size_t *ret_len)
138 size_t size = *ret_len;
139 text_filter_context_t *tfx = opaque;
140 int rc=0;
142 if( control == IOBUFCTRL_UNDERFLOW ) {
143 rc = standard( tfx, a, buf, size, ret_len );
145 else if( control == IOBUFCTRL_FREE ) {
146 if( tfx->truncated )
147 log_error(_("can't handle text lines longer than %d characters\n"),
148 MAX_LINELEN );
149 xfree( tfx->buffer );
150 tfx->buffer = NULL;
152 else if( control == IOBUFCTRL_DESC )
153 *(char**)buf = "text_filter";
154 return rc;
158 /****************
159 * Copy data from INP to OUT and do some escaping if requested.
160 * md is updated as required by rfc2440
163 copy_clearsig_text( IOBUF out, IOBUF inp, gcry_md_hd_t md,
164 int escape_dash, int escape_from, int pgp2mode )
166 unsigned int maxlen;
167 byte *buffer = NULL; /* malloced buffer */
168 unsigned int bufsize; /* and size of this buffer */
169 unsigned int n;
170 int truncated = 0;
171 int pending_lf = 0;
173 if( !opt.pgp2_workarounds )
174 pgp2mode = 0;
176 if( !escape_dash )
177 escape_from = 0;
179 write_status_begin_signing (md);
181 for(;;) {
182 maxlen = MAX_LINELEN;
183 n = iobuf_read_line( inp, &buffer, &bufsize, &maxlen );
184 if( !maxlen )
185 truncated++;
187 if( !n )
188 break; /* read_line has returned eof */
190 /* update the message digest */
191 if( escape_dash ) {
192 if( pending_lf ) {
193 gcry_md_putc ( md, '\r' );
194 gcry_md_putc ( md, '\n' );
196 gcry_md_write ( md, buffer,
197 len_without_trailing_chars (buffer, n,
198 pgp2mode?
199 " \r\n":" \t\r\n"));
201 else
202 gcry_md_write ( md, buffer, n );
203 pending_lf = buffer[n-1] == '\n';
205 /* write the output */
206 if( ( escape_dash && *buffer == '-')
207 || ( escape_from && n > 4 && !memcmp(buffer, "From ", 5 ) ) ) {
208 iobuf_put( out, '-' );
209 iobuf_put( out, ' ' );
212 #if 0 /*defined(HAVE_DOSISH_SYSTEM)*/
213 /* We don't use this anymore because my interpretation of rfc2440 7.1
214 * is that there is no conversion needed. If one decides to
215 * clearsign a unix file on a DOS box he will get a mixed line endings.
216 * If at some point it turns out, that a conversion is a nice feature
217 * we can make an option out of it.
219 /* make sure the lines do end in CR,LF */
220 if( n > 1 && ( (buffer[n-2] == '\r' && buffer[n-1] == '\n' )
221 || (buffer[n-2] == '\n' && buffer[n-1] == '\r'))) {
222 iobuf_write( out, buffer, n-2 );
223 iobuf_put( out, '\r');
224 iobuf_put( out, '\n');
226 else if( n && buffer[n-1] == '\n' ) {
227 iobuf_write( out, buffer, n-1 );
228 iobuf_put( out, '\r');
229 iobuf_put( out, '\n');
231 else
232 iobuf_write( out, buffer, n );
234 #else
235 iobuf_write( out, buffer, n );
236 #endif
239 /* at eof */
240 if( !pending_lf ) { /* make sure that the file ends with a LF */
241 iobuf_writestr( out, LF );
242 if( !escape_dash )
243 gcry_md_putc( md, '\n' );
246 if( truncated )
247 log_info(_("input line longer than %d characters\n"), MAX_LINELEN );
249 return 0; /* okay */