2 * Builtin dlls resource support
4 * Copyright 2000 Alexandre Julliard
14 #include <sys/types.h>
17 #ifdef HAVE_SYS_MMAN_H
24 #define ALIGNMENT 2 /* alignment for resource data */
25 #define ALIGN_MASK ((1 << ALIGNMENT) - 1)
27 /* Unicode string or integer id */
30 char *str; /* ptr to string */
31 WORD id; /* integer id if str is NULL */
34 /* descriptor for a resource */
37 struct string_id type;
38 struct string_id name;
40 unsigned int data_size;
44 /* type level of the resource tree */
47 const struct string_id *type; /* type name */
48 const struct resource *res; /* first resource of this type */
49 unsigned int nb_names; /* total number of names */
52 static struct resource *resources;
53 static int nb_resources;
55 static struct res_type *res_types;
56 static int nb_types; /* total number of types */
58 static const unsigned char *file_pos; /* current position in resource file */
59 static const unsigned char *file_end; /* end of resource file */
60 static const char *file_name; /* current resource file name */
63 inline static struct resource *add_resource(void)
65 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
66 return &resources[nb_resources++];
69 static struct res_type *add_type( const struct resource *res )
71 struct res_type *type;
72 res_types = xrealloc( res_types, (nb_types + 1) * sizeof(*res_types) );
73 type = &res_types[nb_types++];
74 type->type = &res->type;
80 /* get the next byte from the current resource file */
81 static WORD get_byte(void)
83 unsigned char ret = *file_pos++;
84 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
88 /* get the next word from the current resource file */
89 static WORD get_word(void)
91 /* might not be aligned */
92 /* FIXME: should we change this on big-endian machines? */
93 unsigned char low = get_byte();
94 unsigned char high = get_byte();
95 return low | (high << 8);
98 /* get the next dword from the current resource file */
99 static DWORD get_dword(void)
101 WORD low = get_word();
102 WORD high = get_word();
103 return low | (high << 16);
106 /* get a string from the current resource file */
107 static void get_string( struct string_id *str )
109 if (*file_pos == 0xff)
111 get_byte(); /* skip the 0xff */
113 str->id = get_word();
117 char *p = xmalloc( (strlen(file_pos) + 1) );
120 while ((*p++ = get_byte()));
124 /* load the next resource from the current file */
125 static void load_next_resource(void)
127 struct resource *res = add_resource();
129 get_string( &res->type );
130 get_string( &res->name );
131 res->memopt = get_word();
132 res->data_size = get_dword();
133 res->data = file_pos;
134 file_pos += res->data_size;
135 if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
138 /* load a Win16 .res file */
139 void load_res16_file( const char *name )
145 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
146 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
147 if (!st.st_size) fatal_error( "%s is an empty file\n" );
148 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
150 base = xmalloc( st.st_size );
151 if (read( fd, base, st.st_size ) != st.st_size)
152 fatal_error( "Cannot read %s\n", name );
157 file_end = file_pos + st.st_size;
158 while (file_pos < file_end) load_next_resource();
161 /* compare two strings/ids */
162 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
166 if (!str2->str) return str1->id - str2->id;
167 return 1; /* an id compares larger than a string */
169 if (!str2->str) return -1;
170 return strcasecmp( str1->str, str2->str );
173 /* compare two resources for sorting the resource directory */
174 /* resources are stored first by type, then by name */
175 static int cmp_res( const void *ptr1, const void *ptr2 )
177 const struct resource *res1 = ptr1;
178 const struct resource *res2 = ptr2;
181 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
182 return cmp_string( &res1->name, &res2->name );
185 /* build the 2-level (type,name) resource tree */
186 static void build_resource_tree(void)
189 struct res_type *type = NULL;
191 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
193 for (i = 0; i < nb_resources; i++)
195 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
196 type = add_type( &resources[i] );
201 inline static void put_byte( unsigned char **buffer, unsigned char val )
206 inline static void put_word( unsigned char **buffer, WORD val )
208 put_byte( buffer, LOBYTE(val) );
209 put_byte( buffer, HIBYTE(val) );
212 /* output a string preceded by its length */
213 static void output_string( unsigned char **buffer, const char *str )
215 int len = strlen(str);
216 put_byte( buffer, len );
217 while (len--) put_byte( buffer, *str++ );
220 /* output the resource data */
221 int output_res16_data( FILE *outfile )
223 const struct resource *res;
224 unsigned char *buffer, *p;
227 if (!nb_resources) return 0;
229 for (i = total = 0, res = resources; i < nb_resources; i++, res++)
230 total += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
232 buffer = p = xmalloc( total );
233 for (i = 0, res = resources; i < nb_resources; i++, res++)
235 memcpy( p, res->data, res->data_size );
237 while ((int)p & ALIGN_MASK) *p++ = 0;
239 dump_bytes( outfile, buffer, total, "resource_data", 1 );
244 /* output the resource definitions */
245 int output_res16_directory( unsigned char *buffer )
247 int i, j, offset, res_offset = 0;
248 const struct res_type *type;
249 const struct resource *res;
250 unsigned char *start = buffer;
252 build_resource_tree();
254 offset = 4; /* alignment + terminator */
255 offset += nb_types * 8; /* typeinfo structures */
256 offset += nb_resources * 12; /* nameinfo structures */
258 put_word( &buffer, ALIGNMENT );
260 /* type and name structures */
262 for (i = 0, type = res_types; i < nb_types; i++, type++)
266 put_word( &buffer, offset );
267 offset += strlen(type->type->str) + 1;
270 put_word( &buffer, type->type->id | 0x8000 );
272 put_word( &buffer, type->nb_names );
273 put_word( &buffer, 0 );
274 put_word( &buffer, 0 );
276 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
278 put_word( &buffer, res_offset >> ALIGNMENT );
279 put_word( &buffer, (res->data_size + ALIGN_MASK) >> ALIGNMENT );
280 put_word( &buffer, res->memopt );
283 put_word( &buffer, offset );
284 offset += strlen(res->name.str) + 1;
287 put_word( &buffer, res->name.id | 0x8000 );
288 put_word( &buffer, 0 );
289 put_word( &buffer, 0 );
290 res_offset += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
293 put_word( &buffer, 0 ); /* terminator */
297 for (i = 0, type = res_types; i < nb_types; i++, type++)
299 if (type->type->str) output_string( &buffer, type->type->str );
300 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
302 if (res->name.str) output_string( &buffer, res->name.str );
305 put_byte( &buffer, 0 ); /* names terminator */
306 if ((buffer - start) & 1) put_byte( &buffer, 0 ); /* align on word boundary */
308 return buffer - start;