* src/sfnt/ttload.c (sfnt_dir_check): Clean up and return correct error code. (sfnt_init): New function to fill in face->ttc_header. A non-TTC font is synthesized into a TTC font with one offset table. (tt_face_load_sfnt_header): Use sfnt_init. Fix an invalid access when the font is TTC and face_index is -1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
diff --git a/ChangeLog b/ChangeLog
index 1b18a00..f731573 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2005-11-20 Chia-I Wu <b90201047@ntu.edu.tw>
+
+ * src/sfnt/ttload.c (sfnt_dir_check): Clean up and return correct
+ error code.
+ (sfnt_init): New function to fill in face->ttc_header. A non-TTC font
+ is synthesized into a TTC font with one offset table.
+ (tt_face_load_sfnt_header): Use sfnt_init.
+ Fix an invalid access when the font is TTC and face_index is -1.
+
2005-11-18 Werner Lemberg <wl@gnu.org>
* src/sfnt/ttload.c (tt_face_load_metrics): Ignore excess number
diff --git a/src/sfnt/ttload.c b/src/sfnt/ttload.c
index 0c3d357..6a9a7a2 100644
--- a/src/sfnt/ttload.c
+++ b/src/sfnt/ttload.c
@@ -151,13 +151,13 @@
/* Type 42 fonts, and will generally be invalid. */
/* */
static FT_Error
- sfnt_dir_check( FT_Stream stream,
- FT_ULong offset,
- FT_UInt num_tables )
- {
+ sfnt_dir_check( SFNT_Header sfnt,
+ FT_Stream stream )
+ {
FT_Error error;
FT_UInt nn;
FT_UInt has_head = 0, has_sing = 0, has_meta = 0;
+ FT_ULong offset = sfnt->offset + 12;
const FT_ULong glyx_tag = FT_MAKE_TAG( 'g', 'l', 'y', 'x' );
const FT_ULong locx_tag = FT_MAKE_TAG( 'l', 'o', 'c', 'x' );
@@ -176,43 +176,33 @@
};
- /* if 'num_tables' is 0, read the table count from the file */
- if ( num_tables == 0 )
- {
- if ( FT_STREAM_SEEK( offset ) ||
- FT_STREAM_SKIP( 4 ) ||
- FT_READ_USHORT( num_tables ) ||
- FT_STREAM_SKIP( 6 ) )
- goto Bad_Format;
-
- if ( offset + 12 + num_tables*16 > stream->size )
- goto Bad_Format;
- }
- else if ( FT_STREAM_SEEK( offset + 12 ) )
- goto Bad_Format;
+ if ( sfnt->num_tables == 0 ||
+ offset + sfnt->num_tables * 16 > stream->size )
+ return SFNT_Err_Unknown_File_Format;
+
+ if ( FT_STREAM_SEEK( offset ) )
+ return error;
- for ( nn = 0; nn < num_tables; nn++ )
+ for ( nn = 0; nn < sfnt->num_tables; nn++ )
{
TT_TableRec table;
if ( FT_STREAM_READ_FIELDS( sfnt_dir_entry_fields, &table ) )
- goto Bad_Format;
+ return error;
if ( table.Offset + table.Length > stream->size &&
table.Tag != glyx_tag && table.Tag != locx_tag )
- goto Bad_Format;
+ return SFNT_Err_Unknown_File_Format;
- if ( table.Tag == TTAG_head )
+ if ( table.Tag == TTAG_head || table.Tag == TTAG_bhed )
{
FT_UInt32 magic;
-
-#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
- head_retry:
-#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
-
- has_head = 1;
+#ifndef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
+ if ( table.Tag == TTAG_head )
+#endif
+ has_head = 1;
/* The table length should be 0x36, but certain font tools
* make it 0x38, so we will just check that it is greater.
@@ -221,41 +211,98 @@
* the table must be padded to 32-bit lengths, but this doesn't
* apply to the value of its "Length" field!
*/
- if ( table.Length < 0x36 ||
- FT_STREAM_SEEK( table.Offset + 12 ) ||
- FT_READ_ULONG( magic ) ||
- magic != 0x5F0F3CF5UL )
- goto Bad_Format;
-
- if ( FT_STREAM_SEEK( offset + 28 + 16*nn ) )
- goto Bad_Format;
- }
+ if ( table.Length < 0x36 )
+ return SFNT_Err_Unknown_File_Format;
-#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
- else if ( table.Tag == TTAG_bhed )
- goto head_retry;
-#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
+ if ( FT_STREAM_SEEK( table.Offset + 12 ) ||
+ FT_READ_ULONG( magic ) )
+ return error;
+
+ if ( magic != 0x5F0F3CF5UL )
+ return SFNT_Err_Unknown_File_Format;
+ if ( FT_STREAM_SEEK( offset + ( nn + 1 ) * 16 ) )
+ return error;
+ }
else if ( table.Tag == TTAG_SING )
has_sing = 1;
else if ( table.Tag == TTAG_META )
has_meta = 1;
}
- /* when sing and meta are present, head is not present */
- if ( has_sing && has_meta && has_head == 0 )
- goto Exit;
+ if ( has_head || ( has_sing && has_meta ) )
+ return SFNT_Err_Ok;
+ else
+ return SFNT_Err_Unknown_File_Format;
+ }
- /* otherwise, treat a missing head as a failure */
- if ( has_head == 0 )
- goto Bad_Format;
- Exit:
- return error;
+ /* Fill in face->ttc_header. If the font is not a TTC, it is */
+ /* synthesized into a TTC with one offset table. */
+ static FT_Error
+ sfnt_init( FT_Stream stream,
+ TT_Face face )
+ {
+ FT_Memory memory = stream->memory;
+ FT_Error error;
+ FT_ULong tag, offset;
- Bad_Format:
- error = SFNT_Err_Unknown_File_Format;
- goto Exit;
+ static const FT_Frame_Field ttc_header_fields[] =
+ {
+#undef FT_STRUCTURE
+#define FT_STRUCTURE TTC_HeaderRec
+
+ FT_FRAME_START( 8 ),
+ FT_FRAME_LONG( version ),
+ FT_FRAME_LONG( count ),
+ FT_FRAME_END
+ };
+
+ face->ttc_header.tag = 0;
+ face->ttc_header.version = 0;
+ face->ttc_header.count = 0;
+
+ offset = FT_STREAM_POS();
+
+ if ( FT_READ_ULONG( tag ) )
+ return error;
+
+ face->ttc_header.tag = TTAG_ttcf;
+
+ if ( tag == TTAG_ttcf )
+ {
+ FT_Int n;
+
+
+ FT_TRACE3(( "sfnt_init: file is a collection\n" ));
+
+ if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) )
+ return error;
+
+ /* now read the offsets of each font in the file */
+ if ( FT_NEW_ARRAY( face->ttc_header.offsets, face->ttc_header.count ) )
+ return error;
+
+ if ( FT_FRAME_ENTER( face->ttc_header.count * 4L ) )
+ return error;
+
+ for ( n = 0; n < face->ttc_header.count; n++ )
+ face->ttc_header.offsets[n] = FT_GET_ULONG();
+
+ FT_FRAME_EXIT();
+ }
+ else
+ {
+ face->ttc_header.version = 1 << 16;
+ face->ttc_header.count = 1;
+
+ if ( FT_NEW( face->ttc_header.offsets) )
+ return error;
+
+ face->ttc_header.offsets[0] = offset;
+ }
+
+ return error;
}
@@ -295,9 +342,7 @@
FT_Long face_index,
SFNT_Header sfnt )
{
- FT_Error error;
- FT_ULong font_format_tag, format_tag, offset;
- FT_Memory memory = stream->memory;
+ FT_Error error;
static const FT_Frame_Field sfnt_header_fields[] =
{
@@ -312,95 +357,35 @@
FT_FRAME_END
};
- static const FT_Frame_Field ttc_header_fields[] =
- {
-#undef FT_STRUCTURE
-#define FT_STRUCTURE TTC_HeaderRec
-
- FT_FRAME_START( 8 ),
- FT_FRAME_LONG( version ),
- FT_FRAME_LONG( count ),
- FT_FRAME_END
- };
-
FT_TRACE2(( "tt_face_load_sfnt_header: %08p, %ld\n",
face, face_index ));
- face->ttc_header.tag = 0;
- face->ttc_header.version = 0;
- face->ttc_header.count = 0;
-
- face->num_tables = 0;
-
- /* First of all, read the first 4 bytes. If it is `ttcf', then the */
- /* file is a TrueType collection, otherwise it is a single-face font. */
- /* */
- offset = FT_STREAM_POS();
-
- if ( FT_READ_ULONG( font_format_tag ) )
- goto Exit;
-
- format_tag = font_format_tag;
-
- if ( font_format_tag == TTAG_ttcf )
- {
- FT_Int n;
-
-
- FT_TRACE3(( "tt_face_load_sfnt_header: file is a collection\n" ));
-
- /* It is a TrueType collection, i.e. a file containing several */
- /* font files. Read the font directory now */
- if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) )
- goto Exit;
-
- /* now read the offsets of each font in the file */
- if ( FT_NEW_ARRAY( face->ttc_header.offsets, face->ttc_header.count ) ||
- FT_FRAME_ENTER( face->ttc_header.count * 4L ) )
- goto Exit;
-
- for ( n = 0; n < face->ttc_header.count; n++ )
- face->ttc_header.offsets[n] = FT_GET_ULONG();
-
- FT_FRAME_EXIT();
+ error = sfnt_init( stream, face );
+ if ( error )
+ return error;
- /* check face index */
- if ( face_index >= face->ttc_header.count )
- {
- error = SFNT_Err_Bad_Argument;
- goto Exit;
- }
+ if ( face_index < 0 )
+ face_index = 0;
- /* seek to the appropriate TrueType file, then read tag */
- offset = face->ttc_header.offsets[face_index];
+ if ( face_index >= face->ttc_header.count )
+ return SFNT_Err_Bad_Argument;
- if ( FT_STREAM_SEEK( offset ) ||
- FT_READ_LONG( format_tag ) )
- goto Exit;
- }
+ sfnt->offset = face->ttc_header.offsets[face_index];
- /* the format tag was read, now check the rest of the header */
- sfnt->format_tag = format_tag;
- sfnt->offset = offset;
+ if ( FT_STREAM_SEEK( sfnt->offset ) )
+ return error;
- if ( FT_STREAM_READ_FIELDS( sfnt_header_fields, sfnt ) )
- goto Exit;
+ /* read offset table */
+ if ( FT_READ_ULONG( sfnt->format_tag ) ||
+ FT_STREAM_READ_FIELDS( sfnt_header_fields, sfnt ) )
+ return error;
/* now check the sfnt directory */
- error = sfnt_dir_check( stream, offset, sfnt->num_tables );
+ error = sfnt_dir_check( sfnt, stream );
if ( error )
- {
- FT_TRACE2(( "tt_face_load_sfnt_header: file is not SFNT!\n" ));
- error = SFNT_Err_Unknown_File_Format;
- goto Exit;
- }
-
- /* disallow face index values > 0 for non-TTC files */
- if ( font_format_tag != TTAG_ttcf && face_index > 0 )
- error = SFNT_Err_Bad_Argument;
+ FT_TRACE2(( "tt_face_load_sfnt_header: invalid SFNT!\n" ));
- Exit:
return error;
}