d3dxof: Remove superfluous pointer casts.
[wine] / tools / winebuild / res16.c
1 /*
2  * Builtin dlls resource support
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
40
41 #include "windef.h"
42 #include "winbase.h"
43 #include "build.h"
44
45 /* Unicode string or integer id */
46 struct string_id
47 {
48     char  *str;  /* ptr to string */
49     WORD   id;   /* integer id if str is NULL */
50 };
51
52 /* descriptor for a resource */
53 struct resource
54 {
55     struct string_id type;
56     struct string_id name;
57     const void      *data;
58     unsigned int     data_size;
59     WORD             memopt;
60 };
61
62 /* type level of the resource tree */
63 struct res_type
64 {
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 */
68 };
69
70 /* top level of the resource tree */
71 struct res_tree
72 {
73     struct res_type *types;                /* types array */
74     unsigned int     nb_types;             /* total number of types */
75 };
76
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 */
80
81
82 static inline struct resource *add_resource( DLLSPEC *spec )
83 {
84     spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
85     return &spec->resources[spec->nb_resources++];
86 }
87
88 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
89 {
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;
97 }
98
99 /* get the next byte from the current resource file */
100 static unsigned char get_byte(void)
101 {
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;
105 }
106
107 /* get the next word from the current resource file */
108 static WORD get_word(void)
109 {
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);
119 }
120
121 /* get the next dword from the current resource file */
122 static DWORD get_dword(void)
123 {
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);
132 }
133
134 /* get a string from the current resource file */
135 static void get_string( struct string_id *str )
136 {
137     if (*file_pos == 0xff)
138     {
139         get_byte();  /* skip the 0xff */
140         str->str = NULL;
141         str->id = get_word();
142     }
143     else
144     {
145         char *p = xmalloc(strlen((const char *)file_pos) + 1);
146         str->str = p;
147         str->id = 0;
148         while ((*p++ = get_byte()));
149     }
150 }
151
152 /* load the next resource from the current file */
153 static void load_next_resource( DLLSPEC *spec )
154 {
155     struct resource *res = add_resource( spec );
156
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 );
164 }
165
166 /* load a Win16 .res file */
167 void load_res16_file( const char *name, DLLSPEC *spec )
168 {
169     int fd;
170     void *base;
171     struct stat st;
172
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 */
179     {
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 );
183     }
184
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 );
189 }
190
191 /* compare two strings/ids */
192 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
193 {
194     if (!str1->str)
195     {
196         if (!str2->str) return str1->id - str2->id;
197         return 1;  /* an id compares larger than a string */
198     }
199     if (!str2->str) return -1;
200     return strcasecmp( str1->str, str2->str );
201 }
202
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 )
206 {
207     const struct resource *res1 = ptr1;
208     const struct resource *res2 = ptr2;
209     int ret;
210
211     if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
212     return cmp_string( &res1->name, &res2->name );
213 }
214
215 /* build the 2-level (type,name) resource tree */
216 static struct res_tree *build_resource_tree( DLLSPEC *spec )
217 {
218     unsigned int i;
219     struct res_tree *tree;
220     struct res_type *type = NULL;
221
222     qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
223
224     tree = xmalloc( sizeof(*tree) );
225     tree->types = NULL;
226     tree->nb_types = 0;
227
228     for (i = 0; i < spec->nb_resources; i++)
229     {
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++;
233     }
234     return tree;
235 }
236
237 /* free the resource tree */
238 static void free_resource_tree( struct res_tree *tree )
239 {
240     free( tree->types );
241     free( tree );
242 }
243
244 /* output a string preceded by its length */
245 static void output_string( const char *str )
246 {
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 );
251 }
252
253 /* output the resource data */
254 void output_res16_data( DLLSPEC *spec )
255 {
256     const struct resource *res;
257     unsigned int i;
258
259     if (!spec->nb_resources) return;
260
261     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
262     {
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 );
266     }
267 }
268
269 /* output the resource definitions */
270 void output_res16_directory( DLLSPEC *spec, const char *header_name )
271 {
272     unsigned int i, j;
273     struct res_tree *tree;
274     const struct res_type *type;
275     const struct resource *res;
276
277     tree = build_resource_tree( spec );
278
279     output( "\n.L__wine_spec_ne_rsrctab:\n" );
280     output( "\t%s 0\n", get_asm_short_keyword() );  /* alignment */
281
282     /* type and name structures */
283
284     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
285     {
286         if (type->type->str)
287             output( "\t%s .L__wine_spec_restype_%u-.L__wine_spec_ne_rsrctab\n",
288                      get_asm_short_keyword(), i );
289         else
290             output( "\t%s 0x%04x\n", get_asm_short_keyword(), type->type->id | 0x8000 );
291
292         output( "\t%s %u,0,0\n", get_asm_short_keyword(), type->nb_names );
293
294         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
295         {
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 );
302             if (res->name.str)
303                 output( "\t%s .L__wine_spec_resname_%u_%u-.L__wine_spec_ne_rsrctab\n",
304                          get_asm_short_keyword(), i, j );
305             else
306                 output( "\t%s 0x%04x\n", get_asm_short_keyword(), res->name.id | 0x8000 );
307
308             output( "\t%s 0,0\n", get_asm_short_keyword() );
309         }
310     }
311     output( "\t%s 0\n", get_asm_short_keyword() );  /* terminator */
312
313     /* name strings */
314
315     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
316     {
317         if (type->type->str)
318         {
319             output( ".L__wine_spec_restype_%u:\n", i );
320             output_string( type->type->str );
321         }
322         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
323         {
324             if (res->name.str)
325             {
326                 output( ".L__wine_spec_resname_%u_%u:\n", i, j );
327                 output_string( res->name.str );
328             }
329         }
330     }
331     output( "\t.byte 0\n" );  /* names terminator */
332
333     free_resource_tree( tree );
334 }