push c6aba86b6d7f7dbe994528d67faed47594362b60
[wine/hacks.git] / tools / winebuild / res32.c
blob739b322b695524bf036830ec4e99825678f33a9b
1 /*
2 * Builtin dlls resource support
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #include <fcntl.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
40 #include "build.h"
42 typedef unsigned short WCHAR;
44 /* Unicode string or integer id */
45 struct string_id
47 WCHAR *str; /* ptr to Unicode string */
48 unsigned short id; /* integer id if str is NULL */
51 /* descriptor for a resource */
52 struct resource
54 struct string_id type;
55 struct string_id name;
56 const void *data;
57 unsigned int data_size;
58 unsigned short lang;
61 /* name level of the resource tree */
62 struct res_name
64 const struct string_id *name; /* name */
65 const struct resource *res; /* resource */
66 int nb_languages; /* number of languages */
67 unsigned int name_offset; /* offset of name in resource dir */
70 /* type level of the resource tree */
71 struct res_type
73 const struct string_id *type; /* type name */
74 struct res_name *names; /* names array */
75 unsigned int nb_names; /* total number of names */
76 unsigned int nb_id_names; /* number of names that have a numeric id */
77 unsigned int name_offset; /* offset of type name in resource dir */
80 /* top level of the resource tree */
81 struct res_tree
83 struct res_type *types; /* types array */
84 unsigned int nb_types; /* total number of types */
87 static int byte_swapped; /* whether the current resource file is byte-swapped */
88 static const unsigned char *file_pos; /* current position in resource file */
89 static const unsigned char *file_end; /* end of resource file */
90 static const char *file_name; /* current resource file name */
92 /* size of a resource directory with n entries */
93 #define RESOURCE_DIR_SIZE (4 * sizeof(unsigned int))
94 #define RESOURCE_DIR_ENTRY_SIZE (2 * sizeof(unsigned int))
95 #define RESOURCE_DATA_ENTRY_SIZE (4 * sizeof(unsigned int))
96 #define RESDIR_SIZE(n) (RESOURCE_DIR_SIZE + (n) * RESOURCE_DIR_ENTRY_SIZE)
99 static inline struct resource *add_resource( DLLSPEC *spec )
101 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
102 return &spec->resources[spec->nb_resources++];
105 static inline unsigned int strlenW( const WCHAR *str )
107 const WCHAR *s = str;
108 while (*s) s++;
109 return s - str;
112 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
114 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
115 return *str1 - *str2;
118 static struct res_name *add_name( struct res_type *type, const struct resource *res )
120 struct res_name *name;
121 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
122 name = &type->names[type->nb_names++];
123 name->name = &res->name;
124 name->res = res;
125 name->nb_languages = 1;
126 if (!name->name->str) type->nb_id_names++;
127 return name;
130 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
132 struct res_type *type;
133 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
134 type = &tree->types[tree->nb_types++];
135 type->type = &res->type;
136 type->names = NULL;
137 type->nb_names = 0;
138 type->nb_id_names = 0;
139 return type;
142 /* get the next word from the current resource file */
143 static unsigned short get_word(void)
145 unsigned short ret = *(const unsigned short *)file_pos;
146 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
147 file_pos += sizeof(unsigned short);
148 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
149 return ret;
152 /* get the next dword from the current resource file */
153 static unsigned int get_dword(void)
155 unsigned int ret = *(const unsigned int *)file_pos;
156 if (byte_swapped)
157 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
158 file_pos += sizeof(unsigned int);
159 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
160 return ret;
163 /* get a string from the current resource file */
164 static void get_string( struct string_id *str )
166 if (*(const WCHAR *)file_pos == 0xffff)
168 get_word(); /* skip the 0xffff */
169 str->str = NULL;
170 str->id = get_word();
172 else
174 WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
175 str->str = p;
176 str->id = 0;
177 while ((*p++ = get_word()));
181 /* check the file header */
182 /* all values must be zero except header size */
183 static int check_header(void)
185 unsigned int size;
187 if (get_dword()) return 0; /* data size */
188 size = get_dword(); /* header size */
189 if (size == 0x20000000) byte_swapped = 1;
190 else if (size != 0x20) return 0;
191 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
192 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
193 if (get_dword()) return 0; /* data version */
194 if (get_word()) return 0; /* mem options */
195 if (get_word()) return 0; /* language */
196 if (get_dword()) return 0; /* version */
197 if (get_dword()) return 0; /* characteristics */
198 return 1;
201 /* load the next resource from the current file */
202 static void load_next_resource( DLLSPEC *spec )
204 unsigned int hdr_size;
205 struct resource *res = add_resource( spec );
207 res->data_size = (get_dword() + 3) & ~3;
208 hdr_size = get_dword();
209 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
211 res->data = file_pos - 2*sizeof(unsigned int) + hdr_size;
212 get_string( &res->type );
213 get_string( &res->name );
214 if ((unsigned long)file_pos & 2) get_word(); /* align to dword boundary */
215 get_dword(); /* skip data version */
216 get_word(); /* skip mem options */
217 res->lang = get_word();
218 get_dword(); /* skip version */
219 get_dword(); /* skip characteristics */
221 file_pos = (const unsigned char *)res->data + res->data_size;
222 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
225 /* load a Win32 .res file */
226 int load_res32_file( const char *name, DLLSPEC *spec )
228 int fd, ret;
229 void *base;
230 struct stat st;
232 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
233 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
234 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
235 #ifdef HAVE_MMAP
236 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
237 #endif /* HAVE_MMAP */
239 base = xmalloc( st.st_size );
240 if (read( fd, base, st.st_size ) != st.st_size)
241 fatal_error( "Cannot read %s\n", name );
244 byte_swapped = 0;
245 file_name = name;
246 file_pos = base;
247 file_end = file_pos + st.st_size;
248 if ((ret = check_header()))
250 while (file_pos < file_end) load_next_resource( spec );
252 close( fd );
253 return ret;
256 /* compare two unicode strings/ids */
257 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
259 if (!str1->str)
261 if (!str2->str) return str1->id - str2->id;
262 return 1; /* an id compares larger than a string */
264 if (!str2->str) return -1;
265 return strcmpW( str1->str, str2->str );
268 /* compare two resources for sorting the resource directory */
269 /* resources are stored first by type, then by name, then by language */
270 static int cmp_res( const void *ptr1, const void *ptr2 )
272 const struct resource *res1 = ptr1;
273 const struct resource *res2 = ptr2;
274 int ret;
276 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
277 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
278 return res1->lang - res2->lang;
281 /* build the 3-level (type,name,language) resource tree */
282 static struct res_tree *build_resource_tree( DLLSPEC *spec )
284 unsigned int i;
285 struct res_tree *tree;
286 struct res_type *type = NULL;
287 struct res_name *name = NULL;
289 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
291 tree = xmalloc( sizeof(*tree) );
292 tree->types = NULL;
293 tree->nb_types = 0;
295 for (i = 0; i < spec->nb_resources; i++)
297 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
299 type = add_type( tree, &spec->resources[i] );
300 name = add_name( type, &spec->resources[i] );
302 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
304 name = add_name( type, &spec->resources[i] );
306 else name->nb_languages++;
308 return tree;
311 /* free the resource tree */
312 static void free_resource_tree( struct res_tree *tree )
314 unsigned int i;
316 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
317 free( tree->types );
318 free( tree );
321 /* output a Unicode string */
322 static void output_string( const WCHAR *name )
324 int i, len = strlenW(name);
325 output( "\t%s 0x%04x", get_asm_short_keyword(), len );
326 for (i = 0; i < len; i++) output( ",0x%04x", name[i] );
327 output( " /* " );
328 for (i = 0; i < len; i++) output( "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
329 output( " */\n" );
332 /* output a resource directory */
333 static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
335 output( "\t.long 0\n" ); /* Characteristics */
336 output( "\t.long 0\n" ); /* TimeDateStamp */
337 output( "\t%s 0,0\n", /* Major/MinorVersion */
338 get_asm_short_keyword() );
339 output( "\t%s %u,%u\n", /* NumberOfNamed/IdEntries */
340 get_asm_short_keyword(), nb_names, nb_ids );
343 /* output the resource definitions */
344 void output_resources( DLLSPEC *spec )
346 int k, nb_id_types;
347 unsigned int i, n, offset, data_offset;
348 struct res_tree *tree;
349 struct res_type *type;
350 struct res_name *name;
351 const struct resource *res;
353 if (!spec->nb_resources) return;
355 tree = build_resource_tree( spec );
357 /* compute the offsets */
359 offset = RESDIR_SIZE( tree->nb_types );
360 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
362 offset += RESDIR_SIZE( type->nb_names );
363 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
364 offset += RESDIR_SIZE( name->nb_languages );
366 offset += spec->nb_resources * RESOURCE_DATA_ENTRY_SIZE;
368 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
370 if (type->type->str)
372 type->name_offset = offset | 0x80000000;
373 offset += (strlenW(type->type->str)+1) * sizeof(WCHAR);
375 else
377 type->name_offset = type->type->id;
378 nb_id_types++;
381 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
383 if (name->name->str)
385 name->name_offset = offset | 0x80000000;
386 offset += (strlenW(name->name->str)+1) * sizeof(WCHAR);
388 else name->name_offset = name->name->id;
392 /* output the resource directories */
394 output( "\n/* resources */\n\n" );
395 output( "\t.data\n" );
396 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
397 output( ".L__wine_spec_resources:\n" );
399 output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
401 /* dump the type directory */
403 offset = RESDIR_SIZE( tree->nb_types );
404 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
406 output( "\t.long 0x%08x,0x%08x\n",
407 type->name_offset, offset | 0x80000000 );
408 offset += RESDIR_SIZE( type->nb_names );
409 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
410 offset += RESDIR_SIZE( name->nb_languages );
413 data_offset = offset;
414 offset = RESDIR_SIZE( tree->nb_types );
416 /* dump the names and languages directories */
418 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
420 output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
421 offset += RESDIR_SIZE( type->nb_names );
422 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
424 output( "\t.long 0x%08x,0x%08x\n",
425 name->name_offset, offset | 0x80000000 );
426 offset += RESDIR_SIZE( name->nb_languages );
429 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
431 output_res_dir( 0, name->nb_languages );
432 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
434 unsigned int entry_offset = (res - spec->resources) * RESOURCE_DATA_ENTRY_SIZE;
435 output( "\t.long 0x%08x,0x%08x\n", res->lang, data_offset + entry_offset );
440 /* dump the resource data entries */
442 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
443 output( "\t.long .L__wine_spec_res_%d-.L__wine_spec_rva_base,%u,0,0\n",
444 i, res->data_size );
446 /* dump the name strings */
448 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
450 if (type->type->str) output_string( type->type->str );
451 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
452 if (name->name->str) output_string( name->name->str );
455 /* resource data */
457 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
459 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
460 output( ".L__wine_spec_res_%d:\n", i );
461 dump_bytes( res->data, res->data_size );
463 output( ".L__wine_spec_resources_end:\n" );
464 output( "\t.byte 0\n" );
466 free_resource_tree( tree );