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"
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
36 #ifdef HAVE_SYS_MMAN_H
44 #define ALIGNMENT 2 /* alignment for resource data */
45 #define ALIGN_MASK ((1 << ALIGNMENT) - 1)
47 /* Unicode string or integer id */
50 char *str; /* ptr to string */
51 WORD id; /* integer id if str is NULL */
54 /* descriptor for a resource */
57 struct string_id type;
58 struct string_id name;
60 unsigned int data_size;
64 /* type level of the resource tree */
67 const struct string_id *type; /* type name */
68 const struct resource *res; /* first resource of this type */
69 unsigned int nb_names; /* total number of names */
72 static struct resource *resources;
73 static int nb_resources;
75 static struct res_type *res_types;
76 static int nb_types; /* total number of types */
78 static const unsigned char *file_pos; /* current position in resource file */
79 static const unsigned char *file_end; /* end of resource file */
80 static const char *file_name; /* current resource file name */
83 inline static struct resource *add_resource(void)
85 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
86 return &resources[nb_resources++];
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;
100 /* get the next byte from the current resource file */
101 static WORD get_byte(void)
103 unsigned char ret = *file_pos++;
104 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
108 /* get the next word from the current resource file */
109 static WORD get_word(void)
111 /* might not be aligned */
112 #ifdef WORDS_BIGENDIAN
113 unsigned char high = get_byte();
114 unsigned char low = get_byte();
116 unsigned char low = get_byte();
117 unsigned char high = get_byte();
119 return low | (high << 8);
122 /* get the next dword from the current resource file */
123 static DWORD get_dword(void)
125 #ifdef WORDS_BIGENDIAN
126 WORD high = get_word();
127 WORD low = get_word();
129 WORD low = get_word();
130 WORD high = get_word();
132 return low | (high << 16);
135 /* get a string from the current resource file */
136 static void get_string( struct string_id *str )
138 if (*file_pos == 0xff)
140 get_byte(); /* skip the 0xff */
142 str->id = get_word();
146 char *p = xmalloc( (strlen(file_pos) + 1) );
149 while ((*p++ = get_byte()));
153 /* load the next resource from the current file */
154 static void load_next_resource(void)
156 struct resource *res = add_resource();
158 get_string( &res->type );
159 get_string( &res->name );
160 res->memopt = get_word();
161 res->data_size = get_dword();
162 res->data = file_pos;
163 file_pos += res->data_size;
164 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
167 /* load a Win16 .res file */
168 void load_res16_file( const char *name )
174 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
175 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
176 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
178 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
179 #endif /* HAVE_MMAP */
181 base = xmalloc( st.st_size );
182 if (read( fd, base, st.st_size ) != st.st_size)
183 fatal_error( "Cannot read %s\n", name );
188 file_end = file_pos + st.st_size;
189 while (file_pos < file_end) load_next_resource();
192 /* compare two strings/ids */
193 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
197 if (!str2->str) return str1->id - str2->id;
198 return 1; /* an id compares larger than a string */
200 if (!str2->str) return -1;
201 return strcasecmp( str1->str, str2->str );
204 /* compare two resources for sorting the resource directory */
205 /* resources are stored first by type, then by name */
206 static int cmp_res( const void *ptr1, const void *ptr2 )
208 const struct resource *res1 = ptr1;
209 const struct resource *res2 = ptr2;
212 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
213 return cmp_string( &res1->name, &res2->name );
216 /* build the 2-level (type,name) resource tree */
217 static void build_resource_tree(void)
220 struct res_type *type = NULL;
222 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
224 for (i = 0; i < nb_resources; i++)
226 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
227 type = add_type( &resources[i] );
232 inline static void put_byte( unsigned char **buffer, unsigned char val )
237 inline static void put_word( unsigned char **buffer, WORD val )
239 #ifdef WORDS_BIGENDIAN
240 put_byte( buffer, HIBYTE(val) );
241 put_byte( buffer, LOBYTE(val) );
243 put_byte( buffer, LOBYTE(val) );
244 put_byte( buffer, HIBYTE(val) );
248 /* output a string preceded by its length */
249 static void output_string( unsigned char **buffer, const char *str )
251 int len = strlen(str);
252 put_byte( buffer, len );
253 while (len--) put_byte( buffer, *str++ );
256 /* output the resource data */
257 int output_res16_data( FILE *outfile )
259 const struct resource *res;
260 unsigned char *buffer, *p;
263 if (!nb_resources) return 0;
265 for (i = total = 0, res = resources; i < nb_resources; i++, res++)
266 total += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
268 buffer = p = xmalloc( total );
269 for (i = 0, res = resources; i < nb_resources; i++, res++)
271 memcpy( p, res->data, res->data_size );
273 while ((int)p & ALIGN_MASK) *p++ = 0;
275 dump_bytes( outfile, buffer, total, "resource_data", 1 );
280 /* output the resource definitions */
281 int output_res16_directory( unsigned char *buffer )
283 int i, offset, res_offset = 0;
285 const struct res_type *type;
286 const struct resource *res;
287 unsigned char *start = buffer;
289 build_resource_tree();
291 offset = 4; /* alignment + terminator */
292 offset += nb_types * 8; /* typeinfo structures */
293 offset += nb_resources * 12; /* nameinfo structures */
295 put_word( &buffer, ALIGNMENT );
297 /* type and name structures */
299 for (i = 0, type = res_types; i < nb_types; i++, type++)
303 put_word( &buffer, offset );
304 offset += strlen(type->type->str) + 1;
307 put_word( &buffer, type->type->id | 0x8000 );
309 put_word( &buffer, type->nb_names );
310 put_word( &buffer, 0 );
311 put_word( &buffer, 0 );
313 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
315 put_word( &buffer, res_offset >> ALIGNMENT );
316 put_word( &buffer, (res->data_size + ALIGN_MASK) >> ALIGNMENT );
317 put_word( &buffer, res->memopt );
320 put_word( &buffer, offset );
321 offset += strlen(res->name.str) + 1;
324 put_word( &buffer, res->name.id | 0x8000 );
325 put_word( &buffer, 0 );
326 put_word( &buffer, 0 );
327 res_offset += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
330 put_word( &buffer, 0 ); /* terminator */
334 for (i = 0, type = res_types; i < nb_types; i++, type++)
336 if (type->type->str) output_string( &buffer, type->type->str );
337 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
339 if (res->name.str) output_string( &buffer, res->name.str );
342 put_byte( &buffer, 0 ); /* names terminator */
343 if ((buffer - start) & 1) put_byte( &buffer, 0 ); /* align on word boundary */
345 return buffer - start;