Artifact
c1693a11c2210728e8f16318f1fcbce45088139a:
0000: 2f 2a 20 65 64 32 6b 2e 63 20 2d 20 61 6e 20 69 /* ed2k.c - an i
0010: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
0020: 20 45 44 6f 6e 6b 65 79 20 32 30 30 30 20 48 61 EDonkey 2000 Ha
0030: 73 68 20 41 6c 67 6f 72 69 74 68 6d 2e 0a 20 2a sh Algorithm.. *
0040: 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 3a 20 32 . * Copyright: 2
0050: 30 30 36 2d 32 30 31 32 20 41 6c 65 6b 73 65 79 006-2012 Aleksey
0060: 20 4b 72 61 76 63 68 65 6e 6b 6f 20 3c 72 68 61 Kravchenko <rha
0070: 73 68 2e 61 64 6d 69 6e 40 67 6d 61 69 6c 2e 63 sh.admin@gmail.c
0080: 6f 6d 3e 0a 20 2a 0a 20 2a 20 50 65 72 6d 69 73 om>. *. * Permis
0090: 73 69 6f 6e 20 69 73 20 68 65 72 65 62 79 20 67 sion is hereby g
00a0: 72 61 6e 74 65 64 2c 20 20 66 72 65 65 20 6f 66 ranted, free of
00b0: 20 63 68 61 72 67 65 2c 20 20 74 6f 20 61 6e 79 charge, to any
00c0: 20 70 65 72 73 6f 6e 20 20 6f 62 74 61 69 6e 69 person obtaini
00d0: 6e 67 20 61 0a 20 2a 20 63 6f 70 79 20 6f 66 20 ng a. * copy of
00e0: 74 68 69 73 20 73 6f 66 74 77 61 72 65 20 61 6e this software an
00f0: 64 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 d associated doc
0100: 75 6d 65 6e 74 61 74 69 6f 6e 20 66 69 6c 65 73 umentation files
0110: 20 28 74 68 65 20 22 53 6f 66 74 77 61 72 65 22 (the "Software"
0120: 29 2c 0a 20 2a 20 74 6f 20 64 65 61 6c 20 69 6e ),. * to deal in
0130: 20 74 68 65 20 53 6f 66 74 77 61 72 65 20 77 69 the Software wi
0140: 74 68 6f 75 74 20 72 65 73 74 72 69 63 74 69 6f thout restrictio
0150: 6e 2c 20 20 69 6e 63 6c 75 64 69 6e 67 20 77 69 n, including wi
0160: 74 68 6f 75 74 20 6c 69 6d 69 74 61 74 69 6f 6e thout limitation
0170: 0a 20 2a 20 74 68 65 20 72 69 67 68 74 73 20 74 . * the rights t
0180: 6f 20 20 75 73 65 2c 20 63 6f 70 79 2c 20 6d 6f o use, copy, mo
0190: 64 69 66 79 2c 20 20 6d 65 72 67 65 2c 20 70 75 dify, merge, pu
01a0: 62 6c 69 73 68 2c 20 64 69 73 74 72 69 62 75 74 blish, distribut
01b0: 65 2c 20 73 75 62 6c 69 63 65 6e 73 65 2c 0a 20 e, sublicense,.
01c0: 2a 20 61 6e 64 2f 6f 72 20 73 65 6c 6c 20 63 6f * and/or sell co
01d0: 70 69 65 73 20 20 6f 66 20 20 74 68 65 20 53 6f pies of the So
01e0: 66 74 77 61 72 65 2c 20 20 61 6e 64 20 74 6f 20 ftware, and to
01f0: 70 65 72 6d 69 74 20 20 70 65 72 73 6f 6e 73 20 permit persons
0200: 20 74 6f 20 77 68 6f 6d 20 74 68 65 0a 20 2a 20 to whom the. *
0210: 53 6f 66 74 77 61 72 65 20 69 73 20 66 75 72 6e Software is furn
0220: 69 73 68 65 64 20 74 6f 20 64 6f 20 73 6f 2e 0a ished to do so..
0230: 20 2a 0a 20 2a 20 54 68 69 73 20 70 72 6f 67 72 *. * This progr
0240: 61 6d 20 20 69 73 20 20 64 69 73 74 72 69 62 75 am is distribu
0250: 74 65 64 20 20 69 6e 20 20 74 68 65 20 20 68 6f ted in the ho
0260: 70 65 20 20 74 68 61 74 20 69 74 20 77 69 6c 6c pe that it will
0270: 20 62 65 20 75 73 65 66 75 6c 2c 20 20 62 75 74 be useful, but
0280: 0a 20 2a 20 57 49 54 48 4f 55 54 20 41 4e 59 20 . * WITHOUT ANY
0290: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
02a0: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
02b0: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 20 4d ed warranty of M
02c0: 45 52 43 48 41 4e 54 41 42 49 4c 49 54 59 0a 20 ERCHANTABILITY.
02d0: 2a 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 * or FITNESS FOR
02e0: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
02f0: 52 50 4f 53 45 2e 20 20 55 73 65 20 74 68 69 73 RPOSE. Use this
0300: 20 70 72 6f 67 72 61 6d 20 20 61 74 20 20 79 6f program at yo
0310: 75 72 20 6f 77 6e 20 72 69 73 6b 21 0a 20 2a 0a ur own risk!. *.
0320: 20 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 * This file imp
0330: 6c 65 6d 65 6e 74 73 20 65 4d 75 6c 65 2d 63 6f lements eMule-co
0340: 6d 70 61 74 69 62 6c 65 20 76 65 72 73 69 6f 6e mpatible version
0350: 20 6f 66 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 of algorithm..
0360: 2a 20 4e 6f 74 65 20 74 68 61 74 20 65 44 6f 6e * Note that eDon
0370: 6b 65 79 20 61 6e 64 20 65 4d 75 6c 65 20 65 64 key and eMule ed
0380: 32 6b 20 68 61 73 68 65 73 20 61 72 65 20 64 69 2k hashes are di
0390: 66 66 65 72 65 6e 74 20 6f 6e 6c 79 20 66 6f 72 fferent only for
03a0: 0a 20 2a 20 66 69 6c 65 73 20 63 6f 6e 74 61 69 . * files contai
03b0: 6e 69 6e 67 20 65 78 61 63 74 6c 79 20 6d 75 6c ning exactly mul
03c0: 74 69 70 6c 65 20 6f 66 20 39 37 32 38 30 30 30 tiple of 9728000
03d0: 20 62 79 74 65 73 2e 0a 20 2a 0a 20 2a 20 54 68 bytes.. *. * Th
03e0: 65 20 66 69 6c 65 20 64 61 74 61 20 69 73 20 64 e file data is d
03f0: 69 76 69 64 65 64 20 69 6e 74 6f 20 66 75 6c 6c ivided into full
0400: 20 63 68 75 6e 6b 73 20 6f 66 20 39 35 30 30 20 chunks of 9500
0410: 4b 69 42 20 28 39 37 32 38 30 30 30 20 62 79 74 KiB (9728000 byt
0420: 65 73 29 20 70 6c 75 73 0a 20 2a 20 61 20 72 65 es) plus. * a re
0430: 6d 61 69 6e 64 65 72 20 63 68 75 6e 6b 2c 20 61 mainder chunk, a
0440: 6e 64 20 61 20 73 65 70 61 72 61 74 65 20 31 32 nd a separate 12
0450: 38 2d 62 69 74 20 4d 44 34 20 68 61 73 68 20 69 8-bit MD4 hash i
0460: 73 20 63 6f 6d 70 75 74 65 64 20 66 6f 72 20 65 s computed for e
0470: 61 63 68 2e 0a 20 2a 20 49 66 20 74 68 65 20 66 ach.. * If the f
0480: 69 6c 65 20 6c 65 6e 67 74 68 20 69 73 20 61 6e ile length is an
0490: 20 65 78 61 63 74 20 6d 75 6c 74 69 70 6c 65 20 exact multiple
04a0: 6f 66 20 39 35 30 30 20 4b 69 42 2c 20 74 68 65 of 9500 KiB, the
04b0: 20 72 65 6d 61 69 6e 64 65 72 20 7a 65 72 6f 0a remainder zero.
04c0: 20 2a 20 73 69 7a 65 20 63 68 75 6e 6b 20 69 73 * size chunk is
04d0: 20 73 74 69 6c 6c 20 75 73 65 64 20 61 74 20 74 still used at t
04e0: 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 68 61 he end of the ha
04f0: 73 68 20 6c 69 73 74 2e 20 54 68 65 20 65 64 32 sh list. The ed2
0500: 6b 20 68 61 73 68 20 69 73 0a 20 2a 20 63 6f 6d k hash is. * com
0510: 70 75 74 65 64 20 62 79 20 63 6f 6e 63 61 74 65 puted by concate
0520: 6e 61 74 69 6e 67 20 74 68 65 20 63 68 75 6e 6b nating the chunk
0530: 73 27 20 4d 44 34 20 68 61 73 68 65 73 20 69 6e s' MD4 hashes in
0540: 20 6f 72 64 65 72 20 61 6e 64 20 68 61 73 68 69 order and hashi
0550: 6e 67 20 74 68 65 0a 20 2a 20 72 65 73 75 6c 74 ng the. * result
0560: 20 75 73 69 6e 67 20 4d 44 34 2e 20 41 6c 74 68 using MD4. Alth
0570: 6f 75 67 68 2c 20 69 66 20 74 68 65 20 66 69 6c ough, if the fil
0580: 65 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 e is composed of
0590: 20 61 20 73 69 6e 67 6c 65 20 6e 6f 6e 2d 66 75 a single non-fu
05a0: 6c 6c 0a 20 2a 20 63 68 75 6e 6b 2c 20 69 74 73 ll. * chunk, its
05b0: 20 4d 44 34 20 68 61 73 68 20 69 73 20 72 65 74 MD4 hash is ret
05c0: 75 72 6e 65 64 20 77 69 74 68 20 6e 6f 20 66 75 urned with no fu
05d0: 72 74 68 65 72 20 6d 6f 64 69 66 69 63 61 74 69 rther modificati
05e0: 6f 6e 73 2e 0a 20 2a 0a 20 2a 20 53 65 65 20 68 ons.. *. * See h
05f0: 74 74 70 3a 2f 2f 65 6e 2e 77 69 6b 69 70 65 64 ttp://en.wikiped
0600: 69 61 2e 6f 72 67 2f 77 69 6b 69 2f 45 44 6f 6e ia.org/wiki/EDon
0610: 6b 65 79 5f 6e 65 74 77 6f 72 6b 20 66 6f 72 20 key_network for
0620: 61 6c 67 6f 72 69 74 68 6d 20 64 65 73 63 72 69 algorithm descri
0630: 70 74 69 6f 6e 2e 0a 20 2a 2f 0a 0a 23 69 6e 63 ption.. */..#inc
0640: 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a lude <string.h>.
0650: 23 69 6e 63 6c 75 64 65 20 22 65 64 32 6b 2e 68 #include "ed2k.h
0660: 22 0a 0a 2f 2a 20 65 61 63 68 20 68 61 73 68 65 "../* each hashe
0670: 64 20 66 69 6c 65 20 69 73 20 64 69 76 69 64 65 d file is divide
0680: 64 20 69 6e 74 6f 20 39 35 30 30 20 4b 69 42 20 d into 9500 KiB
0690: 73 69 7a 65 64 20 63 68 75 6e 6b 73 20 2a 2f 0a sized chunks */.
06a0: 23 64 65 66 69 6e 65 20 45 44 32 4b 5f 43 48 55 #define ED2K_CHU
06b0: 4e 4b 5f 53 49 5a 45 20 39 37 32 38 30 30 30 0a NK_SIZE 9728000.
06c0: 0a 2f 2a 2a 0a 20 2a 20 49 6e 69 74 69 61 6c 69 ./**. * Initiali
06d0: 7a 65 20 63 6f 6e 74 65 78 74 20 62 65 66 6f 72 ze context befor
06e0: 65 20 63 61 6c 63 75 6c 61 69 6e 67 20 68 61 73 e calculaing has
06f0: 68 2e 0a 20 2a 0a 20 2a 20 40 70 61 72 61 6d 20 h.. *. * @param
0700: 63 74 78 20 63 6f 6e 74 65 78 74 20 74 6f 20 69 ctx context to i
0710: 6e 69 74 69 61 6c 69 7a 65 0a 20 2a 2f 0a 76 6f nitialize. */.vo
0720: 69 64 20 72 68 61 73 68 5f 65 64 32 6b 5f 69 6e id rhash_ed2k_in
0730: 69 74 28 65 64 32 6b 5f 63 74 78 20 2a 63 74 78 it(ed2k_ctx *ctx
0740: 29 0a 7b 0a 09 72 68 61 73 68 5f 6d 64 34 5f 69 ).{..rhash_md4_i
0750: 6e 69 74 28 26 63 74 78 2d 3e 6d 64 34 5f 63 6f nit(&ctx->md4_co
0760: 6e 74 65 78 74 29 3b 0a 09 72 68 61 73 68 5f 6d ntext);..rhash_m
0770: 64 34 5f 69 6e 69 74 28 26 63 74 78 2d 3e 6d 64 d4_init(&ctx->md
0780: 34 5f 63 6f 6e 74 65 78 74 5f 69 6e 6e 65 72 29 4_context_inner)
0790: 3b 0a 09 63 74 78 2d 3e 6e 6f 74 5f 65 6d 75 6c ;..ctx->not_emul
07a0: 65 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 2a 0a 20 2a e = 0;.}../**. *
07b0: 20 43 61 6c 63 75 6c 61 74 65 20 6d 65 73 73 61 Calculate messa
07c0: 67 65 20 68 61 73 68 2e 0a 20 2a 20 43 61 6e 20 ge hash.. * Can
07d0: 62 65 20 63 61 6c 6c 65 64 20 72 65 70 65 61 74 be called repeat
07e0: 65 64 6c 79 20 77 69 74 68 20 63 68 75 6e 6b 73 edly with chunks
07f0: 20 6f 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 of the message
0800: 74 6f 20 62 65 20 68 61 73 68 65 64 2e 0a 20 2a to be hashed.. *
0810: 0a 20 2a 20 40 70 61 72 61 6d 20 63 74 78 20 74 . * @param ctx t
0820: 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 63 6f 6e he algorithm con
0830: 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 text containing
0840: 63 75 72 72 65 6e 74 20 68 61 73 68 69 6e 67 20 current hashing
0850: 73 74 61 74 65 0a 20 2a 20 40 70 61 72 61 6d 20 state. * @param
0860: 6d 73 67 20 6d 65 73 73 61 67 65 20 63 68 75 6e msg message chun
0870: 6b 0a 20 2a 20 40 70 61 72 61 6d 20 73 69 7a 65 k. * @param size
0880: 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6d length of the m
0890: 65 73 73 61 67 65 20 63 68 75 6e 6b 0a 20 2a 2f essage chunk. */
08a0: 0a 76 6f 69 64 20 72 68 61 73 68 5f 65 64 32 6b .void rhash_ed2k
08b0: 5f 75 70 64 61 74 65 28 65 64 32 6b 5f 63 74 78 _update(ed2k_ctx
08c0: 20 2a 63 74 78 2c 20 63 6f 6e 73 74 20 75 6e 73 *ctx, const uns
08d0: 69 67 6e 65 64 20 63 68 61 72 2a 20 6d 73 67 2c igned char* msg,
08e0: 20 73 69 7a 65 5f 74 20 73 69 7a 65 29 0a 7b 0a size_t size).{.
08f0: 09 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 .unsigned char c
0900: 68 75 6e 6b 5f 6d 64 34 5f 68 61 73 68 5b 31 36 hunk_md4_hash[16
0910: 5d 3b 0a 09 75 6e 73 69 67 6e 65 64 20 62 6c 6f ];..unsigned blo
0920: 63 6b 6c 65 66 74 20 3d 20 45 44 32 4b 5f 43 48 ckleft = ED2K_CH
0930: 55 4e 4b 5f 53 49 5a 45 20 2d 20 28 75 6e 73 69 UNK_SIZE - (unsi
0940: 67 6e 65 64 29 63 74 78 2d 3e 6d 64 34 5f 63 6f gned)ctx->md4_co
0950: 6e 74 65 78 74 5f 69 6e 6e 65 72 2e 6c 65 6e 67 ntext_inner.leng
0960: 74 68 3b 0a 0a 09 2f 2a 20 6e 6f 74 65 3a 20 65 th;.../* note: e
0970: 4d 75 6c 65 2d 63 6f 6d 70 61 74 69 62 6c 65 20 Mule-compatible
0980: 61 6c 67 6f 72 69 74 68 6d 20 68 61 73 68 65 73 algorithm hashes
0990: 20 62 79 20 6d 64 34 5f 69 6e 6e 65 72 0a 09 2a by md4_inner..*
09a0: 20 74 68 65 20 6d 65 73 73 61 67 65 73 20 77 68 the messages wh
09b0: 69 63 68 20 73 69 7a 65 73 20 61 72 65 20 6d 75 ich sizes are mu
09c0: 6c 74 69 70 6c 65 20 6f 66 20 39 37 32 38 30 30 ltiple of 972800
09d0: 30 0a 09 2a 20 61 6e 64 20 74 68 65 6e 20 70 72 0..* and then pr
09e0: 6f 63 65 73 73 65 73 20 6f 62 74 61 69 6e 65 64 ocesses obtained
09f0: 20 68 61 73 68 20 62 79 20 65 78 74 65 72 6e 61 hash by externa
0a00: 6c 20 6d 64 34 20 2a 2f 0a 0a 09 77 68 69 6c 65 l md4 */...while
0a10: 20 28 20 73 69 7a 65 20 3e 3d 20 62 6c 6f 63 6b ( size >= block
0a20: 6c 65 66 74 20 29 0a 09 7b 0a 09 09 69 66 20 28 left )..{...if (
0a30: 73 69 7a 65 20 3d 3d 20 62 6c 6f 63 6b 6c 65 66 size == blocklef
0a40: 74 20 26 26 20 63 74 78 2d 3e 6e 6f 74 5f 65 6d t && ctx->not_em
0a50: 75 6c 65 29 20 62 72 65 61 6b 3b 0a 0a 09 09 2f ule) break;..../
0a60: 2a 20 69 66 20 69 6e 74 65 72 6e 61 6c 20 65 64 * if internal ed
0a70: 32 6b 20 63 68 75 6e 6b 20 69 73 20 66 75 6c 6c 2k chunk is full
0a80: 2c 20 74 68 65 6e 20 66 69 6e 61 6c 69 7a 65 20 , then finalize
0a90: 69 74 20 2a 2f 0a 09 09 72 68 61 73 68 5f 6d 64 it */...rhash_md
0aa0: 34 5f 75 70 64 61 74 65 28 26 63 74 78 2d 3e 6d 4_update(&ctx->m
0ab0: 64 34 5f 63 6f 6e 74 65 78 74 5f 69 6e 6e 65 72 d4_context_inner
0ac0: 2c 20 6d 73 67 2c 20 62 6c 6f 63 6b 6c 65 66 74 , msg, blockleft
0ad0: 29 3b 0a 09 09 6d 73 67 20 2b 3d 20 62 6c 6f 63 );...msg += bloc
0ae0: 6b 6c 65 66 74 3b 0a 09 09 73 69 7a 65 20 2d 3d kleft;...size -=
0af0: 20 62 6c 6f 63 6b 6c 65 66 74 3b 0a 09 09 62 6c blockleft;...bl
0b00: 6f 63 6b 6c 65 66 74 20 3d 20 45 44 32 4b 5f 43 ockleft = ED2K_C
0b10: 48 55 4e 4b 5f 53 49 5a 45 3b 0a 0a 09 09 2f 2a HUNK_SIZE;..../*
0b20: 20 6a 75 73 74 20 66 69 6e 69 73 68 65 64 20 61 just finished a
0b30: 6e 20 65 64 32 6b 20 63 68 75 6e 6b 2c 20 75 70 n ed2k chunk, up
0b40: 64 61 74 69 6e 67 20 6d 64 34 5f 65 78 74 65 72 dating md4_exter
0b50: 6e 61 6c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 09 nal context */..
0b60: 09 72 68 61 73 68 5f 6d 64 34 5f 66 69 6e 61 6c .rhash_md4_final
0b70: 28 26 63 74 78 2d 3e 6d 64 34 5f 63 6f 6e 74 65 (&ctx->md4_conte
0b80: 78 74 5f 69 6e 6e 65 72 2c 20 63 68 75 6e 6b 5f xt_inner, chunk_
0b90: 6d 64 34 5f 68 61 73 68 29 3b 0a 09 09 72 68 61 md4_hash);...rha
0ba0: 73 68 5f 6d 64 34 5f 75 70 64 61 74 65 28 26 63 sh_md4_update(&c
0bb0: 74 78 2d 3e 6d 64 34 5f 63 6f 6e 74 65 78 74 2c tx->md4_context,
0bc0: 20 63 68 75 6e 6b 5f 6d 64 34 5f 68 61 73 68 2c chunk_md4_hash,
0bd0: 20 31 36 29 3b 0a 09 09 72 68 61 73 68 5f 6d 64 16);...rhash_md
0be0: 34 5f 69 6e 69 74 28 26 63 74 78 2d 3e 6d 64 34 4_init(&ctx->md4
0bf0: 5f 63 6f 6e 74 65 78 74 5f 69 6e 6e 65 72 29 3b _context_inner);
0c00: 0a 09 7d 0a 0a 09 69 66 20 28 73 69 7a 65 29 20 ..}...if (size)
0c10: 7b 0a 09 09 2f 2a 20 68 61 73 68 20 6c 65 66 74 {.../* hash left
0c20: 6f 76 65 72 73 20 2a 2f 0a 09 09 72 68 61 73 68 overs */...rhash
0c30: 5f 6d 64 34 5f 75 70 64 61 74 65 28 26 63 74 78 _md4_update(&ctx
0c40: 2d 3e 6d 64 34 5f 63 6f 6e 74 65 78 74 5f 69 6e ->md4_context_in
0c50: 6e 65 72 2c 20 6d 73 67 2c 20 73 69 7a 65 29 3b ner, msg, size);
0c60: 0a 09 7d 0a 7d 0a 0a 2f 2a 2a 0a 20 2a 20 53 74 ..}.}../**. * St
0c70: 6f 72 65 20 63 61 6c 63 75 6c 61 74 65 64 20 68 ore calculated h
0c80: 61 73 68 20 69 6e 74 6f 20 74 68 65 20 67 69 76 ash into the giv
0c90: 65 6e 20 61 72 72 61 79 2e 0a 20 2a 0a 20 2a 20 en array.. *. *
0ca0: 40 70 61 72 61 6d 20 63 74 78 20 74 68 65 20 61 @param ctx the a
0cb0: 6c 67 6f 72 69 74 68 6d 20 63 6f 6e 74 65 78 74 lgorithm context
0cc0: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63 75 72 72 containing curr
0cd0: 65 6e 74 20 68 61 73 68 69 6e 67 20 73 74 61 74 ent hashing stat
0ce0: 65 0a 20 2a 20 40 70 61 72 61 6d 20 72 65 73 75 e. * @param resu
0cf0: 6c 74 20 63 61 6c 63 75 6c 61 74 65 64 20 68 61 lt calculated ha
0d00: 73 68 20 69 6e 20 62 69 6e 61 72 79 20 66 6f 72 sh in binary for
0d10: 6d 0a 20 2a 2f 0a 76 6f 69 64 20 72 68 61 73 68 m. */.void rhash
0d20: 5f 65 64 32 6b 5f 66 69 6e 61 6c 28 65 64 32 6b _ed2k_final(ed2k
0d30: 5f 63 74 78 20 2a 63 74 78 2c 20 75 6e 73 69 67 _ctx *ctx, unsig
0d40: 6e 65 64 20 63 68 61 72 20 72 65 73 75 6c 74 5b ned char result[
0d50: 31 36 5d 29 0a 7b 0a 09 2f 2a 20 63 68 65 63 6b 16]).{../* check
0d60: 20 69 66 20 68 61 73 68 65 64 20 6d 65 73 73 61 if hashed messa
0d70: 67 65 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 ge size is great
0d80: 65 72 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 45 er or equal to E
0d90: 44 32 4b 5f 43 48 55 4e 4b 5f 53 49 5a 45 20 2a D2K_CHUNK_SIZE *
0da0: 2f 0a 09 69 66 20 28 20 63 74 78 2d 3e 6d 64 34 /..if ( ctx->md4
0db0: 5f 63 6f 6e 74 65 78 74 2e 6c 65 6e 67 74 68 20 _context.length
0dc0: 29 20 7b 0a 0a 09 09 2f 2a 20 6e 6f 74 65 3a 20 ) {..../* note:
0dd0: 77 65 69 72 64 20 65 4d 75 6c 65 20 61 6c 67 6f weird eMule algo
0de0: 72 69 74 68 6d 20 61 6c 77 61 79 73 20 70 72 6f rithm always pro
0df0: 63 65 73 73 65 73 20 74 68 65 20 69 6e 6e 65 72 cesses the inner
0e00: 0a 09 09 20 2a 20 6d 64 34 20 63 6f 6e 74 65 78 ... * md4 contex
0e10: 74 2c 20 6e 6f 20 6d 61 74 74 65 72 20 69 66 20 t, no matter if
0e20: 69 74 20 63 6f 6e 74 61 69 6e 73 20 64 61 74 61 it contains data
0e30: 20 6f 72 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a or is empty */.
0e40: 0a 09 09 2f 2a 20 69 66 20 61 6e 79 20 64 61 74 .../* if any dat
0e50: 61 20 61 72 65 20 6c 65 66 74 20 69 6e 20 74 68 a are left in th
0e60: 65 20 6d 64 34 5f 63 6f 6e 74 65 78 74 5f 69 6e e md4_context_in
0e70: 6e 65 72 20 2a 2f 0a 09 09 69 66 20 28 20 28 73 ner */...if ( (s
0e80: 69 7a 65 5f 74 29 63 74 78 2d 3e 6d 64 34 5f 63 ize_t)ctx->md4_c
0e90: 6f 6e 74 65 78 74 5f 69 6e 6e 65 72 2e 6c 65 6e ontext_inner.len
0ea0: 67 74 68 20 3e 20 30 20 7c 7c 20 21 63 74 78 2d gth > 0 || !ctx-
0eb0: 3e 6e 6f 74 5f 65 6d 75 6c 65 29 0a 09 09 7b 0a >not_emule)...{.
0ec0: 09 09 09 2f 2a 20 65 6d 75 6c 65 20 61 6c 67 6f .../* emule algo
0ed0: 72 69 74 68 6d 20 70 72 6f 63 65 73 73 65 73 20 rithm processes
0ee0: 61 64 69 74 69 6f 6e 61 6c 20 62 6c 6f 63 6b 2c aditional block,
0ef0: 20 65 76 65 6e 20 69 66 20 69 74 27 73 20 65 6d even if it's em
0f00: 70 74 79 20 2a 2f 0a 09 09 09 75 6e 73 69 67 6e pty */....unsign
0f10: 65 64 20 63 68 61 72 20 6d 64 34 5f 64 69 67 65 ed char md4_dige
0f20: 73 74 5f 69 6e 6e 65 72 5b 31 36 5d 3b 0a 09 09 st_inner[16];...
0f30: 09 72 68 61 73 68 5f 6d 64 34 5f 66 69 6e 61 6c .rhash_md4_final
0f40: 28 26 63 74 78 2d 3e 6d 64 34 5f 63 6f 6e 74 65 (&ctx->md4_conte
0f50: 78 74 5f 69 6e 6e 65 72 2c 20 6d 64 34 5f 64 69 xt_inner, md4_di
0f60: 67 65 73 74 5f 69 6e 6e 65 72 29 3b 0a 09 09 09 gest_inner);....
0f70: 72 68 61 73 68 5f 6d 64 34 5f 75 70 64 61 74 65 rhash_md4_update
0f80: 28 26 63 74 78 2d 3e 6d 64 34 5f 63 6f 6e 74 65 (&ctx->md4_conte
0f90: 78 74 2c 20 6d 64 34 5f 64 69 67 65 73 74 5f 69 xt, md4_digest_i
0fa0: 6e 6e 65 72 2c 20 31 36 29 3b 0a 09 09 7d 0a 09 nner, 16);...}..
0fb0: 09 2f 2a 20 66 69 72 73 74 20 63 61 6c 6c 20 66 ./* first call f
0fc0: 69 6e 61 6c 20 74 6f 20 66 6c 75 73 68 20 6d 64 inal to flush md
0fd0: 34 20 62 75 66 66 65 72 20 61 6e 64 20 66 69 6e 4 buffer and fin
0fe0: 61 6c 69 7a 65 20 74 68 65 20 68 61 73 68 20 76 alize the hash v
0ff0: 61 6c 75 65 20 2a 2f 0a 09 09 72 68 61 73 68 5f alue */...rhash_
1000: 6d 64 34 5f 66 69 6e 61 6c 28 26 63 74 78 2d 3e md4_final(&ctx->
1010: 6d 64 34 5f 63 6f 6e 74 65 78 74 2c 20 72 65 73 md4_context, res
1020: 75 6c 74 29 3b 0a 09 09 2f 2a 20 73 74 6f 72 65 ult);.../* store
1030: 20 74 68 65 20 63 61 6c 63 75 6c 61 74 65 64 20 the calculated
1040: 65 64 32 6b 20 68 61 73 68 20 69 6e 20 74 68 65 ed2k hash in the
1050: 20 6d 64 34 5f 63 6f 6e 74 65 78 74 5f 69 6e 6e md4_context_inn
1060: 65 72 2e 68 61 73 68 20 2a 2f 0a 09 09 6d 65 6d er.hash */...mem
1070: 63 70 79 28 26 63 74 78 2d 3e 6d 64 34 5f 63 6f cpy(&ctx->md4_co
1080: 6e 74 65 78 74 5f 69 6e 6e 65 72 2e 68 61 73 68 ntext_inner.hash
1090: 2c 20 26 63 74 78 2d 3e 6d 64 34 5f 63 6f 6e 74 , &ctx->md4_cont
10a0: 65 78 74 2e 68 61 73 68 2c 20 6d 64 34 5f 68 61 ext.hash, md4_ha
10b0: 73 68 5f 73 69 7a 65 29 3b 0a 09 7d 20 65 6c 73 sh_size);..} els
10c0: 65 20 7b 0a 09 09 2f 2a 20 72 65 74 75 72 6e 20 e {.../* return
10d0: 6a 75 73 74 20 74 68 65 20 6d 65 73 73 61 67 65 just the message
10e0: 20 4d 44 34 20 68 61 73 68 20 2a 2f 0a 09 09 69 MD4 hash */...i
10f0: 66 20 28 72 65 73 75 6c 74 29 20 72 68 61 73 68 f (result) rhash
1100: 5f 6d 64 34 5f 66 69 6e 61 6c 28 26 63 74 78 2d _md4_final(&ctx-
1110: 3e 6d 64 34 5f 63 6f 6e 74 65 78 74 5f 69 6e 6e >md4_context_inn
1120: 65 72 2c 20 72 65 73 75 6c 74 29 3b 0a 09 7d 0a er, result);..}.
1130: 7d 0a }.