wininet: HTTP_BuildProxyRequestUrl returns a pointer.
[wine.git] / tools / winebuild / res32.c
blob5d480ad28bf607f0e6159d38398fb3316e501916
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 "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 */
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 RESDIR_SIZE(n) (sizeof(IMAGE_RESOURCE_DIRECTORY) + (n) * sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY))
96 static inline struct resource *add_resource( DLLSPEC *spec )
98 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
99 return &spec->resources[spec->nb_resources++];
102 static inline unsigned int strlenW( const WCHAR *str )
104 const WCHAR *s = str;
105 while (*s) s++;
106 return s - str;
109 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
111 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
112 return *str1 - *str2;
115 static struct res_name *add_name( struct res_type *type, const struct resource *res )
117 struct res_name *name;
118 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
119 name = &type->names[type->nb_names++];
120 name->name = &res->name;
121 name->res = res;
122 name->nb_languages = 1;
123 if (!name->name->str) type->nb_id_names++;
124 return name;
127 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
129 struct res_type *type;
130 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
131 type = &tree->types[tree->nb_types++];
132 type->type = &res->type;
133 type->names = NULL;
134 type->nb_names = 0;
135 type->nb_id_names = 0;
136 return type;
139 /* get the next word from the current resource file */
140 static WORD get_word(void)
142 WORD ret = *(const WORD *)file_pos;
143 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
144 file_pos += sizeof(WORD);
145 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
146 return ret;
149 /* get the next dword from the current resource file */
150 static DWORD get_dword(void)
152 DWORD ret = *(const DWORD *)file_pos;
153 if (byte_swapped)
154 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
155 file_pos += sizeof(DWORD);
156 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
157 return ret;
160 /* get a string from the current resource file */
161 static void get_string( struct string_id *str )
163 if (*(const WCHAR *)file_pos == 0xffff)
165 get_word(); /* skip the 0xffff */
166 str->str = NULL;
167 str->id = get_word();
169 else
171 WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
172 str->str = p;
173 str->id = 0;
174 while ((*p++ = get_word()));
178 /* check the file header */
179 /* all values must be zero except header size */
180 static int check_header(void)
182 DWORD size;
184 if (get_dword()) return 0; /* data size */
185 size = get_dword(); /* header size */
186 if (size == 0x20000000) byte_swapped = 1;
187 else if (size != 0x20) return 0;
188 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
189 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
190 if (get_dword()) return 0; /* data version */
191 if (get_word()) return 0; /* mem options */
192 if (get_word()) return 0; /* language */
193 if (get_dword()) return 0; /* version */
194 if (get_dword()) return 0; /* characteristics */
195 return 1;
198 /* load the next resource from the current file */
199 static void load_next_resource( DLLSPEC *spec )
201 DWORD hdr_size;
202 struct resource *res = add_resource( spec );
204 res->data_size = (get_dword() + 3) & ~3;
205 hdr_size = get_dword();
206 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
208 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
209 get_string( &res->type );
210 get_string( &res->name );
211 if ((UINT_PTR)file_pos & 2) get_word(); /* align to dword boundary */
212 get_dword(); /* skip data version */
213 get_word(); /* skip mem options */
214 res->lang = get_word();
215 get_dword(); /* skip version */
216 get_dword(); /* skip characteristics */
218 file_pos = (const unsigned char *)res->data + res->data_size;
219 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
222 /* load a Win32 .res file */
223 int load_res32_file( const char *name, DLLSPEC *spec )
225 int fd, ret;
226 void *base;
227 struct stat st;
229 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
230 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
231 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
232 #ifdef HAVE_MMAP
233 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
234 #endif /* HAVE_MMAP */
236 base = xmalloc( st.st_size );
237 if (read( fd, base, st.st_size ) != st.st_size)
238 fatal_error( "Cannot read %s\n", name );
241 byte_swapped = 0;
242 file_name = name;
243 file_pos = base;
244 file_end = file_pos + st.st_size;
245 if ((ret = check_header()))
247 while (file_pos < file_end) load_next_resource( spec );
249 close( fd );
250 return ret;
253 /* compare two unicode strings/ids */
254 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
256 if (!str1->str)
258 if (!str2->str) return str1->id - str2->id;
259 return 1; /* an id compares larger than a string */
261 if (!str2->str) return -1;
262 return strcmpW( str1->str, str2->str );
265 /* compare two resources for sorting the resource directory */
266 /* resources are stored first by type, then by name, then by language */
267 static int cmp_res( const void *ptr1, const void *ptr2 )
269 const struct resource *res1 = ptr1;
270 const struct resource *res2 = ptr2;
271 int ret;
273 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
274 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
275 return res1->lang - res2->lang;
278 /* build the 3-level (type,name,language) resource tree */
279 static struct res_tree *build_resource_tree( DLLSPEC *spec )
281 unsigned int i;
282 struct res_tree *tree;
283 struct res_type *type = NULL;
284 struct res_name *name = NULL;
286 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
288 tree = xmalloc( sizeof(*tree) );
289 tree->types = NULL;
290 tree->nb_types = 0;
292 for (i = 0; i < spec->nb_resources; i++)
294 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
296 type = add_type( tree, &spec->resources[i] );
297 name = add_name( type, &spec->resources[i] );
299 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
301 name = add_name( type, &spec->resources[i] );
303 else name->nb_languages++;
305 return tree;
308 /* free the resource tree */
309 static void free_resource_tree( struct res_tree *tree )
311 unsigned int i;
313 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
314 free( tree->types );
315 free( tree );
318 /* output a Unicode string */
319 static void output_string( const WCHAR *name )
321 int i, len = strlenW(name);
322 output( "\t%s 0x%04x", get_asm_short_keyword(), len );
323 for (i = 0; i < len; i++) output( ",0x%04x", name[i] );
324 output( " /* " );
325 for (i = 0; i < len; i++) output( "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
326 output( " */\n" );
329 /* output a resource directory */
330 static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
332 output( "\t.long 0\n" ); /* Characteristics */
333 output( "\t.long 0\n" ); /* TimeDateStamp */
334 output( "\t%s 0,0\n", /* Major/MinorVersion */
335 get_asm_short_keyword() );
336 output( "\t%s %u,%u\n", /* NumberOfNamed/IdEntries */
337 get_asm_short_keyword(), nb_names, nb_ids );
340 /* output the resource definitions */
341 void output_resources( DLLSPEC *spec )
343 int k, nb_id_types;
344 unsigned int i, n, offset, data_offset;
345 struct res_tree *tree;
346 struct res_type *type;
347 struct res_name *name;
348 const struct resource *res;
350 if (!spec->nb_resources) return;
352 tree = build_resource_tree( spec );
354 /* compute the offsets */
356 offset = RESDIR_SIZE( tree->nb_types );
357 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
359 offset += RESDIR_SIZE( type->nb_names );
360 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
361 offset += RESDIR_SIZE( name->nb_languages );
363 offset += spec->nb_resources * sizeof(IMAGE_RESOURCE_DATA_ENTRY);
365 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
367 if (type->type->str)
369 type->name_offset = offset | 0x80000000;
370 offset += (strlenW(type->type->str)+1) * sizeof(WCHAR);
372 else
374 type->name_offset = type->type->id;
375 nb_id_types++;
378 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
380 if (name->name->str)
382 name->name_offset = offset | 0x80000000;
383 offset += (strlenW(name->name->str)+1) * sizeof(WCHAR);
385 else name->name_offset = name->name->id;
389 /* output the resource directories */
391 output( "\n/* resources */\n\n" );
392 output( "\t.data\n" );
393 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
394 output( ".L__wine_spec_resources:\n" );
396 output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
398 /* dump the type directory */
400 offset = RESDIR_SIZE( tree->nb_types );
401 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
403 output( "\t.long 0x%08x,0x%08x\n",
404 type->name_offset, offset | 0x80000000 );
405 offset += RESDIR_SIZE( type->nb_names );
406 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
407 offset += RESDIR_SIZE( name->nb_languages );
410 data_offset = offset;
411 offset = RESDIR_SIZE( tree->nb_types );
413 /* dump the names and languages directories */
415 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
417 output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
418 offset += RESDIR_SIZE( type->nb_names );
419 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
421 output( "\t.long 0x%08x,0x%08x\n",
422 name->name_offset, offset | 0x80000000 );
423 offset += RESDIR_SIZE( name->nb_languages );
426 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
428 output_res_dir( 0, name->nb_languages );
429 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
431 unsigned int entry_offset = (res - spec->resources) * sizeof(IMAGE_RESOURCE_DATA_ENTRY);
432 output( "\t.long 0x%08x,0x%08x\n", res->lang, data_offset + entry_offset );
437 /* dump the resource data entries */
439 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
440 output( "\t.long .L__wine_spec_res_%d-.L__wine_spec_rva_base,%u,0,0\n",
441 i, res->data_size );
443 /* dump the name strings */
445 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
447 if (type->type->str) output_string( type->type->str );
448 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
449 if (name->name->str) output_string( name->name->str );
452 /* resource data */
454 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
456 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
457 output( ".L__wine_spec_res_%d:\n", i );
458 dump_bytes( res->data, res->data_size );
460 output( ".L__wine_spec_resources_end:\n" );
461 output( "\t.byte 0\n" );
463 free_resource_tree( tree );