Eliminate unitialized garbage being returned from LISTVIEW_GetItemA.
[wine.git] / tools / winebuild / res32.c
blob75f6566fcbea0de94f234cdd6e4aec378f2cf662
1 /*
2 * Builtin dlls resource support
4 * Copyright 2000 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/mman.h>
19 #endif
21 #include "winbase.h"
22 #include "wine/unicode.h"
23 #include "build.h"
25 /* Unicode string or integer id */
26 struct string_id
28 WCHAR *str; /* ptr to Unicode string */
29 WORD id; /* integer id if str is NULL */
32 /* descriptor for a resource */
33 struct resource
35 struct string_id type;
36 struct string_id name;
37 const void *data;
38 unsigned int data_size;
39 WORD lang;
42 /* name level of the resource tree */
43 struct res_name
45 const struct string_id *name; /* name */
46 const struct resource *res; /* resource */
47 int nb_languages; /* number of languages */
50 /* type level of the resource tree */
51 struct res_type
53 const struct string_id *type; /* type name */
54 struct res_name *names; /* names array */
55 unsigned int nb_names; /* total number of names */
56 unsigned int nb_id_names; /* number of names that have a numeric id */
59 static struct resource *resources;
60 static int nb_resources;
62 static struct res_type *res_types;
63 static int nb_types; /* total number of types */
64 static int nb_id_types; /* number of types that have a numeric id */
66 static const unsigned char *file_pos; /* current position in resource file */
67 static const unsigned char *file_end; /* end of resource file */
68 static const char *file_name; /* current resource file name */
71 inline static struct resource *add_resource(void)
73 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
74 return &resources[nb_resources++];
77 static struct res_name *add_name( struct res_type *type, const struct resource *res )
79 struct res_name *name;
80 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
81 name = &type->names[type->nb_names++];
82 name->name = &res->name;
83 name->res = res;
84 name->nb_languages = 1;
85 if (!name->name->str) type->nb_id_names++;
86 return name;
89 static struct res_type *add_type( const struct resource *res )
91 struct res_type *type;
92 res_types = xrealloc( res_types, (nb_types + 1) * sizeof(*res_types) );
93 type = &res_types[nb_types++];
94 type->type = &res->type;
95 type->names = NULL;
96 type->nb_names = 0;
97 type->nb_id_names = 0;
98 if (!type->type->str) nb_id_types++;
99 return type;
102 /* get the next word from the current resource file */
103 static WORD get_word(void)
105 WORD ret = *(WORD *)file_pos;
106 file_pos += sizeof(WORD);
107 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
108 return ret;
111 /* get the next dword from the current resource file */
112 static DWORD get_dword(void)
114 DWORD ret = *(DWORD *)file_pos;
115 file_pos += sizeof(DWORD);
116 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
117 return ret;
120 /* get a string from the current resource file */
121 static void get_string( struct string_id *str )
123 if (*(WCHAR *)file_pos == 0xffff)
125 get_word(); /* skip the 0xffff */
126 str->str = NULL;
127 str->id = get_word();
129 else
131 WCHAR *p = xmalloc( (strlenW((WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
132 str->str = p;
133 str->id = 0;
134 while ((*p++ = get_word()));
138 /* check the file header */
139 /* all values must be zero except header size */
140 static void check_header(void)
142 if (get_dword()) goto error; /* data size */
143 if (get_dword() != 32) goto error; /* header size */
144 if (get_word() != 0xffff || get_word()) goto error; /* type, must be id 0 */
145 if (get_word() != 0xffff || get_word()) goto error; /* name, must be id 0 */
146 if (get_dword()) goto error; /* data version */
147 if (get_word()) goto error; /* mem options */
148 if (get_word()) goto error; /* language */
149 if (get_dword()) goto error; /* version */
150 if (get_dword()) goto error; /* characteristics */
151 return;
152 error:
153 fatal_error( "%s is not a valid Win32 resource file\n", file_name );
156 /* load the next resource from the current file */
157 static void load_next_resource(void)
159 DWORD hdr_size;
160 struct resource *res = add_resource();
162 res->data_size = (get_dword() + 3) & ~3;
163 hdr_size = get_dword();
164 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
166 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
167 get_string( &res->type );
168 get_string( &res->name );
169 if ((int)file_pos & 2) get_word(); /* align to dword boundary */
170 get_dword(); /* skip data version */
171 get_word(); /* skip mem options */
172 res->lang = get_word();
173 get_dword(); /* skip version */
174 get_dword(); /* skip characteristics */
176 file_pos = (char *)res->data + res->data_size;
177 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
180 /* load a Win32 .res file */
181 void load_res32_file( const char *name )
183 int fd;
184 void *base;
185 struct stat st;
187 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
188 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
189 if (!st.st_size) fatal_error( "%s is an empty file\n" );
190 #ifdef HAVE_MMAP
191 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
192 #endif /* HAVE_MMAP */
194 base = xmalloc( st.st_size );
195 if (read( fd, base, st.st_size ) != st.st_size)
196 fatal_error( "Cannot read %s\n", name );
199 file_name = name;
200 file_pos = base;
201 file_end = file_pos + st.st_size;
202 check_header();
203 while (file_pos < file_end) load_next_resource();
206 /* compare two unicode strings/ids */
207 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
209 if (!str1->str)
211 if (!str2->str) return str1->id - str2->id;
212 return 1; /* an id compares larger than a string */
214 if (!str2->str) return -1;
215 return strcmpiW( str1->str, str2->str );
218 /* compare two resources for sorting the resource directory */
219 /* resources are stored first by type, then by name, then by language */
220 static int cmp_res( const void *ptr1, const void *ptr2 )
222 const struct resource *res1 = ptr1;
223 const struct resource *res2 = ptr2;
224 int ret;
226 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
227 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
228 return res1->lang - res2->lang;
231 /* build the 3-level (type,name,language) resource tree */
232 static void build_resource_tree(void)
234 int i;
235 struct res_type *type = NULL;
236 struct res_name *name = NULL;
238 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
240 for (i = 0; i < nb_resources; i++)
242 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
244 type = add_type( &resources[i] );
245 name = add_name( type, &resources[i] );
247 else if (cmp_string( &resources[i].name, &resources[i-1].name )) /* new name */
249 name = add_name( type, &resources[i] );
251 else name->nb_languages++;
255 /* output a Unicode string */
256 static void output_string( FILE *outfile, const WCHAR *name )
258 int i, len = strlenW(name);
259 fprintf( outfile, "0x%04x", len );
260 for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
261 fprintf( outfile, " /* " );
262 for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
263 fprintf( outfile, " */" );
266 /* output the resource definitions */
267 int output_resources( FILE *outfile )
269 int i, j, k;
270 unsigned int n;
271 const struct res_type *type;
272 const struct res_name *name;
273 const struct resource *res;
275 if (!nb_resources) return 0;
277 build_resource_tree();
279 /* resource data */
281 for (i = 0, res = resources; i < nb_resources; i++, res++)
283 const unsigned int *p = res->data;
284 int size = res->data_size / 4;
285 /* dump data as ints to ensure correct alignment */
286 fprintf( outfile, "static const unsigned int res_%d[%d] = {\n ", i, size );
287 for (j = 0; j < size - 1; j++, p++)
289 fprintf( outfile, "0x%08x,", *p );
290 if ((j % 8) == 7) fprintf( outfile, "\n " );
292 fprintf( outfile, "0x%08x\n};\n\n", *p );
295 /* directory structures */
297 fprintf( outfile, "struct res_dir {\n" );
298 fprintf( outfile, " unsigned int Characteristics;\n" );
299 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
300 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
301 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
302 fprintf( outfile, "struct res_dir_entry {\n" );
303 fprintf( outfile, " unsigned int Name;\n" );
304 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
305 fprintf( outfile, "struct res_data_entry {\n" );
306 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
307 fprintf( outfile, " unsigned int Size;\n" );
308 fprintf( outfile, " unsigned int CodePage;\n" );
309 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
311 /* resource directory definition */
313 fprintf( outfile, "#define OFFSETOF(field) ((char*)&((struct res_struct *)0)->field - (char*)((struct res_struct *) 0))\n" );
314 fprintf( outfile, "static struct res_struct{\n" );
315 fprintf( outfile, " struct res_dir type_dir;\n" );
316 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", nb_types );
318 for (i = 0, type = res_types; i < nb_types; i++, type++)
320 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
321 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
322 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
324 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
325 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
326 i, n, name->nb_languages );
330 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", nb_resources );
332 for (i = 0, type = res_types; i < nb_types; i++, type++)
334 if (type->type->str)
335 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
336 i, strlenW(type->type->str)+1 );
337 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
339 if (name->name->str)
340 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
341 i, n, strlenW(name->name->str)+1 );
345 /* resource directory contents */
347 fprintf( outfile, "} resources = {\n" );
348 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", nb_types - nb_id_types, nb_id_types );
350 /* dump the type directory */
351 fprintf( outfile, " {\n" );
352 for (i = 0, type = res_types; i < nb_types; i++, type++)
354 if (!type->type->str)
355 fprintf( outfile, " { 0x%04x, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
356 type->type->id, i );
357 else
358 fprintf( outfile, " { OFFSETOF(type_%d_name) | 0x80000000, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
359 i, i );
361 fprintf( outfile, " },\n" );
363 /* dump the names and languages directories */
364 for (i = 0, type = res_types; i < nb_types; i++, type++)
366 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
367 type->nb_names - type->nb_id_names, type->nb_id_names, i );
368 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
370 if (!name->name->str)
371 fprintf( outfile, " { 0x%04x, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
372 name->name->id, i, n );
373 else
374 fprintf( outfile, " { OFFSETOF(name_%d_%d_name) | 0x80000000, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
375 i, n, i, n );
377 fprintf( outfile, " },\n" );
379 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
381 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
382 name->nb_languages, i, n );
383 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
385 fprintf( outfile, " { 0x%04x, OFFSETOF(data_entries[%d]) },\n",
386 res->lang, res - resources );
388 fprintf( outfile, " },\n" );
392 /* dump the resource data entries */
393 fprintf( outfile, " {\n" );
394 for (i = 0, res = resources; i < nb_resources; i++, res++)
396 fprintf( outfile, " { res_%d, sizeof(res_%d), 0, 0 },\n", i, i );
399 /* dump the name strings */
400 for (i = 0, type = res_types; i < nb_types; i++, type++)
402 if (type->type->str)
404 fprintf( outfile, " },\n { " );
405 output_string( outfile, type->type->str );
407 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
409 if (name->name->str)
411 fprintf( outfile, " },\n { " );
412 output_string( outfile, name->name->str );
416 fprintf( outfile, " }\n};\n#undef OFFSETOF\n\n" );
417 return nb_resources;