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"
28 #include <sys/types.h>
31 #ifdef HAVE_SYS_MMAN_H
38 #define ALIGNMENT 2 /* alignment for resource data */
39 #define ALIGN_MASK ((1 << ALIGNMENT) - 1)
41 /* Unicode string or integer id */
44 char *str; /* ptr to string */
45 WORD id; /* integer id if str is NULL */
48 /* descriptor for a resource */
51 struct string_id type;
52 struct string_id name;
54 unsigned int data_size;
58 /* type level of the resource tree */
61 const struct string_id *type; /* type name */
62 const struct resource *res; /* first resource of this type */
63 unsigned int nb_names; /* total number of names */
66 static struct resource *resources;
67 static int nb_resources;
69 static struct res_type *res_types;
70 static int nb_types; /* total number of types */
72 static const unsigned char *file_pos; /* current position in resource file */
73 static const unsigned char *file_end; /* end of resource file */
74 static const char *file_name; /* current resource file name */
77 inline static struct resource *add_resource(void)
79 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
80 return &resources[nb_resources++];
83 static struct res_type *add_type( const struct resource *res )
85 struct res_type *type;
86 res_types = xrealloc( res_types, (nb_types + 1) * sizeof(*res_types) );
87 type = &res_types[nb_types++];
88 type->type = &res->type;
94 /* get the next byte from the current resource file */
95 static WORD get_byte(void)
97 unsigned char ret = *file_pos++;
98 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
102 /* get the next word from the current resource file */
103 static WORD get_word(void)
105 /* might not be aligned */
106 #ifdef WORDS_BIGENDIAN
107 unsigned char high = get_byte();
108 unsigned char low = get_byte();
110 unsigned char low = get_byte();
111 unsigned char high = get_byte();
113 return low | (high << 8);
116 /* get the next dword from the current resource file */
117 static DWORD get_dword(void)
119 #ifdef WORDS_BIGENDIAN
120 WORD high = get_word();
121 WORD low = get_word();
123 WORD low = get_word();
124 WORD high = get_word();
126 return low | (high << 16);
129 /* get a string from the current resource file */
130 static void get_string( struct string_id *str )
132 if (*file_pos == 0xff)
134 get_byte(); /* skip the 0xff */
136 str->id = get_word();
140 char *p = xmalloc( (strlen(file_pos) + 1) );
143 while ((*p++ = get_byte()));
147 /* load the next resource from the current file */
148 static void load_next_resource(void)
150 struct resource *res = add_resource();
152 get_string( &res->type );
153 get_string( &res->name );
154 res->memopt = get_word();
155 res->data_size = get_dword();
156 res->data = file_pos;
157 file_pos += res->data_size;
158 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
161 /* load a Win16 .res file */
162 void load_res16_file( const char *name )
168 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
169 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
170 if (!st.st_size) fatal_error( "%s is an empty file\n" );
172 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
173 #endif /* HAVE_MMAP */
175 base = xmalloc( st.st_size );
176 if (read( fd, base, st.st_size ) != st.st_size)
177 fatal_error( "Cannot read %s\n", name );
182 file_end = file_pos + st.st_size;
183 while (file_pos < file_end) load_next_resource();
186 /* compare two strings/ids */
187 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
191 if (!str2->str) return str1->id - str2->id;
192 return 1; /* an id compares larger than a string */
194 if (!str2->str) return -1;
195 return strcasecmp( str1->str, str2->str );
198 /* compare two resources for sorting the resource directory */
199 /* resources are stored first by type, then by name */
200 static int cmp_res( const void *ptr1, const void *ptr2 )
202 const struct resource *res1 = ptr1;
203 const struct resource *res2 = ptr2;
206 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
207 return cmp_string( &res1->name, &res2->name );
210 /* build the 2-level (type,name) resource tree */
211 static void build_resource_tree(void)
214 struct res_type *type = NULL;
216 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
218 for (i = 0; i < nb_resources; i++)
220 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
221 type = add_type( &resources[i] );
226 inline static void put_byte( unsigned char **buffer, unsigned char val )
231 inline static void put_word( unsigned char **buffer, WORD val )
233 #ifdef WORDS_BIGENDIAN
234 put_byte( buffer, HIBYTE(val) );
235 put_byte( buffer, LOBYTE(val) );
237 put_byte( buffer, LOBYTE(val) );
238 put_byte( buffer, HIBYTE(val) );
242 /* output a string preceded by its length */
243 static void output_string( unsigned char **buffer, const char *str )
245 int len = strlen(str);
246 put_byte( buffer, len );
247 while (len--) put_byte( buffer, *str++ );
250 /* output the resource data */
251 int output_res16_data( FILE *outfile )
253 const struct resource *res;
254 unsigned char *buffer, *p;
257 if (!nb_resources) return 0;
259 for (i = total = 0, res = resources; i < nb_resources; i++, res++)
260 total += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
262 buffer = p = xmalloc( total );
263 for (i = 0, res = resources; i < nb_resources; i++, res++)
265 memcpy( p, res->data, res->data_size );
267 while ((int)p & ALIGN_MASK) *p++ = 0;
269 dump_bytes( outfile, buffer, total, "resource_data", 1 );
274 /* output the resource definitions */
275 int output_res16_directory( unsigned char *buffer )
277 int i, offset, res_offset = 0;
279 const struct res_type *type;
280 const struct resource *res;
281 unsigned char *start = buffer;
283 build_resource_tree();
285 offset = 4; /* alignment + terminator */
286 offset += nb_types * 8; /* typeinfo structures */
287 offset += nb_resources * 12; /* nameinfo structures */
289 put_word( &buffer, ALIGNMENT );
291 /* type and name structures */
293 for (i = 0, type = res_types; i < nb_types; i++, type++)
297 put_word( &buffer, offset );
298 offset += strlen(type->type->str) + 1;
301 put_word( &buffer, type->type->id | 0x8000 );
303 put_word( &buffer, type->nb_names );
304 put_word( &buffer, 0 );
305 put_word( &buffer, 0 );
307 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
309 put_word( &buffer, res_offset >> ALIGNMENT );
310 put_word( &buffer, (res->data_size + ALIGN_MASK) >> ALIGNMENT );
311 put_word( &buffer, res->memopt );
314 put_word( &buffer, offset );
315 offset += strlen(res->name.str) + 1;
318 put_word( &buffer, res->name.id | 0x8000 );
319 put_word( &buffer, 0 );
320 put_word( &buffer, 0 );
321 res_offset += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
324 put_word( &buffer, 0 ); /* terminator */
328 for (i = 0, type = res_types; i < nb_types; i++, type++)
330 if (type->type->str) output_string( &buffer, type->type->str );
331 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
333 if (res->name.str) output_string( &buffer, res->name.str );
336 put_byte( &buffer, 0 ); /* names terminator */
337 if ((buffer - start) & 1) put_byte( &buffer, 0 ); /* align on word boundary */
339 return buffer - start;