diff options
author | Timo Schulz <twoaday@gnutls.org> | 2007-04-24 12:00:48 +0000 |
---|---|---|
committer | Timo Schulz <twoaday@gnutls.org> | 2007-04-24 12:00:48 +0000 |
commit | 36831e39c7edec5dc29af0652c34364b3f8eeb02 (patch) | |
tree | 82703ea4e908704be175d923e69fc881d5c1b9aa | |
parent | 290b31d22875e2178b83d8ba3f5a63b8f2e65fa8 (diff) | |
download | gnutls-36831e39c7edec5dc29af0652c34364b3f8eeb02.tar.gz |
missing file.
-rw-r--r-- | libextra/opencdk/literal.c | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/libextra/opencdk/literal.c b/libextra/opencdk/literal.c new file mode 100644 index 0000000000..28cae67df0 --- /dev/null +++ b/libextra/opencdk/literal.c @@ -0,0 +1,244 @@ +/* Literal.c - Literal packet filters + * Copyright (C) 2002, 2003 Timo Schulz + * + * This file is part of OpenCDK. + * + * OpenCDK is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * OpenCDK is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <stdio.h> + +#include "opencdk.h" +#include "main.h" +#include "filters.h" + + +static cdk_error_t +literal_decode (void * opaque, FILE * in, FILE * out) +{ + literal_filter_t *pfx = opaque; + cdk_stream_t si, so; + cdk_packet_t pkt; + cdk_pkt_literal_t pt; + byte buf[BUFSIZE]; + size_t nread; + int bufsize; + cdk_error_t rc; + + _cdk_log_debug( "literal filter: decode\n" ); + + if (!pfx || !in || !out) + return CDK_Inv_Value; + + rc = _cdk_stream_fpopen (in, STREAMCTL_READ, &si); + if (rc) + return rc; + + cdk_pkt_new (&pkt); + rc = cdk_pkt_read (si, pkt); + if (rc) + { + cdk_stream_close (si); + return rc; + } + if (pkt->pkttype != CDK_PKT_LITERAL) + { + cdk_pkt_release (pkt); + cdk_stream_close (si); + return CDK_Inv_Packet; + } + + rc = _cdk_stream_fpopen (out, STREAMCTL_WRITE, &so); + if (rc) + { + cdk_pkt_release (pkt); + cdk_stream_close (si); + return rc; + } + + pt = pkt->pkt.literal; + pfx->mode = pt->mode; + pfx->filename = cdk_strdup (pt->name? pt->name : " "); + if (!pfx->filename) + { + /* FIXME: release streams. */ + cdk_pkt_release (pkt); + return CDK_Out_Of_Core; + } + while (!feof (in)) + { + /*_cdk_log_debug( "partial on=%d size=%lu\n", + pfx->blkmode.on, pfx->blkmode.size );*/ + if (pfx->blkmode.on) + bufsize = pfx->blkmode.size; + else + bufsize = pt->len < DIM (buf)-1? pt->len : DIM (buf)-1; + nread = cdk_stream_read (pt->buf, buf, bufsize); + if (nread == EOF) + { + rc = CDK_File_Error; + break; + } + if (pfx->md) + gcry_md_write (pfx->md, buf, nread); + cdk_stream_write (so, buf, nread); + pt->len -= nread; + if (pfx->blkmode.on) + { + pfx->blkmode.size = _cdk_pkt_read_len (in, &pfx->blkmode.on); + if (pfx->blkmode.size == (size_t)EOF) + return CDK_Inv_Packet; + } + if (pt->len <= 0 && !pfx->blkmode.on) + break; + } + + cdk_stream_close (si); + cdk_stream_close (so); + cdk_pkt_release (pkt); + return rc; +} + + +static cdk_error_t +literal_encode (void *opaque, FILE *in, FILE *out) +{ + literal_filter_t *pfx = opaque; + cdk_pkt_literal_t pt; + cdk_stream_t si; + cdk_packet_t pkt; + size_t filelen; + cdk_error_t rc; + + _cdk_log_debug ("literal filter: encode\n"); + + if (!pfx || !in || !out) + return CDK_Inv_Value; + if (!pfx->filename) + { + pfx->filename = cdk_strdup ("_CONSOLE"); + if (!pfx->filename) + return CDK_Out_Of_Core; + } + + rc = _cdk_stream_fpopen (in, STREAMCTL_READ, &si); + if (rc) + return rc; + + filelen = strlen (pfx->filename); + cdk_pkt_new (&pkt); + pt = pkt->pkt.literal = cdk_calloc (1, sizeof *pt + filelen - 1); + if (!pt) + { + cdk_pkt_release (pkt); + cdk_stream_close (si); + return CDK_Out_Of_Core; + } + memcpy (pt->name, pfx->filename, filelen); + pt->namelen = filelen; + pt->name[pt->namelen] = '\0'; + pt->timestamp = _cdk_timestamp (); + pt->mode = pfx->mode ? 't' : 'b'; + pt->len = cdk_stream_get_length (si); + pt->buf = si; + pkt->old_ctb = pfx->rfc1991? 1 : 0; + pkt->pkttype = CDK_PKT_LITERAL; + pkt->pkt.literal = pt; + rc = _cdk_pkt_write_fp (out, pkt); + + cdk_pkt_release (pkt); + cdk_stream_close (si); + return rc; +} + + +int +_cdk_filter_literal( void * opaque, int ctl, FILE * in, FILE * out ) +{ + if( ctl == STREAMCTL_READ ) + return literal_decode( opaque, in, out ); + else if( ctl == STREAMCTL_WRITE ) + return literal_encode( opaque, in, out ); + else if( ctl == STREAMCTL_FREE ) { + literal_filter_t * pfx = opaque; + if( pfx ) { + _cdk_log_debug( "free literal filter\n" ); + cdk_free( pfx->filename ); + pfx->filename = NULL; + } + } + return CDK_Inv_Mode; +} + + +static int +text_encode (void *opaque, FILE *in, FILE *out) +{ + const char *s; + char buf[1024]; + + if (!in || !out) + return CDK_Inv_Value; + + while (!feof (in)) + { + s = fgets (buf, DIM (buf)-1, in); + if (!s) + break; + _cdk_trim_string (buf, 1); + fwrite (buf, 1, strlen (buf), out); + } + + return 0; +} + + +static int +text_decode( void * opaque, FILE * in, FILE * out ) +{ + text_filter_t * tfx = opaque; + const char * s; + char buf[1024]; + + if( !tfx || !in || !out ) + return CDK_Inv_Value; + + while( !feof( in ) ) { + s = fgets( buf, DIM (buf)-1, in ); + if( !s ) + break; + _cdk_trim_string( buf, 0 ); + fwrite( buf, 1, strlen( buf ), out ); + fwrite( tfx->lf, 1, strlen( tfx->lf ), out ); + } + + return 0; +} + + +int +_cdk_filter_text( void * opaque, int ctl, FILE * in, FILE * out ) +{ + if( ctl == STREAMCTL_READ ) + return text_encode( opaque, in, out ); + else if( ctl == STREAMCTL_WRITE ) + return text_decode( opaque, in, out ); + else if( ctl == STREAMCTL_FREE ) { + text_filter_t * tfx = opaque; + if( tfx ) { + _cdk_log_debug( "free text filter\n" ); + tfx->lf = NULL; + } + } + return CDK_Inv_Mode; +} |