lib/crypto/Base64: add overload which returns AllocatedArray<std::byte>

This commit is contained in:
Max Kellermann 2022-05-24 14:26:59 +02:00
parent 3699514d18
commit 3dd2434149
3 changed files with 22 additions and 12 deletions

View File

@ -29,6 +29,7 @@
#include "Base64.hxx"
#include "lib/ffmpeg/Error.hxx"
#include "util/AllocatedArray.hxx"
extern "C" {
#include <libavutil/base64.h>
@ -54,3 +55,12 @@ DecodeBase64(std::span<std::byte> out, const char *in)
return nbytes;
}
AllocatedArray<std::byte>
DecodeBase64(std::string_view src)
{
AllocatedArray<std::byte> dest{CalculateBase64OutputSize(src.size())};
const std::size_t dest_size = DecodeBase64(dest, src);
dest.SetSize(dest_size);
return dest;
}

View File

@ -33,6 +33,8 @@
#include <span>
#include <string_view>
template<typename T> class AllocatedArray;
constexpr size_t
CalculateBase64OutputSize(size_t in_size) noexcept
{
@ -50,3 +52,11 @@ DecodeBase64(std::span<std::byte> out, std::string_view in);
*/
size_t
DecodeBase64(std::span<std::byte> out, const char *in);
/**
* Throws on error.
*
* @return the decoded string
*/
AllocatedArray<std::byte>
DecodeBase64(std::string_view src);

View File

@ -20,11 +20,9 @@
#include "VorbisPicture.hxx"
#include "lib/crypto/Base64.hxx"
#include "tag/Id3Picture.hxx"
#include "tag/Handler.hxx"
#include "util/AllocatedArray.hxx"
#include "config.h"
#include <memory>
void
ScanVorbisPicture(std::string_view value, TagHandler &handler) noexcept
{
@ -33,19 +31,11 @@ ScanVorbisPicture(std::string_view value, TagHandler &handler) noexcept
/* ignore image files which are too huge */
return;
size_t debase64_size = CalculateBase64OutputSize(value.size());
auto debase64_buffer = std::make_unique<std::byte[]>(debase64_size);
try {
debase64_size =
DecodeBase64({debase64_buffer.get(), debase64_size},
value);
return ScanId3Apic(DecodeBase64(value), handler);
} catch (...) {
// TODO: log?
return;
}
return ScanId3Apic({debase64_buffer.get(), debase64_size}, handler);
#else
(void)value;
(void)handler;