comparison src/xs_md5.c @ 657:acaba070cf49

Lots of cosmetic code cleanups; synced the de-gettextification from Audacious-SID, I suppose it makes some sense ...
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 02 Apr 2008 19:46:59 +0300
parents a3fdd2d6c056
children b0743dc9165d
comparison
equal deleted inserted replaced
656:e9257f006f41 657:acaba070cf49
10 #include "xs_support.h" 10 #include "xs_support.h"
11 #include "xs_md5.h" 11 #include "xs_md5.h"
12 #include <glib.h> 12 #include <glib.h>
13 13
14 14
15 #ifndef WORDS_BIGENDIAN 15 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
16 #define xs_md5_bytereverse(buf, len) /* Nothing */ 16 #define xs_md5_bytereverse(buf, len) /* Nothing */
17 #else 17 #else
18 void xs_md5_bytereverse(guint8 *buf, guint l) 18 #if G_BYTE_ORDER == G_BIG_ENDIAN
19 static void xs_md5_bytereverse(guint8 *buf, guint l)
19 { 20 {
20 guint32 t; 21 guint32 t;
21 do { 22 do {
22 t = (guint32) ((guint) buf[3] << 8 | buf[2]) << 16 | ((guint) buf[1] << 8 | buf[0]); 23 t = (guint32) ((guint) buf[3] << 8 | buf[2]) << 16 | ((guint) buf[1] << 8 | buf[0]);
23 *(guint32 *) buf = t; 24 *(guint32 *) buf = t;
24 buf += sizeof(guint32); 25 buf += sizeof(guint32);
25 } while (--l); 26 } while (--l);
26 } 27 }
28 #else
29 #error Unsupported endianess!
30 #endif
27 #endif 31 #endif
28 32
29 33
30 /* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 34 /* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
31 * initialization constants. 35 * initialization constants.
32 */ 36 */
33 void xs_md5_init(t_xs_md5state *ctx) 37 void xs_md5_init(xs_md5state_t *ctx)
34 { 38 {
35 ctx->buf[0] = 0x67452301; 39 ctx->buf[0] = 0x67452301;
36 ctx->buf[1] = 0xefcdab89; 40 ctx->buf[1] = 0xefcdab89;
37 ctx->buf[2] = 0x98badcfe; 41 ctx->buf[2] = 0x98badcfe;
38 ctx->buf[3] = 0x10325476; 42 ctx->buf[3] = 0x10325476;
51 #define F3(x, y, z) (x ^ y ^ z) 55 #define F3(x, y, z) (x ^ y ^ z)
52 #define F4(x, y, z) (y ^ (x | ~z)) 56 #define F4(x, y, z) (y ^ (x | ~z))
53 #define MD5STEP(f, w, x, y, z, data, s) \ 57 #define MD5STEP(f, w, x, y, z, data, s) \
54 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 58 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
55 59
56 void xs_md5_transform(guint32 buf[4], guint32 const in[16]) 60 static void xs_md5_transform(guint32 buf[4], guint32 const in[16])
57 { 61 {
58 register guint32 a, b, c, d; 62 register guint32 a, b, c, d;
59 63
60 a = buf[0]; 64 a = buf[0];
61 b = buf[1]; 65 b = buf[1];
138 142
139 143
140 /* Update context to reflect the concatenation of another buffer full 144 /* Update context to reflect the concatenation of another buffer full
141 * of bytes. 145 * of bytes.
142 */ 146 */
143 void xs_md5_append(t_xs_md5state *ctx, const guint8 *buf, guint len) 147 void xs_md5_append(xs_md5state_t *ctx, const guint8 *buf, guint len)
144 { 148 {
145 guint32 t; 149 guint32 t;
146 150
147 /* Update bitcount */ 151 /* Update bitcount */
148 t = ctx->bits[0]; 152 t = ctx->bits[0];
151 ctx->bits[1] += len >> 29; 155 ctx->bits[1] += len >> 29;
152 156
153 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 157 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
154 158
155 /* Handle any leading odd-sized chunks */ 159 /* Handle any leading odd-sized chunks */
156
157 if (t) { 160 if (t) {
158 guint8 *p = (guint8 *) ctx->in + t; 161 guint8 *p = (guint8 *) ctx->in + t;
159 162
160 t = 64 - t; 163 t = 64 - t;
161 if (len < t) { 164 if (len < t) {
166 xs_md5_bytereverse(ctx->in, 16); 169 xs_md5_bytereverse(ctx->in, 16);
167 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); 170 xs_md5_transform(ctx->buf, (guint32 *) ctx->in);
168 buf += t; 171 buf += t;
169 len -= t; 172 len -= t;
170 } 173 }
174
171 /* Process data in 64-byte chunks */ 175 /* Process data in 64-byte chunks */
172
173 while (len >= 64) { 176 while (len >= 64) {
174 memcpy(ctx->in, buf, 64); 177 memcpy(ctx->in, buf, 64);
175 xs_md5_bytereverse(ctx->in, 16); 178 xs_md5_bytereverse(ctx->in, 16);
176 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); 179 xs_md5_transform(ctx->buf, (guint32 *) ctx->in);
177 buf += 64; 180 buf += 64;
178 len -= 64; 181 len -= 64;
179 } 182 }
180 183
181 /* Handle any remaining bytes of data. */ 184 /* Handle any remaining bytes of data. */
182
183 memcpy(ctx->in, buf, len); 185 memcpy(ctx->in, buf, len);
184 } 186 }
185 187
186 /* Final wrapup - pad to 64-byte boundary with the bit pattern 188 /* Final wrapup - pad to 64-byte boundary with the bit pattern
187 * 1 0* (64-bit count of bits processed, MSB-first) 189 * 1 0* (64-bit count of bits processed, MSB-first)
188 */ 190 */
189 void xs_md5_finish(t_xs_md5state *ctx, t_xs_md5hash digest) 191 void xs_md5_finish(xs_md5state_t *ctx, xs_md5hash_t digest)
190 { 192 {
191 guint count; 193 guint count;
192 guint8 *p; 194 guint8 *p;
193 195
194 /* Compute number of bytes mod 64 */ 196 /* Compute number of bytes mod 64 */
203 count = 64 - 1 - count; 205 count = 64 - 1 - count;
204 206
205 /* Pad out to 56 mod 64 */ 207 /* Pad out to 56 mod 64 */
206 if (count < 8) { 208 if (count < 8) {
207 /* Two lots of padding: Pad the first block to 64 bytes */ 209 /* Two lots of padding: Pad the first block to 64 bytes */
208 xs_memset(p, 0, count); 210 memset(p, 0, count);
209 xs_md5_bytereverse(ctx->in, 16); 211 xs_md5_bytereverse(ctx->in, 16);
210 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); 212 xs_md5_transform(ctx->buf, (guint32 *) ctx->in);
211 213
212 /* Now fill the next block with 56 bytes */ 214 /* Now fill the next block with 56 bytes */
213 xs_memset(ctx->in, 0, 56); 215 memset(ctx->in, 0, 56);
214 } else { 216 } else {
215 /* Pad block to 56 bytes */ 217 /* Pad block to 56 bytes */
216 xs_memset(p, 0, count - 8); 218 memset(p, 0, count - 8);
217 } 219 }
218 xs_md5_bytereverse(ctx->in, 14); 220 xs_md5_bytereverse(ctx->in, 14);
219 221
220 /* Append length in bits and transform */ 222 /* Append length in bits and transform */
221 ((guint32 *) ctx->in)[14] = ctx->bits[0]; 223 ((guint32 *) ctx->in)[14] = ctx->bits[0];
222 ((guint32 *) ctx->in)[15] = ctx->bits[1]; 224 ((guint32 *) ctx->in)[15] = ctx->bits[1];
223 225
224 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); 226 xs_md5_transform(ctx->buf, (guint32 *) ctx->in);
225 xs_md5_bytereverse((guint8 *) ctx->buf, 4); 227 xs_md5_bytereverse((guint8 *) ctx->buf, 4);
226 memcpy(digest, ctx->buf, 16); 228 memcpy(digest, ctx->buf, 16);
227 xs_memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ 229 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
228 } 230 }