* src/sfnt/sfobjs.c (sfnt_init_face): Check that format_tag is known
before loading the table directory.

* src/sfnt/ttload.c (tt_face_load_sfnt_header,
tt_face_load_directory): Delay sfnt_dir_check from
tt_face_load_sfnt_header to tt_face_load_directory.
diff --git a/ChangeLog b/ChangeLog
index f731573..7a4b984 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2005-11-21  Chia-I Wu  <b90201047@ntu.edu.tw>
+
+	* src/sfnt/sfobjs.c (sfnt_init_face): Check that format_tag is known
+	before loading the table directory.
+
+	* src/sfnt/ttload.c (tt_face_load_sfnt_header,
+	tt_face_load_directory): Delay sfnt_dir_check from
+	tt_face_load_sfnt_header to tt_face_load_directory.
+
 2005-11-20  Chia-I Wu  <b90201047@ntu.edu.tw>
 
 	* src/sfnt/ttload.c (sfnt_dir_check): Clean up and return correct
diff --git a/src/sfnt/sfobjs.c b/src/sfnt/sfobjs.c
index 991705d..4fa6d71 100644
--- a/src/sfnt/sfobjs.c
+++ b/src/sfnt/sfobjs.c
@@ -376,6 +376,13 @@
     if ( error )
       goto Exit;
 
+    if ( sfnt_header.format_tag != 0x00010000UL &&
+         sfnt_header.format_tag != TTAG_ttcf &&
+         sfnt_header.format_tag != FT_MAKE_TAG( 'O', 'T', 'T', 'O' ) &&
+         sfnt_header.format_tag != TTAG_true &&
+         sfnt_header.format_tag != 0x00020000UL )
+      return SFNT_Err_Unknown_File_Format;
+
     face->format_tag = sfnt_header.format_tag;
     face->num_tables = sfnt_header.num_tables;
 
diff --git a/src/sfnt/ttload.c b/src/sfnt/ttload.c
index 6a9a7a2..77acfc2 100644
--- a/src/sfnt/ttload.c
+++ b/src/sfnt/ttload.c
@@ -134,13 +134,7 @@
   }
 
 
-  /* In theory, we should check the values of `search_range',              */
-  /* `entry_selector', and `range_shift' to detect non-SFNT based files    */
-  /* whose header might also start with 0x100000L (yes, these exist).      */
-  /*                                                                       */
-  /* Very unfortunately, many TrueType fonts don't have these fields       */
-  /* set correctly and we must ignore them to support them.  An            */
-  /* alternative way to check the font file is thus to:                    */
+  /* Here, we:                                                             */
   /*                                                                       */
   /* - check that `num_tables' is valid                                    */
   /* - look for a "head" table, check its size, and parse it to            */
@@ -381,10 +375,12 @@
          FT_STREAM_READ_FIELDS( sfnt_header_fields, sfnt ) )
       return error;
 
-    /* now check the sfnt directory */
-    error = sfnt_dir_check( sfnt, stream );
-    if ( error )
-      FT_TRACE2(( "tt_face_load_sfnt_header: invalid SFNT!\n" ));
+    /* many fonts don't have these fields set correctly */
+#if 0
+    if ( sfnt->search_range != 1 << ( sfnt->entry_selector + 4 ) ||
+         sfnt->search_range + sfnt->range_shift != sfnt->num_tables << 4 )
+      return SFNT_Err_Unknown_File_Format;
+#endif
 
     return error;
   }
@@ -428,6 +424,14 @@
     FT_TRACE2(( "-- Tables count:   %12u\n",  sfnt->num_tables ));
     FT_TRACE2(( "-- Format version: %08lx\n", sfnt->format_tag ));
 
+    /* check first */
+    error = sfnt_dir_check( sfnt, stream );
+    if ( error ) {
+      FT_TRACE2(( "tt_face_load_directory: directory checking failed!\n" ));
+
+      return error;
+    }
+
     face->num_tables = sfnt->num_tables;
 
     if ( FT_QNEW_ARRAY( face->dir_tables, face->num_tables ) )