2 * Create dynamic new structures of various types
3 * and some utils in that trend.
5 * Copyright 1998 Bertho A. Stultiens
22 #include "wingdi.h" /* for BITMAPINFOHEADER */
24 /* Generate new_* functions that have no parameters (NOTE: no ';') */
25 __NEW_STRUCT_FUNC(dialog)
26 __NEW_STRUCT_FUNC(dialogex)
27 __NEW_STRUCT_FUNC(name_id)
28 __NEW_STRUCT_FUNC(menu)
29 __NEW_STRUCT_FUNC(menuex)
30 __NEW_STRUCT_FUNC(menu_item)
31 __NEW_STRUCT_FUNC(menuex_item)
32 __NEW_STRUCT_FUNC(control)
33 __NEW_STRUCT_FUNC(icon)
34 __NEW_STRUCT_FUNC(cursor)
35 __NEW_STRUCT_FUNC(versioninfo)
36 __NEW_STRUCT_FUNC(ver_value)
37 __NEW_STRUCT_FUNC(ver_block)
38 __NEW_STRUCT_FUNC(stt_entry)
39 __NEW_STRUCT_FUNC(accelerator)
40 __NEW_STRUCT_FUNC(event)
41 __NEW_STRUCT_FUNC(raw_data)
42 __NEW_STRUCT_FUNC(lvc)
43 __NEW_STRUCT_FUNC(res_count)
44 __NEW_STRUCT_FUNC(string)
45 __NEW_STRUCT_FUNC(toolbar_item)
46 __NEW_STRUCT_FUNC(ani_any)
48 /* New instances for all types of structures */
49 /* Very inefficient (in size), but very functional :-]
50 * Especially for type-checking.
52 resource_t *new_resource(enum res_e t, void *res, int memopt, language_t *lan)
54 resource_t *r = (resource_t *)xmalloc(sizeof(resource_t));
62 version_t *new_version(DWORD v)
64 version_t *vp = (version_t *)xmalloc(sizeof(version_t));
69 characts_t *new_characts(DWORD c)
71 characts_t *cp = (characts_t *)xmalloc(sizeof(characts_t));
76 language_t *new_language(int id, int sub)
78 language_t *lan = (language_t *)xmalloc(sizeof(language_t));
84 language_t *dup_language(language_t *l)
87 return new_language(l->id, l->sub);
90 version_t *dup_version(version_t *v)
93 return new_version(*v);
96 characts_t *dup_characts(characts_t *c)
99 return new_characts(*c);
102 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
104 rcdata_t *rc = (rcdata_t *)xmalloc(sizeof(rcdata_t));
108 rc->memopt = *memopt;
112 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
116 font_id_t *new_font_id(int size, string_t *face, int weight, int italic)
118 font_id_t *fid = (font_id_t *)xmalloc(sizeof(font_id_t));
121 fid->weight = weight;
122 fid->italic = italic;
126 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
128 user_t *usr = (user_t *)xmalloc(sizeof(user_t));
132 usr->memopt = *memopt;
136 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
141 font_t *new_font(raw_data_t *rd, int *memopt)
143 font_t *fnt = (font_t *)xmalloc(sizeof(font_t));
147 fnt->memopt = *memopt;
151 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
155 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
157 fontdir_t *fnd = (fontdir_t *)xmalloc(sizeof(fontdir_t));
161 fnd->memopt = *memopt;
165 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
171 * Convert bitmaps to proper endian
173 static void convert_bitmap_swap_v3(BITMAPINFOHEADER *bih)
175 bih->biSize = BYTESWAP_DWORD(bih->biSize);
176 bih->biWidth = BYTESWAP_DWORD(bih->biWidth);
177 bih->biHeight = BYTESWAP_DWORD(bih->biHeight);
178 bih->biPlanes = BYTESWAP_WORD(bih->biPlanes);
179 bih->biBitCount = BYTESWAP_WORD(bih->biBitCount);
180 bih->biCompression = BYTESWAP_DWORD(bih->biCompression);
181 bih->biSizeImage = BYTESWAP_DWORD(bih->biSizeImage);
182 bih->biXPelsPerMeter = BYTESWAP_DWORD(bih->biXPelsPerMeter);
183 bih->biYPelsPerMeter = BYTESWAP_DWORD(bih->biYPelsPerMeter);
184 bih->biClrUsed = BYTESWAP_DWORD(bih->biClrUsed);
185 bih->biClrImportant = BYTESWAP_DWORD(bih->biClrImportant);
188 static void convert_bitmap_swap_v4(BITMAPV4HEADER *b4h)
190 convert_bitmap_swap_v3((BITMAPINFOHEADER *)b4h);
191 b4h->bV4RedMask = BYTESWAP_DWORD(b4h->bV4RedMask);
192 b4h->bV4GreenMask = BYTESWAP_DWORD(b4h->bV4GreenMask);
193 b4h->bV4BlueMask = BYTESWAP_DWORD(b4h->bV4BlueMask);
194 b4h->bV4AlphaMask = BYTESWAP_DWORD(b4h->bV4AlphaMask);
195 b4h->bV4CSType = BYTESWAP_DWORD(b4h->bV4CSType);
196 b4h->bV4EndPoints.ciexyzRed.ciexyzX = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzX);
197 b4h->bV4EndPoints.ciexyzRed.ciexyzY = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzY);
198 b4h->bV4EndPoints.ciexyzRed.ciexyzZ = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzRed.ciexyzZ);
199 b4h->bV4EndPoints.ciexyzGreen.ciexyzX = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzX);
200 b4h->bV4EndPoints.ciexyzGreen.ciexyzY = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzY);
201 b4h->bV4EndPoints.ciexyzGreen.ciexyzZ = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzGreen.ciexyzZ);
202 b4h->bV4EndPoints.ciexyzBlue.ciexyzX = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzX);
203 b4h->bV4EndPoints.ciexyzBlue.ciexyzY = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzY);
204 b4h->bV4EndPoints.ciexyzBlue.ciexyzZ = BYTESWAP_DWORD(b4h->bV4EndPoints.ciexyzBlue.ciexyzZ);
205 b4h->bV4GammaRed = BYTESWAP_DWORD(b4h->bV4GammaRed);
206 b4h->bV4GammaGreen = BYTESWAP_DWORD(b4h->bV4GammaGreen);
207 b4h->bV4GammaBlue = BYTESWAP_DWORD(b4h->bV4GammaBlue);
210 #define FL_SIGBE 0x01
211 #define FL_SIZEBE 0x02
213 static int convert_bitmap(char *data, int size)
215 BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *)data;
216 BITMAPV4HEADER *b4h = (BITMAPV4HEADER *)data;
218 #ifdef WORDS_BIGENDIAN
219 DWORD bisizel = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
220 DWORD b4sizel = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
221 DWORD bisizeb = sizeof(BITMAPINFOHEADER);
222 DWORD b4sizeb = sizeof(BITMAPV4HEADER);
224 DWORD bisizel = sizeof(BITMAPINFOHEADER);
225 DWORD b4sizel = sizeof(BITMAPV4HEADER);
226 DWORD bisizeb = BYTESWAP_DWORD(sizeof(BITMAPINFOHEADER));
227 DWORD b4sizeb = BYTESWAP_DWORD(sizeof(BITMAPV4HEADER));
230 if(data[0] == 'B' && data[1] == 'M')
232 /* Little endian signature */
233 bih = (BITMAPINFOHEADER *)(data + sizeof(BITMAPFILEHEADER));
234 b4h = (BITMAPV4HEADER *)(data + sizeof(BITMAPFILEHEADER));
236 else if(data[0] == 'M' && data[1] == 'B')
238 type |= FL_SIGBE; /* Big endian signature */
239 bih = (BITMAPINFOHEADER *)(data + sizeof(BITMAPFILEHEADER));
240 b4h = (BITMAPV4HEADER *)(data + sizeof(BITMAPFILEHEADER));
243 if(bih->biSize == bisizel)
247 else if(bih->biSize == b4sizel)
251 else if(bih->biSize == bisizeb)
255 else if(bih->biSize == b4sizeb)
257 type |= FL_SIZEBE | FL_V4;
267 case FL_SIZEBE | FL_V4:
268 yywarning("Bitmap v%c signature little-endian, but size big-endian", type & FL_V4 ? '4' : '3');
271 case FL_SIGBE | FL_V4:
272 yywarning("Bitmap v%c signature big-endian, but size little-endian", type & FL_V4 ? '4' : '3');
275 yyerror("Invalid bitmap format");
281 #ifdef WORDS_BIGENDIAN
285 if(!(type & FL_SIZEBE))
288 convert_bitmap_swap_v4(b4h);
290 convert_bitmap_swap_v3(bih);
293 #ifndef WORDS_BIGENDIAN
300 convert_bitmap_swap_v4(b4h);
302 convert_bitmap_swap_v3(bih);
307 if(size && (void *)data != (void *)bih)
309 /* We have the fileheader still attached, remove it */
310 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
311 return sizeof(BITMAPFILEHEADER);
320 * Cursor and icon splitter functions used when allocating
321 * cursor- and icon-groups.
328 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
332 assert(list != NULL);
337 *list = (id_alloc_t *)xmalloc(sizeof(id_alloc_t));
339 (*list)[0].lan = *lan;
344 for(i = 0; i < *n; i++)
346 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
347 return ++((*list)[i].id);
350 *list = (id_alloc_t *)xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
351 (*list)[*n].lan = *lan;
357 static int alloc_icon_id(language_t *lan)
359 static id_alloc_t *idlist = NULL;
362 return get_new_id(&idlist, &nid, lan);
365 static int alloc_cursor_id(language_t *lan)
367 static id_alloc_t *idlist = NULL;
370 return get_new_id(&idlist, &nid, lan);
373 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
377 icon_dir_entry_t *ide;
380 icon_header_t *ih = (icon_header_t *)rd->data;
385 else if(BYTESWAP_WORD(ih->type) == 1)
388 yyerror("Icon resource data has invalid type id %d", ih->type);
390 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
391 ide = (icon_dir_entry_t *)((char *)rd->data + sizeof(icon_header_t));
392 for(i = 0; i < cnt; i++)
395 ico->id = alloc_icon_id(icog->lvc.language);
396 ico->lvc = icog->lvc;
399 ide[i].offset = BYTESWAP_DWORD(ide[i].offset);
400 ide[i].ressize= BYTESWAP_DWORD(ide[i].ressize);
402 if(ide[i].offset > rd->size
403 || ide[i].offset + ide[i].ressize > rd->size)
404 yyerror("Icon resource data corrupt");
405 ico->width = ide[i].width;
406 ico->height = ide[i].height;
407 ico->nclr = ide[i].nclr;
408 ico->planes = swap ? BYTESWAP_WORD(ide[i].planes) : ide[i].planes;
409 ico->bits = swap ? BYTESWAP_WORD(ide[i].bits) : ide[i].bits;
410 convert_bitmap((char *)rd->data + ide[i].offset, 0);
414 /* Argh! They did not fill out the resdir structure */
415 planes = ((BITMAPINFOHEADER *)((char *)rd->data + ide[i].offset))->biPlanes;
416 /* The bitmap is in destination byteorder. We want native for our structures */
419 #ifdef WORDS_BIGENDIAN
424 ico->planes = BYTESWAP_WORD(planes);
427 ico->planes = planes;
433 /* Argh! They did not fill out the resdir structure */
434 bits = ((BITMAPINFOHEADER *)((char *)rd->data + ide[i].offset))->biBitCount;
435 /* The bitmap is in destination byteorder. We want native for our structures */
438 #ifdef WORDS_BIGENDIAN
443 ico->bits = BYTESWAP_WORD(bits);
449 ico->data = new_raw_data();
450 copy_raw_data(ico->data, rd, ide[i].offset, ide[i].ressize);
462 icog->iconlist = list;
466 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
470 cursor_dir_entry_t *cde;
472 cursor_t *list = NULL;
473 cursor_header_t *ch = (cursor_header_t *)rd->data;
478 else if(BYTESWAP_WORD(ch->type) == 2)
481 yyerror("Cursor resource data has invalid type id %d", ch->type);
482 cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
483 cde = (cursor_dir_entry_t *)((char *)rd->data + sizeof(cursor_header_t));
484 for(i = 0; i < cnt; i++)
489 cur->id = alloc_cursor_id(curg->lvc.language);
490 cur->lvc = curg->lvc;
493 cde[i].offset = BYTESWAP_DWORD(cde[i].offset);
494 cde[i].ressize= BYTESWAP_DWORD(cde[i].ressize);
496 if(cde[i].offset > rd->size
497 || cde[i].offset + cde[i].ressize > rd->size)
498 yyerror("Cursor resource data corrupt");
499 cur->width = cde[i].width;
500 cur->height = cde[i].height;
501 cur->nclr = cde[i].nclr;
502 convert_bitmap((char *)rd->data + cde[i].offset, 0);
503 /* The next two are to support color cursors */
504 planes = ((BITMAPINFOHEADER *)((char *)rd->data + cde[i].offset))->biPlanes;
505 bits = ((BITMAPINFOHEADER *)((char *)rd->data + cde[i].offset))->biBitCount;
506 /* The bitmap is in destination byteorder. We want native for our structures */
509 #ifdef WORDS_BIGENDIAN
514 cur->planes = BYTESWAP_WORD(planes);
515 cur->bits = BYTESWAP_WORD(bits);
518 cur->planes = planes;
521 if(!win32 && (cur->planes != 1 || cur->bits != 1))
522 yywarning("Win16 cursor contains colors");
523 cur->xhot = swap ? BYTESWAP_WORD(cde[i].xhot) : cde[i].xhot;
524 cur->yhot = swap ? BYTESWAP_WORD(cde[i].yhot) : cde[i].yhot;
525 cur->data = new_raw_data();
526 copy_raw_data(cur->data, rd, cde[i].offset, cde[i].ressize);
538 curg->cursorlist = list;
543 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
545 icon_group_t *icog = (icon_group_t *)xmalloc(sizeof(icon_group_t));
548 icog->memopt = *memopt;
552 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
554 split_icons(rd, icog, &(icog->nicon));
560 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
562 cursor_group_t *curg = (cursor_group_t *)xmalloc(sizeof(cursor_group_t));
565 curg->memopt = *memopt;
569 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
571 split_cursors(rd, curg, &(curg->ncursor));
578 * Animated cursors and icons
580 * The format of animated cursors and icons is yet another example
581 * of bad design by "The Company". The entire RIFF structure is
582 * flawed by design because it is inconsistent and single minded:
583 * - some tags have lengths attached, others don't. The use of these
584 * non-length tags is absolutely unclear;
585 * - the content of "icon" tags can be both icons and cursors;
586 * - tags lack proper alignment constraints. It seems that everything
587 * is 16bit aligned, but I could not find that in any docu. Just be
588 * prepared to eat anything;
589 * - there are no strict constraints on tag-nesting and the organization
590 * is highly illogical;
592 * Anyhow, here is the basic structure:
593 * "RIFF" { dword taglength }
594 * "ACON" // What does it do?
595 * "LIST" { dword taglength }
596 * "INFO" // And what does this do?
597 * "INAM" { dword taglength } // Icon/cursor name
599 * "IART" { dword taglength } // The artist
601 * "fram" // Is followed by "icon"s
602 * "icon" { dword taglength } // First frame
603 * { icon/cursor data }
604 * "icon" { dword taglength } // Second frame
605 * { icon/cursor data }
607 * "anih" { dword taglength } // Header structure
608 * { aniheader_t structure }
609 * "rate" { dword taglength } // The rate for each frame
611 * "seq " { dword taglength } // The frame blit-order
614 * Tag length are bytelength without the header and length field (i.e. -8).
615 * The "LIST" tag may occur several times and may encapsulate different
616 * tags. The `steps' is the number of "icon" tags found (actually the
617 * number of steps specified in the aniheader_t structure). The "seq "uence
618 * tag can be ommitted, in which case the sequence is equal to the sequence
619 * of "icon"s found in the file. Also "rate" may be ommitted, in which case
620 * the default from the aniheader_t structure is used.
622 * An animated cursor puts `.cur' formatted files into each "icon" tag,
623 * whereas animated icons contain `.ico' formatted files.
625 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
626 * dealt with in the same code. However, this version shows what is going on
627 * and is better debug-able.
629 static const char riff[4] = "RIFF";
630 static const char acon[4] = "ACON";
631 static const char list[4] = "LIST";
632 static const char info[4] = "INFO";
633 static const char inam[4] = "INAM";
634 static const char iart[4] = "IART";
635 static const char fram[4] = "fram";
636 static const char icon[4] = "icon";
637 static const char anih[4] = "anih";
638 static const char rate[4] = "rate";
639 static const char seq[4] = "seq ";
641 #define NEXT_TAG(p) ((riff_tag_t *)(((char *)p) + (isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p)))
643 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
645 cursor_dir_entry_t *cdp;
646 cursor_header_t *chp;
650 static int once = 0; /* This will trigger only once per file! */
651 const char *anistr = type == res_aniico ? "icon" : "cursor";
653 * Both cursor and icon directories are similar
654 * Both cursor and icon headers are similar
657 chp = (cursor_header_t *)(rtp+1);
658 cdp = (cursor_dir_entry_t *)(chp+1);
659 count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
660 ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
661 chp->reserved = BYTESWAP_WORD(chp->reserved);
662 chp->type = BYTESWAP_WORD(chp->type);
663 chp->count = BYTESWAP_WORD(chp->count);
665 if(type == res_anicur && ctype != 2 && !once)
667 yywarning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)",
669 ctype == 1 ? "icontype" : "?");
672 else if(type == res_aniico && ctype != 1 && !once)
674 yywarning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)",
676 ctype == 2 ? "cursortype" : "?");
679 else if(ctype != 1 && ctype != 2 && !once)
681 yywarning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)", anistr, ctype);
685 for(i = 0; i < count; i++)
687 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
688 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
689 if(ofs > rtp->size || ofs+sze > rtp->size)
690 yyerror("Animated %s's data corrupt", anistr);
691 convert_bitmap((char *)chp + ofs, 0);
692 cdp[i].xhot = BYTESWAP_WORD(cdp->xhot);
693 cdp[i].yhot = BYTESWAP_WORD(cdp->yhot);
694 cdp[i].ressize = BYTESWAP_DWORD(cdp->ressize);
695 cdp[i].offset = BYTESWAP_DWORD(cdp->offset);
699 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
701 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
703 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
705 if(!memcmp(rtp->tag, info, sizeof(info)))
707 rtp = (riff_tag_t *)(((char *)rtp) + 4);
709 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
711 /* Ignore the icon/cursor name; its a string */
714 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
716 /* Ignore the author's name; its a string */
719 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
721 /* This should be followed by "icon"s, but we
722 * simply ignore this because it is pure
725 rtp = (riff_tag_t *)(((char *)rtp) + 4);
727 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
729 handle_ani_icon(rtp, type, isswapped);
733 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
734 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
735 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
736 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
737 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
739 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
745 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
747 ani_curico_t *ani = (ani_curico_t *)xmalloc(sizeof(ani_curico_t));
751 const char *anistr = type == res_aniico ? "icon" : "cursor";
753 assert(!memcmp(rd->data, riff, sizeof(riff)));
754 assert(type == res_anicur || type == res_aniico);
756 rtp = (riff_tag_t *)rd->data;
758 if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
760 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
763 yyerror("Animated %s has an invalid RIFF length", anistr);
767 #ifdef WORDS_BIGENDIAN
780 * isswapped | doswap |
781 * ----------+--------+---------------------------------
782 * 0 | 0 | read native; don't convert
783 * 1 | 0 | read swapped size; don't convert
784 * 0 | 1 | read native; convert
785 * 1 | 1 | read swapped size; convert
786 * Reading swapped size if necessary to calculate in native
787 * format. E.g. a little-endian source on a big-endian
792 /* We only go through the RIFF file if we need to swap
793 * bytes in words/dwords. Else we couldn't care less
794 * what the file contains. This is consistent with
795 * MS' rc.exe, which doesn't complain at all, eventhough
796 * the fileformat might not be entirely correct.
798 rtp++; /* Skip the "RIFF" tag */
800 while((char *)rtp < (char *)rd->data + rd->size)
802 if(!memcmp(rtp->tag, acon, sizeof(acon)))
804 rtp = (riff_tag_t *)(((char *)rtp) + 4);
806 else if(!memcmp(rtp->tag, list, sizeof(list)))
808 handle_ani_list(rtp, type, isswapped);
811 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
813 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
814 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
815 ahp->frames = BYTESWAP_DWORD(ahp->frames);
816 ahp->steps = BYTESWAP_DWORD(ahp->steps);
817 ahp->cx = BYTESWAP_DWORD(ahp->cx);
818 ahp->cy = BYTESWAP_DWORD(ahp->cy);
819 ahp->bitcount = BYTESWAP_DWORD(ahp->bitcount);
820 ahp->planes = BYTESWAP_DWORD(ahp->planes);
821 ahp->rate = BYTESWAP_DWORD(ahp->rate);
822 ahp->flags = BYTESWAP_DWORD(ahp->flags);
825 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
827 int cnt = rtp->size / sizeof(DWORD);
828 DWORD *dwp = (DWORD *)(rtp+1);
830 for(i = 0; i < cnt; i++)
831 dwp[i] = BYTESWAP_DWORD(dwp[i]);
834 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
836 int cnt = rtp->size / sizeof(DWORD);
837 DWORD *dwp = (DWORD *)(rtp+1);
839 for(i = 0; i < cnt; i++)
840 dwp[i] = BYTESWAP_DWORD(dwp[i]);
844 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file",
845 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
846 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
847 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
848 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
850 /* FIXME: This relies in sizeof(DWORD) == sizeof(pointer_type) */
855 /* We must end correctly here */
856 if((char *)rtp != (char *)rd->data + rd->size)
857 yyerror("Animated %s contains invalid field size(s)", anistr);
863 ani->memopt = *memopt;
867 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
873 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
875 bitmap_t *bmp = (bitmap_t *)xmalloc(sizeof(bitmap_t));
880 bmp->memopt = *memopt;
884 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
885 rd->size -= convert_bitmap(rd->data, rd->size);
889 ver_words_t *new_ver_words(int i)
891 ver_words_t *w = (ver_words_t *)xmalloc(sizeof(ver_words_t));
892 w->words = (WORD *)xmalloc(sizeof(WORD));
893 w->words[0] = (WORD)i;
898 ver_words_t *add_ver_words(ver_words_t *w, int i)
900 w->words = (WORD *)xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
901 w->words[w->nwords] = (WORD)i;
906 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
907 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
909 messagetable_t *msg = (messagetable_t *)xmalloc(sizeof(messagetable_t));
918 msg->memopt = *memopt;
922 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
924 if(rd->size < sizeof(DWORD))
925 yyerror("Invalid messagetable, size too small");
927 nblk = *(DWORD *)rd->data;
928 lo = WRC_LOWORD(nblk);
929 hi = WRC_HIWORD(nblk);
932 * This test will fail for all n*2^16 blocks in the messagetable.
933 * However, no sane person would want to have so many blocks
934 * and have a table of megabytes attached.
935 * So, I will assume that we have less than 2^16 blocks in the table
936 * and all will just work out fine. Otherwise, we would need to test
937 * the ID, offset and length (and flag) fields to be very sure.
940 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian");
942 yyerror("Invalid messagetable block count 0");
944 #ifdef WORDS_BIGENDIAN
945 if(!hi && lo && byteorder != WRC_BO_LITTLE)
947 if(hi && !lo && byteorder != WRC_BO_BIG)
950 msgtab_block_t *mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
951 nblk = BYTESWAP_DWORD(nblk);
952 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
953 yyerror("Messagetable's blocks are outside of defined data");
954 for(i = 0; i < nblk; i++)
959 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
960 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
961 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
962 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
963 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
965 mep->length = BYTESWAP_WORD(mep->length);
966 mep->flags = BYTESWAP_WORD(mep->flags);
967 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
968 yyerror("Messagetable's data for block %d, ID 0x%08lx is outside of defined data", (int)i, id);
969 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
971 WORD *wp = (WORD *)&mep[1];
972 int l = mep->length/2 - 2; /* Length included header */
976 yyerror("Message 0x%08lx is unicode (block %d), but has odd length (%d)", id, (int)i, mep->length);
977 for(n = 0; n < l; n++)
978 wp[n] = BYTESWAP_WORD(wp[n]);
981 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
988 #undef MSGTAB_BAD_PTR
990 void copy_raw_data(raw_data_t *dst, raw_data_t *src, int offs, int len)
992 assert(offs <= src->size);
993 assert(offs + len <= src->size);
996 dst->data = (char *)xmalloc(len);
1000 dst->data = (char *)xrealloc(dst->data, dst->size + len);
1001 /* dst->size holds the offset to copy to */
1002 memcpy(dst->data + dst->size, src->data + offs, len);
1008 int *ip = (int *)xmalloc(sizeof(int));
1013 stringtable_t *new_stringtable(lvc_t *lvc)
1015 stringtable_t *stt = (stringtable_t *)xmalloc(sizeof(stringtable_t));
1023 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1025 toolbar_t *tb = (toolbar_t *)xmalloc(sizeof(toolbar_t));
1026 tb->button_width = button_width;
1027 tb->button_height = button_height;
1028 tb->nitems = nitems;
1033 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1035 dlginit_t *di = (dlginit_t *)xmalloc(sizeof(dlginit_t));
1039 di->memopt = *memopt;
1043 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1048 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1050 style_pair_t *sp = (style_pair_t *)xmalloc(sizeof(style_pair_t));
1052 sp->exstyle = exstyle;
1056 style_t *new_style(DWORD or_mask, DWORD and_mask)
1058 style_t *st = (style_t *)xmalloc(sizeof(style_t));
1059 st->or_mask = or_mask;
1060 st->and_mask = and_mask;