Commit ab1604bd6bbe80e69358a35c5fd27b97b340700c

Werner Lemberg 2007-06-16T07:59:39

* src/winfonts/winfnt.h: Add necessary structures for PE resource parsing. (WinPE32_HeaderRec): New structure. (WinPE32_SectionRec): New structure. (WinPE_RsrcDirRec): New structure. (WinPE_RsrcDirEntryRec): New structure. (WinPE_RsrcDataEntryRec): New structure. (FNT_FontRec): Remove unused `size_shift' field. * src/winfonts/winfnt.c (fnt_face_get_dll_font): Add support for loading bitmap .fon files in PE format.

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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
diff --git a/ChangeLog b/ChangeLog
index 3872fe0..b5b2fdc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2006-06-16  Dmitry Timoshkov  <dmitry@codeweavers.com>
+
+	* src/winfonts/winfnt.h: Add necessary structures for PE resource
+	parsing.
+	(WinPE32_HeaderRec): New structure.
+	(WinPE32_SectionRec): New structure.
+	(WinPE_RsrcDirRec): New structure.
+	(WinPE_RsrcDirEntryRec): New structure.
+	(WinPE_RsrcDataEntryRec): New structure.
+	(FNT_FontRec): Remove unused `size_shift' field.
+
+	* src/winfonts/winfnt.c (fnt_face_get_dll_font): Add support for
+	loading bitmap .fon files in PE format.
+
 2006-06-15  Dmitry Timoshkov  <dmitry@codeweavers.com>
 
 	* builds/win32/ftdebug.c: Unify debug level handling with other
diff --git a/ChangeLog.21 b/ChangeLog.21
index 1d8bb30..3a1bcf0 100644
--- a/ChangeLog.21
+++ b/ChangeLog.21
@@ -6391,7 +6391,7 @@
 	* include/freetype/internal/fnttypes.h (WinFNT_HeaderRec): Increase
 	size of `reserved2' to avoid memory overwrites.
 
-2003-01-08  Huw Dawies  <huw@codeweavers.com>
+2003-01-08  Huw Davies  <huw@codeweavers.com>
 
 	* src/winfonts/winfnt.c (winfnt_header_fields): Read 16 bytes into
 	`reserved2', not `reserved'.
diff --git a/docs/CHANGES b/docs/CHANGES
index 6a2d7a6..c744cd6 100644
--- a/docs/CHANGES
+++ b/docs/CHANGES
@@ -32,6 +32,9 @@ CHANGES BETWEEN 2.3.5 and 2.3.4
       out  whether  a  given  TrueType  font  uses  patented  bytecode
       instructions.
 
+    - Support for Windows FON files in PE format  has been contributed
+      by Dmitry Timoshkov.
+
 
   III. MISCELLANEOUS
 
diff --git a/src/winfonts/winfnt.c b/src/winfonts/winfnt.c
index 9ee9f67..ff24584 100644
--- a/src/winfonts/winfnt.c
+++ b/src/winfonts/winfnt.c
@@ -6,6 +6,8 @@
 /*                                                                         */
 /*  Copyright 1996-2001, 2002, 2003, 2004, 2006, 2007 by                   */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
+/*  Copyright 2003 Huw D M Davies for Codeweavers                          */
+/*  Copyright 2007 Dmitry Timoshkov for Codeweavers                        */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
 /*  modified, and distributed under the terms of the FreeType project      */
@@ -62,6 +64,80 @@
     FT_FRAME_END
   };
 
