res32.c 14.6 KB
Newer Older
1 2 3 4
/*
 * Builtin dlls resource support
 *
 * Copyright 2000 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21
 */

#include "config.h"
22
#include "wine/port.h"
23 24 25 26

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
27
#include <stdarg.h>
28
#include <stdio.h>
29 30 31
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
32
#include <fcntl.h>
33 34 35
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
36 37 38 39
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

40
#include "windef.h"
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
#include "winbase.h"
#include "build.h"

/* Unicode string or integer id */
struct string_id
{
    WCHAR *str;  /* ptr to Unicode string */
    WORD   id;   /* integer id if str is NULL */
};

/* descriptor for a resource */
struct resource
{
    struct string_id type;
    struct string_id name;
    const void      *data;
    unsigned int     data_size;
    WORD             lang;
};

/* name level of the resource tree */
struct res_name
{
    const struct string_id  *name;         /* name */
    const struct resource   *res;          /* resource */
    int                      nb_languages; /* number of languages */
67
    unsigned int             name_offset;  /* offset of name in resource dir */
68 69 70 71 72 73 74 75 76
};

/* type level of the resource tree */
struct res_type
{
    const struct string_id  *type;         /* type name */
    struct res_name         *names;        /* names array */
    unsigned int             nb_names;     /* total number of names */
    unsigned int             nb_id_names;  /* number of names that have a numeric id */
77
    unsigned int             name_offset;  /* offset of type name in resource dir */
78 79
};

80 81 82 83 84 85
/* top level of the resource tree */
struct res_tree
{
    struct res_type *types;                /* types array */
    unsigned int     nb_types;             /* total number of types */
};
86

87
static int byte_swapped;  /* whether the current resource file is byte-swapped */
88 89
static const unsigned char *file_pos;   /* current position in resource file */
static const unsigned char *file_end;   /* end of resource file */
90 91
static const char *file_name;  /* current resource file name */

92
/* size of a resource directory with n entries */
93
#define RESDIR_SIZE(n)  (sizeof(IMAGE_RESOURCE_DIRECTORY) + (n) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY))
94

95

96
static inline struct resource *add_resource( DLLSPEC *spec )
97
{
98 99
    spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
    return &spec->resources[spec->nb_resources++];
100 101
}

102 103 104 105 106 107 108 109 110 111 112 113 114
static inline unsigned int strlenW( const WCHAR *str )
{
    const WCHAR *s = str;
    while (*s) s++;
    return s - str;
}

static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
{
    while (*str1 && (*str1 == *str2)) { str1++; str2++; }
    return *str1 - *str2;
}

115 116 117 118 119 120 121 122 123 124 125 126
static struct res_name *add_name( struct res_type *type, const struct resource *res )
{
    struct res_name *name;
    type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
    name = &type->names[type->nb_names++];
    name->name         = &res->name;
    name->res          = res;
    name->nb_languages = 1;
    if (!name->name->str) type->nb_id_names++;
    return name;
}

127
static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
128 129
{
    struct res_type *type;
130 131
    tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
    type = &tree->types[tree->nb_types++];
132 133 134 135 136 137 138 139 140 141
    type->type        = &res->type;
    type->names       = NULL;
    type->nb_names    = 0;
    type->nb_id_names = 0;
    return type;
}

/* get the next word from the current resource file */
static WORD get_word(void)
{
142
    WORD ret = *(const WORD *)file_pos;
143
    if (byte_swapped) ret = (ret << 8) | (ret >> 8);
144 145 146 147 148 149 150 151
    file_pos += sizeof(WORD);
    if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
    return ret;
}

/* get the next dword from the current resource file */
static DWORD get_dword(void)
{
152
    DWORD ret = *(const DWORD *)file_pos;
153 154
    if (byte_swapped)
        ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
155 156 157 158 159 160 161 162
    file_pos += sizeof(DWORD);
    if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
    return ret;
}

/* get a string from the current resource file */
static void get_string( struct string_id *str )
{
163
    if (*(const WCHAR *)file_pos == 0xffff)
164 165 166 167 168 169 170
    {
        get_word();  /* skip the 0xffff */
        str->str = NULL;
        str->id = get_word();
    }
    else
    {
171
        WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
172 173
        str->str = p;
        str->id  = 0;
174 175 176 177 178 179
        while ((*p++ = get_word()));
    }
}

