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