2000-10-25 22:33:58 +02:00
|
|
|
/*
|
|
|
|
* Builtin dlls resource support
|
|
|
|
*
|
|
|
|
* Copyright 2000 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-10-25 22:33:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-04-25 23:40:56 +02:00
|
|
|
#include "wine/port.h"
|
2000-10-25 22:33:58 +02:00
|
|
|
|
2005-05-18 20:21:59 +02:00
|
|
|
#include <assert.h>
|
2000-10-25 22:33:58 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-10-25 22:33:58 +02:00
|
|
|
#include <stdio.h>
|
2002-08-17 20:28:43 +02:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
2000-10-25 22:33:58 +02:00
|
|
|
#include <fcntl.h>
|
2002-08-17 20:28:43 +02:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2000-10-25 22:33:58 +02:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2000-10-25 22:33:58 +02:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "build.h"
|
|
|
|
|
|
|
|
/* Unicode string or integer id */
|
|
|
|
struct string_id
|
|
|
|
{
|
|
|
|
char *str; /* ptr to 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 memopt;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* type level of the resource tree */
|
|
|
|
struct res_type
|
|
|
|
{
|
|
|
|
const struct string_id *type; /* type name */
|
|
|
|
const struct resource *res; /* first resource of this type */
|
|
|
|
unsigned int nb_names; /* total number of names */
|
|
|
|
};
|
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
/* top level of the resource tree */
|
|
|
|
struct res_tree
|
|
|
|
{
|
|
|
|
struct res_type *types; /* types array */
|
|
|
|
unsigned int nb_types; /* total number of types */
|
|
|
|
};
|
2000-10-25 22:33:58 +02:00
|
|
|
|
|
|
|
static const unsigned char *file_pos; /* current position in resource file */
|
|
|
|
static const unsigned char *file_end; /* end of resource file */
|
|
|
|
static const char *file_name; /* current resource file name */
|
|
|
|
|
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
inline static struct resource *add_resource( DLLSPEC *spec )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2004-02-11 07:41:01 +01:00
|
|
|
spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
|
|
|
|
return &spec->resources[spec->nb_resources++];
|
2000-10-25 22:33:58 +02:00
|
|
|
}
|
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
|
|
|
struct res_type *type;
|
2004-02-11 07:41:01 +01:00
|
|
|
tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
|
|
|
|
type = &tree->types[tree->nb_types++];
|
2000-10-25 22:33:58 +02:00
|
|
|
type->type = &res->type;
|
|
|
|
type->res = res;
|
|
|
|
type->nb_names = 0;
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next byte from the current resource file */
|
2003-09-24 07:12:28 +02:00
|
|
|
static unsigned char get_byte(void)
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
|
|
|
unsigned char ret = *file_pos++;
|
|
|
|
if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next word from the current resource file */
|
|
|
|
static WORD get_word(void)
|
|
|
|
{
|
|
|
|
/* might not be aligned */
|
2000-12-29 06:17:33 +01:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
unsigned char high = get_byte();
|
|
|
|
unsigned char low = get_byte();
|
|
|
|
#else
|
2000-10-25 22:33:58 +02:00
|
|
|
unsigned char low = get_byte();
|
|
|
|
unsigned char high = get_byte();
|
2000-12-29 06:17:33 +01:00
|
|
|
#endif
|
2000-10-25 22:33:58 +02:00
|
|
|
return low | (high << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the next dword from the current resource file */
|
|
|
|
static DWORD get_dword(void)
|
|
|
|
{
|
2000-12-29 06:17:33 +01:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
WORD high = get_word();
|
|
|
|
WORD low = get_word();
|
|
|
|
#else
|
2000-10-25 22:33:58 +02:00
|
|
|
WORD low = get_word();
|
|
|
|
WORD high = get_word();
|
2000-12-29 06:17:33 +01:00
|
|
|
#endif
|
2000-10-25 22:33:58 +02:00
|
|
|
return low | (high << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a string from the current resource file */
|
|
|
|
static void get_string( struct string_id *str )
|
|
|
|
{
|
|
|
|
if (*file_pos == 0xff)
|
|
|
|
{
|
|
|
|
get_byte(); /* skip the 0xff */
|
|
|
|
str->str = NULL;
|
|
|
|
str->id = get_word();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-23 00:30:52 +02:00
|
|
|
char *p = xmalloc(strlen((const char *)file_pos) + 1);
|
2000-10-25 22:33:58 +02:00
|
|
|
str->str = p;
|
|
|
|
str->id = 0;
|
|
|
|
while ((*p++ = get_byte()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load the next resource from the current file */
|
2004-02-11 07:41:01 +01:00
|
|
|
static void load_next_resource( DLLSPEC *spec )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2004-02-11 07:41:01 +01:00
|
|
|
struct resource *res = add_resource( spec );
|
2000-10-25 22:33:58 +02:00
|
|
|
|
|
|
|
get_string( &res->type );
|
|
|
|
get_string( &res->name );
|
|
|
|
res->memopt = get_word();
|
|
|
|
res->data_size = get_dword();
|
|
|
|
res->data = file_pos;
|
|
|
|
file_pos += res->data_size;
|
|
|
|
if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load a Win16 .res file */
|
2004-02-11 07:41:01 +01:00
|
|
|
void load_res16_file( const char *name, DLLSPEC *spec )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
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 );
|
2003-09-19 02:19:16 +02:00
|
|
|
if (!st.st_size) fatal_error( "%s is an empty file\n", name );
|
2000-11-26 00:54:12 +01:00
|
|
|
#ifdef HAVE_MMAP
|
2000-10-25 22:33:58 +02:00
|
|
|
if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
|
2000-11-26 00:54:12 +01:00
|
|
|
#endif /* HAVE_MMAP */
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
|
|
|
base = xmalloc( st.st_size );
|
|
|
|
if (read( fd, base, st.st_size ) != st.st_size)
|
|
|
|
fatal_error( "Cannot read %s\n", name );
|
|
|
|
}
|
|
|
|
|
|
|
|
file_name = name;
|
|
|
|
file_pos = base;
|
|
|
|
file_end = file_pos + st.st_size;
|
2004-02-11 07:41:01 +01:00
|
|
|
while (file_pos < file_end) load_next_resource( spec );
|
2000-10-25 22:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compare two 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;
|
|
|
|
return strcasecmp( str1->str, str2->str );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compare two resources for sorting the resource directory */
|
|
|
|
/* resources are stored first by type, then by name */
|
|
|
|
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;
|
|
|
|
return cmp_string( &res1->name, &res2->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the 2-level (type,name) resource tree */
|
2004-02-11 07:41:01 +01:00
|
|
|
static struct res_tree *build_resource_tree( DLLSPEC *spec )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2005-03-18 15:04:07 +01:00
|
|
|
unsigned int i;
|
2004-02-11 07:41:01 +01:00
|
|
|
struct res_tree *tree;
|
2000-10-25 22:33:58 +02:00
|
|
|
struct res_type *type = NULL;
|
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
|
|
|
|
|
|
|
|
tree = xmalloc( sizeof(*tree) );
|
|
|
|
tree->types = NULL;
|
|
|
|
tree->nb_types = 0;
|
2000-10-25 22:33:58 +02:00
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
for (i = 0; i < spec->nb_resources; i++)
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2004-02-11 07:41:01 +01:00
|
|
|
if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
|
|
|
|
type = add_type( tree, &spec->resources[i] );
|
2000-10-25 22:33:58 +02:00
|
|
|
type->nb_names++;
|
|
|
|
}
|
2004-02-11 07:41:01 +01:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free the resource tree */
|
|
|
|
static void free_resource_tree( struct res_tree *tree )
|
|
|
|
{
|
|
|
|
free( tree->types );
|
|
|
|
free( tree );
|
2000-10-25 22:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* output a string preceded by its length */
|
2006-08-11 20:41:29 +02:00
|
|
|
static void output_string( const char *str )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2005-09-21 16:23:54 +02:00
|
|
|
unsigned int i, len = strlen(str);
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t.byte 0x%02x", len );
|
|
|
|
for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)str[i] );
|
|
|
|
output( " /* %s */\n", str );
|
2005-05-20 21:19:01 +02:00
|
|
|
}
|
|
|
|
|
2000-10-25 22:33:58 +02:00
|
|
|
/* output the resource data */
|
2006-08-11 20:41:29 +02:00
|
|
|
void output_res16_data( DLLSPEC *spec )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
|
|
|
const struct resource *res;
|
2005-09-21 16:23:54 +02:00
|
|
|
unsigned int i;
|
2005-05-20 21:19:01 +02:00
|
|
|
|
2005-09-21 16:23:54 +02:00
|
|
|
if (!spec->nb_resources) return;
|
2000-10-25 22:33:58 +02:00
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2006-08-11 20:41:29 +02:00
|
|
|
output( ".L__wine_spec_resource_%u:\n", i );
|
|
|
|
dump_bytes( res->data, res->data_size );
|
|
|
|
output( ".L__wine_spec_resource_%u_end:\n", i );
|
2000-10-25 22:33:58 +02:00
|
|
|
}
|
2005-05-20 21:19:01 +02:00
|
|
|
}
|
|
|
|
|
2000-10-25 22:33:58 +02:00
|
|
|
/* output the resource definitions */
|
2006-08-11 20:41:29 +02:00
|
|
|
void output_res16_directory( DLLSPEC *spec, const char *header_name )
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2005-09-21 16:23:54 +02:00
|
|
|
unsigned int i, j;
|
2004-02-11 07:41:01 +01:00
|
|
|
struct res_tree *tree;
|
2000-10-25 22:33:58 +02:00
|
|
|
const struct res_type *type;
|
|
|
|
const struct resource *res;
|
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
tree = build_resource_tree( spec );
|
2000-10-25 22:33:58 +02:00
|
|
|
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\n.L__wine_spec_ne_rsrctab:\n" );
|
|
|
|
output( "\t%s 0\n", get_asm_short_keyword() ); /* alignment */
|
2000-10-25 22:33:58 +02:00
|
|
|
|
|
|
|
/* type and name structures */
|
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
|
|
|
if (type->type->str)
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s .L__wine_spec_restype_%u-.L__wine_spec_ne_rsrctab\n",
|
2005-09-21 16:23:54 +02:00
|
|
|
get_asm_short_keyword(), i );
|
2000-10-25 22:33:58 +02:00
|
|
|
else
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s 0x%04x\n", get_asm_short_keyword(), type->type->id | 0x8000 );
|
2000-10-25 22:33:58 +02:00
|
|
|
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s %u,0,0\n", get_asm_short_keyword(), type->nb_names );
|
2000-10-25 22:33:58 +02:00
|
|
|
|
|
|
|
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
|
|
|
|
{
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s .L__wine_spec_resource_%u-%s\n",
|
2005-09-21 16:23:54 +02:00
|
|
|
get_asm_short_keyword(), res - spec->resources, header_name );
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s .L__wine_spec_resource_%u_end-.L__wine_spec_resource_%u\n",
|
2005-09-21 16:23:54 +02:00
|
|
|
get_asm_short_keyword(), res - spec->resources, res - spec->resources );
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s 0x%04x\n", get_asm_short_keyword(), res->memopt );
|
2000-10-25 22:33:58 +02:00
|
|
|
if (res->name.str)
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s .L__wine_spec_resname_%u_%u-.L__wine_spec_ne_rsrctab\n",
|
2005-09-21 16:23:54 +02:00
|
|
|
get_asm_short_keyword(), i, j );
|
2000-10-25 22:33:58 +02:00
|
|
|
else
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s 0x%04x\n", get_asm_short_keyword(), res->name.id | 0x8000 );
|
2005-09-21 16:23:54 +02:00
|
|
|
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s 0,0\n", get_asm_short_keyword() );
|
2000-10-25 22:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t%s 0\n", get_asm_short_keyword() ); /* terminator */
|
2000-10-25 22:33:58 +02:00
|
|
|
|
|
|
|
/* name strings */
|
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
|
2000-10-25 22:33:58 +02:00
|
|
|
{
|
2005-09-21 16:23:54 +02:00
|
|
|
if (type->type->str)
|
|
|
|
{
|
2006-08-11 20:41:29 +02:00
|
|
|
output( ".L__wine_spec_restype_%u:\n", i );
|
|
|
|
output_string( type->type->str );
|
2005-09-21 16:23:54 +02:00
|
|
|
}
|
2000-10-25 22:33:58 +02:00
|
|
|
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
|
|
|
|
{
|
2005-09-21 16:23:54 +02:00
|
|
|
if (res->name.str)
|
|
|
|
{
|
2006-08-11 20:41:29 +02:00
|
|
|
output( ".L__wine_spec_resname_%u_%u:\n", i, j );
|
|
|
|
output_string( res->name.str );
|
2005-09-21 16:23:54 +02:00
|
|
|
}
|
2000-10-25 22:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-11 20:41:29 +02:00
|
|
|
output( "\t.byte 0\n" ); /* names terminator */
|
2000-10-25 22:33:58 +02:00
|
|
|
|
2004-02-11 07:41:01 +01:00
|
|
|
free_resource_tree( tree );
|
2000-10-25 22:33:58 +02:00
|
|
|
}
|