diff options
Diffstat (limited to 'libhashsum_init_hasher_from_string.c')
-rw-r--r-- | libhashsum_init_hasher_from_string.c | 134 |
1 files changed, 43 insertions, 91 deletions
diff --git a/libhashsum_init_hasher_from_string.c b/libhashsum_init_hasher_from_string.c index 1bdc70d..55fe882 100644 --- a/libhashsum_init_hasher_from_string.c +++ b/libhashsum_init_hasher_from_string.c @@ -2,26 +2,6 @@ #include "common.h" -#if defined(__GNUC__) -__attribute__((__pure__)) -#endif -static int -equiv(const char *a, const char *b) -{ - while (*a && *b && *b != '[') { - if (tolower(*a) == tolower(*b)) { - a++; - b++; - } else if (*b == '-') { - b++; - } else { - return 0; - } - } - return !*a && (!*b || *b == '['); -} - - static int with_n(int (*initfunc)(struct libhashsum_hasher *, size_t), struct libhashsum_hasher *this, const char *algorithm) { @@ -175,75 +155,47 @@ einval: int libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm) { - if (!strcasecmp(algorithm, "MD2")) - return libhashsum_init_md2_hasher(this); - if (!strcasecmp(algorithm, "MD4")) - return libhashsum_init_md4_hasher(this); - if (!strcasecmp(algorithm, "MD5")) - return libhashsum_init_md5_hasher(this); - if (equiv(algorithm, "RIPEMD-128")) - return libhashsum_init_ripemd_128_hasher(this); - if (equiv(algorithm, "RIPEMD-160")) - return libhashsum_init_ripemd_160_hasher(this); - if (equiv(algorithm, "RIPEMD-256")) - return libhashsum_init_ripemd_256_hasher(this); - if (equiv(algorithm, "RIPEMD-320")) - return libhashsum_init_ripemd_320_hasher(this); - if (equiv(algorithm, "SHA-0")) - return libhashsum_init_sha0_hasher(this); - if (equiv(algorithm, "SHA-1")) - return libhashsum_init_sha1_hasher(this); - if (equiv(algorithm, "SHA-224") || !strcasecmp(algorithm, "SHA2-224")) - return libhashsum_init_sha_224_hasher(this); - if (equiv(algorithm, "SHA-256") || !strcasecmp(algorithm, "SHA2-256")) - return libhashsum_init_sha_256_hasher(this); - if (equiv(algorithm, "SHA-384") || !strcasecmp(algorithm, "SHA2-384")) - return libhashsum_init_sha_384_hasher(this); - if (equiv(algorithm, "SHA-512") || !strcasecmp(algorithm, "SHA2-512")) - return libhashsum_init_sha_512_hasher(this); - if (equiv(algorithm, "SHA-512/224") || !strcasecmp(algorithm, "SHA2-512/224")) - return libhashsum_init_sha_512_224_hasher(this); - if (equiv(algorithm, "SHA-512/256") || !strcasecmp(algorithm, "SHA2-512/256")) - return libhashsum_init_sha_512_256_hasher(this); - if (equiv(algorithm, "Keccak-224")) - return libhashsum_init_keccak_224_hasher(this); - if (equiv(algorithm, "Keccak-256")) - return libhashsum_init_keccak_256_hasher(this); - if (equiv(algorithm, "Keccak-384")) - return libhashsum_init_keccak_384_hasher(this); - if (equiv(algorithm, "Keccak-512")) - return libhashsum_init_keccak_512_hasher(this); - if (!strcasecmp(algorithm, "SHA3-224")) - return libhashsum_init_sha3_224_hasher(this); - if (!strcasecmp(algorithm, "SHA3-256")) - return libhashsum_init_sha3_256_hasher(this); - if (!strcasecmp(algorithm, "SHA3-384")) - return libhashsum_init_sha3_384_hasher(this); - if (!strcasecmp(algorithm, "SHA3-512")) - return libhashsum_init_sha3_512_hasher(this); - if (equiv(algorithm, "SHAKE-128[")) - return with_n(&libhashsum_init_shake128_hasher, this, algorithm); - if (equiv(algorithm, "SHAKE-256[")) - return with_n(&libhashsum_init_shake256_hasher, this, algorithm); - if (equiv(algorithm, "SHAKE-512[")) - return with_n(&libhashsum_init_shake512_hasher, this, algorithm); - if (equiv(algorithm, "RawSHAKE-128[")) - return with_n(&libhashsum_init_rawshake128_hasher, this, algorithm); - if (equiv(algorithm, "RawSHAKE-256[")) - return with_n(&libhashsum_init_rawshake256_hasher, this, algorithm); - if (equiv(algorithm, "RawSHAKE-512[")) - return with_n(&libhashsum_init_rawshake512_hasher, this, algorithm); - if (equiv(algorithm, "Keccak[")) - return with_rcn(&libhashsum_init_keccak_hasher, this, algorithm); - if (equiv(algorithm, "BLAKE-224[")) - return with_salt(&libhashsum_init_blake224_hasher, this, algorithm, 16U); - if (equiv(algorithm, "BLAKE-256[")) - return with_salt(&libhashsum_init_blake256_hasher, this, algorithm, 16U); - if (equiv(algorithm, "BLAKE-384[")) - return with_salt(&libhashsum_init_blake384_hasher, this, algorithm, 32U); - if (equiv(algorithm, "BLAKE-512[")) - return with_salt(&libhashsum_init_blake512_hasher, this, algorithm, 32U); - - errno = EINVAL; - return -1; + enum libhashsum_algorithm algo; + if (!libhashsum_get_algorithm_string(&algo, algorithm)) { + errno = EINVAL; + return -1; + } + switch (algo) { + case LIBHASHSUM_MD2: return libhashsum_init_md2_hasher(this); + case LIBHASHSUM_MD4: return libhashsum_init_md4_hasher(this); + case LIBHASHSUM_MD5: return libhashsum_init_md5_hasher(this); + case LIBHASHSUM_RIPEMD_128: return libhashsum_init_ripemd_128_hasher(this); + case LIBHASHSUM_RIPEMD_160: return libhashsum_init_ripemd_160_hasher(this); + case LIBHASHSUM_RIPEMD_256: return libhashsum_init_ripemd_256_hasher(this); + case LIBHASHSUM_RIPEMD_320: return libhashsum_init_ripemd_320_hasher(this); + case LIBHASHSUM_SHA0: return libhashsum_init_sha0_hasher(this); + case LIBHASHSUM_SHA1: return libhashsum_init_sha1_hasher(this); + case LIBHASHSUM_SHA_224: return libhashsum_init_sha_224_hasher(this); + case LIBHASHSUM_SHA_256: return libhashsum_init_sha_256_hasher(this); + case LIBHASHSUM_SHA_384: return libhashsum_init_sha_384_hasher(this); + case LIBHASHSUM_SHA_512: return libhashsum_init_sha_512_hasher(this); + case LIBHASHSUM_SHA_512_224: return libhashsum_init_sha_512_224_hasher(this); + case LIBHASHSUM_SHA_512_256: return libhashsum_init_sha_512_256_hasher(this); + case LIBHASHSUM_KECCAK_224: return libhashsum_init_keccak_224_hasher(this); + case LIBHASHSUM_KECCAK_256: return libhashsum_init_keccak_256_hasher(this); + case LIBHASHSUM_KECCAK_384: return libhashsum_init_keccak_384_hasher(this); + case LIBHASHSUM_KECCAK_512: return libhashsum_init_keccak_512_hasher(this); + case LIBHASHSUM_SHA3_224: return libhashsum_init_sha3_224_hasher(this); + case LIBHASHSUM_SHA3_256: return libhashsum_init_sha3_256_hasher(this); + case LIBHASHSUM_SHA3_384: return libhashsum_init_sha3_384_hasher(this); + case LIBHASHSUM_SHA3_512: return libhashsum_init_sha3_512_hasher(this); + case LIBHASHSUM_SHAKE128: return with_n(&libhashsum_init_shake128_hasher, this, algorithm); + case LIBHASHSUM_SHAKE256: return with_n(&libhashsum_init_shake256_hasher, this, algorithm); + case LIBHASHSUM_SHAKE512: return with_n(&libhashsum_init_shake512_hasher, this, algorithm); + case LIBHASHSUM_RAWSHAKE128: return with_n(&libhashsum_init_rawshake128_hasher, this, algorithm); + case LIBHASHSUM_RAWSHAKE256: return with_n(&libhashsum_init_rawshake256_hasher, this, algorithm); + case LIBHASHSUM_RAWSHAKE512: return with_n(&libhashsum_init_rawshake512_hasher, this, algorithm); + case LIBHASHSUM_KECCAK: return with_rcn(&libhashsum_init_keccak_hasher, this, algorithm); + case LIBHASHSUM_BLAKE224: return with_salt(&libhashsum_init_blake224_hasher, this, algorithm, 16U); + case LIBHASHSUM_BLAKE256: return with_salt(&libhashsum_init_blake256_hasher, this, algorithm, 16U); + case LIBHASHSUM_BLAKE384: return with_salt(&libhashsum_init_blake384_hasher, this, algorithm, 32U); + case LIBHASHSUM_BLAKE512: return with_salt(&libhashsum_init_blake512_hasher, this, algorithm, 32U); + default: + abort(); + } } |