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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 /* Unicode string or integer id */
48 char *str
; /* ptr to string */
49 WORD id
; /* integer id if str is NULL */
52 /* descriptor for a resource */
55 struct string_id type
;
56 struct string_id name
;
58 unsigned int data_size
;
62 /* type level of the resource tree */
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 */
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 static inline 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
;
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
);
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();
115 unsigned char low
= get_byte();
116 unsigned char high
= get_byte();
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();
128 WORD low
= get_word();
129 WORD high
= get_word();
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 */
141 str
->id
= get_word();
145 char *p
= xmalloc(strlen((const char *)file_pos
) + 1);
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
)
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
);
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
);
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
)
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
;
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
)
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
) );
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
] );
237 /* free the resource tree */
238 static void free_resource_tree( struct res_tree
*tree
)
244 /* output a string preceded by its length */
245 static void output_string( const char *str
)
247 unsigned int i
, len
= strlen(str
);
248 output( "\t.byte 0x%02x", len
);
249 for (i
= 0; i
< len
; i
++) output( ",0x%02x", (unsigned char)str
[i
] );
250 output( " /* %s */\n", str
);
253 /* output the resource data */
254 void output_res16_data( DLLSPEC
*spec
)
256 const struct resource
*res
;
259 if (!spec
->nb_resources
) return;
261 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
263 output( ".L__wine_spec_resource_%u:\n", i
);
264 dump_bytes( res
->data
, res
->data_size
);
265 output( ".L__wine_spec_resource_%u_end:\n", i
);
269 /* output the resource definitions */
270 void output_res16_directory( DLLSPEC
*spec
, const char *header_name
)
273 struct res_tree
*tree
;
274 const struct res_type
*type
;
275 const struct resource
*res
;
277 tree
= build_resource_tree( spec
);
279 output( "\n.L__wine_spec_ne_rsrctab:\n" );
280 output( "\t%s 0\n", get_asm_short_keyword() ); /* alignment */
282 /* type and name structures */
284 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
287 output( "\t%s .L__wine_spec_restype_%u-.L__wine_spec_ne_rsrctab\n",
288 get_asm_short_keyword(), i
);
290 output( "\t%s 0x%04x\n", get_asm_short_keyword(), type
->type
->id
| 0x8000 );
292 output( "\t%s %u,0,0\n", get_asm_short_keyword(), type
->nb_names
);
294 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
296 output( "\t%s .L__wine_spec_resource_%lu-%s\n",
297 get_asm_short_keyword(), (unsigned long)(res
- spec
->resources
), header_name
);
298 output( "\t%s .L__wine_spec_resource_%lu_end-.L__wine_spec_resource_%lu\n",
299 get_asm_short_keyword(), (unsigned long)(res
- spec
->resources
),
300 (unsigned long)(res
- spec
->resources
) );
301 output( "\t%s 0x%04x\n", get_asm_short_keyword(), res
->memopt
);
303 output( "\t%s .L__wine_spec_resname_%u_%u-.L__wine_spec_ne_rsrctab\n",
304 get_asm_short_keyword(), i
, j
);
306 output( "\t%s 0x%04x\n", get_asm_short_keyword(), res
->name
.id
| 0x8000 );
308 output( "\t%s 0,0\n", get_asm_short_keyword() );
311 output( "\t%s 0\n", get_asm_short_keyword() ); /* terminator */
315 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
319 output( ".L__wine_spec_restype_%u:\n", i
);
320 output_string( type
->type
->str
);
322 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
326 output( ".L__wine_spec_resname_%u_%u:\n", i
, j
);
327 output_string( res
->name
.str
);
331 output( "\t.byte 0\n" ); /* names terminator */
333 free_resource_tree( tree
);