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
/*
 * PROJECT:     ReactOS Application compatibility module
 * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
 * PURPOSE:     Query file attributes used to match exe's
 * COPYRIGHT:   Copyright 2011 André Hentschel
 *              Copyright 2013 Mislav Blaževic
 *              Copyright 2015-2017 Mark Jansen (mark.jansen@reactos.org)
 */

#define WIN32_NO_STATUS
#include "windef.h"
#include "apphelp.h"
#include "strsafe.h"
#include "winver.h"
#include "rtlfuncs.h"


#define NUM_ATTRIBUTES  28
enum APPHELP_MODULETYPE
{
    MODTYPE_UNKNOWN = 0,
    MODTYPE_DOS = 1,
    MODTYPE_NE = 2,
    MODTYPE_PE = 3,
};


static void WINAPI SdbpSetDWORDAttr(PATTRINFO attr, TAG tag, DWORD value)
{
    attr->type = tag;
    attr->flags = ATTRIBUTE_AVAILABLE;
    attr->dwattr = value;
}

static void WINAPI SdbpSetQWORDAttr(PATTRINFO attr, TAG tag, QWORD value)
{
    attr->type = tag;
    attr->flags = ATTRIBUTE_AVAILABLE;
    attr->qwattr = value;
}

static void WINAPI SdbpSetStringAttr(PATTRINFO attr, TAG tag, WCHAR *string)
{
    if (!string)
    {
        attr->flags = ATTRIBUTE_FAILED;
        return;
    }

    attr->type = tag;
    attr->flags = ATTRIBUTE_AVAILABLE;
    attr->lpattr = SdbpStrDup(string);
}

static void WINAPI SdbpSetAttrFail(PATTRINFO attr)
{
    attr->flags = ATTRIBUTE_FAILED;
}

static WCHAR* WINAPI SdbpGetStringAttr(LPWSTR translation, LPCWSTR attr, PVOID file_info)
{
    UINT size = 0;
    PVOID buffer;
    WCHAR value[128] = {0};

    if (!file_info)
        return NULL;

    StringCchPrintfW(value, ARRAYSIZE(value), translation, attr);
    if (VerQueryValueW(file_info, value, &buffer, &size) && size != 0)
        return (WCHAR*)buffer;

    return NULL;
}

static void WINAPI SdbpSetStringAttrFromAnsiString(PATTRINFO attr, TAG tag, PBYTE string, size_t len)
{
    WCHAR* dest;
    if (!string)
    {
        attr->flags = ATTRIBUTE_FAILED;
        return;
    }

    attr->type = tag;
    attr->flags = ATTRIBUTE_AVAILABLE;
    dest = attr->lpattr = SdbAlloc((len+1) * sizeof(WCHAR));
    while (len--)
        *(dest++) = *(string++);
    *dest = 0;
}

static void WINAPI SdbpSetStringAttrFromPascalString(PATTRINFO attr, TAG tag, PBYTE string)
{
    if (!string)
    {
        attr->flags = ATTRIBUTE_FAILED;
        return;
    }

    SdbpSetStringAttrFromAnsiString(attr, tag, string + 1, *string);
}