+  static const FT_Frame_Field  winpe32_header_fields[] =
+  {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  WinPE32_HeaderRec
+
+    FT_FRAME_START( 248 ),
+      FT_FRAME_ULONG_LE  ( magic ),   /* PE00 */
+      FT_FRAME_USHORT_LE ( machine ), /* 0x014c - i386 */
+      FT_FRAME_USHORT_LE ( number_of_sections ),
+      FT_FRAME_SKIP_BYTES( 12 ),
+      FT_FRAME_USHORT_LE ( size_of_optional_header ),
+      FT_FRAME_SKIP_BYTES( 2 ),
+      FT_FRAME_USHORT_LE ( magic32 ), /* 0x10b */
+      FT_FRAME_SKIP_BYTES( 110 ),
+      FT_FRAME_ULONG_LE  ( rsrc_virtual_address ),
+      FT_FRAME_ULONG_LE  ( rsrc_size ),
+      FT_FRAME_SKIP_BYTES( 104 ),
+    FT_FRAME_END
+  };
+
+  static const FT_Frame_Field  winpe32_section_fields[] =
+  {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  WinPE32_SectionRec
+
+    FT_FRAME_START( 40 ),
+      FT_FRAME_BYTES     ( name, 8 ),
+      FT_FRAME_SKIP_BYTES( 4 ),
+      FT_FRAME_ULONG_LE  ( virtual_address ),
+      FT_FRAME_ULONG_LE  ( size_of_raw_data ),
+      FT_FRAME_ULONG_LE  ( pointer_to_raw_data ),
+      FT_FRAME_SKIP_BYTES( 16 ),
+    FT_FRAME_END
+  };
+
+  static const FT_Frame_Field  winpe_rsrc_dir_fields[] =
+  {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  WinPE_RsrcDirRec
+
+    FT_FRAME_START( 16 ),
+      FT_FRAME_ULONG_LE ( characteristics ),
+      FT_FRAME_ULONG_LE ( time_date_stamp ),
+      FT_FRAME_USHORT_LE( major_version ),
+      FT_FRAME_USHORT_LE( minor_version ),
+      FT_FRAME_USHORT_LE( number_of_named_entries ),
+      FT_FRAME_USHORT_LE( number_of_id_entries ),
+    FT_FRAME_END
+  };
+
+  static const FT_Frame_Field  winpe_rsrc_dir_entry_fields[] =
+  {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  WinPE_RsrcDirEntryRec
+
+    FT_FRAME_START( 8 ),
+      FT_FRAME_ULONG_LE( name ),
+      FT_FRAME_ULONG_LE( offset ),
+    FT_FRAME_END
+  };
+
+  static const FT_Frame_Field  winpe_rsrc_data_entry_fields[] =
+  {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  WinPE_RsrcDataEntryRec
+
+    FT_FRAME_START( 16 ),
+      FT_FRAME_ULONG_LE( offset_to_data ),
+      FT_FRAME_ULONG_LE( size ),
+      FT_FRAME_ULONG_LE( code_page ),
+      FT_FRAME_ULONG_LE( reserved ),
+    FT_FRAME_END
+  };
+
   static const FT_Frame_Field  winfnt_header_fields[] =
   {
 #undef  FT_STRUCTURE
@@ -214,6 +290,8 @@
       WinNE_HeaderRec  ne_header;
 
 
+      FT_TRACE2(( "MZ signature found\n" ));
+
       if ( FT_STREAM_SEEK( mz_header.lfanew )                       ||
            FT_STREAM_READ_FIELDS( winne_header_fields, &ne_header ) )
         goto Exit;
@@ -229,6 +307,8 @@
         FT_ULong   font_offset = 0;
 
 
+        FT_TRACE2(( "NE signature found\n" ));
+
         if ( FT_STREAM_SEEK( res_offset )                    ||
              FT_FRAME_ENTER( ne_header.rname_tab_offset -
                              ne_header.resource_tab_offset ) )
@@ -291,9 +371,8 @@
              FT_FRAME_ENTER( 12 )                            )
           goto Fail;
 
-        face->font->offset     = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
-        face->font->fnt_size   = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
-        face->font->size_shift = size_shift;
+        face->font->offset   = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
+        face->font->fnt_size = (FT_ULong)FT_GET_USHORT_LE() << size_shift;
 
         stream->cursor += 8;
 
@@ -301,6 +380,181 @@
 
         error = fnt_font_load( face->font, stream );
       }
