Prevent crash when no URL is specified.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #include <fcntl.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
39
40 #include "windef.h"
41 #include "winbase.h"
42 #include "build.h"
43
44 #define ALIGNMENT 2 /* alignment for resource data */
45 #define ALIGN_MASK ((1 << ALIGNMENT) - 1)
46
47 /* Unicode string or integer id */
48 struct string_id
49 {
50     char  *str;  /* ptr to string */
51     WORD   id;   /* integer id if str is NULL */
52 };
53
54 /* descriptor for a resource */
55 struct resource
56 {
57     struct string_id type;
58     struct string_id name;
59     const void      *data;
60     unsigned int     data_size;
61     WORD             memopt;
62 };
63
64 /* type level of the resource tree */
65 struct res_type
66 {
67     const struct string_id  *type;         /* type name */
68     const struct resource   *res;          /* first resource of this type */
69     unsigned int             nb_names;     /* total number of names */
70 };
71
72 /* top level of the resource tree */
73 struct res_tree
74 {
75     struct res_type *types;                /* types array */
76     unsigned int     nb_types;             /* total number of types */
77 };
78
79 static const unsigned char *file_pos;   /* current position in resource file */
80 static const unsigned char *file_end;   /* end of resource file */
81 static const char *file_name;  /* current resource file name */
82
83
84 inline static struct resource *add_resource( DLLSPEC *spec )
85 {
86     spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
87     return &spec->resources[spec->nb_resources++];
88 }
89
90 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
91 {
92     struct res_type *type;
93     tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
94     type = &tree->types[tree->nb_types++];
95     type->type        = &res->type;
96     type->res         = res;
97     type->nb_names    = 0;
98     return type;
99 }
100
101 /* get the next byte from the current resource file */
102 static unsigned char get_byte(void)
103 {
104     unsigned char ret = *file_pos++;
105     if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
106     return ret;
107 }
108
109 /* get the next word from the current resource file */
110 static WORD get_word(void)
111 {
112     /* might not be aligned */
113 #ifdef WORDS_BIGENDIAN
114     unsigned char high = get_byte();
115     unsigned char low = get_byte();
116 #else
117     unsigned char low = get_byte();
118     unsigned char high = get_byte();
119 #endif
120     return low | (high << 8);
121 }
122
123 /* get the next dword from the current resource file */
124 static DWORD get_dword(void)
125 {
126 #ifdef WORDS_BIGENDIAN
127     WORD high = get_word();
128     WORD low = get_word();
129 #else
130     WORD low = get_word();
131     WORD high = get_word();
132 #endif
133     return low | (high << 16);
134 }
135
136 /* get a string from the current resource file */
137 static void get_string( struct string_id *str )
138 {
139     if (*file_pos == 0xff)
140     {
141         get_byte();  /* skip the 0xff */
142         str->str = NULL;
143         str->id = get_word();
144     }
145     else
146     {
147         char *p = xmalloc( (strlen(file_pos) + 1) );
148         str->str = p;
149         str->id = 0;
150         while ((*p++ = get_byte()));
151     }
152 }
153
154 /* load the next resource from the current file */
155 static void load_next_resource( DLLSPEC *spec )
156 {
157     struct resource *res = add_resource( spec );
158
159     get_string( &res->type );
160     get_string( &res->name );
161     res->memopt    = get_word();
162     res->data_size = get_dword();
163     res->data      = file_pos;
164     file_pos += res->data_size;
165     if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
166 }
167
168 /* load a Win16 .res file */
169 void load_res16_file( const char *name, DLLSPEC *spec )
170 {
171     int fd;
172     void *base;
173     struct stat st;
174
175     if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
176     if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
177     if (!st.st_size) fatal_error( "%s is an empty file\n", name );
178 #ifdef  HAVE_MMAP
179     if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
180 #endif  /* HAVE_MMAP */
181     {
182         base = xmalloc( st.st_size );
183         if (read( fd, base, st.st_size ) != st.st_size)
184             fatal_error( "Cannot read %s\n", name );
185     }
186
187     file_name = name;
188     file_pos  = base;
189     file_end  = file_pos + st.st_size;
190     while (file_pos < file_end) load_next_resource( spec );
191 }
192
193 /* compare two strings/ids */
194 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
195 {
196     if (!str1->str)
197     {
198         if (!str2->str) return str1->id - str2->id;
199         return 1;  /* an id compares larger than a string */
200     }
201     if (!str2->str) return -1;
202     return strcasecmp( str1->str, str2->str );
203 }
204
205 /* compare two resources for sorting the resource directory */
206 /* resources are stored first by type, then by name */
207 static int cmp_res( const void *ptr1, const void *ptr2 )
208 {
209     const struct resource *res1 = ptr1;
210     const struct resource *res2 = ptr2;
211     int ret;
212
213     if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
214     return cmp_string( &res1->name, &res2->name );
215 }
216
217 /* build the 2-level (type,name) resource tree */
218 static struct res_tree *build_resource_tree( DLLSPEC *spec )
219 {
220     unsigned int i;
221     struct res_tree *tree;
222     struct res_type *type = NULL;
223
224     qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
225
226     tree = xmalloc( sizeof(*tree) );
227     tree->types = NULL;
228     tree->nb_types = 0;
229
230     for (i = 0; i < spec->nb_resources; i++)
231     {
232         if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
233             type = add_type( tree, &spec->resources[i] );
234         type->nb_names++;
235     }
236     return tree;
237 }
238
239 /* free the resource tree */
240 static void free_resource_tree( struct res_tree *tree )
241 {
242     free( tree->types );
243     free( tree );
244 }
245
246 inline static void put_byte( unsigned char **buffer, unsigned char val )
247 {
248     *(*buffer)++ = val;
249 }
250
251 inline static void put_word( unsigned char **buffer, WORD val )
252 {
253 #ifdef WORDS_BIGENDIAN
254     put_byte( buffer, HIBYTE(val) );
255     put_byte( buffer, LOBYTE(val) );
256 #else
257     put_byte( buffer, LOBYTE(val) );
258     put_byte( buffer, HIBYTE(val) );
259 #endif
260 }
261
262 /* output a string preceded by its length */
263 static void output_string( unsigned char **buffer, const char *str )
264 {
265     int len = strlen(str);
266     put_byte( buffer, len );
267     while (len--) put_byte( buffer, *str++ );
268 }
269
270 /* output the resource data */
271 int output_res16_data( FILE *outfile, DLLSPEC *spec )
272 {
273     const struct resource *res;
274     unsigned char *buffer, *p;
275     unsigned int i;
276     int total;
277
278     if (!spec->nb_resources) return 0;
279
280     for (i = total = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
281         total += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
282
283     buffer = p = xmalloc( total );
284     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
285     {
286         memcpy( p, res->data, res->data_size );
287         p += res->data_size;
288         while ((int)p & ALIGN_MASK) *p++ = 0;
289     }
290     dump_bytes( outfile, buffer, total, "resource_data", 1 );
291     free( buffer );
292     return total;
293 }
294
295 /* output the resource definitions */
296 int output_res16_directory( unsigned char *buffer, DLLSPEC *spec )
297 {
298     int offset, res_offset = 0;
299     unsigned int i, j;
300     struct res_tree *tree;
301     const struct res_type *type;
302     const struct resource *res;
303     unsigned char *start = buffer;
304
305     tree = build_resource_tree( spec );
306
307     offset = 4;  /* alignment + terminator */
308     offset += tree->nb_types * 8;  /* typeinfo structures */
309     offset += spec->nb_resources * 12;  /* nameinfo structures */
310
311     put_word( &buffer, ALIGNMENT );
312
313     /* type and name structures */
314
315     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
316     {
317         if (type->type->str)
318         {
319             put_word( &buffer, offset );
320             offset += strlen(type->type->str) + 1;
321         }
322         else
323             put_word( &buffer, type->type->id | 0x8000 );
324
325         put_word( &buffer, type->nb_names );
326         put_word( &buffer, 0 );
327         put_word( &buffer, 0 );
328
329         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
330         {
331             put_word( &buffer, res_offset >> ALIGNMENT );
332             put_word( &buffer, (res->data_size + ALIGN_MASK) >> ALIGNMENT );
333             put_word( &buffer, res->memopt );
334             if (res->name.str)
335             {
336                 put_word( &buffer, offset );
337                 offset += strlen(res->name.str) + 1;
338             }
339             else
340                 put_word( &buffer, res->name.id | 0x8000 );
341             put_word( &buffer, 0 );
342             put_word( &buffer, 0 );
343             res_offset += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
344         }
345     }
346     put_word( &buffer, 0 );  /* terminator */
347
348     /* name strings */
349
350     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
351     {
352         if (type->type->str) output_string( &buffer, type->type->str );
353         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
354         {
355             if (res->name.str) output_string( &buffer, res->name.str );
356         }
357     }
358     put_byte( &buffer, 0 );  /* names terminator */
359     if ((buffer - start) & 1) put_byte( &buffer, 0 );  /* align on word boundary */
360
361     free_resource_tree( tree );
362     return buffer - start;
363 }