From 3dd2434149d3d0f3241c842820a25028b2490341 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 24 May 2022 14:26:59 +0200 Subject: [PATCH] lib/crypto/Base64: add overload which returns AllocatedArray --- src/lib/crypto/Base64.cxx | 10 ++++++++++ src/lib/crypto/Base64.hxx | 10 ++++++++++ src/lib/xiph/VorbisPicture.cxx | 14 ++------------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/lib/crypto/Base64.cxx b/src/lib/crypto/Base64.cxx index f68e35868..5d7ddd36b 100644 --- a/src/lib/crypto/Base64.cxx +++ b/src/lib/crypto/Base64.cxx @@ -29,6 +29,7 @@ #include "Base64.hxx" #include "lib/ffmpeg/Error.hxx" +#include "util/AllocatedArray.hxx" extern "C" { #include @@ -54,3 +55,12 @@ DecodeBase64(std::span out, const char *in) return nbytes; } + +AllocatedArray +DecodeBase64(std::string_view src) +{ + AllocatedArray dest{CalculateBase64OutputSize(src.size())}; + const std::size_t dest_size = DecodeBase64(dest, src); + dest.SetSize(dest_size); + return dest; +} diff --git a/src/lib/crypto/Base64.hxx b/src/lib/crypto/Base64.hxx index 3ab98ae1f..732d1e912 100644 --- a/src/lib/crypto/Base64.hxx +++ b/src/lib/crypto/Base64.hxx @@ -33,6 +33,8 @@ #include #include +template class AllocatedArray; + constexpr size_t CalculateBase64OutputSize(size_t in_size) noexcept { @@ -50,3 +52,11 @@ DecodeBase64(std::span out, std::string_view in); */ size_t DecodeBase64(std::span out, const char *in); + +/** + * Throws on error. + * + * @return the decoded string + */ +AllocatedArray +DecodeBase64(std::string_view src); diff --git a/src/lib/xiph/VorbisPicture.cxx b/src/lib/xiph/VorbisPicture.cxx index 9339b6715..738aa3c14 100644 --- a/src/lib/xiph/VorbisPicture.cxx +++ b/src/lib/xiph/VorbisPicture.cxx @@ -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 - 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(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;