exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / sha1-stream.c
blob7bf44e5ea3f779b2960c8bf66e98e280a999a81d
1 /* sha1.c - Functions to compute SHA1 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-1.
4 Copyright (C) 2000-2001, 2003-2006, 2008-2024 Free Software 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 /* Written by Scott G. Miller
20 Credits:
21 Robert Klep <robert@ilse.nl> -- Expansion function fix
24 #include <config.h>
26 /* Specification. */
27 #if HAVE_OPENSSL_SHA1
28 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
29 #endif
30 #include "sha1.h"
32 #include <stdlib.h>
34 #if USE_UNLOCKED_IO
35 # include "unlocked-io.h"
36 #endif
38 #include "af_alg.h"
40 #define BLOCKSIZE 32768
41 #if BLOCKSIZE % 64 != 0
42 # error "invalid BLOCKSIZE"
43 #endif
45 /* Compute SHA1 message digest for bytes read from STREAM. The
46 resulting message digest number will be written into the 20 bytes
47 beginning at RESBLOCK. */
48 int
49 sha1_stream (FILE *stream, void *resblock)
51 switch (afalg_stream (stream, "sha1", resblock, SHA1_DIGEST_SIZE))
53 case 0: return 0;
54 case -EIO: return 1;
57 char *buffer = malloc (BLOCKSIZE + 72);
58 if (!buffer)
59 return 1;
61 struct sha1_ctx ctx;
62 sha1_init_ctx (&ctx);
63 size_t sum;
65 /* Iterate over full file contents. */
66 while (1)
68 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
69 computation function processes the whole buffer so that with the
70 next round of the loop another block can be read. */
71 size_t n;
72 sum = 0;
74 /* Read block. Take care for partial reads. */
75 while (1)
77 /* Either process a partial fread() from this loop,
78 or the fread() in afalg_stream may have gotten EOF.
79 We need to avoid a subsequent fread() as EOF may
80 not be sticky. For details of such systems, see:
81 https://sourceware.org/bugzilla/show_bug.cgi?id=1190 */
82 if (feof (stream))
83 goto process_partial_block;
85 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
87 sum += n;
89 if (sum == BLOCKSIZE)
90 break;
92 if (n == 0)
94 /* Check for the error flag IFF N == 0, so that we don't
95 exit the loop after a partial read due to e.g., EAGAIN
96 or EWOULDBLOCK. */
97 if (ferror (stream))
99 free (buffer);
100 return 1;
102 goto process_partial_block;
106 /* Process buffer with BLOCKSIZE bytes. Note that
107 BLOCKSIZE % 64 == 0
109 sha1_process_block (buffer, BLOCKSIZE, &ctx);
112 process_partial_block:;
114 /* Process any remaining bytes. */
115 if (sum > 0)
116 sha1_process_bytes (buffer, sum, &ctx);
118 /* Construct result in desired memory. */
119 sha1_finish_ctx (&ctx, resblock);
120 free (buffer);
121 return 0;
125 * Hey Emacs!
126 * Local Variables:
127 * coding: utf-8
128 * End: