wininet: Don't delete the cache file when closing a request.
[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 "build.h"
42
43 /* Unicode string or integer id */
44 struct string_id
45 {
46     char  *str;  /* ptr to string */
47     unsigned short id;   /* integer id if str is NULL */
48 };
49
50 /* descriptor for a resource */
51 struct resource
52 {
53     struct string_id type;
54     struct string_id name;
55     const void      *data;
56     unsigned int     data_size;
57     unsigned int     memopt;
58 };
59
60 /* type level of the resource tree */
61 struct res_type
62 {
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 */
66 };
67
68 /* top level of the resource tree */
69 struct res_tree
70 {
71     struct res_type *types;                /* types array */
72     unsigned int     nb_types;             /* total number of types */
73 };
74
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 */
78
79
80 static inline struct resource *add_resource( DLLSPEC *spec )
81 {
82     spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
83     return &spec->resources[spec->nb_resources++];
84 }
85
86 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
87 {
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;
92     type->res         = res;
93     type->nb_names    = 0;
94     return type;
95 }
96
97 /* get the next byte from the current resource file */
98 static unsigned char get_byte(void)
99 {
100     unsigned char ret = *file_pos++;
101     if (file_pos > file_end) fatal_error( "%s is a truncated/corrupted file\n", file_name );
102     return ret;
103 }
104
105 /* get the next word from the current resource file */
106 static unsigned short get_word(void)
107 {
108     /* might not be aligned */
109 #ifdef WORDS_BIGENDIAN
110     unsigned char high = get_byte();
111     unsigned char low = get_byte();
112 #else
113     unsigned char low = get_byte();
114     unsigned char high = get_byte();
115 #endif
116     return low | (high << 8);
117 }
118
119 /* get the next dword from the current resource file */
120 static unsigned int get_dword(void)
121 {
122 #ifdef WORDS_BIGENDIAN
123     unsigned short high = get_word();
124     unsigned short low = get_word();
125 #else
126     unsigned short low = get_word();
127     unsigned short high = get_word();
128 #endif
129     return low | (high << 16);
130 }
131
132 /* get a string from the current resource file */
133 static void get_string( struct string_id *str )
134 {
135     if (*file_pos == 0xff)
136     {
137         get_byte();  /* skip the 0xff */
138         str->str = NULL;
139         str->id = get_word();
140     }
141     else
142     {
143         char *p = xmalloc(strlen((const char *)file_pos) + 1);
144         str->str = p;
145         str->id = 0;
146         while ((*p++ = get_byte()));
147     }
148 }
149
150 /* load the next resource from the current file */
151 static void load_next_resource( DLLSPEC *spec )
152 {
153     struct resource *res = add_resource( spec );
154
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 );
162 }
163
164 /* load a Win16 .res file */
165 void load_res16_file( const char *name, DLLSPEC *spec )
166 {
167     int fd;
168     void *base;
169     struct stat st;
170
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 );
174 #ifdef  HAVE_MMAP
175     if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
176 #endif  /* HAVE_MMAP */
177     {
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 );
181     }
182
183     file_name = name;
184     file_pos  = base;
185     file_end  = file_pos + st.st_size;
186     while (file_pos < file_end) load_next_resource( spec );
187 }
188
189 /* compare two strings/ids */
190 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
191 {
192     if (!str1->str)
193     {
194         if (!str2->str) return str1->id - str2->id;
195         return 1;  /* an id compares larger than a string */
196     }
197     if (!str2->str) return -1;
198     return strcasecmp( str1->str, str2->str );
199 }
200
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 )
204 {
205     const struct resource *res1 = ptr1;
206     const struct resource *res2 = ptr2;
207     int ret;
208
209     if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
210     return cmp_string( &res1->name, &res2->name );
211 }
212
213 /* build the 2-level (type,name) resource tree */
214 static struct res_tree *build_resource_tree( DLLSPEC *spec )
215 {
216     unsigned int i;
217     struct res_tree *tree;
218     struct res_type *type = NULL;
219
220     qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
221
222     tree = xmalloc( sizeof(*tree) );
223     tree->types = NULL;
224     tree->nb_types = 0;
225
226     for (i = 0; i < spec->nb_resources; i++)
227     {
228         if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
229             type = add_type( tree, &spec->resources[i] );
230         type->nb_names++;
231     }
232     return tree;
233 }
234
235 /* free the resource tree */
236 static void free_resource_tree( struct res_tree *tree )
237 {
238     free( tree->types );
239     free( tree );
240 }
241
242 /* output a string preceded by its length */
243 static void output_string( const char *str )
244 {
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 );
249 }
250
251 /* output the resource data */
252 void output_res16_data( DLLSPEC *spec )
253 {
254     const struct resource *res;
255     unsigned int i;
256
257     if (!spec->nb_resources) return;
258
259     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
260     {
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 );
264     }
265 }
266
267 /* output the resource definitions */
268 void output_res16_directory( DLLSPEC *spec )
269 {
270     unsigned int i, j;
271     struct res_tree *tree;
272     const struct res_type *type;
273     const struct resource *res;
274
275     tree = build_resource_tree( spec );
276
277     output( "\n.L__wine_spec_ne_rsrctab:\n" );
278     output( "\t%s 0\n", get_asm_short_keyword() );  /* alignment */
279
280     /* type and name structures */
281
282     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
283     {
284         if (type->type->str)
285             output( "\t%s .L__wine_spec_restype_%u-.L__wine_spec_ne_rsrctab\n",
286                      get_asm_short_keyword(), i );
287         else
288             output( "\t%s 0x%04x\n", get_asm_short_keyword(), type->type->id | 0x8000 );
289
290         output( "\t%s %u,0,0\n", get_asm_short_keyword(), type->nb_names );
291
292         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
293         {
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 );
300             if (res->name.str)
301                 output( "\t%s .L__wine_spec_resname_%u_%u-.L__wine_spec_ne_rsrctab\n",
302                          get_asm_short_keyword(), i, j );
303             else
304                 output( "\t%s 0x%04x\n", get_asm_short_keyword(), res->name.id | 0x8000 );
305
306             output( "\t%s 0,0\n", get_asm_short_keyword() );
307         }
308     }
309     output( "\t%s 0\n", get_asm_short_keyword() );  /* terminator */
310
311     /* name strings */
312
313     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
314     {
315         if (type->type->str)
316         {
317             output( ".L__wine_spec_restype_%u:\n", i );
318             output_string( type->type->str );
319         }
320         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
321         {
322             if (res->name.str)
323             {
324                 output( ".L__wine_spec_resname_%u_%u:\n", i, j );
325                 output_string( res->name.str );
326             }
327         }
328     }
329     output( "\t.byte 0\n" );  /* names terminator */
330
331     free_resource_tree( tree );
332 }