static void SdbpReadFileVersion(PATTRINFO attr_info, PVOID file_info)
{
    static const WCHAR str_root[] = {'\\',0};

    VS_FIXEDFILEINFO* fixed_info;
    UINT size;
    if (file_info && VerQueryValueW(file_info, str_root, (LPVOID*)&fixed_info, &size) && size)
    {
        if (fixed_info->dwSignature == VS_FFI_SIGNATURE)
        {
            LARGE_INTEGER version;
            version.HighPart = fixed_info->dwFileVersionMS;<--- version.HighPart is assigned
            version.LowPart = fixed_info->dwFileVersionLS;<--- version.LowPart is assigned
            SdbpSetQWORDAttr(&attr_info[2], TAG_BIN_FILE_VERSION, version.QuadPart);
            SdbpSetQWORDAttr(&attr_info[21], TAG_UPTO_BIN_FILE_VERSION, version.QuadPart);
            version.HighPart = fixed_info->dwProductVersionMS;<--- version.HighPart is overwritten
            version.LowPart = fixed_info->dwProductVersionLS;<--- version.LowPart is overwritten
            SdbpSetQWORDAttr(&attr_info[3], TAG_BIN_PRODUCT_VERSION, version.QuadPart);
            SdbpSetQWORDAttr(&attr_info[22], TAG_UPTO_BIN_PRODUCT_VERSION, version.QuadPart);

            SdbpSetDWORDAttr(&attr_info[12], TAG_VERDATEHI, fixed_info->dwFileDateMS);
            SdbpSetDWORDAttr(&attr_info[13], TAG_VERDATELO, fixed_info->dwFileDateLS);
            SdbpSetDWORDAttr(&attr_info[14], TAG_VERFILEOS, fixed_info->dwFileOS);  /* 0x000, 0x4, 0x40004, 0x40000, 0x10004, 0x10001*/
            SdbpSetDWORDAttr(&attr_info[15], TAG_VERFILETYPE, fixed_info->dwFileType);  /* VFT_APP, VFT_DLL, .... */
            return;
        }
    }

    SdbpSetAttrFail(&attr_info[2]);
    SdbpSetAttrFail(&attr_info[3]);
    SdbpSetAttrFail(&attr_info[12]);
    SdbpSetAttrFail(&attr_info[13]);
    SdbpSetAttrFail(&attr_info[14]);
    SdbpSetAttrFail(&attr_info[15]);
    SdbpSetAttrFail(&attr_info[21]);
    SdbpSetAttrFail(&attr_info[22]);
}

static DWORD WINAPI SdbpCalculateFileChecksum(PMEMMAPPED mapping)
{
    size_t n, size;
    PDWORD data;
    DWORD checks = 0, carry = 0;

    if (mapping->size < 4)
        return 0;

    if (mapping->size >= 0x1000)
    {
        size = 0x1000;
        if (mapping->size < 0x1200)
            data = (PDWORD)(mapping->view + mapping->size - size);
        else
            data = (PDWORD)mapping->view + (0x200 / 4);
    }
    else
    {
        data = (PDWORD)mapping->view;
        size = mapping->size;
    }

    for (n = 0; n < size / 4; ++n)
    {
        checks += *data;
        carry = (checks & 1) ? 0x80000000 : 0;
        checks >>= 1;
        checks |= carry;
        ++data;
    }
    return checks;
}

static DWORD WINAPI SdbpGetModuleType(PMEMMAPPED mapping, PIMAGE_NT_HEADERS* nt_headers)
{
    PIMAGE_DOS_HEADER dos = (PIMAGE_DOS_HEADER)mapping->view;
    PIMAGE_OS2_HEADER os2;

    *nt_headers = NULL;

    if (mapping->size < 2 || dos->e_magic != IMAGE_DOS_SIGNATURE)
        return MODTYPE_UNKNOWN;

    if (mapping->size < sizeof(IMAGE_DOS_HEADER) || mapping->size < (dos->e_lfanew+2))
        return MODTYPE_DOS;

    os2 = (PIMAGE_OS2_HEADER)((PBYTE)dos + dos->e_lfanew);
    if (os2->ne_magic == IMAGE_OS2_SIGNATURE || os2->ne_magic == IMAGE_OS2_SIGNATURE_LE)
    {
        *nt_headers = (PIMAGE_NT_HEADERS)os2;
        return MODTYPE_NE;
    }

    if (mapping->size >= (dos->e_lfanew + 4) && ((PIMAGE_NT_HEADERS)os2)->Signature == IMAGE_NT_SIGNATURE)
    {
        *nt_headers = (PIMAGE_NT_HEADERS)os2;
        return MODTYPE_PE;
    }

    return MODTYPE_DOS;
}

/**
 * Frees attribute data allocated by SdbGetFileAttributes.
 * 
 * @note Unlike Windows, this implementation will not crash if attr_info is NULL.
 *
 * @param [in]  attr_info   Pointer to array of ATTRINFO which will be freed.
 *
 * @return  TRUE if it succeeds, FALSE if it fails.
 */
BOOL WINAPI SdbFreeFileAttributes(PATTRINFO attr_info)
{
    WORD i;

    if (!attr_info)
        return FALSE;

    for (i = 0; i < NUM_ATTRIBUTES; i++)
        if ((attr_info[i].type & TAG_TYPE_MASK) == TAG_TYPE_STRINGREF)
            SdbFree(attr_info[i].lpattr);
    SdbFree(attr_info);
    return TRUE;
}

