/src/freetype/src/gxvalid/gxvmort.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 296 lines · 196 code · 62 blank · 38 comment · 18 complexity · 69011460b18cd064a3b0be7590053c54 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. GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA );
  44. }
  45. else if ( !gxv_feat_registry[f->featureType].existence )
  46. {
  47. GXV_TRACE(( "featureType %d is within registered area "
  48. "but undefined, setting %d is unchecked\n",
  49. f->featureType, f->featureSetting ));
  50. GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA );
  51. }
  52. else
  53. {
  54. FT_Byte nSettings_max;
  55. /* nSettings in gxvfeat.c is halved for exclusive on/off settings */
  56. nSettings_max = gxv_feat_registry[f->featureType].nSettings;
  57. if ( gxv_feat_registry[f->featureType].exclusive )
  58. nSettings_max = (FT_Byte)( 2 * nSettings_max );
  59. GXV_TRACE(( "featureType %d is registered", f->featureType ));
  60. GXV_TRACE(( "setting %d", f->featureSetting ));
  61. if ( f->featureSetting > nSettings_max )
  62. {
  63. GXV_TRACE(( "out of defined range %d", nSettings_max ));
  64. GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA );
  65. }
  66. GXV_TRACE(( "\n" ));
  67. }
  68. /* TODO: enableFlags must be unique value in specified chain? */
  69. }
  70. /*
  71. * nFeatureFlags is typed to FT_ULong to accept that in
  72. * mort (typed FT_UShort) and morx (typed FT_ULong).
  73. */
  74. FT_LOCAL_DEF( void )
  75. gxv_mort_featurearray_validate( FT_Bytes table,
  76. FT_Bytes limit,
  77. FT_ULong nFeatureFlags,
  78. GXV_Validator valid )
  79. {
  80. FT_Bytes p = table;
  81. FT_ULong i;
  82. GXV_mort_featureRec f = GXV_MORT_FEATURE_OFF;
  83. GXV_NAME_ENTER( "mort feature list" );
  84. for ( i = 0; i < nFeatureFlags; i++ )
  85. {
  86. GXV_LIMIT_CHECK( 2 + 2 + 4 + 4 );
  87. f.featureType = FT_NEXT_USHORT( p );
  88. f.featureSetting = FT_NEXT_USHORT( p );
  89. f.enableFlags = FT_NEXT_ULONG( p );
  90. f.disableFlags = FT_NEXT_ULONG( p );
  91. gxv_mort_feature_validate( &f, valid );
  92. }
  93. if ( !IS_GXV_MORT_FEATURE_OFF( f ) )
  94. FT_INVALID_DATA;
  95. valid->subtable_length = p - table;
  96. GXV_EXIT;
  97. }
  98. FT_LOCAL_DEF( void )
  99. gxv_mort_coverage_validate( FT_UShort coverage,
  100. GXV_Validator valid )
  101. {
  102. FT_UNUSED( valid );
  103. if ( coverage & 0x8000U )
  104. GXV_TRACE(( " this subtable is for vertical text only\n" ));
  105. else
  106. GXV_TRACE(( " this subtable is for horizontal text only\n" ));
  107. if ( coverage & 0x4000 )
  108. GXV_TRACE(( " this subtable is applied to glyph array "
  109. "in descending order\n" ));
  110. else
  111. GXV_TRACE(( " this subtable is applied to glyph array "
  112. "in ascending order\n" ));
  113. if ( coverage & 0x2000 )
  114. GXV_TRACE(( " this subtable is forcibly applied to "
  115. "vertical/horizontal text\n" ));
  116. if ( coverage & 0x1FF8 )
  117. GXV_TRACE(( " coverage has non-zero bits in reserved area\n" ));
  118. }
  119. static void
  120. gxv_mort_subtables_validate( FT_Bytes table,
  121. FT_Bytes limit,
  122. FT_UShort nSubtables,
  123. GXV_Validator valid )
  124. {
  125. FT_Bytes p = table;
  126. GXV_Validate_Func fmt_funcs_table[] =
  127. {
  128. gxv_mort_subtable_type0_validate, /* 0 */
  129. gxv_mort_subtable_type1_validate, /* 1 */
  130. gxv_mort_subtable_type2_validate, /* 2 */
  131. NULL, /* 3 */
  132. gxv_mort_subtable_type4_validate, /* 4 */
  133. gxv_mort_subtable_type5_validate, /* 5 */
  134. };
  135. GXV_Validate_Func func;
  136. FT_UShort i;
  137. GXV_NAME_ENTER( "subtables in a chain" );
  138. for ( i = 0; i < nSubtables; i++ )
  139. {
  140. FT_UShort length;
  141. FT_UShort coverage;
  142. #ifdef GXV_LOAD_UNUSED_VARS
  143. FT_ULong subFeatureFlags;
  144. #endif
  145. FT_UInt type;
  146. FT_UInt rest;
  147. GXV_LIMIT_CHECK( 2 + 2 + 4 );
  148. length = FT_NEXT_USHORT( p );
  149. coverage = FT_NEXT_USHORT( p );
  150. #ifdef GXV_LOAD_UNUSED_VARS
  151. subFeatureFlags = FT_NEXT_ULONG( p );
  152. #else
  153. p += 4;
  154. #endif
  155. GXV_TRACE(( "validating chain subtable %d/%d (%d bytes)\n",
  156. i + 1, nSubtables, length ));
  157. type = coverage & 0x0007;
  158. rest = length - ( 2 + 2 + 4 );
  159. GXV_LIMIT_CHECK( rest );
  160. gxv_mort_coverage_validate( coverage, valid );
  161. if ( type > 5 )
  162. FT_INVALID_FORMAT;
  163. func = fmt_funcs_table[type];
  164. if ( func == NULL )
  165. GXV_TRACE(( "morx type %d is reserved\n", type ));
  166. func( p, p + rest, valid );
  167. p += rest;
  168. /* TODO: validate subFeatureFlags */
  169. }
  170. valid->subtable_length = p - table;
  171. GXV_EXIT;
  172. }
  173. static void
  174. gxv_mort_chain_validate( FT_Bytes table,
  175. FT_Bytes limit,
  176. GXV_Validator valid )
  177. {
  178. FT_Bytes p = table;
  179. #ifdef GXV_LOAD_UNUSED_VARS
  180. FT_ULong defaultFlags;
  181. #endif
  182. FT_ULong chainLength;
  183. FT_UShort nFeatureFlags;
  184. FT_UShort nSubtables;
  185. GXV_NAME_ENTER( "mort chain header" );
  186. GXV_LIMIT_CHECK( 4 + 4 + 2 + 2 );
  187. #ifdef GXV_LOAD_UNUSED_VARS
  188. defaultFlags = FT_NEXT_ULONG( p );
  189. #else
  190. p += 4;
  191. #endif
  192. chainLength = FT_NEXT_ULONG( p );
  193. nFeatureFlags = FT_NEXT_USHORT( p );
  194. nSubtables = FT_NEXT_USHORT( p );
  195. gxv_mort_featurearray_validate( p, table + chainLength,
  196. nFeatureFlags, valid );
  197. p += valid->subtable_length;
  198. gxv_mort_subtables_validate( p, table + chainLength, nSubtables, valid );
  199. valid->subtable_length = chainLength;
  200. /* TODO: validate defaultFlags */
  201. GXV_EXIT;
  202. }
  203. FT_LOCAL_DEF( void )
  204. gxv_mort_validate( FT_Bytes table,
  205. FT_Face face,
  206. FT_Validator ftvalid )
  207. {
  208. GXV_ValidatorRec validrec;
  209. GXV_Validator valid = &validrec;
  210. FT_Bytes p = table;
  211. FT_Bytes limit = 0;
  212. FT_ULong version;
  213. FT_ULong nChains;
  214. FT_ULong i;
  215. valid->root = ftvalid;
  216. valid->face = face;
  217. limit = valid->root->limit;
  218. FT_TRACE3(( "validating `mort' table\n" ));
  219. GXV_INIT;
  220. GXV_LIMIT_CHECK( 4 + 4 );
  221. version = FT_NEXT_ULONG( p );
  222. nChains = FT_NEXT_ULONG( p );
  223. if (version != 0x00010000UL)
  224. FT_INVALID_FORMAT;
  225. for ( i = 0; i < nChains; i++ )
  226. {
  227. GXV_TRACE(( "validating chain %d/%d\n", i + 1, nChains ));
  228. GXV_32BIT_ALIGNMENT_VALIDATE( p - table );
  229. gxv_mort_chain_validate( p, limit, valid );
  230. p += valid->subtable_length;
  231. }
  232. FT_TRACE4(( "\n" ));
  233. }
  234. /* END */