/* check the file header */
/* all values must be zero except header size */
180
static int check_header(void)
181
{
182 183
    DWORD size;

184
    if (get_dword()) return 0;        /* data size */
185 186 187
    size = get_dword();               /* header size */
    if (size == 0x20000000) byte_swapped = 1;
    else if (size != 0x20) return 0;
188 189 190 191 192 193 194 195
    if (get_word() != 0xffff || get_word()) return 0;  /* type, must be id 0 */
    if (get_word() != 0xffff || get_word()) return 0;  /* name, must be id 0 */
    if (get_dword()) return 0;        /* data version */
    if (get_word()) return 0;         /* mem options */
    if (get_word()) return 0;         /* language */
    if (get_dword()) return 0;        /* version */
    if (get_dword()) return 0;        /* characteristics */
    return 1;
196 197 198
}

/* load the next resource from the current file */
199
static void load_next_resource( DLLSPEC *spec )
200 201
{
    DWORD hdr_size;
202
    struct resource *res = add_resource( spec );
203 204 205 206 207 208 209 210

    res->data_size = (get_dword() + 3) & ~3;
    hdr_size = get_dword();
    if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );

    res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
    get_string( &res->type );
    get_string( &res->name );
211
    if ((UINT_PTR)file_pos & 2) get_word();  /* align to dword boundary */
212 213 214 215 216 217
    get_dword();                        /* skip data version */
    get_word();                         /* skip mem options */
    res->lang = get_word();
    get_dword();                        /* skip version */
    get_dword();                        /* skip characteristics */

218
    file_pos = (const unsigned char *)res->data + res->data_size;
219 220 221 222
    if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
}

/* load a Win32 .res file */
223
int load_res32_file( const char *name, DLLSPEC *spec )
224
{
225
    int fd, ret;
226 227 228 229 230
    void *base;
    struct stat st;

    if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
    if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
231
    if (!st.st_size) fatal_error( "%s is an empty file\n", name );
232
#ifdef	HAVE_MMAP
233
    if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
234
#endif	/* HAVE_MMAP */
235 236 237 238 239 240
    {
        base = xmalloc( st.st_size );
        if (read( fd, base, st.st_size ) != st.st_size)
            fatal_error( "Cannot read %s\n", name );
    }

241
    byte_swapped = 0;
242
    file_name = name;
243 244
    file_pos  = base;
    file_end  = file_pos + st.st_size;
245 246
    if ((ret = check_header()))
    {
247
        while (file_pos < file_end) load_next_resource( spec );
248 249 250
    }
    close( fd );
    return ret;
251 252 253 254 255 256 257 258 259 260 261
}

/* compare two unicode strings/ids */
static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
{
    if (!str1->str)
    {
        if (!str2->str) return str1->id - str2->id;
        return 1;  /* an id compares larger than a string */
    }
    if (!str2->str) return -1;
262
    return strcmpW( str1->str, str2->str );
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
}

/* compare two resources for sorting the resource directory */
/* resources are stored first by type, then by name, then by language */
static int cmp_res( const void *ptr1, const void *ptr2 )
{
    const struct resource *res1 = ptr1;
    const struct resource *res2 = ptr2;
    int ret;

    if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
    if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
    return res1->lang - res2->lang;
}

/* build the 3-level (type,name,language) resource tree */
279
static struct res_tree *build_resource_tree( DLLSPEC *spec )
280
{
Jon Griffiths's avatar
Jon Griffiths committed
281
    unsigned int i;
282
    struct res_tree *tree;
283 284 285
    struct res_type *type = NULL;
    struct res_name *name = NULL;

286
    qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
287

288 289 290 291 292
    tree = xmalloc( sizeof(*tree) );
    tree->types = NULL;
    tree->nb_types = 0;

    for (i = 0; i < spec->nb_resources; i++)
293
    {
294
        if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
295
        {
296 297
            type = add_type( tree, &spec->resources[i] );
            name = add_name( type, &spec->resources[i] );
298
        }
299
        else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
300
        {
301
            name = add_name( type, &spec->resources[i] );
302 303 304
        }
        else name->nb_languages++;
    }
305 306 307 308 309 310
    return tree;
}

/* free the resource tree */
static void free_resource_tree( struct res_tree *tree )
{
Jon Griffiths's avatar
Jon Griffiths committed
311
    unsigned int i;
312 313 314 315

    for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
    free( tree->types );
    free( tree );
316 317 318
}

