Added checks and try-catch when attempting to load icons

Fixes #437
This commit is contained in:
Guillaume Meunier
2025-08-09 10:12:58 +02:00
parent b036731b51
commit d5ef8c1a85
2 changed files with 299 additions and 304 deletions
+287 -299
View File
@@ -126,10 +126,7 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
}; };
if (!rsvg_handle_render_document(handle.get(), cr.get(), &viewport, &error)) if (!rsvg_handle_render_document(handle.get(), cr.get(), &viewport, &error))
{ throw std::runtime_error{std::string{"Could not render: "} + error->message};
g_printerr("could not render: %s", error->message);
return {};
}
// Write a PNG file // Write a PNG file
std::vector<std::byte> png; std::vector<std::byte> png;
@@ -143,11 +140,8 @@ std::vector<std::byte> load_svg(const std::filesystem::path & filename, int size
return CAIRO_STATUS_SUCCESS; return CAIRO_STATUS_SUCCESS;
}; };
if (cairo_surface_write_to_png_stream(surface.get(), cb, &png) != CAIRO_STATUS_SUCCESS) if (auto status = cairo_surface_write_to_png_stream(surface.get(), cb, &png); status != CAIRO_STATUS_SUCCESS)
{ throw std::runtime_error{std::string{"Could not write output: "} + cairo_status_to_string(status)};
g_printerr("could not write output file");
return {};
}
return png; return png;
} }
@@ -201,352 +195,346 @@ std::vector<std::byte> load_ico(const std::filesystem::path & filename, int size
{ {
const uint32_t ICO_PNG_MAGIC = 0x474e5089; const uint32_t ICO_PNG_MAGIC = 0x474e5089;
try auto ico = load_file(filename);
std::span header{ico};
auto reserved = read<uint16_t>(header);
auto resource_type = read<uint16_t>(header);
auto icon_count = read<uint16_t>(header);
if (reserved != 0 or resource_type != 1)
throw std::runtime_error{"Cannot read icon: " + filename.native()};
std::vector<ico_file_entry> entries;
int max_bpp = 0;
for (int i = 0; i < icon_count; i++)
{ {
auto ico = load_file(filename); ico_file_entry entry;
entry.width = read<uint8_t>(header);
entry.height = read<uint8_t>(header);
auto num_colors = read<uint8_t>(header);
auto reserved = read<uint8_t>(header); // reserved
auto planes = read<uint16_t>(header);
entry.bpp = read<uint16_t>(header);
auto size = read<uint32_t>(header);
auto offset = read<uint32_t>(header);
std::span header{ico}; if (offset + size > ico.size())
throw std::runtime_error{"Cannot read icon: " + filename.native()};
auto reserved = read<uint16_t>(header); entry.data = std::span{ico}.subspan(offset, size);
auto resource_type = read<uint16_t>(header); if (entry.width == 0)
auto icon_count = read<uint16_t>(header); entry.width = 256;
if (entry.height == 0)
entry.height = 256;
if (reserved != 0)
throw std::runtime_error{"Cannot read icon: " + filename.native()};
if (planes != 0 and planes != 1)
throw std::runtime_error{"Cannot read icon: " + filename.native()};
if (reserved != 0 or resource_type != 1) entries.push_back(entry);
return {};
std::vector<ico_file_entry> entries; max_bpp = std::max(max_bpp, entry.bpp);
int max_bpp = 0; }
for (int i = 0; i < icon_count; i++) const ico_file_entry * best_entry = index >= 0 ? &entries.at(index) : [&]() {
const ico_file_entry * best_entry = nullptr;
// Look for the smallest multiple of the requested size
int criterion = std::numeric_limits<int>::max();
for (const auto & entry: entries)
{ {
ico_file_entry entry; if (entry.bpp != max_bpp)
entry.width = read<uint8_t>(header); continue;
entry.height = read<uint8_t>(header);
auto num_colors = read<uint8_t>(header);
auto reserved = read<uint8_t>(header); // reserved
auto planes = read<uint16_t>(header);
entry.bpp = read<uint16_t>(header);
auto size = read<uint32_t>(header);
auto offset = read<uint32_t>(header);
if (offset + size > ico.size()) if (entry.width % size != 0 or entry.height % size != 0)
return {}; continue;
entry.data = std::span{ico}.subspan(offset, size); if (std::min(entry.width, entry.height) / size < criterion)
if (entry.width == 0) {
entry.width = 256; criterion = std::min(entry.width, entry.height);
if (entry.height == 0) best_entry = &entry;
entry.height = 256; }
if (reserved != 0)
return {};
if (planes != 0 and planes != 1)
return {};
entries.push_back(entry);
max_bpp = std::max(max_bpp, entry.bpp);
} }
if (best_entry)
const ico_file_entry * best_entry = index >= 0 ? &entries.at(index) : [&]() {
const ico_file_entry * best_entry = nullptr;
// Look for the smallest multiple of the requested size
int criterion = std::numeric_limits<int>::max();
for (const auto & entry: entries)
{
if (entry.bpp != max_bpp)
continue;
if (entry.width % size != 0 or entry.height % size != 0)
continue;
if (std::min(entry.width, entry.height) / size < criterion)
{
criterion = std::min(entry.width, entry.height);
best_entry = &entry;
}
}
if (best_entry)
return best_entry;
// No multiple found: pick the largest icon
criterion = 0;
for (const auto & entry: entries)
{
if (entry.bpp != max_bpp)
continue;
if (entry.width * entry.height > criterion)
{
criterion = entry.width * entry.height;
best_entry = &entry;
}
}
return best_entry; return best_entry;
}();
if (not best_entry) // No multiple found: pick the largest icon
criterion = 0;
for (const auto & entry: entries)
{
if (entry.bpp != max_bpp)
continue;
if (entry.width * entry.height > criterion)
{
criterion = entry.width * entry.height;
best_entry = &entry;
}
}
return best_entry;
}();
if (not best_entry)
return {};
auto data = best_entry->data;
auto magic = read<uint32_t>(data);
if (magic == ICO_PNG_MAGIC)
{
// PNG file
std::vector<std::byte> png_data;
// The PNG magic has already been consumed, put it back
png_data.insert(png_data.end(), data.begin() - 4, data.end());
// Check if the PNG is valid
std::vector<uint8_t> pixels;
png_image png_ref{
.version = PNG_IMAGE_VERSION,
.format = PNG_FORMAT_BGRA,
};
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
throw std::runtime_error{"Cannot read icon: " + filename.native()};
pixels.resize(PNG_IMAGE_SIZE(png_ref));
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
{
png_image_free(&png_ref);
throw std::runtime_error{"Cannot read icon: " + filename.native()};
}
png_image_free(&png_ref);
return png_data;
}
else if (magic == 40)
{
auto width = read<uint32_t>(data); // Width of image in pixels
auto height = read<uint32_t>(data); // Height of image in pixels
auto planes = read<uint16_t>(data); // Must be 1
auto bpp = read<uint16_t>(data); // 1, 4, 8, 16, 24, 32
auto compression = read<uint32_t>(data); // Must be 0 for icons
auto image_size = read<uint32_t>(data); // Size of image (without this header)
auto x_res = read<uint32_t>(data);
auto y_res = read<uint32_t>(data);
auto used_colors = read<uint32_t>(data);
auto important_colors = read<uint32_t>(data);
if (planes != 1 or compression != 0)
return {}; return {};
auto data = best_entry->data; if (bpp != 1 and
auto magic = read<uint32_t>(data); bpp != 4 and
bpp != 8 and
bpp != 16 and
bpp != 24 and
bpp != 32)
return {};
if (magic == ICO_PNG_MAGIC) auto w = width;
auto h = height / 2;
std::span<uint8_t> palette; // BGRX
if (bpp <= 16)
{ {
// PNG file // Load the palette
std::vector<std::byte> png_data; if (used_colors == 0)
used_colors = 1 << bpp;
// The PNG magic has already been consumed, put it back palette = read<uint8_t>(data, used_colors * 4);
png_data.insert(png_data.end(), data.begin() - 4, data.end());
// Check if the PNG is valid
std::vector<uint8_t> pixels;
png_image png_ref{
.version = PNG_IMAGE_VERSION,
.format = PNG_FORMAT_BGRA,
};
if (not png_image_begin_read_from_memory(&png_ref, png_data.data(), png_data.size()))
return {};
pixels.resize(PNG_IMAGE_SIZE(png_ref));
if (!png_image_finish_read(&png_ref, nullptr, pixels.data(), 0, nullptr))
{
png_image_free(&png_ref);
return {};
}
png_image_free(&png_ref);
return png_data;
} }
else if (magic == 40)
size_t xor_stride = rowstride(w, bpp);
size_t and_stride = rowstride(w, 1);
size_t dst_stride = 4 * w;
std::span<uint8_t> xor_map = read<uint8_t>(data, xor_stride * h);
std::span<uint8_t> and_map = read<uint8_t>(data, and_stride * h);
std::vector<uint8_t> dest_buffer;
dest_buffer.resize(h * dst_stride);
switch (bpp)
{ {
auto width = read<uint32_t>(data); // Width of image in pixels case 1:
auto height = read<uint32_t>(data); // Height of image in pixels for (int y = 0; y < h; y++)
auto planes = read<uint16_t>(data); // Must be 1 {
auto bpp = read<uint16_t>(data); // 1, 4, 8, 16, 24, 32 uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
auto compression = read<uint32_t>(data); // Must be 0 for icons uint8_t * src_and_row = and_map.data() + and_stride * y;
auto image_size = read<uint32_t>(data); // Size of image (without this header) uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
auto x_res = read<uint32_t>(data);
auto y_res = read<uint32_t>(data);
auto used_colors = read<uint32_t>(data);
auto important_colors = read<uint32_t>(data);
if (planes != 1 or compression != 0) for (int x = 0, i = 0; x < w; x++)
return {};
if (bpp != 1 and
bpp != 4 and
bpp != 8 and
bpp != 16 and
bpp != 24 and
bpp != 32)
return {};
auto w = width;
auto h = height / 2;
std::span<uint8_t> palette; // BGRX
if (bpp <= 16)
{
// Load the palette
if (used_colors == 0)
used_colors = 1 << bpp;
palette = read<uint8_t>(data, used_colors * 4);
}
size_t xor_stride = rowstride(w, bpp);
size_t and_stride = rowstride(w, 1);
size_t dst_stride = 4 * w;
std::span<uint8_t> xor_map = read<uint8_t>(data, xor_stride * h);
std::span<uint8_t> and_map = read<uint8_t>(data, and_stride * h);
std::vector<uint8_t> dest_buffer;
dest_buffer.resize(h * dst_stride);
switch (bpp)
{
case 1:
for (int y = 0; y < h; y++)
{ {
uint8_t * src_xor_row = xor_map.data() + xor_stride * y; int colour = (src_xor_row[x / 8] >> (7 - (x % 8))) & 0x1;
uint8_t * src_and_row = and_map.data() + and_stride * y; if (colour >= used_colors)
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); return {};
for (int x = 0, i = 0; x < w; x++) uint8_t b = palette[4 * colour + 0]; // Blue
{ uint8_t g = palette[4 * colour + 1]; // Green
int colour = (src_xor_row[x / 8] >> (7 - (x % 8))) & 0x1; uint8_t r = palette[4 * colour + 2]; // Red
if (colour >= used_colors)
return {};
uint8_t b = palette[4 * colour + 0]; // Blue // Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
uint8_t g = palette[4 * colour + 1]; // Green uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
uint8_t r = palette[4 * colour + 2]; // Red
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0) *dst_row++ = b;
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff; *dst_row++ = g;
*dst_row++ = r;
*dst_row++ = b; *dst_row++ = a;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
} }
break; }
break;
case 4: case 4:
for (int y = 0; y < h; y++) for (int y = 0; y < h; y++)
{
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
uint8_t * src_and_row = and_map.data() + and_stride * y;
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
for (int x = 0, i = 0; x < w; x++)
{ {
uint8_t * src_xor_row = xor_map.data() + xor_stride * y; int colour = (src_xor_row[x / 2] >> (4 - 4 * (x % 2))) & 0xf;
uint8_t * src_and_row = and_map.data() + and_stride * y; if (colour >= used_colors)
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); return {};
for (int x = 0, i = 0; x < w; x++) uint8_t b = palette[4 * colour + 0]; // Blue
{ uint8_t g = palette[4 * colour + 1]; // Green
int colour = (src_xor_row[x / 2] >> (4 - 4 * (x % 2))) & 0xf; uint8_t r = palette[4 * colour + 2]; // Red
if (colour >= used_colors)
return {};
uint8_t b = palette[4 * colour + 0]; // Blue // Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
uint8_t g = palette[4 * colour + 1]; // Green uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
uint8_t r = palette[4 * colour + 2]; // Red
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0) *dst_row++ = b;
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff; *dst_row++ = g;
*dst_row++ = r;
*dst_row++ = b; *dst_row++ = a;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
} }
break; }
break;
case 8: case 8:
for (int y = 0; y < h; y++) for (int y = 0; y < h; y++)
{
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
uint8_t * src_and_row = and_map.data() + and_stride * y;
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
for (int x = 0, i = 0; x < w; x++)
{ {
uint8_t * src_xor_row = xor_map.data() + xor_stride * y; int colour = *src_xor_row++;
uint8_t * src_and_row = and_map.data() + and_stride * y; if (colour >= used_colors)
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); return {};
for (int x = 0, i = 0; x < w; x++) uint8_t b = palette[4 * colour + 0]; // Blue
{ uint8_t g = palette[4 * colour + 1]; // Green
int colour = *src_xor_row++; uint8_t r = palette[4 * colour + 2]; // Red
if (colour >= used_colors)
return {};
uint8_t b = palette[4 * colour + 0]; // Blue // Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
uint8_t g = palette[4 * colour + 1]; // Green uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
uint8_t r = palette[4 * colour + 2]; // Red
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0) *dst_row++ = b;
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff; *dst_row++ = g;
*dst_row++ = r;
*dst_row++ = b; *dst_row++ = a;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
} }
break; }
break;
case 16: case 16:
for (int y = 0; y < h; y++) for (int y = 0; y < h; y++)
{
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
uint8_t * src_and_row = and_map.data() + and_stride * y;
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
for (int x = 0, i = 0; x < w; x++)
{ {
uint8_t * src_xor_row = xor_map.data() + xor_stride * y; int colour = (src_xor_row[1] << 8) | src_xor_row[0];
uint8_t * src_and_row = and_map.data() + and_stride * y; src_xor_row += 2;
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
for (int x = 0, i = 0; x < w; x++) if (colour >= used_colors)
{ return {};
int colour = (src_xor_row[1] << 8) | src_xor_row[0];
src_xor_row += 2;
if (colour >= used_colors) uint8_t b = palette[4 * colour + 0]; // Blue
return {}; uint8_t g = palette[4 * colour + 1]; // Green
uint8_t r = palette[4 * colour + 2]; // Red
uint8_t b = palette[4 * colour + 0]; // Blue // Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
uint8_t g = palette[4 * colour + 1]; // Green uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
uint8_t r = palette[4 * colour + 2]; // Red
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0) *dst_row++ = b;
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff; *dst_row++ = g;
*dst_row++ = r;
*dst_row++ = b; *dst_row++ = a;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
} }
break; }
break;
case 24: case 24:
for (int y = 0; y < h; y++) for (int y = 0; y < h; y++)
{
uint8_t * src_xor_row = xor_map.data() + xor_stride * y;
uint8_t * src_and_row = and_map.data() + and_stride * y;
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
for (int x = 0, i = 0; x < w; x++)
{ {
uint8_t * src_xor_row = xor_map.data() + xor_stride * y; uint8_t b = *src_xor_row++; // Blue
uint8_t * src_and_row = and_map.data() + and_stride * y; uint8_t g = *src_xor_row++; // Green
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); uint8_t r = *src_xor_row++; // Red
for (int x = 0, i = 0; x < w; x++) // Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0)
{ uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff;
uint8_t b = *src_xor_row++; // Blue
uint8_t g = *src_xor_row++; // Green
uint8_t r = *src_xor_row++; // Red
// Get alpha from AND mask (0: opaque => 0xff, 1: transparent => 0) *dst_row++ = b;
uint8_t a = src_and_row[x / 8] & (1 << (7 - x % 8)) ? 0 : 0xff; *dst_row++ = g;
*dst_row++ = r;
*dst_row++ = b; *dst_row++ = a;
*dst_row++ = g;
*dst_row++ = r;
*dst_row++ = a;
}
} }
break; }
break;
case 32: case 32:
for (int y = 0; y < h; y++) for (int y = 0; y < h; y++)
{ {
uint8_t * src_xor_row = (uint8_t *)xor_map.data() + xor_stride * y; uint8_t * src_xor_row = (uint8_t *)xor_map.data() + xor_stride * y;
uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y);
memcpy(dst_row, src_xor_row, dst_stride); memcpy(dst_row, src_xor_row, dst_stride);
} }
break; break;
} }
png_image png{ png_image png{
.version = PNG_IMAGE_VERSION, .version = PNG_IMAGE_VERSION,
.width = w, .width = w,
.height = h, .height = h,
.format = PNG_FORMAT_BGRA, .format = PNG_FORMAT_BGRA,
}; };
std::vector<std::byte> png_data{65536}; std::vector<std::byte> png_data{65536};
png_alloc_size_t png_size = png_data.size(); png_alloc_size_t png_size = png_data.size();
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
if (png_size > png_data.size())
{
png_data.resize(png_size);
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr); png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
if (png_size > png_data.size())
{
png_data.resize(png_size);
png_image_write_to_memory(&png, png_data.data(), &png_size, false /* convert_to_8_bit*/, dest_buffer.data(), w * 4, nullptr);
}
else
png_data.resize(png_size);
return png_data;
} }
} else
catch (std::exception & e) png_data.resize(png_size);
{
return png_data;
} }
return {}; throw std::runtime_error{"Wrong magic number in " + filename.native()};
} }
std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache; std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
@@ -556,7 +544,7 @@ std::unordered_map<std::filesystem::path, std::vector<std::byte>> icon_cache;
const std::vector<std::byte> & wivrn::load_icon(const std::filesystem::path & filename, int size) const std::vector<std::byte> & wivrn::load_icon(const std::filesystem::path & filename, int size)
{ {
auto it = icon_cache.find(filename); auto it = icon_cache.find(filename);
if (it != icon_cache.end()) if (it != icon_cache.end() and not it->second.empty())
return it->second; return it->second;
std::vector<std::byte> icon; std::vector<std::byte> icon;
+12 -5
View File
@@ -757,12 +757,19 @@ void wivrn_session::operator()(from_headset::get_application_list && request)
{ {
if (app.icon_path) if (app.icon_path)
{ {
auto icon = load_icon(*app.icon_path, 256); try
{
auto icon = load_icon(*app.icon_path, 256);
send_control(to_headset::application_icon{ send_control(to_headset::application_icon{
.id = id, .id = id,
.image = std::move(icon), .image = std::move(icon),
}); });
}
catch (std::exception & e)
{
U_LOG_W("Error loading icon %s: %s", app.icon_path->c_str(), e.what());
}
} }
} }
} }