server: Return an error for invalid requests instead of killing the client process.
[wine/multimedia.git] / tools / wrc / newstruc.c
blobc729a594f4fba3992aa1e2e3bc56c74a2e405329
1 /*
2 * Create dynamic new structures of various types
3 * and some utils in that trend.
5 * Copyright 1998 Bertho A. Stultiens
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "wrc.h"
31 #include "newstruc.h"
32 #include "utils.h"
33 #include "parser.h"
35 #include "wingdi.h" /* for BITMAPINFOHEADER */
37 #include <pshpack2.h>
38 typedef struct
40 DWORD biSize;
41 WORD biWidth;
42 WORD biHeight;
43 WORD biPlanes;
44 WORD biBitCount;
45 } BITMAPOS2HEADER;
46 #include <poppack.h>
48 /* Generate new_* functions that have no parameters (NOTE: no ';') */
49 __NEW_STRUCT_FUNC(dialog)
50 __NEW_STRUCT_FUNC(dialogex)
51 __NEW_STRUCT_FUNC(name_id)
52 __NEW_STRUCT_FUNC(menu)
53 __NEW_STRUCT_FUNC(menuex)
54 __NEW_STRUCT_FUNC(menu_item)
55 __NEW_STRUCT_FUNC(menuex_item)
56 __NEW_STRUCT_FUNC(control)
57 __NEW_STRUCT_FUNC(icon)
58 __NEW_STRUCT_FUNC(cursor)
59 __NEW_STRUCT_FUNC(versioninfo)
60 __NEW_STRUCT_FUNC(ver_value)
61 __NEW_STRUCT_FUNC(ver_block)
62 __NEW_STRUCT_FUNC(stt_entry)
63 __NEW_STRUCT_FUNC(accelerator)
64 __NEW_STRUCT_FUNC(event)
65 __NEW_STRUCT_FUNC(raw_data)
66 __NEW_STRUCT_FUNC(lvc)
67 __NEW_STRUCT_FUNC(res_count)
68 __NEW_STRUCT_FUNC(string)
69 __NEW_STRUCT_FUNC(toolbar_item)
70 __NEW_STRUCT_FUNC(ani_any)
72 /* New instances for all types of structures */
73 /* Very inefficient (in size), but very functional :-]
74 * Especially for type-checking.
76 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
78 resource_t *r = (resource_t *)xmalloc(sizeof(resource_t));
79 r->type = t;
80 r->res.overlay = res;
81 r->memopt = memopt;
82 r->lan = lan;
83 return r;
86 version_t *new_version(DWORD v)
88 version_t *vp = (version_t *)xmalloc(sizeof(version_t));
89 *vp = v;
90 return vp;
93 characts_t *new_characts(DWORD c)
95 characts_t *cp = (characts_t *)xmalloc(sizeof(characts_t));
96 *cp = c;
97 return cp;
100 language_t *new_language(int id, int sub)
102 language_t *lan = (language_t *)xmalloc(sizeof(language_t));
103 lan->id = id;
104 lan->sub = sub;
105 return lan;
108 language_t *dup_language(language_t *l)
110 if(!l) return NULL;
111 return new_language(l->id, l->sub);
114 version_t *dup_version(version_t *v)
116 if(!v) return NULL;
117 return new_version(*v);
120 characts_t *dup_characts(characts_t *c)
122 if(!c) return NULL;
123 return new_characts(*c);
126 html_t *new_html(raw_data_t *rd, int *memopt)
128 html_t *html = xmalloc(sizeof(html_t));
129 html->data = rd;
130 if(memopt)
132 html->memopt = *memopt;
133 free(memopt);
135 else
136 html->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
137 return html;
140 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
142 rcdata_t *rc = (rcdata_t *)xmalloc(sizeof(rcdata_t));
143 rc->data = rd;
144 if(memopt)
146 rc->memopt = *memopt;
147 free(memopt);
149 else
150 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
151 return rc;
154 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
156 font_id_t *fid = (font_id_t *)xmalloc(sizeof(font_id_t));
157 fid->name = face;
158 fid->size = size;
159 fid->weight = weight;
160 fid->italic = italic;
161 return fid;
164 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
166 user_t *usr = (user_t *)xmalloc(sizeof(user_t));
167 usr->data = rd;
168 if(memopt)
170 usr->memopt = *memopt;
171 free(memopt);
173 else
174 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
175 usr->type = type;
176 return usr;
179 font_t *new_font(raw_data_t *rd, int *memopt)
181 font_t *fnt = (font_t *)xmalloc(sizeof(font_t));
182 fnt->data = rd;
183 if(memopt)
185 fnt->memopt = *memopt;
186 free(memopt);
188 else
189 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
190 return fnt;
193 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
195 fontdir_t *fnd = (fontdir_t *)xmalloc(sizeof(fontdir_t));
196 fnd->data = rd;
197 if(memopt)
199 fnd->memopt = *memopt;
200 free(memopt);
202 else
203 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
204 return fnd;
209 * Convert bitmaps to proper endian
211 static void convert_bitmap_swap_v3(BITMAPINFOHEADER *bih)
213 bih->biSize = BYTESWAP_DWORD(bih->biSize);
214 bih->biWidth = BYTESWAP_DWORD(bih->biWidth);
215 bih->biHeight = BYTESWAP_DWORD(bih->biHeight);
216 bih->biPlanes = BYTESWAP_WORD(bih->biPlanes);
217 bih->biBitCount = BYTESWAP_WORD(bih->biBitCount);
218 bih->biCompression = BYTESWAP_DWORD(bih->biCompression);
219 bih->biSizeImage = BYTESWAP_DWORD(bih->biSizeImage);
220 bih->biXPelsPerMeter = BYTESWAP_DWORD(bih->biXPelsPerMeter);
221 bih->biYPelsPerMeter = BYTESWAP_DWORD(bih->biYPelsPerMeter);
222 bih->biClrUsed = BYTESWAP_DWORD(bih->biClrUsed);
223 bih->biClrImportant = BYTESWAP_DWORD(bih->biClrImportant);
226 static void convert_bitmap_swap_v4(BITMAPV4HEADER *b4h)
228 convert_bitmap_swap_v3((BITMAPINFOHEADER *)b4h);
229 b4h->bV4RedMask = BYTESWAP_DWORD(b4h->bV4RedMask);
230 b4h->bV4GreenMask = BYTESWAP_DWORD(b4h->bV4GreenMask);
231 b4h->bV4BlueMask = BYTESWAP_DWORD(b4h->bV4BlueMask);
232 b4h->bV4AlphaMask = BYTESWAP_DWORD(b4h->bV4AlphaMask);
233 b4h->bV4CSType = BYTESWAP_DWORD(b4h->bV4CSType);
234 b4h->bV4Endpoints.ciexyzRed.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzX);
235 b4h->bV4Endpoints.ciexyzRed.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzY);
236 b4h->bV4Endpoints.ciexyzRed.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzZ);
237 b4h->bV4Endpoints.ciexyzGreen.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzX);
238 b4h->bV4Endpoints.ciexyzGreen.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzY);
239 b4h->bV4Endpoints.ciexyzGreen.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzZ);
240 b4h->bV4Endpoints.ciexyzBlue.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzX);
241 b4h->bV4Endpoints.ciexyzBlue.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzY);
242 b4h->bV4Endpoints.ciexyzBlue.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzZ);
243 b4h->bV4GammaRed = BYTESWAP_DWORD(b4h->bV4GammaRed);
244 b4h->bV4GammaGreen = BYTESWAP_DWORD(b4h->bV4GammaGreen);
245 b4h->bV4GammaBlue = BYTESWAP_DWORD(b4h->bV4GammaBlue);
248 static void convert_bitmap_swap_os2(BITMAPOS2HEADER *boh)
250 boh->biSize = BYTESWAP_DWORD(boh->biSize);
251 boh->biWidth = BYTESWAP_WORD(boh->biWidth);
252 boh->biHeight = BYTESWAP_WORD(boh->biHeight);
253 boh->biPlanes = BYTESWAP_WORD(boh->biPlanes);
254 boh->biBitCount = BYTESWAP_WORD(boh->biBitCount);
257 #define FL_SIGBE 0x01
258 #define FL_SIZEBE 0x02
259 #define FL_V4 0x04
260 #define FL_OS2 0x08
261 static int convert_bitmap(char *data, int size)
263 BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *)data;
264 BITMAPV4HEADER *b4h = (BITMAPV4HEADER *)data;
265 BITMAPOS2HEADER *boh = (BITMAPOS2HEADER *)data;
266 int type = 0;
267 int returnSize = 0; /* size to be returned */
268 #ifdef WORDS_BIGENDIAN
269 DWORD bisizel = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
270 DWORD b4sizel = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
271 DWORD bosizel = BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER));
272 DWORD bisizeb = sizeof(BITMAPINFOHEADER);
273 DWORD b4sizeb = sizeof(BITMAPV4HEADER);
274 DWORD bosizeb = sizeof(BITMAPOS2HEADER);
275 #else
276 DWORD bisizel = sizeof(BITMAPINFOHEADER);
277 DWORD b4sizel = sizeof(BITMAPV4HEADER);
278 DWORD bosizel = sizeof(BITMAPOS2HEADER);
279 DWORD bisizeb = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
280 DWORD b4sizeb = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
281 DWORD bosizeb = BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER));
282 #endif
286 * Originally the bih and b4h pointers were simply incremented here,
287 * and memmoved at the end of the function. This causes alignment
288 * issues on solaris, so we do the memmove here rather than at the end.
290 if(data[0] == 'B' && data[1] == 'M')
292 /* Little endian signature */
293 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
294 returnSize = sizeof(BITMAPFILEHEADER);
296 else if(data[0] == 'M' && data[1] == 'B')
298 type |= FL_SIGBE; /* Big endian signature */
299 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
300 returnSize = sizeof(BITMAPFILEHEADER);
304 if(bih->biSize == bisizel)
306 /* Little endian */
308 else if(bih->biSize == bisizeb)
310 type |= FL_SIZEBE;
312 else if(bih->biSize == b4sizel)
314 type |= FL_V4;
316 else if(bih->biSize == b4sizeb)
318 type |= FL_SIZEBE | FL_V4;
320 else if(!bih->biSize || bih->biSize == bosizel)
322 type |= FL_OS2;
324 else if(bih->biSize == bosizeb)
326 type |= FL_SIZEBE | FL_OS2;
328 else
329 yyerror("Invalid bitmap format, bih->biSize = %ld", bih->biSize);
331 switch(type)
333 default:
334 break;
335 case FL_SIZEBE:
336 case FL_SIZEBE | FL_V4:
337 case FL_SIZEBE | FL_OS2:
338 yywarning("Bitmap v%c signature little-endian, but size big-endian", type & FL_V4 ? '4' : '3');
339 break;
340 case FL_SIGBE:
341 case FL_SIGBE | FL_V4:
342 case FL_SIGBE | FL_OS2:
343 yywarning("Bitmap v%c signature big-endian, but size little-endian", type & FL_V4 ? '4' : '3');
344 break;
347 switch(byteorder)
349 #ifdef WORDS_BIGENDIAN
350 default:
351 #endif
352 case WRC_BO_BIG:
353 if(!(type & FL_SIZEBE))
355 if(type & FL_V4)
356 convert_bitmap_swap_v4(b4h);
357 else if(type & FL_OS2)
359 convert_bitmap_swap_os2(boh);
361 else
362 convert_bitmap_swap_v3(bih);
364 break;
365 #ifndef WORDS_BIGENDIAN
366 default:
367 #endif
368 case WRC_BO_LITTLE:
369 if(type & FL_SIZEBE)
371 if(type & FL_V4)
372 convert_bitmap_swap_v4(b4h);
373 else if(type & FL_OS2)
375 convert_bitmap_swap_os2(boh);
377 else
378 convert_bitmap_swap_v3(bih);
380 break;
383 if(size && (void *)data != (void *)bih)
385 /* We have the fileheader still attached, remove it */
386 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
387 return sizeof(BITMAPFILEHEADER);
389 return returnSize;
391 #undef FL_SIGBE
392 #undef FL_SIZEBE
393 #undef FL_V4
396 * Cursor and icon splitter functions used when allocating
397 * cursor- and icon-groups.
399 typedef struct {
400 language_t lan;
401 int id;
402 } id_alloc_t;
404 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
406 int i;
407 assert(lan != NULL);
408 assert(list != NULL);
409 assert(n != NULL);
411 if(!*list)
413 *list = (id_alloc_t *)xmalloc(sizeof(id_alloc_t));
414 *n = 1;
415 (*list)[0].lan = *lan;
416 (*list)[0].id = 1;
417 return 1;
420 for(i = 0; i < *n; i++)
422 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
423 return ++((*list)[i].id);
426 *list = (id_alloc_t *)xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
427 (*list)[*n].lan = *lan;
428 (*list)[*n].id = 1;
429 *n += 1;
430 return 1;
433 static int alloc_icon_id(language_t *lan)
435 static id_alloc_t *idlist = NULL;
436 static int nid = 0;
438 return get_new_id(&idlist, &nid, lan);
441 static int alloc_cursor_id(language_t *lan)
443 static id_alloc_t *idlist = NULL;
444 static int nid = 0;
446 return get_new_id(&idlist, &nid, lan);
449 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
451 int cnt;
452 int i;
453 icon_t *ico;
454 icon_t *list = NULL;
455 icon_header_t *ih = (icon_header_t *)rd->data;
456 int swap = 0;
458 if(ih->type == 1)
459 swap = 0;
460 else if(BYTESWAP_WORD(ih->type) == 1)
461 swap = 1;
462 else
463 yyerror("Icon resource data has invalid type id %d", ih->type);
465 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
466 for(i = 0; i < cnt; i++)
468 icon_dir_entry_t ide;
469 BITMAPINFOHEADER info;
470 memcpy(&ide, rd->data + sizeof(icon_header_t)
471 + i*sizeof(icon_dir_entry_t), sizeof(ide));
473 ico = new_icon();
474 ico->id = alloc_icon_id(icog->lvc.language);
475 ico->lvc = icog->lvc;
476 if(swap)
478 ide.offset = BYTESWAP_DWORD(ide.offset);
479 ide.ressize= BYTESWAP_DWORD(ide.ressize);
481 if(ide.offset > rd->size
482 || ide.offset + ide.ressize > rd->size)
483 yyerror("Icon resource data corrupt");
484 ico->width = ide.width;
485 ico->height = ide.height;
486 ico->nclr = ide.nclr;
487 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
488 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
489 memcpy(&info, rd->data + ide.offset, sizeof(info));
490 convert_bitmap((char *) &info, 0);
491 memcpy(rd->data + ide.offset, &info, sizeof(info));
493 if(!ico->planes)
495 /* Argh! They did not fill out the resdir structure */
496 /* The bitmap is in destination byteorder. We want native for our structures */
497 switch(byteorder)
499 #ifdef WORDS_BIGENDIAN
500 case WRC_BO_LITTLE:
501 #else
502 case WRC_BO_BIG:
503 #endif
504 ico->planes = BYTESWAP_WORD(info.biPlanes);
505 break;
506 default:
507 ico->planes = info.biPlanes;
510 if(!ico->bits)
512 /* Argh! They did not fill out the resdir structure */
513 /* The bitmap is in destination byteorder. We want native for our structures */
514 switch(byteorder)
516 #ifdef WORDS_BIGENDIAN
517 case WRC_BO_LITTLE:
518 #else
519 case WRC_BO_BIG:
520 #endif
521 ico->bits = BYTESWAP_WORD(info.biBitCount);
522 break;
523 default:
524 ico->bits = info.biBitCount;
527 ico->data = new_raw_data();
528 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
529 if(!list)
531 list = ico;
533 else
535 ico->next = list;
536 list->prev = ico;
537 list = ico;
540 icog->iconlist = list;
541 *nico = cnt;
544 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
546 int cnt;
547 int i;
548 cursor_t *cur;
549 cursor_t *list = NULL;
550 cursor_header_t *ch = (cursor_header_t *)rd->data;
551 int swap = 0;
553 if(ch->type == 2)
554 swap = 0;
555 else if(BYTESWAP_WORD(ch->type) == 2)
556 swap = 1;
557 else
558 yyerror("Cursor resource data has invalid type id %d", ch->type);
559 cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
560 for(i = 0; i < cnt; i++)
562 cursor_dir_entry_t cde;
563 BITMAPINFOHEADER info;
564 memcpy(&cde, rd->data + sizeof(cursor_header_t)
565 + i*sizeof(cursor_dir_entry_t), sizeof(cde));
567 cur = new_cursor();
568 cur->id = alloc_cursor_id(curg->lvc.language);
569 cur->lvc = curg->lvc;
570 if(swap)
572 cde.offset = BYTESWAP_DWORD(cde.offset);
573 cde.ressize= BYTESWAP_DWORD(cde.ressize);
575 if(cde.offset > rd->size
576 || cde.offset + cde.ressize > rd->size)
577 yyerror("Cursor resource data corrupt");
578 cur->width = cde.width;
579 cur->height = cde.height;
580 cur->nclr = cde.nclr;
581 memcpy(&info, rd->data + cde.offset, sizeof(info));
582 convert_bitmap((char *)&info, 0);
583 memcpy(rd->data + cde.offset, &info, sizeof(info));
584 /* The bitmap is in destination byteorder. We want native for our structures */
585 switch(byteorder)
587 #ifdef WORDS_BIGENDIAN
588 case WRC_BO_LITTLE:
589 #else
590 case WRC_BO_BIG:
591 #endif
592 cur->planes = BYTESWAP_WORD(info.biPlanes);
593 cur->bits = BYTESWAP_WORD(info.biBitCount);
594 break;
595 default:
596 cur->planes = info.biPlanes;
597 cur->bits = info.biBitCount;
599 if(!win32 && (cur->planes != 1 || cur->bits != 1))
600 yywarning("Win16 cursor contains colors");
601 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
602 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
603 cur->data = new_raw_data();
604 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
605 if(!list)
607 list = cur;
609 else
611 cur->next = list;
612 list->prev = cur;
613 list = cur;
616 curg->cursorlist = list;
617 *ncur = cnt;
621 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
623 icon_group_t *icog = (icon_group_t *)xmalloc(sizeof(icon_group_t));
624 if(memopt)
626 icog->memopt = *memopt;
627 free(memopt);
629 else
630 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
631 icog->lvc = rd->lvc;
632 split_icons(rd, icog, &(icog->nicon));
633 free(rd->data);
634 free(rd);
635 return icog;
638 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
640 cursor_group_t *curg = (cursor_group_t *)xmalloc(sizeof(cursor_group_t));
641 if(memopt)
643 curg->memopt = *memopt;
644 free(memopt);
646 else
647 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
648 curg->lvc = rd->lvc;
649 split_cursors(rd, curg, &(curg->ncursor));
650 free(rd->data);
651 free(rd);
652 return curg;
656 * Animated cursors and icons
658 * The format of animated cursors and icons is yet another example
659 * of bad design by "The Company". The entire RIFF structure is
660 * flawed by design because it is inconsistent and single minded:
661 * - some tags have lengths attached, others don't. The use of these
662 * non-length tags is absolutely unclear;
663 * - the content of "icon" tags can be both icons and cursors;
664 * - tags lack proper alignment constraints. It seems that everything
665 * is 16bit aligned, but I could not find that in any docu. Just be
666 * prepared to eat anything;
667 * - there are no strict constraints on tag-nesting and the organization
668 * is highly illogical;
670 * Anyhow, here is the basic structure:
671 * "RIFF" { dword taglength }
672 * "ACON" // What does it do?
673 * "LIST" { dword taglength }
674 * "INFO" // And what does this do?
675 * "INAM" { dword taglength } // Icon/cursor name
676 * {inam data}
677 * "IART" { dword taglength } // The artist
678 * {iart data}
679 * "fram" // Is followed by "icon"s
680 * "icon" { dword taglength } // First frame
681 * { icon/cursor data }
682 * "icon" { dword taglength } // Second frame
683 * { icon/cursor data }
684 * ... // ...
685 * "anih" { dword taglength } // Header structure
686 * { aniheader_t structure }
687 * "rate" { dword taglength } // The rate for each frame
688 * { `steps' dwords }
689 * "seq " { dword taglength } // The frame blit-order
690 * { `steps' dwords }
692 * Tag length are bytelength without the header and length field (i.e. -8).
693 * The "LIST" tag may occur several times and may encapsulate different
694 * tags. The `steps' is the number of "icon" tags found (actually the
695 * number of steps specified in the aniheader_t structure). The "seq "uence
696 * tag can be omitted, in which case the sequence is equal to the sequence
697 * of "icon"s found in the file. Also "rate" may be omitted, in which case
698 * the default from the aniheader_t structure is used.
700 * An animated cursor puts `.cur' formatted files into each "icon" tag,
701 * whereas animated icons contain `.ico' formatted files.
703 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
704 * dealt with in the same code. However, this version shows what is going on
705 * and is better debug-able.
707 static const char riff[4] = "RIFF";
708 static const char acon[4] = "ACON";
709 static const char list[4] = "LIST";
710 static const char info[4] = "INFO";
711 static const char inam[4] = "INAM";
712 static const char iart[4] = "IART";
713 static const char fram[4] = "fram";
714 static const char icon[4] = "icon";
715 static const char anih[4] = "anih";
716 static const char rate[4] = "rate";
717 static const char seq[4] = "seq ";
719 #define SKIP_TAG(p,size) ((riff_tag_t *)(((char *)p) + (size)))
721 #define NEXT_TAG(p) SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
723 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
725 cursor_dir_entry_t *cdp;
726 cursor_header_t *chp;
727 int count;
728 int ctype;
729 int i;
730 static int once = 0; /* This will trigger only once per file! */
731 const char *anistr = type == res_aniico ? "icon" : "cursor";
732 /* Notes:
733 * Both cursor and icon directories are similar
734 * Both cursor and icon headers are similar
737 chp = (cursor_header_t *)(rtp+1);
738 cdp = (cursor_dir_entry_t *)(chp+1);
739 count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
740 ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
741 chp->reserved = BYTESWAP_WORD(chp->reserved);
742 chp->type = BYTESWAP_WORD(chp->type);
743 chp->count = BYTESWAP_WORD(chp->count);
745 if(type == res_anicur && ctype != 2 && !once)
747 yywarning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)",
748 ctype,
749 ctype == 1 ? "icontype" : "?");
750 once++;
752 else if(type == res_aniico && ctype != 1 && !once)
754 yywarning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)",
755 ctype,
756 ctype == 2 ? "cursortype" : "?");
757 once++;
759 else if(ctype != 1 && ctype != 2 && !once)
761 yywarning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)", anistr, ctype);
762 once++;
765 for(i = 0; i < count; i++)
767 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
768 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
769 if(ofs > rtp->size || ofs+sze > rtp->size)
770 yyerror("Animated %s's data corrupt", anistr);
771 convert_bitmap((char *)chp + ofs, 0);
772 cdp[i].xhot = BYTESWAP_WORD(cdp->xhot);
773 cdp[i].yhot = BYTESWAP_WORD(cdp->yhot);
774 cdp[i].ressize = BYTESWAP_DWORD(cdp->ressize);
775 cdp[i].offset = BYTESWAP_DWORD(cdp->offset);
779 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
781 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
783 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
785 if(!memcmp(rtp->tag, info, sizeof(info)))
787 rtp = SKIP_TAG(rtp,4);
789 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
791 /* Ignore the icon/cursor name; its a string */
792 rtp = NEXT_TAG(rtp);
794 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
796 /* Ignore the author's name; its a string */
797 rtp = NEXT_TAG(rtp);
799 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
801 /* This should be followed by "icon"s, but we
802 * simply ignore this because it is pure
803 * non-information.
805 rtp = SKIP_TAG(rtp,4);
807 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
809 handle_ani_icon(rtp, type, isswapped);
810 rtp = NEXT_TAG(rtp);
812 else
813 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
814 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
815 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
816 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
817 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
819 if((UINT_PTR)rtp & 1)
820 rtp = SKIP_TAG(rtp,1);
824 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
826 ani_curico_t *ani = (ani_curico_t *)xmalloc(sizeof(ani_curico_t));
827 riff_tag_t *rtp;
828 int isswapped = 0;
829 int doswap;
830 const char *anistr = type == res_aniico ? "icon" : "cursor";
832 assert(!memcmp(rd->data, riff, sizeof(riff)));
833 assert(type == res_anicur || type == res_aniico);
835 rtp = (riff_tag_t *)rd->data;
837 if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
838 isswapped = 1;
839 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
840 isswapped = 0;
841 else
842 yyerror("Animated %s has an invalid RIFF length", anistr);
844 switch(byteorder)
846 #ifdef WORDS_BIGENDIAN
847 case WRC_BO_LITTLE:
848 #else
849 case WRC_BO_BIG:
850 #endif
851 doswap = !isswapped;
852 break;
853 default:
854 doswap = isswapped;
858 * When to swap what:
859 * isswapped | doswap |
860 * ----------+--------+---------------------------------
861 * 0 | 0 | read native; don't convert
862 * 1 | 0 | read swapped size; don't convert
863 * 0 | 1 | read native; convert
864 * 1 | 1 | read swapped size; convert
865 * Reading swapped size if necessary to calculate in native
866 * format. E.g. a little-endian source on a big-endian
867 * processor.
869 if(doswap)
871 /* We only go through the RIFF file if we need to swap
872 * bytes in words/dwords. Else we couldn't care less
873 * what the file contains. This is consistent with
874 * MS' rc.exe, which doesn't complain at all, even though
875 * the file format might not be entirely correct.
877 rtp++; /* Skip the "RIFF" tag */
879 while((char *)rtp < (char *)rd->data + rd->size)
881 if(!memcmp(rtp->tag, acon, sizeof(acon)))
883 rtp = SKIP_TAG(rtp,4);
885 else if(!memcmp(rtp->tag, list, sizeof(list)))
887 handle_ani_list(rtp, type, isswapped);
888 rtp = NEXT_TAG(rtp);
890 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
892 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
893 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
894 ahp->frames = BYTESWAP_DWORD(ahp->frames);
895 ahp->steps = BYTESWAP_DWORD(ahp->steps);
896 ahp->cx = BYTESWAP_DWORD(ahp->cx);
897 ahp->cy = BYTESWAP_DWORD(ahp->cy);
898 ahp->bitcount = BYTESWAP_DWORD(ahp->bitcount);
899 ahp->planes = BYTESWAP_DWORD(ahp->planes);
900 ahp->rate = BYTESWAP_DWORD(ahp->rate);
901 ahp->flags = BYTESWAP_DWORD(ahp->flags);
902 rtp = NEXT_TAG(rtp);
904 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
906 int cnt = rtp->size / sizeof(DWORD);
907 DWORD *dwp = (DWORD *)(rtp+1);
908 int i;
909 for(i = 0; i < cnt; i++)
910 dwp[i] = BYTESWAP_DWORD(dwp[i]);
911 rtp = NEXT_TAG(rtp);
913 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
915 int cnt = rtp->size / sizeof(DWORD);
916 DWORD *dwp = (DWORD *)(rtp+1);
917 int i;
918 for(i = 0; i < cnt; i++)
919 dwp[i] = BYTESWAP_DWORD(dwp[i]);
920 rtp = NEXT_TAG(rtp);
922 else
923 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
924 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
925 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
926 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
927 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
929 if((UINT_PTR)rtp & 1)
930 rtp = SKIP_TAG(rtp,1);
933 /* We must end correctly here */
934 if((char *)rtp != (char *)rd->data + rd->size)
935 yyerror("Animated %s contains invalid field size(s)", anistr);
938 ani->data = rd;
939 if(memopt)
941 ani->memopt = *memopt;
942 free(memopt);
944 else
945 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
946 return ani;
948 #undef NEXT_TAG
950 /* Bitmaps */
951 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
953 bitmap_t *bmp = (bitmap_t *)xmalloc(sizeof(bitmap_t));
955 bmp->data = rd;
956 if(memopt)
958 bmp->memopt = *memopt;
959 free(memopt);
961 else
962 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
963 rd->size -= convert_bitmap(rd->data, rd->size);
964 return bmp;
967 ver_words_t *new_ver_words(int i)
969 ver_words_t *w = (ver_words_t *)xmalloc(sizeof(ver_words_t));
970 w->words = (WORD *)xmalloc(sizeof(WORD));
971 w->words[0] = (WORD)i;
972 w->nwords = 1;
973 return w;
976 ver_words_t *add_ver_words(ver_words_t *w, int i)
978 w->words = (WORD *)xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
979 w->words[w->nwords] = (WORD)i;
980 w->nwords++;
981 return w;
984 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
985 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
987 messagetable_t *msg = (messagetable_t *)xmalloc(sizeof(messagetable_t));
988 msgtab_block_t *mbp;
989 DWORD nblk;
990 DWORD i;
991 WORD lo;
992 WORD hi;
994 msg->data = rd;
995 if(memopt)
997 msg->memopt = *memopt;
998 free(memopt);
1000 else
1001 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1003 if(rd->size < sizeof(DWORD))
1004 yyerror("Invalid messagetable, size too small");
1006 nblk = *(DWORD *)rd->data;
1007 lo = WRC_LOWORD(nblk);
1008 hi = WRC_HIWORD(nblk);
1010 /* FIXME:
1011 * This test will fail for all n*2^16 blocks in the messagetable.
1012 * However, no sane person would want to have so many blocks
1013 * and have a table of megabytes attached.
1014 * So, I will assume that we have less than 2^16 blocks in the table
1015 * and all will just work out fine. Otherwise, we would need to test
1016 * the ID, offset and length (and flag) fields to be very sure.
1018 if(hi && lo)
1019 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian");
1020 if(!hi && !lo)
1021 yyerror("Invalid messagetable block count 0");
1023 if(!hi && lo) /* Messagetable byteorder == native byteorder */
1025 #ifdef WORDS_BIGENDIAN
1026 if(byteorder != WRC_BO_LITTLE) goto out;
1027 #else
1028 if(byteorder != WRC_BO_BIG) goto out;
1029 #endif
1030 /* Resource byteorder != native byteorder */
1032 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1033 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1034 yyerror("Messagetable's blocks are outside of defined data");
1035 for(i = 0; i < nblk; i++)
1037 msgtab_entry_t *mep, *next_mep;
1038 DWORD id;
1040 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1042 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1044 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1045 yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
1046 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1048 WORD *wp = (WORD *)&mep[1];
1049 int l = mep->length/2 - 2; /* Length included header */
1050 int n;
1052 if(mep->length & 1)
1053 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
1054 for(n = 0; n < l; n++)
1055 wp[n] = BYTESWAP_WORD(wp[n]);
1058 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1059 mep->length = BYTESWAP_WORD(mep->length);
1060 mep->flags = BYTESWAP_WORD(mep->flags);
1061 mep = next_mep;
1064 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1065 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1066 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1069 if(hi && !lo) /* Messagetable byteorder != native byteorder */
1071 #ifdef WORDS_BIGENDIAN
1072 if(byteorder == WRC_BO_LITTLE) goto out;
1073 #else
1074 if(byteorder == WRC_BO_BIG) goto out;
1075 #endif
1076 /* Resource byteorder == native byteorder */
1078 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1079 nblk = BYTESWAP_DWORD(nblk);
1080 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1081 yyerror("Messagetable's blocks are outside of defined data");
1082 for(i = 0; i < nblk; i++)
1084 msgtab_entry_t *mep;
1085 DWORD id;
1087 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1088 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1089 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1090 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1092 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1094 mep->length = BYTESWAP_WORD(mep->length);
1095 mep->flags = BYTESWAP_WORD(mep->flags);
1097 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1098 yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
1099 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1101 WORD *wp = (WORD *)&mep[1];
1102 int l = mep->length/2 - 2; /* Length included header */
1103 int n;
1105 if(mep->length & 1)
1106 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
1107 for(n = 0; n < l; n++)
1108 wp[n] = BYTESWAP_WORD(wp[n]);
1111 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1116 out:
1117 return msg;
1119 #undef MSGTAB_BAD_PTR
1121 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1123 assert(offs <= src->size);
1124 assert(offs + len <= src->size);
1125 if(!dst->data)
1127 dst->data = (char *)xmalloc(len);
1128 dst->size = 0;
1130 else
1131 dst->data = (char *)xrealloc(dst->data, dst->size + len);
1132 /* dst->size holds the offset to copy to */
1133 memcpy(dst->data + dst->size, src->data + offs, len);
1134 dst->size += len;
1137 int *new_int(int i)
1139 int *ip = (int *)xmalloc(sizeof(int));
1140 *ip = i;
1141 return ip;
1144 stringtable_t *new_stringtable(lvc_t *lvc)
1146 stringtable_t *stt = (stringtable_t *)xmalloc(sizeof(stringtable_t));
1148 if(lvc)
1149 stt->lvc = *lvc;
1151 return stt;
1154 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1156 toolbar_t *tb = (toolbar_t *)xmalloc(sizeof(toolbar_t));
1157 tb->button_width = button_width;
1158 tb->button_height = button_height;
1159 tb->nitems = nitems;
1160 tb->items = items;
1161 return tb;
1164 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1166 dlginit_t *di = (dlginit_t *)xmalloc(sizeof(dlginit_t));
1167 di->data = rd;
1168 if(memopt)
1170 di->memopt = *memopt;
1171 free(memopt);
1173 else
1174 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1176 return di;
1179 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1181 style_pair_t *sp = (style_pair_t *)xmalloc(sizeof(style_pair_t));
1182 sp->style = style;
1183 sp->exstyle = exstyle;
1184 return sp;
1187 style_t *new_style(DWORD or_mask, DWORD and_mask)
1189 style_t *st = (style_t *)xmalloc(sizeof(style_t));
1190 st->or_mask = or_mask;
1191 st->and_mask = and_mask;
1192 return st;