xref: /netbsd-src/external/gpl3/gcc/dist/include/md5.h (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14fee23f9Smrg /* md5.h - Declaration of functions and data types used for MD5 sum
24fee23f9Smrg    computing library functions.
3*b1e83836Smrg    Copyright (C) 1995-2022 Free Software Foundation, Inc.
44fee23f9Smrg    NOTE: The canonical source of this file is maintained with the GNU C
54fee23f9Smrg    Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
64fee23f9Smrg 
74fee23f9Smrg    This program is free software; you can redistribute it and/or modify it
84fee23f9Smrg    under the terms of the GNU General Public License as published by the
94fee23f9Smrg    Free Software Foundation; either version 2, or (at your option) any
104fee23f9Smrg    later version.
114fee23f9Smrg 
124fee23f9Smrg    This program is distributed in the hope that it will be useful,
134fee23f9Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
144fee23f9Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
154fee23f9Smrg    GNU General Public License for more details.
164fee23f9Smrg 
174fee23f9Smrg    You should have received a copy of the GNU General Public License
184fee23f9Smrg    along with this program; if not, write to the Free Software Foundation,
194fee23f9Smrg    Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
204fee23f9Smrg 
214fee23f9Smrg #ifndef _MD5_H
224fee23f9Smrg #define _MD5_H 1
234fee23f9Smrg 
24*b1e83836Smrg #ifdef USE_SYSTEM_MD5
25*b1e83836Smrg #include_next <md5.h>
26*b1e83836Smrg #else
27*b1e83836Smrg 
284fee23f9Smrg #include <stdio.h>
294fee23f9Smrg 
304fee23f9Smrg #if defined HAVE_LIMITS_H || _LIBC
314fee23f9Smrg # include <limits.h>
324fee23f9Smrg #endif
334fee23f9Smrg 
344fee23f9Smrg #include "ansidecl.h"
354fee23f9Smrg 
364fee23f9Smrg /* The following contortions are an attempt to use the C preprocessor
374fee23f9Smrg    to determine an unsigned integral type that is 32 bits wide.  An
384fee23f9Smrg    alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
394fee23f9Smrg    doing that would require that the configure script compile and *run*
404fee23f9Smrg    the resulting executable.  Locally running cross-compiled executables
414fee23f9Smrg    is usually not possible.  */
424fee23f9Smrg 
434fee23f9Smrg #ifdef _LIBC
444fee23f9Smrg # include <sys/types.h>
454fee23f9Smrg typedef u_int32_t md5_uint32;
464fee23f9Smrg typedef uintptr_t md5_uintptr;
4748fb7bfaSmrg #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
4848fb7bfaSmrg #include <stdint.h>
4948fb7bfaSmrg #include <sys/types.h>
5048fb7bfaSmrg typedef uint32_t md5_uint32;
5148fb7bfaSmrg typedef uintptr_t md5_uintptr;
524fee23f9Smrg #else
534fee23f9Smrg #  define INT_MAX_32_BITS 2147483647
544fee23f9Smrg 
554fee23f9Smrg /* If UINT_MAX isn't defined, assume it's a 32-bit type.
564fee23f9Smrg    This should be valid for all systems GNU cares about because
574fee23f9Smrg    that doesn't include 16-bit systems, and only modern systems
584fee23f9Smrg    (that certainly have <limits.h>) have 64+-bit integral types.  */
594fee23f9Smrg 
604fee23f9Smrg # ifndef INT_MAX
614fee23f9Smrg #  define INT_MAX INT_MAX_32_BITS
624fee23f9Smrg # endif
634fee23f9Smrg 
644fee23f9Smrg # if INT_MAX == INT_MAX_32_BITS
654fee23f9Smrg    typedef unsigned int md5_uint32;
664fee23f9Smrg # else
674fee23f9Smrg #  if SHRT_MAX == INT_MAX_32_BITS
684fee23f9Smrg     typedef unsigned short md5_uint32;
694fee23f9Smrg #  else
704fee23f9Smrg #   if LONG_MAX == INT_MAX_32_BITS
714fee23f9Smrg      typedef unsigned long md5_uint32;
724fee23f9Smrg #   else
734fee23f9Smrg      /* The following line is intended to evoke an error.
744fee23f9Smrg         Using #error is not portable enough.  */
754fee23f9Smrg      "Cannot determine unsigned 32-bit data type."
764fee23f9Smrg #   endif
774fee23f9Smrg #  endif
784fee23f9Smrg # endif
794fee23f9Smrg /* We have to make a guess about the integer type equivalent in size
804fee23f9Smrg    to pointers which should always be correct.  */
814fee23f9Smrg typedef unsigned long int md5_uintptr;
824fee23f9Smrg #endif
834fee23f9Smrg 
844fee23f9Smrg #ifdef __cplusplus
854fee23f9Smrg extern "C" {
864fee23f9Smrg #endif
874fee23f9Smrg 
884fee23f9Smrg /* Structure to save state of computation between the single steps.  */
894fee23f9Smrg struct md5_ctx
904fee23f9Smrg {
914fee23f9Smrg   md5_uint32 A;
924fee23f9Smrg   md5_uint32 B;
934fee23f9Smrg   md5_uint32 C;
944fee23f9Smrg   md5_uint32 D;
954fee23f9Smrg 
964fee23f9Smrg   md5_uint32 total[2];
974fee23f9Smrg   md5_uint32 buflen;
984fee23f9Smrg   char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32);
994fee23f9Smrg };
1004fee23f9Smrg 
1014fee23f9Smrg /*
1024fee23f9Smrg  * The following three functions are build up the low level used in
1034fee23f9Smrg  * the functions `md5_stream' and `md5_buffer'.
1044fee23f9Smrg  */
1054fee23f9Smrg 
1064fee23f9Smrg /* Initialize structure containing state of computation.
1074fee23f9Smrg    (RFC 1321, 3.3: Step 3)  */
1084fee23f9Smrg extern void md5_init_ctx (struct md5_ctx *ctx);
1094fee23f9Smrg 
1104fee23f9Smrg /* Starting with the result of former calls of this function (or the
1114fee23f9Smrg    initialization function update the context for the next LEN bytes
1124fee23f9Smrg    starting at BUFFER.
1134fee23f9Smrg    It is necessary that LEN is a multiple of 64!!! */
1144fee23f9Smrg extern void md5_process_block (const void *buffer, size_t len,
1154fee23f9Smrg                                struct md5_ctx *ctx);
1164fee23f9Smrg 
1174fee23f9Smrg /* Starting with the result of former calls of this function (or the
1184fee23f9Smrg    initialization function update the context for the next LEN bytes
1194fee23f9Smrg    starting at BUFFER.
1204fee23f9Smrg    It is NOT required that LEN is a multiple of 64.  */
1214fee23f9Smrg extern void md5_process_bytes (const void *buffer, size_t len,
1224fee23f9Smrg                                struct md5_ctx *ctx);
1234fee23f9Smrg 
1244fee23f9Smrg /* Process the remaining bytes in the buffer and put result from CTX
1254fee23f9Smrg    in first 16 bytes following RESBUF.  The result is always in little
1264fee23f9Smrg    endian byte order, so that a byte-wise output yields to the wanted
1274fee23f9Smrg    ASCII representation of the message digest.
1284fee23f9Smrg 
1294fee23f9Smrg    IMPORTANT: On some systems it is required that RESBUF is correctly
1304fee23f9Smrg    aligned for a 32 bits value.  */
1314fee23f9Smrg extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
1324fee23f9Smrg 
1334fee23f9Smrg 
1344fee23f9Smrg /* Put result from CTX in first 16 bytes following RESBUF.  The result is
1354fee23f9Smrg    always in little endian byte order, so that a byte-wise output yields
1364fee23f9Smrg    to the wanted ASCII representation of the message digest.
1374fee23f9Smrg 
1384fee23f9Smrg    IMPORTANT: On some systems it is required that RESBUF is correctly
1394fee23f9Smrg    aligned for a 32 bits value.  */
1404fee23f9Smrg extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
1414fee23f9Smrg 
1424fee23f9Smrg 
1434fee23f9Smrg /* Compute MD5 message digest for bytes read from STREAM.  The
1444fee23f9Smrg    resulting message digest number will be written into the 16 bytes
1454fee23f9Smrg    beginning at RESBLOCK.  */
1464fee23f9Smrg extern int md5_stream (FILE *stream, void *resblock);
1474fee23f9Smrg 
1484fee23f9Smrg /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
1494fee23f9Smrg    result is always in little endian byte order, so that a byte-wise
1504fee23f9Smrg    output yields to the wanted ASCII representation of the message
1514fee23f9Smrg    digest.  */
1524fee23f9Smrg extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
1534fee23f9Smrg 
1544fee23f9Smrg #ifdef __cplusplus
1554fee23f9Smrg }
1564fee23f9Smrg #endif
1574fee23f9Smrg 
158*b1e83836Smrg #endif // USE_SYSTEM_MD5
159*b1e83836Smrg 
1604fee23f9Smrg #endif
161