/**
 * Retrieves attribute data shim database requires to match a file with database entry
 * 
 * @note You must free the attr_info allocated by this function by calling SdbFreeFileAttributes.
 *
 * @param [in]  path            Path to the file.
 * @param [out] attr_info_ret   Pointer to array of ATTRINFO. Contains attribute data.
 * @param [out] attr_count      Number of attributes in attr_info.
 *
 * @return  TRUE if it succeeds, FALSE if it fails.
 */
BOOL WINAPI SdbGetFileAttributes(LPCWSTR path, PATTRINFO *attr_info_ret, LPDWORD attr_count)
{
    static const WCHAR str_tinfo[] = {'\\','V','a','r','F','i','l','e','I','n','f','o','\\','T','r','a','n','s','l','a','t','i','o','n',0};
    static const WCHAR str_trans[] = {'\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o','\\','%','0','4','x','%','0','4','x','\\','%','%','s',0};
    static const WCHAR str_CompanyName[] = {'C','o','m','p','a','n','y','N','a','m','e',0};
    static const WCHAR str_FileDescription[] = {'F','i','l','e','D','e','s','c','r','i','p','t','i','o','n',0};
    static const WCHAR str_FileVersion[] = {'F','i','l','e','V','e','r','s','i','o','n',0};
    static const WCHAR str_InternalName[] = {'I','n','t','e','r','n','a','l','N','a','m','e',0};
    static const WCHAR str_LegalCopyright[] = {'L','e','g','a','l','C','o','p','y','r','i','g','h','t',0};
    static const WCHAR str_OriginalFilename[] = {'O','r','i','g','i','n','a','l','F','i','l','e','n','a','m','e',0};
    static const WCHAR str_ProductName[] = {'P','r','o','d','u','c','t','N','a','m','e',0};
    static const WCHAR str_ProductVersion[] = {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};

    PIMAGE_NT_HEADERS headers;
    MEMMAPPED mapped;
    PBYTE mapping_end;
    PVOID file_info = 0;
    DWORD module_type;
    WCHAR translation[128] = {0};
    PATTRINFO attr_info;

    struct LANGANDCODEPAGE {
        WORD language;
        WORD code_page;
    } *lang_page;

    if (!SdbpOpenMemMappedFile(path, &mapped))
    {
        SHIM_ERR("Error retrieving FILEINFO structure\n");
        return FALSE;
    }
    mapping_end = mapped.view + mapped.size;

    attr_info = (PATTRINFO)SdbAlloc(NUM_ATTRIBUTES * sizeof(ATTRINFO));

    SdbpSetDWORDAttr(&attr_info[0], TAG_SIZE, mapped.size);
    if (mapped.size)
        SdbpSetDWORDAttr(&attr_info[1], TAG_CHECKSUM, SdbpCalculateFileChecksum(&mapped));
    else
        SdbpSetAttrFail(&attr_info[1]);
    module_type = SdbpGetModuleType(&mapped, &headers);

    if (module_type != MODTYPE_UNKNOWN)
        SdbpSetDWORDAttr(&attr_info[16], TAG_MODULE_TYPE, module_type);
    else
        SdbpSetAttrFail(&attr_info[16]); /* TAG_MODULE_TYPE */

    if (headers && module_type == MODTYPE_PE && ((PBYTE)(headers+1) <= mapping_end))
    {
        DWORD info_size;
        ULONG export_dir_size;
        PIMAGE_EXPORT_DIRECTORY export_dir;

        info_size = GetFileVersionInfoSizeW(path, NULL);
        if (info_size != 0)
        {
            UINT page_size = 0;
            file_info = SdbAlloc(info_size);
            GetFileVersionInfoW(path, 0, info_size, file_info);
            VerQueryValueW(file_info, str_tinfo, (LPVOID)&lang_page, &page_size);
            StringCchPrintfW(translation, ARRAYSIZE(translation), str_trans, lang_page->language, lang_page->code_page);
        }

        /* Handles 2, 3, 12, 13, 14, 15, 21, 22 */
        SdbpReadFileVersion(attr_info, file_info);

        SdbpSetStringAttr(&attr_info[4], TAG_PRODUCT_VERSION, SdbpGetStringAttr(translation, str_ProductVersion, file_info));
        SdbpSetStringAttr(&attr_info[5], TAG_FILE_DESCRIPTION, SdbpGetStringAttr(translation, str_FileDescription, file_info));
        SdbpSetStringAttr(&attr_info[6], TAG_COMPANY_NAME, SdbpGetStringAttr(translation, str_CompanyName, file_info));
        SdbpSetStringAttr(&attr_info[7], TAG_PRODUCT_NAME, SdbpGetStringAttr(translation, str_ProductName, file_info));
        SdbpSetStringAttr(&attr_info[8], TAG_FILE_VERSION, SdbpGetStringAttr(translation, str_FileVersion, file_info));
        SdbpSetStringAttr(&attr_info[9], TAG_ORIGINAL_FILENAME, SdbpGetStringAttr(translation, str_OriginalFilename, file_info));
        SdbpSetStringAttr(&attr_info[10], TAG_INTERNAL_NAME, SdbpGetStringAttr(translation, str_InternalName, file_info));
        SdbpSetStringAttr(&attr_info[11], TAG_LEGAL_COPYRIGHT, SdbpGetStringAttr(translation, str_LegalCopyright, file_info));

        /* http://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85).aspx */

        SdbpSetDWORDAttr(&attr_info[17], TAG_PE_CHECKSUM, headers->OptionalHeader.CheckSum);

        SdbpSetDWORDAttr(&attr_info[18], TAG_LINKER_VERSION,     /* mislabeled! */
            ((DWORD)headers->OptionalHeader.MajorImageVersion) << 16 | headers->OptionalHeader.MinorImageVersion);
        SdbpSetAttrFail(&attr_info[19]); /* TAG_16BIT_DESCRIPTION */
        SdbpSetAttrFail(&attr_info[20]); /* TAG_16BIT_MODULE_NAME */

        SdbpSetDWORDAttr(&attr_info[23], TAG_LINK_DATE, headers->FileHeader.TimeDateStamp);
        SdbpSetDWORDAttr(&attr_info[24], TAG_UPTO_LINK_DATE, headers->FileHeader.TimeDateStamp);

        export_dir = (PIMAGE_EXPORT_DIRECTORY)RtlImageDirectoryEntryToData(mapped.view, FALSE, IMAGE_DIRECTORY_ENTRY_EXPORT, &export_dir_size);
        if (export_dir && ((PBYTE)(export_dir+1) <= mapping_end))
        {
            PIMAGE_SECTION_HEADER section = NULL;
            PBYTE export_name = RtlImageRvaToVa(headers, mapped.view, export_dir->Name, &section);
            if (export_name)
                SdbpSetStringAttrFromAnsiString(&attr_info[25], TAG_EXPORT_NAME, export_name, strlen((char*)export_name));
            else
                SdbpSetAttrFail(&attr_info[25]); /* TAG_EXPORT_NAME */
        }
        else
        {
            SdbpSetAttrFail(&attr_info[25]); /* TAG_EXPORT_NAME */
        }

        if (info_size)
            SdbpSetDWORDAttr(&attr_info[26], TAG_VER_LANGUAGE, lang_page->language);

        SdbpSetDWORDAttr(&attr_info[27], TAG_EXE_WRAPPER, 0); /* boolean */
    }
    else
    {
        int n;
        for (n = 2; n < NUM_ATTRIBUTES; ++n)
        {
            if (n != 16 && n != 26)
                SdbpSetAttrFail(&attr_info[n]);
        }
        if (module_type == MODTYPE_NE)
        {
            PBYTE ptr;
            PIMAGE_OS2_HEADER os2 = (PIMAGE_OS2_HEADER)headers;
            if ((PBYTE)(os2 + 1) <= mapping_end)
            {
                ptr = mapped.view + os2->ne_nrestab;
                if (ptr <= mapping_end && (ptr + 1 + *ptr) <= mapping_end)
                    SdbpSetStringAttrFromPascalString(&attr_info[19], TAG_16BIT_DESCRIPTION, ptr);
                ptr = (PBYTE)os2 + os2->ne_restab;
                if (ptr <= mapping_end && (ptr + 1 + *ptr) <= mapping_end)
                    SdbpSetStringAttrFromPascalString(&attr_info[20], TAG_16BIT_MODULE_NAME, ptr);
            }
        }
    }

    *attr_info_ret = attr_info;
    *attr_count = NUM_ATTRIBUTES; /* As far as I know, this one is always 28 */

    SdbFree(file_info);
    SdbpCloseMemMappedFile(&mapped);
    return TRUE;
}