[sfnt] Further generalize `sfnt_get_ps_name'; report invalid data. * src/sfnt/sfdriver.c (sfnt_ps_map): New array. (sfnt_is_postscript): New function. (char_type_func): New typedef. (get_win_string, get_apple_string): Add argument to specify character checking function. Add argument whether argument checking failures should be reported. Update callers. (search_name_id): Fix return value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
diff --git a/ChangeLog b/ChangeLog
index 8c81015..7076a6e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
2017-02-23 Werner Lemberg <wl@gnu.org>
+ [sfnt] Further generalize `sfnt_get_ps_name'; report invalid data.
+
+ * src/sfnt/sfdriver.c (sfnt_ps_map): New array.
+ (sfnt_is_postscript): New function.
+ (char_type_func): New typedef.
+ (get_win_string, get_apple_string): Add argument to specify
+ character checking function.
+ Add argument whether argument checking failures should be reported.
+ Update callers.
+ (search_name_id): Fix return value.
+
+2017-02-23 Werner Lemberg <wl@gnu.org>
+
[sfnt] Split off another bit of `sfnt_get_ps_name'.
* src/sfnt/sfdriver.c (sfnt_get_ps_name): Split off some
diff --git a/src/sfnt/sfdriver.c b/src/sfnt/sfdriver.c
index 5c70de9..274ef80 100644
--- a/src/sfnt/sfdriver.c
+++ b/src/sfnt/sfdriver.c
@@ -220,6 +220,39 @@
*
*/
+ /* an array representing allowed ASCII characters in a PS string */
+ static const unsigned char sfnt_ps_map[16] =
+ {
+ /* 4 0 C 8 */
+ 0x00, 0x00, /* 0x00: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 */
+ 0x00, 0x00, /* 0x10: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 */
+ 0xDE, 0x7C, /* 0x20: 1 1 0 1 1 1 1 0 0 1 1 1 1 1 0 0 */
+ 0xFF, 0xAF, /* 0x30: 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 */
+ 0xFF, 0xFF, /* 0x40: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 */
+ 0xFF, 0xD7, /* 0x50: 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 */
+ 0xFF, 0xFF, /* 0x60: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 */
+ 0xFF, 0x57 /* 0x70: 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 1 */
+ };
+
+
+ static int
+ sfnt_is_postscript( int c )
+ {
+ unsigned int cc;
+
+
+ if ( c < 0 || c >= 0x80 )
+ return 0;
+
+ cc = (unsigned int)c;
+
+ return sfnt_ps_map[cc >> 3] & ( 1 << ( cc & 0x07 ) );
+ }
+
+
+ typedef int (*char_type_func)( int c );
+
+
/* handling of PID/EID 3/0 and 3/1 is the same */
#define IS_WIN( n ) ( (n)->platformID == 3 && \
( (n)->encodingID == 1 || (n)->encodingID == 0 ) && \
@@ -230,9 +263,11 @@
(n)->languageID == 0 )
static const char*
- get_win_string( FT_Memory memory,
- FT_Stream stream,
- TT_Name entry )
+ get_win_string( FT_Memory memory,
+ FT_Stream stream,
+ TT_Name entry,
+ char_type_func char_type,
+ FT_Bool report_invalid_characters )
{
FT_Error error = FT_Err_Ok;
@@ -263,8 +298,22 @@
for ( len = entry->stringLength / 2; len > 0; len--, p += 2 )
{
- if ( p[0] == 0 && p[1] >= 32 )
- *r++ = p[1];
+ if ( p[0] == 0 )
+ {
+ if ( char_type( p[1] ) )
+ *r++ = p[1];
+ else
+ {
+ if ( report_invalid_characters )
+ {
+ FT_TRACE0(( "get_win_string:"
+ " Character `%c' (0x%X) invalid in PS name string\n",
+ p[1], p[1] ));
+ /* it's not the job of FreeType to correct PS names... */
+ *r++ = p[1];
+ }
+ }
+ }
}
*r = '\0';
@@ -275,13 +324,18 @@
static const char*
- get_apple_string( FT_Memory memory,
- FT_Stream stream,
- TT_Name entry )
+ get_apple_string( FT_Memory memory,
+ FT_Stream stream,
+ TT_Name entry,
+ char_type_func char_type,
+ FT_Bool report_invalid_characters )
{
FT_Error error = FT_Err_Ok;
const char* result;
+ FT_String* r;
+ FT_Char* p;
+ FT_UInt len;
FT_UNUSED( error );
@@ -289,8 +343,8 @@
if ( FT_ALLOC( result, entry->stringLength + 1 ) )
return NULL;
- if ( FT_STREAM_SEEK( entry->stringOffset ) ||
- FT_STREAM_READ( result, entry->stringLength ) )
+ if ( FT_STREAM_SEEK( entry->stringOffset ) ||
+ FT_FRAME_ENTER( entry->stringLength ) )
{
FT_FREE( result );
entry->stringOffset = 0;
@@ -300,7 +354,28 @@
return NULL;
}
- ((char*)result)[entry->stringLength] = '\0';
+ r = (FT_String*)result;
+ p = (FT_Char*)stream->cursor;
+
+ for ( len = entry->stringLength; len > 0; len--, p++ )
+ {
+ if ( char_type( *p ) )
+ *r++ = *p;
+ else
+ {
+ if ( report_invalid_characters )
+ {
+ FT_TRACE0(( "get_apple_string:"
+ " Character `%c' (0x%X) invalid in PS name string\n",
+ *p, *p ));
+ /* it's not the job of FreeType to correct PS names... */
+ *r++ = *p;
+ }
+ }
+ }
+ *r = '\0';
+
+ FT_FRAME_EXIT();
return result;
}
@@ -333,14 +408,14 @@
}
}
- return *win || *apple;
+ return ( *win >= 0 ) || ( *apple >= 0 );
}
static const char*
sfnt_get_ps_name( TT_Face face )
{
- FT_Int found_win, found_apple;
+ FT_Int found, win, apple;
const char* result = NULL;
@@ -349,17 +424,24 @@
/* scan the name table to see whether we have a Postscript name here, */
/* either in Macintosh or Windows platform encodings */
- search_name_id( face, 6, &found_win, &found_apple );
-
- /* prefer Windows entries over Apple */
- if ( found_win != -1 )
- result = get_win_string( face->root.memory,
- face->name_table.stream,
- face->name_table.names + found_win );
- else if ( found_apple != -1 )
- result = get_apple_string( face->root.memory,
+ found = search_name_id( face, 6, &win, &apple );
+
+ if ( found )
+ {
+ /* prefer Windows entries over Apple */
+ if ( win != -1 )
+ result = get_win_string( face->root.memory,
face->name_table.stream,
- face->name_table.names + found_apple );
+ face->name_table.names + win,
+ sfnt_is_postscript,
+ 1 );
+ else
+ result = get_apple_string( face->root.memory,
+ face->name_table.stream,
+ face->name_table.names + apple,
+ sfnt_is_postscript,
+ 1 );
+ }
face->postscript_name = result;