2 * Copyright (C) 1998, 1999, 2000, 2001 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
37 #ifdef HAVE_DOSISH_SYSTEM
43 #define MAX_LINELEN 19995 /* a little bit smaller than in armor.c */
44 /* to make sure that a warning is displayed while */
45 /* creating a message */
48 len_without_trailing_chars( byte
*line
, unsigned len
, const char *trimchars
)
53 for(mark
=NULL
, p
=line
, n
=0; n
< len
; n
++, p
++ ) {
54 if( strchr( trimchars
, *p
) ) {
62 return mark
? (mark
- line
) : len
;
66 len_without_trailing_ws( byte
*line
, unsigned len
)
68 return len_without_trailing_chars( line
, len
, " \t\r\n" );
75 standard( text_filter_context_t
*tfx
, iobuf_t a
,
76 byte
*buf
, size_t size
, size_t *ret_len
)
83 size
-= 2; /* reserve 2 bytes to append CR,LF */
84 while( !rc
&& len
< size
) {
87 while( len
< size
&& tfx
->buffer_pos
< tfx
->buffer_len
)
88 buf
[len
++] = tfx
->buffer
[tfx
->buffer_pos
++];
92 /* read the next line */
95 tfx
->buffer_len
= iobuf_read_line( a
, &tfx
->buffer
,
96 &tfx
->buffer_size
, &maxlen
);
99 if( !tfx
->buffer_len
) {
104 lf_seen
= tfx
->buffer
[tfx
->buffer_len
-1] == '\n';
105 tfx
->buffer_len
= trim_trailing_ws( tfx
->buffer
, tfx
->buffer_len
);
107 tfx
->buffer
[tfx
->buffer_len
++] = '\r';
108 tfx
->buffer
[tfx
->buffer_len
++] = '\n';
119 * The filter is used to make canonical text: Lines are terminated by
120 * CR, LF, trailing white spaces are removed.
123 text_filter( void *opaque
, int control
,
124 iobuf_t a
, byte
*buf
, size_t *ret_len
)
126 size_t size
= *ret_len
;
127 text_filter_context_t
*tfx
= opaque
;
130 if( control
== IOBUFCTRL_UNDERFLOW
) {
131 rc
= standard( tfx
, a
, buf
, size
, ret_len
);
133 else if( control
== IOBUFCTRL_FREE
) {
135 log_error(_("can't handle text lines longer than %d characters\n"),
137 xfree ( tfx
->buffer
);
140 else if( control
== IOBUFCTRL_DESC
)
141 *(char**)buf
= "text_filter";
147 * Copy data from INP to OUT and do some escaping if requested.
148 * md is updated as required by rfc2440
151 copy_clearsig_text( iobuf_t out
, iobuf_t inp
, MD_HANDLE md
,
152 int escape_dash
, int escape_from
, int pgp2mode
)
155 byte
*buffer
= NULL
; /* malloced buffer */
156 unsigned bufsize
; /* and size of this buffer */
161 if( !opt
.pgp2_workarounds
)
168 maxlen
= MAX_LINELEN
;
169 n
= iobuf_read_line( inp
, &buffer
, &bufsize
, &maxlen
);
174 break; /* read_line has returned eof */
176 /* update the message digest */
179 gcry_md_putc( md
, '\r' );
180 gcry_md_putc( md
, '\n' );
182 gcry_md_write( md
, buffer
,
183 len_without_trailing_chars( buffer
, n
,
184 pgp2mode
? " \r\n":" \t\r\n"));
187 gcry_md_write( md
, buffer
, n
);
188 pending_lf
= buffer
[n
-1] == '\n';
190 /* write the output */
191 if( ( escape_dash
&& *buffer
== '-')
192 || ( escape_from
&& n
> 4 && !memcmp(buffer
, "From ", 5 ) ) ) {
193 iobuf_put( out
, '-' );
194 iobuf_put( out
, ' ' );
197 #if 0 /*defined(HAVE_DOSISH_SYSTEM)*/
198 /* We don't use this anymore because my interpretation of rfc2440 7.1
199 * is that there is no conversion needed. If one decides to
200 * clearsign a unix file on a DOS box he will get a mixed line endings.
201 * If at some point it turns out, that a conversion is a nice feature
202 * we can make an option out of it.
204 /* make sure the lines do end in CR,LF */
205 if( n
> 1 && ( (buffer
[n
-2] == '\r' && buffer
[n
-1] == '\n' )
206 || (buffer
[n
-2] == '\n' && buffer
[n
-1] == '\r'))) {
207 iobuf_write( out
, buffer
, n
-2 );
208 iobuf_put( out
, '\r');
209 iobuf_put( out
, '\n');
211 else if( n
&& buffer
[n
-1] == '\n' ) {
212 iobuf_write( out
, buffer
, n
-1 );
213 iobuf_put( out
, '\r');
214 iobuf_put( out
, '\n');
217 iobuf_write( out
, buffer
, n
);
220 iobuf_write( out
, buffer
, n
);
225 if( !pending_lf
) { /* make sure that the file ends with a LF */
226 iobuf_writestr( out
, LF
);
228 gcry_md_putc( md
, '\n' );
232 log_info(_("input line longer than %d characters\n"), MAX_LINELEN
);