/* output a Unicode string */
319
static void output_string( const WCHAR *name )
320 321
{
    int i, len = strlenW(name);
322 323 324 325 326
    output( "\t%s 0x%04x", get_asm_short_keyword(), len );
    for (i = 0; i < len; i++) output( ",0x%04x", name[i] );
    output( " /* " );
    for (i = 0; i < len; i++) output( "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
    output( " */\n" );
327 328 329
}

/* output a resource directory */
330
static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
331
{
332 333 334
    output( "\t.long 0\n" );  /* Characteristics */
    output( "\t.long 0\n" );  /* TimeDateStamp */
    output( "\t%s 0,0\n",     /* Major/MinorVersion */
335
             get_asm_short_keyword() );
336
    output( "\t%s %u,%u\n",   /* NumberOfNamed/IdEntries */
337
             get_asm_short_keyword(), nb_names, nb_ids );
338 339 340
}

/* output the resource definitions */
341
void output_resources( DLLSPEC *spec )
342
{
343
    int k, nb_id_types;
Jon Griffiths's avatar
Jon Griffiths committed
344
    unsigned int i, n, offset, data_offset;
345
    struct res_tree *tree;
346 347
    struct res_type *type;
    struct res_name *name;
348 349
    const struct resource *res;

350
    if (!spec->nb_resources) return;
351

352
    tree = build_resource_tree( spec );
353

354
    /* compute the offsets */
355

356
    offset = RESDIR_SIZE( tree->nb_types );
357
    for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
358
    {
359
        offset += RESDIR_SIZE( type->nb_names );
360
        for (n = 0, name = type->names; n < type->nb_names; n++, name++)
361
            offset += RESDIR_SIZE( name->nb_languages );
362
    }
363
    offset += spec->nb_resources * sizeof(IMAGE_RESOURCE_DATA_ENTRY);
364

365
    for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
366 367
    {
        if (type->type->str)
368 369
        {
            type->name_offset = offset | 0x80000000;
370
            offset += (strlenW(type->type->str)+1) * sizeof(WCHAR);
371
        }
372
        else
373 374
        {
            type->name_offset = type->type->id;
375
            nb_id_types++;
376
        }
377

378
        for (n = 0, name = type->names; n < type->nb_names; n++, name++)
379 380
        {
            if (name->name->str)
381 382
            {
                name->name_offset = offset | 0x80000000;
383
                offset += (strlenW(name->name->str)+1) * sizeof(WCHAR);
384 385
            }
            else name->name_offset = name->name->id;
386 387 388
        }
    }

389 390
    /* output the resource directories */

391 392 393 394
    output( "\n/* resources */\n\n" );
    output( "\t.data\n" );
    output( "\t.align %d\n", get_alignment(get_ptr_size()) );
    output( ".L__wine_spec_resources:\n" );
395

396
    output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
397 398

    /* dump the type directory */
399

400
    offset = RESDIR_SIZE( tree->nb_types );
401
    for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
402
    {
403
        output( "\t.long 0x%08x,0x%08x\n",
404
                 type->name_offset, offset | 0x80000000 );
405 406 407
        offset += RESDIR_SIZE( type->nb_names );
        for (n = 0, name = type->names; n < type->nb_names; n++, name++)
            offset += RESDIR_SIZE( name->nb_languages );
408 409
    }

410 411 412
    data_offset = offset;
    offset = RESDIR_SIZE( tree->nb_types );

413
    /* dump the names and languages directories */
414

415
    for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
416
    {
417
        output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
418
        offset += RESDIR_SIZE( type->nb_names );
419
        for (n = 0, name = type->names; n < type->nb_names; n++, name++)
420
        {
421
            output( "\t.long 0x%08x,0x%08x\n",
422
                     name->name_offset, offset | 0x80000000 );
423
            offset += RESDIR_SIZE( name->nb_languages );
424 425
        }

426
        for (n = 0, name = type->names; n < type->nb_names; n++, name++)
427
        {
428
            output_res_dir( 0, name->nb_languages );
429 430
            for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
            {
431
                unsigned int entry_offset = (res - spec->resources) * sizeof(IMAGE_RESOURCE_DATA_ENTRY);
432
                output( "\t.long 0x%08x,0x%08x\n", res->lang, data_offset + entry_offset );
433 434 435 436 437
            }
        }
    }

    /* dump the resource data entries */
438

439
    for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
440
        output( "\t.long .L__wine_spec_res_%d-.L__wine_spec_rva_base,%u,0,0\n",
441
                 i, res->data_size );
442 443

    /* dump the name strings */
444

445
    for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
446
    {
447
        if (type->type->str) output_string( type->type->str );
448
        for (n = 0, name = type->names; n < type->nb_names; n++, name++)
449
            if (name->name->str) output_string( name->name->str );
450
    }
451 452 453 454 455

    /* resource data */

    for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
    {
456 457 458
        output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
        output( ".L__wine_spec_res_%d:\n", i );
        dump_bytes( res->data, res->data_size );
459
    }
460 461
    output( ".L__wine_spec_resources_end:\n" );
    output( "\t.byte 0\n" );
462

463
    free_resource_tree( tree );
464
}