2004-02-26 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / textfilter.c
bloba3ea4b138caa9e7e0db5071f17b6a35aa3098344
1 /* textfilter.c
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
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
28 #include "gpg.h"
29 #include "errors.h"
30 #include "iobuf.h"
31 #include "memory.h"
32 #include "util.h"
33 #include "filter.h"
34 #include "i18n.h"
35 #include "options.h"
37 #ifdef HAVE_DOSISH_SYSTEM
38 #define LF "\r\n"
39 #else
40 #define LF "\n"
41 #endif
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 */
47 static unsigned
48 len_without_trailing_chars( byte *line, unsigned len, const char *trimchars )
50 byte *p, *mark;
51 unsigned n;
53 for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
54 if( strchr( trimchars, *p ) ) {
55 if( !mark )
56 mark = p;
58 else
59 mark = NULL;
62 return mark? (mark - line) : len;
65 unsigned
66 len_without_trailing_ws( byte *line, unsigned len )
68 return len_without_trailing_chars( line, len, " \t\r\n" );
74 static int
75 standard( text_filter_context_t *tfx, iobuf_t a,
76 byte *buf, size_t size, size_t *ret_len)
78 int rc=0;
79 size_t len = 0;
80 unsigned maxlen;
82 assert( size > 10 );
83 size -= 2; /* reserve 2 bytes to append CR,LF */
84 while( !rc && len < size ) {
85 int lf_seen;
87 while( len < size && tfx->buffer_pos < tfx->buffer_len )
88 buf[len++] = tfx->buffer[tfx->buffer_pos++];
89 if( len >= size )
90 continue;
92 /* read the next line */
93 maxlen = MAX_LINELEN;
94 tfx->buffer_pos = 0;
95 tfx->buffer_len = iobuf_read_line( a, &tfx->buffer,
96 &tfx->buffer_size, &maxlen );
97 if( !maxlen )
98 tfx->truncated++;
99 if( !tfx->buffer_len ) {
100 if( !len )
101 rc = -1; /* eof */
102 break;
104 lf_seen = tfx->buffer[tfx->buffer_len-1] == '\n';
105 tfx->buffer_len = trim_trailing_ws( tfx->buffer, tfx->buffer_len );
106 if( lf_seen ) {
107 tfx->buffer[tfx->buffer_len++] = '\r';
108 tfx->buffer[tfx->buffer_len++] = '\n';
111 *ret_len = len;
112 return rc;
118 /****************
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;
128 int rc=0;
130 if( control == IOBUFCTRL_UNDERFLOW ) {
131 rc = standard( tfx, a, buf, size, ret_len );
133 else if( control == IOBUFCTRL_FREE ) {
134 if( tfx->truncated )
135 log_error(_("can't handle text lines longer than %d characters\n"),
136 MAX_LINELEN );
137 xfree ( tfx->buffer );
138 tfx->buffer = NULL;
140 else if( control == IOBUFCTRL_DESC )
141 *(char**)buf = "text_filter";
142 return rc;
146 /****************
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 )
154 unsigned maxlen;
155 byte *buffer = NULL; /* malloced buffer */
156 unsigned bufsize; /* and size of this buffer */
157 unsigned n;
158 int truncated = 0;
159 int pending_lf = 0;
161 if( !opt.pgp2_workarounds )
162 pgp2mode = 0;
164 if( !escape_dash )
165 escape_from = 0;
167 for(;;) {
168 maxlen = MAX_LINELEN;
169 n = iobuf_read_line( inp, &buffer, &bufsize, &maxlen );
170 if( !maxlen )
171 truncated++;
173 if( !n )
174 break; /* read_line has returned eof */
176 /* update the message digest */
177 if( escape_dash ) {
178 if( pending_lf ) {
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"));
186 else
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');
216 else
217 iobuf_write( out, buffer, n );
219 #else
220 iobuf_write( out, buffer, n );
221 #endif
224 /* at eof */
225 if( !pending_lf ) { /* make sure that the file ends with a LF */
226 iobuf_writestr( out, LF );
227 if( !escape_dash )
228 gcry_md_putc( md, '\n' );
231 if( truncated )
232 log_info(_("input line longer than %d characters\n"), MAX_LINELEN );
234 return 0; /* okay */