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