exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / md5.h
blob2f470703f5c0815d23b7070c46cf938227d8caa8
1 /* Declaration of functions and data types used for MD5 sum computing
2 library functions.
3 Copyright (C) 1995-1997, 1999-2001, 2004-2006, 2008-2024 Free Software
4 Foundation, Inc.
5 This file is part of the GNU C Library.
7 This file is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 This file is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 #ifndef _MD5_H
21 #define _MD5_H 1
23 /* This file uses HAVE_OPENSSL_MD5. */
24 #if !_GL_CONFIG_H_INCLUDED
25 #error "Please include config.h first."
26 #endif
28 #include <stdio.h>
29 #include <stdint.h>
31 # if HAVE_OPENSSL_MD5
32 # ifndef OPENSSL_API_COMPAT
33 # define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
34 # endif
35 /* If <openssl/macros.h> would give a compile-time error, don't use OpenSSL. */
36 # include <openssl/opensslv.h>
37 # if OPENSSL_VERSION_MAJOR >= 3
38 # include <openssl/configuration.h>
39 # if (OPENSSL_CONFIGURED_API \
40 < (OPENSSL_API_COMPAT < 0x900000L ? OPENSSL_API_COMPAT : \
41 ((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000 \
42 + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
43 + ((OPENSSL_API_COMPAT >> 12) & 0xFF)))
44 # undef HAVE_OPENSSL_MD5
45 # endif
46 # endif
47 # if HAVE_OPENSSL_MD5
48 # include <openssl/md5.h>
49 # endif
50 # endif
52 #define MD5_DIGEST_SIZE 16
53 #define MD5_BLOCK_SIZE 64
55 #ifndef __GNUC_PREREQ
56 # if defined __GNUC__ && defined __GNUC_MINOR__
57 # define __GNUC_PREREQ(maj, min) \
58 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
59 # else
60 # define __GNUC_PREREQ(maj, min) 0
61 # endif
62 #endif
64 #ifndef __THROW
65 # if defined __cplusplus && (__GNUC_PREREQ (2,8) || __clang_major__ >= 4)
66 # if __cplusplus >= 201103L
67 # define __THROW noexcept (true)
68 # else
69 # define __THROW throw ()
70 # endif
71 # else
72 # define __THROW
73 # endif
74 #endif
76 #ifndef _LIBC
77 # define __md5_buffer md5_buffer
78 # define __md5_finish_ctx md5_finish_ctx
79 # define __md5_init_ctx md5_init_ctx
80 # define __md5_process_block md5_process_block
81 # define __md5_process_bytes md5_process_bytes
82 # define __md5_read_ctx md5_read_ctx
83 # define __md5_stream md5_stream
84 #endif
86 # ifdef __cplusplus
87 extern "C" {
88 # endif
90 # if HAVE_OPENSSL_MD5
91 # define GL_OPENSSL_NAME 5
92 # include "gl_openssl.h"
93 # else
94 /* Structure to save state of computation between the single steps. */
95 struct md5_ctx
97 uint32_t A;
98 uint32_t B;
99 uint32_t C;
100 uint32_t D;
102 uint32_t total[2];
103 uint32_t buflen; /* ≥ 0, ≤ 128 */
104 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
108 * The following three functions are build up the low level used in
109 * the functions 'md5_stream' and 'md5_buffer'.
112 /* Initialize structure containing state of computation.
113 (RFC 1321, 3.3: Step 3) */
114 extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
116 /* Starting with the result of former calls of this function (or the
117 initialization function update the context for the next LEN bytes
118 starting at BUFFER.
119 It is necessary that LEN is a multiple of 64!!! */
120 extern void __md5_process_block (const void *buffer, size_t len,
121 struct md5_ctx *ctx) __THROW;
123 /* Starting with the result of former calls of this function (or the
124 initialization function update the context for the next LEN bytes
125 starting at BUFFER.
126 It is NOT required that LEN is a multiple of 64. */
127 extern void __md5_process_bytes (const void *buffer, size_t len,
128 struct md5_ctx *ctx) __THROW;
130 /* Process the remaining bytes in the buffer and put result from CTX
131 in first 16 bytes following RESBUF. The result is always in little
132 endian byte order, so that a byte-wise output yields to the wanted
133 ASCII representation of the message digest. */
134 extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *restrict resbuf)
135 __THROW;
138 /* Put result from CTX in first 16 bytes following RESBUF. The result is
139 always in little endian byte order, so that a byte-wise output yields
140 to the wanted ASCII representation of the message digest. */
141 extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *restrict resbuf)
142 __THROW;
145 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
146 result is always in little endian byte order, so that a byte-wise
147 output yields to the wanted ASCII representation of the message
148 digest. */
149 extern void *__md5_buffer (const char *buffer, size_t len,
150 void *restrict resblock) __THROW;
152 # endif
154 /* Compute MD5 message digest for bytes read from STREAM.
155 STREAM is an open file stream. Regular files are handled more efficiently.
156 The contents of STREAM from its current position to its end will be read.
157 The case that the last operation on STREAM was an 'ungetc' is not supported.
158 The resulting message digest number will be written into the 16 bytes
159 beginning at RESBLOCK. */
160 extern int __md5_stream (FILE *stream, void *resblock) __THROW;
163 # ifdef __cplusplus
165 # endif
167 #endif /* md5.h */
170 * Hey Emacs!
171 * Local Variables:
172 * coding: utf-8
173 * End: