chiark / gitweb /
gpg agent threading bugs: Add some `xxx' comments.
[gnupg2.git] / g10 / mdfilter.c
1 /* mdfilter.c - filter data and calculate a message digest
2  * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "gpg.h"
27 #include "status.h"
28 #include "iobuf.h"
29 #include "util.h"
30 #include "filter.h"
31
32
33
34 /****************
35  * This filter is used to collect a message digest
36  */
37 int
38 md_filter( void *opaque, int control,
39                IOBUF a, byte *buf, size_t *ret_len)
40 {
41     size_t size = *ret_len;
42     md_filter_context_t *mfx = opaque;
43     int i, rc=0;
44
45     if( control == IOBUFCTRL_UNDERFLOW ) {
46         if( mfx->maxbuf_size && size > mfx->maxbuf_size )
47             size = mfx->maxbuf_size;
48         i = iobuf_read( a, buf, size );
49         if( i == -1 ) i = 0;
50         if( i ) {
51             gcry_md_write(mfx->md, buf, i );
52             if( mfx->md2 )
53                 gcry_md_write(mfx->md2, buf, i );
54         }
55         else
56             rc = -1; /* eof */
57         *ret_len = i;
58     }
59     else if( control == IOBUFCTRL_DESC )
60         mem2str (buf, "md_filter", *ret_len);
61     return rc;
62 }
63
64
65 void
66 free_md_filter_context( md_filter_context_t *mfx )
67 {
68     gcry_md_close(mfx->md);
69     gcry_md_close(mfx->md2);
70     mfx->md = NULL;
71     mfx->md2 = NULL;
72     mfx->maxbuf_size = 0;
73 }