util/HexFormat: use std::span instead of ConstBuffer

This commit is contained in:
Max Kellermann 2022-02-18 15:03:22 +01:00 committed by Max Kellermann
parent 8333927737
commit 27e78c71e0
7 changed files with 51 additions and 62 deletions

View File

@ -211,8 +211,8 @@ QobuzClient::MakeSignedUrl(const char *object, const char *method,
concatenated_query += app_secret; concatenated_query += app_secret;
const auto md5_hex = MD5Hex({concatenated_query.data(), concatenated_query.size()}); const auto md5_hex = MD5Hex(std::as_bytes(std::span{concatenated_query}));
q(uri, "request_sig", md5_hex.c_str()); q(uri, "request_sig", std::string_view{md5_hex.data(), md5_hex.size()});
return uri; return uri;
} }

View File

@ -50,21 +50,22 @@ GlobalInitMD5() noexcept
#endif #endif
} }
std::array<uint8_t, 16> std::array<std::byte, 16>
MD5(ConstBuffer<void> input) noexcept MD5(std::span<const std::byte> input) noexcept
{ {
#ifdef HAVE_LIBAVUTIL #ifdef HAVE_LIBAVUTIL
std::array<uint8_t, 16> result; std::array<std::byte, 16> result;
av_md5_sum(&result.front(), (const uint8_t *)input.data, input.size); av_md5_sum((uint8_t *)result.data(),
(const uint8_t *)input.data(), input.size());
return result; return result;
#else #else
return Gcrypt::MD5(input); return Gcrypt::MD5(input);
#endif #endif
} }
StringBuffer<33> std::array<char, 32>
MD5Hex(ConstBuffer<void> input) noexcept MD5Hex(std::span<const std::byte> input) noexcept
{ {
const auto raw = MD5(input); const auto raw = MD5(input);
return HexFormatBuffer<raw.size()>(&raw.front()); return HexFormat<raw.size()>(raw);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2018-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2018-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,25 +27,18 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef MD5_HXX #pragma once
#define MD5_HXX
#include "util/StringBuffer.hxx"
#include <array> #include <array>
#include <cstdint> #include <span>
template<typename T> struct ConstBuffer;
void void
GlobalInitMD5() noexcept; GlobalInitMD5() noexcept;
[[gnu::pure]] [[gnu::pure]]
std::array<uint8_t, 16> std::array<std::byte, 16>
MD5(ConstBuffer<void> input) noexcept; MD5(std::span<const std::byte> input) noexcept;
[[gnu::pure]] [[gnu::pure]]
StringBuffer<33> std::array<char, 32>
MD5Hex(ConstBuffer<void> input) noexcept; MD5Hex(std::span<const std::byte> input) noexcept;
#endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2018-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,28 +27,24 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef GCRYPT_HASH_HXX #pragma once
#define GCRYPT_HASH_HXX
#include "util/ConstBuffer.hxx"
#include <gcrypt.h> #include <gcrypt.h>
#include <array> #include <array>
#include <span>
namespace Gcrypt { namespace Gcrypt {
template<int algo, size_t size> template<int algo, size_t size>
[[gnu::pure]] [[gnu::pure]]
auto auto
Hash(ConstBuffer<void> input) noexcept Hash(std::span<const std::byte> input) noexcept
{ {
std::array<uint8_t, size> result; std::array<std::byte, size> result;
gcry_md_hash_buffer(algo, &result.front(), gcry_md_hash_buffer(algo, &result.front(),
input.data, input.size); input.data(), input.size());
return result; return result;
} }
} /* namespace Gcrypt */ } /* namespace Gcrypt */
#endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2018-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2018-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -32,8 +32,8 @@
namespace Gcrypt { namespace Gcrypt {
std::array<uint8_t, 16> std::array<std::byte, 16>
MD5(ConstBuffer<void> input) noexcept MD5(std::span<const std::byte> input) noexcept
{ {
return Gcrypt::Hash<GCRY_MD_MD5, 16>(input); return Gcrypt::Hash<GCRY_MD_MD5, 16>(input);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2018-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2018-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,22 +27,15 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef GCRYPT_MD5_HXX #pragma once
#define GCRYPT_MD5_HXX
#include "util/StringBuffer.hxx"
#include <array> #include <array>
#include <cstdint> #include <span>
template<typename T> struct ConstBuffer;
namespace Gcrypt { namespace Gcrypt {
[[gnu::pure]] [[gnu::pure]]
std::array<uint8_t, 16> std::array<std::byte, 16>
MD5(ConstBuffer<void> input) noexcept; MD5(std::span<const std::byte> input) noexcept;
} // namespace Gcrypt } // namespace Gcrypt
#endif

View File

@ -32,10 +32,7 @@
#pragma once #pragma once
#include "ConstBuffer.hxx" #include <array>
#include "StringBuffer.hxx"
#include <cstddef>
#include <cstdint> #include <cstdint>
constexpr char hex_digits[] = "0123456789abcdef"; constexpr char hex_digits[] = "0123456789abcdef";
@ -84,6 +81,14 @@ HexFormatUint64Fixed(char dest[16], uint64_t number) noexcept
return dest; return dest;
} }
#if __cplusplus >= 202002 || (defined(__GNUC__) && __GNUC__ >= 10)
#include <version>
#endif
#ifdef __cpp_lib_span
#include <array>
#include <span>
/** /**
* Format the given input buffer of bytes to hex. The caller ensures * 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. * 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 * @return a pointer to one after the last written character
*/ */
constexpr char * constexpr char *
HexFormat(char *output, ConstBuffer<uint8_t> input) noexcept HexFormat(char *output, std::span<const std::byte> input) noexcept
{ {
for (const auto &i : input) for (const auto &i : input)
output = HexFormatUint8Fixed(output, i); output = HexFormatUint8Fixed(output, (uint8_t)i);
return output; return output;
} }
/** /**
* Like HexFormat(), but return a #StringBuffer with exactly the * Return a std::array<char> (not null-terminated) containing a hex
* required size. * dump of the given fixed-size input.
*/ */
template<size_t size> template<std::size_t size>
[[gnu::pure]]
constexpr auto constexpr auto
HexFormatBuffer(const uint8_t *src) noexcept HexFormat(std::span<const std::byte, size> input) noexcept
{ {
StringBuffer<size * 2 + 1> dest; std::array<char, size * 2> output;
*HexFormat(dest.data(), {src, size}) = 0; HexFormat(output.data(), input);
return dest; return output;
} }
#endif