Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 730bfdc

Browse files
hughbemarek-safar
authored andcommitted
Fix decoding of EMF headers with missing data
1 parent 72cfcb7 commit 730bfdc

File tree

2 files changed

+9
-28
lines changed

2 files changed

+9
-28
lines changed

src/metafile.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,9 +1348,10 @@ gdip_read_emf_header_optionals (ENHMETAHEADER3 *header, void *pointer, ImageSour
13481348
/* Header big enough to contain an extension? */
13491349
if (headerSize >= HeaderExtension1Size)
13501350
{
1351+
/* Match GDI+ behaviour where missing header data is set to 0. */
13511352
HeaderExtension1 extension;
1352-
if (gdip_read_emf_data (pointer, (BYTE *) &extension, sizeof (HeaderExtension1), source) != sizeof (HeaderExtension1))
1353-
return OutOfMemory;
1353+
memset (&extension, 0, sizeof (HeaderExtension1));
1354+
gdip_read_emf_data (pointer, (BYTE *) &extension, sizeof (HeaderExtension1), source);
13541355

13551356
/* Valid pixel format values? */
13561357
if (extension.offPixelFormat >= HeaderExtension1Size && (extension.offPixelFormat + extension.cbPixelFormat) <= header->nSize)
@@ -1361,19 +1362,6 @@ gdip_read_emf_header_optionals (ENHMETAHEADER3 *header, void *pointer, ImageSour
13611362
}
13621363
}
13631364
}
1364-
1365-
int sizeToRead = originalHeaderSize - headerSize;
1366-
if (sizeToRead > 0) {
1367-
while (sizeToRead > sizeof (DWORD)) {
1368-
if (gdip_read_emf_data (pointer, (void*) &key, sizeof (DWORD), source) != sizeof (DWORD))
1369-
return OutOfMemory;
1370-
sizeToRead -= sizeof (DWORD);
1371-
}
1372-
if (sizeToRead > 0) {
1373-
if (gdip_read_emf_data (pointer, (void*) &key, sizeToRead, source) != sizeToRead)
1374-
return OutOfMemory;
1375-
}
1376-
}
13771365

13781366
header->nSize = headerSize;
13791367
return Ok;
@@ -1435,11 +1423,14 @@ g_warning ("ALDUS_PLACEABLE_METAFILE key %d, hmf %d, L %d, T %d, R %d, B %d, inc
14351423
status = combine_headers (NULL, header);
14361424
break;
14371425
case EMF_EMR_HEADER_KEY:
1438-
emf = &(header->Header.Emf);
1426+
emf = &header->Header.Emf;
14391427
emf->iType = key;
1428+
1429+
/* Match GDI+ behaviour where missing header data is set to 0. */
14401430
size = sizeof (ENHMETAHEADER3) - size;
1441-
if (gdip_read_emf_data (pointer, (BYTE*)(&header->Header.Emf) + sizeof (DWORD), size, source) != size)
1442-
return OutOfMemory;
1431+
memset ((BYTE *) emf + sizeof (DWORD), 0, size);
1432+
gdip_read_emf_data (pointer, (BYTE *) emf + sizeof (DWORD), size, source);
1433+
14431434
EnhMetaHeaderLE (&header->Header.Emf);
14441435

14451436
#ifdef DEBUG_METAFILE

tests/testemfcodec.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ static void test_valid ()
359359

360360
/* EMR_EOF */ 0x0E, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00
361361
};
362-
#if defined(USE_WINDOWS_GDIPLUS)
363362
BYTE shortMillimetres[] = {
364363
/* EMR_HEADER */ 0x01, 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00,
365364
/* Bounds */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00, 0x00,
@@ -376,7 +375,6 @@ static void test_valid ()
376375
/* Device */ 0xA0, 0x05, 0x00, 0x00, 0x84, 0x03, 0x00, 0x00,
377376
/* Millimetres */ 0xD8, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00
378377
};
379-
#endif
380378
BYTE invalidPixelFormat[] = {
381379
/* EMR_HEADER */ 0x01, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00,
382380
/* Bounds */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00, 0x00,
@@ -463,7 +461,6 @@ static void test_valid ()
463461

464462
/* EMR_EOF */ 0x0E, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00
465463
};
466-
#if defined(USE_WINDOWS_GDIPLUS)
467464
BYTE noRecords[] = {
468465
/* EMR_HEADER */ 0x01, 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00,
469466
/* Bounds */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00, 0x00,
@@ -624,7 +621,6 @@ static void test_valid ()
624621
/* bOpenGL */ 0x00, 0x00, 0x00, 0x00,
625622
/* Micrometers */ 0xC0, 0x4B, 0x03, 0x00, 0xD8, 0x41, 0x04
626623
};
627-
#endif
628624
BYTE tooLargeFileSize[] = {
629625
/* EMR_HEADER */ 0x01, 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00,
630626
/* Bounds */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00, 0x00,
@@ -661,16 +657,11 @@ static void test_valid ()
661657
createFileSuccess (nonZeroReserved, 14, 20, 50, 18, 750, 216);
662658
createFileSuccess (invalidDescription, 14, 20, 50, 18, 750, 216);
663659
createFileSuccess (invalidPalEntries, 14, 20, 50, 18, 750, 216);
664-
// FIXME: GDI+ allows 4 bytes off the size of Millimetres.
665-
#if defined(USE_WINDOWS_GDIPLUS)
666660
createFileSuccess (shortMillimetres, 14, 20, 50, 18, 750, 216);
667-
#endif
668661
createFileSuccess (invalidPixelFormat, 14, 20, 50, 18, 750, 216);
669662
createFileSuccess (invalidOpenGL, 14, 20, 50, 18, 750, 216);
670663
createFileSuccess (zeroMicrometresWidth, 14, 20, 50, 18, 750, 216);
671664
createFileSuccess (zeroMicrometresHeight, 14, 20, 50, 18, 750, 216);
672-
// FIXME: GDI+ allows short header extensions.
673-
#if defined(USE_WINDOWS_GDIPLUS)
674665
createFileSuccess (noRecords, 14, 20, 50, 18, 750, 216);
675666
createFileSuccess (noCbPixelFormat, 14, 20, 50, 18, 750, 216);
676667
createFileSuccess (shortCbPixelFormat, 14, 20, 50, 18, 750, 216);
@@ -680,7 +671,6 @@ static void test_valid ()
680671
createFileSuccess (shortOpenGL, 14, 20, 50, 18, 750, 216);
681672
createFileSuccess (noMicrometers, 14, 20, 50, 18, 750, 216);
682673
createFileSuccess (shortMicrometers, 14, 20, 50, 18, 750, 216);
683-
#endif
684674
createFileSuccess (tooLargeFileSize, 14, 20, 50, 18, 750, 216);
685675
}
686676

0 commit comments

Comments
 (0)