mshtml: Updated Portuguese (Brazilian) translation.
[wine/multimedia.git] / tools / wrc / newstruc.c
blob603eb0c4dc40f6fddddcf64500bc7819517cc5f8
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(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;
262 DWORD bmsize;
263 int type = 0;
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
289 type |= FL_SIZEBE;
290 #endif
291 bmsize = BYTESWAP_DWORD( bmsize );
293 else
295 #ifdef WORDS_BIGENDIAN
296 type |= FL_SIZEBE;
297 #endif
300 switch (bmsize)
302 case sizeof(BITMAPOS2HEADER):
303 case sizeof(BITMAPINFOHEADER):
304 case sizeof(BITMAPV4HEADER):
305 case sizeof(BITMAPV5HEADER):
306 break;
307 default:
308 parser_error("Invalid bitmap format, bih->biSize = %d", bih->bV5Size);
311 switch(type)
313 case FL_SIZEBE:
314 parser_warning("Bitmap signature little-endian, but size big-endian\n");
315 break;
316 case FL_SIGBE:
317 parser_warning("Bitmap signature big-endian, but size little-endian\n");
318 break;
321 switch(byteorder)
323 #ifdef WORDS_BIGENDIAN
324 default:
325 #endif
326 case WRC_BO_BIG:
327 if(!(type & FL_SIZEBE))
329 if (bmsize == sizeof(BITMAPOS2HEADER))
330 convert_bitmap_swap_os2(boh);
331 else
332 convert_bitmap_swap(bih, bmsize);
334 break;
335 #ifndef WORDS_BIGENDIAN
336 default:
337 #endif
338 case WRC_BO_LITTLE:
339 if(type & FL_SIZEBE)
341 if (bmsize == sizeof(BITMAPOS2HEADER))
342 convert_bitmap_swap_os2(boh);
343 else
344 convert_bitmap_swap(bih, bmsize);
346 break;
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);
355 return returnSize;
357 #undef FL_SIGBE
358 #undef FL_SIZEBE
361 * Cursor and icon splitter functions used when allocating
362 * cursor- and icon-groups.
364 typedef struct {
365 language_t lan;
366 int id;
367 } id_alloc_t;
369 static int get_new_id(id_alloc_t **list, int *n, language_t *lan)
371 int i;
372 assert(lan != NULL);
373 assert(list != NULL);
374 assert(n != NULL);
376 if(!*list)
378 *list = xmalloc(sizeof(id_alloc_t));
379 *n = 1;
380 (*list)[0].lan = *lan;
381 (*list)[0].id = 1;
382 return 1;
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;
393 (*list)[*n].id = 1;
394 *n += 1;
395 return 1;
398 static int alloc_icon_id(language_t *lan)
400 static id_alloc_t *idlist = NULL;
401 static int nid = 0;
403 return get_new_id(&idlist, &nid, lan);
406 static int alloc_cursor_id(language_t *lan)
408 static id_alloc_t *idlist = NULL;
409 static int nid = 0;
411 return get_new_id(&idlist, &nid, lan);
414 static void split_icons(raw_data_t *rd, icon_group_t *icog, int *nico)
416 int cnt;
417 int i;
418 icon_t *ico;
419 icon_t *list = NULL;
420 icon_header_t *ih = (icon_header_t *)rd->data;
421 int swap = 0;
423 if(ih->type == 1)
424 swap = 0;
425 else if(BYTESWAP_WORD(ih->type) == 1)
426 swap = 1;
427 else
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));
438 ico = new_icon();
439 ico->id = alloc_icon_id(icog->lvc.language);
440 ico->lvc = icog->lvc;
441 if(swap)
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));
458 if(!ico->planes)
460 /* Argh! They did not fill out the resdir structure */
461 /* The bitmap is in destination byteorder. We want native for our structures */
462 switch(byteorder)
464 #ifdef WORDS_BIGENDIAN
465 case WRC_BO_LITTLE:
466 #else
467 case WRC_BO_BIG:
468 #endif
469 ico->planes = BYTESWAP_WORD(info.biPlanes);
470 break;
471 default:
472 ico->planes = info.biPlanes;
475 if(!ico->bits)
477 /* Argh! They did not fill out the resdir structure */
478 /* The bitmap is in destination byteorder. We want native for our structures */
479 switch(byteorder)
481 #ifdef WORDS_BIGENDIAN
482 case WRC_BO_LITTLE:
483 #else
484 case WRC_BO_BIG:
485 #endif
486 ico->bits = BYTESWAP_WORD(info.biBitCount);
487 break;
488 default:
489 ico->bits = info.biBitCount;
492 ico->data = new_raw_data();
493 copy_raw_data(ico->data, rd, ide.offset, ide.ressize);
494 if(!list)
496 list = ico;
498 else
500 ico->next = list;
501 list->prev = ico;
502 list = ico;
505 icog->iconlist = list;
506 *nico = cnt;
509 static void split_cursors(raw_data_t *rd, cursor_group_t *curg, int *ncur)
511 int cnt;
512 int i;
513 cursor_t *cur;
514 cursor_t *list = NULL;
515 cursor_header_t *ch = (cursor_header_t *)rd->data;
516 int swap = 0;
518 if(ch->type == 2)
519 swap = 0;
520 else if(BYTESWAP_WORD(ch->type) == 2)
521 swap = 1;
522 else
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));
532 cur = new_cursor();
533 cur->id = alloc_cursor_id(curg->lvc.language);
534 cur->lvc = curg->lvc;
535 if(swap)
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 */
550 switch(byteorder)
552 #ifdef WORDS_BIGENDIAN
553 case WRC_BO_LITTLE:
554 #else
555 case WRC_BO_BIG:
556 #endif
557 cur->planes = BYTESWAP_WORD(info.biPlanes);
558 cur->bits = BYTESWAP_WORD(info.biBitCount);
559 break;
560 default:
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);
570 if(!list)
572 list = cur;
574 else
576 cur->next = list;
577 list->prev = cur;
578 list = cur;
581 curg->cursorlist = list;
582 *ncur = cnt;
586 icon_group_t *new_icon_group(raw_data_t *rd, int *memopt)
588 icon_group_t *icog = xmalloc(sizeof(icon_group_t));
589 if(memopt)
591 icog->memopt = *memopt;
592 free(memopt);
594 else
595 icog->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
596 icog->lvc = rd->lvc;
597 split_icons(rd, icog, &(icog->nicon));
598 free(rd->data);
599 free(rd);
600 return icog;
603 cursor_group_t *new_cursor_group(raw_data_t *rd, int *memopt)
605 cursor_group_t *curg = xmalloc(sizeof(cursor_group_t));
606 if(memopt)
608 curg->memopt = *memopt;
609 free(memopt);
611 else
612 curg->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
613 curg->lvc = rd->lvc;
614 split_cursors(rd, curg, &(curg->ncursor));
615 free(rd->data);
616 free(rd);
617 return curg;
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
641 * {inam data}
642 * "IART" { dword taglength } // The artist
643 * {iart data}
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 }
649 * ... // ...
650 * "anih" { dword taglength } // Header structure
651 * { aniheader_t structure }
652 * "rate" { dword taglength } // The rate for each frame
653 * { `steps' dwords }
654 * "seq " { dword taglength } // The frame blit-order
655 * { `steps' dwords }
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;
692 int count;
693 int ctype;
694 int i;
695 static int once = 0; /* This will trigger only once per file! */
696 const char *anistr = type == res_aniico ? "icon" : "cursor";
697 /* Notes:
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",
713 ctype,
714 ctype == 1 ? "icontype" : "?");
715 once++;
717 else if(type == res_aniico && ctype != 1 && !once)
719 parser_warning("Animated icon contains invalid \"icon\" tag icon-file (%d->%s)\n",
720 ctype,
721 ctype == 2 ? "cursortype" : "?");
722 once++;
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);
727 once++;
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 */
757 rtp = NEXT_TAG(rtp);
759 else if(!memcmp(rtp->tag, iart, sizeof(iart)))
761 /* Ignore the author's name; it's a string */
762 rtp = NEXT_TAG(rtp);
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
768 * non-information.
770 rtp = SKIP_TAG(rtp,4);
772 else if(!memcmp(rtp->tag, icon, sizeof(icon)))
774 handle_ani_icon(rtp, type, isswapped);
775 rtp = NEXT_TAG(rtp);
777 else
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));
792 riff_tag_t *rtp;
793 int isswapped = 0;
794 int doswap;
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)
803 isswapped = 1;
804 else if(rtp->size + 2*sizeof(DWORD) == rd->size)
805 isswapped = 0;
806 else
807 parser_error("Animated %s has an invalid RIFF length", anistr);
809 switch(byteorder)
811 #ifdef WORDS_BIGENDIAN
812 case WRC_BO_LITTLE:
813 #else
814 case WRC_BO_BIG:
815 #endif
816 doswap = !isswapped;
817 break;
818 default:
819 doswap = isswapped;
823 * When to swap what:
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
832 * processor.
834 if(doswap)
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);
853 rtp = NEXT_TAG(rtp);
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);
867 rtp = NEXT_TAG(rtp);
869 else if(!memcmp(rtp->tag, rate, sizeof(rate)))
871 int cnt = rtp->size / sizeof(DWORD);
872 DWORD *dwp = (DWORD *)(rtp+1);
873 int i;
874 for(i = 0; i < cnt; i++)
875 dwp[i] = BYTESWAP_DWORD(dwp[i]);
876 rtp = NEXT_TAG(rtp);
878 else if(!memcmp(rtp->tag, seq, sizeof(seq)))
880 int cnt = rtp->size / sizeof(DWORD);
881 DWORD *dwp = (DWORD *)(rtp+1);
882 int i;
883 for(i = 0; i < cnt; i++)
884 dwp[i] = BYTESWAP_DWORD(dwp[i]);
885 rtp = NEXT_TAG(rtp);
887 else
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);
903 ani->data = rd;
904 if(memopt)
906 ani->memopt = *memopt;
907 free(memopt);
909 else
910 ani->memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE;
911 return ani;
913 #undef NEXT_TAG
915 /* Bitmaps */
916 bitmap_t *new_bitmap(raw_data_t *rd, int *memopt)
918 bitmap_t *bmp = xmalloc(sizeof(bitmap_t));
920 bmp->data = rd;
921 if(memopt)
923 bmp->memopt = *memopt;
924 free(memopt);
926 else
927 bmp->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
928 rd->size -= convert_bitmap(rd->data, rd->size);
929 return bmp;
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;
937 w->nwords = 1;
938 return w;
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;
945 w->nwords++;
946 return w;
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));
953 msgtab_block_t *mbp;
954 DWORD nblk;
955 DWORD i;
956 WORD lo;
957 WORD hi;
959 msg->data = rd;
960 if(memopt)
962 msg->memopt = *memopt;
963 free(memopt);
965 else
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);
975 /* FIXME:
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.
983 if(hi && lo)
984 internal_error(__FILE__, __LINE__, "Messagetable contains more than 65535 blocks; cannot determine endian\n");
985 if(!hi && !lo)
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;
992 #else
993 if(byteorder != WRC_BO_BIG) goto out;
994 #endif
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;
1003 DWORD id;
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 */
1015 int n;
1017 if(mep->length & 1)
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);
1026 mep = next_mep;
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;
1038 #else
1039 if(byteorder == WRC_BO_BIG) goto out;
1040 #endif
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;
1050 DWORD id;
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 */
1068 int n;
1070 if(mep->length & 1)
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);
1081 out:
1082 return msg;
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);
1090 if(!dst->data)
1092 dst->data = xmalloc(len);
1093 dst->size = 0;
1095 else
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);
1099 dst->size += len;
1102 int *new_int(int i)
1104 int *ip = xmalloc(sizeof(int));
1105 *ip = i;
1106 return ip;
1109 stringtable_t *new_stringtable(lvc_t *lvc)
1111 stringtable_t *stt = xmalloc(sizeof(stringtable_t));
1113 memset( stt, 0, sizeof(*stt) );
1114 if(lvc)
1115 stt->lvc = *lvc;
1117 return 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;
1127 tb->items = items;
1128 return tb;
1131 dlginit_t *new_dlginit(raw_data_t *rd, int *memopt)
1133 dlginit_t *di = xmalloc(sizeof(dlginit_t));
1134 di->data = rd;
1135 if(memopt)
1137 di->memopt = *memopt;
1138 free(memopt);
1140 else
1141 di->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1143 return di;
1146 style_pair_t *new_style_pair(style_t *style, style_t *exstyle)
1148 style_pair_t *sp = xmalloc(sizeof(style_pair_t));
1149 sp->style = style;
1150 sp->exstyle = exstyle;
1151 return sp;
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;
1159 return st;