Skip to content

Commit

Permalink
util/HexFormat: use std::span instead of ConstBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jun 1, 2022
1 parent 8333927 commit 27e78c7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/input/plugins/QobuzClient.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 8 additions & 7 deletions src/lib/crypto/MD5.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
21 changes: 7 additions & 14 deletions src/lib/crypto/MD5.hxx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
16 changes: 6 additions & 10 deletions src/lib/gcrypt/Hash.hxx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/lib/gcrypt/MD5.cxx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
17 changes: 5 additions & 12 deletions src/lib/gcrypt/MD5.hxx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
34 changes: 20 additions & 14 deletions src/util/HexFormat.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@

#pragma once

#include "ConstBuffer.hxx"
#include "StringBuffer.hxx"

#include <cstddef>
#include <array>
#include <cstdint>

constexpr char hex_digits[] = "0123456789abcdef";
Expand Down Expand Up @@ -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.
Expand All @@ -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

0 comments on commit 27e78c7

Please sign in to comment.