/modules/freetype2/src/gxvalid/gxvmort.c

http://github.com/zpao/v8monkey · C · 285 lines · 187 code · 62 blank · 36 comment · 21 complexity · cef81604193e880b819f61ef149c23d7 MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* gxvmort.c */
  4. /* */
  5. /* TrueTypeGX/AAT mort table validation (body). */
  6. /* */
  7. /* Copyright 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. /* gxvalid is derived from both gxlayout module and otvalid module. */
  20. /* Development of gxlayout is supported by the Information-technology */
  21. /* Promotion Agency(IPA), Japan. */
  22. /* */
  23. /***************************************************************************/
  24. #include "gxvmort.h"
  25. #include "gxvfeat.h"
  26. /*************************************************************************/
  27. /* */
  28. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  29. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  30. /* messages during execution. */
  31. /* */
  32. #undef FT_COMPONENT
  33. #define FT_COMPONENT trace_gxvmort
  34. static void
  35. gxv_mort_feature_validate( GXV_mort_feature f,
  36. GXV_Validator valid )
  37. {
  38. if ( f->featureType >= gxv_feat_registry_length )
  39. {
  40. GXV_TRACE(( "featureType %d is out of registered range, "
  41. "setting %d is unchecked\n",
  42. f->featureType, f->featureSetting ));
  43. if ( valid->root->level >= FT_VALIDATE_PARANOID )
  44. FT_INVALID_DATA;
  45. }
  46. else if ( !gxv_feat_registry[f->featureType].existence )
  47. {
  48. GXV_TRACE(( "featureType %d is within registered area "
  49. "but undefined, setting %d is unchecked\n",
  50. f->featureType, f->featureSetting ));
  51. if ( valid->root->level >= FT_VALIDATE_PARANOID )
  52. FT_INVALID_DATA;
  53. }
  54. else
  55. {
  56. FT_Byte nSettings_max;
  57. /* nSettings in gxvfeat.c is halved for exclusive on/off settings */
  58. nSettings_max = gxv_feat_registry[f->featureType].nSettings;
  59. if ( gxv_feat_registry[f->featureType].exclusive )
  60. nSettings_max = (FT_Byte)( 2 * nSettings_max );
  61. GXV_TRACE(( "featureType %d is registered", f->featureType ));
  62. GXV_TRACE(( "setting %d", f->featureSetting ));
  63. if ( f->featureSetting > nSettings_max )
  64. {
  65. GXV_TRACE(( "out of defined range %d", nSettings_max ));
  66. if ( valid->root->level >= FT_VALIDATE_PARANOID )
  67. FT_INVALID_DATA;
  68. }
  69. GXV_TRACE(( "\n" ));
  70. }
  71. /* TODO: enableFlags must be unique value in specified chain? */
  72. }
  73. /*
  74. * nFeatureFlags is typed to FT_ULong to accept that in
  75. * mort (typed FT_UShort) and morx (typed FT_ULong).
  76. */
  77. FT_LOCAL_DEF( void )
  78. gxv_mort_featurearray_validate( FT_Bytes table,
  79. FT_Bytes limit,
  80. FT_ULong nFeatureFlags,
  81. GXV_Validator valid )
  82. {
  83. FT_Bytes p = table;
  84. FT_ULong i;
  85. GXV_mort_featureRec f = GXV_MORT_FEATURE_OFF;
  86. GXV_NAME_ENTER( "mort feature list" );
  87. for ( i = 0; i < nFeatureFlags; i++ )
  88. {
  89. GXV_LIMIT_CHECK( 2 + 2 + 4 + 4 );
  90. f.featureType = FT_NEXT_USHORT( p );
  91. f.featureSetting = FT_NEXT_USHORT( p );
  92. f.enableFlags = FT_NEXT_ULONG( p );
  93. f.disableFlags = FT_NEXT_ULONG( p );
  94. gxv_mort_feature_validate( &f, valid );
  95. }
  96. if ( !IS_GXV_MORT_FEATURE_OFF( f ) )
  97. FT_INVALID_DATA;
  98. valid->subtable_length = p - table;
  99. GXV_EXIT;
  100. }
  101. FT_LOCAL_DEF( void )
  102. gxv_mort_coverage_validate( FT_UShort coverage,
  103. GXV_Validator valid )
  104. {
  105. FT_UNUSED( valid );
  106. if ( coverage & 0x8000U )
  107. GXV_TRACE(( " this subtable is for vertical text only\n" ));
  108. else
  109. GXV_TRACE(( " this subtable is for horizontal text only\n" ));
  110. if ( coverage & 0x4000 )
  111. GXV_TRACE(( " this subtable is applied to glyph array "
  112. "in descending order\n" ));
  113. else
  114. GXV_TRACE(( " this subtable is applied to glyph array "
  115. "in ascending order\n" ));
  116. if ( coverage & 0x2000 )
  117. GXV_TRACE(( " this subtable is forcibly applied to "
  118. "vertical/horizontal text\n" ));
  119. if ( coverage & 0x1FF8 )
  120. GXV_TRACE(( " coverage has non-zero bits in reserved area\n" ));
  121. }
  122. static void
  123. gxv_mort_subtables_validate( FT_Bytes table,
  124. FT_Bytes limit,
  125. FT_UShort nSubtables,
  126. GXV_Validator valid )
  127. {
  128. FT_Bytes p = table;
  129. GXV_Validate_Func fmt_funcs_table[] =
  130. {
  131. gxv_mort_subtable_type0_validate, /* 0 */
  132. gxv_mort_subtable_type1_validate, /* 1 */
  133. gxv_mort_subtable_type2_validate, /* 2 */
  134. NULL, /* 3 */
  135. gxv_mort_subtable_type4_validate, /* 4 */
  136. gxv_mort_subtable_type5_validate, /* 5 */
  137. };
  138. GXV_Validate_Func func;
  139. FT_UShort i;
  140. GXV_NAME_ENTER( "subtables in a chain" );
  141. for ( i = 0; i < nSubtables; i++ )
  142. {
  143. FT_UShort length;
  144. FT_UShort coverage;
  145. FT_ULong subFeatureFlags;
  146. FT_UInt type;
  147. FT_UInt rest;
  148. GXV_LIMIT_CHECK( 2 + 2 + 4 );
  149. length = FT_NEXT_USHORT( p );
  150. coverage = FT_NEXT_USHORT( p );
  151. subFeatureFlags = FT_NEXT_ULONG( p );
  152. GXV_TRACE(( "validating chain subtable %d/%d (%d bytes)\n",
  153. i + 1, nSubtables, length ));
  154. type = coverage & 0x0007;
  155. rest = length - ( 2 + 2 + 4 );
  156. GXV_LIMIT_CHECK( rest );
  157. gxv_mort_coverage_validate( coverage, valid );
  158. if ( type > 5 )
  159. FT_INVALID_FORMAT;
  160. func = fmt_funcs_table[type];
  161. if ( func == NULL )
  162. GXV_TRACE(( "morx type %d is reserved\n", type ));
  163. func( p, p + rest, valid );
  164. p += rest;
  165. }
  166. valid->subtable_length = p - table;
  167. GXV_EXIT;
  168. }
  169. static void
  170. gxv_mort_chain_validate( FT_Bytes table,
  171. FT_Bytes limit,
  172. GXV_Validator valid )
  173. {
  174. FT_Bytes p = table;
  175. FT_ULong defaultFlags;
  176. FT_ULong chainLength;
  177. FT_UShort nFeatureFlags;
  178. FT_UShort nSubtables;
  179. GXV_NAME_ENTER( "mort chain header" );
  180. GXV_LIMIT_CHECK( 4 + 4 + 2 + 2 );
  181. defaultFlags = FT_NEXT_ULONG( p );
  182. chainLength = FT_NEXT_ULONG( p );
  183. nFeatureFlags = FT_NEXT_USHORT( p );
  184. nSubtables = FT_NEXT_USHORT( p );
  185. gxv_mort_featurearray_validate( p, table + chainLength,
  186. nFeatureFlags, valid );
  187. p += valid->subtable_length;
  188. gxv_mort_subtables_validate( p, table + chainLength, nSubtables, valid );
  189. valid->subtable_length = chainLength;
  190. GXV_EXIT;
  191. }
  192. FT_LOCAL_DEF( void )
  193. gxv_mort_validate( FT_Bytes table,
  194. FT_Face face,
  195. FT_Validator ftvalid )
  196. {
  197. GXV_ValidatorRec validrec;
  198. GXV_Validator valid = &validrec;
  199. FT_Bytes p = table;
  200. FT_Bytes limit = 0;
  201. FT_ULong version;
  202. FT_ULong nChains;
  203. FT_ULong i;
  204. valid->root = ftvalid;
  205. valid->face = face;
  206. limit = valid->root->limit;
  207. FT_TRACE3(( "validating `mort' table\n" ));
  208. GXV_INIT;
  209. GXV_LIMIT_CHECK( 4 + 4 );
  210. version = FT_NEXT_ULONG( p );
  211. nChains = FT_NEXT_ULONG( p );
  212. if (version != 0x00010000UL)
  213. FT_INVALID_FORMAT;
  214. for ( i = 0; i < nChains; i++ )
  215. {
  216. GXV_TRACE(( "validating chain %d/%d\n", i + 1, nChains ));
  217. GXV_32BIT_ALIGNMENT_VALIDATE( p - table );
  218. gxv_mort_chain_validate( p, limit, valid );
  219. p += valid->subtable_length;
  220. }
  221. FT_TRACE4(( "\n" ));
  222. }
  223. /* END */