DECLARE_TEST(md5, empty) { md5_t* md5; char md5str[32]; string_t digest; md5 = md5_allocate(); md5_digest_finalize(md5); digest = md5_get_digest(md5, md5str, sizeof(md5str)); EXPECT_STRINGEQ(digest, string_const(STRING_CONST("D41D8CD98F00B204E9800998ECF8427E"))); md5_initialize(md5); md5_digest_finalize(md5); digest = md5_get_digest(md5, md5str, sizeof(md5str)); EXPECT_STRINGEQ(digest, string_const(STRING_CONST("D41D8CD98F00B204E9800998ECF8427E"))); md5_deallocate(md5); return 0; }
DECLARE_TEST( md5, empty ) { md5_t* md5; char* md5str; md5 = md5_allocate(); md5_digest_finalize( md5 ); md5str = md5_get_digest( md5 ); EXPECT_STREQ( md5str, "D41D8CD98F00B204E9800998ECF8427E" ); string_deallocate( md5str ); md5_initialize( md5 ); md5_digest_finalize( md5 ); md5str = md5_get_digest( md5 ); EXPECT_STREQ( md5str, "D41D8CD98F00B204E9800998ECF8427E" ); string_deallocate( md5str ); md5_deallocate( md5 ); return 0; }
uint128_t stream_md5(stream_t* stream) { size_t cur, ic, lastc, num, limit; md5_t md5; uint128_t ret = uint128_null(); unsigned char buf[1025]; bool ignore_lf = false; if (stream->vtable->md5) return stream->vtable->md5(stream); if (stream_is_sequential(stream) || !(stream->mode & STREAM_IN)) return ret; cur = stream_tell(stream); stream_seek(stream, 0, STREAM_SEEK_BEGIN); md5_initialize(&md5); limit = sizeof(buf)-1; buf[limit] = 0; while (!stream_eos(stream)) { num = stream->vtable->read(stream, buf, limit); if (!num) continue; if (stream->mode & STREAM_BINARY) md5_digest(&md5, buf, (size_t)num); else { //If last buffer ended with CR, ignore a leading LF lastc = 0; if (ignore_lf && (buf[0] == '\n')) lastc = 1; ignore_lf = false; //Digest one line at a time //Treat all line endings (LF, CR, CR+LF) as Unix style LF. If file has mixed line endings //(for example, one line ending in a single CR and next is empty and ending in a single LF), //it will not work! /*lint -e{850} */ for (ic = lastc; ic < num && ic < limit; ++ic) { bool was_cr = (buf[ic] == '\r'); bool was_lf = (buf[ic] == '\n'); if (was_cr || was_lf) { if (was_cr && (ic == limit-1)) ignore_lf = true; //Make next buffer ignore leading LF as it is part of CR+LF buf[ic] = '\n'; md5_digest(&md5, buf + lastc, (size_t)((ic - lastc) + 1)); //Include the LF if (was_cr && (buf[ic + 1] == '\n')) //Check for CR+LF ++ic; lastc = ic + 1; } } if (lastc < num) md5_digest(&md5, buf + lastc, (size_t)(num - lastc)); } } stream_seek(stream, (ssize_t)cur, STREAM_SEEK_BEGIN); md5_digest_finalize(&md5); ret = md5_get_digest_raw(&md5); md5_finalize(&md5); return ret; }