flac_md5.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #if HAVE_CONFIG_H
  2. # include <config.h>
  3. #endif
  4. #include <stdlib.h> /* for malloc() */
  5. #include <string.h> /* for memcpy() */
  6. #include "flac_private_autocpu.h"
  7. #include "flac_private_md5.h"
  8. #include "flac_share_alloc.h"
  9. #ifndef FLaC__INLINE
  10. #define FLaC__INLINE
  11. #endif
  12. /*
  13. * This code implements the MD5 message-digest algorithm.
  14. * The algorithm is due to Ron Rivest. This code was
  15. * written by Colin Plumb in 1993, no copyright is claimed.
  16. * This code is in the public domain; do with it what you wish.
  17. *
  18. * Equivalent code is available from RSA Data Security, Inc.
  19. * This code has been tested against that, and is equivalent,
  20. * except that you don't need to include two pages of legalese
  21. * with every copy.
  22. *
  23. * To compute the message digest of a chunk of bytes, declare an
  24. * MD5Context structure, pass it to MD5Init, call MD5Update as
  25. * needed on buffers full of bytes, and then call MD5Final, which
  26. * will fill a supplied 16-byte array with the digest.
  27. *
  28. * Changed so as no longer to depend on Colin Plumb's `usual.h' header
  29. * definitions; now uses stuff from dpkg's config.h.
  30. * - Ian Jackson <ijackson@nyx.cs.du.edu>.
  31. * Still in the public domain.
  32. *
  33. * Josh Coalson: made some changes to integrate with libFLAC.
  34. * Still in the public domain.
  35. */
  36. /* The four core functions - F1 is optimized somewhat */
  37. /* #define F1(x, y, z) (x & y | ~x & z) */
  38. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  39. #define F2(x, y, z) F1(z, x, y)
  40. #define F3(x, y, z) (x ^ y ^ z)
  41. #define F4(x, y, z) (y ^ (x | ~z))
  42. /* This is the central step in the MD5 algorithm. */
  43. #define MD5STEP(f,w,x,y,z,in,s) \
  44. (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
  45. /*
  46. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  47. * reflect the addition of 16 longwords of new data. MD5Update blocks
  48. * the data and converts bytes into longwords for this routine.
  49. */
  50. static void FLAC__MD5Transform(FLAC__uint32 buf[4], FLAC__uint32 const in[16])
  51. {
  52. register FLAC__uint32 a, b, c, d;
  53. a = buf[0];
  54. b = buf[1];
  55. c = buf[2];
  56. d = buf[3];
  57. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  58. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  59. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  60. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  61. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  62. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  63. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  64. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  65. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  66. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  67. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  68. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  69. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  70. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  71. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  72. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  73. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  74. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  75. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  76. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  77. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  78. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  79. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  80. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  81. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  82. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  83. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  84. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  85. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  86. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  87. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  88. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  89. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  90. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  91. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  92. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  93. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  94. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  95. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  96. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  97. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  98. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  99. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  100. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  101. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  102. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  103. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  104. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  105. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  106. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  107. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  108. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  109. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  110. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  111. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  112. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  113. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  114. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  115. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  116. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  117. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  118. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  119. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  120. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  121. buf[0] += a;
  122. buf[1] += b;
  123. buf[2] += c;
  124. buf[3] += d;
  125. }
  126. #if WORDS_BIGENDIAN
  127. //@@@@@@ OPT: use bswap/intrinsics
  128. static void byteSwap(FLAC__uint32 *buf, unsigned words)
  129. {
  130. register FLAC__uint32 x;
  131. do {
  132. x = *buf;
  133. x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff);
  134. *buf++ = (x >> 16) | (x << 16);
  135. } while (--words);
  136. }
  137. static void byteSwapX16(FLAC__uint32 *buf)
  138. {
  139. register FLAC__uint32 x;
  140. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  141. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  142. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  143. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  144. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  145. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  146. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  147. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  148. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  149. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  150. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  151. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  152. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  153. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  154. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  155. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf = (x >> 16) | (x << 16);
  156. }
  157. #else
  158. #define byteSwap(buf, words)
  159. #define byteSwapX16(buf)
  160. #endif
  161. /*
  162. * Update context to reflect the concatenation of another buffer full
  163. * of bytes.
  164. */
  165. static void FLAC__MD5Update(FLAC__MD5Context *ctx, FLAC__byte const *buf, unsigned len)
  166. {
  167. FLAC__uint32 t;
  168. /* Update byte count */
  169. t = ctx->bytes[0];
  170. if ((ctx->bytes[0] = t + len) < t)
  171. ctx->bytes[1]++; /* Carry from low to high */
  172. t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
  173. if (t > len) {
  174. memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len);
  175. return;
  176. }
  177. /* First chunk is an odd size */
  178. memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t);
  179. byteSwapX16(ctx->in);
  180. FLAC__MD5Transform(ctx->buf, ctx->in);
  181. buf += t;
  182. len -= t;
  183. /* Process data in 64-byte chunks */
  184. while (len >= 64) {
  185. memcpy(ctx->in, buf, 64);
  186. byteSwapX16(ctx->in);
  187. FLAC__MD5Transform(ctx->buf, ctx->in);
  188. buf += 64;
  189. len -= 64;
  190. }
  191. /* Handle any remaining bytes of data. */
  192. memcpy(ctx->in, buf, len);
  193. }
  194. /*
  195. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  196. * initialization constants.
  197. */
  198. void FLAC__MD5Init(FLAC__MD5Context *ctx)
  199. {
  200. ctx->buf[0] = 0x67452301;
  201. ctx->buf[1] = 0xefcdab89;
  202. ctx->buf[2] = 0x98badcfe;
  203. ctx->buf[3] = 0x10325476;
  204. ctx->bytes[0] = 0;
  205. ctx->bytes[1] = 0;
  206. ctx->internal_buf = 0;
  207. ctx->capacity = 0;
  208. }
  209. /*
  210. * Final wrapup - pad to 64-byte boundary with the bit pattern
  211. * 1 0* (64-bit count of bits processed, MSB-first)
  212. */
  213. void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *ctx)
  214. {
  215. int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
  216. FLAC__byte *p = (FLAC__byte *)ctx->in + count;
  217. /* Set the first char of padding to 0x80. There is always room. */
  218. *p++ = 0x80;
  219. /* Bytes of padding needed to make 56 bytes (-8..55) */
  220. count = 56 - 1 - count;
  221. if (count < 0) { /* Padding forces an extra block */
  222. memset(p, 0, count + 8);
  223. byteSwapX16(ctx->in);
  224. FLAC__MD5Transform(ctx->buf, ctx->in);
  225. p = (FLAC__byte *)ctx->in;
  226. count = 56;
  227. }
  228. memset(p, 0, count);
  229. byteSwap(ctx->in, 14);
  230. /* Append length in bits and transform */
  231. ctx->in[14] = ctx->bytes[0] << 3;
  232. ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
  233. FLAC__MD5Transform(ctx->buf, ctx->in);
  234. byteSwap(ctx->buf, 4);
  235. memcpy(digest, ctx->buf, 16);
  236. #if 0
  237. memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
  238. if(0 != ctx->internal_buf) {
  239. free(ctx->internal_buf);
  240. ctx->internal_buf = 0;
  241. ctx->capacity = 0;
  242. }
  243. #else
  244. // ppgb BUGFIX: found two mistakes, hence: // 20131226
  245. if(0 != ctx->internal_buf) {
  246. free(ctx->internal_buf);
  247. ctx->internal_buf = 0;
  248. ctx->capacity = 0;
  249. }
  250. memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
  251. #endif
  252. }
  253. /*
  254. * Convert the incoming audio signal to a byte stream
  255. */
  256. static void format_input_(FLAC__byte *buf, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
  257. {
  258. unsigned channel, sample;
  259. register FLAC__int32 a_word;
  260. register FLAC__byte *buf_ = buf;
  261. #if WORDS_BIGENDIAN
  262. #else
  263. if(channels == 2 && bytes_per_sample == 2) {
  264. FLAC__int16 *buf1_ = ((FLAC__int16*)buf_) + 1;
  265. memcpy(buf_, signal[0], sizeof(FLAC__int32) * samples);
  266. for(sample = 0; sample < samples; sample++, buf1_+=2)
  267. *buf1_ = (FLAC__int16)signal[1][sample];
  268. }
  269. else if(channels == 1 && bytes_per_sample == 2) {
  270. FLAC__int16 *buf1_ = (FLAC__int16*)buf_;
  271. for(sample = 0; sample < samples; sample++)
  272. *buf1_++ = (FLAC__int16)signal[0][sample];
  273. }
  274. else
  275. #endif
  276. if(bytes_per_sample == 2) {
  277. if(channels == 2) {
  278. for(sample = 0; sample < samples; sample++) {
  279. a_word = signal[0][sample];
  280. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  281. *buf_++ = (FLAC__byte)a_word;
  282. a_word = signal[1][sample];
  283. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  284. *buf_++ = (FLAC__byte)a_word;
  285. }
  286. }
  287. else if(channels == 1) {
  288. for(sample = 0; sample < samples; sample++) {
  289. a_word = signal[0][sample];
  290. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  291. *buf_++ = (FLAC__byte)a_word;
  292. }
  293. }
  294. else {
  295. for(sample = 0; sample < samples; sample++) {
  296. for(channel = 0; channel < channels; channel++) {
  297. a_word = signal[channel][sample];
  298. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  299. *buf_++ = (FLAC__byte)a_word;
  300. }
  301. }
  302. }
  303. }
  304. else if(bytes_per_sample == 3) {
  305. if(channels == 2) {
  306. for(sample = 0; sample < samples; sample++) {
  307. a_word = signal[0][sample];
  308. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  309. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  310. *buf_++ = (FLAC__byte)a_word;
  311. a_word = signal[1][sample];
  312. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  313. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  314. *buf_++ = (FLAC__byte)a_word;
  315. }
  316. }
  317. else if(channels == 1) {
  318. for(sample = 0; sample < samples; sample++) {
  319. a_word = signal[0][sample];
  320. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  321. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  322. *buf_++ = (FLAC__byte)a_word;
  323. }
  324. }
  325. else {
  326. for(sample = 0; sample < samples; sample++) {
  327. for(channel = 0; channel < channels; channel++) {
  328. a_word = signal[channel][sample];
  329. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  330. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  331. *buf_++ = (FLAC__byte)a_word;
  332. }
  333. }
  334. }
  335. }
  336. else if(bytes_per_sample == 1) {
  337. if(channels == 2) {
  338. for(sample = 0; sample < samples; sample++) {
  339. a_word = signal[0][sample];
  340. *buf_++ = (FLAC__byte)a_word;
  341. a_word = signal[1][sample];
  342. *buf_++ = (FLAC__byte)a_word;
  343. }
  344. }
  345. else if(channels == 1) {
  346. for(sample = 0; sample < samples; sample++) {
  347. a_word = signal[0][sample];
  348. *buf_++ = (FLAC__byte)a_word;
  349. }
  350. }
  351. else {
  352. for(sample = 0; sample < samples; sample++) {
  353. for(channel = 0; channel < channels; channel++) {
  354. a_word = signal[channel][sample];
  355. *buf_++ = (FLAC__byte)a_word;
  356. }
  357. }
  358. }
  359. }
  360. else { /* bytes_per_sample == 4, maybe optimize more later */
  361. for(sample = 0; sample < samples; sample++) {
  362. for(channel = 0; channel < channels; channel++) {
  363. a_word = signal[channel][sample];
  364. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  365. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  366. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  367. *buf_++ = (FLAC__byte)a_word;
  368. }
  369. }
  370. }
  371. }
  372. /*
  373. * Convert the incoming audio signal to a byte stream and FLAC__MD5Update it.
  374. */
  375. FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
  376. {
  377. const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample;
  378. /* overflow check */
  379. if((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample)
  380. return false;
  381. if((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples)
  382. return false;
  383. if(ctx->capacity < bytes_needed) {
  384. FLAC__byte *tmp = (FLAC__byte*)realloc(ctx->internal_buf, bytes_needed);
  385. if(0 == tmp) {
  386. free(ctx->internal_buf);
  387. if(0 == (ctx->internal_buf = (FLAC__byte*)safe_malloc_(bytes_needed)))
  388. return false;
  389. }
  390. ctx->internal_buf = tmp;
  391. ctx->capacity = bytes_needed;
  392. }
  393. format_input_(ctx->internal_buf, signal, channels, samples, bytes_per_sample);
  394. FLAC__MD5Update(ctx, ctx->internal_buf, bytes_needed);
  395. return true;
  396. }