LCOV - code coverage report
Current view: top level - girepository - gitypelib.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 64.6 % 881 569
Test Date: 2025-05-13 05:22:17 Functions: 96.5 % 57 55
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
       2                 :             :  * GObject introspection: typelib validation, auxiliary functions
       3                 :             :  * related to the binary typelib format
       4                 :             :  *
       5                 :             :  * Copyright (C) 2005 Matthias Clasen
       6                 :             :  *
       7                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       8                 :             :  *
       9                 :             :  * This library is free software; you can redistribute it and/or
      10                 :             :  * modify it under the terms of the GNU Lesser General Public
      11                 :             :  * License as published by the Free Software Foundation; either
      12                 :             :  * version 2 of the License, or (at your option) any later version.
      13                 :             :  *
      14                 :             :  * This library is distributed in the hope that it will be useful,
      15                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :             :  * Lesser General Public License for more details.
      18                 :             :  *
      19                 :             :  * You should have received a copy of the GNU Lesser General Public
      20                 :             :  * License along with this library; if not, write to the
      21                 :             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      22                 :             :  * Boston, MA 02111-1307, USA.
      23                 :             :  */
      24                 :             : 
      25                 :             : #include "config.h"
      26                 :             : 
      27                 :             : #include <stdlib.h>
      28                 :             : #include <string.h>
      29                 :             : 
      30                 :             : #include <glib.h>
      31                 :             : 
      32                 :             : #include "gitypelib-internal.h"
      33                 :             : #include "gitypelib.h"
      34                 :             : 
      35                 :             : /**
      36                 :             :  * GITypelib:
      37                 :             :  *
      38                 :             :  * `GITypelib` represents a loaded `.typelib` file, which contains a description
      39                 :             :  * of a single module’s API.
      40                 :             :  *
      41                 :             :  * Since: 2.80
      42                 :             :  */
      43                 :             : 
      44                 :           2 : G_DEFINE_BOXED_TYPE (GITypelib, gi_typelib, gi_typelib_ref, gi_typelib_unref)
      45                 :             : 
      46                 :             : typedef struct {
      47                 :             :   GITypelib *typelib;
      48                 :             :   GSList *context_stack;
      49                 :             : } ValidateContext;
      50                 :             : 
      51                 :             : #define ALIGN_VALUE(this, boundary) \
      52                 :             :   (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
      53                 :             : 
      54                 :             : static void
      55                 :       16292 : push_context (ValidateContext *ctx, const char *name)
      56                 :             : {
      57                 :       16292 :   ctx->context_stack = g_slist_prepend (ctx->context_stack, (char*)name);
      58                 :       16292 : }
      59                 :             : 
      60                 :             : static void
      61                 :       16292 : pop_context (ValidateContext *ctx)
      62                 :             : {
      63                 :       16292 :   g_assert (ctx->context_stack != NULL);
      64                 :       16292 :   ctx->context_stack = g_slist_delete_link (ctx->context_stack,
      65                 :             :                                             ctx->context_stack);
      66                 :       16292 : }
      67                 :             : 
      68                 :             : static gboolean
      69                 :             : validate_interface_blob (ValidateContext *ctx,
      70                 :             :                          uint32_t       offset,
      71                 :             :                          GError       **error);
      72                 :             : 
      73                 :             : static DirEntry *
      74                 :       18521 : get_dir_entry_checked (GITypelib *typelib,
      75                 :             :                        uint16_t   index,
      76                 :             :                        GError   **error)
      77                 :             : {
      78                 :       18521 :   Header *header = (Header *)typelib->data;
      79                 :             :   uint32_t offset;
      80                 :             : 
      81                 :       18521 :   if (index == 0 || index > header->n_entries)
      82                 :             :     {
      83                 :           0 :       g_set_error (error,
      84                 :             :                    GI_TYPELIB_ERROR,
      85                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
      86                 :             :                    "Invalid directory index %d", index);
      87                 :           0 :       return FALSE;
      88                 :             :     }
      89                 :             : 
      90                 :       18521 :   offset = header->directory + (index - 1u) * header->entry_blob_size;
      91                 :             : 
      92                 :       18521 :   if (typelib->len < offset + sizeof (DirEntry))
      93                 :             :     {
      94                 :           0 :       g_set_error (error,
      95                 :             :                    GI_TYPELIB_ERROR,
      96                 :             :                    GI_TYPELIB_ERROR_INVALID,
      97                 :             :                    "The buffer is too short");
      98                 :           0 :       return FALSE;
      99                 :             :     }
     100                 :             : 
     101                 :       18521 :   return (DirEntry *)&typelib->data[offset];
     102                 :             : }
     103                 :             : 
     104                 :             : 
     105                 :             : static CommonBlob *
     106                 :         658 : get_blob (GITypelib *typelib,
     107                 :             :           uint32_t   offset,
     108                 :             :           GError  **error)
     109                 :             : {
     110                 :         658 :   if (typelib->len < offset + sizeof (CommonBlob))
     111                 :             :     {
     112                 :           0 :       g_set_error (error,
     113                 :             :                    GI_TYPELIB_ERROR,
     114                 :             :                    GI_TYPELIB_ERROR_INVALID,
     115                 :             :                    "The buffer is too short");
     116                 :           0 :       return FALSE;
     117                 :             :     }
     118                 :         658 :   return (CommonBlob *)&typelib->data[offset];
     119                 :             : }
     120                 :             : 
     121                 :             : static InterfaceTypeBlob *
     122                 :         658 : get_type_blob (GITypelib *typelib,
     123                 :             :                SimpleTypeBlob *simple,
     124                 :             :                GError  **error)
     125                 :             : {
     126                 :         658 :   if (simple->offset == 0)
     127                 :             :     {
     128                 :           0 :       g_set_error (error,
     129                 :             :                    GI_TYPELIB_ERROR,
     130                 :             :                    GI_TYPELIB_ERROR_INVALID,
     131                 :             :                    "Expected blob for type");
     132                 :           0 :       return FALSE;
     133                 :             :     }
     134                 :             : 
     135                 :         658 :   if (simple->flags.reserved == 0 && simple->flags.reserved2 == 0)
     136                 :             :     {
     137                 :           0 :       g_set_error (error,
     138                 :             :                    GI_TYPELIB_ERROR,
     139                 :             :                    GI_TYPELIB_ERROR_INVALID,
     140                 :             :                    "Expected non-basic type but got %d",
     141                 :           0 :                    simple->flags.tag);
     142                 :           0 :       return FALSE;
     143                 :             :     }
     144                 :             : 
     145                 :         658 :   return (InterfaceTypeBlob*) get_blob (typelib, simple->offset, error);
     146                 :             : }
     147                 :             : 
     148                 :             : /**
     149                 :             :  * gi_typelib_get_dir_entry:
     150                 :             :  * @typelib: a #GITypelib
     151                 :             :  * @index: index to retrieve
     152                 :             :  *
     153                 :             :  * Get the typelib directory entry at the given @index.
     154                 :             :  *
     155                 :             :  * Returns: (transfer none): a `DirEntry`
     156                 :             :  * Since: 2.80
     157                 :             :  */
     158                 :             : DirEntry *
     159                 :       19178 : gi_typelib_get_dir_entry (GITypelib *typelib,
     160                 :             :                           uint16_t   index)
     161                 :             : {
     162                 :       19178 :   Header *header = (Header *)typelib->data;
     163                 :             : 
     164                 :             :   /* this deliberately doesn’t check for underflow of @index; see get_dir_entry_checked() for that */
     165                 :       19178 :   return (DirEntry *)&typelib->data[header->directory + (index - 1u) * header->entry_blob_size];
     166                 :             : }
     167                 :             : 
     168                 :             : static Section *
     169                 :          63 : get_section_by_id (GITypelib   *typelib,
     170                 :             :                    SectionType  section_type)
     171                 :             : {
     172                 :          63 :   Header *header = (Header *)typelib->data;
     173                 :             :   Section *section;
     174                 :             : 
     175                 :          63 :   if (header->sections == 0)
     176                 :           0 :     return NULL;
     177                 :             : 
     178                 :          63 :   for (section = (Section*)&typelib->data[header->sections];
     179                 :          63 :        section->id != GI_SECTION_END;
     180                 :           0 :        section++)
     181                 :             :     {
     182                 :          63 :       if (section->id == section_type)
     183                 :          63 :         return section;
     184                 :             :     }
     185                 :           0 :   return NULL;
     186                 :             : }
     187                 :             : 
     188                 :             : /**
     189                 :             :  * gi_typelib_get_dir_entry_by_name:
     190                 :             :  * @typelib: a #GITypelib
     191                 :             :  * @name: name to look up
     192                 :             :  *
     193                 :             :  * Get the typelib directory entry which has @name.
     194                 :             :  *
     195                 :             :  * Returns: (transfer none) (nullable): entry corresponding to @name, or `NULL`
     196                 :             :  *   if none was found
     197                 :             :  * Since: 2.80
     198                 :             :  */
     199                 :             : DirEntry *
     200                 :          63 : gi_typelib_get_dir_entry_by_name (GITypelib  *typelib,
     201                 :             :                                   const char *name)
     202                 :             : {
     203                 :             :   Section *dirindex;
     204                 :             :   size_t i, n_entries;
     205                 :             :   const char *entry_name;
     206                 :             :   DirEntry *entry;
     207                 :             : 
     208                 :          63 :   dirindex = get_section_by_id (typelib, GI_SECTION_DIRECTORY_INDEX);
     209                 :          63 :   n_entries = ((Header *)typelib->data)->n_local_entries;
     210                 :             : 
     211                 :          63 :   if (dirindex == NULL)
     212                 :             :     {
     213                 :           0 :       for (i = 1; i <= n_entries; i++)
     214                 :             :         {
     215                 :           0 :           entry = gi_typelib_get_dir_entry (typelib, i);
     216                 :           0 :           entry_name = gi_typelib_get_string (typelib, entry->name);
     217                 :           0 :           if (strcmp (name, entry_name) == 0)
     218                 :           0 :             return entry;
     219                 :             :         }
     220                 :           0 :       return NULL;
     221                 :             :     }
     222                 :             :   else
     223                 :             :     {
     224                 :          63 :       uint8_t *hash = (uint8_t *) &typelib->data[dirindex->offset];
     225                 :             :       uint16_t index;
     226                 :             : 
     227                 :          63 :       index = gi_typelib_hash_search (hash, name, n_entries);
     228                 :          63 :       entry = gi_typelib_get_dir_entry (typelib, index + 1);
     229                 :          63 :       entry_name = gi_typelib_get_string (typelib, entry->name);
     230                 :          63 :       if (strcmp (name, entry_name) == 0)
     231                 :          62 :         return entry;
     232                 :           1 :       return NULL;
     233                 :             :     }
     234                 :             : }
     235                 :             : 
     236                 :             : /**
     237                 :             :  * gi_typelib_get_dir_entry_by_gtype_name:
     238                 :             :  * @typelib: a #GITypelib
     239                 :             :  * @gtype_name: name of a [type@GObject.Type] to look up
     240                 :             :  *
     241                 :             :  * Get the typelib directory entry for the [type@GObject.Type] with the given
     242                 :             :  * @gtype_name.
     243                 :             :  *
     244                 :             :  * Returns: (transfer none) (nullable): entry corresponding to @gtype_name, or
     245                 :             :  *   `NULL` if none was found
     246                 :             :  * Since: 2.80
     247                 :             :  */
     248                 :             : DirEntry *
     249                 :          17 : gi_typelib_get_dir_entry_by_gtype_name (GITypelib   *typelib,
     250                 :             :                                         const char  *gtype_name)
     251                 :             : {
     252                 :          17 :   Header *header = (Header *)typelib->data;
     253                 :             : 
     254                 :        5682 :   for (size_t i = 1; i <= header->n_local_entries; i++)
     255                 :             :     {
     256                 :             :       RegisteredTypeBlob *blob;
     257                 :             :       const char *type;
     258                 :        5670 :       DirEntry *entry = gi_typelib_get_dir_entry (typelib, i);
     259                 :       11340 :       if (!BLOB_IS_REGISTERED_TYPE (entry))
     260                 :        3530 :         continue;
     261                 :             : 
     262                 :        2140 :       blob = (RegisteredTypeBlob *)(&typelib->data[entry->offset]);
     263                 :        2140 :       if (!blob->gtype_name)
     264                 :        1076 :         continue;
     265                 :             : 
     266                 :        1064 :       type = gi_typelib_get_string (typelib, blob->gtype_name);
     267                 :        1064 :       if (strcmp (type, gtype_name) == 0)
     268                 :           5 :         return entry;
     269                 :             :     }
     270                 :          12 :   return NULL;
     271                 :             : }
     272                 :             : 
     273                 :             : typedef struct {
     274                 :             :   const char *s;
     275                 :             :   const char *separator;
     276                 :             :   size_t sep_len;
     277                 :             :   GString buf;
     278                 :             : } StrSplitIter;
     279                 :             : 
     280                 :             : static void
     281                 :          12 : strsplit_iter_init (StrSplitIter  *iter,
     282                 :             :                     const char    *s,
     283                 :             :                     const char    *separator)
     284                 :             : {
     285                 :          12 :   iter->s = s;
     286                 :          12 :   iter->separator = separator;
     287                 :          12 :   iter->sep_len = strlen (separator);
     288                 :          12 :   iter->buf.str = NULL;
     289                 :          12 :   iter->buf.len = 0;
     290                 :          12 :   iter->buf.allocated_len = 0;
     291                 :          12 : }
     292                 :             : 
     293                 :             : static gboolean
     294                 :          15 : strsplit_iter_next (StrSplitIter  *iter,
     295                 :             :                     const char   **out_val)
     296                 :             : {
     297                 :          15 :   const char *s = iter->s;
     298                 :             :   const char *next;
     299                 :             :   size_t len;
     300                 :             : 
     301                 :          15 :   if (!s)
     302                 :           0 :     return FALSE;
     303                 :          15 :   next = strstr (s, iter->separator);
     304                 :          15 :   if (next)
     305                 :             :     {
     306                 :           3 :       iter->s = next + iter->sep_len;
     307                 :           3 :       len = (size_t) (next - s);
     308                 :             :     }
     309                 :             :   else
     310                 :             :     {
     311                 :          12 :       iter->s = NULL;
     312                 :          12 :       len = strlen (s);
     313                 :             :     }
     314                 :          15 :   if (len == 0)
     315                 :             :     {
     316                 :           0 :       *out_val = "";
     317                 :             :     }
     318                 :             :   else
     319                 :             :     {
     320                 :          15 :       g_string_overwrite_len (&iter->buf, 0, s, (gssize)len + 1);
     321                 :          15 :       iter->buf.str[len] = '\0';
     322                 :          15 :       *out_val = iter->buf.str;
     323                 :             :     }
     324                 :          15 :   return TRUE;
     325                 :             : }
     326                 :             : 
     327                 :             : static void
     328                 :          12 : strsplit_iter_clear (StrSplitIter  *iter)
     329                 :             : {
     330                 :          12 :   g_free (iter->buf.str);
     331                 :          12 : }
     332                 :             : 
     333                 :             : /**
     334                 :             :  * gi_typelib_matches_gtype_name_prefix:
     335                 :             :  * @typelib: a #GITypelib
     336                 :             :  * @gtype_name: name of a [type@GObject.Type]
     337                 :             :  *
     338                 :             :  * Check whether the symbol prefix for @typelib is a prefix of the given
     339                 :             :  * @gtype_name.
     340                 :             :  *
     341                 :             :  * Returns: `TRUE` if the prefix for @typelib prefixes @gtype_name
     342                 :             :  * Since: 2.80
     343                 :             :  */
     344                 :             : gboolean
     345                 :          12 : gi_typelib_matches_gtype_name_prefix (GITypelib   *typelib,
     346                 :             :                                       const char  *gtype_name)
     347                 :             : {
     348                 :          12 :   Header *header = (Header *)typelib->data;
     349                 :             :   const char *c_prefix;
     350                 :             :   const char *prefix;
     351                 :          12 :   gboolean ret = FALSE;
     352                 :             :   StrSplitIter split_iter;
     353                 :             :   size_t gtype_name_len;
     354                 :             : 
     355                 :          12 :   c_prefix = gi_typelib_get_string (typelib, header->c_prefix);
     356                 :          12 :   if (c_prefix == NULL || strlen (c_prefix) == 0)
     357                 :           0 :     return FALSE;
     358                 :             : 
     359                 :          12 :   gtype_name_len = strlen (gtype_name);
     360                 :             : 
     361                 :             :   /* c_prefix is a comma separated string of supported prefixes
     362                 :             :    * in the typelib.
     363                 :             :    * We match the specified gtype_name if the gtype_name starts
     364                 :             :    * with the prefix, and is followed by a capital letter.
     365                 :             :    * For example, a typelib offering the 'Gdk' prefix does match
     366                 :             :    * GdkX11Cursor, however a typelib offering the 'G' prefix does not.
     367                 :             :    */
     368                 :          12 :   strsplit_iter_init (&split_iter, c_prefix, ",");
     369                 :          15 :   while (strsplit_iter_next (&split_iter, &prefix))
     370                 :             :     {
     371                 :          15 :       size_t len = strlen (prefix);
     372                 :             : 
     373                 :          15 :       if (gtype_name_len < len)
     374                 :           0 :         continue;
     375                 :             : 
     376                 :          15 :       if (strncmp (prefix, gtype_name, len) != 0)
     377                 :           3 :         continue;
     378                 :             : 
     379                 :          12 :       if (g_ascii_isupper (gtype_name[len]))
     380                 :             :         {
     381                 :          12 :           ret = TRUE;
     382                 :          12 :           break;
     383                 :             :         }
     384                 :             :     }
     385                 :          12 :   strsplit_iter_clear (&split_iter);
     386                 :          12 :   return ret;
     387                 :             : }
     388                 :             : 
     389                 :             : /**
     390                 :             :  * gi_typelib_get_dir_entry_by_error_domain:
     391                 :             :  * @typelib: a #GITypelib
     392                 :             :  * @error_domain: name of a [type@GLib.Error] domain to look up
     393                 :             :  *
     394                 :             :  * Get the typelib directory entry for the [type@GLib.Error] domain with the
     395                 :             :  * given @error_domain name.
     396                 :             :  *
     397                 :             :  * Returns: (transfer none) (nullable): entry corresponding to @error_domain, or
     398                 :             :  *   `NULL` if none was found
     399                 :             :  * Since: 2.80
     400                 :             :  */
     401                 :             : DirEntry *
     402                 :          12 : gi_typelib_get_dir_entry_by_error_domain (GITypelib *typelib,
     403                 :             :                                           GQuark     error_domain)
     404                 :             : {
     405                 :          12 :   Header *header = (Header *)typelib->data;
     406                 :          12 :   size_t n_entries = header->n_local_entries;
     407                 :          12 :   const char *domain_string = g_quark_to_string (error_domain);
     408                 :             :   DirEntry *entry;
     409                 :             : 
     410                 :        4885 :   for (size_t i = 1; i <= n_entries; i++)
     411                 :             :     {
     412                 :             :       EnumBlob *blob;
     413                 :             :       const char *enum_domain_string;
     414                 :             : 
     415                 :        4874 :       entry = gi_typelib_get_dir_entry (typelib, i);
     416                 :        4874 :       if (entry->blob_type != BLOB_TYPE_ENUM)
     417                 :        4683 :         continue;
     418                 :             : 
     419                 :         191 :       blob = (EnumBlob *)(&typelib->data[entry->offset]);
     420                 :         191 :       if (!blob->error_domain)
     421                 :         146 :         continue;
     422                 :             : 
     423                 :          45 :       enum_domain_string = gi_typelib_get_string (typelib, blob->error_domain);
     424                 :          45 :       if (strcmp (domain_string, enum_domain_string) == 0)
     425                 :           1 :         return entry;
     426                 :             :     }
     427                 :          11 :   return NULL;
     428                 :             : }
     429                 :             : 
     430                 :             : /* When changing the size of a typelib structure, you are required to update
     431                 :             :  * the hardcoded size here.  Do NOT change these to use sizeof(); these
     432                 :             :  * should match whatever is defined in the text specification and serve as
     433                 :             :  * a sanity check on structure modifications.
     434                 :             :  *
     435                 :             :  * Everything else in the code however should be using sizeof().
     436                 :             :  */
     437                 :             : 
     438                 :             : G_STATIC_ASSERT (sizeof (Header) == 112);
     439                 :             : G_STATIC_ASSERT (sizeof (DirEntry) == 12);
     440                 :             : G_STATIC_ASSERT (sizeof (SimpleTypeBlob) == 4);
     441                 :             : G_STATIC_ASSERT (sizeof (ArgBlob) == 16);
     442                 :             : G_STATIC_ASSERT (sizeof (SignatureBlob) == 8);
     443                 :             : G_STATIC_ASSERT (sizeof (CommonBlob) == 8);
     444                 :             : G_STATIC_ASSERT (sizeof (FunctionBlob) == 20);
     445                 :             : G_STATIC_ASSERT (sizeof (CallbackBlob) == 12);
     446                 :             : G_STATIC_ASSERT (sizeof (InterfaceTypeBlob) == 4);
     447                 :             : G_STATIC_ASSERT (sizeof (ArrayTypeBlob) == 8);
     448                 :             : G_STATIC_ASSERT (sizeof (ParamTypeBlob) == 4);
     449                 :             : G_STATIC_ASSERT (sizeof (ErrorTypeBlob) == 4);
     450                 :             : G_STATIC_ASSERT (sizeof (ValueBlob) == 12);
     451                 :             : G_STATIC_ASSERT (sizeof (FieldBlob) == 16);
     452                 :             : G_STATIC_ASSERT (sizeof (RegisteredTypeBlob) == 16);
     453                 :             : G_STATIC_ASSERT (sizeof (StructBlob) == 32);
     454                 :             : G_STATIC_ASSERT (sizeof (EnumBlob) == 24);
     455                 :             : G_STATIC_ASSERT (sizeof (PropertyBlob) == 16);
     456                 :             : G_STATIC_ASSERT (sizeof (SignalBlob) == 16);
     457                 :             : G_STATIC_ASSERT (sizeof (VFuncBlob) == 20);
     458                 :             : G_STATIC_ASSERT (sizeof (ObjectBlob) == 60);
     459                 :             : G_STATIC_ASSERT (sizeof (InterfaceBlob) == 40);
     460                 :             : G_STATIC_ASSERT (sizeof (ConstantBlob) == 24);
     461                 :             : G_STATIC_ASSERT (sizeof (AttributeBlob) == 12);
     462                 :             : G_STATIC_ASSERT (sizeof (UnionBlob) == 40);
     463                 :             : 
     464                 :             : static gboolean
     465                 :        7754 : is_aligned (uint32_t offset)
     466                 :             : {
     467                 :        7754 :   return offset == ALIGN_VALUE (offset, 4);
     468                 :             : }
     469                 :             : 
     470                 :             : #define MAX_NAME_LEN 2048
     471                 :             : 
     472                 :             : static const char *
     473                 :       92015 : get_string (GITypelib *typelib, uint32_t offset, GError **error)
     474                 :             : {
     475                 :       92015 :   if (typelib->len < offset)
     476                 :             :     {
     477                 :           0 :       g_set_error (error,
     478                 :             :                    GI_TYPELIB_ERROR,
     479                 :             :                    GI_TYPELIB_ERROR_INVALID,
     480                 :             :                    "Buffer is too short while looking up name");
     481                 :           0 :       return NULL;
     482                 :             :     }
     483                 :             : 
     484                 :       92015 :   return (const char*)&typelib->data[offset];
     485                 :             : }
     486                 :             : 
     487                 :             : static const char *
     488                 :       16292 : get_string_nofail (GITypelib *typelib, uint32_t offset)
     489                 :             : {
     490                 :       16292 :   const char *ret = get_string (typelib, offset, NULL);
     491                 :       16292 :   g_assert (ret);
     492                 :       16292 :   return ret;
     493                 :             : }
     494                 :             : 
     495                 :             : static gboolean
     496                 :       75723 : validate_name (GITypelib   *typelib,
     497                 :             :                const char *msg,
     498                 :             :                const uint8_t *data,
     499                 :             :                uint32_t offset,
     500                 :             :                GError **error)
     501                 :             : {
     502                 :             :   const char *name;
     503                 :             : 
     504                 :       75723 :   name = get_string (typelib, offset, error);
     505                 :       75723 :   if (!name)
     506                 :           0 :     return FALSE;
     507                 :             : 
     508                 :       75723 :   if (!memchr (name, '\0', MAX_NAME_LEN))
     509                 :             :     {
     510                 :           0 :       g_set_error (error,
     511                 :             :                    GI_TYPELIB_ERROR,
     512                 :             :                    GI_TYPELIB_ERROR_INVALID,
     513                 :             :                    "The %s is too long: %s",
     514                 :             :                    msg, name);
     515                 :           0 :       return FALSE;
     516                 :             :     }
     517                 :             : 
     518                 :       75723 :   if (strspn (name, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_") < strlen (name))
     519                 :             :     {
     520                 :           0 :       g_set_error (error,
     521                 :             :                    GI_TYPELIB_ERROR,
     522                 :             :                    GI_TYPELIB_ERROR_INVALID,
     523                 :             :                    "The %s contains invalid characters: '%s'",
     524                 :             :                    msg, name);
     525                 :           0 :       return FALSE;
     526                 :             :     }
     527                 :             : 
     528                 :       75723 :   return TRUE;
     529                 :             : }
     530                 :             : 
     531                 :             : /* Fast path sanity check, operates on a memory blob */
     532                 :             : static gboolean
     533                 :         253 : validate_header_basic (const uint8_t  *memory,
     534                 :             :                        size_t          len,
     535                 :             :                        GError        **error)
     536                 :             : {
     537                 :         253 :   Header *header = (Header *)memory;
     538                 :             : 
     539                 :         253 :   if (len < sizeof (Header))
     540                 :             :     {
     541                 :           0 :       g_set_error (error,
     542                 :             :                    GI_TYPELIB_ERROR,
     543                 :             :                    GI_TYPELIB_ERROR_INVALID,
     544                 :             :                    "The specified typelib length %zu is too short", len);
     545                 :           0 :       return FALSE;
     546                 :             :     }
     547                 :             : 
     548                 :         253 :   if (strncmp (header->magic, GI_IR_MAGIC, 16) != 0)
     549                 :             :     {
     550                 :           0 :       g_set_error (error,
     551                 :             :                    GI_TYPELIB_ERROR,
     552                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     553                 :             :                    "Invalid magic header");
     554                 :           0 :       return FALSE;
     555                 :             : 
     556                 :             :     }
     557                 :             : 
     558                 :         253 :   if (header->major_version != 4)
     559                 :             :     {
     560                 :           0 :       g_set_error (error,
     561                 :             :                    GI_TYPELIB_ERROR,
     562                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     563                 :             :                    "Typelib version mismatch; expected 4, found %d",
     564                 :           0 :                    header->major_version);
     565                 :           0 :       return FALSE;
     566                 :             : 
     567                 :             :     }
     568                 :             : 
     569                 :         253 :   if (header->n_entries < header->n_local_entries)
     570                 :             :     {
     571                 :           0 :       g_set_error (error,
     572                 :             :                    GI_TYPELIB_ERROR,
     573                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     574                 :             :                    "Inconsistent entry counts");
     575                 :           0 :       return FALSE;
     576                 :             :     }
     577                 :             : 
     578                 :         253 :   if (header->size != len)
     579                 :             :     {
     580                 :           0 :       g_set_error (error,
     581                 :             :                    GI_TYPELIB_ERROR,
     582                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     583                 :             :                    "Typelib size %zu does not match %zu",
     584                 :           0 :                    (size_t) header->size, len);
     585                 :           0 :       return FALSE;
     586                 :             :     }
     587                 :             : 
     588                 :             :   /* This is a sanity check for a specific typelib; it
     589                 :             :    * prevents us from loading an incompatible typelib.
     590                 :             :    *
     591                 :             :    * The hardcoded static checks to protect against inadvertent
     592                 :             :    * or buggy changes to the typelib format itself.
     593                 :             :    */
     594                 :             : 
     595                 :         253 :   if (header->entry_blob_size != sizeof (DirEntry) ||
     596                 :         253 :       header->function_blob_size != sizeof (FunctionBlob) ||
     597                 :         253 :       header->callback_blob_size != sizeof (CallbackBlob) ||
     598                 :         253 :       header->signal_blob_size != sizeof (SignalBlob) ||
     599                 :         253 :       header->vfunc_blob_size != sizeof (VFuncBlob) ||
     600                 :         253 :       header->arg_blob_size != sizeof (ArgBlob) ||
     601                 :         253 :       header->property_blob_size != sizeof (PropertyBlob) ||
     602                 :         253 :       header->field_blob_size != sizeof (FieldBlob) ||
     603                 :         253 :       header->value_blob_size != sizeof (ValueBlob) ||
     604                 :         253 :       header->constant_blob_size != sizeof (ConstantBlob) ||
     605                 :         253 :       header->attribute_blob_size != sizeof (AttributeBlob) ||
     606                 :         253 :       header->signature_blob_size != sizeof (SignatureBlob) ||
     607                 :         253 :       header->enum_blob_size != sizeof (EnumBlob) ||
     608                 :         253 :       header->struct_blob_size != sizeof (StructBlob) ||
     609                 :         253 :       header->object_blob_size != sizeof(ObjectBlob) ||
     610                 :         253 :       header->interface_blob_size != sizeof (InterfaceBlob) ||
     611                 :         253 :       header->union_blob_size != sizeof (UnionBlob))
     612                 :             :     {
     613                 :           0 :       g_set_error (error,
     614                 :             :                    GI_TYPELIB_ERROR,
     615                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     616                 :             :                    "Blob size mismatch");
     617                 :           0 :       return FALSE;
     618                 :             :     }
     619                 :             : 
     620                 :         253 :   if (!is_aligned (header->directory))
     621                 :             :     {
     622                 :           0 :       g_set_error (error,
     623                 :             :                    GI_TYPELIB_ERROR,
     624                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     625                 :             :                    "Misaligned directory");
     626                 :           0 :       return FALSE;
     627                 :             :     }
     628                 :             : 
     629                 :         253 :   if (!is_aligned (header->attributes))
     630                 :             :     {
     631                 :           0 :       g_set_error (error,
     632                 :             :                    GI_TYPELIB_ERROR,
     633                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     634                 :             :                    "Misaligned attributes");
     635                 :           0 :       return FALSE;
     636                 :             :     }
     637                 :             : 
     638                 :         253 :   if (header->attributes == 0 && header->n_attributes > 0)
     639                 :             :     {
     640                 :           0 :       g_set_error (error,
     641                 :             :                    GI_TYPELIB_ERROR,
     642                 :             :                    GI_TYPELIB_ERROR_INVALID_HEADER,
     643                 :             :                    "Wrong number of attributes");
     644                 :           0 :       return FALSE;
     645                 :             :     }
     646                 :             : 
     647                 :         253 :   return TRUE;
     648                 :             : }
     649                 :             : 
     650                 :             : static gboolean
     651                 :          13 : validate_header (ValidateContext  *ctx,
     652                 :             :                  GError          **error)
     653                 :             : {
     654                 :          13 :   GITypelib *typelib = ctx->typelib;
     655                 :             :   
     656                 :          13 :   if (!validate_header_basic (typelib->data, typelib->len, error))
     657                 :           0 :     return FALSE;
     658                 :             : 
     659                 :             :   {
     660                 :          13 :     Header *header = (Header*)typelib->data;
     661                 :          13 :     if (!validate_name (typelib, "namespace", typelib->data, header->namespace, error))
     662                 :           0 :       return FALSE;
     663                 :             :   }
     664                 :             : 
     665                 :          13 :   return TRUE;
     666                 :             : }
     667                 :             : 
     668                 :             : static gboolean validate_type_blob (GITypelib     *typelib,
     669                 :             :                                     uint32_t       offset,
     670                 :             :                                     uint32_t       signature_offset,
     671                 :             :                                     gboolean       return_type,
     672                 :             :                                     GError       **error);
     673                 :             : 
     674                 :             : static gboolean
     675                 :        1574 : validate_array_type_blob (GITypelib     *typelib,
     676                 :             :                           uint32_t       offset,
     677                 :             :                           uint32_t       signature_offset,
     678                 :             :                           gboolean       return_type,
     679                 :             :                           GError       **error)
     680                 :             : {
     681                 :             :   /* FIXME validate length */
     682                 :             : 
     683                 :        1574 :   if (!validate_type_blob (typelib,
     684                 :             :                            offset + G_STRUCT_OFFSET (ArrayTypeBlob, type),
     685                 :             :                            0, FALSE, error))
     686                 :           0 :     return FALSE;
     687                 :             : 
     688                 :        1574 :   return TRUE;
     689                 :             : }
     690                 :             : 
     691                 :             : static gboolean
     692                 :       17584 : validate_iface_type_blob (GITypelib     *typelib,
     693                 :             :                           uint32_t       offset,
     694                 :             :                           uint32_t       signature_offset,
     695                 :             :                           gboolean       return_type,
     696                 :             :                           GError       **error)
     697                 :             : {
     698                 :             :   InterfaceTypeBlob *blob;
     699                 :             :   InterfaceBlob *target;
     700                 :             : 
     701                 :       17584 :   blob = (InterfaceTypeBlob*)&typelib->data[offset];
     702                 :             : 
     703                 :       17584 :   target = (InterfaceBlob*) get_dir_entry_checked (typelib, blob->interface, error);
     704                 :             : 
     705                 :       17584 :   if (!target)
     706                 :           0 :     return FALSE;
     707                 :       17584 :   if (target->blob_type == 0) /* non-local */
     708                 :        1924 :     return TRUE;
     709                 :             : 
     710                 :       15660 :   return TRUE;
     711                 :             : }
     712                 :             : 
     713                 :             : static gboolean
     714                 :         480 : validate_param_type_blob (GITypelib     *typelib,
     715                 :             :                           uint32_t       offset,
     716                 :             :                           uint32_t       signature_offset,
     717                 :             :                           gboolean       return_type,
     718                 :             :                           size_t         n_params,
     719                 :             :                           GError       **error)
     720                 :             : {
     721                 :             :   ParamTypeBlob *blob;
     722                 :             : 
     723                 :         480 :   blob = (ParamTypeBlob*)&typelib->data[offset];
     724                 :             : 
     725                 :         480 :   if (!blob->pointer)
     726                 :             :     {
     727                 :           0 :       g_set_error (error,
     728                 :             :                    GI_TYPELIB_ERROR,
     729                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
     730                 :           0 :                    "Pointer type exected for tag %d", blob->tag);
     731                 :           0 :       return FALSE;
     732                 :             :     }
     733                 :             : 
     734                 :         480 :   if (blob->n_types != n_params)
     735                 :             :     {
     736                 :           0 :       g_set_error (error,
     737                 :             :                    GI_TYPELIB_ERROR,
     738                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
     739                 :             :                    "Parameter type number mismatch");
     740                 :           0 :       return FALSE;
     741                 :             :     }
     742                 :             : 
     743                 :        1119 :   for (size_t i = 0; i < n_params; i++)
     744                 :             :     {
     745                 :         639 :       if (!validate_type_blob (typelib,
     746                 :         639 :                                offset + sizeof (ParamTypeBlob) +
     747                 :             :                                i * sizeof (SimpleTypeBlob),
     748                 :             :                                0, FALSE, error))
     749                 :           0 :         return FALSE;
     750                 :             :     }
     751                 :             : 
     752                 :         480 :   return TRUE;
     753                 :             : }
     754                 :             : 
     755                 :             : static gboolean
     756                 :          99 : validate_error_type_blob (GITypelib     *typelib,
     757                 :             :                           uint32_t       offset,
     758                 :             :                           uint32_t       signature_offset,
     759                 :             :                           gboolean       return_type,
     760                 :             :                           GError       **error)
     761                 :             : {
     762                 :             :   ErrorTypeBlob *blob;
     763                 :             : 
     764                 :          99 :   blob = (ErrorTypeBlob*)&typelib->data[offset];
     765                 :             : 
     766                 :          99 :   if (!blob->pointer)
     767                 :             :     {
     768                 :           0 :       g_set_error (error,
     769                 :             :                    GI_TYPELIB_ERROR,
     770                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
     771                 :           0 :                    "Pointer type exected for tag %d", blob->tag);
     772                 :           0 :       return FALSE;
     773                 :             :     }
     774                 :             : 
     775                 :          99 :   return TRUE;
     776                 :             : }
     777                 :             : 
     778                 :             : static gboolean
     779                 :       44405 : validate_type_blob (GITypelib     *typelib,
     780                 :             :                     uint32_t       offset,
     781                 :             :                     uint32_t       signature_offset,
     782                 :             :                     gboolean       return_type,
     783                 :             :                     GError       **error)
     784                 :             : {
     785                 :             :   SimpleTypeBlob *simple;
     786                 :             :   InterfaceTypeBlob *iface;
     787                 :             : 
     788                 :       44405 :   simple = (SimpleTypeBlob *)&typelib->data[offset];
     789                 :             : 
     790                 :       44405 :   if (simple->flags.reserved == 0 &&
     791                 :       24888 :       simple->flags.reserved2 == 0)
     792                 :             :     {
     793                 :       24668 :       if (!GI_TYPE_TAG_IS_BASIC(simple->flags.tag))
     794                 :             :         {
     795                 :           0 :           g_set_error (error,
     796                 :             :                        GI_TYPELIB_ERROR,
     797                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
     798                 :           0 :                        "Invalid non-basic tag %d in simple type", simple->flags.tag);
     799                 :           0 :           return FALSE;
     800                 :             :         }
     801                 :             : 
     802                 :       24668 :       if (simple->flags.tag >= GI_TYPE_TAG_UTF8 &&
     803                 :        8303 :           simple->flags.tag != GI_TYPE_TAG_UNICHAR &&
     804                 :        8123 :           !simple->flags.pointer)
     805                 :             :         {
     806                 :           0 :           g_set_error (error,
     807                 :             :                        GI_TYPELIB_ERROR,
     808                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
     809                 :           0 :                        "Pointer type exected for tag %d", simple->flags.tag);
     810                 :           0 :           return FALSE;
     811                 :             :         }
     812                 :             : 
     813                 :       24668 :       return TRUE;
     814                 :             :     }
     815                 :             : 
     816                 :       19737 :   iface = (InterfaceTypeBlob*)&typelib->data[simple->offset];
     817                 :             : 
     818                 :       19737 :   switch (iface->tag)
     819                 :             :     {
     820                 :        1574 :     case GI_TYPE_TAG_ARRAY:
     821                 :        1574 :       if (!validate_array_type_blob (typelib, simple->offset,
     822                 :             :                                      signature_offset, return_type, error))
     823                 :           0 :         return FALSE;
     824                 :        1574 :       break;
     825                 :       17584 :     case GI_TYPE_TAG_INTERFACE:
     826                 :       17584 :       if (!validate_iface_type_blob (typelib, simple->offset,
     827                 :             :                                      signature_offset, return_type, error))
     828                 :           0 :         return FALSE;
     829                 :       17584 :       break;
     830                 :         321 :     case GI_TYPE_TAG_GLIST:
     831                 :             :     case GI_TYPE_TAG_GSLIST:
     832                 :         321 :       if (!validate_param_type_blob (typelib, simple->offset,
     833                 :             :                                      signature_offset, return_type, 1, error))
     834                 :           0 :         return FALSE;
     835                 :         321 :       break;
     836                 :         159 :     case GI_TYPE_TAG_GHASH:
     837                 :         159 :       if (!validate_param_type_blob (typelib, simple->offset,
     838                 :             :                                      signature_offset, return_type, 2, error))
     839                 :           0 :         return FALSE;
     840                 :         159 :       break;
     841                 :          99 :     case GI_TYPE_TAG_ERROR:
     842                 :          99 :       if (!validate_error_type_blob (typelib, simple->offset,
     843                 :             :                                      signature_offset, return_type, error))
     844                 :           0 :         return FALSE;
     845                 :          99 :       break;
     846                 :           0 :     default:
     847                 :           0 :       g_set_error (error,
     848                 :             :                    GI_TYPELIB_ERROR,
     849                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
     850                 :             :                    "Wrong tag in complex type");
     851                 :           0 :       return FALSE;
     852                 :             :     }
     853                 :             : 
     854                 :       19737 :   return TRUE;
     855                 :             : }
     856                 :             : 
     857                 :             : static gboolean
     858                 :       26400 : validate_arg_blob (GITypelib     *typelib,
     859                 :             :                    uint32_t       offset,
     860                 :             :                    uint32_t       signature_offset,
     861                 :             :                    GError       **error)
     862                 :             : {
     863                 :             :   ArgBlob *blob;
     864                 :             : 
     865                 :       26400 :   if (typelib->len < offset + sizeof (ArgBlob))
     866                 :             :     {
     867                 :           0 :       g_set_error (error,
     868                 :             :                    GI_TYPELIB_ERROR,
     869                 :             :                    GI_TYPELIB_ERROR_INVALID,
     870                 :             :                    "The buffer is too short");
     871                 :           0 :       return FALSE;
     872                 :             :     }
     873                 :             : 
     874                 :       26400 :   blob = (ArgBlob*) &typelib->data[offset];
     875                 :             : 
     876                 :       26400 :   if (!validate_name (typelib, "argument", typelib->data, blob->name, error))
     877                 :           0 :     return FALSE;
     878                 :             : 
     879                 :       26400 :   if (!validate_type_blob (typelib,
     880                 :             :                            offset + G_STRUCT_OFFSET (ArgBlob, arg_type),
     881                 :             :                            signature_offset, FALSE, error))
     882                 :           0 :     return FALSE;
     883                 :             : 
     884                 :       26400 :   return TRUE;
     885                 :             : }
     886                 :             : 
     887                 :             : static SimpleTypeBlob *
     888                 :         658 : return_type_from_signature (GITypelib *typelib,
     889                 :             :                             uint32_t   offset,
     890                 :             :                             GError  **error)
     891                 :             : {
     892                 :             :   SignatureBlob *blob;
     893                 :         658 :   if (typelib->len < offset + sizeof (SignatureBlob))
     894                 :             :     {
     895                 :           0 :       g_set_error (error,
     896                 :             :                    GI_TYPELIB_ERROR,
     897                 :             :                    GI_TYPELIB_ERROR_INVALID,
     898                 :             :                    "The buffer is too short");
     899                 :           0 :       return NULL;
     900                 :             :     }
     901                 :             : 
     902                 :         658 :   blob = (SignatureBlob*) &typelib->data[offset];
     903                 :         658 :   if (blob->return_type.offset == 0)
     904                 :             :     {
     905                 :           0 :       g_set_error (error,
     906                 :             :                    GI_TYPELIB_ERROR,
     907                 :             :                    GI_TYPELIB_ERROR_INVALID,
     908                 :             :                    "No return type found in signature");
     909                 :           0 :       return NULL;
     910                 :             :     }
     911                 :             : 
     912                 :         658 :   return (SimpleTypeBlob *)&typelib->data[offset + G_STRUCT_OFFSET (SignatureBlob, return_type)];
     913                 :             : }
     914                 :             : 
     915                 :             : static gboolean
     916                 :       16026 : validate_signature_blob (GITypelib     *typelib,
     917                 :             :                          uint32_t       offset,
     918                 :             :                          GError       **error)
     919                 :             : {
     920                 :             :   SignatureBlob *blob;
     921                 :             : 
     922                 :       16026 :   if (typelib->len < offset + sizeof (SignatureBlob))
     923                 :             :     {
     924                 :           0 :       g_set_error (error,
     925                 :             :                    GI_TYPELIB_ERROR,
     926                 :             :                    GI_TYPELIB_ERROR_INVALID,
     927                 :             :                    "The buffer is too short");
     928                 :           0 :       return FALSE;
     929                 :             :     }
     930                 :             : 
     931                 :       16026 :   blob = (SignatureBlob*) &typelib->data[offset];
     932                 :             : 
     933                 :       16026 :   if (blob->return_type.offset != 0)
     934                 :             :     {
     935                 :       11125 :       if (!validate_type_blob (typelib,
     936                 :             :                                offset + G_STRUCT_OFFSET (SignatureBlob, return_type),
     937                 :             :                                offset, TRUE, error))
     938                 :           0 :         return FALSE;
     939                 :             :     }
     940                 :             : 
     941                 :       42426 :   for (size_t i = 0; i < blob->n_arguments; i++)
     942                 :             :     {
     943                 :       26400 :       if (!validate_arg_blob (typelib,
     944                 :       26400 :                               offset + sizeof (SignatureBlob) +
     945                 :             :                               i * sizeof (ArgBlob),
     946                 :             :                               offset,
     947                 :             :                               error))
     948                 :           0 :         return FALSE;
     949                 :             :     }
     950                 :             : 
     951                 :             :   /* FIXME check constraints on return_value */
     952                 :             :   /* FIXME check array-length pairs */
     953                 :       16026 :   return TRUE;
     954                 :             : }
     955                 :             : 
     956                 :             : static gboolean
     957                 :       11933 : validate_function_blob (ValidateContext *ctx,
     958                 :             :                         uint32_t       offset,
     959                 :             :                         uint16_t       container_type,
     960                 :             :                         GError       **error)
     961                 :             : {
     962                 :       11933 :   GITypelib *typelib = ctx->typelib;
     963                 :             :   FunctionBlob *blob;
     964                 :             : 
     965                 :       11933 :   if (typelib->len < offset + sizeof (FunctionBlob))
     966                 :             :     {
     967                 :           0 :       g_set_error (error,
     968                 :             :                    GI_TYPELIB_ERROR,
     969                 :             :                    GI_TYPELIB_ERROR_INVALID,
     970                 :             :                    "The buffer is too short");
     971                 :           0 :       return FALSE;
     972                 :             :     }
     973                 :             : 
     974                 :       11933 :   blob = (FunctionBlob*) &typelib->data[offset];
     975                 :             : 
     976                 :       11933 :   if (blob->blob_type != BLOB_TYPE_FUNCTION)
     977                 :             :     {
     978                 :           0 :       g_set_error (error,
     979                 :             :                    GI_TYPELIB_ERROR,
     980                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
     981                 :           0 :                    "Wrong blob type %d, expected function", blob->blob_type);
     982                 :           0 :       return FALSE;
     983                 :             :     }
     984                 :             : 
     985                 :       11933 :   if (!validate_name (typelib, "function", typelib->data, blob->name, error))
     986                 :           0 :     return FALSE;
     987                 :             : 
     988                 :       11933 :   push_context (ctx, get_string_nofail (typelib, blob->name));
     989                 :             : 
     990                 :       11933 :   if (!validate_name (typelib, "function symbol", typelib->data, blob->symbol, error))
     991                 :           0 :     return FALSE;
     992                 :             : 
     993                 :       11933 :   if (blob->constructor)
     994                 :             :     {
     995                 :         658 :       switch (container_type)
     996                 :             :         {
     997                 :         658 :         case BLOB_TYPE_BOXED:
     998                 :             :         case BLOB_TYPE_STRUCT:
     999                 :             :         case BLOB_TYPE_UNION:
    1000                 :             :         case BLOB_TYPE_OBJECT:
    1001                 :             :         case BLOB_TYPE_INTERFACE:
    1002                 :         658 :           break;
    1003                 :           0 :         default:
    1004                 :           0 :           g_set_error (error,
    1005                 :             :                        GI_TYPELIB_ERROR,
    1006                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1007                 :             :                        "Constructor not allowed");
    1008                 :           0 :           return FALSE;
    1009                 :             :         }
    1010                 :             :     }
    1011                 :             : 
    1012                 :       11933 :   if (blob->setter || blob->getter || blob->wraps_vfunc)
    1013                 :             :     {
    1014                 :         780 :       switch (container_type)
    1015                 :             :         {
    1016                 :         780 :         case BLOB_TYPE_OBJECT:
    1017                 :             :         case BLOB_TYPE_INTERFACE:
    1018                 :         780 :           break;
    1019                 :           0 :         default:
    1020                 :           0 :           g_set_error (error,
    1021                 :             :                        GI_TYPELIB_ERROR,
    1022                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1023                 :             :                        "Setter, getter or wrapper not allowed");
    1024                 :           0 :           return FALSE;
    1025                 :             :         }
    1026                 :             :     }
    1027                 :             : 
    1028                 :       11933 :   if (blob->index)
    1029                 :             :     {
    1030                 :         579 :       if (!(blob->setter || blob->getter || blob->wraps_vfunc))
    1031                 :             :         {
    1032                 :           0 :           g_set_error (error,
    1033                 :             :                        GI_TYPELIB_ERROR,
    1034                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1035                 :             :                        "Must be setter, getter or wrapper");
    1036                 :           0 :           return FALSE;
    1037                 :             :         }
    1038                 :             :     }
    1039                 :             : 
    1040                 :             :   /* FIXME: validate index range */
    1041                 :             : 
    1042                 :       11933 :   if (!validate_signature_blob (typelib, blob->signature, error))
    1043                 :           0 :     return FALSE;
    1044                 :             : 
    1045                 :       11933 :   if (blob->constructor)
    1046                 :             :     {
    1047                 :         658 :       SimpleTypeBlob *simple = return_type_from_signature (typelib,
    1048                 :             :                                                            blob->signature,
    1049                 :             :                                                            error);
    1050                 :             :       InterfaceTypeBlob *iface_type;
    1051                 :             : 
    1052                 :         658 :       if (!simple)
    1053                 :           0 :         return FALSE;
    1054                 :         658 :       iface_type = get_type_blob (typelib, simple, error);
    1055                 :         658 :       if (!iface_type)
    1056                 :           0 :         return FALSE;
    1057                 :         658 :       if (iface_type->tag != GI_TYPE_TAG_INTERFACE &&
    1058                 :           3 :           (container_type == BLOB_TYPE_OBJECT ||
    1059                 :             :            container_type == BLOB_TYPE_INTERFACE))
    1060                 :             :         {
    1061                 :           0 :           g_set_error (error,
    1062                 :             :                        GI_TYPELIB_ERROR,
    1063                 :             :                        GI_TYPELIB_ERROR_INVALID,
    1064                 :             :                        "Invalid return type '%s' for constructor '%s'",
    1065                 :           0 :                        gi_type_tag_to_string (iface_type->tag),
    1066                 :             :                        get_string_nofail (typelib, blob->symbol));
    1067                 :           0 :           return FALSE;
    1068                 :             :         }
    1069                 :             :     }
    1070                 :             : 
    1071                 :       11933 :   pop_context (ctx);
    1072                 :             : 
    1073                 :       11933 :   return TRUE;
    1074                 :             : }
    1075                 :             : 
    1076                 :             : static gboolean
    1077                 :        2183 : validate_callback_blob (ValidateContext *ctx,
    1078                 :             :                         uint32_t       offset,
    1079                 :             :                         GError       **error)
    1080                 :             : {
    1081                 :        2183 :   GITypelib *typelib = ctx->typelib;
    1082                 :             :   CallbackBlob *blob;
    1083                 :             : 
    1084                 :        2183 :   if (typelib->len < offset + sizeof (CallbackBlob))
    1085                 :             :     {
    1086                 :           0 :       g_set_error (error,
    1087                 :             :                    GI_TYPELIB_ERROR,
    1088                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1089                 :             :                    "The buffer is too short");
    1090                 :           0 :       return FALSE;
    1091                 :             :     }
    1092                 :             : 
    1093                 :        2183 :   blob = (CallbackBlob*) &typelib->data[offset];
    1094                 :             : 
    1095                 :        2183 :   if (blob->blob_type != BLOB_TYPE_CALLBACK)
    1096                 :             :     {
    1097                 :           0 :       g_set_error (error,
    1098                 :             :                    GI_TYPELIB_ERROR,
    1099                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1100                 :             :                    "Wrong blob type");
    1101                 :           0 :       return FALSE;
    1102                 :             :     }
    1103                 :             : 
    1104                 :        2183 :   if (!validate_name (typelib, "callback", typelib->data, blob->name, error))
    1105                 :           0 :     return FALSE;
    1106                 :             : 
    1107                 :        2183 :   push_context (ctx, get_string_nofail (typelib, blob->name));
    1108                 :             : 
    1109                 :        2183 :   if (!validate_signature_blob (typelib, blob->signature, error))
    1110                 :           0 :     return FALSE;
    1111                 :             : 
    1112                 :        2183 :   pop_context (ctx);
    1113                 :             : 
    1114                 :        2183 :   return TRUE;
    1115                 :             : }
    1116                 :             : 
    1117                 :             : static gboolean
    1118                 :         867 : validate_constant_blob (GITypelib     *typelib,
    1119                 :             :                         uint32_t       offset,
    1120                 :             :                         GError       **error)
    1121                 :             : {
    1122                 :         867 :   size_t value_size[] = {
    1123                 :             :     0, /* VOID */
    1124                 :             :     4, /* BOOLEAN */
    1125                 :             :     1, /* INT8 */
    1126                 :             :     1, /* UINT8 */
    1127                 :             :     2, /* INT16 */
    1128                 :             :     2, /* UINT16 */
    1129                 :             :     4, /* INT32 */
    1130                 :             :     4, /* UINT32 */
    1131                 :             :     8, /* INT64 */
    1132                 :             :     8, /* UINT64 */
    1133                 :             :     sizeof (float),
    1134                 :             :     sizeof (double),
    1135                 :             :     0, /* GTYPE */
    1136                 :             :     0, /* UTF8 */
    1137                 :             :     0, /* FILENAME */
    1138                 :             :     0, /* ARRAY */
    1139                 :             :     0, /* INTERFACE */
    1140                 :             :     0, /* GLIST */
    1141                 :             :     0, /* GSLIST */
    1142                 :             :     0, /* GHASH */
    1143                 :             :     0, /* ERROR */
    1144                 :             :     4 /* UNICHAR */
    1145                 :             :   };
    1146                 :             :   ConstantBlob *blob;
    1147                 :             :   SimpleTypeBlob *type;
    1148                 :             : 
    1149                 :             :   g_assert (G_N_ELEMENTS (value_size) == GI_TYPE_TAG_N_TYPES);
    1150                 :             : 
    1151                 :         867 :   if (typelib->len < offset + sizeof (ConstantBlob))
    1152                 :             :     {
    1153                 :           0 :       g_set_error (error,
    1154                 :             :                    GI_TYPELIB_ERROR,
    1155                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1156                 :             :                    "The buffer is too short");
    1157                 :           0 :       return FALSE;
    1158                 :             :     }
    1159                 :             : 
    1160                 :         867 :   blob = (ConstantBlob*) &typelib->data[offset];
    1161                 :             : 
    1162                 :         867 :   if (blob->blob_type != BLOB_TYPE_CONSTANT)
    1163                 :             :     {
    1164                 :           0 :       g_set_error (error,
    1165                 :             :                    GI_TYPELIB_ERROR,
    1166                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1167                 :             :                    "Wrong blob type");
    1168                 :           0 :       return FALSE;
    1169                 :             :     }
    1170                 :             : 
    1171                 :         867 :   if (!validate_name (typelib, "constant", typelib->data, blob->name, error))
    1172                 :           0 :     return FALSE;
    1173                 :             : 
    1174                 :         867 :   if (!validate_type_blob (typelib, offset + G_STRUCT_OFFSET (ConstantBlob, type),
    1175                 :             :                            0, FALSE, error))
    1176                 :           0 :     return FALSE;
    1177                 :             : 
    1178                 :         867 :   if (!is_aligned (blob->offset))
    1179                 :             :     {
    1180                 :           0 :       g_set_error (error,
    1181                 :             :                    GI_TYPELIB_ERROR,
    1182                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1183                 :             :                    "Misaligned constant value");
    1184                 :           0 :       return FALSE;
    1185                 :             :     }
    1186                 :             : 
    1187                 :         867 :   type = (SimpleTypeBlob *)&typelib->data[offset + G_STRUCT_OFFSET (ConstantBlob, type)];
    1188                 :         867 :   if (type->flags.reserved == 0 && type->flags.reserved2 == 0)
    1189                 :             :     {
    1190                 :         867 :       if (type->flags.tag == 0)
    1191                 :             :         {
    1192                 :           0 :           g_set_error (error,
    1193                 :             :                        GI_TYPELIB_ERROR,
    1194                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1195                 :             :                        "Constant value type void");
    1196                 :           0 :           return FALSE;
    1197                 :             :         }
    1198                 :             : 
    1199                 :         867 :       if (value_size[type->flags.tag] != 0 &&
    1200                 :         311 :           blob->size != value_size[type->flags.tag])
    1201                 :             :         {
    1202                 :           0 :           g_set_error (error,
    1203                 :             :                        GI_TYPELIB_ERROR,
    1204                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1205                 :             :                        "Constant value size mismatch");
    1206                 :           0 :           return FALSE;
    1207                 :             :         }
    1208                 :             :       /* FIXME check string values */
    1209                 :             :     }
    1210                 :             : 
    1211                 :         867 :   return TRUE;
    1212                 :             : }
    1213                 :             : 
    1214                 :             : static gboolean
    1215                 :        3769 : validate_value_blob (GITypelib     *typelib,
    1216                 :             :                      uint32_t       offset,
    1217                 :             :                      GError       **error)
    1218                 :             : {
    1219                 :             :   ValueBlob *blob;
    1220                 :             : 
    1221                 :        3769 :   if (typelib->len < offset + sizeof (ValueBlob))
    1222                 :             :     {
    1223                 :           0 :       g_set_error (error,
    1224                 :             :                    GI_TYPELIB_ERROR,
    1225                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1226                 :             :                    "The buffer is too short");
    1227                 :           0 :       return FALSE;
    1228                 :             :     }
    1229                 :             : 
    1230                 :        3769 :   blob = (ValueBlob*) &typelib->data[offset];
    1231                 :             : 
    1232                 :        3769 :   if (!validate_name (typelib, "value", typelib->data, blob->name, error))
    1233                 :           0 :     return FALSE;
    1234                 :             : 
    1235                 :        3769 :   return TRUE;
    1236                 :             : }
    1237                 :             : 
    1238                 :             : static gboolean
    1239                 :        4746 : validate_field_blob (ValidateContext *ctx,
    1240                 :             :                      uint32_t       offset,
    1241                 :             :                      GError       **error)
    1242                 :             : {
    1243                 :        4746 :   GITypelib *typelib = ctx->typelib;
    1244                 :        4746 :   Header *header = (Header *)typelib->data;
    1245                 :             :   FieldBlob *blob;
    1246                 :             : 
    1247                 :        4746 :   if (typelib->len < offset + sizeof (FieldBlob))
    1248                 :             :     {
    1249                 :           0 :       g_set_error (error,
    1250                 :             :                    GI_TYPELIB_ERROR,
    1251                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1252                 :             :                    "The buffer is too short");
    1253                 :           0 :       return FALSE;
    1254                 :             :     }
    1255                 :             : 
    1256                 :        4746 :   blob = (FieldBlob*) &typelib->data[offset];
    1257                 :             : 
    1258                 :        4746 :   if (!validate_name (typelib, "field", typelib->data, blob->name, error))
    1259                 :           0 :     return FALSE;
    1260                 :             : 
    1261                 :        4746 :   if (blob->has_embedded_type)
    1262                 :             :     {
    1263                 :        1804 :       if (!validate_callback_blob (ctx, offset + header->field_blob_size, error))
    1264                 :           0 :         return FALSE;
    1265                 :             :     }
    1266                 :        2942 :   else if (!validate_type_blob (typelib,
    1267                 :             :                                 offset + G_STRUCT_OFFSET (FieldBlob, type),
    1268                 :             :                                 0, FALSE, error))
    1269                 :           0 :     return FALSE;
    1270                 :             : 
    1271                 :        4746 :   return TRUE;
    1272                 :             : }
    1273                 :             : 
    1274                 :             : static gboolean
    1275                 :         858 : validate_property_blob (GITypelib     *typelib,
    1276                 :             :                         uint32_t       offset,
    1277                 :             :                         GError       **error)
    1278                 :             : {
    1279                 :             :   PropertyBlob *blob;
    1280                 :             : 
    1281                 :         858 :   if (typelib->len < offset + sizeof (PropertyBlob))
    1282                 :             :     {
    1283                 :           0 :       g_set_error (error,
    1284                 :             :                    GI_TYPELIB_ERROR,
    1285                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1286                 :             :                    "The buffer is too short");
    1287                 :           0 :       return FALSE;
    1288                 :             :     }
    1289                 :             : 
    1290                 :         858 :   blob = (PropertyBlob*) &typelib->data[offset];
    1291                 :             : 
    1292                 :         858 :   if (!validate_name (typelib, "property", typelib->data, blob->name, error))
    1293                 :           0 :     return FALSE;
    1294                 :             : 
    1295                 :         858 :   if (!validate_type_blob (typelib,
    1296                 :             :                            offset + G_STRUCT_OFFSET (PropertyBlob, type),
    1297                 :             :                            0, FALSE, error))
    1298                 :           0 :     return FALSE;
    1299                 :             : 
    1300                 :         858 :   return TRUE;
    1301                 :             : }
    1302                 :             : 
    1303                 :             : static gboolean
    1304                 :         254 : validate_signal_blob (GITypelib     *typelib,
    1305                 :             :                       uint32_t       offset,
    1306                 :             :                       uint32_t       container_offset,
    1307                 :             :                       GError       **error)
    1308                 :             : {
    1309                 :             :   SignalBlob *blob;
    1310                 :             :   size_t n_signals;
    1311                 :             : 
    1312                 :         254 :   if (typelib->len < offset + sizeof (SignalBlob))
    1313                 :             :     {
    1314                 :           0 :       g_set_error (error,
    1315                 :             :                    GI_TYPELIB_ERROR,
    1316                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1317                 :             :                    "The buffer is too short");
    1318                 :           0 :       return FALSE;
    1319                 :             :     }
    1320                 :             : 
    1321                 :         254 :   blob = (SignalBlob*) &typelib->data[offset];
    1322                 :             : 
    1323                 :         254 :   if (!validate_name (typelib, "signal", typelib->data, blob->name, error))
    1324                 :           0 :     return FALSE;
    1325                 :             : 
    1326                 :         254 :   if ((blob->run_first != 0) +
    1327                 :         254 :       (blob->run_last != 0) +
    1328                 :         254 :       (blob->run_cleanup != 0) != 1)
    1329                 :             :     {
    1330                 :           0 :       g_set_error (error,
    1331                 :             :                    GI_TYPELIB_ERROR,
    1332                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1333                 :             :                    "Invalid signal run flags");
    1334                 :           0 :       return FALSE;
    1335                 :             :     }
    1336                 :             : 
    1337                 :         254 :   if (blob->has_class_closure)
    1338                 :             :     {
    1339                 :           0 :       if (((CommonBlob*)&typelib->data[container_offset])->blob_type == BLOB_TYPE_OBJECT)
    1340                 :             :         {
    1341                 :             :           ObjectBlob *object;
    1342                 :             : 
    1343                 :           0 :           object = (ObjectBlob*)&typelib->data[container_offset];
    1344                 :             : 
    1345                 :           0 :           n_signals = object->n_signals;
    1346                 :             :         }
    1347                 :             :       else
    1348                 :             :         {
    1349                 :             :           InterfaceBlob *iface;
    1350                 :             : 
    1351                 :           0 :           iface = (InterfaceBlob*)&typelib->data[container_offset];
    1352                 :             : 
    1353                 :           0 :           n_signals = iface->n_signals;
    1354                 :             :         }
    1355                 :             : 
    1356                 :           0 :       if (blob->class_closure >= n_signals)
    1357                 :             :         {
    1358                 :           0 :           g_set_error (error,
    1359                 :             :                        GI_TYPELIB_ERROR,
    1360                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1361                 :             :                        "Invalid class closure index");
    1362                 :           0 :           return FALSE;
    1363                 :             :         }
    1364                 :             :     }
    1365                 :             : 
    1366                 :         254 :   if (!validate_signature_blob (typelib, blob->signature, error))
    1367                 :           0 :     return FALSE;
    1368                 :             : 
    1369                 :         254 :   return TRUE;
    1370                 :             : }
    1371                 :             : 
    1372                 :             : static gboolean
    1373                 :        1656 : validate_vfunc_blob (GITypelib     *typelib,
    1374                 :             :                      uint32_t       offset,
    1375                 :             :                      uint32_t       container_offset,
    1376                 :             :                      GError       **error)
    1377                 :             : {
    1378                 :             :   VFuncBlob *blob;
    1379                 :             :   size_t n_vfuncs;
    1380                 :             : 
    1381                 :        1656 :   if (typelib->len < offset + sizeof (VFuncBlob))
    1382                 :             :     {
    1383                 :           0 :       g_set_error (error,
    1384                 :             :                    GI_TYPELIB_ERROR,
    1385                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1386                 :             :                    "The buffer is too short");
    1387                 :           0 :       return FALSE;
    1388                 :             :     }
    1389                 :             : 
    1390                 :        1656 :   blob = (VFuncBlob*) &typelib->data[offset];
    1391                 :             : 
    1392                 :        1656 :   if (!validate_name (typelib, "vfunc", typelib->data, blob->name, error))
    1393                 :           0 :     return FALSE;
    1394                 :             : 
    1395                 :        1656 :   if (blob->class_closure)
    1396                 :             :     {
    1397                 :           0 :       if (((CommonBlob*)&typelib->data[container_offset])->blob_type == BLOB_TYPE_OBJECT)
    1398                 :             :         {
    1399                 :             :           ObjectBlob *object;
    1400                 :             : 
    1401                 :           0 :           object = (ObjectBlob*)&typelib->data[container_offset];
    1402                 :             : 
    1403                 :           0 :           n_vfuncs = object->n_vfuncs;
    1404                 :             :         }
    1405                 :             :       else
    1406                 :             :         {
    1407                 :             :           InterfaceBlob *iface;
    1408                 :             : 
    1409                 :           0 :           iface = (InterfaceBlob*)&typelib->data[container_offset];
    1410                 :             : 
    1411                 :           0 :           n_vfuncs = iface->n_vfuncs;
    1412                 :             :         }
    1413                 :             : 
    1414                 :           0 :       if (blob->class_closure >= n_vfuncs)
    1415                 :             :         {
    1416                 :           0 :           g_set_error (error,
    1417                 :             :                        GI_TYPELIB_ERROR,
    1418                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1419                 :             :                        "Invalid class closure index");
    1420                 :           0 :           return FALSE;
    1421                 :             :         }
    1422                 :             :     }
    1423                 :             : 
    1424                 :        1656 :   if (!validate_signature_blob (typelib, blob->signature, error))
    1425                 :           0 :     return FALSE;
    1426                 :             : 
    1427                 :        1656 :   return TRUE;
    1428                 :             : }
    1429                 :             : 
    1430                 :             : static gboolean
    1431                 :        1141 : validate_struct_blob (ValidateContext *ctx,
    1432                 :             :                       uint32_t        offset,
    1433                 :             :                       uint16_t         blob_type,
    1434                 :             :                       GError         **error)
    1435                 :             : {
    1436                 :        1141 :   GITypelib *typelib = ctx->typelib;
    1437                 :             :   StructBlob *blob;
    1438                 :             :   size_t i;
    1439                 :             :   uint32_t field_offset;
    1440                 :             : 
    1441                 :        1141 :   if (typelib->len < offset + sizeof (StructBlob))
    1442                 :             :     {
    1443                 :           0 :       g_set_error (error,
    1444                 :             :                    GI_TYPELIB_ERROR,
    1445                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1446                 :             :                    "The buffer is too short");
    1447                 :           0 :       return FALSE;
    1448                 :             :     }
    1449                 :             : 
    1450                 :        1141 :   blob = (StructBlob*) &typelib->data[offset];
    1451                 :             : 
    1452                 :        1141 :   if (blob->blob_type != blob_type)
    1453                 :             :     {
    1454                 :           0 :       g_set_error (error,
    1455                 :             :                    GI_TYPELIB_ERROR,
    1456                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1457                 :             :                    "Wrong blob type");
    1458                 :           0 :       return FALSE;
    1459                 :             :     }
    1460                 :             : 
    1461                 :        1141 :   if (!validate_name (typelib, "struct", typelib->data, blob->name, error))
    1462                 :           0 :     return FALSE;
    1463                 :             : 
    1464                 :        1141 :   push_context (ctx, get_string_nofail (typelib, blob->name));
    1465                 :             : 
    1466                 :        1141 :   if (!blob->unregistered)
    1467                 :             :     {
    1468                 :         270 :       if (!validate_name (typelib, "boxed", typelib->data, blob->gtype_name, error))
    1469                 :           0 :         return FALSE;
    1470                 :             : 
    1471                 :         270 :       if (!validate_name (typelib, "boxed", typelib->data, blob->gtype_init, error))
    1472                 :           0 :         return FALSE;
    1473                 :             :     }
    1474                 :             :   else
    1475                 :             :     {
    1476                 :         871 :       if (blob->gtype_name || blob->gtype_init)
    1477                 :             :         {
    1478                 :           0 :           g_set_error (error,
    1479                 :             :                        GI_TYPELIB_ERROR,
    1480                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1481                 :             :                        "Gtype data in struct");
    1482                 :           0 :           return FALSE;
    1483                 :             :         }
    1484                 :             :     }
    1485                 :             : 
    1486                 :        1141 :   if (typelib->len < offset + sizeof (StructBlob) +
    1487                 :        1141 :             blob->n_fields * sizeof (FieldBlob) +
    1488                 :        1141 :             blob->n_methods * sizeof (FunctionBlob))
    1489                 :             :     {
    1490                 :           0 :       g_set_error (error,
    1491                 :             :                    GI_TYPELIB_ERROR,
    1492                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1493                 :             :                    "The buffer is too short");
    1494                 :           0 :       return FALSE;
    1495                 :             :     }
    1496                 :             : 
    1497                 :        1141 :   field_offset = offset + sizeof (StructBlob);
    1498                 :        5163 :   for (i = 0; i < blob->n_fields; i++)
    1499                 :             :     {
    1500                 :        4022 :       FieldBlob *field_blob = (FieldBlob*) &typelib->data[field_offset];
    1501                 :             : 
    1502                 :        4022 :       if (!validate_field_blob (ctx,
    1503                 :             :                                 field_offset,
    1504                 :             :                                 error))
    1505                 :           0 :         return FALSE;
    1506                 :             : 
    1507                 :        4022 :       field_offset += sizeof (FieldBlob);
    1508                 :        4022 :       if (field_blob->has_embedded_type)
    1509                 :        1804 :         field_offset += sizeof (CallbackBlob);
    1510                 :             :     }
    1511                 :             : 
    1512                 :        4918 :   for (i = 0; i < blob->n_methods; i++)
    1513                 :             :     {
    1514                 :        3777 :       if (!validate_function_blob (ctx,
    1515                 :             :                                    field_offset +
    1516                 :             :                                    i * sizeof (FunctionBlob),
    1517                 :             :                                    blob_type,
    1518                 :             :                                    error))
    1519                 :           0 :         return FALSE;
    1520                 :             :     }
    1521                 :             : 
    1522                 :        1141 :   pop_context (ctx);
    1523                 :             : 
    1524                 :        1141 :   return TRUE;
    1525                 :             : }
    1526                 :             : 
    1527                 :             : static gboolean
    1528                 :         470 : validate_enum_blob (ValidateContext *ctx,
    1529                 :             :                     uint32_t       offset,
    1530                 :             :                     uint16_t       blob_type,
    1531                 :             :                     GError       **error)
    1532                 :             : {
    1533                 :         470 :   GITypelib *typelib = ctx->typelib;
    1534                 :             :   EnumBlob *blob;
    1535                 :             :   uint32_t offset2;
    1536                 :             : 
    1537                 :         470 :   if (typelib->len < offset + sizeof (EnumBlob))
    1538                 :             :     {
    1539                 :           0 :       g_set_error (error,
    1540                 :             :                    GI_TYPELIB_ERROR,
    1541                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1542                 :             :                    "The buffer is too short");
    1543                 :           0 :       return FALSE;
    1544                 :             :     }
    1545                 :             : 
    1546                 :         470 :   blob = (EnumBlob*) &typelib->data[offset];
    1547                 :             : 
    1548                 :         470 :   if (blob->blob_type != blob_type)
    1549                 :             :     {
    1550                 :           0 :       g_set_error (error,
    1551                 :             :                    GI_TYPELIB_ERROR,
    1552                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1553                 :             :                    "Wrong blob type");
    1554                 :           0 :       return FALSE;
    1555                 :             :     }
    1556                 :             : 
    1557                 :         470 :   if (!blob->unregistered)
    1558                 :             :     {
    1559                 :         267 :       if (!validate_name (typelib, "enum", typelib->data, blob->gtype_name, error))
    1560                 :           0 :         return FALSE;
    1561                 :             : 
    1562                 :         267 :       if (!validate_name (typelib, "enum", typelib->data, blob->gtype_init, error))
    1563                 :           0 :         return FALSE;
    1564                 :             :     }
    1565                 :             :   else
    1566                 :             :     {
    1567                 :         203 :       if (blob->gtype_name || blob->gtype_init)
    1568                 :             :         {
    1569                 :           0 :           g_set_error (error,
    1570                 :             :                        GI_TYPELIB_ERROR,
    1571                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1572                 :             :                        "Gtype data in unregistered enum");
    1573                 :           0 :           return FALSE;
    1574                 :             :         }
    1575                 :             :     }
    1576                 :             : 
    1577                 :         470 :   if (!validate_name (typelib, "enum", typelib->data, blob->name, error))
    1578                 :           0 :     return FALSE;
    1579                 :             : 
    1580                 :         470 :   if (typelib->len < offset + sizeof (EnumBlob) +
    1581                 :         470 :       blob->n_values * sizeof (ValueBlob) +
    1582                 :         470 :       blob->n_methods * sizeof (FunctionBlob))
    1583                 :             :     {
    1584                 :           0 :       g_set_error (error,
    1585                 :             :                    GI_TYPELIB_ERROR,
    1586                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1587                 :             :                    "The buffer is too short");
    1588                 :           0 :       return FALSE;
    1589                 :             :     }
    1590                 :             : 
    1591                 :         470 :   offset2 = offset + sizeof (EnumBlob);
    1592                 :             : 
    1593                 :         470 :   push_context (ctx, get_string_nofail (typelib, blob->name));
    1594                 :             : 
    1595                 :        4239 :   for (size_t i = 0; i < blob->n_values; i++, offset2 += sizeof (ValueBlob))
    1596                 :             :     {
    1597                 :        3769 :       if (!validate_value_blob (typelib,
    1598                 :             :                                 offset2,
    1599                 :             :                                 error))
    1600                 :           0 :         return FALSE;
    1601                 :             : 
    1602                 :             : #if 0
    1603                 :             :       v1 = (ValueBlob *)&typelib->data[offset2];
    1604                 :             :       for (j = 0; j < i; j++)
    1605                 :             :         {
    1606                 :             :           v2 = (ValueBlob *)&typelib->data[offset2 +
    1607                 :             :                                             j * sizeof (ValueBlob)];
    1608                 :             : 
    1609                 :             :           if (v1->value == v2->value)
    1610                 :             :             {
    1611                 :             : 
    1612                 :             :               /* FIXME should this be an error ? */
    1613                 :             :               g_set_error (error,
    1614                 :             :                            GI_TYPELIB_ERROR,
    1615                 :             :                            GI_TYPELIB_ERROR_INVALID_BLOB,
    1616                 :             :                            "Duplicate enum value");
    1617                 :             :               return FALSE;
    1618                 :             :             }
    1619                 :             :         }
    1620                 :             : #endif
    1621                 :             :     }
    1622                 :             : 
    1623                 :         515 :   for (size_t i = 0; i < blob->n_methods; i++, offset2 += sizeof (FunctionBlob))
    1624                 :             :     {
    1625                 :          45 :       if (!validate_function_blob (ctx, offset2, BLOB_TYPE_ENUM, error))
    1626                 :           0 :         return FALSE;
    1627                 :             :     }
    1628                 :             : 
    1629                 :         470 :   pop_context (ctx);
    1630                 :             : 
    1631                 :         470 :   return TRUE;
    1632                 :             : }
    1633                 :             : 
    1634                 :             : static gboolean
    1635                 :         443 : validate_object_blob (ValidateContext *ctx,
    1636                 :             :                       uint32_t       offset,
    1637                 :             :                       GError       **error)
    1638                 :             : {
    1639                 :         443 :   GITypelib *typelib = ctx->typelib;
    1640                 :             :   Header *header;
    1641                 :             :   ObjectBlob *blob;
    1642                 :             :   size_t i;
    1643                 :             :   uint32_t offset2;
    1644                 :             :   uint16_t n_field_callbacks;
    1645                 :             : 
    1646                 :         443 :   header = (Header *)typelib->data;
    1647                 :             : 
    1648                 :         443 :   if (typelib->len < offset + sizeof (ObjectBlob))
    1649                 :             :     {
    1650                 :           0 :       g_set_error (error,
    1651                 :             :                    GI_TYPELIB_ERROR,
    1652                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1653                 :             :                    "The buffer is too short");
    1654                 :           0 :       return FALSE;
    1655                 :             :     }
    1656                 :             : 
    1657                 :         443 :   blob = (ObjectBlob*) &typelib->data[offset];
    1658                 :             : 
    1659                 :         443 :   if (blob->blob_type != BLOB_TYPE_OBJECT)
    1660                 :             :     {
    1661                 :           0 :       g_set_error (error,
    1662                 :             :                    GI_TYPELIB_ERROR,
    1663                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1664                 :             :                    "Wrong blob type");
    1665                 :           0 :       return FALSE;
    1666                 :             :     }
    1667                 :             : 
    1668                 :         443 :   if (!validate_name (typelib, "object", typelib->data, blob->gtype_name, error))
    1669                 :           0 :     return FALSE;
    1670                 :             : 
    1671                 :         443 :   if (!validate_name (typelib, "object", typelib->data, blob->gtype_init, error))
    1672                 :           0 :     return FALSE;
    1673                 :             : 
    1674                 :         443 :   if (!validate_name (typelib, "object", typelib->data, blob->name, error))
    1675                 :           0 :     return FALSE;
    1676                 :             : 
    1677                 :         443 :   if (blob->parent > header->n_entries)
    1678                 :             :     {
    1679                 :           0 :       g_set_error (error,
    1680                 :             :                    GI_TYPELIB_ERROR,
    1681                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1682                 :             :                    "Invalid parent index");
    1683                 :           0 :       return FALSE;
    1684                 :             :     }
    1685                 :             : 
    1686                 :         443 :   if (blob->parent != 0)
    1687                 :             :     {
    1688                 :             :       DirEntry *entry;
    1689                 :             : 
    1690                 :         436 :       entry = get_dir_entry_checked (typelib, blob->parent, error);
    1691                 :         436 :       if (!entry)
    1692                 :           0 :         return FALSE;
    1693                 :         436 :       if (entry->blob_type != BLOB_TYPE_OBJECT &&
    1694                 :         228 :           (entry->local || entry->blob_type != 0))
    1695                 :             :         {
    1696                 :           0 :           g_set_error (error,
    1697                 :             :                        GI_TYPELIB_ERROR,
    1698                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1699                 :             :                        "Parent not object");
    1700                 :           0 :           return FALSE;
    1701                 :             :         }
    1702                 :             :     }
    1703                 :             : 
    1704                 :         443 :   if (blob->gtype_struct != 0)
    1705                 :             :     {
    1706                 :             :       DirEntry *entry;
    1707                 :             : 
    1708                 :         289 :       entry = get_dir_entry_checked (typelib, blob->gtype_struct, error);
    1709                 :         289 :       if (!entry)
    1710                 :           0 :         return FALSE;
    1711                 :         289 :       if (entry->blob_type != BLOB_TYPE_STRUCT && entry->local)
    1712                 :             :         {
    1713                 :           0 :           g_set_error (error,
    1714                 :             :                        GI_TYPELIB_ERROR,
    1715                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1716                 :             :                        "Class struct invalid type or not local");
    1717                 :           0 :           return FALSE;
    1718                 :             :         }
    1719                 :             :     }
    1720                 :             : 
    1721                 :         443 :   if (typelib->len < offset + sizeof (ObjectBlob) +
    1722                 :         443 :             (blob->n_interfaces + blob->n_interfaces % 2u) * 2u +
    1723                 :         443 :             blob->n_fields * sizeof (FieldBlob) +
    1724                 :         443 :             blob->n_properties * sizeof (PropertyBlob) +
    1725                 :         443 :             blob->n_methods * sizeof (FunctionBlob) +
    1726                 :         443 :             blob->n_signals * sizeof (SignalBlob) +
    1727                 :         443 :             blob->n_vfuncs * sizeof (VFuncBlob) +
    1728                 :         443 :             blob->n_constants * sizeof (ConstantBlob))
    1729                 :             : 
    1730                 :             :     {
    1731                 :           0 :       g_set_error (error,
    1732                 :             :                    GI_TYPELIB_ERROR,
    1733                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1734                 :             :                    "The buffer is too short");
    1735                 :           0 :       return FALSE;
    1736                 :             :     }
    1737                 :             : 
    1738                 :         443 :   offset2 = offset + sizeof (ObjectBlob);
    1739                 :             : 
    1740                 :         655 :   for (i = 0; i < blob->n_interfaces; i++, offset2 += 2)
    1741                 :             :     {
    1742                 :             :       uint16_t iface;
    1743                 :             :       DirEntry *entry;
    1744                 :             : 
    1745                 :         212 :       iface = *(uint16_t *)&typelib->data[offset2];
    1746                 :         212 :       if (iface == 0 || iface > header->n_entries)
    1747                 :             :         {
    1748                 :           0 :           g_set_error (error,
    1749                 :             :                        GI_TYPELIB_ERROR,
    1750                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1751                 :             :                        "Invalid interface index");
    1752                 :           0 :           return FALSE;
    1753                 :             :         }
    1754                 :             : 
    1755                 :         212 :       entry = get_dir_entry_checked (typelib, iface, error);
    1756                 :         212 :       if (!entry)
    1757                 :           0 :         return FALSE;
    1758                 :             : 
    1759                 :         212 :       if (entry->blob_type != BLOB_TYPE_INTERFACE &&
    1760                 :           6 :           (entry->local || entry->blob_type != 0))
    1761                 :             :         {
    1762                 :           0 :           g_set_error (error,
    1763                 :             :                        GI_TYPELIB_ERROR,
    1764                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1765                 :             :                        "Not an interface");
    1766                 :           0 :           return FALSE;
    1767                 :             :         }
    1768                 :             :     }
    1769                 :             : 
    1770                 :         443 :   offset2 += 2 * (blob->n_interfaces %2);
    1771                 :             : 
    1772                 :         443 :   push_context (ctx, get_string_nofail (typelib, blob->name));
    1773                 :             : 
    1774                 :         443 :   n_field_callbacks = 0;
    1775                 :        1167 :   for (i = 0; i < blob->n_fields; i++)
    1776                 :             :     {
    1777                 :         724 :       FieldBlob *field_blob = (FieldBlob*) &typelib->data[offset2];
    1778                 :             : 
    1779                 :         724 :       if (!validate_field_blob (ctx, offset2, error))
    1780                 :           0 :         return FALSE;
    1781                 :             : 
    1782                 :         724 :       offset2 += sizeof (FieldBlob);
    1783                 :             :       /* Special case fields which are callbacks. */
    1784                 :         724 :       if (field_blob->has_embedded_type) {
    1785                 :           0 :         offset2 += sizeof (CallbackBlob);
    1786                 :           0 :         n_field_callbacks++;
    1787                 :             :       }
    1788                 :             :     }
    1789                 :             : 
    1790                 :         443 :   if (blob->n_field_callbacks != n_field_callbacks)
    1791                 :             :     {
    1792                 :           0 :       g_set_error (error,
    1793                 :             :                    GI_TYPELIB_ERROR,
    1794                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1795                 :             :                    "Incorrect number of field callbacks; expected "
    1796                 :             :                    "%" G_GUINT16_FORMAT ", got %" G_GUINT16_FORMAT,
    1797                 :           0 :                    blob->n_field_callbacks, n_field_callbacks);
    1798                 :           0 :       return FALSE;
    1799                 :             :     }
    1800                 :             : 
    1801                 :        1205 :   for (i = 0; i < blob->n_properties; i++, offset2 += sizeof (PropertyBlob))
    1802                 :             :     {
    1803                 :         762 :       if (!validate_property_blob (typelib, offset2, error))
    1804                 :           0 :         return FALSE;
    1805                 :             :     }
    1806                 :             : 
    1807                 :        4334 :   for (i = 0; i < blob->n_methods; i++, offset2 += sizeof (FunctionBlob))
    1808                 :             :     {
    1809                 :        3891 :       if (!validate_function_blob (ctx, offset2, BLOB_TYPE_OBJECT, error))
    1810                 :           0 :         return FALSE;
    1811                 :             :     }
    1812                 :             : 
    1813                 :         628 :   for (i = 0; i < blob->n_signals; i++, offset2 += sizeof (SignalBlob))
    1814                 :             :     {
    1815                 :         185 :       if (!validate_signal_blob (typelib, offset2, offset, error))
    1816                 :           0 :         return FALSE;
    1817                 :             :     }
    1818                 :             : 
    1819                 :        1154 :   for (i = 0; i < blob->n_vfuncs; i++, offset2 += sizeof (VFuncBlob))
    1820                 :             :     {
    1821                 :         711 :       if (!validate_vfunc_blob (typelib, offset2, offset, error))
    1822                 :           0 :         return FALSE;
    1823                 :             :     }
    1824                 :             : 
    1825                 :         443 :   for (i = 0; i < blob->n_constants; i++, offset2 += sizeof (ConstantBlob))
    1826                 :             :     {
    1827                 :           0 :       if (!validate_constant_blob (typelib, offset2, error))
    1828                 :           0 :         return FALSE;
    1829                 :             :     }
    1830                 :             : 
    1831                 :         443 :   pop_context (ctx);
    1832                 :             : 
    1833                 :         443 :   return TRUE;
    1834                 :             : }
    1835                 :             : 
    1836                 :             : static gboolean
    1837                 :         122 : validate_interface_blob (ValidateContext *ctx,
    1838                 :             :                          uint32_t       offset,
    1839                 :             :                          GError       **error)
    1840                 :             : {
    1841                 :         122 :   GITypelib *typelib = ctx->typelib;
    1842                 :             :   Header *header;
    1843                 :             :   InterfaceBlob *blob;
    1844                 :             :   size_t i;
    1845                 :             :   uint32_t offset2;
    1846                 :             : 
    1847                 :         122 :   header = (Header *)typelib->data;
    1848                 :             : 
    1849                 :         122 :   if (typelib->len < offset + sizeof (InterfaceBlob))
    1850                 :             :     {
    1851                 :           0 :       g_set_error (error,
    1852                 :             :                    GI_TYPELIB_ERROR,
    1853                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1854                 :             :                    "The buffer is too short");
    1855                 :           0 :       return FALSE;
    1856                 :             :     }
    1857                 :             : 
    1858                 :         122 :   blob = (InterfaceBlob*) &typelib->data[offset];
    1859                 :             : 
    1860                 :         122 :   if (blob->blob_type != BLOB_TYPE_INTERFACE)
    1861                 :             :     {
    1862                 :           0 :       g_set_error (error,
    1863                 :             :                    GI_TYPELIB_ERROR,
    1864                 :             :                    GI_TYPELIB_ERROR_INVALID_BLOB,
    1865                 :           0 :                    "Wrong blob type; expected interface, got %d", blob->blob_type);
    1866                 :           0 :       return FALSE;
    1867                 :             :     }
    1868                 :             : 
    1869                 :         122 :   if (!validate_name (typelib, "interface", typelib->data, blob->gtype_name, error))
    1870                 :           0 :     return FALSE;
    1871                 :             : 
    1872                 :         122 :   if (!validate_name (typelib, "interface", typelib->data, blob->gtype_init, error))
    1873                 :           0 :     return FALSE;
    1874                 :             : 
    1875                 :         122 :   if (!validate_name (typelib, "interface", typelib->data, blob->name, error))
    1876                 :           0 :     return FALSE;
    1877                 :             : 
    1878                 :         122 :   if (typelib->len < offset + sizeof (InterfaceBlob) +
    1879                 :         122 :             (blob->n_prerequisites + blob->n_prerequisites % 2u) * 2u +
    1880                 :         122 :             blob->n_properties * sizeof (PropertyBlob) +
    1881                 :         122 :             blob->n_methods * sizeof (FunctionBlob) +
    1882                 :         122 :             blob->n_signals * sizeof (SignalBlob) +
    1883                 :         122 :             blob->n_vfuncs * sizeof (VFuncBlob) +
    1884                 :         122 :             blob->n_constants * sizeof (ConstantBlob))
    1885                 :             : 
    1886                 :             :     {
    1887                 :           0 :       g_set_error (error,
    1888                 :             :                    GI_TYPELIB_ERROR,
    1889                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1890                 :             :                    "The buffer is too short");
    1891                 :           0 :       return FALSE;
    1892                 :             :     }
    1893                 :             : 
    1894                 :         122 :   offset2 = offset + sizeof (InterfaceBlob);
    1895                 :             : 
    1896                 :         170 :   for (i = 0; i < blob->n_prerequisites; i++, offset2 += 2)
    1897                 :             :     {
    1898                 :             :       DirEntry *entry;
    1899                 :             :       uint16_t req;
    1900                 :             : 
    1901                 :          48 :       req = *(uint16_t *)&typelib->data[offset2];
    1902                 :          48 :       if (req == 0 || req > header->n_entries)
    1903                 :             :         {
    1904                 :           0 :           g_set_error (error,
    1905                 :             :                        GI_TYPELIB_ERROR,
    1906                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1907                 :             :                        "Invalid prerequisite index");
    1908                 :           0 :           return FALSE;
    1909                 :             :         }
    1910                 :             : 
    1911                 :          48 :       entry = gi_typelib_get_dir_entry (typelib, req);
    1912                 :          48 :       if (entry->blob_type != BLOB_TYPE_INTERFACE &&
    1913                 :          15 :           entry->blob_type != BLOB_TYPE_OBJECT &&
    1914                 :           0 :           (entry->local || entry->blob_type != 0))
    1915                 :             :         {
    1916                 :           0 :           g_set_error (error,
    1917                 :             :                        GI_TYPELIB_ERROR,
    1918                 :             :                        GI_TYPELIB_ERROR_INVALID_BLOB,
    1919                 :             :                        "Not an interface or object");
    1920                 :           0 :           return FALSE;
    1921                 :             :         }
    1922                 :             :     }
    1923                 :             : 
    1924                 :         122 :   offset2 += 2 * (blob->n_prerequisites % 2);
    1925                 :             : 
    1926                 :         122 :   push_context (ctx, get_string_nofail (typelib, blob->name));
    1927                 :             : 
    1928                 :         218 :   for (i = 0; i < blob->n_properties; i++, offset2 += sizeof (PropertyBlob))
    1929                 :             :     {
    1930                 :          96 :       if (!validate_property_blob (typelib, offset2, error))
    1931                 :           0 :         return FALSE;
    1932                 :             :     }
    1933                 :             : 
    1934                 :        1402 :   for (i = 0; i < blob->n_methods; i++, offset2 += sizeof (FunctionBlob))
    1935                 :             :     {
    1936                 :        1280 :       if (!validate_function_blob (ctx, offset2, BLOB_TYPE_INTERFACE, error))
    1937                 :           0 :         return FALSE;
    1938                 :             :     }
    1939                 :             : 
    1940                 :         191 :   for (i = 0; i < blob->n_signals; i++, offset2 += sizeof (SignalBlob))
    1941                 :             :     {
    1942                 :          69 :       if (!validate_signal_blob (typelib, offset2, offset, error))
    1943                 :           0 :         return FALSE;
    1944                 :             :     }
    1945                 :             : 
    1946                 :        1067 :   for (i = 0; i < blob->n_vfuncs; i++, offset2 += sizeof (VFuncBlob))
    1947                 :             :     {
    1948                 :         945 :       if (!validate_vfunc_blob (typelib, offset2, offset, error))
    1949                 :           0 :         return FALSE;
    1950                 :             :     }
    1951                 :             : 
    1952                 :         122 :   for (i = 0; i < blob->n_constants; i++, offset2 += sizeof (ConstantBlob))
    1953                 :             :     {
    1954                 :           0 :       if (!validate_constant_blob (typelib, offset2, error))
    1955                 :           0 :         return FALSE;
    1956                 :             :     }
    1957                 :             : 
    1958                 :         122 :   pop_context (ctx);
    1959                 :             : 
    1960                 :         122 :   return TRUE;
    1961                 :             : }
    1962                 :             : 
    1963                 :             : static gboolean
    1964                 :          19 : validate_union_blob (GITypelib     *typelib,
    1965                 :             :                      uint32_t       offset,
    1966                 :             :                      GError       **error)
    1967                 :             : {
    1968                 :          19 :   return TRUE;
    1969                 :             : }
    1970                 :             : 
    1971                 :             : static gboolean
    1972                 :        6381 : validate_blob (ValidateContext *ctx,
    1973                 :             :                uint32_t         offset,
    1974                 :             :                GError         **error)
    1975                 :             : {
    1976                 :        6381 :   GITypelib *typelib = ctx->typelib;
    1977                 :             :   CommonBlob *common;
    1978                 :             : 
    1979                 :        6381 :   if (typelib->len < offset + sizeof (CommonBlob))
    1980                 :             :     {
    1981                 :           0 :       g_set_error (error,
    1982                 :             :                    GI_TYPELIB_ERROR,
    1983                 :             :                    GI_TYPELIB_ERROR_INVALID,
    1984                 :             :                    "The buffer is too short");
    1985                 :           0 :       return FALSE;
    1986                 :             :     }
    1987                 :             : 
    1988                 :        6381 :   common = (CommonBlob*)&typelib->data[offset];
    1989                 :             : 
    1990                 :        6381 :   switch (common->blob_type)
    1991                 :             :     {
    1992                 :        2940 :     case BLOB_TYPE_FUNCTION:
    1993                 :        2940 :       if (!validate_function_blob (ctx, offset, 0, error))
    1994                 :           0 :         return FALSE;
    1995                 :        2940 :       break;
    1996                 :         379 :     case BLOB_TYPE_CALLBACK:
    1997                 :         379 :       if (!validate_callback_blob (ctx, offset, error))
    1998                 :           0 :         return FALSE;
    1999                 :         379 :       break;
    2000                 :        1141 :     case BLOB_TYPE_STRUCT:
    2001                 :             :     case BLOB_TYPE_BOXED:
    2002                 :        1141 :       if (!validate_struct_blob (ctx, offset, common->blob_type, error))
    2003                 :           0 :         return FALSE;
    2004                 :        1141 :       break;
    2005                 :         470 :     case BLOB_TYPE_ENUM:
    2006                 :             :     case BLOB_TYPE_FLAGS:
    2007                 :         470 :       if (!validate_enum_blob (ctx, offset, common->blob_type, error))
    2008                 :           0 :         return FALSE;
    2009                 :         470 :       break;
    2010                 :         443 :     case BLOB_TYPE_OBJECT:
    2011                 :         443 :       if (!validate_object_blob (ctx, offset, error))
    2012                 :           0 :         return FALSE;
    2013                 :         443 :       break;
    2014                 :         122 :     case BLOB_TYPE_INTERFACE:
    2015                 :         122 :       if (!validate_interface_blob (ctx, offset, error))
    2016                 :           0 :         return FALSE;
    2017                 :         122 :       break;
    2018                 :         867 :     case BLOB_TYPE_CONSTANT:
    2019                 :         867 :       if (!validate_constant_blob (typelib, offset, error))
    2020                 :           0 :         return FALSE;
    2021                 :         867 :       break;
    2022                 :          19 :     case BLOB_TYPE_UNION:
    2023                 :          19 :       if (!validate_union_blob (typelib, offset, error))
    2024                 :           0 :         return FALSE;
    2025                 :          19 :       break;
    2026                 :           0 :     default:
    2027                 :           0 :       g_set_error (error,
    2028                 :             :                    GI_TYPELIB_ERROR,
    2029                 :             :                    GI_TYPELIB_ERROR_INVALID_ENTRY,
    2030                 :             :                    "Invalid blob type");
    2031                 :           0 :       return FALSE;
    2032                 :             :     }
    2033                 :             : 
    2034                 :        6381 :   return TRUE;
    2035                 :             : }
    2036                 :             : 
    2037                 :             : static gboolean
    2038                 :          13 : validate_directory (ValidateContext   *ctx,
    2039                 :             :                     GError            **error)
    2040                 :             : {
    2041                 :          13 :   GITypelib *typelib = ctx->typelib;
    2042                 :          13 :   Header *header = (Header *)typelib->data;
    2043                 :             :   DirEntry *entry;
    2044                 :             :   size_t i;
    2045                 :             : 
    2046                 :          13 :   if (typelib->len < header->directory + header->n_entries * sizeof (DirEntry))
    2047                 :             :     {
    2048                 :           0 :       g_set_error (error,
    2049                 :             :                    GI_TYPELIB_ERROR,
    2050                 :             :                    GI_TYPELIB_ERROR_INVALID,
    2051                 :             :                    "The buffer is too short");
    2052                 :           0 :       return FALSE;
    2053                 :             :     }
    2054                 :             : 
    2055                 :        6569 :   for (i = 0; i < header->n_entries; i++)
    2056                 :             :     {
    2057                 :        6556 :       entry = gi_typelib_get_dir_entry (typelib, i + 1);
    2058                 :             : 
    2059                 :        6556 :       if (!validate_name (typelib, "entry", typelib->data, entry->name, error))
    2060                 :           0 :         return FALSE;
    2061                 :             : 
    2062                 :        6556 :       if ((entry->local && entry->blob_type == BLOB_TYPE_INVALID) ||
    2063                 :        6556 :           entry->blob_type > BLOB_TYPE_UNION)
    2064                 :             :         {
    2065                 :           0 :           g_set_error (error,
    2066                 :             :                        GI_TYPELIB_ERROR,
    2067                 :             :                        GI_TYPELIB_ERROR_INVALID_DIRECTORY,
    2068                 :             :                        "Invalid entry type");
    2069                 :           0 :           return FALSE;
    2070                 :             :         }
    2071                 :             : 
    2072                 :        6556 :       if (i < header->n_local_entries)
    2073                 :             :         {
    2074                 :        6381 :           if (!entry->local)
    2075                 :             :             {
    2076                 :           0 :               g_set_error (error,
    2077                 :             :                            GI_TYPELIB_ERROR,
    2078                 :             :                            GI_TYPELIB_ERROR_INVALID_DIRECTORY,
    2079                 :             :                            "Too few local directory entries");
    2080                 :           0 :               return FALSE;
    2081                 :             :             }
    2082                 :             : 
    2083                 :        6381 :           if (!is_aligned (entry->offset))
    2084                 :             :             {
    2085                 :           0 :               g_set_error (error,
    2086                 :             :                            GI_TYPELIB_ERROR,
    2087                 :             :                            GI_TYPELIB_ERROR_INVALID_DIRECTORY,
    2088                 :             :                            "Misaligned entry");
    2089                 :           0 :               return FALSE;
    2090                 :             :             }
    2091                 :             : 
    2092                 :        6381 :           if (!validate_blob (ctx, entry->offset, error))
    2093                 :           0 :             return FALSE;
    2094                 :             :         }
    2095                 :             :       else
    2096                 :             :         {
    2097                 :         175 :           if (entry->local)
    2098                 :             :             {
    2099                 :           0 :               g_set_error (error,
    2100                 :             :                            GI_TYPELIB_ERROR,
    2101                 :             :                            GI_TYPELIB_ERROR_INVALID_DIRECTORY,
    2102                 :             :                            "Too many local directory entries");
    2103                 :           0 :               return FALSE;
    2104                 :             :             }
    2105                 :             : 
    2106                 :         175 :           if (!validate_name (typelib, "namespace", typelib->data, entry->offset, error))
    2107                 :           0 :             return FALSE;
    2108                 :             :         }
    2109                 :             :     }
    2110                 :             : 
    2111                 :          13 :   return TRUE;
    2112                 :             : }
    2113                 :             : 
    2114                 :             : static gboolean
    2115                 :          13 : validate_attributes (ValidateContext *ctx,
    2116                 :             :                      GError       **error)
    2117                 :             : {
    2118                 :          13 :   GITypelib *typelib = ctx->typelib;
    2119                 :          13 :   Header *header = (Header *)typelib->data;
    2120                 :             : 
    2121                 :          13 :   if (header->size < header->attributes + header->n_attributes * sizeof (AttributeBlob))
    2122                 :             :     {
    2123                 :           0 :       g_set_error (error,
    2124                 :             :                    GI_TYPELIB_ERROR,
    2125                 :             :                    GI_TYPELIB_ERROR_INVALID,
    2126                 :             :                    "The buffer is too short");
    2127                 :           0 :       return FALSE;
    2128                 :             :     }
    2129                 :             : 
    2130                 :          13 :   return TRUE;
    2131                 :             : }
    2132                 :             : 
    2133                 :             : static void
    2134                 :           0 : prefix_with_context (GError **error,
    2135                 :             :                      const char *section,
    2136                 :             :                      ValidateContext *ctx)
    2137                 :             : {
    2138                 :             :   GString *str;
    2139                 :             :   GSList *link;
    2140                 :             :   char *buf;
    2141                 :             : 
    2142                 :           0 :   link = ctx->context_stack;
    2143                 :           0 :   if (!link)
    2144                 :             :     {
    2145                 :           0 :       g_prefix_error (error, "In %s:", section);
    2146                 :           0 :       return;
    2147                 :             :     }
    2148                 :             : 
    2149                 :           0 :   str = g_string_new (NULL);
    2150                 :             : 
    2151                 :           0 :   for (; link; link = link->next)
    2152                 :             :     {
    2153                 :           0 :       g_string_append (str, link->data);
    2154                 :           0 :       if (link->next)
    2155                 :             :         g_string_append_c (str, '/');
    2156                 :             :     }
    2157                 :             :   g_string_append_c (str, ')');
    2158                 :           0 :   buf = g_string_free (str, FALSE);
    2159                 :           0 :   g_prefix_error (error, "In %s (Context: %s): ", section, buf);
    2160                 :           0 :   g_free (buf);
    2161                 :             : }
    2162                 :             : 
    2163                 :             : /**
    2164                 :             :  * gi_typelib_validate:
    2165                 :             :  * @typelib: a #GITypelib
    2166                 :             :  * @error: return location for a [type@GLib.Error], or `NULL`
    2167                 :             :  *
    2168                 :             :  * Check whether @typelib is well-formed, i.e. that the file is not corrupt or
    2169                 :             :  * truncated.
    2170                 :             :  *
    2171                 :             :  * Returns: `TRUE` if @typelib is well-formed, `FALSE` otherwise
    2172                 :             :  * Since: 2.80
    2173                 :             :  */
    2174                 :             : gboolean
    2175                 :          13 : gi_typelib_validate (GITypelib  *typelib,
    2176                 :             :                      GError    **error)
    2177                 :             : {
    2178                 :             :   ValidateContext ctx;
    2179                 :          13 :   ctx.typelib = typelib;
    2180                 :          13 :   ctx.context_stack = NULL;
    2181                 :             : 
    2182                 :          13 :   if (!validate_header (&ctx, error))
    2183                 :             :     {
    2184                 :           0 :       prefix_with_context (error, "In header", &ctx);
    2185                 :           0 :       return FALSE;
    2186                 :             :     }
    2187                 :             : 
    2188                 :          13 :   if (!validate_directory (&ctx, error))
    2189                 :             :     {
    2190                 :           0 :       prefix_with_context (error, "directory", &ctx);
    2191                 :           0 :       return FALSE;
    2192                 :             :     }
    2193                 :             : 
    2194                 :          13 :   if (!validate_attributes (&ctx, error))
    2195                 :             :     {
    2196                 :           0 :       prefix_with_context (error, "attributes", &ctx);
    2197                 :           0 :       return FALSE;
    2198                 :             :     }
    2199                 :             : 
    2200                 :          13 :   return TRUE;
    2201                 :             : }
    2202                 :             : 
    2203                 :             : /**
    2204                 :             :  * gi_typelib_error_quark:
    2205                 :             :  *
    2206                 :             :  * Get the quark representing the [type@GIRepository.TypelibError] error domain.
    2207                 :             :  *
    2208                 :             :  * Returns: quark representing the error domain
    2209                 :             :  * Since: 2.80
    2210                 :             :  */
    2211                 :             : GQuark
    2212                 :           0 : gi_typelib_error_quark (void)
    2213                 :             : {
    2214                 :             :   static GQuark quark = 0;
    2215                 :           0 :   if (quark == 0)
    2216                 :           0 :     quark = g_quark_from_static_string ("gi-typelib-error-quark");
    2217                 :           0 :   return quark;
    2218                 :             : }
    2219                 :             : 
    2220                 :             : /* Note on the GModule flags used by this function:
    2221                 :             : 
    2222                 :             :  * Glade's autoconnect feature and OpenGL's extension mechanism
    2223                 :             :  * as used by Clutter rely on g_module_open(NULL) to work as a means of
    2224                 :             :  * accessing the app's symbols. This keeps us from using
    2225                 :             :  * G_MODULE_BIND_LOCAL. BIND_LOCAL may have other issues as well;
    2226                 :             :  * in general libraries are not expecting multiple copies of
    2227                 :             :  * themselves and are not expecting to be unloaded. So we just
    2228                 :             :  * load modules globally for now.
    2229                 :             :  */
    2230                 :             : static GModule *
    2231                 :           7 : load_one_shared_library (GITypelib  *typelib,
    2232                 :             :                          const char *shlib)
    2233                 :             : {
    2234                 :             :   GModule *m;
    2235                 :             : 
    2236                 :             : #ifdef __APPLE__
    2237                 :             :   /* On macOS, @-prefixed shlib paths (@rpath, @executable_path, @loader_path)
    2238                 :             :      need to be treated as absolute; trying to combine them with a
    2239                 :             :      configured library path produces a mangled path that is unresolvable
    2240                 :             :      and may cause unintended side effects (such as loading the library
    2241                 :             :      from a fall-back location on macOS 12.0.1).
    2242                 :             :   */
    2243                 :             :   if (!g_path_is_absolute (shlib) && !g_str_has_prefix (shlib, "@"))
    2244                 :             : #else
    2245                 :           7 :   if (!g_path_is_absolute (shlib))
    2246                 :             : #endif
    2247                 :             :     {
    2248                 :             :       /* First try in configured library paths */
    2249                 :           7 :       for (unsigned int i = 0; typelib->library_paths != NULL && i < typelib->library_paths->len; i++)
    2250                 :             :         {
    2251                 :           0 :           char *path = g_build_filename (typelib->library_paths->pdata[i], shlib, NULL);
    2252                 :             : 
    2253                 :           0 :           m = g_module_open (path, G_MODULE_BIND_LAZY);
    2254                 :             : 
    2255                 :           0 :           g_free (path);
    2256                 :           0 :           if (m != NULL)
    2257                 :           0 :             return m;
    2258                 :             :         }
    2259                 :             :     }
    2260                 :             : 
    2261                 :             :   /* Then try loading from standard paths */
    2262                 :             :   /* Do not attempt to fix up shlib to replace .la with .so:
    2263                 :             :      it's done by GModule anyway.
    2264                 :             :   */
    2265                 :           7 :   return g_module_open (shlib, G_MODULE_BIND_LAZY);
    2266                 :             : }
    2267                 :             : 
    2268                 :             : static void
    2269                 :           5 : gi_typelib_do_dlopen (GITypelib *typelib)
    2270                 :             : {
    2271                 :             :   Header *header;
    2272                 :             :   const char *shlib_str;
    2273                 :             : 
    2274                 :           5 :   header = (Header *) typelib->data;
    2275                 :             :   /* note that NULL shlib means to open the main app, which is allowed */
    2276                 :           5 :   if (header->shared_library)
    2277                 :           5 :     shlib_str = gi_typelib_get_string (typelib, header->shared_library);
    2278                 :             :   else
    2279                 :           0 :     shlib_str = NULL;
    2280                 :             : 
    2281                 :           5 :   if (shlib_str != NULL && shlib_str[0] != '\0')
    2282                 :           5 :     {
    2283                 :             :       char **shlibs;
    2284                 :             : 
    2285                 :             :       /* shared-library is a comma-separated list of libraries */
    2286                 :           5 :       shlibs = g_strsplit (shlib_str, ",", 0);
    2287                 :             : 
    2288                 :             :        /* We load all passed libs unconditionally as if the same library is loaded
    2289                 :             :         * again with g_module_open(), the same file handle will be returned. See bug:
    2290                 :             :         * http://bugzilla.gnome.org/show_bug.cgi?id=555294
    2291                 :             :         */
    2292                 :          12 :       for (size_t i = 0; shlibs[i]; i++)
    2293                 :             :         {
    2294                 :             :           GModule *module;
    2295                 :             : 
    2296                 :           7 :           module = load_one_shared_library (typelib, shlibs[i]);
    2297                 :             : 
    2298                 :           7 :           if (module == NULL)
    2299                 :             :             {
    2300                 :           0 :               g_warning ("Failed to load shared library '%s' referenced by the typelib: %s",
    2301                 :             :                          shlibs[i], g_module_error ());
    2302                 :             :             }
    2303                 :             :           else
    2304                 :             :             {
    2305                 :           7 :               typelib->modules = g_list_append (typelib->modules, module);
    2306                 :             :             }
    2307                 :             :        }
    2308                 :             : 
    2309                 :           5 :       g_strfreev (shlibs);
    2310                 :             :     }
    2311                 :             :   else
    2312                 :             :     {
    2313                 :             :       /* If there's no shared-library entry for this module, assume that
    2314                 :             :        * the module is for the application.  Some of the hand-written .gir files
    2315                 :             :        * in gobject-introspection don't have shared-library entries, but no one
    2316                 :             :        * is really going to be calling g_module_symbol on them either.
    2317                 :             :        */
    2318                 :           0 :       GModule *module = g_module_open (NULL, 0);
    2319                 :           0 :       if (module == NULL)
    2320                 :           0 :         g_warning ("gtypelib.c: Failed to g_module_open (NULL): %s", g_module_error ());
    2321                 :             :       else
    2322                 :           0 :         typelib->modules = g_list_prepend (typelib->modules, module);
    2323                 :             :     }
    2324                 :           5 : }
    2325                 :             : 
    2326                 :             : static inline void
    2327                 :           5 : gi_typelib_ensure_open (GITypelib *typelib)
    2328                 :             : {
    2329                 :           5 :   if (typelib->open_attempted)
    2330                 :           0 :     return;
    2331                 :           5 :   typelib->open_attempted = TRUE;
    2332                 :           5 :   gi_typelib_do_dlopen (typelib);
    2333                 :             : }
    2334                 :             : 
    2335                 :             : /**
    2336                 :             :  * gi_typelib_new_from_bytes:
    2337                 :             :  * @bytes: memory chunk containing the typelib
    2338                 :             :  * @error: a [type@GLib.Error]
    2339                 :             :  *
    2340                 :             :  * Creates a new [type@GIRepository.Typelib] from a [type@GLib.Bytes].
    2341                 :             :  *
    2342                 :             :  * The [type@GLib.Bytes] can point to a memory location or a mapped file, and
    2343                 :             :  * the typelib will hold a reference to it until the repository is destroyed.
    2344                 :             :  *
    2345                 :             :  * Returns: (transfer full): the new [type@GIRepository.Typelib]
    2346                 :             :  * Since: 2.80
    2347                 :             :  */
    2348                 :             : GITypelib *
    2349                 :         240 : gi_typelib_new_from_bytes (GBytes *bytes,
    2350                 :             :                            GError **error)
    2351                 :             : {
    2352                 :             :   GITypelib *meta;
    2353                 :             :   size_t len;
    2354                 :         240 :   const uint8_t *data = g_bytes_get_data (bytes, &len);
    2355                 :             : 
    2356                 :         240 :   if (!validate_header_basic (data, len, error))
    2357                 :           0 :     return NULL;
    2358                 :             : 
    2359                 :         240 :   meta = g_slice_new0 (GITypelib);
    2360                 :         240 :   g_atomic_ref_count_init (&meta->ref_count);
    2361                 :         240 :   meta->bytes = g_bytes_ref (bytes);
    2362                 :         240 :   meta->data = data;
    2363                 :         240 :   meta->len = len;
    2364                 :         240 :   meta->modules = NULL;
    2365                 :             : 
    2366                 :         240 :   return meta;
    2367                 :             : }
    2368                 :             : 
    2369                 :             : /**
    2370                 :             :  * gi_typelib_ref:
    2371                 :             :  * @typelib: (transfer none): a #GITypelib
    2372                 :             :  *
    2373                 :             :  * Increment the reference count of a [type@GIRepository.Typelib].
    2374                 :             :  *
    2375                 :             :  * Returns: (transfer full): the same @typelib pointer
    2376                 :             :  * Since: 2.80
    2377                 :             :  */
    2378                 :             : GITypelib *
    2379                 :         228 : gi_typelib_ref (GITypelib *typelib)
    2380                 :             : {
    2381                 :         228 :   g_return_val_if_fail (typelib != NULL, NULL);
    2382                 :             : 
    2383                 :         228 :   g_atomic_ref_count_inc (&typelib->ref_count);
    2384                 :             : 
    2385                 :         228 :   return typelib;
    2386                 :             : }
    2387                 :             : 
    2388                 :             : /**
    2389                 :             :  * gi_typelib_unref:
    2390                 :             :  * @typelib: (transfer full): a #GITypelib
    2391                 :             :  *
    2392                 :             :  * Decrement the reference count of a [type@GIRepository.Typelib].
    2393                 :             :  *
    2394                 :             :  * Once the reference count reaches zero, the typelib is freed.
    2395                 :             :  *
    2396                 :             :  * Since: 2.80
    2397                 :             :  */
    2398                 :             : void
    2399                 :         468 : gi_typelib_unref (GITypelib *typelib)
    2400                 :             : {
    2401                 :         468 :   g_return_if_fail (typelib != NULL);
    2402                 :             : 
    2403                 :         468 :   if (g_atomic_ref_count_dec (&typelib->ref_count))
    2404                 :             :     {
    2405                 :         240 :       g_clear_pointer (&typelib->bytes, g_bytes_unref);
    2406                 :             : 
    2407                 :         240 :       g_clear_pointer (&typelib->library_paths, g_ptr_array_unref);
    2408                 :             : 
    2409                 :         240 :       if (typelib->modules)
    2410                 :             :         {
    2411                 :           5 :           g_list_foreach (typelib->modules, (GFunc) (void *) g_module_close, NULL);
    2412                 :           5 :           g_list_free (typelib->modules);
    2413                 :             :         }
    2414                 :         240 :       g_slice_free (GITypelib, typelib);
    2415                 :             :     }
    2416                 :             : }
    2417                 :             : 
    2418                 :             : /**
    2419                 :             :  * gi_typelib_get_namespace:
    2420                 :             :  * @typelib: a #GITypelib
    2421                 :             :  *
    2422                 :             :  * Get the name of the namespace represented by @typelib.
    2423                 :             :  *
    2424                 :             :  * Returns: name of the namespace represented by @typelib
    2425                 :             :  * Since: 2.80
    2426                 :             :  */
    2427                 :             : const char *
    2428                 :         693 : gi_typelib_get_namespace (GITypelib *typelib)
    2429                 :             : {
    2430                 :         693 :   return gi_typelib_get_string (typelib, ((Header *) typelib->data)->namespace);
    2431                 :             : }
    2432                 :             : 
    2433                 :             : /**
    2434                 :             :  * gi_typelib_symbol:
    2435                 :             :  * @typelib: the typelib
    2436                 :             :  * @symbol_name: name of symbol to be loaded
    2437                 :             :  * @symbol: (out) (nullable): returns a pointer to the symbol value, or `NULL`
    2438                 :             :  *   on failure
    2439                 :             :  *
    2440                 :             :  * Loads a symbol from a `GITypelib`.
    2441                 :             :  *
    2442                 :             :  * Returns: `TRUE` on success
    2443                 :             :  * Since: 2.80
    2444                 :             :  */
    2445                 :             : gboolean
    2446                 :           5 : gi_typelib_symbol (GITypelib *typelib, const char *symbol_name, void **symbol)
    2447                 :             : {
    2448                 :             :   GList *l;
    2449                 :             : 
    2450                 :           5 :   gi_typelib_ensure_open (typelib);
    2451                 :             : 
    2452                 :             :   /*
    2453                 :             :    * The reason for having multiple modules dates from gir-repository
    2454                 :             :    * when it was desired to inject code (accessors, etc.) into an
    2455                 :             :    * existing library.  In that situation, the first module listed
    2456                 :             :    * will be the custom one, which overrides the main one.  A bit
    2457                 :             :    * inefficient, but the problem will go away when gir-repository
    2458                 :             :    * does.
    2459                 :             :    *
    2460                 :             :    * For modules with no shared library, we dlopen'd the current
    2461                 :             :    * process above.
    2462                 :             :    */
    2463                 :           5 :   for (l = typelib->modules; l; l = l->next)
    2464                 :             :     {
    2465                 :           5 :       GModule *module = l->data;
    2466                 :             : 
    2467                 :           5 :       if (g_module_symbol (module, symbol_name, symbol))
    2468                 :           5 :         return TRUE;
    2469                 :             :     }
    2470                 :             : 
    2471                 :           0 :   return FALSE;
    2472                 :             : }
        

Generated by: LCOV version 2.0-1