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 "Base64.hxx"
#include "lib/ffmpeg/Error.hxx" #include "lib/ffmpeg/Error.hxx"
#include "util/AllocatedArray.hxx"
extern "C" { extern "C" {
#include <libavutil/base64.h> #include <libavutil/base64.h>
@ -54,3 +55,12 @@ DecodeBase64(std::span<std::byte> out, const char *in)
return nbytes; 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 <span>
#include <string_view> #include <string_view>
template<typename T> class AllocatedArray;
constexpr size_t constexpr size_t
CalculateBase64OutputSize(size_t in_size) noexcept CalculateBase64OutputSize(size_t in_size) noexcept
{ {
@ -50,3 +52,11 @@ DecodeBase64(std::span<std::byte> out, std::string_view in);
*/ */
size_t size_t
DecodeBase64(std::span<std::byte> out, const char *in); 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 "VorbisPicture.hxx"
#include "lib/crypto/Base64.hxx" #include "lib/crypto/Base64.hxx"
#include "tag/Id3Picture.hxx" #include "tag/Id3Picture.hxx"
#include "tag/Handler.hxx" #include "util/AllocatedArray.hxx"
#include "config.h" #include "config.h"
#include <memory>
void void
ScanVorbisPicture(std::string_view value, TagHandler &handler) noexcept 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 */ /* ignore image files which are too huge */
return; return;
size_t debase64_size = CalculateBase64OutputSize(value.size());
auto debase64_buffer = std::make_unique<std::byte[]>(debase64_size);
try { try {
debase64_size = return ScanId3Apic(DecodeBase64(value), handler);
DecodeBase64({debase64_buffer.get(), debase64_size},
value);
} catch (...) { } catch (...) {
// TODO: log? // TODO: log?
return;
} }
return ScanId3Apic({debase64_buffer.get(), debase64_size}, handler);
#else #else
(void)value; (void)value;
(void)handler; (void)handler;