/*
 * Builtin dlls resource support
 *
 * Copyright 2000 Alexandre Julliard
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#include "build.h"

/* Unicode string or integer id */
struct string_id
{
    char  *str;  /* ptr to string */
    unsigned short 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     name_offset;
    unsigned int     data_size;
    unsigned int     memopt;
};

/* type level of the resource tree */
struct res_type
{
    const struct string_id  *type;         /* type name */
    struct resource         *res;          /* first resource of this type */
    unsigned int             name_offset;  /* name offset if string */
    unsigned int             nb_names;     /* total number of 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;
}

/* get a string from the current resource file */
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()) /* nothing */;
    }
}

/* load the next resource from the current file */
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 );
}

/* load a Win16 .res file */
void load_res16_file( const char *name, DLLSPEC *spec )
{
    init_input_buffer( name );
    while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
}

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

    ARRAY_FOR_EACH( res, &spec->resources, struct resource )
    {
        if (!type || cmp_string( &res->type, &res[-1].type ))  /* new type */
        {
            type = add_type( &types, res );
            offset += 8;
        }
        type->nb_names++;
        offset += 12;
    }
    offset += 2;  /* terminator */

    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;
}

/* output a string preceded by its length */
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 );
}

/* output a string preceded by its length in binary format*/
static void output_bin_string( const char *str )
{
    put_byte( strlen(str) );
    while (*str) put_byte( *str++ );
}

/* output the resource data */
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 );
    }
}

/* output the resource definitions */
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" );  /* alignment */

    /* type and name structures */

    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" );  /* terminator */

    /* name strings */

    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" );  /* names terminator */
}

/* output the resource data in binary format */
void output_bin_res16_data( DLLSPEC *spec )
{
    ARRAY_FOR_EACH( res, &spec->resources, const struct resource ) put_data( res->data, res->data_size );
}

/* output the resource definitions in binary format */
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 );  /* alignment */

    /* type and name structures */

    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 );  /* terminator */

    /* name strings */

    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 );  /* names terminator */
}
