]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkrgba.c
x11: Store GDK name in xsettings hash table
[~andy/gtk] / gdk / gdkrgba.c
index f5e20c5f3aeb7d46b860c7d1d666de6054c382ee..1151c1245d54aea0bd781eafb7fe0814eaf8abcc 100644 (file)
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
 #include "config.h"
 #include "gdkrgba.h"
 #include <string.h>
+#include <errno.h>
+#include <math.h>
+
+#include "fallback-c89.c"
 
 /**
  * SECTION:rgba_colors
@@ -71,15 +73,7 @@ G_DEFINE_BOXED_TYPE (GdkRGBA, gdk_rgba,
 GdkRGBA *
 gdk_rgba_copy (const GdkRGBA *rgba)
 {
-  GdkRGBA *copy;
-
-  copy = g_slice_new (GdkRGBA);
-  copy->red = rgba->red;
-  copy->green = rgba->green;
-  copy->blue = rgba->blue;
-  copy->alpha = rgba->alpha;
-
-  return copy;
+  return g_slice_dup (GdkRGBA, rgba);
 }
 
 /**
@@ -109,14 +103,17 @@ gdk_rgba_free (GdkRGBA *rgba)
  *  - We accept mixed percentages and non-percentages in a single
  *    rgb() or rgba() specification.
  */
-static double
-parse_rgb_value (const char  *str,
-                 char       **endp)
+static gboolean
+parse_rgb_value (const gchar  *str,
+                 gchar       **endp,
+                 gdouble      *number)
 {
-  double number;
   const char *p;
 
-  number = g_ascii_strtod (str, endp);
+  *number = g_ascii_strtod (str, endp);
+  if (errno == ERANGE || *endp == str ||
+      isinf (*number) || isnan (*number))
+    return FALSE;
 
   p = *endp;
 
@@ -125,12 +122,14 @@ parse_rgb_value (const char  *str,
   if (*p == '%')
     {
       *endp = (char *)(p + 1);
-      return CLAMP(number / 100., 0., 1.);
+      *number = CLAMP(*number / 100., 0., 1.);
     }
   else
     {
-      return CLAMP(number / 255., 0., 1.);
+      *number = CLAMP(*number / 255., 0., 1.);
     }
+
+  return TRUE;
 }
 
 /**
@@ -149,7 +148,8 @@ parse_rgb_value (const char  *str,
  * A standard name (Taken from the X11 rgb.txt file).
  * </listitem>
  * <listitem>
- * A hex value in the form '#rgb' '#rrggbb' '#rrrgggbbb' or '#rrrrggggbbbb'
+ * A hex value in the form '&num;rgb' '&num;rrggbb' '&num;rrrgggbbb'
+ * or '&num;rrrrggggbbbb'
  * </listitem>
  * <listitem>
  * A RGB color in the form 'rgb(r,g,b)' (In this case the color will
@@ -176,6 +176,7 @@ gdk_rgba_parse (GdkRGBA     *rgba,
   gboolean has_alpha;
   gdouble r, g, b, a;
   gchar *str = (gchar *) spec;
+  gchar *p;
 
   if (strncmp (str, "rgba", 4) == 0)
     {
@@ -220,7 +221,8 @@ gdk_rgba_parse (GdkRGBA     *rgba,
 
   /* Parse red */
   SKIP_WHITESPACES (str);
-  r = parse_rgb_value (str, &str);
+  if (!parse_rgb_value (str, &str, &r))
+    return FALSE;
   SKIP_WHITESPACES (str);
 
   if (*str != ',')
@@ -230,7 +232,8 @@ gdk_rgba_parse (GdkRGBA     *rgba,
 
   /* Parse green */
   SKIP_WHITESPACES (str);
-  g = parse_rgb_value (str, &str);
+  if (!parse_rgb_value (str, &str, &g))
+    return FALSE;
   SKIP_WHITESPACES (str);
 
   if (*str != ',')
@@ -240,7 +243,8 @@ gdk_rgba_parse (GdkRGBA     *rgba,
 
   /* Parse blue */
   SKIP_WHITESPACES (str);
-  b = parse_rgb_value (str, &str);
+  if (!parse_rgb_value (str, &str, &b))
+    return FALSE;
   SKIP_WHITESPACES (str);
 
   if (has_alpha)
@@ -251,13 +255,24 @@ gdk_rgba_parse (GdkRGBA     *rgba,
       str++;
 
       SKIP_WHITESPACES (str);
-      a = g_ascii_strtod (str, &str);
+      a = g_ascii_strtod (str, &p);
+      if (errno == ERANGE || p == str ||
+          isinf (a) || isnan (a))
+        return FALSE;
+      str = p;
       SKIP_WHITESPACES (str);
     }
 
   if (*str != ')')
     return FALSE;
 
+  str++;
+
+  SKIP_WHITESPACES (str);
+
+  if (*str != '\0')
+    return FALSE;
+
   if (rgba)
     {
       rgba->red = CLAMP (r, 0, 1);