#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;
};
static struct res_type *add_type( struct array *types, struct resource *res )
{
struct res_type *type = ARRAY_ADD( types, struct res_type );
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 = ARRAY_ADD( &spec->resources, struct resource );
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 array build_resource_tree( DLLSPEC *spec )
{
unsigned int j, offset;
struct array types = empty_array;
struct res_type *type = NULL;
struct resource *res;
ARRAY_SORT( &spec->resources, struct resource, cmp_res );
offset = 2;
ARRAY_FOR_EACH( res, &spec->resources, struct resource )
{
if (!type || cmp_string( &res->type, &res[-1].type ))
{
type = add_type( &types, res );
offset += 8;
}
type->nb_names++;
offset += 12;
}
offset += 2;
ARRAY_FOR_EACH( type, &types, struct res_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 types;
}
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 )
{
unsigned int i = 0;
ARRAY_FOR_EACH( res, &spec->resources, const struct resource )
{
output( ".L__wine_spec_resource_%u:\n", i++ );
dump_bytes( res->data, res->data_size );
}
}
void output_res16_directory( DLLSPEC *spec )
{
unsigned int j;
const struct resource *res;
struct array types = build_resource_tree( spec );
output( "\n.L__wine_spec_ne_rsrctab:\n" );
output( "\t.short 0\n" );
ARRAY_FOR_EACH( type, &types, const struct res_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 - (struct resource *)spec->resources.data), res->data_size );
output( "\t.short 0x%04x,0x%04x,0,0\n", res->memopt, res->name_offset );
}
}
output( "\t.short 0\n" );
ARRAY_FOR_EACH( type, &types, const struct res_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" );
}
void output_bin_res16_data( DLLSPEC *spec )
{
ARRAY_FOR_EACH( res, &spec->resources, const struct resource ) put_data( res->data, res->data_size );
}
void output_bin_res16_directory( DLLSPEC *spec, unsigned int data_offset )
{
unsigned int j;
const struct resource *res;
struct array types = build_resource_tree( spec );
put_word( 0 );
ARRAY_FOR_EACH( type, &types, const struct res_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 );
ARRAY_FOR_EACH( type, &types, const struct res_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 );
}