quartz: Don't cast WSTR to BSTR, convert properly instead.
[wine/testsucceed.git] / tools / wrc / newstruc.c
blob6ed49dca1e2cc0e1e72e15594573146b1f201eaf
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(name_id)
51 __NEW_STRUCT_FUNC(menu)
52 __NEW_STRUCT_FUNC(menu_item)
53 __NEW_STRUCT_FUNC(control)
54 __NEW_STRUCT_FUNC(icon)
55 __NEW_STRUCT_FUNC(cursor)
56 __NEW_STRUCT_FUNC(versioninfo)
57 __NEW_STRUCT_FUNC(ver_value)
58 __NEW_STRUCT_FUNC(ver_block)
59 __NEW_STRUCT_FUNC(stt_entry)
60 __NEW_STRUCT_FUNC(accelerator)
61 __NEW_STRUCT_FUNC(event)
62 __NEW_STRUCT_FUNC(raw_data)
63 __NEW_STRUCT_FUNC(lvc)
64 __NEW_STRUCT_FUNC(res_count)
65 __NEW_STRUCT_FUNC(string)
66 __NEW_STRUCT_FUNC(toolbar_item)
67 __NEW_STRUCT_FUNC(ani_any)
69 /* New instances for all types of structures */
70 /* Very inefficient (in size), but very functional :-]
71 * Especially for type-checking.
73 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
75 resource_t *r = xmalloc(sizeof(resource_t));
76 memset( r, 0, sizeof(*r) );
77 r->type = t;
78 r->res.overlay = res;
79 r->memopt = memopt;
80 r->lan = lan;
81 return r;
84 version_t *new_version(DWORD v)
86 version_t *vp = xmalloc(sizeof(version_t));
87 *vp = v;
88 return vp;
91 characts_t *new_characts(DWORD c)
93 characts_t *cp = xmalloc(sizeof(characts_t));
94 *cp = c;
95 return cp;
98 language_t *new_language(int id, int sub)
100 language_t *lan = xmalloc(sizeof(language_t));
101 lan->id = id;
102 lan->sub = sub;
103 return lan;
106 language_t *dup_language(language_t *l)
108 if(!l) return NULL;
109 return new_language(l->id, l->sub);
112 version_t *dup_version(version_t *v)
114 if(!v) return NULL;
115 return new_version(*v);
118 characts_t *dup_characts(characts_t *c)
120 if(!c) return NULL;
121 return new_characts(*c);
124 html_t *new_html(raw_data_t *rd, int *memopt)
126 html_t *html = xmalloc(sizeof(html_t));
127 html->data = rd;
128 if(memopt)
130 html->memopt = *memopt;
131 free(memopt);
133 else
134 html->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
135 return html;
138 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
140 rcdata_t *rc = xmalloc(sizeof(rcdata_t));
141 rc->data = rd;
142 if(memopt)
144 rc->memopt = *memopt;
145 free(memopt);
147 else
148 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
149 return rc;
152 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
154 font_id_t *fid = xmalloc(sizeof(font_id_t));
155 fid->name = face;
156 fid->size = size;
157 fid->weight = weight;
158 fid->italic = italic;
159 return fid;
162 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
164 user_t *usr = xmalloc(sizeof(user_t));
165 usr->data = rd;
166 if(memopt)
168 usr->memopt = *memopt;
169 free(memopt);
171 else
172 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
173 usr->type = type;
174 return usr;
177 font_t *new_font(raw_data_t *rd, int *memopt)
179 font_t *fnt = xmalloc(sizeof(font_t));
180 fnt->data = rd;
181 if(memopt)
183 fnt->memopt = *memopt;
184 free(memopt);
186 else
187 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
188 return fnt;
191 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
193 fontdir_t *fnd = xmalloc(sizeof(fontdir_t));
194 fnd->data = rd;
195 if(memopt)
197 fnd->memopt = *memopt;
198 free(memopt);
200 else
201 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
202 return fnd;
207 * Convert bitmaps to proper endian
209 static void convert_bitmap_swap_v3(BITMAPINFOHEADER *bih)
211 bih->biSize = BYTESWAP_DWORD(bih->biSize);
212 bih->biWidth = BYTESWAP_DWORD(bih->biWidth);
213 bih->biHeight = BYTESWAP_DWORD(bih->biHeight);
214 bih->biPlanes = BYTESWAP_WORD(bih->biPlanes);
215 bih->biBitCount = BYTESWAP_WORD(bih->biBitCount);
216 bih->biCompression = BYTESWAP_DWORD(bih->biCompression);
217 bih->biSizeImage = BYTESWAP_DWORD(bih->biSizeImage);
218 bih->biXPelsPerMeter = BYTESWAP_DWORD(bih->biXPelsPerMeter);
219 bih->biYPelsPerMeter = BYTESWAP_DWORD(bih->biYPelsPerMeter);
220 bih->biClrUsed = BYTESWAP_DWORD(bih->biClrUsed);
221 bih->biClrImportant = BYTESWAP_DWORD(bih->biClrImportant);
224 static void convert_bitmap_swap_v4(BITMAPV4HEADER *b4h)
226 convert_bitmap_swap_v3((BITMAPINFOHEADER *)b4h);
227 b4h->bV4RedMask = BYTESWAP_DWORD(b4h->bV4RedMask);
228 b4h->bV4GreenMask = BYTESWAP_DWORD(b4h->bV4GreenMask);
229 b4h->bV4BlueMask = BYTESWAP_DWORD(b4h->bV4BlueMask);
230 b4h->bV4AlphaMask = BYTESWAP_DWORD(b4h->bV4AlphaMask);
231 b4h->bV4CSType = BYTESWAP_DWORD(b4h->bV4CSType);
232 b4h->bV4Endpoints.ciexyzRed.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzX);
233 b4h->bV4Endpoints.ciexyzRed.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzY);
234 b4h->bV4Endpoints.ciexyzRed.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzRed.ciexyzZ);
235 b4h->bV4Endpoints.ciexyzGreen.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzX);
236 b4h->bV4Endpoints.ciexyzGreen.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzY);
237 b4h->bV4Endpoints.ciexyzGreen.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzGreen.ciexyzZ);
238 b4h->bV4Endpoints.ciexyzBlue.ciexyzX = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzX);
239 b4h->bV4Endpoints.ciexyzBlue.ciexyzY = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzY);
240 b4h->bV4Endpoints.ciexyzBlue.ciexyzZ = BYTESWAP_DWORD(b4h->bV4Endpoints.ciexyzBlue.ciexyzZ);
241 b4h->bV4GammaRed = BYTESWAP_DWORD(b4h->bV4GammaRed);
242 b4h->bV4GammaGreen = BYTESWAP_DWORD(b4h->bV4GammaGreen);
243 b4h->bV4GammaBlue = BYTESWAP_DWORD(b4h->bV4GammaBlue);
246 static void convert_bitmap_swap_os2(BITMAPOS2HEADER *boh)
248 boh->biSize = BYTESWAP_DWORD(boh->biSize);
249 boh->biWidth = BYTESWAP_WORD(boh->biWidth);
250 boh->biHeight = BYTESWAP_WORD(boh->biHeight);
251 boh->biPlanes = BYTESWAP_WORD(boh->biPlanes);
252 boh->biBitCount = BYTESWAP_WORD(boh->biBitCount);
255 #define FL_SIGBE 0x01
256 #define FL_SIZEBE 0x02
257 #define FL_V4 0x04
258 #define FL_OS2 0x08
259 static int convert_bitmap(char *data, int size)
261 BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *)data;
262 BITMAPV4HEADER *b4h = (BITMAPV4HEADER *)data;
263 BITMAPOS2HEADER *boh = (BITMAPOS2HEADER *)data;
264 int type = 0;
265 int returnSize = 0; /* size to be returned */
266 #ifdef WORDS_BIGENDIAN
267 DWORD bisizel = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
268 DWORD b4sizel = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
269 DWORD bosizel = BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER));
270 DWORD bisizeb = sizeof(BITMAPINFOHEADER);
271 DWORD b4sizeb = sizeof(BITMAPV4HEADER);
272 DWORD bosizeb = sizeof(BITMAPOS2HEADER);
273 #else
274 DWORD bisizel = sizeof(BITMAPINFOHEADER);
275 DWORD b4sizel = sizeof(BITMAPV4HEADER);
276 DWORD bosizel = sizeof(BITMAPOS2HEADER);
277 DWORD bisizeb = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
278 DWORD b4sizeb = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
279 DWORD bosizeb = BYTESWAP_DWORD(sizeof(BITMAPOS2HEADER));
280 #endif
284 * Originally the bih and b4h pointers were simply incremented here,
285 * and memmoved at the end of the function. This causes alignment
286 * issues on solaris, so we do the memmove here rather than at the end.
288 if(data[0] == 'B' && data[1] == 'M')
290 /* Little endian signature */
291 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
292 returnSize = sizeof(BITMAPFILEHEADER);
294 else if(data[0] == 'M' && data[1] == 'B')
296 type |= FL_SIGBE; /* Big endian signature */
297 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
298 returnSize = sizeof(BITMAPFILEHEADER);
302 if(bih->biSize == bisizel)
304 /* Little endian */
306 else if(bih->biSize == bisizeb)
308 type |= FL_SIZEBE;
310 else if(bih->biSize == b4sizel)
312 type |= FL_V4;
314 else if(bih->biSize == b4sizeb)
316 type |= FL_SIZEBE | FL_V4;
318 else if(!bih->biSize || bih->biSize == bosizel)
320 type |= FL_OS2;
322 else if(bih->biSize == bosizeb)
324 type |= FL_SIZEBE | FL_OS2;
326 else
327 parser_error("Invalid bitmap format, bih->biSize = %d", bih->biSize);
329 switch(type)
331 default:
332 break;
333 case FL_SIZEBE:
334 case FL_SIZEBE | FL_V4:
335 case FL_SIZEBE | FL_OS2:
336 parser_warning("Bitmap v%c signature little-endian, but size big-endian\n", type & FL_V4 ? '4' : '3');
337 break;
338 case FL_SIGBE:
339 case FL_SIGBE | FL_V4:
340 case FL_SIGBE | FL_OS2:
341 parser_warning("Bitmap v%c signature big-endian, but size little-endian\n", type & FL_V4 ? '4' : '3');
342 break;
345 switch(byteorder)
347 #ifdef WORDS_BIGENDIAN
348 default:
349 #endif
350 case WRC_BO_BIG:
351 if(!(type & FL_SIZEBE))
353 if(type & FL_V4)
354 convert_bitmap_swap_v4(b4h);
355 else if(type & FL_OS2)
357 convert_bitmap_swap_os2(boh);
359 else
360 convert_bitmap_swap_v3(bih);
362 break;
363 #ifndef WORDS_BIGENDIAN
364 default:
365 #endif
366 case WRC_BO_LITTLE:
367 if(type & FL_SIZEBE)
369 if(type & FL_V4)
370 convert_bitmap_swap_v4(b4h);
371 else if(type & FL_OS2)
373 convert_bitmap_swap_os2(boh);
375 else
376 convert_bitmap_swap_v3(bih);
378 break;
381 if(size && (void *)data != (void *)bih)
383 /* We have the fileheader still attached, remove it */
384 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
385 return sizeof(BITMAPFILEHEADER);
387 return returnSize;
389 #undef FL_SIGBE
390 #undef FL_SIZEBE
391 #undef FL_V4
394 * Cursor and icon splitter functions used when allocating
395 * cursor- and icon-groups.
397 typedef struct {
398 language_t lan;
399 int id;
400 } id_alloc_t;
402 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
404 int i;
405 assert(lan != NULL);
406 assert(list != NULL);
407 assert(n != NULL);
409 if(!*list)
411 *list = xmalloc(sizeof(id_alloc_t));
412 *n = 1;
413 (*list)[0].lan = *lan;
414 (*list)[0].id = 1;
415 return 1;
418 for(i = 0; i < *n; i++)
420 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
421 return ++((*list)[i].id);
424 *list = xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
425 (*list)[*n].lan = *lan;
426 (*list)[*n].id = 1;
427 *n += 1;
428 return 1;
431 static int alloc_icon_id(language_t *lan)
433 static id_alloc_t *idlist = NULL;
434 static int nid = 0;
436 return get_new_id(&idlist, &nid, lan);
439 static int alloc_cursor_id(language_t *lan)
441 static id_alloc_t *idlist = NULL;
442 static int nid = 0;
444 return get_new_id(&idlist, &nid, lan);
447 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
449 int cnt;
450 int i;
451 icon_t *ico;
452 icon_t *list = NULL;
453 icon_header_t *ih = (icon_header_t *)rd->data;
454 int swap = 0;
456 if(ih->type == 1)
457 swap = 0;
458 else if(BYTESWAP_WORD(ih->type) == 1)
459 swap = 1;
460 else
461 parser_error("Icon resource data has invalid type id %d", ih->type);
463 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
464 for(i = 0; i < cnt; i++)
466 icon_dir_entry_t ide;
467 BITMAPINFOHEADER info;
468 memcpy(&ide, rd->data + sizeof(icon_header_t)
469 + i*sizeof(icon_dir_entry_t), sizeof(ide));
471 ico = new_icon();
472 ico->id = alloc_icon_id(icog->lvc.language);
473 ico->lvc = icog->lvc;
474 if(swap)
476 ide.offset = BYTESWAP_DWORD(ide.offset);
477 ide.ressize= BYTESWAP_DWORD(ide.ressize);
479 if(ide.offset > rd->size
480 || ide.offset + ide.ressize > rd->size)
481 parser_error("Icon resource data corrupt");
482 ico->width = ide.width;
483 ico->height = ide.height;
484 ico->nclr = ide.nclr;
485 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
486 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
487 memcpy(&info, rd->data + ide.offset, sizeof(info));
488 convert_bitmap((char *) &info, 0);
489 memcpy(rd->data + ide.offset, &info, sizeof(info));
491 if(!ico->planes)
493 /* Argh! They did not fill out the resdir structure */
494 /* The bitmap is in destination byteorder. We want native for our structures */
495 switch(byteorder)
497 #ifdef WORDS_BIGENDIAN
498 case WRC_BO_LITTLE:
499 #else
500 case WRC_BO_BIG:
501 #endif
502 ico->planes = BYTESWAP_WORD(info.biPlanes);
503 break;
504 default:
505 ico->planes = info.biPlanes;
508 if(!ico->bits)
510 /* Argh! They did not fill out the resdir structure */
511 /* The bitmap is in destination byteorder. We want native for our structures */
512 switch(byteorder)
514 #ifdef WORDS_BIGENDIAN
515 case WRC_BO_LITTLE:
516 #else
517 case WRC_BO_BIG:
518 #endif
519 ico->bits = BYTESWAP_WORD(info.biBitCount);
520 break;
521 default:
522 ico->bits = info.biBitCount;
525 ico->data = new_raw_data();
526 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
527 if(!list)
529 list = ico;
531 else
533 ico->next = list;
534 list->prev = ico;
535 list = ico;
538 icog->iconlist = list;
539 *nico = cnt;
542 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
544 int cnt;
545 int i;
546 cursor_t *cur;
547 cursor_t *list = NULL;
548 cursor_header_t *ch = (cursor_header_t *)rd->data;
549 int swap = 0;
551 if(ch->type == 2)
552 swap = 0;
553 else if(BYTESWAP_WORD(ch->type) == 2)
554 swap = 1;
555 else
556 parser_error("Cursor resource data has invalid type id %d", ch->type);
557 cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
558 for(i = 0; i < cnt; i++)
560 cursor_dir_entry_t cde;
561 BITMAPINFOHEADER info;
562 memcpy(&cde, rd->data + sizeof(cursor_header_t)
563 + i*sizeof(cursor_dir_entry_t), sizeof(cde));
565 cur = new_cursor();
566 cur->id = alloc_cursor_id(curg->lvc.language);
567 cur->lvc = curg->lvc;
568 if(swap)
570 cde.offset = BYTESWAP_DWORD(cde.offset);
571 cde.ressize= BYTESWAP_DWORD(cde.ressize);
573 if(cde.offset > rd->size
574 || cde.offset + cde.ressize > rd->size)
575 parser_error("Cursor resource data corrupt");
576 cur->width = cde.width;
577 cur->height = cde.height;
578 cur->nclr = cde.nclr;
579 memcpy(&info, rd->data + cde.offset, sizeof(info));
580 convert_bitmap((char *)&info, 0);
581 memcpy(rd->data + cde.offset, &info, sizeof(info));
582 /* The bitmap is in destination byteorder. We want native for our structures */
583 switch(byteorder)
585 #ifdef WORDS_BIGENDIAN
586 case WRC_BO_LITTLE:
587 #else
588 case WRC_BO_BIG:
589 #endif
590 cur->planes = BYTESWAP_WORD(info.biPlanes);
591 cur->bits = BYTESWAP_WORD(info.biBitCount);
592 break;
593 default:
594 cur->planes = info.biPlanes;
595 cur->bits = info.biBitCount;
597 if(!win32 && (cur->planes != 1 || cur->bits != 1))
598 parser_warning("Win16 cursor contains colors\n");
599 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
600 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
601 cur->data = new_raw_data();
602 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
603 if(!list)
605 list = cur;
607 else
609 cur->next = list;
610 list->prev = cur;
611 list = cur;
614 curg->cursorlist = list;
615 *ncur = cnt;
619 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
621 icon_group_t *icog = xmalloc(sizeof(icon_group_t));
622 if(memopt)
624 icog->memopt = *memopt;
625 free(memopt);
627 else
628 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
629 icog->lvc = rd->lvc;
630 split_icons(rd, icog, &(icog->nicon));
631 free(rd->data);
632 free(rd);
633 return icog;
636 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
638 cursor_group_t *curg = xmalloc(sizeof(cursor_group_t));
639 if(memopt)
641 curg->memopt = *memopt;
642 free(memopt);
644 else
645 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
646 curg->lvc = rd->lvc;
647 split_cursors(rd, curg, &(curg->ncursor));
648 free(rd->data);
649 free(rd);
650 return curg;
654 * Animated cursors and icons
656 * The format of animated cursors and icons is yet another example
657 * of bad design by "The Company". The entire RIFF structure is
658 * flawed by design because it is inconsistent and single minded:
659 * - some tags have lengths attached, others don't. The use of these
660 * non-length tags is absolutely unclear;
661 * - the content of "icon" tags can be both icons and cursors;
662 * - tags lack proper alignment constraints. It seems that everything
663 * is 16bit aligned, but I could not find that in any docu. Just be
664 * prepared to eat anything;
665 * - there are no strict constraints on tag-nesting and the organization
666 * is highly illogical;
668 * Anyhow, here is the basic structure:
669 * "RIFF" { dword taglength }
670 * "ACON" // What does it do?
671 * "LIST" { dword taglength }
672 * "INFO" // And what does this do?
673 * "INAM" { dword taglength } // Icon/cursor name
674 * {inam data}
675 * "IART" { dword taglength } // The artist
676 * {iart data}
677 * "fram" // Is followed by "icon"s
678 * "icon" { dword taglength } // First frame
679 * { icon/cursor data }
680 * "icon" { dword taglength } // Second frame
681 * { icon/cursor data }
682 * ... // ...
683 * "anih" { dword taglength } // Header structure
684 * { aniheader_t structure }
685 * "rate" { dword taglength } // The rate for each frame
686 * { `steps' dwords }
687 * "seq " { dword taglength } // The frame blit-order
688 * { `steps' dwords }
690 * Tag length are bytelength without the header and length field (i.e. -8).
691 * The "LIST" tag may occur several times and may encapsulate different
692 * tags. The `steps' is the number of "icon" tags found (actually the
693 * number of steps specified in the aniheader_t structure). The "seq "uence
694 * tag can be omitted, in which case the sequence is equal to the sequence
695 * of "icon"s found in the file. Also "rate" may be omitted, in which case
696 * the default from the aniheader_t structure is used.
698 * An animated cursor puts `.cur' formatted files into each "icon" tag,
699 * whereas animated icons contain `.ico' formatted files.
701 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
702 * dealt with in the same code. However, this version shows what is going on
703 * and is better debug-able.
705 static const char riff[4] = "RIFF";
706 static const char acon[4] = "ACON";
707 static const char list[4] = "LIST";
708 static const char info[4] = "INFO";
709 static const char inam[4] = "INAM";
710 static const char iart[4] = "IART";
711 static const char fram[4] = "fram";
712 static const char icon[4] = "icon";
713 static const char anih[4] = "anih";
714 static const char rate[4] = "rate";
715 static const char seq[4] = "seq ";
717 #define SKIP_TAG(p,size) ((riff_tag_t *)(((char *)p) + (size)))
719 #define NEXT_TAG(p) SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
721 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
723 cursor_dir_entry_t *cdp;
724 cursor_header_t *chp;
725 int count;
726 int ctype;
727 int i;
728 static int once = 0; /* This will trigger only once per file! */
729 const char *anistr = type == res_aniico ? "icon" : "cursor";
730 /* Notes:
731 * Both cursor and icon directories are similar
732 * Both cursor and icon headers are similar
735 chp = (cursor_header_t *)(rtp+1);
736 cdp = (cursor_dir_entry_t *)(chp+1);
737 count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
738 ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
739 chp->reserved = BYTESWAP_WORD(chp->reserved);
740 chp->type = BYTESWAP_WORD(chp->type);
741 chp->count = BYTESWAP_WORD(chp->count);
743 if(type == res_anicur && ctype != 2 && !once)
745 parser_warning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)\n",
746 ctype,
747 ctype == 1 ? "icontype" : "?");
748 once++;
750 else if(type == res_aniico && ctype != 1 && !once)
752 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
753 ctype,
754 ctype == 2 ? "cursortype" : "?");
755 once++;
757 else if(ctype != 1 && ctype != 2 && !once)
759 parser_warning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)\n", anistr, ctype);
760 once++;
763 for(i = 0; i < count; i++)
765 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
766 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
767 if(ofs > rtp->size || ofs+sze > rtp->size)
768 parser_error("Animated %s's data corrupt", anistr);
769 convert_bitmap((char *)chp + ofs, 0);
770 cdp[i].xhot = BYTESWAP_WORD(cdp->xhot);
771 cdp[i].yhot = BYTESWAP_WORD(cdp->yhot);
772 cdp[i].ressize = BYTESWAP_DWORD(cdp->ressize);
773 cdp[i].offset = BYTESWAP_DWORD(cdp->offset);
777 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
779 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
781 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
783 if(!memcmp(rtp->tag, info, sizeof(info)))
785 rtp = SKIP_TAG(rtp,4);
787 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
789 /* Ignore the icon/cursor name; its a string */
790 rtp = NEXT_TAG(rtp);
792 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
794 /* Ignore the author's name; it's a string */
795 rtp = NEXT_TAG(rtp);
797 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
799 /* This should be followed by "icon"s, but we
800 * simply ignore this because it is pure
801 * non-information.
803 rtp = SKIP_TAG(rtp,4);
805 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
807 handle_ani_icon(rtp, type, isswapped);
808 rtp = NEXT_TAG(rtp);
810 else
811 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
812 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
813 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
814 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
815 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
817 if((UINT_PTR)rtp & 1)
818 rtp = SKIP_TAG(rtp,1);
822 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
824 ani_curico_t *ani = xmalloc(sizeof(ani_curico_t));
825 riff_tag_t *rtp;
826 int isswapped = 0;
827 int doswap;
828 const char *anistr = type == res_aniico ? "icon" : "cursor";
830 assert(!memcmp(rd->data, riff, sizeof(riff)));
831 assert(type == res_anicur || type == res_aniico);
833 rtp = (riff_tag_t *)rd->data;
835 if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
836 isswapped = 1;
837 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
838 isswapped = 0;
839 else
840 parser_error("Animated %s has an invalid RIFF length", anistr);
842 switch(byteorder)
844 #ifdef WORDS_BIGENDIAN
845 case WRC_BO_LITTLE:
846 #else
847 case WRC_BO_BIG:
848 #endif
849 doswap = !isswapped;
850 break;
851 default:
852 doswap = isswapped;
856 * When to swap what:
857 * isswapped | doswap |
858 * ----------+--------+---------------------------------
859 * 0 | 0 | read native; don't convert
860 * 1 | 0 | read swapped size; don't convert
861 * 0 | 1 | read native; convert
862 * 1 | 1 | read swapped size; convert
863 * Reading swapped size if necessary to calculate in native
864 * format. E.g. a little-endian source on a big-endian
865 * processor.
867 if(doswap)
869 /* We only go through the RIFF file if we need to swap
870 * bytes in words/dwords. Else we couldn't care less
871 * what the file contains. This is consistent with
872 * MS' rc.exe, which doesn't complain at all, even though
873 * the file format might not be entirely correct.
875 rtp++; /* Skip the "RIFF" tag */
877 while((char *)rtp < (char *)rd->data + rd->size)
879 if(!memcmp(rtp->tag, acon, sizeof(acon)))
881 rtp = SKIP_TAG(rtp,4);
883 else if(!memcmp(rtp->tag, list, sizeof(list)))
885 handle_ani_list(rtp, type, isswapped);
886 rtp = NEXT_TAG(rtp);
888 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
890 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
891 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
892 ahp->frames = BYTESWAP_DWORD(ahp->frames);
893 ahp->steps = BYTESWAP_DWORD(ahp->steps);
894 ahp->cx = BYTESWAP_DWORD(ahp->cx);
895 ahp->cy = BYTESWAP_DWORD(ahp->cy);
896 ahp->bitcount = BYTESWAP_DWORD(ahp->bitcount);
897 ahp->planes = BYTESWAP_DWORD(ahp->planes);
898 ahp->rate = BYTESWAP_DWORD(ahp->rate);
899 ahp->flags = BYTESWAP_DWORD(ahp->flags);
900 rtp = NEXT_TAG(rtp);
902 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
904 int cnt = rtp->size / sizeof(DWORD);
905 DWORD *dwp = (DWORD *)(rtp+1);
906 int i;
907 for(i = 0; i < cnt; i++)
908 dwp[i] = BYTESWAP_DWORD(dwp[i]);
909 rtp = NEXT_TAG(rtp);
911 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
913 int cnt = rtp->size / sizeof(DWORD);
914 DWORD *dwp = (DWORD *)(rtp+1);
915 int i;
916 for(i = 0; i < cnt; i++)
917 dwp[i] = BYTESWAP_DWORD(dwp[i]);
918 rtp = NEXT_TAG(rtp);
920 else
921 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
922 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
923 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
924 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
925 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
927 if((UINT_PTR)rtp & 1)
928 rtp = SKIP_TAG(rtp,1);
931 /* We must end correctly here */
932 if((char *)rtp != (char *)rd->data + rd->size)
933 parser_error("Animated %s contains invalid field size(s)", anistr);
936 ani->data = rd;
937 if(memopt)
939 ani->memopt = *memopt;
940 free(memopt);
942 else
943 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
944 return ani;
946 #undef NEXT_TAG
948 /* Bitmaps */
949 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
951 bitmap_t *bmp = xmalloc(sizeof(bitmap_t));
953 bmp->data = rd;
954 if(memopt)
956 bmp->memopt = *memopt;
957 free(memopt);
959 else
960 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
961 rd->size -= convert_bitmap(rd->data, rd->size);
962 return bmp;
965 ver_words_t *new_ver_words(int i)
967 ver_words_t *w = xmalloc(sizeof(ver_words_t));
968 w->words = xmalloc(sizeof(WORD));
969 w->words[0] = (WORD)i;
970 w->nwords = 1;
971 return w;
974 ver_words_t *add_ver_words(ver_words_t *w, int i)
976 w->words = xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
977 w->words[w->nwords] = (WORD)i;
978 w->nwords++;
979 return w;
982 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
983 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
985 messagetable_t *msg = xmalloc(sizeof(messagetable_t));
986 msgtab_block_t *mbp;
987 DWORD nblk;
988 DWORD i;
989 WORD lo;
990 WORD hi;
992 msg->data = rd;
993 if(memopt)
995 msg->memopt = *memopt;
996 free(memopt);
998 else
999 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1001 if(rd->size < sizeof(DWORD))
1002 parser_error("Invalid messagetable, size too small");
1004 nblk = *(DWORD *)rd->data;
1005 lo = WRC_LOWORD(nblk);
1006 hi = WRC_HIWORD(nblk);
1008 /* FIXME:
1009 * This test will fail for all n*2^16 blocks in the messagetable.
1010 * However, no sane person would want to have so many blocks
1011 * and have a table of megabytes attached.
1012 * So, I will assume that we have less than 2^16 blocks in the table
1013 * and all will just work out fine. Otherwise, we would need to test
1014 * the ID, offset and length (and flag) fields to be very sure.
1016 if(hi && lo)
1017 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian\n");
1018 if(!hi && !lo)
1019 parser_error("Invalid messagetable block count 0");
1021 if(!hi && lo) /* Messagetable byteorder == native byteorder */
1023 #ifdef WORDS_BIGENDIAN
1024 if(byteorder != WRC_BO_LITTLE) goto out;
1025 #else
1026 if(byteorder != WRC_BO_BIG) goto out;
1027 #endif
1028 /* Resource byteorder != native byteorder */
1030 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1031 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1032 parser_error("Messagetable's blocks are outside of defined data");
1033 for(i = 0; i < nblk; i++)
1035 msgtab_entry_t *mep, *next_mep;
1036 DWORD id;
1038 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1040 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1042 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1043 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1044 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1046 WORD *wp = (WORD *)&mep[1];
1047 int l = mep->length/2 - 2; /* Length included header */
1048 int n;
1050 if(mep->length & 1)
1051 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1052 for(n = 0; n < l; n++)
1053 wp[n] = BYTESWAP_WORD(wp[n]);
1056 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1057 mep->length = BYTESWAP_WORD(mep->length);
1058 mep->flags = BYTESWAP_WORD(mep->flags);
1059 mep = next_mep;
1062 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1063 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1064 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1067 if(hi && !lo) /* Messagetable byteorder != native byteorder */
1069 #ifdef WORDS_BIGENDIAN
1070 if(byteorder == WRC_BO_LITTLE) goto out;
1071 #else
1072 if(byteorder == WRC_BO_BIG) goto out;
1073 #endif
1074 /* Resource byteorder == native byteorder */
1076 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1077 nblk = BYTESWAP_DWORD(nblk);
1078 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1079 parser_error("Messagetable's blocks are outside of defined data");
1080 for(i = 0; i < nblk; i++)
1082 msgtab_entry_t *mep;
1083 DWORD id;
1085 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1086 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1087 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1088 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1090 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1092 mep->length = BYTESWAP_WORD(mep->length);
1093 mep->flags = BYTESWAP_WORD(mep->flags);
1095 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1096 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1097 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1099 WORD *wp = (WORD *)&mep[1];
1100 int l = mep->length/2 - 2; /* Length included header */
1101 int n;
1103 if(mep->length & 1)
1104 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1105 for(n = 0; n < l; n++)
1106 wp[n] = BYTESWAP_WORD(wp[n]);
1109 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1114 out:
1115 return msg;
1117 #undef MSGTAB_BAD_PTR
1119 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1121 assert(offs <= src->size);
1122 assert(offs + len <= src->size);
1123 if(!dst->data)
1125 dst->data = xmalloc(len);
1126 dst->size = 0;
1128 else
1129 dst->data = xrealloc(dst->data, dst->size + len);
1130 /* dst->size holds the offset to copy to */
1131 memcpy(dst->data + dst->size, src->data + offs, len);
1132 dst->size += len;
1135 int *new_int(int i)
1137 int *ip = xmalloc(sizeof(int));
1138 *ip = i;
1139 return ip;
1142 stringtable_t *new_stringtable(lvc_t *lvc)
1144 stringtable_t *stt = xmalloc(sizeof(stringtable_t));
1146 memset( stt, 0, sizeof(*stt) );
1147 if(lvc)
1148 stt->lvc = *lvc;
1150 return stt;
1153 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1155 toolbar_t *tb = xmalloc(sizeof(toolbar_t));
1156 memset( tb, 0, sizeof(*tb) );
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 = 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 = 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 = xmalloc(sizeof(style_t));
1190 st->or_mask = or_mask;
1191 st->and_mask = and_mask;
1192 return st;