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
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
36 #ifdef HAVE_SYS_MMAN_H
44 /* Unicode string or integer id */
47 WCHAR *str; /* ptr to Unicode string */
48 WORD id; /* integer id if str is NULL */
51 /* descriptor for a resource */
54 struct string_id type;
55 struct string_id name;
57 unsigned int data_size;
61 /* name level of the resource tree */
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 */
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 static struct resource *resources;
79 static int nb_resources;
81 static struct res_type *res_types;
82 static int nb_types; /* total number of types */
83 static int nb_id_types; /* number of types that have a numeric id */
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(void)
92 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
93 return &resources[nb_resources++];
96 static inline unsigned int strlenW( const WCHAR *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;
116 name->nb_languages = 1;
117 if (!name->name->str) type->nb_id_names++;
121 static struct res_type *add_type( const struct resource *res )
123 struct res_type *type;
124 res_types = xrealloc( res_types, (nb_types + 1) * sizeof(*res_types) );
125 type = &res_types[nb_types++];
126 type->type = &res->type;
129 type->nb_id_names = 0;
130 if (!type->type->str) nb_id_types++;
134 /* get the next word from the current resource file */
135 static WORD get_word(void)
137 WORD ret = *(WORD *)file_pos;
138 file_pos += sizeof(WORD);
139 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
143 /* get the next dword from the current resource file */
144 static DWORD get_dword(void)
146 DWORD ret = *(DWORD *)file_pos;
147 file_pos += sizeof(DWORD);
148 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
152 /* get a string from the current resource file */
153 static void get_string( struct string_id *str )
155 if (*(WCHAR *)file_pos == 0xffff)
157 get_word(); /* skip the 0xffff */
159 str->id = get_word();
163 WCHAR *p = xmalloc( (strlenW((WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
166 while ((*p++ = get_word()));
170 /* check the file header */
171 /* all values must be zero except header size */
172 static int check_header(void)
174 if (get_dword()) return 0; /* data size */
175 if (get_dword() != 32) return 0; /* header size */
176 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
177 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
178 if (get_dword()) return 0; /* data version */
179 if (get_word()) return 0; /* mem options */
180 if (get_word()) return 0; /* language */
181 if (get_dword()) return 0; /* version */
182 if (get_dword()) return 0; /* characteristics */
186 /* load the next resource from the current file */
187 static void load_next_resource(void)
190 struct resource *res = add_resource();
192 res->data_size = (get_dword() + 3) & ~3;
193 hdr_size = get_dword();
194 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
196 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
197 get_string( &res->type );
198 get_string( &res->name );
199 if ((int)file_pos & 2) get_word(); /* align to dword boundary */
200 get_dword(); /* skip data version */
201 get_word(); /* skip mem options */
202 res->lang = get_word();
203 get_dword(); /* skip version */
204 get_dword(); /* skip characteristics */
206 file_pos = (char *)res->data + res->data_size;
207 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
210 /* load a Win32 .res file */
211 int load_res32_file( const char *name )
217 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
218 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
219 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
221 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
222 #endif /* HAVE_MMAP */
224 base = xmalloc( st.st_size );
225 if (read( fd, base, st.st_size ) != st.st_size)
226 fatal_error( "Cannot read %s\n", name );
231 file_end = file_pos + st.st_size;
232 if ((ret = check_header()))
234 while (file_pos < file_end) load_next_resource();
240 /* compare two unicode strings/ids */
241 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
245 if (!str2->str) return str1->id - str2->id;
246 return 1; /* an id compares larger than a string */
248 if (!str2->str) return -1;
249 return strcmpW( str1->str, str2->str );
252 /* compare two resources for sorting the resource directory */
253 /* resources are stored first by type, then by name, then by language */
254 static int cmp_res( const void *ptr1, const void *ptr2 )
256 const struct resource *res1 = ptr1;
257 const struct resource *res2 = ptr2;
260 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
261 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
262 return res1->lang - res2->lang;
265 /* build the 3-level (type,name,language) resource tree */
266 static void build_resource_tree(void)
269 struct res_type *type = NULL;
270 struct res_name *name = NULL;
272 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
274 for (i = 0; i < nb_resources; i++)
276 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
278 type = add_type( &resources[i] );
279 name = add_name( type, &resources[i] );
281 else if (cmp_string( &resources[i].name, &resources[i-1].name )) /* new name */
283 name = add_name( type, &resources[i] );
285 else name->nb_languages++;
289 /* output a Unicode string */
290 static void output_string( FILE *outfile, const WCHAR *name )
292 int i, len = strlenW(name);
293 fprintf( outfile, "0x%04x", len );
294 for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
295 fprintf( outfile, " /* " );
296 for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
297 fprintf( outfile, " */" );
300 /* output the resource definitions */
301 int output_resources( FILE *outfile )
305 const struct res_type *type;
306 const struct res_name *name;
307 const struct resource *res;
309 if (!nb_resources) return 0;
311 build_resource_tree();
315 for (i = 0, res = resources; i < nb_resources; i++, res++)
317 const unsigned int *p = res->data;
318 int size = res->data_size / 4;
319 /* dump data as ints to ensure correct alignment */
320 fprintf( outfile, "static const unsigned int res_%d[%d] = {\n ", i, size );
321 for (j = 0; j < size - 1; j++, p++)
323 fprintf( outfile, "0x%08x,", *p );
324 if ((j % 8) == 7) fprintf( outfile, "\n " );
326 fprintf( outfile, "0x%08x\n};\n\n", *p );
329 /* directory structures */
331 fprintf( outfile, "struct res_dir {\n" );
332 fprintf( outfile, " unsigned int Characteristics;\n" );
333 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
334 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
335 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
336 fprintf( outfile, "struct res_dir_entry {\n" );
337 fprintf( outfile, " unsigned int Name;\n" );
338 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
339 fprintf( outfile, "struct res_data_entry {\n" );
340 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
341 fprintf( outfile, " unsigned int Size;\n" );
342 fprintf( outfile, " unsigned int CodePage;\n" );
343 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
345 /* resource directory definition */
347 fprintf( outfile, "#define OFFSETOF(field) ((char*)&((struct res_struct *)0)->field - (char*)((struct res_struct *) 0))\n" );
348 fprintf( outfile, "static struct res_struct{\n" );
349 fprintf( outfile, " struct res_dir type_dir;\n" );
350 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", nb_types );
352 for (i = 0, type = res_types; i < nb_types; i++, type++)
354 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
355 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
356 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
358 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
359 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
360 i, n, name->nb_languages );
364 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", nb_resources );
366 for (i = 0, type = res_types; i < nb_types; i++, type++)
369 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
370 i, strlenW(type->type->str)+1 );
371 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
374 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
375 i, n, strlenW(name->name->str)+1 );
379 /* resource directory contents */
381 fprintf( outfile, "} resources = {\n" );
382 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", nb_types - nb_id_types, nb_id_types );
384 /* dump the type directory */
385 fprintf( outfile, " {\n" );
386 for (i = 0, type = res_types; i < nb_types; i++, type++)
388 if (!type->type->str)
389 fprintf( outfile, " { 0x%04x, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
392 fprintf( outfile, " { OFFSETOF(type_%d_name) | 0x80000000, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
395 fprintf( outfile, " },\n" );
397 /* dump the names and languages directories */
398 for (i = 0, type = res_types; i < nb_types; i++, type++)
400 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
401 type->nb_names - type->nb_id_names, type->nb_id_names, i );
402 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
404 if (!name->name->str)
405 fprintf( outfile, " { 0x%04x, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
406 name->name->id, i, n );
408 fprintf( outfile, " { OFFSETOF(name_%d_%d_name) | 0x80000000, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
411 fprintf( outfile, " },\n" );
413 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
415 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
416 name->nb_languages, i, n );
417 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
419 fprintf( outfile, " { 0x%04x, OFFSETOF(data_entries[%d]) },\n",
420 res->lang, res - resources );
422 fprintf( outfile, " },\n" );
426 /* dump the resource data entries */
427 fprintf( outfile, " {\n" );
428 for (i = 0, res = resources; i < nb_resources; i++, res++)
430 fprintf( outfile, " { res_%d, sizeof(res_%d), 0, 0 },\n", i, i );
433 /* dump the name strings */
434 for (i = 0, type = res_types; i < nb_types; i++, type++)
438 fprintf( outfile, " },\n { " );
439 output_string( outfile, type->type->str );
441 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
445 fprintf( outfile, " },\n { " );
446 output_string( outfile, name->name->str );
450 fprintf( outfile, " }\n};\n#undef OFFSETOF\n\n" );