int main(void) { TEST_SHA1 sha1_expected; TEST_SHA1 sha1_got; char hex_buffer[sizeof(SHA1_HEX)]; unsigned val = 0; plan_tests(15); is(sxe_hex_to_unsigned("0", 2), 0, "'0':2 -> 0"); is(sxe_hex_to_unsigned("face", 4), 0xface, "'face':4 -> 0xface"); is(sxe_hex_to_unsigned("B00B", 2), 0xb0, "'B00B':2 -> 0xb0"); is(sxe_hex_to_unsigned("XXXX", 4), SXE_UNSIGNED_MAXIMUM, "'XXXX':4 -> 0x%x (SXE_UNSIGNED_MAXIMUM)", sxe_hex_to_unsigned("XXXX", 4)); is(sxe_valid_hex_to_unsigned("0a", 2, &val), SXE_RETURN_OK, "0a is valid hex"); is(val, 10, "0a hex is 10 decimal"); is(sxe_valid_hex_to_unsigned("F45C6AC6", 8, &val), SXE_RETURN_OK, "F45C6AC6 is valid hex"); is(val, 4099697350, "F45C6AC6 hex is 4099697350 decimal"); is(sxe_valid_hex_to_unsigned("ZZ", 2, &val), SXE_RETURN_ERROR_INTERNAL, "ZZ is not valid hex"); ok(sxe_hex_to_bytes((unsigned char *)&sha1_got, "goofy goober", 12) != SXE_RETURN_OK, "Conversion from hex 'goofy goober' to bytes failed"); is(sxe_hex_to_bytes((unsigned char *)&sha1_got, SHA1_HEX, 40), SXE_RETURN_OK, "Conversion from hex '%s' to bytes succeeded", SHA1_HEX); memcpy(&sha1_expected, sxe_sha1_expected_bytes, sizeof(sha1_expected)); if (memcmp(&sha1_got, &sha1_expected, sizeof(TEST_SHA1)) == 0) { pass( "bytes are as expected"); } else { SXEL1("Expected:"); SXED1(&sha1_expected, sizeof(sha1_expected)); SXEL1("Got:"); SXED1(&sha1_got, sizeof(sha1_got)); fail( "bytes are not as expected"); } tap_test_case_name("sxe_hex_from_bytes"); hex_buffer[sizeof(hex_buffer) - 1] = 0xBE; is(sxe_hex_from_bytes(hex_buffer, sxe_sha1_expected_bytes, sizeof(sxe_sha1_expected_bytes)), hex_buffer, "Returns hex buffer"); is_strncmp(hex_buffer, SHA1_HEX, SXE_LITERAL_LENGTH(SHA1_HEX), "SHA1 converted to hex as expected"); is((unsigned char)hex_buffer[sizeof(hex_buffer) - 1], 0xBE, "Guard byte is intact"); return exit_status(); }
SXE_RETURN sha1_from_hex(SOPHOS_SHA1 * sha1, const char * sha1_in_hex) { SXE_RETURN result; SXEE82("sxe_sha1_from_hex(sha1=%p,sha1_in_hex='%s'", sha1, sha1_in_hex); result = sxe_hex_to_bytes((unsigned char *)sha1, sha1_in_hex, SHA1_IN_HEX_LENGTH); SXER81("return %s", sxe_return_to_string(result)); return result; }
SXE_RETURN md5_from_hex(SOPHOS_MD5 * md5, const char * md5_in_hex) { SXE_RETURN result; SXEE82("(md5=%p,md5_in_hex='%s'", md5, md5_in_hex); result = sxe_hex_to_bytes((unsigned char *)md5, md5_in_hex, MD5_IN_HEX_LENGTH); SXER81("return %s", sxe_return_to_string(result)); return result; }