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