exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / md2-stream.c
blob1637c790016a575e12cf814cd9a4f1e51881476b
1 /* Functions to compute MD2 message digest of files or memory blocks.
2 according to the definition of MD2 in RFC 1319 from April 1992.
3 Copyright (C) 1995-1997, 1999-2003, 2005-2006, 2008-2024 Free Software
4 Foundation, Inc.
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 This file 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* Adapted by Simon Josefsson from public domain Libtomcrypt 1.06 by
20 Tom St Denis. */
22 #include <config.h>
24 /* Specification. */
25 #include "md2.h"
27 #include <stdlib.h>
29 #if USE_UNLOCKED_IO
30 # include "unlocked-io.h"
31 #endif
33 #define BLOCKSIZE 32768
34 #if BLOCKSIZE % 64 != 0
35 # error "invalid BLOCKSIZE"
36 #endif
38 /* Compute MD2 message digest for bytes read from STREAM. The
39 resulting message digest number will be written into the 16 bytes
40 beginning at RESBLOCK. */
41 int
42 md2_stream (FILE *stream, void *resblock)
44 struct md2_ctx ctx;
45 size_t sum;
47 char *buffer = malloc (BLOCKSIZE + 72);
48 if (!buffer)
49 return 1;
51 /* Initialize the computation context. */
52 md2_init_ctx (&ctx);
54 /* Iterate over full file contents. */
55 while (1)
57 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
58 computation function processes the whole buffer so that with the
59 next round of the loop another block can be read. */
60 size_t n;
61 sum = 0;
63 /* Read block. Take care for partial reads. */
64 while (1)
66 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
68 sum += n;
70 if (sum == BLOCKSIZE)
71 break;
73 if (n == 0)
75 /* Check for the error flag IFF N == 0, so that we don't
76 exit the loop after a partial read due to e.g., EAGAIN
77 or EWOULDBLOCK. */
78 if (ferror (stream))
80 free (buffer);
81 return 1;
83 goto process_partial_block;
86 /* We've read at least one byte, so ignore errors. But always
87 check for EOF, since feof may be true even though N > 0.
88 Otherwise, we could end up calling fread after EOF. */
89 if (feof (stream))
90 goto process_partial_block;
93 /* Process buffer with BLOCKSIZE bytes. Note that
94 BLOCKSIZE % 64 == 0
96 md2_process_block (buffer, BLOCKSIZE, &ctx);
99 process_partial_block:;
101 /* Process any remaining bytes. */
102 if (sum > 0)
103 md2_process_bytes (buffer, sum, &ctx);
105 /* Construct result in desired memory. */
106 md2_finish_ctx (&ctx, resblock);
107 free (buffer);
108 return 0;