wbemprox: Always convert from BSTR.
[wine] / tools / winebuild / res16.c
1 /*
2  * Builtin dlls resource support
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #include <fcntl.h>
34
35 #include "build.h"
36
37 /* Unicode string or integer id */
38 struct string_id
39 {
40     char  *str;  /* ptr to string */
41     unsigned short id;   /* integer id if str is NULL */
42 };
43
44 /* descriptor for a resource */
45 struct resource
46 {
47     struct string_id type;
48     struct string_id name;
49     const void      *data;
50     unsigned int     name_offset;
51     unsigned int     data_size;
52     unsigned int     memopt;
53 };
54
55 /* type level of the resource tree */
56 struct res_type
57 {
58     const struct string_id  *type;         /* type name */
59     struct resource         *res;          /* first resource of this type */
60     unsigned int             name_offset;  /* name offset if string */
61     unsigned int             nb_names;     /* total number of names */
62 };
63
64 /* top level of the resource tree */
65 struct res_tree
66 {
67     struct res_type *types;                /* types array */
68     unsigned int     nb_types;             /* total number of types */
69 };
70
71
72 static inline struct resource *add_resource( DLLSPEC *spec )
73 {
74     spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
75     return &spec->resources[spec->nb_resources++];
76 }
77
78 static struct res_type *add_type( struct res_tree *tree, struct resource *res )
79 {
80     struct res_type *type;
81     tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
82     type = &tree->types[tree->nb_types++];
83     type->type        = &res->type;
84     type->res         = res;
85     type->nb_names    = 0;
86     return type;
87 }
88
89 /* get a string from the current resource file */
90 static void get_string( struct string_id *str )
91 {
92     unsigned char c = get_byte();
93
94     if (c == 0xff)
95     {
96         str->str = NULL;
97         str->id = get_word();
98     }
99     else
100     {
101         str->str = (char *)input_buffer + input_buffer_pos - 1;
102         str->id = 0;
103         while (get_byte()) /* nothing */;
104     }
105 }
106
107 /* load the next resource from the current file */
108 static void load_next_resource( DLLSPEC *spec )
109 {
110     struct resource *res = add_resource( spec );
111
112     get_string( &res->type );
113     get_string( &res->name );
114     res->memopt    = get_word();
115     res->data_size = get_dword();
116     res->data      = input_buffer + input_buffer_pos;
117     input_buffer_pos += res->data_size;
118     if (input_buffer_pos > input_buffer_size)
119         fatal_error( "%s is a truncated/corrupted file\n", input_buffer_filename );
120 }
121
122 /* load a Win16 .res file */
123 void load_res16_file( const char *name, DLLSPEC *spec )
124 {
125     init_input_buffer( name );
126     while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
127 }
128
129 /* compare two strings/ids */
130 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
131 {
132     if (!str1->str)
133     {
134         if (!str2->str) return str1->id - str2->id;
135         return 1;  /* an id compares larger than a string */
136     }
137     if (!str2->str) return -1;
138     return strcasecmp( str1->str, str2->str );
139 }
140
141 /* compare two resources for sorting the resource directory */
142 /* resources are stored first by type, then by name */
143 static int cmp_res( const void *ptr1, const void *ptr2 )
144 {
145     const struct resource *res1 = ptr1;
146     const struct resource *res2 = ptr2;
147     int ret;
148
149     if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
150     return cmp_string( &res1->name, &res2->name );
151 }
152
153 /* build the 2-level (type,name) resource tree */
154 static struct res_tree *build_resource_tree( DLLSPEC *spec )
155 {
156     unsigned int i, j, offset;
157     struct res_tree *tree;
158     struct res_type *type = NULL;
159     struct resource *res;
160
161     qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
162
163     offset = 2;  /* alignment */
164     tree = xmalloc( sizeof(*tree) );
165     tree->types = NULL;
166     tree->nb_types = 0;
167
168     for (i = 0; i < spec->nb_resources; i++)
169     {
170         if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
171         {
172             type = add_type( tree, &spec->resources[i] );
173             offset += 8;
174         }
175         type->nb_names++;
176         offset += 12;
177     }
178     offset += 2;  /* terminator */
179
180     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
181     {
182         if (type->type->str)
183         {
184             type->name_offset = offset;
185             offset += strlen(type->type->str) + 1;
186         }
187         else type->name_offset = type->type->id | 0x8000;
188
189         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
190         {
191             if (res->name.str)
192             {
193                 res->name_offset = offset;
194                 offset += strlen(res->name.str) + 1;
195             }
196             else res->name_offset = res->name.id | 0x8000;
197         }
198     }
199     return tree;
200 }
201
202 /* free the resource tree */
203 static void free_resource_tree( struct res_tree *tree )
204 {
205     free( tree->types );
206     free( tree );
207 }
208
209 /* output a string preceded by its length */
210 static void output_string( const char *str )
211 {
212     unsigned int i, len = strlen(str);
213     output( "\t.byte 0x%02x", len );
214     for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)str[i] );
215     output( " /* %s */\n", str );
216 }
217
218 /* output a string preceded by its length in binary format*/
219 static void output_bin_string( const char *str )
220 {
221     put_byte( strlen(str) );
222     while (*str) put_byte( *str++ );
223 }
224
225 /* output the resource data */
226 void output_res16_data( DLLSPEC *spec )
227 {
228     const struct resource *res;
229     unsigned int i;
230
231     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
232     {
233         output( ".L__wine_spec_resource_%u:\n", i );
234         dump_bytes( res->data, res->data_size );
235     }
236 }
237
238 /* output the resource definitions */
239 void output_res16_directory( DLLSPEC *spec )
240 {
241     unsigned int i, j;
242     struct res_tree *tree;
243     const struct res_type *type;
244     const struct resource *res;
245
246     tree = build_resource_tree( spec );
247
248     output( "\n.L__wine_spec_ne_rsrctab:\n" );
249     output( "\t%s 0\n", get_asm_short_keyword() );  /* alignment */
250
251     /* type and name structures */
252
253     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
254     {
255         output( "\t%s 0x%04x,%u,0,0\n", get_asm_short_keyword(), type->name_offset, type->nb_names );
256
257         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
258         {
259             output( "\t%s .L__wine_spec_resource_%lu-.L__wine_spec_dos_header,%u\n",
260                     get_asm_short_keyword(), (unsigned long)(res - spec->resources), res->data_size );
261             output( "\t%s 0x%04x,0x%04x,0,0\n", get_asm_short_keyword(), res->memopt, res->name_offset );
262         }
263     }
264     output( "\t%s 0\n", get_asm_short_keyword() );  /* terminator */
265
266     /* name strings */
267
268     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
269     {
270         if (type->type->str) output_string( type->type->str );
271         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
272             if (res->name.str) output_string( res->name.str );
273     }
274     output( "\t.byte 0\n" );  /* names terminator */
275
276     free_resource_tree( tree );
277 }
278
279 /* output the resource data in binary format */
280 void output_bin_res16_data( DLLSPEC *spec )
281 {
282     const struct resource *res;
283     unsigned int i;
284
285     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
286         put_data( res->data, res->data_size );
287 }
288
289 /* output the resource definitions in binary format */
290 void output_bin_res16_directory( DLLSPEC *spec, unsigned int data_offset )
291 {
292     unsigned int i, j;
293     struct res_tree *tree;
294     const struct res_type *type;
295     const struct resource *res;
296
297     tree = build_resource_tree( spec );
298
299     put_word( 0 );  /* alignment */
300
301     /* type and name structures */
302
303     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
304     {
305         put_word( type->name_offset );
306         put_word( type->nb_names );
307         put_word( 0 );
308         put_word( 0 );
309
310         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
311         {
312             put_word( data_offset );
313             put_word( res->data_size );
314             put_word( res->memopt );
315             put_word( res->name_offset );
316             put_word( 0 );
317             put_word( 0 );
318             data_offset += res->data_size;
319         }
320     }
321     put_word( 0 );  /* terminator */
322
323     /* name strings */
324
325     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
326     {
327         if (type->type->str) output_bin_string( type->type->str );
328         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
329             if (res->name.str) output_bin_string( res->name.str );
330     }
331     put_byte( 0 );  /* names terminator */
332
333     free_resource_tree( tree );
334 }