Generate 16-bit resources in the proper format inside the module data,
[wine/dcerpc.git] / tools / winebuild / res16.c
blob9aad666bdfbaeb9f4d31dbbb62da039f30141054
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 <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #include <fcntl.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
41 #include "windef.h"
42 #include "winbase.h"
43 #include "build.h"
45 /* Unicode string or integer id */
46 struct string_id
48 char *str; /* ptr to string */
49 WORD id; /* integer id if str is NULL */
52 /* descriptor for a resource */
53 struct resource
55 struct string_id type;
56 struct string_id name;
57 const void *data;
58 unsigned int data_size;
59 WORD memopt;
62 /* type level of the resource tree */
63 struct res_type
65 const struct string_id *type; /* type name */
66 const struct resource *res; /* first resource of this type */
67 unsigned int nb_names; /* total number of names */
70 /* top level of the resource tree */
71 struct res_tree
73 struct res_type *types; /* types array */
74 unsigned int nb_types; /* total number of types */
77 static const unsigned char *file_pos; /* current position in resource file */
78 static const unsigned char *file_end; /* end of resource file */
79 static const char *file_name; /* current resource file name */
82 inline static struct resource *add_resource( DLLSPEC *spec )
84 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
85 return &spec->resources[spec->nb_resources++];
88 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
90 struct res_type *type;
91 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
92 type = &tree->types[tree->nb_types++];
93 type->type = &res->type;
94 type->res = res;
95 type->nb_names = 0;
96 return type;
99 /* get the next byte from the current resource file */
100 static unsigned char get_byte(void)
102 unsigned char ret = *file_pos++;
103 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
104 return ret;
107 /* get the next word from the current resource file */
108 static WORD get_word(void)
110 /* might not be aligned */
111 #ifdef WORDS_BIGENDIAN
112 unsigned char high = get_byte();
113 unsigned char low = get_byte();
114 #else
115 unsigned char low = get_byte();
116 unsigned char high = get_byte();
117 #endif
118 return low | (high << 8);
121 /* get the next dword from the current resource file */
122 static DWORD get_dword(void)
124 #ifdef WORDS_BIGENDIAN
125 WORD high = get_word();
126 WORD low = get_word();
127 #else
128 WORD low = get_word();
129 WORD high = get_word();
130 #endif
131 return low | (high << 16);
134 /* get a string from the current resource file */
135 static void get_string( struct string_id *str )
137 if (*file_pos == 0xff)
139 get_byte(); /* skip the 0xff */
140 str->str = NULL;
141 str->id = get_word();
143 else
145 char *p = xmalloc( (strlen(file_pos) + 1) );
146 str->str = p;
147 str->id = 0;
148 while ((*p++ = get_byte()));
152 /* load the next resource from the current file */
153 static void load_next_resource( DLLSPEC *spec )
155 struct resource *res = add_resource( spec );
157 get_string( &res->type );
158 get_string( &res->name );
159 res->memopt = get_word();
160 res->data_size = get_dword();
161 res->data = file_pos;
162 file_pos += res->data_size;
163 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
166 /* load a Win16 .res file */
167 void load_res16_file( const char *name, DLLSPEC *spec )
169 int fd;
170 void *base;
171 struct stat st;
173 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
174 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
175 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
176 #ifdef HAVE_MMAP
177 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
178 #endif /* HAVE_MMAP */
180 base = xmalloc( st.st_size );
181 if (read( fd, base, st.st_size ) != st.st_size)
182 fatal_error( "Cannot read %s\n", name );
185 file_name = name;
186 file_pos = base;
187 file_end = file_pos + st.st_size;
188 while (file_pos < file_end) load_next_resource( spec );
191 /* compare two strings/ids */
192 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
194 if (!str1->str)
196 if (!str2->str) return str1->id - str2->id;
197 return 1; /* an id compares larger than a string */
199 if (!str2->str) return -1;
200 return strcasecmp( str1->str, str2->str );
203 /* compare two resources for sorting the resource directory */
204 /* resources are stored first by type, then by name */
205 static int cmp_res( const void *ptr1, const void *ptr2 )
207 const struct resource *res1 = ptr1;
208 const struct resource *res2 = ptr2;
209 int ret;
211 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
212 return cmp_string( &res1->name, &res2->name );
215 /* build the 2-level (type,name) resource tree */
216 static struct res_tree *build_resource_tree( DLLSPEC *spec )
218 unsigned int i;
219 struct res_tree *tree;
220 struct res_type *type = NULL;
222 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
224 tree = xmalloc( sizeof(*tree) );
225 tree->types = NULL;
226 tree->nb_types = 0;
228 for (i = 0; i < spec->nb_resources; i++)
230 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
231 type = add_type( tree, &spec->resources[i] );
232 type->nb_names++;
234 return tree;
237 /* free the resource tree */
238 static void free_resource_tree( struct res_tree *tree )
240 free( tree->types );
241 free( tree );
244 inline static void put_byte( unsigned char **buffer, unsigned char val )
246 *(*buffer)++ = val;
249 inline static void put_word( unsigned char **buffer, WORD val )
251 #ifdef WORDS_BIGENDIAN
252 put_byte( buffer, HIBYTE(val) );
253 put_byte( buffer, LOBYTE(val) );
254 #else
255 put_byte( buffer, LOBYTE(val) );
256 put_byte( buffer, HIBYTE(val) );
257 #endif
260 /* output a string preceded by its length */
261 static void output_string( unsigned char **buffer, const char *str )
263 int len = strlen(str);
264 put_byte( buffer, len );
265 while (len--) put_byte( buffer, *str++ );
268 /* get the resource data total size */
269 unsigned int get_res16_data_size( DLLSPEC *spec, unsigned int res_offset, unsigned int alignment )
271 const struct resource *res;
272 unsigned int i, total;
273 unsigned int align_mask = (1 << alignment) - 1;
275 if (!spec->nb_resources) return 0;
277 /* add padding at the beginning if needed so that resources are properly aligned */
278 total = ((res_offset + align_mask) & ~align_mask) - res_offset;
280 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
281 total += (res->data_size + align_mask) & ~align_mask;
283 return total;
286 /* output the resource data */
287 unsigned int output_res16_data( unsigned char **ret_buf, DLLSPEC *spec,
288 unsigned int res_offset, unsigned int alignment )
290 const struct resource *res;
291 unsigned char *p;
292 unsigned int i, total, padding;
293 unsigned int align_mask = (1 << alignment) - 1;
295 if (!spec->nb_resources) return 0;
297 /* add padding at the beginning if needed so that resources are properly aligned */
298 padding = ((res_offset + align_mask) & ~align_mask) - res_offset;
300 for (i = total = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
301 total += (res->data_size + align_mask) & ~align_mask;
303 *ret_buf = p = xmalloc( total + padding );
304 memset( p, 0, padding );
305 p += padding;
306 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
308 unsigned int size = (res->data_size + align_mask) & ~align_mask;
309 memcpy( p, res->data, res->data_size );
310 memset( p + res->data_size, 0, size - res->data_size );
311 p += size;
313 return total;
316 /* get the resource definitions total size */
317 unsigned int get_res16_directory_size( DLLSPEC *spec )
319 unsigned int i, j, total_size;
320 struct res_tree *tree;
321 const struct res_type *type;
322 const struct resource *res;
324 tree = build_resource_tree( spec );
326 total_size = 4; /* alignment + terminator */
327 total_size += tree->nb_types * 8; /* typeinfo structures */
328 total_size += spec->nb_resources * 12; /* nameinfo structures */
330 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
332 if (type->type->str) total_size += strlen(type->type->str) + 1;
333 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
334 if (res->name.str) total_size += strlen(res->name.str) + 1;
336 total_size++; /* final terminator */
337 if (total_size & 1) total_size++;
338 return total_size;
341 /* output the resource definitions */
342 unsigned int output_res16_directory( unsigned char **ret_buf, DLLSPEC *spec,
343 unsigned int res_offset, unsigned int alignment )
345 int offset;
346 unsigned int i, j, total_size;
347 unsigned int align_mask = (1 << alignment) - 1;
348 struct res_tree *tree;
349 const struct res_type *type;
350 const struct resource *res;
351 unsigned char *buffer;
353 tree = build_resource_tree( spec );
355 /* make sure data offset is properly aligned */
356 res_offset = (res_offset + align_mask) & ~align_mask;
358 /* first compute total size */
360 offset = 4; /* alignment + terminator */
361 offset += tree->nb_types * 8; /* typeinfo structures */
362 offset += spec->nb_resources * 12; /* nameinfo structures */
364 total_size = offset;
366 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
368 if (type->type->str) total_size += strlen(type->type->str) + 1;
369 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
370 if (res->name.str) total_size += strlen(res->name.str) + 1;
372 total_size++; /* final terminator */
373 if (total_size & 1) total_size++;
374 *ret_buf = buffer = xmalloc( total_size );
376 put_word( &buffer, alignment );
378 /* type and name structures */
380 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
382 if (type->type->str)
384 put_word( &buffer, offset );
385 offset += strlen(type->type->str) + 1;
387 else
388 put_word( &buffer, type->type->id | 0x8000 );
390 put_word( &buffer, type->nb_names );
391 put_word( &buffer, 0 );
392 put_word( &buffer, 0 );
394 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
396 put_word( &buffer, res_offset >> alignment );
397 put_word( &buffer, (res->data_size + align_mask) >> alignment );
398 put_word( &buffer, res->memopt );
399 if (res->name.str)
401 put_word( &buffer, offset );
402 offset += strlen(res->name.str) + 1;
404 else
405 put_word( &buffer, res->name.id | 0x8000 );
406 put_word( &buffer, 0 );
407 put_word( &buffer, 0 );
408 res_offset += (res->data_size + align_mask) & ~align_mask;
411 put_word( &buffer, 0 ); /* terminator */
413 /* name strings */
415 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
417 if (type->type->str) output_string( &buffer, type->type->str );
418 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
420 if (res->name.str) output_string( &buffer, res->name.str );
423 put_byte( &buffer, 0 ); /* names terminator */
424 if ((buffer - *ret_buf) & 1) put_byte( &buffer, 0 ); /* align on word boundary */
425 assert( buffer - *ret_buf == total_size );
427 free_resource_tree( tree );
428 return total_size;