Skip to content

Commit 02c6486

Browse files
committed
LibGfx/JPEG: Allow random bytes between segments
1 parent 3b86c41 commit 02c6486

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -889,24 +889,14 @@ static inline bool is_supported_marker(Marker const marker)
889889
return false;
890890
}
891891

892-
static inline ErrorOr<Marker> read_marker_at_cursor(JPEGStream& stream)
892+
static inline ErrorOr<Marker> read_until_marker(JPEGStream& stream)
893893
{
894894
u16 marker = TRY(stream.read_u16());
895895

896-
if (marker == 0xFFFF) {
897-
u8 next { 0xFF };
896+
while (!is_supported_marker(marker))
897+
marker = marker << 8 | TRY(stream.read_u8());
898898

899-
while (next == 0xFF)
900-
next = TRY(stream.read_u8());
901-
902-
marker = 0xFF00 | next;
903-
}
904-
905-
if (is_supported_marker(marker))
906-
return marker;
907-
908-
dbgln_if(JPEG_DEBUG, "Unsupported marker: {:#04x} around offset {:#x}", marker, stream.byte_offset());
909-
return Error::from_string_literal("Reached an unsupported marker");
899+
return marker;
910900
}
911901

912902
static ErrorOr<u16> read_effective_chunk_size(JPEGStream& stream)
@@ -1881,13 +1871,13 @@ static ErrorOr<void> handle_miscellaneous_or_table(JPEGStream& stream, JPEGLoadi
18811871

18821872
static ErrorOr<void> parse_header(JPEGStream& stream, JPEGLoadingContext& context)
18831873
{
1884-
auto marker = TRY(read_marker_at_cursor(stream));
1874+
auto marker = TRY(read_until_marker(stream));
18851875
if (marker != JPEG_SOI) {
18861876
dbgln_if(JPEG_DEBUG, "SOI not found: {:x}!", marker);
18871877
return Error::from_string_literal("SOI not found");
18881878
}
18891879
for (;;) {
1890-
marker = TRY(read_marker_at_cursor(stream));
1880+
marker = TRY(read_until_marker(stream));
18911881

18921882
if (is_miscellaneous_or_table_marker(marker)) {
18931883
TRY(handle_miscellaneous_or_table(stream, context, marker));
@@ -1955,7 +1945,7 @@ static ErrorOr<Vector<Macroblock>> construct_macroblocks(JPEGLoadingContext& con
19551945
Vector<Macroblock> macroblocks;
19561946
TRY(macroblocks.try_resize(context.mblock_meta.padded_total));
19571947

1958-
Marker marker = TRY(read_marker_at_cursor(context.stream));
1948+
Marker marker = TRY(read_until_marker(context.stream));
19591949
while (true) {
19601950
if (is_miscellaneous_or_table_marker(marker)) {
19611951
TRY(handle_miscellaneous_or_table(context.stream, context, marker));
@@ -1969,7 +1959,7 @@ static ErrorOr<Vector<Macroblock>> construct_macroblocks(JPEGLoadingContext& con
19691959
return Error::from_string_literal("Unexpected marker");
19701960
}
19711961

1972-
marker = TRY(read_marker_at_cursor(context.stream));
1962+
marker = TRY(read_until_marker(context.stream));
19731963
}
19741964
}
19751965

0 commit comments

Comments
 (0)