+      else if ( ne_header.magic == WINFNT_PE_MAGIC )
+      {
+        WinPE32_HeaderRec       pe32_header;
+        WinPE32_SectionRec      pe32_section;
+        WinPE_RsrcDirRec        root_dir, name_dir, lang_dir;
+        WinPE_RsrcDirEntryRec   dir_entry1, dir_entry2, dir_entry3;
+        WinPE_RsrcDataEntryRec  data_entry;
+
+        FT_Long    root_dir_offset, name_dir_offset, lang_dir_offset;
+        FT_UShort  i, j, k;
+
+
+        FT_TRACE2(( "PE signature found\n" ));
+
+        if ( FT_STREAM_SEEK( mz_header.lfanew )                           ||
+             FT_STREAM_READ_FIELDS( winpe32_header_fields, &pe32_header ) )
+          goto Exit;
+
+        FT_TRACE2(( "magic %04lx, machine %02x, number_of_sections %u, "
+                    "size_of_optional_header %02x\n"
+                    "magic32 %02x, rsrc_virtual_address %04lx, "
+                    "rsrc_size %04lx\n",
+                    pe32_header.magic, pe32_header.machine,
+                    pe32_header.number_of_sections,
+                    pe32_header.size_of_optional_header,
+                    pe32_header.magic32, pe32_header.rsrc_virtual_address,
+                    pe32_header.rsrc_size ));
+
+        if ( pe32_header.magic != WINFNT_PE_MAGIC /* check full signature */ ||
+             pe32_header.machine != 0x014c /* i386 */                        ||
+             pe32_header.size_of_optional_header != 0xe0 /* FIXME */         ||
+             pe32_header.magic32 != 0x10b                                    )
+        {
+          error = FNT_Err_Invalid_File_Format;
+          goto Exit;
+        }
+
+        face->root.num_faces = 0;
+
+        for ( i = 0; i < pe32_header.number_of_sections; i++ )
+        {
+          if ( FT_STREAM_READ_FIELDS( winpe32_section_fields,
+                                      &pe32_section ) )
+            goto Exit;
+
+          FT_TRACE2(( "name %.8s, va %04lx, size %04lx, offset %04lx\n",
+                      pe32_section.name, pe32_section.virtual_address,
+                      pe32_section.size_of_raw_data,
+                      pe32_section.pointer_to_raw_data ));
+
+          if ( pe32_header.rsrc_virtual_address ==
+                 pe32_section.virtual_address )
+            goto Found_rsrc_section;
+        }
+
+        error = FNT_Err_Invalid_File_Format;
+        goto Exit;
+
+      Found_rsrc_section:
+        FT_TRACE2(( "found resources section %.8s\n", pe32_section.name ));
+
+        if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data )        ||
+             FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &root_dir ) )
+          goto Exit;
+
+        root_dir_offset = pe32_section.pointer_to_raw_data;
+
+        for ( i = 0; i < root_dir.number_of_named_entries +
+                           root_dir.number_of_id_entries; i++ )
+        {
+          if ( FT_STREAM_SEEK( root_dir_offset + 16 + i * 8 )      ||
+               FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
+                                      &dir_entry1 )                )
+            goto Exit;
+
+          if ( !(dir_entry1.offset & 0x80000000UL ) /* DataIsDirectory */ )
+          {
+            error = FNT_Err_Invalid_File_Format;
+            goto Exit;
+          }
+
+          dir_entry1.offset &= ~0x80000000UL;
+
+          name_dir_offset = pe32_section.pointer_to_raw_data +
+                            dir_entry1.offset;
+
+          if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data +
+                               dir_entry1.offset )                       ||
+               FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &name_dir ) )
+            goto Exit;
+
+          for ( j = 0; j < name_dir.number_of_named_entries +
+                             name_dir.number_of_id_entries; j++ )
+          {
+            if ( FT_STREAM_SEEK( name_dir_offset + 16 + j * 8 )      ||
+                 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
+                                        &dir_entry2 )                )
+              goto Exit;
+
+            if ( !(dir_entry2.offset & 0x80000000UL ) /* DataIsDirectory */ )
+            {
+              error = FNT_Err_Invalid_File_Format;
+              goto Exit;
+            }
+
+            dir_entry2.offset &= ~0x80000000UL;
+
+            lang_dir_offset = pe32_section.pointer_to_raw_data +
+                                dir_entry2.offset;
+
+            if ( FT_STREAM_SEEK( pe32_section.pointer_to_raw_data +
+                                   dir_entry2.offset )                     ||
+                 FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &lang_dir ) )
+              goto Exit;
+
+            for ( k = 0; k < lang_dir.number_of_named_entries +
+                               lang_dir.number_of_id_entries; k++ )
+            {
+              if ( FT_STREAM_SEEK( lang_dir_offset + 16 + k * 8 )      ||
+                   FT_STREAM_READ_FIELDS( winpe_rsrc_dir_entry_fields,
+                                          &dir_entry3 )                )
+                goto Exit;
+
+              if ( dir_entry2.offset & 0x80000000UL /* DataIsDirectory */ )
+              {
+                error = FNT_Err_Invalid_File_Format;
+                goto Exit;
+              }
+
+              if ( dir_entry1.name == 8 /* RT_FONT */ )
+              {
+                if ( FT_STREAM_SEEK( root_dir_offset + dir_entry3.offset ) ||
+                     FT_STREAM_READ_FIELDS( winpe_rsrc_data_entry_fields,
+                                            &data_entry )                  )
+                  goto Exit;
+
+                FT_TRACE2(( "found font #%lu, offset %04lx, "
+                            "size %04lx, cp %lu\n",
+                            dir_entry2.name,
+                            pe32_section.pointer_to_raw_data +
+                              data_entry.offset_to_data -
+                              pe32_section.virtual_address,
+                            data_entry.size, data_entry.code_page ));
+
+                if ( face_index == face->root.num_faces )
+                {
+                  if ( FT_NEW( face->font ) )
+                    goto Exit;
+
+                  face->font->offset   = pe32_section.pointer_to_raw_data +
+                                           data_entry.offset_to_data -
+                                           pe32_section.virtual_address;
+                  face->font->fnt_size = data_entry.size;
+
+                  error = fnt_font_load( face->font, stream );
+                  if ( error )
+                    FT_TRACE2(( "font #%lu load error %d\n",
+                                dir_entry2.name, error ));
+                  else
+                    FT_TRACE2(( "font #%lu successfully loaded\n",
+                                dir_entry2.name ));
+                }
+
+                face->root.num_faces++;
+              }
+            }
+          }
+        }
+      }
+
+      if ( face_index >= face->root.num_faces )
+      {
+        error = FNT_Err_Bad_Argument;
+        goto Exit;
+      }
     }
 
   Fail:
