res16.c 9.86 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 18
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 20 21
 */

#include "config.h"
22
#include "wine/port.h"
23

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

41
#include "windef.h"
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
#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 */
};

70 71 72 73 74 75
/* top level of the resource tree */
struct res_tree
{
    struct res_type *types;                /* types array */
    unsigned int     nb_types;             /* total number of types */
};
76 77 78 79 80 81

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 */


82
inline static struct resource *add_resource( DLLSPEC *spec )
83
{
84 85
    spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
    return &spec->resources[spec->nb_resources++];
86 87
}

88
static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
89 90
{
    struct res_type *type;
91 92
    tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
    type = &tree->types[tree->nb_types++];
93 94 95 96 97 98 99
    type->type        = &res->type;
    type->res         = res;
    type->nb_names    = 0;
    return type;
}

/* get the next byte from the current resource file */
Jon Griffiths's avatar
Jon Griffiths committed
100
static unsigned char get_byte(void)
101 102 103 104 105 106 107 108 109 110
{
    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 */
111 112 113 114
#ifdef WORDS_BIGENDIAN
    unsigned char high = get_byte();
    unsigned char low = get_byte();
#else
115 116
    unsigned char low = get_byte();
    unsigned char high = get_byte();
117
#endif
118 119 120 121 122 123
    return low | (high << 8);
}

/* get the next dword from the current resource file */
static DWORD get_dword(void)
{
124 125 126 127
#ifdef WORDS_BIGENDIAN
    WORD high = get_word();
    WORD low = get_word();
#else
128 129
    WORD low = get_word();
    WORD high = get_word();
130
#endif
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    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
    {
145
        char *p = xmalloc( (strlen((char*)file_pos) + 1) );
146 147 148 149 150 151 152
        str->str = p;
        str->id = 0;
        while ((*p++ = get_byte()));
    }
}

/* load the next resource from the current file */
153
static void load_next_resource( DLLSPEC *spec )
154
{
155
    struct resource *res = add_resource( spec );
156 157 158 159 160 161 162 163 164 165 166

    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 */
167
void load_res16_file( const char *name, DLLSPEC *spec )
168 169 170 171 172 173 174
{
    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 );
175
    if (!st.st_size) fatal_error( "%s is an empty file\n", name );
176
#ifdef	HAVE_MMAP
177
    if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
178
#endif	/* HAVE_MMAP */
179 180 181 182 183 184 185 186 187
    {
        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;
188
    while (file_pos < file_end) load_next_resource( spec );
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
}

/* 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 */
216
static struct res_tree *build_resource_tree( DLLSPEC *spec )
217
{
Jon Griffiths's avatar
Jon Griffiths committed
218
    unsigned int i;
219
    struct res_tree *tree;
220 221
    struct res_type *type = NULL;

222 223 224 225 226
    qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );

    tree = xmalloc( sizeof(*tree) );
    tree->types = NULL;
    tree->nb_types = 0;
227

228
    for (i = 0; i < spec->nb_resources; i++)
229
    {
230 231
        if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
            type = add_type( tree, &spec->resources[i] );
232 233
        type->nb_names++;
    }
234 235 236 237 238 239 240 241
    return tree;
}

/* free the resource tree */
static void free_resource_tree( struct res_tree *tree )
{
    free( tree->types );
    free( tree );
242 243 244
}

/* output a string preceded by its length */
245
static void output_string( FILE *outfile, const char *str )
246
{
247 248 249 250
    unsigned int i, len = strlen(str);
    fprintf( outfile, "\t.byte 0x%02x", len );
    for (i = 0; i < len; i++) fprintf( outfile, ",0x%02x", (unsigned char)str[i] );
    fprintf( outfile, " /* %s */\n", str );
251 252
}

253
/* output the resource data */
254
void output_res16_data( FILE *outfile, DLLSPEC *spec )
255 256
{
    const struct resource *res;
257
    unsigned int i;
258

259
    if (!spec->nb_resources) return;
260

261
    for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
262
    {
263 264 265
        fprintf( outfile, ".L__wine_spec_resource_%u:\n", i );
        dump_bytes( outfile, res->data, res->data_size );
        fprintf( outfile, ".L__wine_spec_resource_%u_end:\n", i );
266
    }
267 268
}

269
/* output the resource definitions */
270
void output_res16_directory( FILE *outfile, DLLSPEC *spec, const char *header_name )
271
{
272
    unsigned int i, j;
273
    struct res_tree *tree;
274 275 276
    const struct res_type *type;
    const struct resource *res;

277
    tree = build_resource_tree( spec );
278

279 280
    fprintf( outfile, "\n.L__wine_spec_ne_rsrctab:\n" );
    fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );  /* alignment */
281 282 283

    /* type and name structures */

284
    for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
285 286
    {
        if (type->type->str)
287 288
            fprintf( outfile, "\t%s .L__wine_spec_restype_%u-.L__wine_spec_ne_rsrctab\n",
                     get_asm_short_keyword(), i );
289
        else
290
            fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), type->type->id | 0x8000 );
291

292
        fprintf( outfile, "\t%s %u,0,0\n", get_asm_short_keyword(), type->nb_names );
293 294 295

        for (j = 0, res = type->res; j < type->nb_names; j++, res++)
        {
296 297 298 299 300
            fprintf( outfile, "\t%s .L__wine_spec_resource_%u-%s\n",
                     get_asm_short_keyword(), res - spec->resources, header_name );
            fprintf( outfile, "\t%s .L__wine_spec_resource_%u_end-.L__wine_spec_resource_%u\n",
                     get_asm_short_keyword(), res - spec->resources, res - spec->resources );
            fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), res->memopt );
301
            if (res->name.str)
302 303
                fprintf( outfile, "\t%s .L__wine_spec_resname_%u_%u-.L__wine_spec_ne_rsrctab\n",
                         get_asm_short_keyword(), i, j );
304
            else
305 306 307
                fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), res->name.id | 0x8000 );

            fprintf( outfile, "\t%s 0,0\n", get_asm_short_keyword() );
308 309
        }
    }
310
    fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );  /* terminator */
311 312 313

    /* name strings */

314
    for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
315
    {
316 317 318 319 320
        if (type->type->str)
        {
            fprintf( outfile, ".L__wine_spec_restype_%u:\n", i );
            output_string( outfile, type->type->str );
        }
321 322
        for (j = 0, res = type->res; j < type->nb_names; j++, res++)
        {
323 324 325 326 327
            if (res->name.str)
            {
                fprintf( outfile, ".L__wine_spec_resname_%u_%u:\n", i, j );
                output_string( outfile, res->name.str );
            }
328 329
        }
    }
330
    fprintf( outfile, "\t.byte 0\n" );  /* names terminator */
331

332
    free_resource_tree( tree );
333
}