zlib: Import upstream release 1.2.12.
[wine.git] / tools / winebuild / res16.c
blob732af0151990ea339003c819f97b24823e77e7ee
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "build.h"
32 /* Unicode string or integer id */
33 struct string_id
35 char *str; /* ptr to string */
36 unsigned short id; /* integer id if str is NULL */
39 /* descriptor for a resource */
40 struct resource
42 struct string_id type;
43 struct string_id name;
44 const void *data;
45 unsigned int name_offset;
46 unsigned int data_size;
47 unsigned int memopt;
50 /* type level of the resource tree */
51 struct res_type
53 const struct string_id *type; /* type name */
54 struct resource *res; /* first resource of this type */
55 unsigned int name_offset; /* name offset if string */
56 unsigned int nb_names; /* total number of names */
59 /* top level of the resource tree */
60 struct res_tree
62 struct res_type *types; /* types array */
63 unsigned int nb_types; /* total number of types */
67 static inline struct resource *add_resource( DLLSPEC *spec )
69 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
70 return &spec->resources[spec->nb_resources++];
73 static struct res_type *add_type( struct res_tree *tree, struct resource *res )
75 struct res_type *type;
76 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
77 type = &tree->types[tree->nb_types++];
78 type->type = &res->type;
79 type->res = res;
80 type->nb_names = 0;
81 return type;
84 /* get a string from the current resource file */
85 static void get_string( struct string_id *str )
87 unsigned char c = get_byte();
89 if (c == 0xff)
91 str->str = NULL;
92 str->id = get_word();
94 else
96 str->str = (char *)input_buffer + input_buffer_pos - 1;
97 str->id = 0;
98 while (get_byte()) /* nothing */;
102 /* load the next resource from the current file */
103 static void load_next_resource( DLLSPEC *spec )
105 struct resource *res = add_resource( spec );
107 get_string( &res->type );
108 get_string( &res->name );
109 res->memopt = get_word();
110 res->data_size = get_dword();
111 res->data = input_buffer + input_buffer_pos;
112 input_buffer_pos += res->data_size;
113 if (input_buffer_pos > input_buffer_size)
114 fatal_error( "%s is a truncated/corrupted file\n", input_buffer_filename );
117 /* load a Win16 .res file */
118 void load_res16_file( const char *name, DLLSPEC *spec )
120 init_input_buffer( name );
121 while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
124 /* compare two strings/ids */
125 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
127 if (!str1->str)
129 if (!str2->str) return str1->id - str2->id;
130 return 1; /* an id compares larger than a string */
132 if (!str2->str) return -1;
133 return strcasecmp( str1->str, str2->str );
136 /* compare two resources for sorting the resource directory */
137 /* resources are stored first by type, then by name */
138 static int cmp_res( const void *ptr1, const void *ptr2 )
140 const struct resource *res1 = ptr1;
141 const struct resource *res2 = ptr2;
142 int ret;
144 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
145 return cmp_string( &res1->name, &res2->name );
148 /* build the 2-level (type,name) resource tree */
149 static struct res_tree *build_resource_tree( DLLSPEC *spec )
151 unsigned int i, j, offset;
152 struct res_tree *tree;
153 struct res_type *type = NULL;
154 struct resource *res;
156 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
158 offset = 2; /* alignment */
159 tree = xmalloc( sizeof(*tree) );
160 tree->types = NULL;
161 tree->nb_types = 0;
163 for (i = 0; i < spec->nb_resources; i++)
165 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
167 type = add_type( tree, &spec->resources[i] );
168 offset += 8;
170 type->nb_names++;
171 offset += 12;
173 offset += 2; /* terminator */
175 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
177 if (type->type->str)
179 type->name_offset = offset;
180 offset += strlen(type->type->str) + 1;
182 else type->name_offset = type->type->id | 0x8000;
184 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
186 if (res->name.str)
188 res->name_offset = offset;
189 offset += strlen(res->name.str) + 1;
191 else res->name_offset = res->name.id | 0x8000;
194 return tree;
197 /* free the resource tree */
198 static void free_resource_tree( struct res_tree *tree )
200 free( tree->types );
201 free( tree );
204 /* output a string preceded by its length */
205 static void output_string( const char *str )
207 unsigned int i, len = strlen(str);
208 output( "\t.byte 0x%02x", len );
209 for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)str[i] );
210 output( " /* %s */\n", str );
213 /* output a string preceded by its length in binary format*/
214 static void output_bin_string( const char *str )
216 put_byte( strlen(str) );
217 while (*str) put_byte( *str++ );
220 /* output the resource data */
221 void output_res16_data( DLLSPEC *spec )
223 const struct resource *res;
224 unsigned int i;
226 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
228 output( ".L__wine_spec_resource_%u:\n", i );
229 dump_bytes( res->data, res->data_size );
233 /* output the resource definitions */
234 void output_res16_directory( DLLSPEC *spec )
236 unsigned int i, j;
237 struct res_tree *tree;
238 const struct res_type *type;
239 const struct resource *res;
241 tree = build_resource_tree( spec );
243 output( "\n.L__wine_spec_ne_rsrctab:\n" );
244 output( "\t.short 0\n" ); /* alignment */
246 /* type and name structures */
248 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
250 output( "\t.short 0x%04x,%u,0,0\n", type->name_offset, type->nb_names );
252 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
254 output( "\t.short .L__wine_spec_resource_%lu-.L__wine_spec_dos_header,%u\n",
255 (unsigned long)(res - spec->resources), res->data_size );
256 output( "\t.short 0x%04x,0x%04x,0,0\n", res->memopt, res->name_offset );
259 output( "\t.short 0\n" ); /* terminator */
261 /* name strings */
263 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
265 if (type->type->str) output_string( type->type->str );
266 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
267 if (res->name.str) output_string( res->name.str );
269 output( "\t.byte 0\n" ); /* names terminator */
271 free_resource_tree( tree );
274 /* output the resource data in binary format */
275 void output_bin_res16_data( DLLSPEC *spec )
277 const struct resource *res;
278 unsigned int i;
280 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
281 put_data( res->data, res->data_size );
284 /* output the resource definitions in binary format */
285 void output_bin_res16_directory( DLLSPEC *spec, unsigned int data_offset )
287 unsigned int i, j;
288 struct res_tree *tree;
289 const struct res_type *type;
290 const struct resource *res;
292 tree = build_resource_tree( spec );
294 put_word( 0 ); /* alignment */
296 /* type and name structures */
298 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
300 put_word( type->name_offset );
301 put_word( type->nb_names );
302 put_word( 0 );
303 put_word( 0 );
305 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
307 put_word( data_offset );
308 put_word( res->data_size );
309 put_word( res->memopt );
310 put_word( res->name_offset );
311 put_word( 0 );
312 put_word( 0 );
313 data_offset += res->data_size;
316 put_word( 0 ); /* terminator */
318 /* name strings */
320 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
322 if (type->type->str) output_bin_string( type->type->str );
323 for (j = 0, res = type->res; j < type->nb_names; j++, res++)
324 if (res->name.str) output_bin_string( res->name.str );
326 put_byte( 0 ); /* names terminator */
328 free_resource_tree( tree );