diff --git a/src/winfonts/winfnt.h b/src/winfonts/winfnt.h
index 32ab6da..ca75c95 100644
--- a/src/winfonts/winfnt.h
+++ b/src/winfonts/winfnt.h
@@ -4,8 +4,9 @@
 /*                                                                         */
 /*    FreeType font driver for Windows FNT/FON files                       */
 /*                                                                         */
-/*  Copyright 1996-2001, 2002, 2003, 2004 by                               */
+/*  Copyright 1996-2001, 2002, 2003, 2004, 2007 by                         */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
+/*  Copyright 2007 Dmitry Timoshkov for Codeweavers                        */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
 /*  modified, and distributed under the terms of the FreeType project      */
@@ -46,6 +47,65 @@ FT_BEGIN_HEADER
   } WinNE_HeaderRec;
 
 
+  typedef struct  WinPE32_HeaderRec_
+  {
+    FT_ULong   magic;
+    FT_UShort  machine;
+    FT_UShort  number_of_sections;
+    /* skipped content */
+    FT_UShort  size_of_optional_header;
+    /* skipped content */
+    FT_UShort  magic32;
+    /* skipped content */
+    FT_ULong   rsrc_virtual_address;
+    FT_ULong   rsrc_size;
+    /* skipped content */
+
+  } WinPE32_HeaderRec;
+
+
+  typedef struct  WinPE32_SectionRec_
+  {
+    FT_Byte   name[8];
+    /* skipped content */
+    FT_ULong  virtual_address;
+    FT_ULong  size_of_raw_data;
+    FT_ULong  pointer_to_raw_data;
+    /* skipped content */
+
+  } WinPE32_SectionRec;
+
+
+  typedef struct  WinPE_RsrcDirRec_
+  {
+    FT_ULong   characteristics;
+    FT_ULong   time_date_stamp;
+    FT_UShort  major_version;
+    FT_UShort  minor_version;
+    FT_UShort  number_of_named_entries;
+    FT_UShort  number_of_id_entries;
+
+  } WinPE_RsrcDirRec;
+
+
+  typedef struct  WinPE_RsrcDirEntryRec_
+  {
+    FT_ULong  name;
+    FT_ULong  offset;
+
+  } WinPE_RsrcDirEntryRec;
+
+
+  typedef struct  WinPE_RsrcDataEntryRec_
+  {
+    FT_ULong  offset_to_data;
+    FT_ULong  size;
+    FT_ULong  code_page;
+    FT_ULong  reserved;
+
+  } WinPE_RsrcDataEntryRec;
+
+
   typedef struct  WinNameInfoRec_
   {
     FT_UShort  offset;
@@ -68,12 +128,12 @@ FT_BEGIN_HEADER
 
 #define WINFNT_MZ_MAGIC  0x5A4D
 #define WINFNT_NE_MAGIC  0x454E
+#define WINFNT_PE_MAGIC  0x4550
 
 
   typedef struct  FNT_FontRec_
   {
     FT_ULong             offset;
-    FT_Int               size_shift;
 
     FT_WinFNT_HeaderRec  header;