1 /**************************************************************************** 2 * 3 * ftvalid.h 4 * 5 * FreeType validation support (specification). 6 * 7 * Copyright (C) 2004-2020 by 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 #ifndef FTVALID_H_ 20 #define FTVALID_H_ 21 22 #include <ft2build.h> 23 #include FT_CONFIG_STANDARD_LIBRARY_H /* for ft_jmpbuf */ 24 25 #include "compiler-macros.h" 26 27 FT_BEGIN_HEADER 28 29 30 /*************************************************************************/ 31 /*************************************************************************/ 32 /*************************************************************************/ 33 /**** ****/ 34 /**** ****/ 35 /**** V A L I D A T I O N ****/ 36 /**** ****/ 37 /**** ****/ 38 /*************************************************************************/ 39 /*************************************************************************/ 40 /*************************************************************************/ 41 42 /* handle to a validation object */ 43 typedef struct FT_ValidatorRec_ volatile* FT_Validator; 44 45 46 /************************************************************************** 47 * 48 * There are three distinct validation levels defined here: 49 * 50 * FT_VALIDATE_DEFAULT :: 51 * A table that passes this validation level can be used reliably by 52 * FreeType. It generally means that all offsets have been checked to 53 * prevent out-of-bound reads, that array counts are correct, etc. 54 * 55 * FT_VALIDATE_TIGHT :: 56 * A table that passes this validation level can be used reliably and 57 * doesn't contain invalid data. For example, a charmap table that 58 * returns invalid glyph indices will not pass, even though it can be 59 * used with FreeType in default mode (the library will simply return an 60 * error later when trying to load the glyph). 61 * 62 * It also checks that fields which must be a multiple of 2, 4, or 8, 63 * don't have incorrect values, etc. 64 * 65 * FT_VALIDATE_PARANOID :: 66 * Only for font debugging. Checks that a table follows the 67 * specification by 100%. Very few fonts will be able to pass this level 68 * anyway but it can be useful for certain tools like font 69 * editors/converters. 70 */ 71 typedef enum FT_ValidationLevel_ 72 { 73 FT_VALIDATE_DEFAULT = 0, 74 FT_VALIDATE_TIGHT, 75 FT_VALIDATE_PARANOID 76 77 } FT_ValidationLevel; 78 79 80 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */ 81 /* We disable the warning `structure was padded due to */ 82 /* __declspec(align())' in order to compile cleanly with */ 83 /* the maximum level of warnings. */ 84 #pragma warning( push ) 85 #pragma warning( disable : 4324 ) 86 #endif /* _MSC_VER */ 87 88 /* validator structure */ 89 typedef struct FT_ValidatorRec_ 90 { 91 ft_jmp_buf jump_buffer; /* used for exception handling */ 92 93 const FT_Byte* base; /* address of table in memory */ 94 const FT_Byte* limit; /* `base' + sizeof(table) in memory */ 95 FT_ValidationLevel level; /* validation level */ 96 FT_Error error; /* error returned. 0 means success */ 97 98 } FT_ValidatorRec; 99 100 #if defined( _MSC_VER ) 101 #pragma warning( pop ) 102 #endif 103 104 #define FT_VALIDATOR( x ) ( (FT_Validator)( x ) ) 105 106 107 FT_BASE( void ) 108 ft_validator_init( FT_Validator valid, 109 const FT_Byte* base, 110 const FT_Byte* limit, 111 FT_ValidationLevel level ); 112 113 /* Do not use this. It's broken and will cause your validator to crash */ 114 /* if you run it on an invalid font. */ 115 FT_BASE( FT_Int ) 116 ft_validator_run( FT_Validator valid ); 117 118 /* Sets the error field in a validator, then calls `longjmp' to return */ 119 /* to high-level caller. Using `setjmp/longjmp' avoids many stupid */ 120 /* error checks within the validation routines. */ 121 /* */ 122 FT_BASE( void ) 123 ft_validator_error( FT_Validator valid, 124 FT_Error error ); 125 126 127 /* Calls ft_validate_error. Assumes that the `valid' local variable */ 128 /* holds a pointer to the current validator object. */ 129 /* */ 130 #define FT_INVALID( _error ) FT_INVALID_( _error ) 131 #define FT_INVALID_( _error ) \ 132 ft_validator_error( valid, FT_THROW( _error ) ) 133 134 /* called when a broken table is detected */ 135 #define FT_INVALID_TOO_SHORT \ 136 FT_INVALID( Invalid_Table ) 137 138 /* called when an invalid offset is detected */ 139 #define FT_INVALID_OFFSET \ 140 FT_INVALID( Invalid_Offset ) 141 142 /* called when an invalid format/value is detected */ 143 #define FT_INVALID_FORMAT \ 144 FT_INVALID( Invalid_Table ) 145 146 /* called when an invalid glyph index is detected */ 147 #define FT_INVALID_GLYPH_ID \ 148 FT_INVALID( Invalid_Glyph_Index ) 149 150 /* called when an invalid field value is detected */ 151 #define FT_INVALID_DATA \ 152 FT_INVALID( Invalid_Table ) 153 154 155 FT_END_HEADER 156 157 #endif /* FTVALID_H_ */ 158 159 160 /* END */ 161