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"
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
37 #ifdef HAVE_SYS_MMAN_H
45 #define ALIGNMENT 2 /* alignment for resource data */
46 #define ALIGN_MASK ((1 << ALIGNMENT) - 1)
48 /* Unicode string or integer id */
51 char *str; /* ptr to string */
52 WORD id; /* integer id if str is NULL */
55 /* descriptor for a resource */
58 struct string_id type;
59 struct string_id name;
61 unsigned int data_size;
65 /* type level of the resource tree */
68 const struct string_id *type; /* type name */
69 const struct resource *res; /* first resource of this type */
70 unsigned int nb_names; /* total number of names */
73 /* top level of the resource tree */
76 struct res_type *types; /* types array */
77 unsigned int nb_types; /* total number of types */
80 static const unsigned char *file_pos; /* current position in resource file */
81 static const unsigned char *file_end; /* end of resource file */
82 static const char *file_name; /* current resource file name */
85 inline static struct resource *add_resource( DLLSPEC *spec )
87 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
88 return &spec->resources[spec->nb_resources++];
91 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
93 struct res_type *type;
94 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
95 type = &tree->types[tree->nb_types++];
96 type->type = &res->type;
102 /* get the next byte from the current resource file */
103 static unsigned char get_byte(void)
105 unsigned char ret = *file_pos++;
106 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
110 /* get the next word from the current resource file */
111 static WORD get_word(void)
113 /* might not be aligned */
114 #ifdef WORDS_BIGENDIAN
115 unsigned char high = get_byte();
116 unsigned char low = get_byte();
118 unsigned char low = get_byte();
119 unsigned char high = get_byte();
121 return low | (high << 8);
124 /* get the next dword from the current resource file */
125 static DWORD get_dword(void)
127 #ifdef WORDS_BIGENDIAN
128 WORD high = get_word();
129 WORD low = get_word();
131 WORD low = get_word();
132 WORD high = get_word();
134 return low | (high << 16);
137 /* get a string from the current resource file */
138 static void get_string( struct string_id *str )
140 if (*file_pos == 0xff)
142 get_byte(); /* skip the 0xff */
144 str->id = get_word();
148 char *p = xmalloc( (strlen(file_pos) + 1) );
151 while ((*p++ = get_byte()));
155 /* load the next resource from the current file */
156 static void load_next_resource( DLLSPEC *spec )
158 struct resource *res = add_resource( spec );
160 get_string( &res->type );
161 get_string( &res->name );
162 res->memopt = get_word();
163 res->data_size = get_dword();
164 res->data = file_pos;
165 file_pos += res->data_size;
166 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
169 /* load a Win16 .res file */
170 void load_res16_file( const char *name, DLLSPEC *spec )
176 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
177 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
178 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
180 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
181 #endif /* HAVE_MMAP */
183 base = xmalloc( st.st_size );
184 if (read( fd, base, st.st_size ) != st.st_size)
185 fatal_error( "Cannot read %s\n", name );
190 file_end = file_pos + st.st_size;
191 while (file_pos < file_end) load_next_resource( spec );
194 /* compare two strings/ids */
195 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
199 if (!str2->str) return str1->id - str2->id;
200 return 1; /* an id compares larger than a string */
202 if (!str2->str) return -1;
203 return strcasecmp( str1->str, str2->str );
206 /* compare two resources for sorting the resource directory */
207 /* resources are stored first by type, then by name */
208 static int cmp_res( const void *ptr1, const void *ptr2 )
210 const struct resource *res1 = ptr1;
211 const struct resource *res2 = ptr2;
214 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
215 return cmp_string( &res1->name, &res2->name );
218 /* build the 2-level (type,name) resource tree */
219 static struct res_tree *build_resource_tree( DLLSPEC *spec )
222 struct res_tree *tree;
223 struct res_type *type = NULL;
225 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
227 tree = xmalloc( sizeof(*tree) );
231 for (i = 0; i < spec->nb_resources; i++)
233 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
234 type = add_type( tree, &spec->resources[i] );
240 /* free the resource tree */
241 static void free_resource_tree( struct res_tree *tree )
247 inline static void put_byte( unsigned char **buffer, unsigned char val )
252 inline static void put_word( unsigned char **buffer, WORD val )
254 #ifdef WORDS_BIGENDIAN
255 put_byte( buffer, HIBYTE(val) );
256 put_byte( buffer, LOBYTE(val) );
258 put_byte( buffer, LOBYTE(val) );
259 put_byte( buffer, HIBYTE(val) );
263 /* output a string preceded by its length */
264 static void output_string( unsigned char **buffer, const char *str )
266 int len = strlen(str);
267 put_byte( buffer, len );
268 while (len--) put_byte( buffer, *str++ );
271 /* output the resource data */
272 int output_res16_data( FILE *outfile, DLLSPEC *spec )
274 const struct resource *res;
275 unsigned char *buffer, *p;
279 if (!spec->nb_resources) return 0;
281 for (i = total = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
282 total += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
284 buffer = p = xmalloc( total );
285 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
287 memcpy( p, res->data, res->data_size );
289 while ((int)p & ALIGN_MASK) *p++ = 0;
291 dump_bytes( outfile, buffer, total, "resource_data", 1 );
296 /* output the resource definitions */
297 unsigned int output_res16_directory( unsigned char **ret_buf, DLLSPEC *spec )
299 int offset, res_offset = 0;
300 unsigned int i, j, total_size;
301 struct res_tree *tree;
302 const struct res_type *type;
303 const struct resource *res;
304 unsigned char *buffer;
306 tree = build_resource_tree( spec );
308 /* first compute total size */
310 offset = 4; /* alignment + terminator */
311 offset += tree->nb_types * 8; /* typeinfo structures */
312 offset += spec->nb_resources * 12; /* nameinfo structures */
316 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
318 if (type->type->str) total_size += strlen(type->type->str) + 1;
319 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
320 if (res->name.str) total_size += strlen(res->name.str) + 1;
322 total_size++; /* final terminator */
323 if (total_size & 1) total_size++;
324 *ret_buf = buffer = xmalloc( total_size );
326 put_word( &buffer, ALIGNMENT );
328 /* type and name structures */
330 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
334 put_word( &buffer, offset );
335 offset += strlen(type->type->str) + 1;
338 put_word( &buffer, type->type->id | 0x8000 );
340 put_word( &buffer, type->nb_names );
341 put_word( &buffer, 0 );
342 put_word( &buffer, 0 );
344 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
346 put_word( &buffer, res_offset >> ALIGNMENT );
347 put_word( &buffer, (res->data_size + ALIGN_MASK) >> ALIGNMENT );
348 put_word( &buffer, res->memopt );
351 put_word( &buffer, offset );
352 offset += strlen(res->name.str) + 1;
355 put_word( &buffer, res->name.id | 0x8000 );
356 put_word( &buffer, 0 );
357 put_word( &buffer, 0 );
358 res_offset += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
361 put_word( &buffer, 0 ); /* terminator */
365 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
367 if (type->type->str) output_string( &buffer, type->type->str );
368 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
370 if (res->name.str) output_string( &buffer, res->name.str );
373 put_byte( &buffer, 0 ); /* names terminator */
374 if ((buffer - *ret_buf) & 1) put_byte( &buffer, 0 ); /* align on word boundary */
375 assert( buffer - *ret_buf == total_size );
377 free_resource_tree( tree );