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

@@ -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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -30,7 +30,6 @@
#include "Base64.hxx" #include "Base64.hxx"
#include "lib/ffmpeg/Error.hxx" #include "lib/ffmpeg/Error.hxx"
#include "util/StringView.hxx" #include "util/StringView.hxx"
#include "util/WritableBuffer.hxx"
extern "C" { extern "C" {
#include <libavutil/base64.h> #include <libavutil/base64.h>
@@ -39,7 +38,7 @@ extern "C" {
#include <string> #include <string>
size_t 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 /* since av_base64_decode() wants a null-terminated string, we
need to make a copy here and null-terminate it */ need to make a copy here and null-terminate it */
@@ -48,9 +47,9 @@ DecodeBase64(WritableBuffer<void> out, StringView in)
} }
size_t 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) if (nbytes < 0)
throw MakeFfmpegError(nbytes, "Base64 decoder failed"); throw MakeFfmpegError(nbytes, "Base64 decoder failed");

@@ -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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -27,12 +27,11 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef BASE64_HXX #pragma once
#define BASE64_HXX
#include <cstddef> #include <cstddef>
#include <span>
template<typename T> struct WritableBuffer;
struct StringView; struct StringView;
constexpr size_t constexpr size_t
@@ -45,12 +44,10 @@ CalculateBase64OutputSize(size_t in_size) noexcept
* Throws on error. * Throws on error.
*/ */
size_t size_t
DecodeBase64(WritableBuffer<void> out, StringView in); DecodeBase64(std::span<std::byte> out, StringView in);
/** /**
* Throws on error. * Throws on error.
*/ */
size_t size_t
DecodeBase64(WritableBuffer<void> out, const char *in); DecodeBase64(std::span<std::byte> out, const char *in);
#endif

@@ -22,7 +22,6 @@
#include "tag/Id3Picture.hxx" #include "tag/Id3Picture.hxx"
#include "tag/Handler.hxx" #include "tag/Handler.hxx"
#include "util/StringView.hxx" #include "util/StringView.hxx"
#include "util/WritableBuffer.hxx"
#include "config.h" #include "config.h"
#include <memory> #include <memory>
@@ -36,7 +35,7 @@ ScanVorbisPicture(StringView value, TagHandler &handler) noexcept
return; return;
size_t debase64_size = CalculateBase64OutputSize(value.size); 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 { try {
debase64_size = debase64_size =