2022-09-13 19:40:37 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2022-09-13 19:40:37 -05:00
|
|
|
#include <AK/Error.h>
|
|
|
|
#include <AK/Format.h>
|
|
|
|
#include <AK/SourceLocation.h>
|
2024-06-10 15:04:03 -05:00
|
|
|
#include <LibMedia/Forward.h>
|
2022-09-13 19:40:37 -05:00
|
|
|
#include <errno.h>
|
|
|
|
|
2024-06-10 15:04:03 -05:00
|
|
|
namespace Media {
|
2022-09-13 19:40:37 -05:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
using DecoderErrorOr = ErrorOr<T, DecoderError>;
|
|
|
|
|
|
|
|
enum class DecoderErrorCategory : u32 {
|
|
|
|
Unknown,
|
|
|
|
IO,
|
2022-11-03 19:18:38 -05:00
|
|
|
NeedsMoreInput,
|
2022-10-29 17:02:43 -05:00
|
|
|
EndOfStream,
|
2022-10-08 21:54:20 -05:00
|
|
|
Memory,
|
2022-09-13 19:40:37 -05:00
|
|
|
// The input is corrupted.
|
|
|
|
Corrupted,
|
2022-10-10 05:03:57 -05:00
|
|
|
// Invalid call.
|
|
|
|
Invalid,
|
2022-09-13 19:40:37 -05:00
|
|
|
// The input uses features that are not yet implemented.
|
|
|
|
NotImplemented,
|
|
|
|
};
|
|
|
|
|
2023-04-09 11:59:35 -04:00
|
|
|
class DecoderError {
|
2022-09-13 19:40:37 -05:00
|
|
|
public:
|
|
|
|
static DecoderError with_description(DecoderErrorCategory category, StringView description)
|
|
|
|
{
|
|
|
|
return DecoderError(category, description);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Parameters>
|
|
|
|
static DecoderError format(DecoderErrorCategory category, CheckedFormatString<Parameters...>&& format_string, Parameters const&... parameters)
|
|
|
|
{
|
2023-01-13 02:19:40 +00:00
|
|
|
AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... };
|
2023-12-16 17:49:34 +03:30
|
|
|
return DecoderError::with_description(category, ByteString::vformatted(format_string.view(), variadic_format_params));
|
2022-09-13 19:40:37 -05:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:18:54 -06:00
|
|
|
static DecoderError from_source_location(DecoderErrorCategory category, StringView description, SourceLocation location = SourceLocation::current())
|
|
|
|
{
|
|
|
|
return DecoderError::format(category, "[{} @ {}:{}]: {}", location.function_name(), location.filename(), location.line_number(), description);
|
|
|
|
}
|
|
|
|
|
2022-09-13 19:40:37 -05:00
|
|
|
static DecoderError corrupted(StringView description, SourceLocation location = SourceLocation::current())
|
|
|
|
{
|
2022-11-09 07:18:54 -06:00
|
|
|
return DecoderError::from_source_location(DecoderErrorCategory::Corrupted, description, location);
|
2022-09-13 19:40:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static DecoderError not_implemented(SourceLocation location = SourceLocation::current())
|
|
|
|
{
|
|
|
|
return DecoderError::format(DecoderErrorCategory::NotImplemented, "{} is not implemented", location.function_name());
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:34:15 -05:00
|
|
|
DecoderErrorCategory category() const { return m_category; }
|
|
|
|
StringView description() const { return m_description; }
|
|
|
|
StringView string_literal() const { return m_description; }
|
2022-09-13 19:40:37 -05:00
|
|
|
|
|
|
|
private:
|
2023-12-16 17:49:34 +03:30
|
|
|
DecoderError(DecoderErrorCategory category, ByteString description)
|
2022-09-13 19:40:37 -05:00
|
|
|
: m_category(category)
|
|
|
|
, m_description(move(description))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DecoderErrorCategory m_category { DecoderErrorCategory::Unknown };
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_description;
|
2022-09-13 19:40:37 -05:00
|
|
|
};
|
|
|
|
|
2023-01-14 16:34:44 -07:00
|
|
|
#define DECODER_TRY(category, expression) \
|
|
|
|
({ \
|
2023-02-09 13:27:43 -05:00
|
|
|
auto&& _result = ((expression)); \
|
2023-01-14 16:34:44 -07:00
|
|
|
if (_result.is_error()) [[unlikely]] { \
|
|
|
|
auto _error_string = _result.release_error().string_literal(); \
|
|
|
|
return DecoderError::from_source_location( \
|
|
|
|
((category)), _error_string, SourceLocation::current()); \
|
|
|
|
} \
|
|
|
|
static_assert(!::AK::Detail::IsLvalueReference<decltype(_result.release_value())>, \
|
|
|
|
"Do not return a reference from a fallible expression"); \
|
|
|
|
_result.release_value(); \
|
2022-09-13 19:40:37 -05:00
|
|
|
})
|
|
|
|
|
2022-10-08 21:54:20 -05:00
|
|
|
#define DECODER_TRY_ALLOC(expression) DECODER_TRY(DecoderErrorCategory::Memory, expression)
|
|
|
|
|
2022-09-13 19:40:37 -05:00
|
|
|
}
|