/src/freetype/src/gxvalid/gxvmorx2.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 327 lines · 222 code · 69 blank · 36 comment · 16 complexity · 743c6d0aa028ee72da62934b08517bce MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* gxvmorx2.c */
  4. /* */
  5. /* TrueTypeGX/AAT morx table validation */
  6. /* body for type2 (Ligature Substitution) subtable. */
  7. /* */
  8. /* Copyright 2005 by suzuki toshiya, Masatake YAMATO, Red Hat K.K., */
  9. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  10. /* */
  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. */
  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. #include "gxvmorx.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_gxvmorx
  34. typedef struct GXV_morx_subtable_type2_StateOptRec_
  35. {
  36. FT_ULong ligActionTable;
  37. FT_ULong componentTable;
  38. FT_ULong ligatureTable;
  39. FT_ULong ligActionTable_length;
  40. FT_ULong componentTable_length;
  41. FT_ULong ligatureTable_length;
  42. } GXV_morx_subtable_type2_StateOptRec,
  43. *GXV_morx_subtable_type2_StateOptRecData;
  44. #define GXV_MORX_SUBTABLE_TYPE2_HEADER_SIZE \
  45. ( GXV_XSTATETABLE_HEADER_SIZE + 4 + 4 + 4 )
  46. static void
  47. gxv_morx_subtable_type2_opttable_load( FT_Bytes table,
  48. FT_Bytes limit,
  49. GXV_Validator valid )
  50. {
  51. FT_Bytes p = table;
  52. GXV_morx_subtable_type2_StateOptRecData optdata =
  53. (GXV_morx_subtable_type2_StateOptRecData)valid->xstatetable.optdata;
  54. GXV_LIMIT_CHECK( 4 + 4 + 4 );
  55. optdata->ligActionTable = FT_NEXT_ULONG( p );
  56. optdata->componentTable = FT_NEXT_ULONG( p );
  57. optdata->ligatureTable = FT_NEXT_ULONG( p );
  58. GXV_TRACE(( "offset to ligActionTable=0x%08x\n",
  59. optdata->ligActionTable ));
  60. GXV_TRACE(( "offset to componentTable=0x%08x\n",
  61. optdata->componentTable ));
  62. GXV_TRACE(( "offset to ligatureTable=0x%08x\n",
  63. optdata->ligatureTable ));
  64. }
  65. static void
  66. gxv_morx_subtable_type2_subtable_setup( FT_ULong table_size,
  67. FT_ULong classTable,
  68. FT_ULong stateArray,
  69. FT_ULong entryTable,
  70. FT_ULong* classTable_length_p,
  71. FT_ULong* stateArray_length_p,
  72. FT_ULong* entryTable_length_p,
  73. GXV_Validator valid )
  74. {
  75. FT_ULong o[6];
  76. FT_ULong* l[6];
  77. FT_ULong buff[7];
  78. GXV_morx_subtable_type2_StateOptRecData optdata =
  79. (GXV_morx_subtable_type2_StateOptRecData)valid->xstatetable.optdata;
  80. GXV_NAME_ENTER( "subtable boundaries setup" );
  81. o[0] = classTable;
  82. o[1] = stateArray;
  83. o[2] = entryTable;
  84. o[3] = optdata->ligActionTable;
  85. o[4] = optdata->componentTable;
  86. o[5] = optdata->ligatureTable;
  87. l[0] = classTable_length_p;
  88. l[1] = stateArray_length_p;
  89. l[2] = entryTable_length_p;
  90. l[3] = &(optdata->ligActionTable_length);
  91. l[4] = &(optdata->componentTable_length);
  92. l[5] = &(optdata->ligatureTable_length);
  93. gxv_set_length_by_ulong_offset( o, l, buff, 6, table_size, valid );
  94. GXV_TRACE(( "classTable: offset=0x%08x length=0x%08x\n",
  95. classTable, *classTable_length_p ));
  96. GXV_TRACE(( "stateArray: offset=0x%08x length=0x%08x\n",
  97. stateArray, *stateArray_length_p ));
  98. GXV_TRACE(( "entryTable: offset=0x%08x length=0x%08x\n",
  99. entryTable, *entryTable_length_p ));
  100. GXV_TRACE(( "ligActionTable: offset=0x%08x length=0x%08x\n",
  101. optdata->ligActionTable,
  102. optdata->ligActionTable_length ));
  103. GXV_TRACE(( "componentTable: offset=0x%08x length=0x%08x\n",
  104. optdata->componentTable,
  105. optdata->componentTable_length ));
  106. GXV_TRACE(( "ligatureTable: offset=0x%08x length=0x%08x\n",
  107. optdata->ligatureTable,
  108. optdata->ligatureTable_length ));
  109. GXV_EXIT;
  110. }
  111. #define GXV_MORX_LIGACTION_ENTRY_SIZE 4
  112. static void
  113. gxv_morx_subtable_type2_ligActionIndex_validate(
  114. FT_Bytes table,
  115. FT_UShort ligActionIndex,
  116. GXV_Validator valid )
  117. {
  118. /* access ligActionTable */
  119. GXV_morx_subtable_type2_StateOptRecData optdata =
  120. (GXV_morx_subtable_type2_StateOptRecData)valid->xstatetable.optdata;
  121. FT_Bytes lat_base = table + optdata->ligActionTable;
  122. FT_Bytes p = lat_base +
  123. ligActionIndex * GXV_MORX_LIGACTION_ENTRY_SIZE;
  124. FT_Bytes lat_limit = lat_base + optdata->ligActionTable;
  125. if ( p < lat_base )
  126. {
  127. GXV_TRACE(( "p < lat_base (%d byte rewind)\n", lat_base - p ));
  128. FT_INVALID_OFFSET;
  129. }
  130. else if ( lat_limit < p )
  131. {
  132. GXV_TRACE(( "lat_limit < p (%d byte overrun)\n", p - lat_limit ));
  133. FT_INVALID_OFFSET;
  134. }
  135. {
  136. /* validate entry in ligActionTable */
  137. FT_ULong lig_action;
  138. #ifdef GXV_LOAD_UNUSED_VARS
  139. FT_UShort last;
  140. FT_UShort store;
  141. #endif
  142. FT_ULong offset;
  143. FT_Long gid_limit;
  144. lig_action = FT_NEXT_ULONG( p );
  145. #ifdef GXV_LOAD_UNUSED_VARS
  146. last = (FT_UShort)( ( lig_action >> 31 ) & 1 );
  147. store = (FT_UShort)( ( lig_action >> 30 ) & 1 );
  148. #endif
  149. offset = lig_action & 0x3FFFFFFFUL;
  150. /* this offset is 30-bit signed value to add to GID */
  151. /* it is different from the location offset in mort */
  152. if ( ( offset & 0x3FFF0000UL ) == 0x3FFF0000UL )
  153. { /* negative offset */
  154. gid_limit = valid->face->num_glyphs - ( offset & 0x0000FFFFUL );
  155. if ( gid_limit > 0 )
  156. return;
  157. GXV_TRACE(( "ligature action table includes"
  158. " too negative offset moving all GID"
  159. " below defined range: 0x%04x\n",
  160. offset & 0xFFFFU ));
  161. GXV_SET_ERR_IF_PARANOID( FT_INVALID_OFFSET );
  162. }
  163. else if ( ( offset & 0x3FFF0000UL ) == 0x0000000UL )
  164. { /* positive offset */
  165. if ( (FT_Long)offset < valid->face->num_glyphs )
  166. return;
  167. GXV_TRACE(( "ligature action table includes"
  168. " too large offset moving all GID"
  169. " over defined range: 0x%04x\n",
  170. offset & 0xFFFFU ));
  171. GXV_SET_ERR_IF_PARANOID( FT_INVALID_OFFSET );
  172. }
  173. GXV_TRACE(( "ligature action table includes"
  174. " invalid offset to add to 16-bit GID:"
  175. " 0x%08x\n", offset ));
  176. GXV_SET_ERR_IF_PARANOID( FT_INVALID_OFFSET );
  177. }
  178. }
  179. static void
  180. gxv_morx_subtable_type2_entry_validate(
  181. FT_UShort state,
  182. FT_UShort flags,
  183. GXV_StateTable_GlyphOffsetCPtr glyphOffset_p,
  184. FT_Bytes table,
  185. FT_Bytes limit,
  186. GXV_Validator valid )
  187. {
  188. #ifdef GXV_LOAD_UNUSED_VARS
  189. FT_UShort setComponent;
  190. FT_UShort dontAdvance;
  191. FT_UShort performAction;
  192. #endif
  193. FT_UShort reserved;
  194. FT_UShort ligActionIndex;
  195. FT_UNUSED( state );
  196. FT_UNUSED( limit );
  197. #ifdef GXV_LOAD_UNUSED_VARS
  198. setComponent = (FT_UShort)( ( flags >> 15 ) & 1 );
  199. dontAdvance = (FT_UShort)( ( flags >> 14 ) & 1 );
  200. performAction = (FT_UShort)( ( flags >> 13 ) & 1 );
  201. #endif
  202. reserved = (FT_UShort)( flags & 0x1FFF );
  203. ligActionIndex = glyphOffset_p->u;
  204. if ( reserved > 0 )
  205. GXV_TRACE(( " reserved 14bit is non-zero\n" ));
  206. if ( 0 < ligActionIndex )
  207. gxv_morx_subtable_type2_ligActionIndex_validate(
  208. table, ligActionIndex, valid );
  209. }
  210. static void
  211. gxv_morx_subtable_type2_ligatureTable_validate( FT_Bytes table,
  212. GXV_Validator valid )
  213. {
  214. GXV_morx_subtable_type2_StateOptRecData optdata =
  215. (GXV_morx_subtable_type2_StateOptRecData)valid->xstatetable.optdata;
  216. FT_Bytes p = table + optdata->ligatureTable;
  217. FT_Bytes limit = table + optdata->ligatureTable
  218. + optdata->ligatureTable_length;
  219. GXV_NAME_ENTER( "morx chain subtable type2 - substitutionTable" );
  220. if ( 0 != optdata->ligatureTable )
  221. {
  222. /* Apple does not give specification of ligatureTable format */
  223. while ( p < limit )
  224. {
  225. FT_UShort lig_gid;
  226. GXV_LIMIT_CHECK( 2 );
  227. lig_gid = FT_NEXT_USHORT( p );
  228. if ( lig_gid < valid->face->num_glyphs )
  229. GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
  230. }
  231. }
  232. GXV_EXIT;
  233. }
  234. FT_LOCAL_DEF( void )
  235. gxv_morx_subtable_type2_validate( FT_Bytes table,
  236. FT_Bytes limit,
  237. GXV_Validator valid )
  238. {
  239. FT_Bytes p = table;
  240. GXV_morx_subtable_type2_StateOptRec lig_rec;
  241. GXV_NAME_ENTER( "morx chain subtable type2 (Ligature Substitution)" );
  242. GXV_LIMIT_CHECK( GXV_MORX_SUBTABLE_TYPE2_HEADER_SIZE );
  243. valid->xstatetable.optdata =
  244. &lig_rec;
  245. valid->xstatetable.optdata_load_func =
  246. gxv_morx_subtable_type2_opttable_load;
  247. valid->xstatetable.subtable_setup_func =
  248. gxv_morx_subtable_type2_subtable_setup;
  249. valid->xstatetable.entry_glyphoffset_fmt =
  250. GXV_GLYPHOFFSET_USHORT;
  251. valid->xstatetable.entry_validate_func =
  252. gxv_morx_subtable_type2_entry_validate;
  253. gxv_XStateTable_validate( p, limit, valid );
  254. p += valid->subtable_length;
  255. gxv_morx_subtable_type2_ligatureTable_validate( table, valid );
  256. GXV_EXIT;
  257. }
  258. /* END */