LibGfx: JPGLoader: Centralize error handling in read_be_word

This commit is contained in:
ericLemanissier 2022-12-23 13:12:58 +01:00 committed by Tim Flynn
parent 14eefedbf9
commit 05b9e6ac3b
Notes: sideshowbarker 2024-07-18 05:37:06 +09:00

View file

@ -447,18 +447,20 @@ static inline bool is_valid_marker(const Marker marker)
return false; return false;
} }
static inline u16 read_be_word(InputMemoryStream& stream) static inline ErrorOr<u16> read_be_word(InputMemoryStream& stream)
{ {
BigEndian<u16> tmp; BigEndian<u16> tmp;
stream >> tmp; stream >> tmp;
TRY(stream.try_handle_any_error());
return tmp; return tmp;
} }
static inline Marker read_marker_at_cursor(InputMemoryStream& stream) static inline Marker read_marker_at_cursor(InputMemoryStream& stream)
{ {
u16 marker = read_be_word(stream); auto result = read_be_word(stream);
if (stream.handle_any_error()) if (result.is_error())
return JPG_INVALID; return JPG_INVALID;
u16 marker = result.release_value();
if (is_valid_marker(marker)) if (is_valid_marker(marker))
return marker; return marker;
if (marker != 0xFFFF) if (marker != 0xFFFF)
@ -480,9 +482,7 @@ static ErrorOr<void> read_start_of_scan(InputMemoryStream& stream, JPGLoadingCon
return Error::from_string_literal("SOS found before reading a SOF"); return Error::from_string_literal("SOS found before reading a SOF");
} }
u16 bytes_to_read = read_be_word(stream); u16 bytes_to_read = TRY(read_be_word(stream)) - 2;
TRY(stream.try_handle_any_error());
bytes_to_read -= 2;
TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size)); TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size));
u8 component_count = 0; u8 component_count = 0;
stream >> component_count; stream >> component_count;
@ -549,22 +549,18 @@ static ErrorOr<void> read_start_of_scan(InputMemoryStream& stream, JPGLoadingCon
static ErrorOr<void> read_reset_marker(InputMemoryStream& stream, JPGLoadingContext& context) static ErrorOr<void> read_reset_marker(InputMemoryStream& stream, JPGLoadingContext& context)
{ {
u16 bytes_to_read = read_be_word(stream); u16 bytes_to_read = TRY(read_be_word(stream)) - 2;
TRY(stream.try_handle_any_error());
bytes_to_read -= 2;
if (bytes_to_read != 2) { if (bytes_to_read != 2) {
dbgln_if(JPG_DEBUG, "{}: Malformed reset marker found!", stream.offset()); dbgln_if(JPG_DEBUG, "{}: Malformed reset marker found!", stream.offset());
return Error::from_string_literal("Malformed reset marker found"); return Error::from_string_literal("Malformed reset marker found");
} }
context.dc_reset_interval = read_be_word(stream); context.dc_reset_interval = TRY(read_be_word(stream));
TRY(stream.try_handle_any_error());
return {}; return {};
} }
static ErrorOr<void> read_huffman_table(InputMemoryStream& stream, JPGLoadingContext& context) static ErrorOr<void> read_huffman_table(InputMemoryStream& stream, JPGLoadingContext& context)
{ {
i32 bytes_to_read = read_be_word(stream); i32 bytes_to_read = TRY(read_be_word(stream));
TRY(stream.try_handle_any_error());
TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size)); TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size));
bytes_to_read -= 2; bytes_to_read -= 2;
while (bytes_to_read > 0) { while (bytes_to_read > 0) {
@ -658,8 +654,7 @@ static ErrorOr<void> read_start_of_frame(InputMemoryStream& stream, JPGLoadingCo
return Error::from_string_literal("SOF repeated"); return Error::from_string_literal("SOF repeated");
} }
i32 bytes_to_read = read_be_word(stream); i32 bytes_to_read = TRY(read_be_word(stream));
TRY(stream.try_handle_any_error());
bytes_to_read -= 2; bytes_to_read -= 2;
TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size)); TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size));
@ -671,10 +666,8 @@ static ErrorOr<void> read_start_of_frame(InputMemoryStream& stream, JPGLoadingCo
return Error::from_string_literal("SOF precision != 8"); return Error::from_string_literal("SOF precision != 8");
} }
context.frame.height = read_be_word(stream); context.frame.height = TRY(read_be_word(stream));
TRY(stream.try_handle_any_error()); context.frame.width = TRY(read_be_word(stream));
context.frame.width = read_be_word(stream);
TRY(stream.try_handle_any_error());
if (!context.frame.width || !context.frame.height) { if (!context.frame.width || !context.frame.height) {
dbgln_if(JPG_DEBUG, "{}: ERROR! Image height: {}, Image width: {}!", stream.offset(), context.frame.height, context.frame.width); dbgln_if(JPG_DEBUG, "{}: ERROR! Image height: {}, Image width: {}!", stream.offset(), context.frame.height, context.frame.width);
return Error::from_string_literal("Image frame height of width null"); return Error::from_string_literal("Image frame height of width null");
@ -747,9 +740,7 @@ static ErrorOr<void> read_start_of_frame(InputMemoryStream& stream, JPGLoadingCo
static ErrorOr<void> read_quantization_table(InputMemoryStream& stream, JPGLoadingContext& context) static ErrorOr<void> read_quantization_table(InputMemoryStream& stream, JPGLoadingContext& context)
{ {
i32 bytes_to_read = read_be_word(stream); i32 bytes_to_read = TRY(read_be_word(stream)) - 2;
TRY(stream.try_handle_any_error());
bytes_to_read -= 2;
TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size)); TRY(ensure_bounds_okay(stream.offset(), bytes_to_read, context.data_size));
while (bytes_to_read > 0) { while (bytes_to_read > 0) {
u8 info_byte = 0; u8 info_byte = 0;
@ -773,8 +764,7 @@ static ErrorOr<void> read_quantization_table(InputMemoryStream& stream, JPGLoadi
TRY(stream.try_handle_any_error()); TRY(stream.try_handle_any_error());
table[zigzag_map[i]] = tmp; table[zigzag_map[i]] = tmp;
} else { } else {
table[zigzag_map[i]] = read_be_word(stream); table[zigzag_map[i]] = TRY(read_be_word(stream));
TRY(stream.try_handle_any_error());
} }
} }
TRY(stream.try_handle_any_error()); TRY(stream.try_handle_any_error());
@ -791,9 +781,7 @@ static ErrorOr<void> read_quantization_table(InputMemoryStream& stream, JPGLoadi
static ErrorOr<void> skip_marker_with_length(InputMemoryStream& stream) static ErrorOr<void> skip_marker_with_length(InputMemoryStream& stream)
{ {
u16 bytes_to_skip = read_be_word(stream); u16 bytes_to_skip = TRY(read_be_word(stream)) - 2;
bytes_to_skip -= 2;
TRY(stream.try_handle_any_error());
stream.discard_or_error(bytes_to_skip); stream.discard_or_error(bytes_to_skip);
TRY(stream.try_handle_any_error()); TRY(stream.try_handle_any_error());
return {}; return {};