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
43 /* Unicode string or integer id */
46 char *str
; /* ptr to string */
47 unsigned short id
; /* integer id if str is NULL */
50 /* descriptor for a resource */
53 struct string_id type
;
54 struct string_id name
;
56 unsigned int data_size
;
60 /* type level of the resource tree */
63 const struct string_id
*type
; /* type name */
64 const struct resource
*res
; /* first resource of this type */
65 unsigned int nb_names
; /* total number of names */
68 /* top level of the resource tree */
71 struct res_type
*types
; /* types array */
72 unsigned int nb_types
; /* total number of types */
75 static const unsigned char *file_pos
; /* current position in resource file */
76 static const unsigned char *file_end
; /* end of resource file */
77 static const char *file_name
; /* current resource file name */
80 static inline struct resource
*add_resource( DLLSPEC
*spec
)
82 spec
->resources
= xrealloc( spec
->resources
, (spec
->nb_resources
+ 1) * sizeof(*spec
->resources
) );
83 return &spec
->resources
[spec
->nb_resources
++];
86 static struct res_type
*add_type( struct res_tree
*tree
, const struct resource
*res
)
88 struct res_type
*type
;
89 tree
->types
= xrealloc( tree
->types
, (tree
->nb_types
+ 1) * sizeof(*tree
->types
) );
90 type
= &tree
->types
[tree
->nb_types
++];
91 type
->type
= &res
->type
;
97 /* get the next byte from the current resource file */
98 static unsigned char get_byte(void)
100 unsigned char ret
= *file_pos
++;
101 if (file_pos
> file_end
) fatal_error( "%s is a truncated/corrupted file\n", file_name
);
105 /* get the next word from the current resource file */
106 static unsigned short get_word(void)
108 /* might not be aligned */
109 #ifdef WORDS_BIGENDIAN
110 unsigned char high
= get_byte();
111 unsigned char low
= get_byte();
113 unsigned char low
= get_byte();
114 unsigned char high
= get_byte();
116 return low
| (high
<< 8);
119 /* get the next dword from the current resource file */
120 static unsigned int get_dword(void)
122 #ifdef WORDS_BIGENDIAN
123 unsigned short high
= get_word();
124 unsigned short low
= get_word();
126 unsigned short low
= get_word();
127 unsigned short high
= get_word();
129 return low
| (high
<< 16);
132 /* get a string from the current resource file */
133 static void get_string( struct string_id
*str
)
135 if (*file_pos
== 0xff)
137 get_byte(); /* skip the 0xff */
139 str
->id
= get_word();
143 char *p
= xmalloc(strlen((const char *)file_pos
) + 1);
146 while ((*p
++ = get_byte()));
150 /* load the next resource from the current file */
151 static void load_next_resource( DLLSPEC
*spec
)
153 struct resource
*res
= add_resource( spec
);
155 get_string( &res
->type
);
156 get_string( &res
->name
);
157 res
->memopt
= get_word();
158 res
->data_size
= get_dword();
159 res
->data
= file_pos
;
160 file_pos
+= res
->data_size
;
161 if (file_pos
> file_end
) fatal_error( "%s is a truncated/corrupted file\n", file_name
);
164 /* load a Win16 .res file */
165 void load_res16_file( const char *name
, DLLSPEC
*spec
)
171 if ((fd
= open( name
, O_RDONLY
)) == -1) fatal_perror( "Cannot open %s", name
);
172 if ((fstat( fd
, &st
) == -1)) fatal_perror( "Cannot stat %s", name
);
173 if (!st
.st_size
) fatal_error( "%s is an empty file\n", name
);
175 if ((base
= mmap( NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0 )) == (void*)-1)
176 #endif /* HAVE_MMAP */
178 base
= xmalloc( st
.st_size
);
179 if (read( fd
, base
, st
.st_size
) != st
.st_size
)
180 fatal_error( "Cannot read %s\n", name
);
185 file_end
= file_pos
+ st
.st_size
;
186 while (file_pos
< file_end
) load_next_resource( spec
);
189 /* compare two strings/ids */
190 static int cmp_string( const struct string_id
*str1
, const struct string_id
*str2
)
194 if (!str2
->str
) return str1
->id
- str2
->id
;
195 return 1; /* an id compares larger than a string */
197 if (!str2
->str
) return -1;
198 return strcasecmp( str1
->str
, str2
->str
);
201 /* compare two resources for sorting the resource directory */
202 /* resources are stored first by type, then by name */
203 static int cmp_res( const void *ptr1
, const void *ptr2
)
205 const struct resource
*res1
= ptr1
;
206 const struct resource
*res2
= ptr2
;
209 if ((ret
= cmp_string( &res1
->type
, &res2
->type
))) return ret
;
210 return cmp_string( &res1
->name
, &res2
->name
);
213 /* build the 2-level (type,name) resource tree */
214 static struct res_tree
*build_resource_tree( DLLSPEC
*spec
)
217 struct res_tree
*tree
;
218 struct res_type
*type
= NULL
;
220 qsort( spec
->resources
, spec
->nb_resources
, sizeof(*spec
->resources
), cmp_res
);
222 tree
= xmalloc( sizeof(*tree
) );
226 for (i
= 0; i
< spec
->nb_resources
; i
++)
228 if (!i
|| cmp_string( &spec
->resources
[i
].type
, &spec
->resources
[i
-1].type
)) /* new type */
229 type
= add_type( tree
, &spec
->resources
[i
] );
235 /* free the resource tree */
236 static void free_resource_tree( struct res_tree
*tree
)
242 /* output a string preceded by its length */
243 static void output_string( const char *str
)
245 unsigned int i
, len
= strlen(str
);
246 output( "\t.byte 0x%02x", len
);
247 for (i
= 0; i
< len
; i
++) output( ",0x%02x", (unsigned char)str
[i
] );
248 output( " /* %s */\n", str
);
251 /* output the resource data */
252 void output_res16_data( DLLSPEC
*spec
)
254 const struct resource
*res
;
257 if (!spec
->nb_resources
) return;
259 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
261 output( ".L__wine_spec_resource_%u:\n", i
);
262 dump_bytes( res
->data
, res
->data_size
);
263 output( ".L__wine_spec_resource_%u_end:\n", i
);
267 /* output the resource definitions */
268 void output_res16_directory( DLLSPEC
*spec
)
271 struct res_tree
*tree
;
272 const struct res_type
*type
;
273 const struct resource
*res
;
275 tree
= build_resource_tree( spec
);
277 output( "\n.L__wine_spec_ne_rsrctab:\n" );
278 output( "\t%s 0\n", get_asm_short_keyword() ); /* alignment */
280 /* type and name structures */
282 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
285 output( "\t%s .L__wine_spec_restype_%u-.L__wine_spec_ne_rsrctab\n",
286 get_asm_short_keyword(), i
);
288 output( "\t%s 0x%04x\n", get_asm_short_keyword(), type
->type
->id
| 0x8000 );
290 output( "\t%s %u,0,0\n", get_asm_short_keyword(), type
->nb_names
);
292 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
294 output( "\t%s .L__wine_spec_resource_%lu-.L__wine_spec_dos_header\n",
295 get_asm_short_keyword(), (unsigned long)(res
- spec
->resources
) );
296 output( "\t%s .L__wine_spec_resource_%lu_end-.L__wine_spec_resource_%lu\n",
297 get_asm_short_keyword(), (unsigned long)(res
- spec
->resources
),
298 (unsigned long)(res
- spec
->resources
) );
299 output( "\t%s 0x%04x\n", get_asm_short_keyword(), res
->memopt
);
301 output( "\t%s .L__wine_spec_resname_%u_%u-.L__wine_spec_ne_rsrctab\n",
302 get_asm_short_keyword(), i
, j
);
304 output( "\t%s 0x%04x\n", get_asm_short_keyword(), res
->name
.id
| 0x8000 );
306 output( "\t%s 0,0\n", get_asm_short_keyword() );
309 output( "\t%s 0\n", get_asm_short_keyword() ); /* terminator */
313 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
317 output( ".L__wine_spec_restype_%u:\n", i
);
318 output_string( type
->type
->str
);
320 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
324 output( ".L__wine_spec_resname_%u_%u:\n", i
, j
);
325 output_string( res
->name
.str
);
329 output( "\t.byte 0\n" ); /* names terminator */
331 free_resource_tree( tree
);