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;
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;
}

View File

@ -50,21 +50,22 @@ GlobalInitMD5() noexcept
#endif
}
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept
std::array<std::byte, 16>
MD5(std::span<const std::byte> input) noexcept
{
#ifdef HAVE_LIBAVUTIL
std::array<uint8_t, 16> result;
av_md5_sum(&result.front(), (const uint8_t *)input.data, input.size);
std::array<std::byte, 16> 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<void> input) noexcept
std::array<char, 32>
MD5Hex(std::span<const std::byte> input) noexcept
{
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
* 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 <array>
#include <cstdint>
template<typename T> struct ConstBuffer;
#include <span>
void
GlobalInitMD5() noexcept;
[[gnu::pure]]
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept;
std::array<std::byte, 16>
MD5(std::span<const std::byte> input) noexcept;
[[gnu::pure]]
StringBuffer<33>
MD5Hex(ConstBuffer<void> input) noexcept;
#endif
std::array<char, 32>
MD5Hex(std::span<const std::byte> input) noexcept;

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
* 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 <gcrypt.h>
#include <array>
#include <span>
namespace Gcrypt {
template<int algo, size_t size>
[[gnu::pure]]
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(),
input.data, input.size);
input.data(), input.size());
return result;
}
} /* 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
* modification, are permitted provided that the following conditions
@ -32,8 +32,8 @@
namespace Gcrypt {
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept
std::array<std::byte, 16>
MD5(std::span<const std::byte> input) noexcept
{
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
* 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 <array>
#include <cstdint>
template<typename T> struct ConstBuffer;
#include <span>
namespace Gcrypt {
[[gnu::pure]]
std::array<uint8_t, 16>
MD5(ConstBuffer<void> input) noexcept;
std::array<std::byte, 16>
MD5(std::span<const std::byte> input) noexcept;
} // namespace Gcrypt
#endif

View File

@ -32,10 +32,7 @@
#pragma once
#include "ConstBuffer.hxx"
#include "StringBuffer.hxx"
#include <cstddef>
#include <array>
#include <cstdint>
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 <version>
#endif
#ifdef __cpp_lib_span
#include <array>
#include <span>
/**
* 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<uint8_t> input) noexcept
HexFormat(char *output, std::span<const std::byte> 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<char> (not null-terminated) containing a hex
* dump of the given fixed-size input.
*/
template<size_t size>
[[gnu::pure]]
template<std::size_t size>
constexpr auto
HexFormatBuffer(const uint8_t *src) noexcept
HexFormat(std::span<const std::byte, size> input) noexcept
{
StringBuffer<size * 2 + 1> dest;
*HexFormat(dest.data(), {src, size}) = 0;
return dest;
std::array<char, size * 2> output;
HexFormat(output.data(), input);
return output;
}
#endif