Merge from mainline.
[bpt/emacs.git] / lib / md5.h
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-2011 Free Software
4 Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11
12 This program 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 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 #ifndef _MD5_H
22 #define _MD5_H 1
23
24 #include <stdio.h>
25
26 #define MD5_DIGEST_SIZE 16
27 #define MD5_BLOCK_SIZE 64
28
29 #ifndef __GNUC_PREREQ
30 # if defined __GNUC__ && defined __GNUC_MINOR__
31 # define __GNUC_PREREQ(maj, min) \
32 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
33 # else
34 # define __GNUC_PREREQ(maj, min) 0
35 # endif
36 #endif
37
38 #ifndef __THROW
39 # if defined __cplusplus && __GNUC_PREREQ (2,8)
40 # define __THROW throw ()
41 # else
42 # define __THROW
43 # endif
44 #endif
45
46 #ifndef _LIBC
47 # define __md5_buffer md5_buffer
48 # define __md5_finish_ctx md5_finish_ctx
49 # define __md5_init_ctx md5_init_ctx
50 # define __md5_process_block md5_process_block
51 # define __md5_process_bytes md5_process_bytes
52 # define __md5_read_ctx md5_read_ctx
53 # define __md5_stream md5_stream
54 #endif
55
56 # ifdef __cplusplus
57 extern "C" {
58 # endif
59
60 /* The following contortions are an attempt to use the C preprocessor
61 to determine an unsigned integral type that is 32 bits wide. An
62 alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
63 doing that would require that the configure script compile and *run*
64 the resulting executable. Locally running cross-compiled executables
65 is usually not possible. */
66
67 #if defined _LIBC
68 # include <stdint.h>
69 typedef uint32_t md5_uint32;
70 #else
71 # if defined __STDC__ && __STDC__
72 # define UINT_MAX_32_BITS 4294967295U
73 # else
74 # define UINT_MAX_32_BITS 0xFFFFFFFF
75 # endif
76
77 # include <limits.h>
78
79 # if UINT_MAX == UINT_MAX_32_BITS
80 typedef unsigned int md5_uint32;
81 # else
82 # if USHRT_MAX == UINT_MAX_32_BITS
83 typedef unsigned short md5_uint32;
84 # else
85 # if ULONG_MAX == UINT_MAX_32_BITS
86 typedef unsigned long md5_uint32;
87 # else
88 /* A machine this weird should have <stdint.h>. */
89 # include <stdint.h>
90 typedef uint32_t md5_uint32;
91 # endif
92 # endif
93 # endif
94 #endif
95
96 /* Structure to save state of computation between the single steps. */
97 struct md5_ctx
98 {
99 md5_uint32 A;
100 md5_uint32 B;
101 md5_uint32 C;
102 md5_uint32 D;
103
104 md5_uint32 total[2];
105 md5_uint32 buflen;
106 md5_uint32 buffer[32];
107 };
108
109 /*
110 * The following three functions are build up the low level used in
111 * the functions `md5_stream' and `md5_buffer'.
112 */
113
114 /* Initialize structure containing state of computation.
115 (RFC 1321, 3.3: Step 3) */
116 extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
117
118 /* Starting with the result of former calls of this function (or the
119 initialization function update the context for the next LEN bytes
120 starting at BUFFER.
121 It is necessary that LEN is a multiple of 64!!! */
122 extern void __md5_process_block (const void *buffer, size_t len,
123 struct md5_ctx *ctx) __THROW;
124
125 /* Starting with the result of former calls of this function (or the
126 initialization function update the context for the next LEN bytes
127 starting at BUFFER.
128 It is NOT required that LEN is a multiple of 64. */
129 extern void __md5_process_bytes (const void *buffer, size_t len,
130 struct md5_ctx *ctx) __THROW;
131
132 /* Process the remaining bytes in the buffer and put result from CTX
133 in first 16 bytes following RESBUF. The result is always in little
134 endian byte order, so that a byte-wise output yields to the wanted
135 ASCII representation of the message digest. */
136 extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
137
138
139 /* Put result from CTX in first 16 bytes following RESBUF. The result is
140 always in little endian byte order, so that a byte-wise output yields
141 to the wanted ASCII representation of the message digest. */
142 extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
143
144
145 /* Compute MD5 message digest for bytes read from STREAM. The
146 resulting message digest number will be written into the 16 bytes
147 beginning at RESBLOCK. */
148 extern int __md5_stream (FILE *stream, void *resblock) __THROW;
149
150 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
151 result is always in little endian byte order, so that a byte-wise
152 output yields to the wanted ASCII representation of the message
153 digest. */
154 extern void *__md5_buffer (const char *buffer, size_t len,
155 void *resblock) __THROW;
156
157 # ifdef __cplusplus
158 }
159 # endif
160
161 #endif /* md5.h */