Implement GetGlyphIndices. Tweak GetCharacterPlacement to use it.
[wine/testsucceed.git] / tools / winebuild / res16.c
blob047cc986b6bd7bfe6a173ed695f9fcc35354fecf
1 /*
2 * Builtin dlls resource support
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
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
34 #include "winbase.h"
35 #include "build.h"
37 #define ALIGNMENT 2 /* alignment for resource data */
38 #define ALIGN_MASK ((1 << ALIGNMENT) - 1)
40 /* Unicode string or integer id */
41 struct string_id
43 char *str; /* ptr to string */
44 WORD id; /* integer id if str is NULL */
47 /* descriptor for a resource */
48 struct resource
50 struct string_id type;
51 struct string_id name;
52 const void *data;
53 unsigned int data_size;
54 WORD memopt;
57 /* type level of the resource tree */
58 struct res_type
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 */
65 static struct resource *resources;
66 static int nb_resources;
68 static struct res_type *res_types;
69 static int nb_types; /* total number of types */
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 */
76 inline static struct resource *add_resource(void)
78 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
79 return &resources[nb_resources++];
82 static struct res_type *add_type( const struct resource *res )
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;
93 /* get the next byte from the current resource file */
94 static WORD get_byte(void)
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;
101 /* get the next word from the current resource file */
102 static WORD get_word(void)
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);
115 /* get the next dword from the current resource file */
116 static DWORD get_dword(void)
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);
128 /* get a string from the current resource file */
129 static void get_string( struct string_id *str )
131 if (*file_pos == 0xff)
133 get_byte(); /* skip the 0xff */
134 str->str = NULL;
135 str->id = get_word();
137 else
139 char *p = xmalloc( (strlen(file_pos) + 1) );
140 str->str = p;
141 str->id = 0;
142 while ((*p++ = get_byte()));
146 /* load the next resource from the current file */
147 static void load_next_resource(void)
149 struct resource *res = add_resource();
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 );
160 /* load a Win16 .res file */
161 void load_res16_file( const char *name )
163 int fd;
164 void *base;
165 struct stat st;
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 */
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 );
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();
185 /* compare two strings/ids */
186 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
188 if (!str1->str)
190 if (!str2->str) return str1->id - str2->id;
191 return 1; /* an id compares larger than a string */
193 if (!str2->str) return -1;
194 return strcasecmp( str1->str, str2->str );
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 )
201 const struct resource *res1 = ptr1;
202 const struct resource *res2 = ptr2;
203 int ret;
205 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
206 return cmp_string( &res1->name, &res2->name );
209 /* build the 2-level (type,name) resource tree */
210 static void build_resource_tree(void)
212 int i;
213 struct res_type *type = NULL;
215 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
217 for (i = 0; i < nb_resources; i++)
219 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
220 type = add_type( &resources[i] );
221 type->nb_names++;
225 inline static void put_byte( unsigned char **buffer, unsigned char val )
227 *(*buffer)++ = val;
230 inline static void put_word( unsigned char **buffer, WORD val )
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
241 /* output a string preceded by its length */
242 static void output_string( unsigned char **buffer, const char *str )
244 int len = strlen(str);
245 put_byte( buffer, len );
246 while (len--) put_byte( buffer, *str++ );
249 /* output the resource data */
250 int output_res16_data( FILE *outfile )
252 const struct resource *res;
253 unsigned char *buffer, *p;
254 int i, total;
256 if (!nb_resources) return 0;
258 for (i = total = 0, res = resources; i < nb_resources; i++, res++)
259 total += (res->data_size + ALIGN_MASK) & ~ALIGN_MASK;
261 buffer = p = xmalloc( total );
262 for (i = 0, res = resources; i < nb_resources; i++, res++)
264 memcpy( p, res->data, res->data_size );
265 p += res->data_size;
266 while ((int)p & ALIGN_MASK) *p++ = 0;
268 dump_bytes( outfile, buffer, total, "resource_data", 1 );
269 free( buffer );
270 return total;
273 /* output the resource definitions */
274 int output_res16_directory( unsigned char *buffer )
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;
282 build_resource_tree();
284 offset = 4; /* alignment + terminator */
285 offset += nb_types * 8; /* typeinfo structures */
286 offset += nb_resources * 12; /* nameinfo structures */
288 put_word( &buffer, ALIGNMENT );
290 /* type and name structures */
292 for (i = 0, type = res_types; i < nb_types; i++, type++)
294 if (type->type->str)
296 put_word( &buffer, offset );
297 offset += strlen(type->type->str) + 1;
299 else
300 put_word( &buffer, type->type->id | 0x8000 );
302 put_word( &buffer, type->nb_names );
303 put_word( &buffer, 0 );
304 put_word( &buffer, 0 );
306 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
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)
313 put_word( &buffer, offset );
314 offset += strlen(res->name.str) + 1;
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;
323 put_word( &buffer, 0 ); /* terminator */
325 /* name strings */
327 for (i = 0, type = res_types; i < nb_types; i++, type++)
329 if (type->type->str) output_string( &buffer, type->type->str );
330 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
332 if (res->name.str) output_string( &buffer, res->name.str );
335 put_byte( &buffer, 0 ); /* names terminator */
336 if ((buffer - start) & 1) put_byte( &buffer, 0 ); /* align on word boundary */
338 return buffer - start;