]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcname.c
Fix Gecko-exposed segfault from my last hack to FcObjectToPtrLookup.
[fontconfig.git] / src / fcname.c
index a21e99523ce1331d21a88b9db29a7f26aa364061..ed09597d94a5265a092f7f3b6d536393740a2cd0 100644 (file)
 #include "fcint.h"
 
 /* Please do not revoke any of these bindings. */
+/* The __DUMMY__ object enables callers to distinguish the error return
+ * of FcObjectToPtrLookup from FC_FAMILY's FcObjectPtr, which would
+ * otherwise be 0. */
 static const FcObjectType _FcBaseObjectTypes[] = {
-    { "__DUMMY__",      FcTypeVoid, },
+    { "__DUMMY__",      FcTypeVoid, }, 
     { FC_FAMILY,       FcTypeString, },
     { FC_FAMILYLANG,   FcTypeString, },
     { FC_STYLE,                FcTypeString, },
@@ -72,6 +75,7 @@ static const FcObjectType _FcBaseObjectTypes[] = {
     { FC_CAPABILITY,   FcTypeString },
     { FC_FONTFORMAT,   FcTypeString },
     { FC_EMBOLDEN,     FcTypeBool },
+    { FC_EMBEDDED_BITMAP,   FcTypeBool },
 };
 
 #define NUM_OBJECT_TYPES    (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
@@ -92,7 +96,8 @@ struct _FcObjectTypeList {
 static const FcObjectTypeList _FcBaseObjectTypesList = {
     0,
     _FcBaseObjectTypes,
-    NUM_OBJECT_TYPES
+    NUM_OBJECT_TYPES,
+    0
 };
 
 static const FcObjectTypeList  *_FcObjectTypes = &_FcBaseObjectTypesList;
@@ -153,6 +158,9 @@ FcNameGetObjectType (const char *object)
     
     for (l = _FcObjectTypes; l; l = l->next)
     {
+        if (l == (FcObjectTypeList*)_FcUserObjectNames)
+            continue;
+
        for (i = 0; i < l->ntypes; i++)
        {
            t = &l->types[i];
@@ -186,6 +194,7 @@ FcObjectToPtrLookup (const char * object)
     FcObjectPtr                    i = 0, n;
     const FcObjectTypeList  *l;
     FcObjectType           *t = _FcUserObjectNames;
+    FcBool                 replace;
 
     for (l = _FcObjectTypes; l; l = l->next)
     {
@@ -194,7 +203,7 @@ FcObjectToPtrLookup (const char * object)
            t = (FcObjectType *)&l->types[i];
            if (!strcmp (object, t->object))
            {
-               if (l == (FcObjectTypeList*)_FcUserObjectNames)
+               if (l->types == _FcUserObjectNames)
                     return -i;
                else
                    return l->basic_offset + i;
@@ -202,27 +211,28 @@ FcObjectToPtrLookup (const char * object)
        }
     }
 
-    /* We didn't match.  Look for the correct FcObjectTypeList
-     * to replace it in-place. */
+    /* We didn't match.  Look for the application's FcObjectTypeList
+     * and replace it in-place. */
     for (l = _FcObjectTypes; l; l = l->next)
     {
        if (l->types == _FcUserObjectNames)
            break;
     }
 
+    replace = l && l->types == _FcUserObjectNames;
     if (!_FcUserObjectNames || 
-        (l && l->types == _FcUserObjectNames && user_obj_alloc < l->ntypes))
+        (replace && user_obj_alloc <= l->ntypes))
     {
        int nt = user_obj_alloc + 4;
-        FcObjectType * t = realloc (_FcUserObjectNames, 
+        FcObjectType * tt = realloc (_FcUserObjectNames, 
                                    nt * sizeof (FcObjectType));
-        if (!t)
+        if (!tt)
             return 0;
-       _FcUserObjectNames = t;
+       _FcUserObjectNames = tt;
        user_obj_alloc = nt;
     }
 
-    if (l && l->types == _FcUserObjectNames)
+    if (replace)
     {
        n = l->ntypes;
        FcNameUnregisterObjectTypesFree (l->types, l->ntypes, FcFalse);
@@ -232,20 +242,11 @@ FcObjectToPtrLookup (const char * object)
 
     FcNameRegisterObjectTypes (_FcUserObjectNames, n+1);
 
-    for (l = _FcObjectTypes; l; l = l->next)
-    {
-       if (l->types == _FcUserObjectNames)
-       {
-           t = (FcObjectType *)l->types;
-           break;
-       }
-    }
-
-    if (!t)
+    if (!_FcUserObjectNames)
        return 0;
 
-    t[n].object = object;
-    t[n].type = FcTypeVoid;
+    _FcUserObjectNames[n].object = object;
+    _FcUserObjectNames[n].type = FcTypeVoid;
 
     return -n;
 }
@@ -263,8 +264,9 @@ FcObjectToPtr (const char * name)
         if (b->hash == hash && !strcmp (name, (char *) (b + 1)))
             return b->id;
     size = sizeof (struct objectBucket) + strlen (name) + 1;
-    b = malloc (size);
-    FcMemAlloc (FC_MEM_STATICSTR, size);
+    /* workaround glibc bug which reads strlen in groups of 4 */
+    b = malloc (size + sizeof (int));
+    FcMemAlloc (FC_MEM_STATICSTR, size + sizeof(int));
     if (!b)
         return 0;
     b->next = 0;
@@ -330,18 +332,26 @@ FcObjectNeededBytes ()
     return num + sizeof(int);
 }
 
+int
+FcObjectNeededBytesAlign (void)
+{
+    return fc_alignof (int) + fc_alignof (char);
+}
+
 void *
 FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
 {
+    block_ptr = ALIGN (block_ptr, int);
     *(int *)block_ptr = biggest_known_ntypes;
     block_ptr = (int *) block_ptr + 1;
+    block_ptr = ALIGN (block_ptr, char);
     biggest_ptr = block_ptr;
     block_ptr = (char *) block_ptr + biggest_known_count;
     return block_ptr;
 }
 
 void
-FcObjectSerialize ()
+FcObjectSerialize (void)
 {
     int i;
     for (i = 0; i < biggest_known_ntypes; i++)
@@ -353,9 +363,10 @@ FcObjectSerialize ()
 }
 
 void *
-FcObjectUnserialize (FcCache metadata, void *block_ptr)
+FcObjectUnserialize (FcCache metadata, void *block_ptr)
 {
     int new_biggest;
+    block_ptr = ALIGN (block_ptr, int);
     new_biggest = *(int *)block_ptr;
     block_ptr = (int *) block_ptr + 1;
     if (biggest_known_ntypes < new_biggest)
@@ -363,19 +374,11 @@ FcObjectUnserialize (FcCache metadata, void *block_ptr)
        int i;
        char * bp = (char *)block_ptr;
        FcObjectType * bn;
-       FcObjectTypeList * bnl;
 
        bn = malloc (sizeof (const FcObjectType) * (new_biggest + 1));
        if (!bn)
            return 0;
 
-       bnl = malloc (sizeof (FcObjectTypeList));
-       if (!bnl)
-       {
-           free (bn);
-           return 0;
-       }
-
        for (i = 0; i < new_biggest; i++)
        {
            const FcObjectType * t = FcNameGetObjectType(bp);
@@ -399,16 +402,11 @@ FcObjectUnserialize (FcCache metadata, void *block_ptr)
        biggest_known_ntypes = new_biggest;
        biggest_known_types = (const FcObjectType *)bn;
     }
+    block_ptr = ALIGN (block_ptr, char);
     block_ptr = (char *) block_ptr + biggest_known_count;
     return block_ptr;
 }
 
-int
-FcObjectPtrCompare (const FcObjectPtr a, const FcObjectPtr b)
-{
-    return a - b;
-}
-
 static const FcConstant _FcBaseConstants[] = {
     { (FcChar8 *) "thin",          "weight",   FC_WEIGHT_THIN, },
     { (FcChar8 *) "extralight",            "weight",   FC_WEIGHT_EXTRALIGHT, },
@@ -693,7 +691,7 @@ FcNameParse (const FcChar8 *name)
                for (;;)
                {
                    name = FcNameFindNext (name, ":,", save, &delim);
-                   if (t)
+                   if (t && strcmp (t->object, _FcBaseObjectTypes[0].object))
                    {
                        v = FcNameConvert (t->type, save, &m);
                        if (!FcPatternAdd (pat, t->object, v, FcTrue))
@@ -767,7 +765,6 @@ FcNameUnparseString (FcStrBuf           *buf,
 
 static FcBool
 FcNameUnparseValue (FcStrBuf   *buf,
-                   int         bank,
                    FcValue     *v0,
                    FcChar8     *escape)
 {
@@ -808,7 +805,7 @@ FcNameUnparseValueList (FcStrBuf    *buf,
 {
     while (FcValueListPtrU(v))
     {
-       if (!FcNameUnparseValue (buf, v.bank, &FcValueListPtrU(v)->value, escape))
+       if (!FcNameUnparseValue (buf, &FcValueListPtrU(v)->value, escape))
            return FcFalse;
        if (FcValueListPtrU(v = FcValueListPtrU(v)->next))
            if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
@@ -822,6 +819,12 @@ FcNameUnparseValueList (FcStrBuf   *buf,
 
 FcChar8 *
 FcNameUnparse (FcPattern *pat)
+{
+    return FcNameUnparseEscaped (pat, FcTrue);
+}
+
+FcChar8 *
+FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
 {
     FcStrBuf               buf;
     FcChar8                buf_static[8192];
@@ -834,7 +837,7 @@ FcNameUnparse (FcPattern *pat)
     e = FcPatternFindElt (pat, FC_FAMILY);
     if (e)
     {
-       if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
+        if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
            goto bail0;
     }
     e = FcPatternFindElt (pat, FC_SIZE);
@@ -842,7 +845,7 @@ FcNameUnparse (FcPattern *pat)
     {
        if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
            goto bail0;
-       if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
+       if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
            goto bail0;
     }
     for (l = _FcObjectTypes; l; l = l->next)
@@ -860,12 +863,12 @@ FcNameUnparse (FcPattern *pat)
            {
                if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
                    goto bail0;
-               if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
+               if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
                    goto bail0;
                if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
                    goto bail0;
-               if (!FcNameUnparseValueList (&buf, e->values, 
-                                            (FcChar8 *) FC_ESCAPE_VARIABLE))
+               if (!FcNameUnparseValueList (&buf, e->values, escape ? 
+                                            (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
                    goto bail0;
            }
        }