mf.c 5.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  Dump a Windows Metafile
 *
 *  Copyright 2021 Zhiyi Zhang for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

23
#include "winedump.h"
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
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"

#define META_EOF        0
#define METAFILE_MEMORY 1
#define METAFILE_DISK   2

static unsigned offset = 0;

static unsigned short read_word(const unsigned char *buffer)
{
    return buffer[0] + (buffer[1] << 8);
}

static unsigned int read_int(const unsigned char *buffer)
{
    return buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24);
}

static int dump_mfrecord(void)
{
    unsigned int type, size, i;
    const unsigned char *ptr;

    ptr = PRD(offset, 6);
    if (!ptr)
        return -1;

    /* METAHEADER */
    if (offset == 0)
    {
        type = read_word(ptr);
        /* mtHeaderSize is in words */
        size = read_word(ptr + 2) * 2;
    }
    /* METARECORD */
    else
    {
        /* rdSize is in words */
        size = read_int(ptr) * 2;
        type = read_word(ptr + 4);
    }

#define MRCASE(x)                         \
    case x:                               \
        printf("%-20s %08x\n", #x, size); \
        break

    switch (type)
    {
    case METAFILE_MEMORY:
    case METAFILE_DISK:
    {
        const METAHEADER *header = PRD(offset, sizeof(*header));

        printf("%-20s %08x\n", "METAHEADER", size);
        printf("type %d header_size %#x version %#x size %#x object_count %d max_record_size %#x "
               "parameter_count %d\n",
83 84
               header->mtType, header->mtHeaderSize * 2, header->mtVersion, (UINT)header->mtSize * 2,
               header->mtNoObjects, (UINT)header->mtMaxRecord * 2, header->mtNoParameters);
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
        break;
    }
    MRCASE(META_SETBKCOLOR);
    MRCASE(META_SETBKMODE);
    MRCASE(META_SETMAPMODE);
    MRCASE(META_SETROP2);
    MRCASE(META_SETRELABS);
    MRCASE(META_SETPOLYFILLMODE);
    MRCASE(META_SETSTRETCHBLTMODE);
    MRCASE(META_SETTEXTCHAREXTRA);
    MRCASE(META_SETTEXTCOLOR);
    MRCASE(META_SETTEXTJUSTIFICATION);
    MRCASE(META_SETWINDOWORG);
    MRCASE(META_SETWINDOWEXT);
    MRCASE(META_SETVIEWPORTORG);
    MRCASE(META_SETVIEWPORTEXT);
    MRCASE(META_OFFSETWINDOWORG);
    MRCASE(META_SCALEWINDOWEXT);
    MRCASE(META_OFFSETVIEWPORTORG);
    MRCASE(META_SCALEVIEWPORTEXT);
    MRCASE(META_LINETO);
    MRCASE(META_MOVETO);
    MRCASE(META_EXCLUDECLIPRECT);
    MRCASE(META_INTERSECTCLIPRECT);
    MRCASE(META_ARC);
    MRCASE(META_ELLIPSE);
    MRCASE(META_FLOODFILL);
    MRCASE(META_PIE);
    MRCASE(META_RECTANGLE);
    MRCASE(META_ROUNDRECT);
    MRCASE(META_PATBLT);
    MRCASE(META_SAVEDC);
    MRCASE(META_SETPIXEL);
    MRCASE(META_OFFSETCLIPRGN);
    MRCASE(META_TEXTOUT);
    MRCASE(META_BITBLT);
    MRCASE(META_STRETCHBLT);
    MRCASE(META_POLYGON);
    MRCASE(META_POLYLINE);
    MRCASE(META_ESCAPE);
    MRCASE(META_RESTOREDC);
    MRCASE(META_FILLREGION);
    MRCASE(META_FRAMEREGION);
    MRCASE(META_INVERTREGION);
    MRCASE(META_PAINTREGION);
    MRCASE(META_SELECTCLIPREGION);
    MRCASE(META_SELECTOBJECT);
    MRCASE(META_SETTEXTALIGN);
    MRCASE(META_DRAWTEXT);
    MRCASE(META_CHORD);
    MRCASE(META_SETMAPPERFLAGS);
    MRCASE(META_EXTTEXTOUT);
    MRCASE(META_SETDIBTODEV);
    MRCASE(META_SELECTPALETTE);
    MRCASE(META_REALIZEPALETTE);
    MRCASE(META_ANIMATEPALETTE);
    MRCASE(META_SETPALENTRIES);
    MRCASE(META_POLYPOLYGON);
    MRCASE(META_RESIZEPALETTE);
    MRCASE(META_DIBBITBLT);
    MRCASE(META_DIBSTRETCHBLT);
    MRCASE(META_DIBCREATEPATTERNBRUSH);
    MRCASE(META_STRETCHDIB);
    MRCASE(META_EXTFLOODFILL);
    MRCASE(META_RESETDC);
    MRCASE(META_STARTDOC);
    MRCASE(META_STARTPAGE);
    MRCASE(META_ENDPAGE);
    MRCASE(META_ABORTDOC);
    MRCASE(META_ENDDOC);
    MRCASE(META_SETLAYOUT);
    MRCASE(META_DELETEOBJECT);
    MRCASE(META_CREATEPALETTE);
    MRCASE(META_CREATEBRUSH);
    MRCASE(META_CREATEPATTERNBRUSH);
    MRCASE(META_CREATEPENINDIRECT);
    MRCASE(META_CREATEFONTINDIRECT);
    MRCASE(META_CREATEBRUSHINDIRECT);
    MRCASE(META_CREATEBITMAPINDIRECT);
    MRCASE(META_CREATEBITMAP);
    MRCASE(META_CREATEREGION);
    MRCASE(META_UNKNOWN);
    MRCASE(META_EOF);

    default:
        printf("%u %08x\n", type, size);
        break;
    }

#undef MRCASE

    if ((size < 6) || (size % 2))
        return -1;

    /* METARECORD */
    if (offset)
    {
        size -= 6;
        offset += 6;
    }

    for (i = 0; i < size; i += 2)
    {
        if (i % 16 == 0)
            printf("   ");
        if (!(ptr = PRD(offset, 2)))
            return -1;
        offset += 2;
        printf("%04x ", read_word(ptr));
        if ((i % 16 == 14) || (i + 2 == size))
            printf("\n");
    }

    return 0;
}

enum FileSig get_kind_mf(void)
{
    const METAHEADER *hdr;

    hdr = PRD(0, sizeof(*hdr));
    if (hdr && (hdr->mtType == METAFILE_MEMORY || hdr->mtType == METAFILE_DISK)
        && hdr->mtHeaderSize == sizeof(METAHEADER) / sizeof(WORD)
        && (hdr->mtVersion == 0x0100 || hdr->mtVersion == 0x0300))
        return SIG_MF;
    return SIG_UNKNOWN;
}

void mf_dump(void)
{
    offset = 0;
    while (!dump_mfrecord())
        ;
}