- Remove svn:needs-lock, svn:eol-type, and svn:eol-tyle properties.
[reactos.git] / reactos / lib / 3rdparty / freetype / src / gxvalid / gxvfeat.c
1 /***************************************************************************/
2 /* */
3 /* gxvfeat.c */
4 /* */
5 /* TrueTypeGX/AAT feat table validation (body). */
6 /* */
7 /* Copyright 2004, 2005 by suzuki toshiya, Masatake YAMATO, Red Hat K.K., */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
17
18 /***************************************************************************/
19 /* */
20 /* gxvalid is derived from both gxlayout module and otvalid module. */
21 /* Development of gxlayout is supported by the Information-technology */
22 /* Promotion Agency(IPA), Japan. */
23 /* */
24 /***************************************************************************/
25
26
27 #include "gxvalid.h"
28 #include "gxvcommn.h"
29 #include "gxvfeat.h"
30
31
32 /*************************************************************************/
33 /* */
34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
36 /* messages during execution. */
37 /* */
38 #undef FT_COMPONENT
39 #define FT_COMPONENT trace_gxvfeat
40
41
42 /*************************************************************************/
43 /*************************************************************************/
44 /***** *****/
45 /***** Data and Types *****/
46 /***** *****/
47 /*************************************************************************/
48 /*************************************************************************/
49
50 typedef struct GXV_feat_DataRec_
51 {
52 FT_UInt reserved_size;
53 FT_UShort feature;
54 FT_UShort setting;
55
56 } GXV_feat_DataRec, *GXV_feat_Data;
57
58
59 #define GXV_FEAT_DATA( field ) GXV_TABLE_DATA( feat, field )
60
61
62 typedef enum
63 {
64 GXV_FEAT_MASK_EXCLUSIVE_SETTINGS = 0x8000U,
65 GXV_FEAT_MASK_DYNAMIC_DEFAULT = 0x4000,
66 GXV_FEAT_MASK_UNUSED = 0x3F00,
67 GXV_FEAT_MASK_DEFAULT_SETTING = 0x00FF
68
69 } GXV_FeatureFlagsMask;
70
71
72 /*************************************************************************/
73 /*************************************************************************/
74 /***** *****/
75 /***** UTILITY FUNCTIONS *****/
76 /***** *****/
77 /*************************************************************************/
78 /*************************************************************************/
79
80 static void
81 gxv_feat_registry_validate( FT_UShort feature,
82 FT_UShort nSettings,
83 FT_Bool exclusive,
84 GXV_Validator valid )
85 {
86 GXV_NAME_ENTER( "feature in registry" );
87
88 GXV_TRACE(( " (feature = %u)\n", feature ));
89
90 if ( feature >= gxv_feat_registry_length )
91 {
92 GXV_TRACE(( "feature number %d is out of range %d\n",
93 feature, gxv_feat_registry_length ));
94 if ( valid->root->level == FT_VALIDATE_PARANOID )
95 FT_INVALID_DATA;
96 goto Exit;
97 }
98
99 if ( gxv_feat_registry[feature].existence == 0 )
100 {
101 GXV_TRACE(( "feature number %d is in defined range but doesn't exist\n",
102 feature ));
103 if ( valid->root->level == FT_VALIDATE_PARANOID )
104 FT_INVALID_DATA;
105 goto Exit;
106 }
107
108 if ( gxv_feat_registry[feature].apple_reserved )
109 {
110 /* Don't use here. Apple is reserved. */
111 GXV_TRACE(( "feature number %d is reserved by Apple\n", feature ));
112 if ( valid->root->level >= FT_VALIDATE_TIGHT )
113 FT_INVALID_DATA;
114 }
115
116 if ( nSettings != gxv_feat_registry[feature].nSettings )
117 {
118 GXV_TRACE(( "feature %d: nSettings %d != defined nSettings %d\n",
119 feature, nSettings,
120 gxv_feat_registry[feature].nSettings ));
121 if ( valid->root->level >= FT_VALIDATE_TIGHT )
122 FT_INVALID_DATA;
123 }
124
125 if ( exclusive != gxv_feat_registry[feature].exclusive )
126 {
127 GXV_TRACE(( "exclusive flag %d differs from predefined value\n",
128 exclusive ));
129 if ( valid->root->level >= FT_VALIDATE_TIGHT )
130 FT_INVALID_DATA;
131 }
132
133 Exit:
134 GXV_EXIT;
135 }
136
137
138 static void
139 gxv_feat_name_index_validate( FT_Bytes table,
140 FT_Bytes limit,
141 GXV_Validator valid )
142 {
143 FT_Bytes p = table;
144
145 FT_Short nameIndex;
146
147
148 GXV_NAME_ENTER( "nameIndex" );
149
150 GXV_LIMIT_CHECK( 2 );
151 nameIndex = FT_NEXT_SHORT ( p );
152 GXV_TRACE(( " (nameIndex = %d)\n", nameIndex ));
153
154 gxv_sfntName_validate( (FT_UShort)nameIndex,
155 255,
156 32768U,
157 valid );
158
159 GXV_EXIT;
160 }
161
162
163 static void
164 gxv_feat_setting_validate( FT_Bytes table,
165 FT_Bytes limit,
166 FT_Bool exclusive,
167 GXV_Validator valid )
168 {
169 FT_Bytes p = table;
170 FT_UShort setting;
171
172
173 GXV_NAME_ENTER( "setting" );
174
175 GXV_LIMIT_CHECK( 2 );
176
177 setting = FT_NEXT_USHORT( p );
178
179 /* If we have exclusive setting, the setting should be odd. */
180 if ( exclusive && ( setting % 2 ) == 0 )
181 FT_INVALID_DATA;
182
183 gxv_feat_name_index_validate( p, limit, valid );
184
185 GXV_FEAT_DATA( setting ) = setting;
186
187 GXV_EXIT;
188 }
189
190
191 static void
192 gxv_feat_name_validate( FT_Bytes table,
193 FT_Bytes limit,
194 GXV_Validator valid )
195 {
196 FT_Bytes p = table;
197 FT_UInt reserved_size = GXV_FEAT_DATA( reserved_size );
198
199 FT_UShort feature;
200 FT_UShort nSettings;
201 FT_UInt settingTable;
202 FT_UShort featureFlags;
203
204 FT_Bool exclusive;
205 FT_Int last_setting;
206 FT_UInt i;
207
208
209 GXV_NAME_ENTER( "name" );
210
211 /* feature + nSettings + settingTable + featureFlags */
212 GXV_LIMIT_CHECK( 2 + 2 + 4 + 2 );
213
214 feature = FT_NEXT_USHORT( p );
215 GXV_FEAT_DATA( feature ) = feature;
216
217 nSettings = FT_NEXT_USHORT( p );
218 settingTable = FT_NEXT_ULONG ( p );
219 featureFlags = FT_NEXT_USHORT( p );
220
221 if ( settingTable < reserved_size )
222 FT_INVALID_OFFSET;
223
224 if ( valid->root->level == FT_VALIDATE_PARANOID &&
225 ( featureFlags & GXV_FEAT_MASK_UNUSED ) == 0 )
226 FT_INVALID_DATA;
227
228 exclusive = FT_BOOL( featureFlags & GXV_FEAT_MASK_EXCLUSIVE_SETTINGS );
229 if ( exclusive )
230 {
231 FT_Byte dynamic_default;
232
233
234 if ( featureFlags & GXV_FEAT_MASK_DYNAMIC_DEFAULT )
235 dynamic_default = (FT_Byte)( featureFlags &
236 GXV_FEAT_MASK_DEFAULT_SETTING );
237 else
238 dynamic_default = 0;
239
240 /* If exclusive, check whether default setting is in the range. */
241 if ( !( dynamic_default < nSettings ) )
242 FT_INVALID_FORMAT;
243 }
244
245 gxv_feat_registry_validate( feature, nSettings, exclusive, valid );
246
247 gxv_feat_name_index_validate( p, limit, valid );
248
249 p = valid->root->base + settingTable;
250 for ( last_setting = -1, i = 0; i < nSettings; i++ )
251 {
252 gxv_feat_setting_validate( p, limit, exclusive, valid );
253
254 if ( valid->root->level == FT_VALIDATE_PARANOID &&
255 (FT_Int)GXV_FEAT_DATA( setting ) <= last_setting )
256 FT_INVALID_FORMAT;
257
258 last_setting = (FT_Int)GXV_FEAT_DATA( setting );
259 /* setting + nameIndex */
260 p += ( 2 + 2 );
261 }
262
263 GXV_EXIT;
264 }
265
266
267 /*************************************************************************/
268 /*************************************************************************/
269 /***** *****/
270 /***** feat TABLE *****/
271 /***** *****/
272 /*************************************************************************/
273 /*************************************************************************/
274
275 FT_LOCAL_DEF( void )
276 gxv_feat_validate( FT_Bytes table,
277 FT_Face face,
278 FT_Validator ftvalid )
279 {
280 GXV_ValidatorRec validrec;
281 GXV_Validator valid = &validrec;
282
283 GXV_feat_DataRec featrec;
284 GXV_feat_Data feat = &featrec;
285
286 FT_Bytes p = table;
287 FT_Bytes limit = 0;
288
289 FT_UInt featureNameCount;
290
291 FT_UInt i;
292 FT_Int last_feature;
293
294
295 valid->root = ftvalid;
296 valid->table_data = feat;
297 valid->face = face;
298
299 FT_TRACE3(( "validating `feat' table\n" ));
300 GXV_INIT;
301
302 feat->reserved_size = 0;
303
304 /* version + featureNameCount + none_0 + none_1 */
305 GXV_LIMIT_CHECK( 4 + 2 + 2 + 4 );
306 feat->reserved_size += 4 + 2 + 2 + 4;
307
308 if ( FT_NEXT_ULONG( p ) != 0x00010000UL ) /* Version */
309 FT_INVALID_FORMAT;
310
311 featureNameCount = FT_NEXT_USHORT( p );
312 GXV_TRACE(( " (featureNameCount = %d)\n", featureNameCount ));
313
314 if ( valid->root->level != FT_VALIDATE_PARANOID )
315 p += 6; /* skip (none) and (none) */
316 else
317 {
318 if ( FT_NEXT_USHORT( p ) != 0 )
319 FT_INVALID_DATA;
320
321 if ( FT_NEXT_ULONG( p ) != 0 )
322 FT_INVALID_DATA;
323 }
324
325 feat->reserved_size += featureNameCount * ( 2 + 2 + 4 + 2 + 2 );
326
327 for ( last_feature = -1, i = 0; i < featureNameCount; i++ )
328 {
329 gxv_feat_name_validate( p, limit, valid );
330
331 if ( valid->root->level == FT_VALIDATE_PARANOID &&
332 (FT_Int)GXV_FEAT_DATA( feature ) <= last_feature )
333 FT_INVALID_FORMAT;
334
335 last_feature = GXV_FEAT_DATA( feature );
336 p += 2 + 2 + 4 + 2 + 2;
337 }
338
339 FT_TRACE4(( "\n" ));
340 }
341
342
343 /* END */