[beast: 2/4] BSE: add Bse::icon_sanitize() to ensure consistency of icon fields



commit fb127b8e727f9c003846160fa5e189c17c7010f7
Author: Tim Janik <timj gnu org>
Date:   Mon Feb 20 18:55:20 2017 +0100

    BSE: add Bse::icon_sanitize() to ensure consistency of icon fields
    
    Signed-off-by: Tim Janik <timj gnu org>

 bse/bseutils.cc |   21 +++++++++++++++++++++
 bse/bseutils.hh |    1 +
 2 files changed, 22 insertions(+), 0 deletions(-)
---
diff --git a/bse/bseutils.cc b/bse/bseutils.cc
index 07a818f..ad00356 100644
--- a/bse/bseutils.cc
+++ b/bse/bseutils.cc
@@ -683,6 +683,27 @@ icon_from_pixstream (const uint8 *pixstream)
   return icon;
 }
 
+static const int max_icon_dim = 1024;
+
+/// Ensure consistency of the @a icon fields.
+bool
+icon_sanitize (Icon *icon)
+{
+  if (icon->width == 0 && icon->height == 0 && icon->pixels.size() == 0)
+    return false;
+  if (icon->width > 0 && icon->width <= max_icon_dim &&
+      icon->height > 0 && icon->height <= max_icon_dim &&
+      icon->pixels.size() == size_t (icon->width) * icon->height)
+    return false;
+  // sanitize
+  icon->width = CLAMP (icon->width, 0, max_icon_dim);
+  icon->height = CLAMP (icon->height, 0, max_icon_dim);
+  if (icon->width == 0 || icon->height == 0)
+    icon->width = icon->height = 0;
+  icon->pixels.resize (icon->width * icon->height);
+  return true; // indicate modifications
+}
+
 } // Bse
 
 
diff --git a/bse/bseutils.hh b/bse/bseutils.hh
index 65255a6..ffb293e 100644
--- a/bse/bseutils.hh
+++ b/bse/bseutils.hh
@@ -85,6 +85,7 @@ gint          bse_string_equals               (gconstpointer   string1,
 namespace Bse {
 
 Icon icon_from_pixstream (const guint8 *pixstream);
+bool icon_sanitize (Icon *icon);
 
 } // Bse
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]