widl: Only output code for non-object interfaces in client and server.
[wine.git] / tools / winebuild / res16.c
blobc9860077d4c87d1bdf2e82fd90dd353b4dd3ebf3
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #include <fcntl.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
41 #include "windef.h"
42 #include "winbase.h"
43 #include "build.h"
45 /* Unicode string or integer id */
46 struct string_id
48 char *str; /* ptr to string */
49 WORD id; /* integer id if str is NULL */
52 /* descriptor for a resource */
53 struct resource
55 struct string_id type;
56 struct string_id name;
57 const void *data;
58 unsigned int data_size;
59 WORD memopt;
62 /* type level of the resource tree */
63 struct res_type
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 */
71 struct res_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 inline static 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;
94 type->res = res;
95 type->nb_names = 0;
96 return 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 );
104 return ret;
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();
114 #else
115 unsigned char low = get_byte();
116 unsigned char high = get_byte();
117 #endif
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();
127 #else
128 WORD low = get_word();
129 WORD high = get_word();
130 #endif
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 */
140 str->str = NULL;
141 str->id = get_word();
143 else
145 char *p = xmalloc( (strlen((char*)file_pos) + 1) );
146 str->str = p;
147 str->id = 0;
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 )
169 int fd;
170 void *base;
171 struct stat st;
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 );
176 #ifdef HAVE_MMAP
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 );
185 file_name = name;
186 file_pos = base;
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 )
194 if (!str1->str)
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;
209 int ret;
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 )
218 unsigned int i;
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) );
225 tree->types = NULL;
226 tree->nb_types = 0;
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] );
232 type->nb_names++;
234 return tree;
237 /* free the resource tree */
238 static void free_resource_tree( struct res_tree *tree )
240 free( tree->types );
241 free( tree );
244 /* output a string preceded by its length */
245 static void output_string( FILE *outfile, const char *str )
247 unsigned int i, len = strlen(str);
248 fprintf( outfile, "\t.byte 0x%02x", len );
249 for (i = 0; i < len; i++) fprintf( outfile, ",0x%02x", (unsigned char)str[i] );
250 fprintf( outfile, " /* %s */\n", str );
253 /* output the resource data */
254 void output_res16_data( FILE *outfile, DLLSPEC *spec )
256 const struct resource *res;
257 unsigned int i;
259 if (!spec->nb_resources) return;
261 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
263 fprintf( outfile, ".L__wine_spec_resource_%u:\n", i );
264 dump_bytes( outfile, res->data, res->data_size );
265 fprintf( outfile, ".L__wine_spec_resource_%u_end:\n", i );
269 /* output the resource definitions */
270 void output_res16_directory( FILE *outfile, DLLSPEC *spec, const char *header_name )
272 unsigned int i, j;
273 struct res_tree *tree;
274 const struct res_type *type;
275 const struct resource *res;
277 tree = build_resource_tree( spec );
279 fprintf( outfile, "\n.L__wine_spec_ne_rsrctab:\n" );
280 fprintf( outfile, "\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++)
286 if (type->type->str)
287 fprintf( outfile, "\t%s .L__wine_spec_restype_%u-.L__wine_spec_ne_rsrctab\n",
288 get_asm_short_keyword(), i );
289 else
290 fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), type->type->id | 0x8000 );
292 fprintf( outfile, "\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 fprintf( outfile, "\t%s .L__wine_spec_resource_%u-%s\n",
297 get_asm_short_keyword(), res - spec->resources, header_name );
298 fprintf( outfile, "\t%s .L__wine_spec_resource_%u_end-.L__wine_spec_resource_%u\n",
299 get_asm_short_keyword(), res - spec->resources, res - spec->resources );
300 fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), res->memopt );
301 if (res->name.str)
302 fprintf( outfile, "\t%s .L__wine_spec_resname_%u_%u-.L__wine_spec_ne_rsrctab\n",
303 get_asm_short_keyword(), i, j );
304 else
305 fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), res->name.id | 0x8000 );
307 fprintf( outfile, "\t%s 0,0\n", get_asm_short_keyword() );
310 fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() ); /* terminator */
312 /* name strings */
314 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
316 if (type->type->str)
318 fprintf( outfile, ".L__wine_spec_restype_%u:\n", i );
319 output_string( outfile, type->type->str );
321 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
323 if (res->name.str)
325 fprintf( outfile, ".L__wine_spec_resname_%u_%u:\n", i, j );
326 output_string( outfile, res->name.str );
330 fprintf( outfile, "\t.byte 0\n" ); /* names terminator */
332 free_resource_tree( tree );