From 27e78c71e0f49dfd6b369d71f94121b3c1c7ff99 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 18 Feb 2022 15:03:22 +0100 Subject: [PATCH] util/HexFormat: use std::span instead of ConstBuffer --- src/input/plugins/QobuzClient.cxx | 4 ++-- src/lib/crypto/MD5.cxx | 15 +++++++------- src/lib/crypto/MD5.hxx | 21 +++++++------------ src/lib/gcrypt/Hash.hxx | 16 ++++++--------- src/lib/gcrypt/MD5.cxx | 6 +++--- src/lib/gcrypt/MD5.hxx | 17 +++++----------- src/util/HexFormat.hxx | 34 ++++++++++++++++++------------- 7 files changed, 51 insertions(+), 62 deletions(-) diff --git a/src/input/plugins/QobuzClient.cxx b/src/input/plugins/QobuzClient.cxx index 469860ea4..69d3022be 100644 --- a/src/input/plugins/QobuzClient.cxx +++ b/src/input/plugins/QobuzClient.cxx @@ -211,8 +211,8 @@ QobuzClient::MakeSignedUrl(const char *object, const char *method, concatenated_query += app_secret; - const auto md5_hex = MD5Hex({concatenated_query.data(), concatenated_query.size()}); - q(uri, "request_sig", md5_hex.c_str()); + const auto md5_hex = MD5Hex(std::as_bytes(std::span{concatenated_query})); + q(uri, "request_sig", std::string_view{md5_hex.data(), md5_hex.size()}); return uri; } diff --git a/src/lib/crypto/MD5.cxx b/src/lib/crypto/MD5.cxx index 720cea2f4..fcd68e9c3 100644 --- a/src/lib/crypto/MD5.cxx +++ b/src/lib/crypto/MD5.cxx @@ -50,21 +50,22 @@ GlobalInitMD5() noexcept #endif } -std::array -MD5(ConstBuffer input) noexcept +std::array +MD5(std::span input) noexcept { #ifdef HAVE_LIBAVUTIL - std::array result; - av_md5_sum(&result.front(), (const uint8_t *)input.data, input.size); + std::array result; + av_md5_sum((uint8_t *)result.data(), + (const uint8_t *)input.data(), input.size()); return result; #else return Gcrypt::MD5(input); #endif } -StringBuffer<33> -MD5Hex(ConstBuffer input) noexcept +std::array +MD5Hex(std::span input) noexcept { const auto raw = MD5(input); - return HexFormatBuffer(&raw.front()); + return HexFormat(raw); } diff --git a/src/lib/crypto/MD5.hxx b/src/lib/crypto/MD5.hxx index 1116effd8..d69001a92 100644 --- a/src/lib/crypto/MD5.hxx +++ b/src/lib/crypto/MD5.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Max Kellermann + * Copyright 2018-2022 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,25 +27,18 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef MD5_HXX -#define MD5_HXX - -#include "util/StringBuffer.hxx" +#pragma once #include -#include - -template struct ConstBuffer; +#include void GlobalInitMD5() noexcept; [[gnu::pure]] -std::array -MD5(ConstBuffer input) noexcept; +std::array +MD5(std::span input) noexcept; [[gnu::pure]] -StringBuffer<33> -MD5Hex(ConstBuffer input) noexcept; - -#endif +std::array +MD5Hex(std::span input) noexcept; diff --git a/src/lib/gcrypt/Hash.hxx b/src/lib/gcrypt/Hash.hxx index fa704dc89..a1a52a644 100644 --- a/src/lib/gcrypt/Hash.hxx +++ b/src/lib/gcrypt/Hash.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Max Kellermann + * Copyright 2018-2022 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,28 +27,24 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef GCRYPT_HASH_HXX -#define GCRYPT_HASH_HXX - -#include "util/ConstBuffer.hxx" +#pragma once #include #include +#include namespace Gcrypt { template [[gnu::pure]] auto -Hash(ConstBuffer input) noexcept +Hash(std::span input) noexcept { - std::array result; + std::array result; gcry_md_hash_buffer(algo, &result.front(), - input.data, input.size); + input.data(), input.size()); return result; } } /* namespace Gcrypt */ - -#endif diff --git a/src/lib/gcrypt/MD5.cxx b/src/lib/gcrypt/MD5.cxx index db0cf38ad..f1b75401d 100644 --- a/src/lib/gcrypt/MD5.cxx +++ b/src/lib/gcrypt/MD5.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Max Kellermann + * Copyright 2018-2022 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,8 +32,8 @@ namespace Gcrypt { -std::array -MD5(ConstBuffer input) noexcept +std::array +MD5(std::span input) noexcept { return Gcrypt::Hash(input); } diff --git a/src/lib/gcrypt/MD5.hxx b/src/lib/gcrypt/MD5.hxx index d5670f380..f20f91ab1 100644 --- a/src/lib/gcrypt/MD5.hxx +++ b/src/lib/gcrypt/MD5.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 Max Kellermann + * Copyright 2018-2022 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,22 +27,15 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef GCRYPT_MD5_HXX -#define GCRYPT_MD5_HXX - -#include "util/StringBuffer.hxx" +#pragma once #include -#include - -template struct ConstBuffer; +#include namespace Gcrypt { [[gnu::pure]] -std::array -MD5(ConstBuffer input) noexcept; +std::array +MD5(std::span input) noexcept; } // namespace Gcrypt - -#endif diff --git a/src/util/HexFormat.hxx b/src/util/HexFormat.hxx index cea55135e..c1b0708cf 100644 --- a/src/util/HexFormat.hxx +++ b/src/util/HexFormat.hxx @@ -32,10 +32,7 @@ #pragma once -#include "ConstBuffer.hxx" -#include "StringBuffer.hxx" - -#include +#include #include constexpr char hex_digits[] = "0123456789abcdef"; @@ -84,6 +81,14 @@ HexFormatUint64Fixed(char dest[16], uint64_t number) noexcept return dest; } +#if __cplusplus >= 202002 || (defined(__GNUC__) && __GNUC__ >= 10) +#include +#endif + +#ifdef __cpp_lib_span +#include +#include + /** * Format the given input buffer of bytes to hex. The caller ensures * that the output buffer is at least twice as large as the input. @@ -92,24 +97,25 @@ HexFormatUint64Fixed(char dest[16], uint64_t number) noexcept * @return a pointer to one after the last written character */ constexpr char * -HexFormat(char *output, ConstBuffer input) noexcept +HexFormat(char *output, std::span input) noexcept { for (const auto &i : input) - output = HexFormatUint8Fixed(output, i); + output = HexFormatUint8Fixed(output, (uint8_t)i); return output; } /** - * Like HexFormat(), but return a #StringBuffer with exactly the - * required size. + * Return a std::array (not null-terminated) containing a hex + * dump of the given fixed-size input. */ -template -[[gnu::pure]] +template constexpr auto -HexFormatBuffer(const uint8_t *src) noexcept +HexFormat(std::span input) noexcept { - StringBuffer dest; - *HexFormat(dest.data(), {src, size}) = 0; - return dest; + std::array output; + HexFormat(output.data(), input); + return output; } + +#endif