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/>.
36 #ifdef HAVE_DOSISH_SYSTEM
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 */
47 len_without_trailing_chars( byte
*line
, unsigned len
, const char *trimchars
)
52 for(mark
=NULL
, p
=line
, n
=0; n
< len
; n
++, p
++ ) {
53 if( strchr( trimchars
, *p
) ) {
61 return mark
? (mark
- line
) : len
;
66 standard( text_filter_context_t
*tfx
, IOBUF a
,
67 byte
*buf
, size_t size
, size_t *ret_len
)
74 size
-= 2; /* reserve 2 bytes to append CR,LF */
75 while( !rc
&& len
< size
) {
78 while( len
< size
&& tfx
->buffer_pos
< tfx
->buffer_len
)
79 buf
[len
++] = tfx
->buffer
[tfx
->buffer_pos
++];
83 /* read the next line */
86 tfx
->buffer_len
= iobuf_read_line( a
, &tfx
->buffer
,
87 &tfx
->buffer_size
, &maxlen
);
90 if( !tfx
->buffer_len
) {
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. */
114 tfx
->buffer_len
=trim_trailing_chars(tfx
->buffer
,tfx
->buffer_len
,
117 tfx
->buffer_len
=trim_trailing_chars(tfx
->buffer
,tfx
->buffer_len
,
121 tfx
->buffer
[tfx
->buffer_len
++] = '\r';
122 tfx
->buffer
[tfx
->buffer_len
++] = '\n';
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
;
142 if( control
== IOBUFCTRL_UNDERFLOW
) {
143 rc
= standard( tfx
, a
, buf
, size
, ret_len
);
145 else if( control
== IOBUFCTRL_FREE
) {
147 log_error(_("can't handle text lines longer than %d characters\n"),
149 xfree( tfx
->buffer
);
152 else if( control
== IOBUFCTRL_DESC
)
153 *(char**)buf
= "text_filter";
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
)
167 byte
*buffer
= NULL
; /* malloced buffer */
168 unsigned int bufsize
; /* and size of this buffer */
173 if( !opt
.pgp2_workarounds
)
179 write_status_begin_signing (md
);
182 maxlen
= MAX_LINELEN
;
183 n
= iobuf_read_line( inp
, &buffer
, &bufsize
, &maxlen
);
188 break; /* read_line has returned eof */
190 /* update the message digest */
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
,
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');
232 iobuf_write( out
, buffer
, n
);
235 iobuf_write( out
, buffer
, n
);
240 if( !pending_lf
) { /* make sure that the file ends with a LF */
241 iobuf_writestr( out
, LF
);
243 gcry_md_putc( md
, '\n' );
247 log_info(_("input line longer than %d characters\n"), MAX_LINELEN
);