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>
37 /* Unicode string or integer id */
40 char *str
; /* ptr to string */
41 unsigned short id
; /* integer id if str is NULL */
44 /* descriptor for a resource */
47 struct string_id type
;
48 struct string_id name
;
50 unsigned int name_offset
;
51 unsigned int data_size
;
55 /* type level of the resource tree */
58 const struct string_id
*type
; /* type name */
59 struct resource
*res
; /* first resource of this type */
60 unsigned int name_offset
; /* name offset if string */
61 unsigned int nb_names
; /* total number of names */
64 /* top level of the resource tree */
67 struct res_type
*types
; /* types array */
68 unsigned int nb_types
; /* total number of types */
72 static inline struct resource
*add_resource( DLLSPEC
*spec
)
74 spec
->resources
= xrealloc( spec
->resources
, (spec
->nb_resources
+ 1) * sizeof(*spec
->resources
) );
75 return &spec
->resources
[spec
->nb_resources
++];
78 static struct res_type
*add_type( struct res_tree
*tree
, struct resource
*res
)
80 struct res_type
*type
;
81 tree
->types
= xrealloc( tree
->types
, (tree
->nb_types
+ 1) * sizeof(*tree
->types
) );
82 type
= &tree
->types
[tree
->nb_types
++];
83 type
->type
= &res
->type
;
89 /* get a string from the current resource file */
90 static void get_string( struct string_id
*str
)
92 unsigned char c
= get_byte();
101 str
->str
= (char *)input_buffer
+ input_buffer_pos
- 1;
103 while (get_byte()) /* nothing */;
107 /* load the next resource from the current file */
108 static void load_next_resource( DLLSPEC
*spec
)
110 struct resource
*res
= add_resource( spec
);
112 get_string( &res
->type
);
113 get_string( &res
->name
);
114 res
->memopt
= get_word();
115 res
->data_size
= get_dword();
116 res
->data
= input_buffer
+ input_buffer_pos
;
117 input_buffer_pos
+= res
->data_size
;
118 if (input_buffer_pos
> input_buffer_size
)
119 fatal_error( "%s is a truncated/corrupted file\n", input_buffer_filename
);
122 /* load a Win16 .res file */
123 void load_res16_file( const char *name
, DLLSPEC
*spec
)
125 init_input_buffer( name
);
126 while (input_buffer_pos
< input_buffer_size
) load_next_resource( spec
);
129 /* compare two strings/ids */
130 static int cmp_string( const struct string_id
*str1
, const struct string_id
*str2
)
134 if (!str2
->str
) return str1
->id
- str2
->id
;
135 return 1; /* an id compares larger than a string */
137 if (!str2
->str
) return -1;
138 return strcasecmp( str1
->str
, str2
->str
);
141 /* compare two resources for sorting the resource directory */
142 /* resources are stored first by type, then by name */
143 static int cmp_res( const void *ptr1
, const void *ptr2
)
145 const struct resource
*res1
= ptr1
;
146 const struct resource
*res2
= ptr2
;
149 if ((ret
= cmp_string( &res1
->type
, &res2
->type
))) return ret
;
150 return cmp_string( &res1
->name
, &res2
->name
);
153 /* build the 2-level (type,name) resource tree */
154 static struct res_tree
*build_resource_tree( DLLSPEC
*spec
)
156 unsigned int i
, j
, offset
;
157 struct res_tree
*tree
;
158 struct res_type
*type
= NULL
;
159 struct resource
*res
;
161 qsort( spec
->resources
, spec
->nb_resources
, sizeof(*spec
->resources
), cmp_res
);
163 offset
= 2; /* alignment */
164 tree
= xmalloc( sizeof(*tree
) );
168 for (i
= 0; i
< spec
->nb_resources
; i
++)
170 if (!i
|| cmp_string( &spec
->resources
[i
].type
, &spec
->resources
[i
-1].type
)) /* new type */
172 type
= add_type( tree
, &spec
->resources
[i
] );
178 offset
+= 2; /* terminator */
180 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
184 type
->name_offset
= offset
;
185 offset
+= strlen(type
->type
->str
) + 1;
187 else type
->name_offset
= type
->type
->id
| 0x8000;
189 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
193 res
->name_offset
= offset
;
194 offset
+= strlen(res
->name
.str
) + 1;
196 else res
->name_offset
= res
->name
.id
| 0x8000;
202 /* free the resource tree */
203 static void free_resource_tree( struct res_tree
*tree
)
209 /* output a string preceded by its length */
210 static void output_string( const char *str
)
212 unsigned int i
, len
= strlen(str
);
213 output( "\t.byte 0x%02x", len
);
214 for (i
= 0; i
< len
; i
++) output( ",0x%02x", (unsigned char)str
[i
] );
215 output( " /* %s */\n", str
);
218 /* output a string preceded by its length in binary format*/
219 static void output_bin_string( const char *str
)
221 put_byte( strlen(str
) );
222 while (*str
) put_byte( *str
++ );
225 /* output the resource data */
226 void output_res16_data( DLLSPEC
*spec
)
228 const struct resource
*res
;
231 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
233 output( ".L__wine_spec_resource_%u:\n", i
);
234 dump_bytes( res
->data
, res
->data_size
);
238 /* output the resource definitions */
239 void output_res16_directory( DLLSPEC
*spec
)
242 struct res_tree
*tree
;
243 const struct res_type
*type
;
244 const struct resource
*res
;
246 tree
= build_resource_tree( spec
);
248 output( "\n.L__wine_spec_ne_rsrctab:\n" );
249 output( "\t%s 0\n", get_asm_short_keyword() ); /* alignment */
251 /* type and name structures */
253 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
255 output( "\t%s 0x%04x,%u,0,0\n", get_asm_short_keyword(), type
->name_offset
, type
->nb_names
);
257 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
259 output( "\t%s .L__wine_spec_resource_%lu-.L__wine_spec_dos_header,%u\n",
260 get_asm_short_keyword(), (unsigned long)(res
- spec
->resources
), res
->data_size
);
261 output( "\t%s 0x%04x,0x%04x,0,0\n", get_asm_short_keyword(), res
->memopt
, res
->name_offset
);
264 output( "\t%s 0\n", get_asm_short_keyword() ); /* terminator */
268 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
270 if (type
->type
->str
) output_string( type
->type
->str
);
271 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
272 if (res
->name
.str
) output_string( res
->name
.str
);
274 output( "\t.byte 0\n" ); /* names terminator */
276 free_resource_tree( tree
);
279 /* output the resource data in binary format */
280 void output_bin_res16_data( DLLSPEC
*spec
)
282 const struct resource
*res
;
285 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
286 put_data( res
->data
, res
->data_size
);
289 /* output the resource definitions in binary format */
290 void output_bin_res16_directory( DLLSPEC
*spec
, unsigned int data_offset
)
293 struct res_tree
*tree
;
294 const struct res_type
*type
;
295 const struct resource
*res
;
297 tree
= build_resource_tree( spec
);
299 put_word( 0 ); /* alignment */
301 /* type and name structures */
303 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
305 put_word( type
->name_offset
);
306 put_word( type
->nb_names
);
310 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
312 put_word( data_offset
);
313 put_word( res
->data_size
);
314 put_word( res
->memopt
);
315 put_word( res
->name_offset
);
318 data_offset
+= res
->data_size
;
321 put_word( 0 ); /* terminator */
325 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
327 if (type
->type
->str
) output_bin_string( type
->type
->str
);
328 for (j
= 0, res
= type
->res
; j
< type
->nb_names
; j
++, res
++)
329 if (res
->name
.str
) output_bin_string( res
->name
.str
);
331 put_byte( 0 ); /* names terminator */
333 free_resource_tree( tree
);