udapted vi.po
[rhythmbox.git] / daapsharing / rb-daap-hash.c
blob3a788397879fc169daa40a610833e3c66cc7b49b
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Implementation of DAAP (iTunes Music Sharing) hashing, parsing, connection
5 * Copyright (C) 2004,2005 Charles Schmidt <cschmidt2@emich.edu>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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.
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
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <math.h>
29 #ifdef HAVE_LIBZ
30 #include <zlib.h>
31 #endif
33 #include <glib.h>
35 #include "rb-daap-hash.h"
37 /* hashing - based on/copied from libopendaap
38 * Copyright (c) 2004 David Hammerton
41 typedef struct {
42 guint32 buf[4];
43 guint32 bits[2];
44 unsigned char in[64];
45 int version;
46 } MD5_CTX;
49 * This code implements the MD5 message-digest algorithm.
50 * The algorithm is due to Ron Rivest. This code was
51 * written by Colin Plumb in 1993, no copyright is claimed.
52 * This code is in the public domain; do with it what you wish.
54 * Equivalent code is available from RSA Data Security, Inc.
55 * This code has been tested against that, and is equivalent,
56 * except that you don't need to include two pages of legalese
57 * with every copy.
59 * To compute the message digest of a chunk of bytes, declare an MD5Context
60 * structure, pass it to OpenDaap_MD5Init, call OpenDaap_MD5Update as needed
61 * on buffers full of bytes, and then call OpenDaap_MD5Final, which will fill
62 * a supplied 16-byte array with the digest.
64 static void
65 MD5Transform (guint32 buf[4],
66 guint32 const in[16],
67 gint version);
68 /* for some reason we still have to reverse bytes on bigendian machines
69 * I don't really know why... but otherwise it fails..
70 * Any MD5 gurus out there know why???
72 #if 0 //ndef WORDS_BIGENDIAN /* was: HIGHFIRST */
73 #define byteReverse(buf, len) /* Nothing */
74 #else
75 static void
76 byteReverse (unsigned char *buf,
77 unsigned longs);
79 #ifndef ASM_MD5
81 * Note: this code is harmless on little-endian machines.
83 static void
84 byteReverse (unsigned char *buf,
85 unsigned longs)
87 guint32 t;
88 do {
89 t = (guint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
90 ((unsigned) buf[1] << 8 | buf[0]);
91 *(guint32 *) buf = t;
92 buf += 4;
93 } while (--longs);
95 #endif /* ! ASM_MD5 */
96 #endif /* #if 0 */
98 static void
99 OpenDaap_MD5Init (MD5_CTX *ctx,
100 gint version)
102 memset (ctx, 0, sizeof (MD5_CTX));
103 ctx->buf[0] = 0x67452301;
104 ctx->buf[1] = 0xefcdab89;
105 ctx->buf[2] = 0x98badcfe;
106 ctx->buf[3] = 0x10325476;
108 ctx->bits[0] = 0;
109 ctx->bits[1] = 0;
111 ctx->version = version;
114 static void
115 OpenDaap_MD5Update (MD5_CTX *ctx,
116 unsigned char const *buf,
117 unsigned int len)
119 guint32 t;
121 /* Update bitcount */
123 t = ctx->bits[0];
124 if ((ctx->bits[0] = t + ((guint32) len << 3)) < t)
125 ctx->bits[1]++; /* Carry from low to high */
126 ctx->bits[1] += len >> 29;
128 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
130 /* Handle any leading odd-sized chunks */
132 if (t) {
133 unsigned char *p = (unsigned char *) ctx->in + t;
135 t = 64 - t;
136 if (len < t) {
137 memcpy (p, buf, len);
138 return;
140 memcpy (p, buf, t);
141 byteReverse (ctx->in, 16);
142 MD5Transform (ctx->buf, (guint32 *) ctx->in, ctx->version);
143 buf += t;
144 len -= t;
146 /* Process data in 64-byte chunks */
148 while (len >= 64) {
149 memcpy (ctx->in, buf, 64);
150 byteReverse (ctx->in, 16);
151 MD5Transform (ctx->buf, (guint32 *) ctx->in, ctx->version);
152 buf += 64;
153 len -= 64;
156 /* Handle any remaining bytes of data. */
158 memcpy (ctx->in, buf, len);
162 static void
163 OpenDaap_MD5Final (MD5_CTX *ctx,
164 unsigned char digest[16])
166 unsigned count;
167 unsigned char *p;
169 /* Compute number of bytes mod 64 */
170 count = (ctx->bits[0] >> 3) & 0x3F;
172 /* Set the first char of padding to 0x80. This is safe since there is
173 always at least one byte free */
174 p = ctx->in + count;
175 *p++ = 0x80;
177 /* Bytes of padding needed to make 64 bytes */
178 count = 64 - 1 - count;
180 /* Pad out to 56 mod 64 */
181 if (count < 8) {
182 /* Two lots of padding: Pad the first block to 64 bytes */
183 memset (p, 0, count);
184 byteReverse (ctx->in, 16);
185 MD5Transform (ctx->buf, (guint32 *) ctx->in, ctx->version);
187 /* Now fill the next block with 56 bytes */
188 memset (ctx->in, 0, 56);
189 } else {
190 /* Pad block to 56 bytes */
191 memset (p, 0, count - 8);
193 byteReverse (ctx->in, 14);
195 /* Append length in bits and transform */
196 ((guint32 *) ctx->in)[14] = ctx->bits[0];
197 ((guint32 *) ctx->in)[15] = ctx->bits[1];
199 MD5Transform (ctx->buf, (guint32 *) ctx->in, ctx->version);
200 byteReverse ((unsigned char *) ctx->buf, 4);
201 memcpy (digest, ctx->buf, 16);
202 memset (ctx, 0, sizeof(ctx)); /* In case it's sensitive */
204 return;
207 #ifndef ASM_MD5
209 /* The four core functions - F1 is optimized somewhat */
211 /* #define F1(x, y, z) (x & y | ~x & z) */
212 #define F1(x, y, z) (z ^ (x & (y ^ z)))
213 #define F2(x, y, z) F1(z, x, y)
214 #define F3(x, y, z) (x ^ y ^ z)
215 #define F4(x, y, z) (y ^ (x | ~z))
217 /* This is the central step in the MD5 algorithm. */
218 #define MD5STEP(f, w, x, y, z, data, s) \
219 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
222 * The core of the MD5 algorithm, this alters an existing MD5 hash to reflect
223 * the addition of 16 longwords of new data. OpenDaap_MD5Update blocks the
224 * data and converts bytes into longwords for this routine.
226 static void
227 MD5Transform (guint32 buf[4],
228 guint32 const in[16],
229 gint version)
231 guint32 a, b, c, d;
233 a = buf[0];
234 b = buf[1];
235 c = buf[2];
236 d = buf[3];
238 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
239 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
240 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
241 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
242 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
243 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
244 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
245 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
246 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
247 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
248 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
249 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
250 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
251 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
252 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
253 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
255 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
256 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
257 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
258 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
259 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
260 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
261 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
262 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
263 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
264 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
265 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
267 if (version == 1)
269 MD5STEP(F2, b, c, d, a, in[8] + 0x445a14ed, 20);
271 else
273 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
275 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
276 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
277 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
278 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
280 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
281 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
282 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
283 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
284 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
285 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
286 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
287 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
288 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
289 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
290 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
291 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
292 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
293 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
294 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
295 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
297 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
298 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
299 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
300 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
301 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
302 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
303 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
304 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
305 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
306 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
307 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
308 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
309 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
310 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
311 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
312 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
314 buf[0] += a;
315 buf[1] += b;
316 buf[2] += c;
317 buf[3] += d;
320 #endif
322 static int staticHashDone = 0;
323 static unsigned char staticHash_42[256*65] = {0};
324 static unsigned char staticHash_45[256*65] = {0};
326 static const char hexchars[] = "0123456789ABCDEF";
327 static char ac[] = "Dpqzsjhiu!3114!Bqqmf!Dpnqvufs-!Jod/"; /* +1 */
328 static gboolean ac_unfudged = FALSE;
330 static void
331 DigestToString (const unsigned char *digest,
332 char *string)
334 int i;
335 for (i = 0; i < 16; i++)
337 unsigned char tmp = digest[i];
338 string[i*2+1] = hexchars[tmp & 0x0f];
339 string[i*2] = hexchars[(tmp >> 4) & 0x0f];
343 static void
344 GenerateStatic_42 ()
346 MD5_CTX ctx;
347 unsigned char *p = staticHash_42;
348 int i;
349 unsigned char buf[16];
351 for (i = 0; i < 256; i++)
353 OpenDaap_MD5Init (&ctx, 0);
355 #define MD5_STRUPDATE(str) OpenDaap_MD5Update(&ctx, (unsigned char const *)str, strlen(str))
357 if ((i & 0x80) != 0)
358 MD5_STRUPDATE("Accept-Language");
359 else
360 MD5_STRUPDATE("user-agent");
362 if ((i & 0x40) != 0)
363 MD5_STRUPDATE("max-age");
364 else
365 MD5_STRUPDATE("Authorization");
367 if ((i & 0x20) != 0)
368 MD5_STRUPDATE("Client-DAAP-Version");
369 else
370 MD5_STRUPDATE("Accept-Encoding");
372 if ((i & 0x10) != 0)
373 MD5_STRUPDATE("daap.protocolversion");
374 else
375 MD5_STRUPDATE("daap.songartist");
377 if ((i & 0x08) != 0)
378 MD5_STRUPDATE("daap.songcomposer");
379 else
380 MD5_STRUPDATE("daap.songdatemodified");
382 if ((i & 0x04) != 0)
383 MD5_STRUPDATE("daap.songdiscnumber");
384 else
385 MD5_STRUPDATE("daap.songdisabled");
387 if ((i & 0x02) != 0)
388 MD5_STRUPDATE("playlist-item-spec");
389 else
390 MD5_STRUPDATE("revision-number");
392 if ((i & 0x01) != 0)
393 MD5_STRUPDATE("session-id");
394 else
395 MD5_STRUPDATE("content-codes");
396 #undef MD5_STRUPDATE
398 OpenDaap_MD5Final (&ctx, buf);
399 DigestToString (buf, (char *)p);
400 p += 65;
404 static void GenerateStatic_45()
406 MD5_CTX ctx;
407 unsigned char *p = staticHash_45;
408 int i;
409 unsigned char buf[16];
411 for (i = 0; i < 256; i++)
413 OpenDaap_MD5Init (&ctx, 1);
415 #define MD5_STRUPDATE(str) OpenDaap_MD5Update(&ctx, (unsigned char const *)str, strlen(str))
417 if ((i & 0x40) != 0)
418 MD5_STRUPDATE("eqwsdxcqwesdc");
419 else
420 MD5_STRUPDATE("op[;lm,piojkmn");
422 if ((i & 0x20) != 0)
423 MD5_STRUPDATE("876trfvb 34rtgbvc");
424 else
425 MD5_STRUPDATE("=-0ol.,m3ewrdfv");
427 if ((i & 0x10) != 0)
428 MD5_STRUPDATE("87654323e4rgbv ");
429 else
430 MD5_STRUPDATE("1535753690868867974342659792");
432 if ((i & 0x08) != 0)
433 MD5_STRUPDATE("Song Name");
434 else
435 MD5_STRUPDATE("DAAP-CLIENT-ID:");
437 if ((i & 0x04) != 0)
438 MD5_STRUPDATE("111222333444555");
439 else
440 MD5_STRUPDATE("4089961010");
442 if ((i & 0x02) != 0)
443 MD5_STRUPDATE("playlist-item-spec");
444 else
445 MD5_STRUPDATE("revision-number");
447 if ((i & 0x01) != 0)
448 MD5_STRUPDATE("session-id");
449 else
450 MD5_STRUPDATE("content-codes");
452 if ((i & 0x80) != 0)
453 MD5_STRUPDATE("IUYHGFDCXWEDFGHN");
454 else
455 MD5_STRUPDATE("iuytgfdxwerfghjm");
457 #undef MD5_STRUPDATE
459 OpenDaap_MD5Final (&ctx, buf);
460 DigestToString (buf, (char *)p);
461 p += 65;
465 void
466 rb_daap_hash_generate (short version_major,
467 const guchar *url,
468 guchar hash_select,
469 guchar *out,
470 gint request_id)
472 unsigned char buf[16];
473 MD5_CTX ctx;
474 int i;
476 unsigned char *hashTable = (version_major == 3) ?
477 staticHash_45 : staticHash_42;
479 if (!staticHashDone)
481 GenerateStatic_42 ();
482 GenerateStatic_45 ();
483 staticHashDone = 1;
486 OpenDaap_MD5Init (&ctx, (version_major == 3) ? 1 : 0);
488 OpenDaap_MD5Update (&ctx, url, strlen ((const gchar*)url));
489 if (ac_unfudged == FALSE) {
490 for (i = 0; i < strlen (ac); i++) {
491 ac[i] = ac[i]-1;
493 ac_unfudged = TRUE;
495 OpenDaap_MD5Update (&ctx, (const guchar*)ac, strlen (ac));
497 OpenDaap_MD5Update (&ctx, &hashTable[hash_select * 65], 32);
499 if (request_id && version_major == 3)
501 char scribble[20];
502 sprintf (scribble, "%u", request_id);
503 OpenDaap_MD5Update (&ctx, (const guchar*)scribble, strlen (scribble));
506 OpenDaap_MD5Final (&ctx, buf);
507 DigestToString (buf, (char *)out);
509 return;