xref: /netbsd-src/external/gpl3/gdb/dist/include/md5.h (revision e663ba6e3a60083e70de702e9d54bf486a57b6a7)
198b9484cSchristos /* md5.h - Declaration of functions and data types used for MD5 sum
298b9484cSchristos    computing library functions.
3*e663ba6eSchristos    Copyright (C) 1995-2024 Free Software Foundation, Inc.
498b9484cSchristos    NOTE: The canonical source of this file is maintained with the GNU C
598b9484cSchristos    Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
698b9484cSchristos 
798b9484cSchristos    This program is free software; you can redistribute it and/or modify it
898b9484cSchristos    under the terms of the GNU General Public License as published by the
998b9484cSchristos    Free Software Foundation; either version 2, or (at your option) any
1098b9484cSchristos    later version.
1198b9484cSchristos 
1298b9484cSchristos    This program is distributed in the hope that it will be useful,
1398b9484cSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1498b9484cSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1598b9484cSchristos    GNU General Public License for more details.
1698b9484cSchristos 
1798b9484cSchristos    You should have received a copy of the GNU General Public License
1898b9484cSchristos    along with this program; if not, write to the Free Software Foundation,
1998b9484cSchristos    Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2098b9484cSchristos 
2198b9484cSchristos #ifndef _MD5_H
2298b9484cSchristos #define _MD5_H 1
2398b9484cSchristos 
244b169a6bSchristos #ifdef USE_SYSTEM_MD5
254b169a6bSchristos #include_next <md5.h>
264b169a6bSchristos #else
274b169a6bSchristos 
2898b9484cSchristos #include <stdio.h>
2998b9484cSchristos 
3098b9484cSchristos #if defined HAVE_LIMITS_H || _LIBC
3198b9484cSchristos # include <limits.h>
3298b9484cSchristos #endif
3398b9484cSchristos 
3498b9484cSchristos #include "ansidecl.h"
3598b9484cSchristos 
3698b9484cSchristos /* The following contortions are an attempt to use the C preprocessor
3798b9484cSchristos    to determine an unsigned integral type that is 32 bits wide.  An
3898b9484cSchristos    alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
3998b9484cSchristos    doing that would require that the configure script compile and *run*
4098b9484cSchristos    the resulting executable.  Locally running cross-compiled executables
4198b9484cSchristos    is usually not possible.  */
4298b9484cSchristos 
4398b9484cSchristos #ifdef _LIBC
4498b9484cSchristos # include <sys/types.h>
4598b9484cSchristos typedef u_int32_t md5_uint32;
4698b9484cSchristos typedef uintptr_t md5_uintptr;
47a2e2270fSchristos #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
48a2e2270fSchristos #include <stdint.h>
49a2e2270fSchristos #include <sys/types.h>
50a2e2270fSchristos typedef uint32_t md5_uint32;
51a2e2270fSchristos typedef uintptr_t md5_uintptr;
5298b9484cSchristos #else
5398b9484cSchristos #  define INT_MAX_32_BITS 2147483647
5498b9484cSchristos 
5598b9484cSchristos /* If UINT_MAX isn't defined, assume it's a 32-bit type.
5698b9484cSchristos    This should be valid for all systems GNU cares about because
5798b9484cSchristos    that doesn't include 16-bit systems, and only modern systems
5898b9484cSchristos    (that certainly have <limits.h>) have 64+-bit integral types.  */
5998b9484cSchristos 
6098b9484cSchristos # ifndef INT_MAX
6198b9484cSchristos #  define INT_MAX INT_MAX_32_BITS
6298b9484cSchristos # endif
6398b9484cSchristos 
6498b9484cSchristos # if INT_MAX == INT_MAX_32_BITS
6598b9484cSchristos    typedef unsigned int md5_uint32;
6698b9484cSchristos # else
6798b9484cSchristos #  if SHRT_MAX == INT_MAX_32_BITS
6898b9484cSchristos     typedef unsigned short md5_uint32;
6998b9484cSchristos #  else
7098b9484cSchristos #   if LONG_MAX == INT_MAX_32_BITS
7198b9484cSchristos      typedef unsigned long md5_uint32;
7298b9484cSchristos #   else
7398b9484cSchristos      /* The following line is intended to evoke an error.
7498b9484cSchristos         Using #error is not portable enough.  */
7598b9484cSchristos      "Cannot determine unsigned 32-bit data type."
7698b9484cSchristos #   endif
7798b9484cSchristos #  endif
7898b9484cSchristos # endif
7998b9484cSchristos /* We have to make a guess about the integer type equivalent in size
8098b9484cSchristos    to pointers which should always be correct.  */
8198b9484cSchristos typedef unsigned long int md5_uintptr;
8298b9484cSchristos #endif
8398b9484cSchristos 
8498b9484cSchristos #ifdef __cplusplus
8598b9484cSchristos extern "C" {
8698b9484cSchristos #endif
8798b9484cSchristos 
8898b9484cSchristos /* Structure to save state of computation between the single steps.  */
8998b9484cSchristos struct md5_ctx
9098b9484cSchristos {
9198b9484cSchristos   md5_uint32 A;
9298b9484cSchristos   md5_uint32 B;
9398b9484cSchristos   md5_uint32 C;
9498b9484cSchristos   md5_uint32 D;
9598b9484cSchristos 
9698b9484cSchristos   md5_uint32 total[2];
9798b9484cSchristos   md5_uint32 buflen;
9898b9484cSchristos   char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32);
9998b9484cSchristos };
10098b9484cSchristos 
10198b9484cSchristos /*
10298b9484cSchristos  * The following three functions are build up the low level used in
10398b9484cSchristos  * the functions `md5_stream' and `md5_buffer'.
10498b9484cSchristos  */
10598b9484cSchristos 
10698b9484cSchristos /* Initialize structure containing state of computation.
10798b9484cSchristos    (RFC 1321, 3.3: Step 3)  */
10898b9484cSchristos extern void md5_init_ctx (struct md5_ctx *ctx);
10998b9484cSchristos 
11098b9484cSchristos /* Starting with the result of former calls of this function (or the
11198b9484cSchristos    initialization function update the context for the next LEN bytes
11298b9484cSchristos    starting at BUFFER.
11398b9484cSchristos    It is necessary that LEN is a multiple of 64!!! */
11498b9484cSchristos extern void md5_process_block (const void *buffer, size_t len,
11598b9484cSchristos                                struct md5_ctx *ctx);
11698b9484cSchristos 
11798b9484cSchristos /* Starting with the result of former calls of this function (or the
11898b9484cSchristos    initialization function update the context for the next LEN bytes
11998b9484cSchristos    starting at BUFFER.
12098b9484cSchristos    It is NOT required that LEN is a multiple of 64.  */
12198b9484cSchristos extern void md5_process_bytes (const void *buffer, size_t len,
12298b9484cSchristos                                struct md5_ctx *ctx);
12398b9484cSchristos 
12498b9484cSchristos /* Process the remaining bytes in the buffer and put result from CTX
12598b9484cSchristos    in first 16 bytes following RESBUF.  The result is always in little
12698b9484cSchristos    endian byte order, so that a byte-wise output yields to the wanted
12798b9484cSchristos    ASCII representation of the message digest.
12898b9484cSchristos 
12998b9484cSchristos    IMPORTANT: On some systems it is required that RESBUF is correctly
13098b9484cSchristos    aligned for a 32 bits value.  */
13198b9484cSchristos extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
13298b9484cSchristos 
13398b9484cSchristos 
13498b9484cSchristos /* Put result from CTX in first 16 bytes following RESBUF.  The result is
13598b9484cSchristos    always in little endian byte order, so that a byte-wise output yields
13698b9484cSchristos    to the wanted ASCII representation of the message digest.
13798b9484cSchristos 
13898b9484cSchristos    IMPORTANT: On some systems it is required that RESBUF is correctly
13998b9484cSchristos    aligned for a 32 bits value.  */
14098b9484cSchristos extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
14198b9484cSchristos 
14298b9484cSchristos 
14398b9484cSchristos /* Compute MD5 message digest for bytes read from STREAM.  The
14498b9484cSchristos    resulting message digest number will be written into the 16 bytes
14598b9484cSchristos    beginning at RESBLOCK.  */
14698b9484cSchristos extern int md5_stream (FILE *stream, void *resblock);
14798b9484cSchristos 
14898b9484cSchristos /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
14998b9484cSchristos    result is always in little endian byte order, so that a byte-wise
15098b9484cSchristos    output yields to the wanted ASCII representation of the message
15198b9484cSchristos    digest.  */
15298b9484cSchristos extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
15398b9484cSchristos 
15498b9484cSchristos #ifdef __cplusplus
15598b9484cSchristos }
15698b9484cSchristos #endif
15798b9484cSchristos 
1584b169a6bSchristos #endif // USE_SYSTEM_MD5
1594b169a6bSchristos 
16098b9484cSchristos #endif
161