1 /***************************************************************************/
5 /* TrueTypeGX/AAT feat table validation (body). */
7 /* Copyright 2004, 2005, 2008, 2012 by */
8 /* suzuki toshiya, Masatake YAMATO, Red Hat K.K., */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
19 /***************************************************************************/
21 /* gxvalid is derived from both gxlayout module and otvalid module. */
22 /* Development of gxlayout is supported by the Information-technology */
23 /* Promotion Agency(IPA), Japan. */
25 /***************************************************************************/
33 /*************************************************************************/
35 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
36 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
37 /* messages during execution. */
40 #define FT_COMPONENT trace_gxvfeat
43 /*************************************************************************/
44 /*************************************************************************/
46 /***** Data and Types *****/
48 /*************************************************************************/
49 /*************************************************************************/
51 typedef struct GXV_feat_DataRec_
53 FT_UInt reserved_size;
57 } GXV_feat_DataRec, *GXV_feat_Data;
60 #define GXV_FEAT_DATA( field ) GXV_TABLE_DATA( feat, field )
63 typedef enum GXV_FeatureFlagsMask_
65 GXV_FEAT_MASK_EXCLUSIVE_SETTINGS = 0x8000U,
66 GXV_FEAT_MASK_DYNAMIC_DEFAULT = 0x4000,
67 GXV_FEAT_MASK_UNUSED = 0x3F00,
68 GXV_FEAT_MASK_DEFAULT_SETTING = 0x00FF
70 } GXV_FeatureFlagsMask;
73 /*************************************************************************/
74 /*************************************************************************/
76 /***** UTILITY FUNCTIONS *****/
78 /*************************************************************************/
79 /*************************************************************************/
82 gxv_feat_registry_validate( FT_UShort feature,
87 GXV_NAME_ENTER( "feature in registry" );
89 GXV_TRACE(( " (feature = %u)\n", feature ));
91 if ( feature >= gxv_feat_registry_length )
93 GXV_TRACE(( "feature number %d is out of range %d\n",
94 feature, gxv_feat_registry_length ));
95 GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA );
99 if ( gxv_feat_registry[feature].existence == 0 )
101 GXV_TRACE(( "feature number %d is in defined range but doesn't exist\n",
103 GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA );
107 if ( gxv_feat_registry[feature].apple_reserved )
109 /* Don't use here. Apple is reserved. */
110 GXV_TRACE(( "feature number %d is reserved by Apple\n", feature ));
111 if ( valid->root->level >= FT_VALIDATE_TIGHT )
115 if ( nSettings != gxv_feat_registry[feature].nSettings )
117 GXV_TRACE(( "feature %d: nSettings %d != defined nSettings %d\n",
119 gxv_feat_registry[feature].nSettings ));
120 if ( valid->root->level >= FT_VALIDATE_TIGHT )
124 if ( exclusive != gxv_feat_registry[feature].exclusive )
126 GXV_TRACE(( "exclusive flag %d differs from predefined value\n",
128 if ( valid->root->level >= FT_VALIDATE_TIGHT )
138 gxv_feat_name_index_validate( FT_Bytes table,
140 GXV_Validator valid )
147 GXV_NAME_ENTER( "nameIndex" );
149 GXV_LIMIT_CHECK( 2 );
150 nameIndex = FT_NEXT_SHORT ( p );
151 GXV_TRACE(( " (nameIndex = %d)\n", nameIndex ));
153 gxv_sfntName_validate( (FT_UShort)nameIndex,
163 gxv_feat_setting_validate( FT_Bytes table,
166 GXV_Validator valid )
172 GXV_NAME_ENTER( "setting" );
174 GXV_LIMIT_CHECK( 2 );
176 setting = FT_NEXT_USHORT( p );
178 /* If we have exclusive setting, the setting should be odd. */
179 if ( exclusive && ( setting & 1 ) == 0 )
182 gxv_feat_name_index_validate( p, limit, valid );
184 GXV_FEAT_DATA( setting ) = setting;
191 gxv_feat_name_validate( FT_Bytes table,
193 GXV_Validator valid )
196 FT_UInt reserved_size = GXV_FEAT_DATA( reserved_size );
200 FT_ULong settingTable;
201 FT_UShort featureFlags;
208 GXV_NAME_ENTER( "name" );
210 /* feature + nSettings + settingTable + featureFlags */
211 GXV_LIMIT_CHECK( 2 + 2 + 4 + 2 );
213 feature = FT_NEXT_USHORT( p );
214 GXV_FEAT_DATA( feature ) = feature;
216 nSettings = FT_NEXT_USHORT( p );
217 settingTable = FT_NEXT_ULONG ( p );
218 featureFlags = FT_NEXT_USHORT( p );
220 if ( settingTable < reserved_size )
223 if ( ( featureFlags & GXV_FEAT_MASK_UNUSED ) == 0 )
224 GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA );
226 exclusive = FT_BOOL( featureFlags & GXV_FEAT_MASK_EXCLUSIVE_SETTINGS );
229 FT_Byte dynamic_default;
232 if ( featureFlags & GXV_FEAT_MASK_DYNAMIC_DEFAULT )
233 dynamic_default = (FT_Byte)( featureFlags &
234 GXV_FEAT_MASK_DEFAULT_SETTING );
238 /* If exclusive, check whether default setting is in the range. */
239 if ( !( dynamic_default < nSettings ) )
243 gxv_feat_registry_validate( feature, nSettings, exclusive, valid );
245 gxv_feat_name_index_validate( p, limit, valid );
247 p = valid->root->base + settingTable;
248 for ( last_setting = -1, i = 0; i < nSettings; i++ )
250 gxv_feat_setting_validate( p, limit, exclusive, valid );
252 if ( (FT_Int)GXV_FEAT_DATA( setting ) <= last_setting )
253 GXV_SET_ERR_IF_PARANOID( FT_INVALID_FORMAT );
255 last_setting = (FT_Int)GXV_FEAT_DATA( setting );
256 /* setting + nameIndex */
264 /*************************************************************************/
265 /*************************************************************************/
267 /***** feat TABLE *****/
269 /*************************************************************************/
270 /*************************************************************************/
273 gxv_feat_validate( FT_Bytes table,
275 FT_Validator ftvalid )
277 GXV_ValidatorRec validrec;
278 GXV_Validator valid = &validrec;
280 GXV_feat_DataRec featrec;
281 GXV_feat_Data feat = &featrec;
286 FT_UInt featureNameCount;
292 valid->root = ftvalid;
293 valid->table_data = feat;
296 FT_TRACE3(( "validating `feat' table\n" ));
299 feat->reserved_size = 0;
301 /* version + featureNameCount + none_0 + none_1 */
302 GXV_LIMIT_CHECK( 4 + 2 + 2 + 4 );
303 feat->reserved_size += 4 + 2 + 2 + 4;
305 if ( FT_NEXT_ULONG( p ) != 0x00010000UL ) /* Version */
308 featureNameCount = FT_NEXT_USHORT( p );
309 GXV_TRACE(( " (featureNameCount = %d)\n", featureNameCount ));
311 if ( !( IS_PARANOID_VALIDATION ) )
312 p += 6; /* skip (none) and (none) */
315 if ( FT_NEXT_USHORT( p ) != 0 )
318 if ( FT_NEXT_ULONG( p ) != 0 )
322 feat->reserved_size += featureNameCount * ( 2 + 2 + 4 + 2 + 2 );
324 for ( last_feature = -1, i = 0; i < featureNameCount; i++ )
326 gxv_feat_name_validate( p, limit, valid );
328 if ( (FT_Int)GXV_FEAT_DATA( feature ) <= last_feature )
329 GXV_SET_ERR_IF_PARANOID( FT_INVALID_FORMAT );
331 last_feature = GXV_FEAT_DATA( feature );
332 p += 2 + 2 + 4 + 2 + 2;