24 #include <SDL_endian.h>
32 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
33 #define byteReverse(buf, len) {}
41 const uint32_t t = SDL_SwapLE32(*(uint32_t*)buf);
54 ctx->
buf[0] = 0x67452301;
55 ctx->
buf[1] = 0xefcdab89;
56 ctx->
buf[2] = 0x98badcfe;
57 ctx->
buf[3] = 0x10325476;
65 #define F1(x, y, z) (z ^ (x & (y ^ z)))
66 #define F2(x, y, z) F1(z, x, y)
67 #define F3(x, y, z) (x ^ y ^ z)
68 #define F4(x, y, z) (y ^ (x | ~z))
71 #define MD5STEP(f, w, x, y, z, data, s) \
72 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
82 register uint32_t a = buf[0];
83 register uint32_t b = buf[1];
84 register uint32_t c = buf[2];
85 register uint32_t d = buf[3];
87 MD5STEP(
F1, a, b, c, d, in[0] + 0xd76aa478, 7);
88 MD5STEP(
F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
89 MD5STEP(
F1, c, d, a, b, in[2] + 0x242070db, 17);
90 MD5STEP(
F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
91 MD5STEP(
F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
92 MD5STEP(
F1, d, a, b, c, in[5] + 0x4787c62a, 12);
93 MD5STEP(
F1, c, d, a, b, in[6] + 0xa8304613, 17);
94 MD5STEP(
F1, b, c, d, a, in[7] + 0xfd469501, 22);
95 MD5STEP(
F1, a, b, c, d, in[8] + 0x698098d8, 7);
96 MD5STEP(
F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
97 MD5STEP(
F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
98 MD5STEP(
F1, b, c, d, a, in[11] + 0x895cd7be, 22);
99 MD5STEP(
F1, a, b, c, d, in[12] + 0x6b901122, 7);
100 MD5STEP(
F1, d, a, b, c, in[13] + 0xfd987193, 12);
101 MD5STEP(
F1, c, d, a, b, in[14] + 0xa679438e, 17);
102 MD5STEP(
F1, b, c, d, a, in[15] + 0x49b40821, 22);
104 MD5STEP(
F2, a, b, c, d, in[1] + 0xf61e2562, 5);
105 MD5STEP(
F2, d, a, b, c, in[6] + 0xc040b340, 9);
106 MD5STEP(
F2, c, d, a, b, in[11] + 0x265e5a51, 14);
107 MD5STEP(
F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
108 MD5STEP(
F2, a, b, c, d, in[5] + 0xd62f105d, 5);
109 MD5STEP(
F2, d, a, b, c, in[10] + 0x02441453, 9);
110 MD5STEP(
F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
111 MD5STEP(
F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
112 MD5STEP(
F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
113 MD5STEP(
F2, d, a, b, c, in[14] + 0xc33707d6, 9);
114 MD5STEP(
F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
115 MD5STEP(
F2, b, c, d, a, in[8] + 0x455a14ed, 20);
116 MD5STEP(
F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
117 MD5STEP(
F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
118 MD5STEP(
F2, c, d, a, b, in[7] + 0x676f02d9, 14);
119 MD5STEP(
F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
121 MD5STEP(
F3, a, b, c, d, in[5] + 0xfffa3942, 4);
122 MD5STEP(
F3, d, a, b, c, in[8] + 0x8771f681, 11);
123 MD5STEP(
F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
124 MD5STEP(
F3, b, c, d, a, in[14] + 0xfde5380c, 23);
125 MD5STEP(
F3, a, b, c, d, in[1] + 0xa4beea44, 4);
126 MD5STEP(
F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
127 MD5STEP(
F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
128 MD5STEP(
F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
129 MD5STEP(
F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
130 MD5STEP(
F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
131 MD5STEP(
F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
132 MD5STEP(
F3, b, c, d, a, in[6] + 0x04881d05, 23);
133 MD5STEP(
F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
134 MD5STEP(
F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
135 MD5STEP(
F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
136 MD5STEP(
F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
138 MD5STEP(
F4, a, b, c, d, in[0] + 0xf4292244, 6);
139 MD5STEP(
F4, d, a, b, c, in[7] + 0x432aff97, 10);
140 MD5STEP(
F4, c, d, a, b, in[14] + 0xab9423a7, 15);
141 MD5STEP(
F4, b, c, d, a, in[5] + 0xfc93a039, 21);
142 MD5STEP(
F4, a, b, c, d, in[12] + 0x655b59c3, 6);
143 MD5STEP(
F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
144 MD5STEP(
F4, c, d, a, b, in[10] + 0xffeff47d, 15);
145 MD5STEP(
F4, b, c, d, a, in[1] + 0x85845dd1, 21);
146 MD5STEP(
F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
147 MD5STEP(
F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
148 MD5STEP(
F4, c, d, a, b, in[6] + 0xa3014314, 15);
149 MD5STEP(
F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
150 MD5STEP(
F4, a, b, c, d, in[4] + 0xf7537e82, 6);
151 MD5STEP(
F4, d, a, b, c, in[11] + 0xbd3af235, 10);
152 MD5STEP(
F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
153 MD5STEP(
F4, b, c, d, a, in[9] + 0xeb86d391, 21);
169 uint32_t t = ctx->
bits[0];
170 if ((ctx->
bits[0] = t + ((uint32_t) len << 3)) < t)
172 ctx->
bits[1] += len >> 29;
179 unsigned char* p = (
unsigned char*) ctx->
in + t;
195 memcpy(ctx->
in, buf, 64);
204 memcpy(ctx->
in, buf, len);
214 unsigned count = (ctx->
bits[0] >> 3) & 0x3F;
218 unsigned char* p = ctx->
in +
count;
222 count = 64 - 1 -
count;
232 memset(ctx->
in, 0, 56);
235 memset(p, 0, count - 8);
240 ((uint32_t*) ctx->
in)[14] = ctx->
bits[0];
241 ((uint32_t*) ctx->
in)[15] = ctx->
bits[1];
246 if (digest !=
nullptr)
247 memcpy(digest, ctx->
buf, 16);
264 if (filelen < length || !length)
273 int r =
FS_Read(buffer,
sizeof(buffer), &f);
276 if (r + total > length)
279 MD5Update(&md5, (
const unsigned char*) buffer, r);
280 if (r <
sizeof(buffer) || total >= length)
283 unsigned char digest[16] = {
"" };
286 static char final[33];
288 for (
int i = 0;
i < 16;
i++)
289 Q_strcat(
final,
sizeof(
final),
"%02x", digest[
i]);
309 unsigned char digest[16] = {
"" };
312 static char final[33];
314 for (
int i = 0;
i < 16;
i++)
315 Q_strcat(
final,
sizeof(
final),
"%02x", digest[
i]);
const char * Com_MD5Buffer(const byte *buf, size_t len)
Compute the md5sum of the given buffer.
int FS_OpenFile(const char *filename, qFILE *file, filemode_t mode)
Finds and opens the file in the search path.
const char * Com_MD5File(const char *fn, int length)
Compute the md5sum of a given file.
static void MD5Init(struct MD5Context *ctx)
Start MD5 accumulation. Set bit count to 0 and buffer to mysterious initialization constants...
QGL_EXTERN GLuint GLsizei GLsizei * length
int FS_Read(void *buffer, int len, qFILE *f)
#define MD5STEP(f, w, x, y, z, data, s)
QGL_EXTERN GLuint GLchar GLuint * len
static void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
Update context to reflect the concatenation of another buffer full of bytes.
void Q_strcat(char *dest, size_t destsize, const char *format,...)
Safely (without overflowing the destination buffer) concatenates two strings.
definitions common between client and server, but not game lib
#define byteReverse(buf, len)
static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
The core of the MD5 algorithm, this alters an existing MD5 hash to reflect the addition of 16 longwor...
static void MD5Final(struct MD5Context *ctx, unsigned char *digest)
Final wrapup - pad to 64-byte boundary with the bit pattern 1 0* (64-bit count of bits processed...
struct MD5Context MD5_CTX