#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "build.h"
struct string_id
{
char *str;
unsigned short id;
};
struct resource
{
struct string_id type;
struct string_id name;
const void *data;
unsigned int name_offset;
unsigned int data_size;
unsigned int memopt;
};
struct res_type
{
const struct string_id *type;
struct resource *res;
unsigned int name_offset;
unsigned int nb_names;
};
struct res_tree
{
struct res_type *types;
unsigned int nb_types;
};
static inline struct resource *add_resource( DLLSPEC *spec )
{
spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
return &spec->resources[spec->nb_resources++];
}
static struct res_type *add_type( struct res_tree *tree, struct resource *res )
{
struct res_type *type;
tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
type = &tree->types[tree->nb_types++];
type->type = &res->type;
type->res = res;
type->nb_names = 0;
return type;
}
static void get_string( struct string_id *str )
{
unsigned char c = get_byte();
if (c == 0xff)
{
str->str = NULL;
str->id = get_word();
}
else
{
str->str = (char *)input_buffer + input_buffer_pos - 1;
str->id = 0;
while (get_byte()) ;
}
}
static void load_next_resource( DLLSPEC *spec )
{
struct resource *res = add_resource( spec );
get_string( &res->type );
get_string( &res->name );
res->memopt = get_word();
res->data_size = get_dword();
res->data = input_buffer + input_buffer_pos;
input_buffer_pos += res->data_size;
if (input_buffer_pos > input_buffer_size)
fatal_error( "%s is a truncated/corrupted file\n", input_buffer_filename );
}
void load_res16_file( const char *name, DLLSPEC *spec )
{
init_input_buffer( name );
while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
}
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;
}
if (!str2->str) return -1;
return strcasecmp( str1->str, str2->str );
}
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 );
}
static struct res_tree *build_resource_tree( DLLSPEC *spec )
{
unsigned int i, j, offset;
struct res_tree *tree;
struct res_type *type = NULL;
struct resource *res;
qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
offset = 2;
tree = xmalloc( sizeof(*tree) );
tree->types = NULL;
tree->nb_types = 0;
for (i = 0; i < spec->nb_resources; i++)
{
if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))
{
type = add_type( tree, &spec->resources[i] );
offset += 8;
}
type->nb_names++;
offset += 12;
}
offset += 2;
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
if (type->type->str)
{
type->name_offset = offset;
offset += strlen(type->type->str) + 1;
}
else type->name_offset = type->type->id | 0x8000;
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
{
if (res->name.str)
{
res->name_offset = offset;
offset += strlen(res->name.str) + 1;
}
else res->name_offset = res->name.id | 0x8000;
}
}
return tree;
}
static void free_resource_tree( struct res_tree *tree )
{
free( tree->types );
free( tree );
}
static void output_string( const char *str )
{
unsigned int i, len = strlen(str);
output( "\t.byte 0x%02x", len );
for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)str[i] );
output( " /* %s */\n", str );
}
static void output_bin_string( const char *str )
{
put_byte( strlen(str) );
while (*str) put_byte( *str++ );
}
void output_res16_data( DLLSPEC *spec )
{
const struct resource *res;
unsigned int i;
for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
{
output( ".L__wine_spec_resource_%u:\n", i );
dump_bytes( res->data, res->data_size );
}
}
void output_res16_directory( DLLSPEC *spec )
{
unsigned int i, j;
struct res_tree *tree;
const struct res_type *type;
const struct resource *res;
tree = build_resource_tree( spec );
output( "\n.L__wine_spec_ne_rsrctab:\n" );
output( "\t.short 0\n" );
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
output( "\t.short 0x%04x,%u,0,0\n", type->name_offset, type->nb_names );
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
{
output( "\t.short .L__wine_spec_resource_%lu-.L__wine_spec_dos_header,%u\n",
(unsigned long)(res - spec->resources), res->data_size );
output( "\t.short 0x%04x,0x%04x,0,0\n", res->memopt, res->name_offset );
}
}
output( "\t.short 0\n" );
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
if (type->type->str) output_string( type->type->str );
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
if (res->name.str) output_string( res->name.str );
}
output( "\t.byte 0\n" );
free_resource_tree( tree );
}
void output_bin_res16_data( DLLSPEC *spec )
{
const struct resource *res;
unsigned int i;
for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
put_data( res->data, res->data_size );
}
void output_bin_res16_directory( DLLSPEC *spec, unsigned int data_offset )
{
unsigned int i, j;
struct res_tree *tree;
const struct res_type *type;
const struct resource *res;
tree = build_resource_tree( spec );
put_word( 0 );
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
put_word( type->name_offset );
put_word( type->nb_names );
put_word( 0 );
put_word( 0 );
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
{
put_word( data_offset );
put_word( res->data_size );
put_word( res->memopt );
put_word( res->name_offset );
put_word( 0 );
put_word( 0 );
data_offset += res->data_size;
}
}
put_word( 0 );
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
if (type->type->str) output_bin_string( type->type->str );
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
if (res->name.str) output_bin_string( res->name.str );
}
put_byte( 0 );
free_resource_tree( tree );
}