Use a better location than HKCU\Wine for saving the temporary
[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 <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 inline static 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(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 inline static void put_byte( unsigned char **buffer, unsigned char val )
245 {
246     *(*buffer)++ = val;
247 }
248
249 inline static void put_word( unsigned char **buffer, WORD val )
250 {
251 #ifdef WORDS_BIGENDIAN
252     put_byte( buffer, HIBYTE(val) );
253     put_byte( buffer, LOBYTE(val) );
254 #else
255     put_byte( buffer, LOBYTE(val) );
256     put_byte( buffer, HIBYTE(val) );
257 #endif
258 }
259
260 /* output a string preceded by its length */
261 static void output_string( unsigned char **buffer, const char *str )
262 {
263     int len = strlen(str);
264     put_byte( buffer, len );
265     while (len--) put_byte( buffer, *str++ );
266 }
267
268 /* get the resource data total size */
269 unsigned int get_res16_data_size( DLLSPEC *spec, unsigned int res_offset, unsigned int alignment )
270 {
271     const struct resource *res;
272     unsigned int i, total;
273     unsigned int align_mask = (1 << alignment) - 1;
274
275     if (!spec->nb_resources) return 0;
276
277     /* add padding at the beginning if needed so that resources are properly aligned */
278     total = ((res_offset + align_mask) & ~align_mask) - res_offset;
279
280     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
281         total += (res->data_size + align_mask) & ~align_mask;
282
283     return total;
284 }
285
286 /* output the resource data */
287 unsigned int output_res16_data( unsigned char **ret_buf, DLLSPEC *spec,
288                                 unsigned int res_offset, unsigned int alignment )
289 {
290     const struct resource *res;
291     unsigned char *p;
292     unsigned int i, total, padding;
293     unsigned int align_mask = (1 << alignment) - 1;
294
295     if (!spec->nb_resources) return 0;
296
297     /* add padding at the beginning if needed so that resources are properly aligned */
298     padding = ((res_offset + align_mask) & ~align_mask) - res_offset;
299
300     for (i = total = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
301         total += (res->data_size + align_mask) & ~align_mask;
302
303     *ret_buf = p = xmalloc( total + padding );
304     memset( p, 0, padding );
305     p += padding;
306     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
307     {
308         unsigned int size = (res->data_size + align_mask) & ~align_mask;
309         memcpy( p, res->data, res->data_size );
310         memset( p + res->data_size, 0, size - res->data_size );
311         p += size;
312     }
313     return total;
314 }
315
316 /* get the resource definitions total size */
317 unsigned int get_res16_directory_size( DLLSPEC *spec )
318 {
319     unsigned int i, j, total_size;
320     struct res_tree *tree;
321     const struct res_type *type;
322     const struct resource *res;
323
324     tree = build_resource_tree( spec );
325
326     total_size = 4;  /* alignment + terminator */
327     total_size += tree->nb_types * 8;  /* typeinfo structures */
328     total_size += spec->nb_resources * 12;  /* nameinfo structures */
329
330     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
331     {
332         if (type->type->str) total_size += strlen(type->type->str) + 1;
333         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
334             if (res->name.str) total_size += strlen(res->name.str) + 1;
335     }
336     total_size++;  /* final terminator */
337     if (total_size & 1) total_size++;
338     return total_size;
339 }
340
341 /* output the resource definitions */
342 unsigned int output_res16_directory( unsigned char **ret_buf, DLLSPEC *spec,
343                                      unsigned int res_offset, unsigned int alignment )
344 {
345     int offset;
346     unsigned int i, j, total_size;
347     unsigned int align_mask = (1 << alignment) - 1;
348     struct res_tree *tree;
349     const struct res_type *type;
350     const struct resource *res;
351     unsigned char *buffer;
352
353     tree = build_resource_tree( spec );
354
355     /* make sure data offset is properly aligned */
356     res_offset = (res_offset + align_mask) & ~align_mask;
357
358     /* first compute total size */
359
360     offset = 4;  /* alignment + terminator */
361     offset += tree->nb_types * 8;  /* typeinfo structures */
362     offset += spec->nb_resources * 12;  /* nameinfo structures */
363
364     total_size = offset;
365
366     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
367     {
368         if (type->type->str) total_size += strlen(type->type->str) + 1;
369         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
370             if (res->name.str) total_size += strlen(res->name.str) + 1;
371     }
372     total_size++;  /* final terminator */
373     if (total_size & 1) total_size++;
374     *ret_buf = buffer = xmalloc( total_size );
375
376     put_word( &buffer, alignment );
377
378     /* type and name structures */
379
380     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
381     {
382         if (type->type->str)
383         {
384             put_word( &buffer, offset );
385             offset += strlen(type->type->str) + 1;
386         }
387         else
388             put_word( &buffer, type->type->id | 0x8000 );
389
390         put_word( &buffer, type->nb_names );
391         put_word( &buffer, 0 );
392         put_word( &buffer, 0 );
393
394         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
395         {
396             put_word( &buffer, res_offset >> alignment );
397             put_word( &buffer, (res->data_size + align_mask) >> alignment );
398             put_word( &buffer, res->memopt );
399             if (res->name.str)
400             {
401                 put_word( &buffer, offset );
402                 offset += strlen(res->name.str) + 1;
403             }
404             else
405                 put_word( &buffer, res->name.id | 0x8000 );
406             put_word( &buffer, 0 );
407             put_word( &buffer, 0 );
408             res_offset += (res->data_size + align_mask) & ~align_mask;
409         }
410     }
411     put_word( &buffer, 0 );  /* terminator */
412
413     /* name strings */
414
415     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
416     {
417         if (type->type->str) output_string( &buffer, type->type->str );
418         for (j = 0, res = type->res; j < type->nb_names; j++, res++)
419         {
420             if (res->name.str) output_string( &buffer, res->name.str );
421         }
422     }
423     put_byte( &buffer, 0 );  /* names terminator */
424     if ((buffer - *ret_buf) & 1) put_byte( &buffer, 0 );  /* align on word boundary */
425     assert( buffer - *ret_buf == total_size );
426
427     free_resource_tree( tree );
428     return total_size;
429 }