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
35 #include "wingdi.h" /* for BITMAPINFOHEADER */
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) );
84 version_t *new_version(DWORD v)
86 version_t *vp = xmalloc(sizeof(version_t));
91 characts_t *new_characts(DWORD c)
93 characts_t *cp = xmalloc(sizeof(characts_t));
98 language_t *new_language(int id, int sub)
100 language_t *lan = xmalloc(sizeof(language_t));
106 language_t *dup_language(language_t *l)
109 return new_language(l->id, l->sub);
112 version_t *dup_version(version_t *v)
115 return new_version(*v);
118 characts_t *dup_characts(characts_t *c)
121 return new_characts(*c);
124 html_t *new_html(raw_data_t *rd, int *memopt)
126 html_t *html = xmalloc(sizeof(html_t));
130 html->memopt = *memopt;
134 html->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
138 rcdata_t *new_rcdata(raw_data_t *rd, int *memopt)
140 rcdata_t *rc = xmalloc(sizeof(rcdata_t));
144 rc->memopt = *memopt;
148 rc->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
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));
157 fid->weight = weight;
158 fid->italic = italic;
162 user_t *new_user(name_id_t *type, raw_data_t *rd, int *memopt)
164 user_t *usr = xmalloc(sizeof(user_t));
168 usr->memopt = *memopt;
172 usr->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
177 font_t *new_font(raw_data_t *rd, int *memopt)
179 font_t *fnt = xmalloc(sizeof(font_t));
183 fnt->memopt = *memopt;
187 fnt->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
191 fontdir_t *new_fontdir(raw_data_t *rd, int *memopt)
193 fontdir_t *fnd = xmalloc(sizeof(fontdir_t));
197 fnd->memopt = *memopt;
201 fnd->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
207 * Convert bitmaps to proper endian
209 static void convert_bitmap_swap(BITMAPV5HEADER *bh, DWORD size)
211 bh->bV5Size = BYTESWAP_DWORD(bh->bV5Size);
212 bh->bV5Width = BYTESWAP_DWORD(bh->bV5Width);
213 bh->bV5Height = BYTESWAP_DWORD(bh->bV5Height);
214 bh->bV5Planes = BYTESWAP_WORD(bh->bV5Planes);
215 bh->bV5BitCount = BYTESWAP_WORD(bh->bV5BitCount);
216 bh->bV5Compression = BYTESWAP_DWORD(bh->bV5Compression);
217 bh->bV5SizeImage = BYTESWAP_DWORD(bh->bV5SizeImage);
218 bh->bV5XPelsPerMeter = BYTESWAP_DWORD(bh->bV5XPelsPerMeter);
219 bh->bV5YPelsPerMeter = BYTESWAP_DWORD(bh->bV5YPelsPerMeter);
220 bh->bV5ClrUsed = BYTESWAP_DWORD(bh->bV5ClrUsed);
221 bh->bV5ClrImportant = BYTESWAP_DWORD(bh->bV5ClrImportant);
222 if (size == sizeof(BITMAPINFOHEADER)) return;
223 bh->bV5RedMask = BYTESWAP_DWORD(bh->bV5RedMask);
224 bh->bV5GreenMask = BYTESWAP_DWORD(bh->bV5GreenMask);
225 bh->bV5BlueMask = BYTESWAP_DWORD(bh->bV5BlueMask);
226 bh->bV5AlphaMask = BYTESWAP_DWORD(bh->bV5AlphaMask);
227 bh->bV5CSType = BYTESWAP_DWORD(bh->bV5CSType);
228 bh->bV5Endpoints.ciexyzRed.ciexyzX = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzX);
229 bh->bV5Endpoints.ciexyzRed.ciexyzY = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzY);
230 bh->bV5Endpoints.ciexyzRed.ciexyzZ = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzRed.ciexyzZ);
231 bh->bV5Endpoints.ciexyzGreen.ciexyzX = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzX);
232 bh->bV5Endpoints.ciexyzGreen.ciexyzY = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzY);
233 bh->bV5Endpoints.ciexyzGreen.ciexyzZ = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzGreen.ciexyzZ);
234 bh->bV5Endpoints.ciexyzBlue.ciexyzX = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzX);
235 bh->bV5Endpoints.ciexyzBlue.ciexyzY = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzY);
236 bh->bV5Endpoints.ciexyzBlue.ciexyzZ = BYTESWAP_DWORD(bh->bV5Endpoints.ciexyzBlue.ciexyzZ);
237 bh->bV5GammaRed = BYTESWAP_DWORD(bh->bV5GammaRed);
238 bh->bV5GammaGreen = BYTESWAP_DWORD(bh->bV5GammaGreen);
239 bh->bV5GammaBlue = BYTESWAP_DWORD(bh->bV5GammaBlue);
240 if (size == sizeof(BITMAPV4HEADER)) return;
241 bh->bV5Intent = BYTESWAP_DWORD(bh->bV5Intent);
242 bh->bV5ProfileData = BYTESWAP_DWORD(bh->bV5ProfileData);
243 bh->bV5ProfileSize = BYTESWAP_DWORD(bh->bV5ProfileSize);
244 bh->bV5Reserved = BYTESWAP_DWORD(bh->bV5Reserved);
247 static void convert_bitmap_swap_os2(BITMAPOS2HEADER *boh)
249 boh->biSize = BYTESWAP_DWORD(boh->biSize);
250 boh->biWidth = BYTESWAP_WORD(boh->biWidth);
251 boh->biHeight = BYTESWAP_WORD(boh->biHeight);
252 boh->biPlanes = BYTESWAP_WORD(boh->biPlanes);
253 boh->biBitCount = BYTESWAP_WORD(boh->biBitCount);
256 #define FL_SIGBE 0x01
257 #define FL_SIZEBE 0x02
258 static int convert_bitmap(char *data, int size)
260 BITMAPV5HEADER *bih = (BITMAPV5HEADER *)data;
261 BITMAPOS2HEADER *boh = (BITMAPOS2HEADER *)data;
264 int returnSize = 0; /* size to be returned */
267 * Originally the bih and b4h pointers were simply incremented here,
268 * and memmoved at the end of the function. This causes alignment
269 * issues on solaris, so we do the memmove here rather than at the end.
271 if(data[0] == 'B' && data[1] == 'M')
273 /* Little endian signature */
274 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
275 returnSize = sizeof(BITMAPFILEHEADER);
277 else if(data[0] == 'M' && data[1] == 'B')
279 type |= FL_SIGBE; /* Big endian signature */
280 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
281 returnSize = sizeof(BITMAPFILEHEADER);
285 bmsize = bih->bV5Size;
286 if (bmsize >> 16) /* assume swapped */
288 #ifndef WORDS_BIGENDIAN
291 bmsize = BYTESWAP_DWORD( bmsize );
295 #ifdef WORDS_BIGENDIAN
302 case sizeof(BITMAPOS2HEADER):
303 case sizeof(BITMAPINFOHEADER):
304 case sizeof(BITMAPV4HEADER):
305 case sizeof(BITMAPV5HEADER):
308 parser_error("Invalid bitmap format, bih->biSize = %d", bih->bV5Size);
314 parser_warning("Bitmap signature little-endian, but size big-endian\n");
317 parser_warning("Bitmap signature big-endian, but size little-endian\n");
323 #ifdef WORDS_BIGENDIAN
327 if(!(type & FL_SIZEBE))
329 if (bmsize == sizeof(BITMAPOS2HEADER))
330 convert_bitmap_swap_os2(boh);
332 convert_bitmap_swap(bih, bmsize);
335 #ifndef WORDS_BIGENDIAN
341 if (bmsize == sizeof(BITMAPOS2HEADER))
342 convert_bitmap_swap_os2(boh);
344 convert_bitmap_swap(bih, bmsize);
349 if(size && (void *)data != (void *)bih)
351 /* We have the fileheader still attached, remove it */
352 memmove(data, data+sizeof(BITMAPFILEHEADER), size - sizeof(BITMAPFILEHEADER));
353 return sizeof(BITMAPFILEHEADER);
361 * Cursor and icon splitter functions used when allocating
362 * cursor- and icon-groups.
369 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
373 assert(list != NULL);
378 *list = xmalloc(sizeof(id_alloc_t));
380 (*list)[0].lan = *lan;
385 for(i = 0; i < *n; i++)
387 if((*list)[i].lan.id == lan->id && (*list)[i].lan.sub == lan->sub)
388 return ++((*list)[i].id);
391 *list = xrealloc(*list, sizeof(id_alloc_t) * (*n+1));
392 (*list)[*n].lan = *lan;
398 static int alloc_icon_id(language_t *lan)
400 static id_alloc_t *idlist = NULL;
403 return get_new_id(&idlist, &nid, lan);
406 static int alloc_cursor_id(language_t *lan)
408 static id_alloc_t *idlist = NULL;
411 return get_new_id(&idlist, &nid, lan);
414 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
420 icon_header_t *ih = (icon_header_t *)rd->data;
425 else if(BYTESWAP_WORD(ih->type) == 1)
428 parser_error("Icon resource data has invalid type id %d", ih->type);
430 cnt = swap ? BYTESWAP_WORD(ih->count) : ih->count;
431 for(i = 0; i < cnt; i++)
433 icon_dir_entry_t ide;
434 BITMAPINFOHEADER info;
435 memcpy(&ide, rd->data + sizeof(icon_header_t)
436 + i*sizeof(icon_dir_entry_t), sizeof(ide));
439 ico->id = alloc_icon_id(icog->lvc.language);
440 ico->lvc = icog->lvc;
443 ide.offset = BYTESWAP_DWORD(ide.offset);
444 ide.ressize= BYTESWAP_DWORD(ide.ressize);
446 if(ide.offset > rd->size
447 || ide.offset + ide.ressize > rd->size)
448 parser_error("Icon resource data corrupt");
449 ico->width = ide.width;
450 ico->height = ide.height;
451 ico->nclr = ide.nclr;
452 ico->planes = swap ? BYTESWAP_WORD(ide.planes) : ide.planes;
453 ico->bits = swap ? BYTESWAP_WORD(ide.bits) : ide.bits;
454 memcpy(&info, rd->data + ide.offset, sizeof(info));
455 convert_bitmap((char *) &info, 0);
456 memcpy(rd->data + ide.offset, &info, sizeof(info));
460 /* Argh! They did not fill out the resdir structure */
461 /* The bitmap is in destination byteorder. We want native for our structures */
464 #ifdef WORDS_BIGENDIAN
469 ico->planes = BYTESWAP_WORD(info.biPlanes);
472 ico->planes = info.biPlanes;
477 /* Argh! They did not fill out the resdir structure */
478 /* The bitmap is in destination byteorder. We want native for our structures */
481 #ifdef WORDS_BIGENDIAN
486 ico->bits = BYTESWAP_WORD(info.biBitCount);
489 ico->bits = info.biBitCount;
492 ico->data = new_raw_data();
493 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
505 icog->iconlist = list;
509 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
514 cursor_t *list = NULL;
515 cursor_header_t *ch = (cursor_header_t *)rd->data;
520 else if(BYTESWAP_WORD(ch->type) == 2)
523 parser_error("Cursor resource data has invalid type id %d", ch->type);
524 cnt = swap ? BYTESWAP_WORD(ch->count) : ch->count;
525 for(i = 0; i < cnt; i++)
527 cursor_dir_entry_t cde;
528 BITMAPINFOHEADER info;
529 memcpy(&cde, rd->data + sizeof(cursor_header_t)
530 + i*sizeof(cursor_dir_entry_t), sizeof(cde));
533 cur->id = alloc_cursor_id(curg->lvc.language);
534 cur->lvc = curg->lvc;
537 cde.offset = BYTESWAP_DWORD(cde.offset);
538 cde.ressize= BYTESWAP_DWORD(cde.ressize);
540 if(cde.offset > rd->size
541 || cde.offset + cde.ressize > rd->size)
542 parser_error("Cursor resource data corrupt");
543 cur->width = cde.width;
544 cur->height = cde.height;
545 cur->nclr = cde.nclr;
546 memcpy(&info, rd->data + cde.offset, sizeof(info));
547 convert_bitmap((char *)&info, 0);
548 memcpy(rd->data + cde.offset, &info, sizeof(info));
549 /* The bitmap is in destination byteorder. We want native for our structures */
552 #ifdef WORDS_BIGENDIAN
557 cur->planes = BYTESWAP_WORD(info.biPlanes);
558 cur->bits = BYTESWAP_WORD(info.biBitCount);
561 cur->planes = info.biPlanes;
562 cur->bits = info.biBitCount;
564 if(!win32 && (cur->planes != 1 || cur->bits != 1))
565 parser_warning("Win16 cursor contains colors\n");
566 cur->xhot = swap ? BYTESWAP_WORD(cde.xhot) : cde.xhot;
567 cur->yhot = swap ? BYTESWAP_WORD(cde.yhot) : cde.yhot;
568 cur->data = new_raw_data();
569 copy_raw_data(cur->data, rd, cde.offset, cde.ressize);
581 curg->cursorlist = list;
586 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
588 icon_group_t *icog = xmalloc(sizeof(icon_group_t));
591 icog->memopt = *memopt;
595 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
597 split_icons(rd, icog, &(icog->nicon));
603 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
605 cursor_group_t *curg = xmalloc(sizeof(cursor_group_t));
608 curg->memopt = *memopt;
612 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
614 split_cursors(rd, curg, &(curg->ncursor));
621 * Animated cursors and icons
623 * The format of animated cursors and icons is yet another example
624 * of bad design by "The Company". The entire RIFF structure is
625 * flawed by design because it is inconsistent and single minded:
626 * - some tags have lengths attached, others don't. The use of these
627 * non-length tags is absolutely unclear;
628 * - the content of "icon" tags can be both icons and cursors;
629 * - tags lack proper alignment constraints. It seems that everything
630 * is 16bit aligned, but I could not find that in any docu. Just be
631 * prepared to eat anything;
632 * - there are no strict constraints on tag-nesting and the organization
633 * is highly illogical;
635 * Anyhow, here is the basic structure:
636 * "RIFF" { dword taglength }
637 * "ACON" // What does it do?
638 * "LIST" { dword taglength }
639 * "INFO" // And what does this do?
640 * "INAM" { dword taglength } // Icon/cursor name
642 * "IART" { dword taglength } // The artist
644 * "fram" // Is followed by "icon"s
645 * "icon" { dword taglength } // First frame
646 * { icon/cursor data }
647 * "icon" { dword taglength } // Second frame
648 * { icon/cursor data }
650 * "anih" { dword taglength } // Header structure
651 * { aniheader_t structure }
652 * "rate" { dword taglength } // The rate for each frame
654 * "seq " { dword taglength } // The frame blit-order
657 * Tag length are bytelength without the header and length field (i.e. -8).
658 * The "LIST" tag may occur several times and may encapsulate different
659 * tags. The `steps' is the number of "icon" tags found (actually the
660 * number of steps specified in the aniheader_t structure). The "seq "uence
661 * tag can be omitted, in which case the sequence is equal to the sequence
662 * of "icon"s found in the file. Also "rate" may be omitted, in which case
663 * the default from the aniheader_t structure is used.
665 * An animated cursor puts `.cur' formatted files into each "icon" tag,
666 * whereas animated icons contain `.ico' formatted files.
668 * Note about the code: Yes, it can be shorter/compressed. Some tags can be
669 * dealt with in the same code. However, this version shows what is going on
670 * and is better debug-able.
672 static const char riff[4] = "RIFF";
673 static const char acon[4] = "ACON";
674 static const char list[4] = "LIST";
675 static const char info[4] = "INFO";
676 static const char inam[4] = "INAM";
677 static const char iart[4] = "IART";
678 static const char fram[4] = "fram";
679 static const char icon[4] = "icon";
680 static const char anih[4] = "anih";
681 static const char rate[4] = "rate";
682 static const char seq[4] = "seq ";
684 #define SKIP_TAG(p,size) ((riff_tag_t *)(((char *)p) + (size)))
686 #define NEXT_TAG(p) SKIP_TAG(p,(isswapped ? BYTESWAP_DWORD(p->size) : p->size) + sizeof(*p))
688 static void handle_ani_icon(riff_tag_t *rtp, enum res_e type, int isswapped)
690 cursor_dir_entry_t *cdp;
691 cursor_header_t *chp;
695 static int once = 0; /* This will trigger only once per file! */
696 const char *anistr = type == res_aniico ? "icon" : "cursor";
698 * Both cursor and icon directories are similar
699 * Both cursor and icon headers are similar
702 chp = (cursor_header_t *)(rtp+1);
703 cdp = (cursor_dir_entry_t *)(chp+1);
704 count = isswapped ? BYTESWAP_WORD(chp->count) : chp->count;
705 ctype = isswapped ? BYTESWAP_WORD(chp->type) : chp->type;
706 chp->reserved = BYTESWAP_WORD(chp->reserved);
707 chp->type = BYTESWAP_WORD(chp->type);
708 chp->count = BYTESWAP_WORD(chp->count);
710 if(type == res_anicur && ctype != 2 && !once)
712 parser_warning("Animated cursor contains invalid \"icon\" tag cursor-file (%d->%s)\n",
714 ctype == 1 ? "icontype" : "?");
717 else if(type == res_aniico && ctype != 1 && !once)
719 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
721 ctype == 2 ? "cursortype" : "?");
724 else if(ctype != 1 && ctype != 2 && !once)
726 parser_warning("Animated %s contains invalid \"icon\" tag file-type (%d; neither icon nor cursor)\n", anistr, ctype);
730 for(i = 0; i < count; i++)
732 DWORD ofs = isswapped ? BYTESWAP_DWORD(cdp[i].offset) : cdp[i].offset;
733 DWORD sze = isswapped ? BYTESWAP_DWORD(cdp[i].ressize) : cdp[i].ressize;
734 if(ofs > rtp->size || ofs+sze > rtp->size)
735 parser_error("Animated %s's data corrupt", anistr);
736 convert_bitmap((char *)chp + ofs, 0);
737 cdp[i].xhot = BYTESWAP_WORD(cdp->xhot);
738 cdp[i].yhot = BYTESWAP_WORD(cdp->yhot);
739 cdp[i].ressize = BYTESWAP_DWORD(cdp->ressize);
740 cdp[i].offset = BYTESWAP_DWORD(cdp->offset);
744 static void handle_ani_list(riff_tag_t *lst, enum res_e type, int isswapped)
746 riff_tag_t *rtp = lst+1; /* Skip the "LIST" tag */
748 while((char *)rtp < (char *)lst + lst->size + sizeof(*lst))
750 if(!memcmp(rtp->tag, info, sizeof(info)))
752 rtp = SKIP_TAG(rtp,4);
754 else if(!memcmp(rtp->tag, inam, sizeof(inam)))
756 /* Ignore the icon/cursor name; its a string */
759 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
761 /* Ignore the author's name; it's a string */
764 else if(!memcmp(rtp->tag, fram, sizeof(fram)))
766 /* This should be followed by "icon"s, but we
767 * simply ignore this because it is pure
770 rtp = SKIP_TAG(rtp,4);
772 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
774 handle_ani_icon(rtp, type, isswapped);
778 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
779 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
780 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
781 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
782 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
784 if((UINT_PTR)rtp & 1)
785 rtp = SKIP_TAG(rtp,1);
789 ani_curico_t *new_ani_curico(enum res_e type, raw_data_t *rd, int *memopt)
791 ani_curico_t *ani = xmalloc(sizeof(ani_curico_t));
795 const char *anistr = type == res_aniico ? "icon" : "cursor";
797 assert(!memcmp(rd->data, riff, sizeof(riff)));
798 assert(type == res_anicur || type == res_aniico);
800 rtp = (riff_tag_t *)rd->data;
802 if(BYTESWAP_DWORD(rtp->size) + 2*sizeof(DWORD) == rd->size)
804 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
807 parser_error("Animated %s has an invalid RIFF length", anistr);
811 #ifdef WORDS_BIGENDIAN
824 * isswapped | doswap |
825 * ----------+--------+---------------------------------
826 * 0 | 0 | read native; don't convert
827 * 1 | 0 | read swapped size; don't convert
828 * 0 | 1 | read native; convert
829 * 1 | 1 | read swapped size; convert
830 * Reading swapped size if necessary to calculate in native
831 * format. E.g. a little-endian source on a big-endian
836 /* We only go through the RIFF file if we need to swap
837 * bytes in words/dwords. Else we couldn't care less
838 * what the file contains. This is consistent with
839 * MS' rc.exe, which doesn't complain at all, even though
840 * the file format might not be entirely correct.
842 rtp++; /* Skip the "RIFF" tag */
844 while((char *)rtp < (char *)rd->data + rd->size)
846 if(!memcmp(rtp->tag, acon, sizeof(acon)))
848 rtp = SKIP_TAG(rtp,4);
850 else if(!memcmp(rtp->tag, list, sizeof(list)))
852 handle_ani_list(rtp, type, isswapped);
855 else if(!memcmp(rtp->tag, anih, sizeof(anih)))
857 aniheader_t *ahp = (aniheader_t *)((char *)(rtp+1));
858 ahp->structsize = BYTESWAP_DWORD(ahp->structsize);
859 ahp->frames = BYTESWAP_DWORD(ahp->frames);
860 ahp->steps = BYTESWAP_DWORD(ahp->steps);
861 ahp->cx = BYTESWAP_DWORD(ahp->cx);
862 ahp->cy = BYTESWAP_DWORD(ahp->cy);
863 ahp->bitcount = BYTESWAP_DWORD(ahp->bitcount);
864 ahp->planes = BYTESWAP_DWORD(ahp->planes);
865 ahp->rate = BYTESWAP_DWORD(ahp->rate);
866 ahp->flags = BYTESWAP_DWORD(ahp->flags);
869 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
871 int cnt = rtp->size / sizeof(DWORD);
872 DWORD *dwp = (DWORD *)(rtp+1);
874 for(i = 0; i < cnt; i++)
875 dwp[i] = BYTESWAP_DWORD(dwp[i]);
878 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
880 int cnt = rtp->size / sizeof(DWORD);
881 DWORD *dwp = (DWORD *)(rtp+1);
883 for(i = 0; i < cnt; i++)
884 dwp[i] = BYTESWAP_DWORD(dwp[i]);
888 internal_error(__FILE__, __LINE__, "Unknown tag \"%c%c%c%c\" in RIFF file\n",
889 isprint(rtp->tag[0]) ? rtp->tag[0] : '.',
890 isprint(rtp->tag[1]) ? rtp->tag[1] : '.',
891 isprint(rtp->tag[2]) ? rtp->tag[2] : '.',
892 isprint(rtp->tag[3]) ? rtp->tag[3] : '.');
894 if((UINT_PTR)rtp & 1)
895 rtp = SKIP_TAG(rtp,1);
898 /* We must end correctly here */
899 if((char *)rtp != (char *)rd->data + rd->size)
900 parser_error("Animated %s contains invalid field size(s)", anistr);
906 ani->memopt = *memopt;
910 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
916 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
918 bitmap_t *bmp = xmalloc(sizeof(bitmap_t));
923 bmp->memopt = *memopt;
927 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
928 rd->size -= convert_bitmap(rd->data, rd->size);
932 ver_words_t *new_ver_words(int i)
934 ver_words_t *w = xmalloc(sizeof(ver_words_t));
935 w->words = xmalloc(sizeof(WORD));
936 w->words[0] = (WORD)i;
941 ver_words_t *add_ver_words(ver_words_t *w, int i)
943 w->words = xrealloc(w->words, (w->nwords+1) * sizeof(WORD));
944 w->words[w->nwords] = (WORD)i;
949 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
950 messagetable_t *new_messagetable(raw_data_t *rd, int *memopt)
952 messagetable_t *msg = xmalloc(sizeof(messagetable_t));
962 msg->memopt = *memopt;
966 msg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
968 if(rd->size < sizeof(DWORD))
969 parser_error("Invalid messagetable, size too small");
971 nblk = *(DWORD *)rd->data;
972 lo = WRC_LOWORD(nblk);
973 hi = WRC_HIWORD(nblk);
976 * This test will fail for all n*2^16 blocks in the messagetable.
977 * However, no sane person would want to have so many blocks
978 * and have a table of megabytes attached.
979 * So, I will assume that we have less than 2^16 blocks in the table
980 * and all will just work out fine. Otherwise, we would need to test
981 * the ID, offset and length (and flag) fields to be very sure.
984 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian\n");
986 parser_error("Invalid messagetable block count 0");
988 if(!hi && lo) /* Messagetable byteorder == native byteorder */
990 #ifdef WORDS_BIGENDIAN
991 if(byteorder != WRC_BO_LITTLE) goto out;
993 if(byteorder != WRC_BO_BIG) goto out;
995 /* Resource byteorder != native byteorder */
997 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
998 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
999 parser_error("Messagetable's blocks are outside of defined data");
1000 for(i = 0; i < nblk; i++)
1002 msgtab_entry_t *mep, *next_mep;
1005 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1007 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1009 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1010 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1011 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1013 WORD *wp = (WORD *)&mep[1];
1014 int l = mep->length/2 - 2; /* Length included header */
1018 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1019 for(n = 0; n < l; n++)
1020 wp[n] = BYTESWAP_WORD(wp[n]);
1023 next_mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1024 mep->length = BYTESWAP_WORD(mep->length);
1025 mep->flags = BYTESWAP_WORD(mep->flags);
1029 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1030 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1031 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1034 if(hi && !lo) /* Messagetable byteorder != native byteorder */
1036 #ifdef WORDS_BIGENDIAN
1037 if(byteorder == WRC_BO_LITTLE) goto out;
1039 if(byteorder == WRC_BO_BIG) goto out;
1041 /* Resource byteorder == native byteorder */
1043 mbp = (msgtab_block_t *)&(((DWORD *)rd->data)[1]);
1044 nblk = BYTESWAP_DWORD(nblk);
1045 if(MSGTAB_BAD_PTR(mbp, rd->data, rd->size, nblk * sizeof(*mbp)))
1046 parser_error("Messagetable's blocks are outside of defined data");
1047 for(i = 0; i < nblk; i++)
1049 msgtab_entry_t *mep;
1052 mbp[i].idlo = BYTESWAP_DWORD(mbp[i].idlo);
1053 mbp[i].idhi = BYTESWAP_DWORD(mbp[i].idhi);
1054 mbp[i].offset = BYTESWAP_DWORD(mbp[i].offset);
1055 mep = (msgtab_entry_t *)(((char *)rd->data) + mbp[i].offset);
1057 for(id = mbp[i].idlo; id <= mbp[i].idhi; id++)
1059 mep->length = BYTESWAP_WORD(mep->length);
1060 mep->flags = BYTESWAP_WORD(mep->flags);
1062 if(MSGTAB_BAD_PTR(mep, rd->data, rd->size, mep->length))
1063 parser_error("Messagetable's data for block %d, ID 0x%08x is outside of defined data", i, id);
1064 if(mep->flags == 1) /* Docu says 'flags == 0x0001' for unicode */
1066 WORD *wp = (WORD *)&mep[1];
1067 int l = mep->length/2 - 2; /* Length included header */
1071 parser_error("Message 0x%08x is unicode (block %d), but has odd length (%d)", id, i, mep->length);
1072 for(n = 0; n < l; n++)
1073 wp[n] = BYTESWAP_WORD(wp[n]);
1076 mep = (msgtab_entry_t *)(((char *)mep) + mep->length);
1084 #undef MSGTAB_BAD_PTR
1086 void copy_raw_data(raw_data_t *dst, raw_data_t *src, unsigned int offs, int len)
1088 assert(offs <= src->size);
1089 assert(offs + len <= src->size);
1092 dst->data = xmalloc(len);
1096 dst->data = xrealloc(dst->data, dst->size + len);
1097 /* dst->size holds the offset to copy to */
1098 memcpy(dst->data + dst->size, src->data + offs, len);
1104 int *ip = xmalloc(sizeof(int));
1109 stringtable_t *new_stringtable(lvc_t *lvc)
1111 stringtable_t *stt = xmalloc(sizeof(stringtable_t));
1113 memset( stt, 0, sizeof(*stt) );
1120 toolbar_t *new_toolbar(int button_width, int button_height, toolbar_item_t *items, int nitems)
1122 toolbar_t *tb = xmalloc(sizeof(toolbar_t));
1123 memset( tb, 0, sizeof(*tb) );
1124 tb->button_width = button_width;
1125 tb->button_height = button_height;
1126 tb->nitems = nitems;
1131 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1133 dlginit_t *di = xmalloc(sizeof(dlginit_t));
1137 di->memopt = *memopt;
1141 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1146 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1148 style_pair_t *sp = xmalloc(sizeof(style_pair_t));
1150 sp->exstyle = exstyle;
1154 style_t *new_style(DWORD or_mask, DWORD and_mask)
1156 style_t *st = xmalloc(sizeof(style_t));
1157 st->or_mask = or_mask;
1158 st->and_mask = and_mask;