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