From 0a528bf3c1c5b19f497e1a9f0848b6e191394a88 Mon Sep 17 00:00:00 2001
From: Dmitry Timoshkov <dmitry@baikal.ru>
Date: Wed, 20 Jan 2016 13:26:49 +0800
Subject: widl: Create library block index right after the CompObj one.

Otherwise Wine's oleaut32 refuses to load a typelib.
---
 tools/widl/write_sltg.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/tools/widl/write_sltg.c b/tools/widl/write_sltg.c
index 79c0f0ee..650fdfa7 100644
--- a/tools/widl/write_sltg.c
+++ b/tools/widl/write_sltg.c
@@ -374,15 +374,13 @@ static void init_library(struct sltg_typelib *sltg)
     }
 }
 
-static void add_block(struct sltg_typelib *sltg, void *data, int size, const char *name)
+static void add_block_index(struct sltg_typelib *sltg, void *data, int size, int index)
 {
     struct sltg_block *block = xmalloc(sizeof(*block));
 
-    chat("add_block: %p,%d,\"%s\"\n", data, size, name);
-
     block->length = size;
     block->data = data;
-    block->index_string = add_index(&sltg->index, name);
+    block->index_string = index;
     block->next = NULL;
 
     if (sltg->blocks)
@@ -400,17 +398,28 @@ static void add_block(struct sltg_typelib *sltg, void *data, int size, const cha
     sltg->n_file_blocks++;
 }
 
-static void add_library_block(struct sltg_typelib *typelib)
+static void add_block(struct sltg_typelib *sltg, void *data, int size, const char *name)
+{
+    struct sltg_block *block = xmalloc(sizeof(*block));
+    int index;
+
+    chat("add_block: %p,%d,\"%s\"\n", data, size, name);
+
+    index = add_index(&sltg->index, name);
+
+    add_block_index(sltg, data, size, index);
+}
+
+static void *create_library_block(struct sltg_typelib *typelib, int *size, int *index)
 {
     void *block;
     short *p;
-    int size;
 
-    size = sizeof(short) * 9 + sizeof(int) * 3 + sizeof(GUID);
-    if (typelib->library.helpstring) size += strlen(typelib->library.helpstring);
-    if (typelib->library.helpfile) size += strlen(typelib->library.helpfile);
+    *size = sizeof(short) * 9 + sizeof(int) * 3 + sizeof(GUID);
+    if (typelib->library.helpstring) *size += strlen(typelib->library.helpstring);
+    if (typelib->library.helpfile) *size += strlen(typelib->library.helpfile);
 
-    block = xmalloc(size);
+    block = xmalloc(*size);
     p = block;
     *p++ = 0x51cc; /* magic */
     *p++ = 3; /* res02 */
@@ -443,7 +452,9 @@ static void add_library_block(struct sltg_typelib *typelib)
     p += 2;
     *(GUID *)p = typelib->library.uuid;
 
-    add_block(typelib, block, size, "dir");
+    *index = add_index(&typelib->index, "dir");
+
+    return block;
 }
 
 static const char *new_index_name(void)
@@ -1763,6 +1774,8 @@ int create_sltg_typelib(typelib_t *typelib)
 {
     struct sltg_typelib sltg;
     const statement_t *stmt;
+    void *library_block;
+    int library_block_size, library_block_index;
 
     sltg.typelib = typelib;
     sltg.typeinfo_count = 0;
@@ -1776,11 +1789,13 @@ int create_sltg_typelib(typelib_t *typelib)
     init_name_table(&sltg);
     init_library(&sltg);
 
+    library_block = create_library_block(&sltg, &library_block_size, &library_block_index);
+
     if (typelib->stmts)
         LIST_FOR_EACH_ENTRY(stmt, typelib->stmts, const statement_t, entry)
             add_statement(&sltg, stmt);
 
-    add_library_block(&sltg);
+    add_block_index(&sltg, library_block, library_block_size, library_block_index);
 
     save_all_changes(&sltg);
 
-- 
2.19.1

