lib/crypto/Base64: use std::span

This commit is contained in:
Max Kellermann 2022-05-20 11:18:54 +02:00
parent ef54b7d9de
commit 1260a0147a
3 changed files with 10 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2019-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
@ -30,7 +30,6 @@
#include "Base64.hxx"
#include "lib/ffmpeg/Error.hxx"
#include "util/StringView.hxx"
#include "util/WritableBuffer.hxx"
extern "C" {
#include <libavutil/base64.h>
@ -39,7 +38,7 @@ extern "C" {
#include <string>
size_t
DecodeBase64(WritableBuffer<void> out, StringView in)
DecodeBase64(std::span<std::byte> out, StringView in)
{
/* since av_base64_decode() wants a null-terminated string, we
need to make a copy here and null-terminate it */
@ -48,9 +47,9 @@ DecodeBase64(WritableBuffer<void> out, StringView in)
}
size_t
DecodeBase64(WritableBuffer<void> out, const char *in)
DecodeBase64(std::span<std::byte> out, const char *in)
{
int nbytes = av_base64_decode((uint8_t *)out.data, in, out.size);
int nbytes = av_base64_decode((uint8_t *)out.data(), in, out.size());
if (nbytes < 0)
throw MakeFfmpegError(nbytes, "Base64 decoder failed");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2019-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,12 +27,11 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef BASE64_HXX
#define BASE64_HXX
#pragma once
#include <cstddef>
#include <span>
template<typename T> struct WritableBuffer;
struct StringView;
constexpr size_t
@ -45,12 +44,10 @@ CalculateBase64OutputSize(size_t in_size) noexcept
* Throws on error.
*/
size_t
DecodeBase64(WritableBuffer<void> out, StringView in);
DecodeBase64(std::span<std::byte> out, StringView in);
/**
* Throws on error.
*/
size_t
DecodeBase64(WritableBuffer<void> out, const char *in);
#endif
DecodeBase64(std::span<std::byte> out, const char *in);

View File

@ -22,7 +22,6 @@
#include "tag/Id3Picture.hxx"
#include "tag/Handler.hxx"
#include "util/StringView.hxx"
#include "util/WritableBuffer.hxx"
#include "config.h"
#include <memory>
@ -36,7 +35,7 @@ ScanVorbisPicture(StringView value, TagHandler &handler) noexcept
return;
size_t debase64_size = CalculateBase64OutputSize(value.size);
auto debase64_buffer = std::make_unique<uint8_t[]>(debase64_size);
auto debase64_buffer = std::make_unique<std::byte[]>(debase64_size);
try {
debase64_size =