From 77e0e09222fdc121d9044289229a6f19c0ba406b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 7 Jul 2022 15:18:51 +0200 Subject: Inline the output size functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- algorithm_output_size.c | 16 +--------------- hmac_state_output_size.c | 6 +----- libsha2.h | 47 +++++++++++++++++++++++++++++++++++------------ state_output_size.c | 12 +----------- 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/algorithm_output_size.c b/algorithm_output_size.c index f7a0352..4457c49 100644 --- a/algorithm_output_size.c +++ b/algorithm_output_size.c @@ -8,18 +8,4 @@ * @param algorithm The hashing algorithm * @return The number of bytes in the output, zero on error */ -size_t -libsha2_algorithm_output_size(enum libsha2_algorithm algorithm) -{ - switch (algorithm) { - case LIBSHA2_224: return 28; - case LIBSHA2_256: return 32; - case LIBSHA2_384: return 48; - case LIBSHA2_512: return 64; - case LIBSHA2_512_224: return 28; - case LIBSHA2_512_256: return 32; - default: - errno = EINVAL; - return 0; - } -} +extern inline size_t libsha2_algorithm_output_size(enum libsha2_algorithm); diff --git a/hmac_state_output_size.c b/hmac_state_output_size.c index d1a94dd..eff86b9 100644 --- a/hmac_state_output_size.c +++ b/hmac_state_output_size.c @@ -8,8 +8,4 @@ * @param state The state * @return The number of bytes in the output, zero on error */ -size_t -libsha2_hmac_state_output_size(const struct libsha2_hmac_state *restrict state) -{ - return libsha2_algorithm_output_size(state->sha2_state.algorithm); -} +extern inline size_t libsha2_hmac_state_output_size(const struct libsha2_hmac_state *restrict); diff --git a/libsha2.h b/libsha2.h index 0399f98..3cd19b9 100644 --- a/libsha2.h +++ b/libsha2.h @@ -2,6 +2,7 @@ #ifndef LIBSHA2_H #define LIBSHA2_H 1 +#include #include #include @@ -195,26 +196,44 @@ __attribute__((__leaf__, __nothrow__, __nonnull__)) int libsha2_init(struct libsha2_state *restrict, enum libsha2_algorithm); /** - * Get the output size of the algorithm specified for a state + * Get the output size of an algorithm * - * @param state The state - * @return The number of bytes in the output, zero on error + * @param algorithm The hashing algorithm + * @return The number of bytes in the output, zero on error */ #if defined(__GNUC__) -__attribute__((__nothrow__, __nonnull__, __pure__)) +__attribute__((__warn_unused_result__, __nothrow__)) #endif -size_t libsha2_state_output_size(const struct libsha2_state *restrict); +inline size_t +libsha2_algorithm_output_size(enum libsha2_algorithm algorithm__) +{ + switch (algorithm__) { + case LIBSHA2_224: return 28; + case LIBSHA2_256: return 32; + case LIBSHA2_384: return 48; + case LIBSHA2_512: return 64; + case LIBSHA2_512_224: return 28; + case LIBSHA2_512_256: return 32; + default: + errno = EINVAL; + return 0; + } +} /** - * Get the output size of an algorithm + * Get the output size of the algorithm specified for a state * - * @param algorithm The hashing algorithm - * @return The number of bytes in the output, zero on error + * @param state The state + * @return The number of bytes in the output, zero on error */ #if defined(__GNUC__) -__attribute__((__leaf__, __nothrow__, __const__)) +__attribute__((__warn_unused_result__, __nothrow__, __nonnull__)) #endif -size_t libsha2_algorithm_output_size(enum libsha2_algorithm); +inline size_t +libsha2_state_output_size(const struct libsha2_state *restrict state__) +{ + return libsha2_algorithm_output_size(state__->algorithm); +} /** * Absorb more of the message @@ -338,9 +357,13 @@ int libsha2_hmac_init(struct libsha2_hmac_state *restrict, enum libsha2_algorith * @return The number of bytes in the output, zero on error */ #if defined(__GNUC__) -__attribute__((__nothrow__, __nonnull__, __pure__)) +__attribute__((__warn_unused_result__, __nothrow__, __nonnull__)) #endif -size_t libsha2_hmac_state_output_size(const struct libsha2_hmac_state *restrict); +inline size_t +libsha2_hmac_state_output_size(const struct libsha2_hmac_state *restrict state__) +{ + return libsha2_algorithm_output_size(state__->sha2_state.algorithm); +} /** * Feed data into the HMAC algorithm diff --git a/state_output_size.c b/state_output_size.c index 85da7c2..9ab0f51 100644 --- a/state_output_size.c +++ b/state_output_size.c @@ -2,14 +2,4 @@ #include "common.h" -/** - * Get the output size of the algorithm specified for a state - * - * @param state The state - * @return The number of bytes in the output, zero on error - */ -size_t -libsha2_state_output_size(const struct libsha2_state *restrict state) -{ - return libsha2_algorithm_output_size(state->algorithm); -} +extern inline size_t libsha2_state_output_size(const struct libsha2_state *restrict); -- cgit v1.2.3-70-g09d2