Split the MSVCRT implementation headers from the public headers.
[wine/dcerpc.git] / tools / winebuild / res32.c
blobb56af9d1886258bb7bbae9b652f539557e04f59f
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "windef.h"
41 #include "winbase.h"
42 #include "build.h"
44 /* Unicode string or integer id */
45 struct string_id
47 WCHAR *str; /* ptr to Unicode string */
48 WORD 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 WORD 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 */
69 /* type level of the resource tree */
70 struct res_type
72 const struct string_id *type; /* type name */
73 struct res_name *names; /* names array */
74 unsigned int nb_names; /* total number of names */
75 unsigned int nb_id_names; /* number of names that have a numeric id */
78 /* top level of the resource tree */
79 struct res_tree
81 struct res_type *types; /* types array */
82 unsigned int nb_types; /* total number of types */
85 static const unsigned char *file_pos; /* current position in resource file */
86 static const unsigned char *file_end; /* end of resource file */
87 static const char *file_name; /* current resource file name */
90 inline static struct resource *add_resource( DLLSPEC *spec )
92 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
93 return &spec->resources[spec->nb_resources++];
96 static inline unsigned int strlenW( const WCHAR *str )
98 const WCHAR *s = str;
99 while (*s) s++;
100 return s - str;
103 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
105 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
106 return *str1 - *str2;
109 static struct res_name *add_name( struct res_type *type, const struct resource *res )
111 struct res_name *name;
112 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
113 name = &type->names[type->nb_names++];
114 name->name = &res->name;
115 name->res = res;
116 name->nb_languages = 1;
117 if (!name->name->str) type->nb_id_names++;
118 return name;
121 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
123 struct res_type *type;
124 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
125 type = &tree->types[tree->nb_types++];
126 type->type = &res->type;
127 type->names = NULL;
128 type->nb_names = 0;
129 type->nb_id_names = 0;
130 return type;
133 /* get the next word from the current resource file */
134 static WORD get_word(void)
136 WORD ret = *(const WORD *)file_pos;
137 file_pos += sizeof(WORD);
138 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
139 return ret;
142 /* get the next dword from the current resource file */
143 static DWORD get_dword(void)
145 DWORD ret = *(const DWORD *)file_pos;
146 file_pos += sizeof(DWORD);
147 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
148 return ret;
151 /* get a string from the current resource file */
152 static void get_string( struct string_id *str )
154 if (*(const WCHAR *)file_pos == 0xffff)
156 get_word(); /* skip the 0xffff */
157 str->str = NULL;
158 str->id = get_word();
160 else
162 WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
163 str->str = p;
164 str->id = 0;
165 while ((*p++ = get_word()));
169 /* check the file header */
170 /* all values must be zero except header size */
171 static int check_header(void)
173 if (get_dword()) return 0; /* data size */
174 if (get_dword() != 32) return 0; /* header size */
175 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
176 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
177 if (get_dword()) return 0; /* data version */
178 if (get_word()) return 0; /* mem options */
179 if (get_word()) return 0; /* language */
180 if (get_dword()) return 0; /* version */
181 if (get_dword()) return 0; /* characteristics */
182 return 1;
185 /* load the next resource from the current file */
186 static void load_next_resource( DLLSPEC *spec )
188 DWORD hdr_size;
189 struct resource *res = add_resource( spec );
191 res->data_size = (get_dword() + 3) & ~3;
192 hdr_size = get_dword();
193 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
195 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
196 get_string( &res->type );
197 get_string( &res->name );
198 if ((int)file_pos & 2) get_word(); /* align to dword boundary */
199 get_dword(); /* skip data version */
200 get_word(); /* skip mem options */
201 res->lang = get_word();
202 get_dword(); /* skip version */
203 get_dword(); /* skip characteristics */
205 file_pos = (const char *)res->data + res->data_size;
206 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
209 /* load a Win32 .res file */
210 int load_res32_file( const char *name, DLLSPEC *spec )
212 int fd, ret;
213 void *base;
214 struct stat st;
216 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
217 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
218 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
219 #ifdef HAVE_MMAP
220 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
221 #endif /* HAVE_MMAP */
223 base = xmalloc( st.st_size );
224 if (read( fd, base, st.st_size ) != st.st_size)
225 fatal_error( "Cannot read %s\n", name );
228 file_name = name;
229 file_pos = base;
230 file_end = file_pos + st.st_size;
231 if ((ret = check_header()))
233 while (file_pos < file_end) load_next_resource( spec );
235 close( fd );
236 return ret;
239 /* compare two unicode strings/ids */
240 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
242 if (!str1->str)
244 if (!str2->str) return str1->id - str2->id;
245 return 1; /* an id compares larger than a string */
247 if (!str2->str) return -1;
248 return strcmpW( str1->str, str2->str );
251 /* compare two resources for sorting the resource directory */
252 /* resources are stored first by type, then by name, then by language */
253 static int cmp_res( const void *ptr1, const void *ptr2 )
255 const struct resource *res1 = ptr1;
256 const struct resource *res2 = ptr2;
257 int ret;
259 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
260 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
261 return res1->lang - res2->lang;
264 /* build the 3-level (type,name,language) resource tree */
265 static struct res_tree *build_resource_tree( DLLSPEC *spec )
267 int i;
268 struct res_tree *tree;
269 struct res_type *type = NULL;
270 struct res_name *name = NULL;
272 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
274 tree = xmalloc( sizeof(*tree) );
275 tree->types = NULL;
276 tree->nb_types = 0;
278 for (i = 0; i < spec->nb_resources; i++)
280 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
282 type = add_type( tree, &spec->resources[i] );
283 name = add_name( type, &spec->resources[i] );
285 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
287 name = add_name( type, &spec->resources[i] );
289 else name->nb_languages++;
291 return tree;
294 /* free the resource tree */
295 static void free_resource_tree( struct res_tree *tree )
297 int i;
299 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
300 free( tree->types );
301 free( tree );
304 /* output a Unicode string */
305 static void output_string( FILE *outfile, const WCHAR *name )
307 int i, len = strlenW(name);
308 fprintf( outfile, "0x%04x", len );
309 for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
310 fprintf( outfile, " /* " );
311 for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
312 fprintf( outfile, " */" );
315 /* output the resource definitions */
316 void output_resources( FILE *outfile, DLLSPEC *spec )
318 int i, j, k, nb_id_types;
319 unsigned int n;
320 struct res_tree *tree;
321 const struct res_type *type;
322 const struct res_name *name;
323 const struct resource *res;
325 if (!spec->nb_resources) return;
327 tree = build_resource_tree( spec );
329 /* resource data */
331 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
333 const unsigned int *p = res->data;
334 int size = res->data_size / 4;
335 /* dump data as ints to ensure correct alignment */
336 fprintf( outfile, "static const unsigned int res_%d[%d] = {\n ", i, size );
337 for (j = 0; j < size - 1; j++, p++)
339 fprintf( outfile, "0x%08x,", *p );
340 if ((j % 8) == 7) fprintf( outfile, "\n " );
342 fprintf( outfile, "0x%08x\n};\n\n", *p );
345 /* directory structures */
347 fprintf( outfile, "struct res_dir {\n" );
348 fprintf( outfile, " unsigned int Characteristics;\n" );
349 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
350 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
351 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
352 fprintf( outfile, "struct res_dir_entry {\n" );
353 fprintf( outfile, " unsigned int Name;\n" );
354 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
355 fprintf( outfile, "struct res_data_entry {\n" );
356 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
357 fprintf( outfile, " unsigned int Size;\n" );
358 fprintf( outfile, " unsigned int CodePage;\n" );
359 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
361 /* resource directory definition */
363 fprintf( outfile, "#define OFFSETOF(field) ((char*)&((struct res_struct *)0)->field - (char*)((struct res_struct *) 0))\n" );
364 fprintf( outfile, "static struct res_struct{\n" );
365 fprintf( outfile, " struct res_dir type_dir;\n" );
366 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", tree->nb_types );
368 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
370 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
371 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
372 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
374 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
375 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
376 i, n, name->nb_languages );
380 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", spec->nb_resources );
382 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
384 if (type->type->str)
385 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
386 i, strlenW(type->type->str)+1 );
387 else
388 nb_id_types++;
390 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
392 if (name->name->str)
393 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
394 i, n, strlenW(name->name->str)+1 );
398 /* resource directory contents */
400 fprintf( outfile, "} resources = {\n" );
401 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", tree->nb_types - nb_id_types, nb_id_types );
403 /* dump the type directory */
404 fprintf( outfile, " {\n" );
405 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
407 if (!type->type->str)
408 fprintf( outfile, " { 0x%04x, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
409 type->type->id, i );
410 else
411 fprintf( outfile, " { OFFSETOF(type_%d_name) | 0x80000000, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
412 i, i );
414 fprintf( outfile, " },\n" );
416 /* dump the names and languages directories */
417 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
419 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
420 type->nb_names - type->nb_id_names, type->nb_id_names, i );
421 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
423 if (!name->name->str)
424 fprintf( outfile, " { 0x%04x, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
425 name->name->id, i, n );
426 else
427 fprintf( outfile, " { OFFSETOF(name_%d_%d_name) | 0x80000000, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
428 i, n, i, n );
430 fprintf( outfile, " },\n" );
432 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
434 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
435 name->nb_languages, i, n );
436 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
438 fprintf( outfile, " { 0x%04x, OFFSETOF(data_entries[%d]) },\n",
439 res->lang, res - spec->resources );
441 fprintf( outfile, " },\n" );
445 /* dump the resource data entries */
446 fprintf( outfile, " {\n" );
447 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
449 fprintf( outfile, " { res_%d, sizeof(res_%d), 0, 0 },\n", i, i );
452 /* dump the name strings */
453 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
455 if (type->type->str)
457 fprintf( outfile, " },\n { " );
458 output_string( outfile, type->type->str );
460 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
462 if (name->name->str)
464 fprintf( outfile, " },\n { " );
465 output_string( outfile, name->name->str );
469 fprintf( outfile, " }\n};\n#undef OFFSETOF\n\n" );
470 free_resource_tree( tree );