2 * Generate .res format from a resource-tree
4 * Copyright 1998 Bertho A. Stultiens
6 * 05-May-2000 BS - Added code to support endian conversions. The
7 * extra functions also aid unaligned access, but
8 * this is not yet implemented.
9 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
10 * names in .s and .h files.
28 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
33 r = (res_t *)xmalloc(sizeof(res_t));
34 r->allocsize = RES_BLOCKSIZE;
36 r->data = (char *)xmalloc(RES_BLOCKSIZE);
40 res_t *grow_res(res_t *r, int add)
43 r->data = (char *)xrealloc(r->data, r->allocsize);
48 *****************************************************************************
52 * Syntax : void put_byte(res_t *res, unsigned c)
53 * void put_word(res_t *res, unsigned w)
54 * void put_dword(res_t *res, unsigned d)
56 * res - Binary resource to put the data in
57 * c, w, d - Data to put
59 * Description : Put primitives that put an item in the binary resource.
60 * The data array grows automatically.
62 *****************************************************************************
64 void put_byte(res_t *res, unsigned c)
66 if(res->allocsize - res->size < sizeof(char))
67 grow_res(res, RES_BLOCKSIZE);
68 *(char *)&(res->data[res->size]) = (char)c;
69 res->size += sizeof(char);
72 void put_word(res_t *res, unsigned w)
74 if(res->allocsize - res->size < sizeof(WORD))
75 grow_res(res, RES_BLOCKSIZE);
78 #ifndef WORDS_BIGENDIAN
83 *(WORD *)&(res->data[res->size]) = BYTESWAP_WORD((WORD)w);
86 *(WORD *)&(res->data[res->size]) = (WORD)w;
89 res->size += sizeof(WORD);
92 void put_dword(res_t *res, unsigned d)
94 if(res->allocsize - res->size < sizeof(DWORD))
95 grow_res(res, RES_BLOCKSIZE);
98 #ifndef WORDS_BIGENDIAN
103 *(DWORD *)&(res->data[res->size]) = BYTESWAP_DWORD((DWORD)d);
106 *(DWORD *)&(res->data[res->size]) = (DWORD)d;
109 res->size += sizeof(DWORD);
112 void put_pad(res_t *res)
114 while(res->size & 0x3)
119 *****************************************************************************
120 * Function : set_word
122 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
123 * void set_dword(res_t *res, int ofs, unsigned d)
125 * res - Binary resource to put the data in
126 * ofs - Byte offset in data-array
129 * Description : Set the value of a binary resource data array to a
132 *****************************************************************************
134 void set_word(res_t *res, int ofs, unsigned w)
138 #ifndef WORDS_BIGENDIAN
143 *(WORD *)&(res->data[ofs]) = BYTESWAP_WORD((WORD)w);
146 *(WORD *)&(res->data[ofs]) = (WORD)w;
151 void set_dword(res_t *res, int ofs, unsigned d)
155 #ifndef WORDS_BIGENDIAN
160 *(DWORD *)&(res->data[ofs]) = BYTESWAP_DWORD((DWORD)d);
163 *(DWORD *)&(res->data[ofs]) = (DWORD)d;
169 *****************************************************************************
170 * Function : get_word
172 * Syntax : WORD get_word(res_t *res, int ofs)
173 * DWORD get_dword(res_t *res, int ofs)
175 * res - Binary resource to put the data in
176 * ofs - Byte offset in data-array
177 * Output : The data in native endian
178 * Description : Get the value of a binary resource data array in native
181 *****************************************************************************
183 WORD get_word(res_t *res, int ofs)
187 #ifndef WORDS_BIGENDIAN
192 return BYTESWAP_WORD(*(WORD *)&(res->data[ofs]));
194 return *(WORD *)&(res->data[ofs]);
198 DWORD get_dword(res_t *res, int ofs)
202 #ifndef WORDS_BIGENDIAN
207 return BYTESWAP_DWORD(*(DWORD *)&(res->data[ofs]));
209 return *(DWORD *)&(res->data[ofs]);
214 *****************************************************************************
215 * Function : string_to_upper
216 * Syntax : void string_to_upper(string_t *str)
220 * Remarks : FIXME: codepages...
221 *****************************************************************************
223 void string_to_upper(string_t *str)
225 if(str->type == str_char)
227 char *cptr = str->str.cstr;
229 *cptr = (char)toupper(*cptr);
231 else if(str->type == str_unicode)
233 short *sptr = str->str.wstr;
236 *sptr = (short)toupper(*sptr);
240 internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
245 *****************************************************************************
246 * Function : put_string
247 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
250 * res - Binary resource to put the data in
251 * str - String to put
252 * type - Data has to be written in either str_char or str_unicode
253 * isterm - The string is '\0' terminated (disregard the string's
258 *****************************************************************************
260 static void put_string(res_t *res, string_t *str, enum str_e type, int isterm)
266 if(!isterm && str->size == 0)
268 warning("String length is zero, not written");
272 str = convert_string(str, type);
273 if(str->type == str_unicode && type == str_unicode)
275 for(cnt = 0; cnt < str->size; cnt++)
277 c = str->str.wstr[cnt];
282 if(isterm && (str->size == 0 || (cnt == str->size && c)))
285 else if(str->type == str_char && type == str_char)
287 for(cnt = 0; cnt < str->size; cnt++)
289 c = str->str.cstr[cnt];
294 if(isterm && (str->size == 0 || (cnt == str->size && c)))
297 else if(str->type == str_unicode && type == str_char)
299 for(cnt = 0; cnt < str->size; cnt++)
301 c = str->str.wstr[cnt];
306 if(isterm && (str->size == 0 || (cnt == str->size && c)))
309 else /* str->type == str_char && type == str_unicode */
311 for(cnt = 0; cnt < str->size; cnt++)
313 c = str->str.cstr[cnt];
314 put_word(res, c & 0xff);
318 if(isterm && (str->size == 0 || (cnt == str->size && c)))
325 *****************************************************************************
326 * Function : put_name_id
327 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase)
332 *****************************************************************************
334 void put_name_id(res_t *res, name_id_t *nid, int upcase)
336 if(nid->type == name_ord)
339 put_word(res, 0xffff);
342 put_word(res, (WORD)nid->name.i_name);
344 else if(nid->type == name_str)
347 string_to_upper(nid->name.s_name);
348 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE);
352 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
357 *****************************************************************************
359 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
364 *****************************************************************************
366 void put_lvc(res_t *res, lvc_t *lvc)
368 if(lvc && lvc->language)
369 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
371 put_word(res, 0); /* Neutral */
372 if(lvc && lvc->version)
373 put_dword(res, *(lvc->version));
376 if(lvc && lvc->characts)
377 put_dword(res, *(lvc->characts));
380 if(lvc && lvc->language)
381 set_language( lvc->language->id, lvc->language->sub );
382 else if (currentlanguage)
383 set_language( currentlanguage->id, currentlanguage->sub );
385 set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
389 *****************************************************************************
390 * Function : put_raw_data
391 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
396 *****************************************************************************
398 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
400 int wsize = raw->size - offset;
401 if(res->allocsize - res->size < wsize)
402 grow_res(res, wsize);
403 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
408 *****************************************************************************
409 * Function : put_res_header
410 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
411 * name_id_t *name, DWORD memopt, lvc_t *lvc)
414 * res - Binary resource descriptor to write to
415 * type - Resource identifier (if ntype == NULL)
416 * ntype - Name id of type
417 * name - Resource's name
418 * memopt - Resource's memory options to write
419 * lvc - Language, version and characteristics (win32 only)
420 * Output : An index to the resource size field. The resource size field
421 * contains the header size upon exit.
424 *****************************************************************************
426 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
427 DWORD memopt, lvc_t *lvc)
431 put_dword(res, 0); /* We will overwrite these later */
435 put_word(res, 0xffff); /* ResType */
439 put_name_id(res, ntype, TRUE);
440 put_name_id(res, name, TRUE); /* ResName */
442 put_dword(res, 0); /* DataVersion */
443 put_word(res, memopt); /* Memory options */
444 put_lvc(res, lvc); /* Language, version and characts */
445 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
446 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
447 res->dataidx = res->size;
455 put_byte(res, 0xff); /* ResType */
459 put_name_id(res, ntype, TRUE);
460 put_name_id(res, name, TRUE); /* ResName */
461 put_word(res, memopt); /* Memory options */
463 put_dword(res, 0); /* ResSize overwritten later*/
464 set_dword(res, tag, res->size);
465 res->dataidx = res->size;
471 *****************************************************************************
472 * Function : accelerator2res
473 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
475 * name - Name/ordinal of the resource
476 * acc - The accelerator descriptor
477 * Output : New .res format structure
480 *****************************************************************************
482 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
487 assert(name != NULL);
494 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
497 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
498 put_word(res, ev->key);
499 put_word(res, ev->id);
500 put_word(res, 0); /* Padding */
507 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
510 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
511 put_word(res, ev->key);
512 put_word(res, ev->id);
516 /* Set ResourceSize */
517 SetResSize(res, restag);
522 *****************************************************************************
523 * Function : dialog2res
524 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
526 * name - Name/ordinal of the resource
527 * dlg - The dialog descriptor
528 * Output : New .res format structure
531 *****************************************************************************
533 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
540 assert(name != NULL);
543 ctrl = dlg->controls;
547 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
549 put_dword(res, dlg->style->or_mask);
550 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
551 tag_nctrl = res->size;
552 put_word(res, 0); /* Number of controls */
553 put_word(res, dlg->x);
554 put_word(res, dlg->y);
555 put_word(res, dlg->width);
556 put_word(res, dlg->height);
558 put_name_id(res, dlg->menu, TRUE);
562 put_name_id(res, dlg->dlgclass, TRUE);
566 put_string(res, dlg->title, str_unicode, TRUE);
571 put_word(res, dlg->font->size);
572 put_string(res, dlg->font->name, str_unicode, TRUE);
578 /* FIXME: what is default control style? */
579 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
580 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
581 put_word(res, ctrl->x);
582 put_word(res, ctrl->y);
583 put_word(res, ctrl->width);
584 put_word(res, ctrl->height);
585 put_word(res, ctrl->id);
587 put_name_id(res, ctrl->ctlclass, TRUE);
589 internal_error(__FILE__, __LINE__, "Control has no control-class");
591 put_name_id(res, ctrl->title, FALSE);
596 put_word(res, ctrl->extra->size+2);
598 put_raw_data(res, ctrl->extra, 0);
608 /* Set number of controls */
609 set_word(res, tag_nctrl, (WORD)nctrl);
613 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
615 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
616 tag_nctrl = res->size;
617 put_byte(res, 0); /* Number of controls */
618 put_word(res, dlg->x);
619 put_word(res, dlg->y);
620 put_word(res, dlg->width);
621 put_word(res, dlg->height);
623 put_name_id(res, dlg->menu, TRUE);
627 put_name_id(res, dlg->dlgclass, TRUE);
631 put_string(res, dlg->title, str_char, TRUE);
636 put_word(res, dlg->font->size);
637 put_string(res, dlg->font->name, str_char, TRUE);
642 put_word(res, ctrl->x);
643 put_word(res, ctrl->y);
644 put_word(res, ctrl->width);
645 put_word(res, ctrl->height);
646 put_word(res, ctrl->id);
647 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
650 if(ctrl->ctlclass->type == name_ord
651 && ctrl->ctlclass->name.i_name >= 0x80
652 && ctrl->ctlclass->name.i_name <= 0x85)
653 put_byte(res, ctrl->ctlclass->name.i_name);
654 else if(ctrl->ctlclass->type == name_str)
655 put_name_id(res, ctrl->ctlclass, FALSE);
657 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
660 internal_error(__FILE__, __LINE__, "Control has no control-class");
662 put_name_id(res, ctrl->title, FALSE);
666 /* FIXME: What is this extra byte doing here? */
672 /* Set number of controls */
673 ((char *)res->data)[tag_nctrl] = (char)nctrl;
675 /* Set ResourceSize */
676 SetResSize(res, restag);
681 *****************************************************************************
682 * Function : dialogex2res
683 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
685 * name - Name/ordinal of the resource
686 * dlgex - The dialogex descriptor
687 * Output : New .res format structure
690 *****************************************************************************
692 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
699 assert(name != NULL);
700 assert(dlgex != NULL);
702 ctrl = dlgex->controls;
706 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
708 /* FIXME: MS doc says thet the first word must contain 0xffff
709 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
710 * compiler reverses the two words.
711 * I don't know which one to choose, but I write it as Mr. B
714 put_word(res, 1); /* Signature */
715 put_word(res, 0xffff); /* DlgVer */
716 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
717 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
718 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
719 tag_nctrl = res->size;
720 put_word(res, 0); /* Number of controls */
721 put_word(res, dlgex->x);
722 put_word(res, dlgex->y);
723 put_word(res, dlgex->width);
724 put_word(res, dlgex->height);
726 put_name_id(res, dlgex->menu, TRUE);
730 put_name_id(res, dlgex->dlgclass, TRUE);
734 put_string(res, dlgex->title, str_unicode, TRUE);
739 put_word(res, dlgex->font->size);
740 put_word(res, dlgex->font->weight);
741 /* FIXME: ? TRUE should be sufficient to say that its
742 * italic, but Borland's compiler says its 0x0101.
743 * I just copy it here, and hope for the best.
745 put_word(res, dlgex->font->italic ? 0x0101 : 0);
746 put_string(res, dlgex->font->name, str_unicode, TRUE);
752 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
753 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
754 /* FIXME: what is default control style? */
755 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
756 put_word(res, ctrl->x);
757 put_word(res, ctrl->y);
758 put_word(res, ctrl->width);
759 put_word(res, ctrl->height);
760 put_word(res, ctrl->id);
761 /* FIXME: Pad is _NOT_ documented!?! */
764 put_name_id(res, ctrl->ctlclass, TRUE);
766 internal_error(__FILE__, __LINE__, "Control has no control-class");
768 put_name_id(res, ctrl->title, FALSE);
774 put_word(res, ctrl->extra->size);
775 put_raw_data(res, ctrl->extra, 0);
784 /* Set number of controls */
785 set_word(res, tag_nctrl, (WORD)nctrl);
786 /* Set ResourceSize */
787 SetResSize(res, restag);
792 /* Do not generate anything in 16-bit mode */
801 *****************************************************************************
802 * Function : menuitem2res
803 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
807 * Remarks : Self recursive
808 *****************************************************************************
810 static void menuitem2res(res_t *res, menu_item_t *menitem)
812 menu_item_t *itm = menitem;
817 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
819 put_word(res, itm->id);
821 put_string(res, itm->name, str_unicode, TRUE);
825 menuitem2res(res, itm->popup);
833 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
835 put_word(res, itm->id);
837 put_string(res, itm->name, str_char, TRUE);
841 menuitem2res(res, itm->popup);
849 *****************************************************************************
850 * Function : menu2res
851 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
853 * name - Name/ordinal of the resource
854 * men - The menu descriptor
855 * Output : New .res format structure
858 *****************************************************************************
860 static res_t *menu2res(name_id_t *name, menu_t *men)
864 assert(name != NULL);
868 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
870 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
871 menuitem2res(res, men->items);
872 /* Set ResourceSize */
873 SetResSize(res, restag);
880 *****************************************************************************
881 * Function : menuexitem2res
882 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
886 * Remarks : Self recursive
887 *****************************************************************************
889 static void menuexitem2res(res_t *res, menuex_item_t *menitem)
891 menuex_item_t *itm = menitem;
895 put_dword(res, itm->gottype ? itm->type : 0);
896 put_dword(res, itm->gotstate ? itm->state : 0);
897 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
898 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
900 put_string(res, itm->name, str_unicode, TRUE);
906 put_dword(res, itm->gothelpid ? itm->helpid : 0);
907 menuexitem2res(res, itm->popup);
915 *****************************************************************************
916 * Function : menuex2res
917 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
919 * name - Name/ordinal of the resource
920 * menex - The menuex descriptor
921 * Output : New .res format structure
924 *****************************************************************************
926 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
930 assert(name != NULL);
931 assert(menex != NULL);
936 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
938 put_word(res, 1); /* Menuheader: Version */
939 put_word(res, 4); /* Offset */
940 put_dword(res, 0); /* HelpId */
942 menuexitem2res(res, menex->items);
943 /* Set ResourceSize */
944 SetResSize(res, restag);
949 /* Do not generate anything in 16-bit mode */
958 *****************************************************************************
959 * Function : cursorgroup2res
960 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
962 * name - Name/ordinal of the resource
963 * curg - The cursor descriptor
964 * Output : New .res format structure
967 *****************************************************************************
969 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
974 assert(name != NULL);
975 assert(curg != NULL);
978 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
981 put_word(res, 0); /* Reserved */
982 /* FIXME: The ResType in the NEWHEADER structure should
983 * contain 14 according to the MS win32 doc. This is
984 * not the case with the BRC compiler and I really doubt
985 * the latter. Putting one here is compliant to win16 spec,
986 * but who knows the true value?
988 put_word(res, 2); /* ResType */
989 put_word(res, curg->ncursor);
991 for(cur = curg->cursorlist; cur; cur = cur->next)
993 cur = curg->cursorlist;
996 for(; cur; cur = cur->prev)
999 put_word(res, cur->width);
1000 /* FIXME: The height of a cursor is half the size of
1001 * the bitmap's height. BRC puts the height from the
1002 * BITMAPINFOHEADER here instead of the cursorfile's
1003 * height. MS doesn't seem to care...
1005 put_word(res, cur->height);
1006 /* FIXME: The next two are reversed in BRC and I don't
1007 * know why. Probably a bug. But, we can safely ignore
1008 * it because win16 does not support color cursors.
1009 * A warning should have been generated by the parser.
1011 put_word(res, cur->planes);
1012 put_word(res, cur->bits);
1013 /* FIXME: The +4 is the hotspot in the cursor resource.
1014 * However, I cound not find this in the documentation.
1015 * The hotspot bytes must either be included or MS
1018 put_dword(res, cur->data->size +4);
1019 put_word(res, cur->id);
1024 put_word(res, 0); /* Reserved */
1025 put_word(res, 2); /* ResType */
1026 put_word(res, curg->ncursor);
1028 for(cur = curg->cursorlist; cur; cur = cur->next)
1030 cur = curg->cursorlist;
1033 for(; cur; cur = cur->prev)
1036 put_word(res, cur->width);
1037 /* FIXME: The height of a cursor is half the size of
1038 * the bitmap's height. BRC puts the height from the
1039 * BITMAPINFOHEADER here instead of the cursorfile's
1040 * height. MS doesn't seem to care...
1042 put_word(res, cur->height);
1043 /* FIXME: The next two are reversed in BRC and I don't
1044 * know why. Probably a bug. But, we can safely ignore
1045 * it because win16 does not support color cursors.
1046 * A warning should have been generated by the parser.
1048 put_word(res, cur->planes);
1049 put_word(res, cur->bits);
1050 /* FIXME: The +4 is the hotspot in the cursor resource.
1051 * However, I cound not find this in the documentation.
1052 * The hotspot bytes must either be included or MS
1055 put_dword(res, cur->data->size +4);
1056 put_word(res, cur->id);
1059 SetResSize(res, restag); /* Set ResourceSize */
1067 *****************************************************************************
1068 * Function : cursor2res
1069 * Syntax : res_t *cursor2res(cursor_t *cur)
1071 * cur - The cursor descriptor
1072 * Output : New .res format structure
1075 *****************************************************************************
1077 static res_t *cursor2res(cursor_t *cur)
1083 assert(cur != NULL);
1086 name.type = name_ord;
1087 name.name.i_name = cur->id;
1088 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1089 put_word(res, cur->xhot);
1090 put_word(res, cur->yhot);
1091 put_raw_data(res, cur->data, 0);
1093 SetResSize(res, restag); /* Set ResourceSize */
1101 *****************************************************************************
1102 * Function : icongroup2res
1103 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1105 * name - Name/ordinal of the resource
1106 * icog - The icon group descriptor
1107 * Output : New .res format structure
1110 *****************************************************************************
1112 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1117 assert(name != NULL);
1118 assert(icog != NULL);
1121 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1124 put_word(res, 0); /* Reserved */
1125 /* FIXME: The ResType in the NEWHEADER structure should
1126 * contain 14 according to the MS win32 doc. This is
1127 * not the case with the BRC compiler and I really doubt
1128 * the latter. Putting one here is compliant to win16 spec,
1129 * but who knows the true value?
1131 put_word(res, 1); /* ResType */
1132 put_word(res, icog->nicon);
1133 for(ico = icog->iconlist; ico; ico = ico->next)
1135 put_byte(res, ico->width);
1136 put_byte(res, ico->height);
1137 put_byte(res, ico->nclr);
1138 put_byte(res, 0); /* Reserved */
1139 put_word(res, ico->planes);
1140 put_word(res, ico->bits);
1141 put_dword(res, ico->data->size);
1142 put_word(res, ico->id);
1147 put_word(res, 0); /* Reserved */
1148 put_word(res, 1); /* ResType */
1149 put_word(res, icog->nicon);
1150 for(ico = icog->iconlist; ico; ico = ico->next)
1152 put_byte(res, ico->width);
1153 put_byte(res, ico->height);
1154 put_byte(res, ico->nclr);
1155 put_byte(res, 0); /* Reserved */
1156 put_word(res, ico->planes);
1157 put_word(res, ico->bits);
1158 put_dword(res, ico->data->size);
1159 put_word(res, ico->id);
1162 SetResSize(res, restag); /* Set ResourceSize */
1170 *****************************************************************************
1171 * Function : icon2res
1172 * Syntax : res_t *icon2res(icon_t *ico)
1174 * ico - The icon descriptor
1175 * Output : New .res format structure
1178 *****************************************************************************
1180 static res_t *icon2res(icon_t *ico)
1186 assert(ico != NULL);
1189 name.type = name_ord;
1190 name.name.i_name = ico->id;
1191 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1192 put_raw_data(res, ico->data, 0);
1194 SetResSize(res, restag); /* Set ResourceSize */
1202 *****************************************************************************
1203 * Function : anicurico2res
1204 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1206 * name - Name/ordinal of the resource
1207 * ani - The animated object descriptor
1208 * Output : New .res format structure
1210 * Remarks : The endian of the object's structures have been converted
1212 * There are rumors that win311 could handle animated stuff.
1213 * That is why they are generated for both win16 and win32
1215 *****************************************************************************
1217 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1221 assert(name != NULL);
1222 assert(ani != NULL);
1225 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1226 NULL, name, ani->memopt, NULL);
1227 put_raw_data(res, ani->data, 0);
1228 /* Set ResourceSize */
1229 SetResSize(res, restag);
1236 *****************************************************************************
1237 * Function : bitmap2res
1238 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1240 * name - Name/ordinal of the resource
1241 * bmp - The bitmap descriptor
1242 * Output : New .res format structure
1244 * Remarks : The endian of the bitmap structures have been converted
1246 *****************************************************************************
1248 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1252 assert(name != NULL);
1253 assert(bmp != NULL);
1256 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1257 if(bmp->data->data[0] == 'B'
1258 && bmp->data->data[1] == 'M'
1259 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1260 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1262 /* The File header is still attached, don't write it */
1263 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1267 put_raw_data(res, bmp->data, 0);
1269 /* Set ResourceSize */
1270 SetResSize(res, restag);
1277 *****************************************************************************
1278 * Function : font2res
1279 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1281 * name - Name/ordinal of the resource
1282 * fnt - The font descriptor
1283 * Output : New .res format structure
1285 * Remarks : The data has been prepared just after parsing.
1286 *****************************************************************************
1288 static res_t *font2res(name_id_t *name, font_t *fnt)
1292 assert(name != NULL);
1293 assert(fnt != NULL);
1296 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1297 put_raw_data(res, fnt->data, 0);
1298 /* Set ResourceSize */
1299 SetResSize(res, restag);
1306 *****************************************************************************
1307 * Function : fontdir2res
1308 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1310 * name - Name/ordinal of the resource
1311 * fntdir - The fontdir descriptor
1312 * Output : New .res format structure
1314 * Remarks : The data has been prepared just after parsing.
1315 *****************************************************************************
1317 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1321 assert(name != NULL);
1322 assert(fnd != NULL);
1325 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1326 put_raw_data(res, fnd->data, 0);
1327 /* Set ResourceSize */
1328 SetResSize(res, restag);
1335 *****************************************************************************
1336 * Function : rcdata2res
1337 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1339 * name - Name/ordinal of the resource
1340 * rdt - The rcdata descriptor
1341 * Output : New .res format structure
1344 *****************************************************************************
1346 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1350 assert(name != NULL);
1351 assert(rdt != NULL);
1354 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1355 put_raw_data(res, rdt->data, 0);
1356 /* Set ResourceSize */
1357 SetResSize(res, restag);
1364 *****************************************************************************
1365 * Function : messagetable2res
1366 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1368 * name - Name/ordinal of the resource
1369 * msg - The messagetable descriptor
1370 * Output : New .res format structure
1372 * Remarks : The data has been converted to the appropriate endian
1373 * after is was parsed.
1374 *****************************************************************************
1376 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1380 assert(name != NULL);
1381 assert(msg != NULL);
1384 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1385 put_raw_data(res, msg->data, 0);
1386 /* Set ResourceSize */
1387 SetResSize(res, restag);
1394 *****************************************************************************
1395 * Function : stringtable2res
1396 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1398 * stt - The stringtable descriptor
1399 * Output : New .res format structure
1402 *****************************************************************************
1404 static res_t *stringtable2res(stringtable_t *stt)
1412 assert(stt != NULL);
1415 for(; stt; stt = stt->next)
1419 warning("Empty internal stringtable");
1422 name.type = name_ord;
1423 name.name.i_name = (stt->idbase >> 4) + 1;
1424 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1425 for(i = 0; i < stt->nentries; i++)
1427 if(stt->entries[i].str && stt->entries[i].str->size)
1429 string_t *str = convert_string(stt->entries[i].str, win32 ? str_unicode : str_char);
1431 put_word(res, str->size);
1433 put_byte(res, str->size);
1434 put_string(res, str, win32 ? str_unicode : str_char, FALSE);
1445 /* Set ResourceSize */
1446 SetResSize(res, restag - lastsize);
1449 lastsize = res->size;
1455 *****************************************************************************
1456 * Function : user2res
1457 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1459 * name - Name/ordinal of the resource
1460 * usr - The userresource descriptor
1461 * Output : New .res format structure
1464 *****************************************************************************
1466 static res_t *user2res(name_id_t *name, user_t *usr)
1470 assert(name != NULL);
1471 assert(usr != NULL);
1474 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1475 put_raw_data(res, usr->data, 0);
1476 /* Set ResourceSize */
1477 SetResSize(res, restag);
1484 *****************************************************************************
1485 * Function : versionblock2res
1486 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1488 * res - Binary resource to write to
1489 * blk - The version block to be written
1492 * Remarks : Self recursive
1493 *****************************************************************************
1495 static void versionblock2res(res_t *res, ver_block_t *blk, int level)
1504 blksizetag = res->size;
1505 put_word(res, 0); /* Will be overwritten later */
1508 put_word(res, 0); /* level ? */
1509 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE);
1511 for(val = blk->values; val; val = val->next)
1513 if(val->type == val_str)
1515 valblksizetag = res->size;
1516 put_word(res, 0); /* Will be overwritten later */
1517 valvalsizetag = res->size;
1518 put_word(res, 0); /* Will be overwritten later */
1521 put_word(res, level);
1523 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1526 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE);
1528 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1530 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1531 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1534 else if(val->type == val_words)
1536 valblksizetag = res->size;
1537 put_word(res, 0); /* Will be overwritten later */
1538 valvalsizetag = res->size;
1539 put_word(res, 0); /* Will be overwritten later */
1542 put_word(res, level);
1544 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1547 for(i = 0; i < val->value.words->nwords; i++)
1549 put_word(res, val->value.words->words[i]);
1551 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1552 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1555 else if(val->type == val_block)
1557 versionblock2res(res, val->value.block, level+1);
1561 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1566 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1570 *****************************************************************************
1571 * Function : versioninfo2res
1572 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1574 * name - Name/ordinal of the resource
1575 * ver - The versioninfo descriptor
1576 * Output : New .res format structure
1579 *****************************************************************************
1581 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1584 int rootblocksizetag;
1591 assert(name != NULL);
1592 assert(ver != NULL);
1594 vsvi.type = str_char;
1595 vsvi.str.cstr = "VS_VERSION_INFO";
1596 vsvi.size = 15; /* Excl. termination */
1599 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1600 rootblocksizetag = res->size;
1601 put_word(res, 0); /* BlockSize filled in later */
1602 valsizetag = res->size;
1603 put_word(res, 0); /* ValueSize filled in later*/
1605 put_word(res, 0); /* Tree-level ? */
1606 put_string(res, &vsvi, win32 ? str_unicode : str_char, TRUE);
1610 put_dword(res, VS_FFI_SIGNATURE);
1611 put_dword(res, VS_FFI_STRUCVERSION);
1612 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1613 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1614 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1615 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1616 put_dword(res, ver->fileflagsmask);
1617 put_dword(res, ver->fileflags);
1618 put_dword(res, ver->fileos);
1619 put_dword(res, ver->filetype);
1620 put_dword(res, ver->filesubtype);
1621 put_dword(res, 0); /* FileDateMS */
1622 put_dword(res, 0); /* FileDateLS */
1624 set_word(res, valsizetag, (WORD)(res->size - tag));
1625 /* Descend into the blocks */
1626 for(blk = ver->blocks; blk; blk = blk->next)
1627 versionblock2res(res, blk, 0);
1628 /* Set root block's size */
1629 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1631 SetResSize(res, restag);
1639 *****************************************************************************
1640 * Function : toolbaritem2res
1641 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1645 * Remarks : Self recursive
1646 *****************************************************************************
1648 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1650 toolbar_item_t *itm = tbitem;
1654 put_word(res, itm->id);
1661 *****************************************************************************
1662 * Function : toolbar2res
1663 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1665 * name - Name/ordinal of the resource
1666 * toolbar - The toolbar descriptor
1667 * Output : New .res format structure
1670 *****************************************************************************
1672 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1676 assert(name != NULL);
1677 assert(toolbar != NULL);
1682 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1684 put_word(res, 1); /* Menuheader: Version */
1685 put_word(res, toolbar->button_width); /* (in pixels?) */
1686 put_word(res, toolbar->button_height); /* (in pixels?) */
1687 put_word(res, toolbar->nitems);
1689 toolbaritem2res(res, toolbar->items);
1690 /* Set ResourceSize */
1691 SetResSize(res, restag);
1696 /* Do not generate anything in 16-bit mode */
1705 *****************************************************************************
1706 * Function : dlginit2res
1707 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1709 * name - Name/ordinal of the resource
1710 * rdt - The dlginit descriptor
1711 * Output : New .res format structure
1714 *****************************************************************************
1716 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1720 assert(name != NULL);
1721 assert(dit != NULL);
1724 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1725 put_raw_data(res, dit->data, 0);
1726 /* Set ResourceSize */
1727 SetResSize(res, restag);
1734 *****************************************************************************
1735 * Function : prep_nid_for_label
1736 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1739 * Description : Converts a resource name into the first 32 (or less)
1740 * characters of the name with conversions.
1742 *****************************************************************************
1744 #define MAXNAMELEN 32
1745 char *prep_nid_for_label(name_id_t *nid)
1747 static char buf[MAXNAMELEN+1];
1749 assert(nid != NULL);
1751 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1755 sptr = nid->name.s_name->str.wstr;
1757 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1759 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1762 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1766 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1770 cptr = nid->name.s_name->str.cstr;
1772 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1774 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1777 warning("Resourcename (str_char) contain unprintable characters, ignored");
1781 else if(nid->type == name_ord)
1783 sprintf(buf, "%u", nid->name.i_name);
1787 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1794 *****************************************************************************
1795 * Function : make_c_name
1796 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1799 * Description : Converts a resource name into a valid c-identifier in the
1802 *****************************************************************************
1804 char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1811 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1812 buf = prep_nid_for_label(nid);
1813 nlen = strlen(buf) + strlen(lanbuf);
1814 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1815 ret = (char *)xmalloc(nlen);
1821 strcat(ret, lanbuf);
1826 *****************************************************************************
1827 * Function : get_c_typename
1828 * Syntax : char *get_c_typename(enum res_e type)
1831 * Description : Convert resource enum to char string to be used in c-name
1834 *****************************************************************************
1836 char *get_c_typename(enum res_e type)
1840 case res_acc: return "Acc";
1841 case res_anicur:return "AniCur";
1842 case res_aniico:return "AniIco";
1843 case res_bmp: return "Bmp";
1844 case res_cur: return "Cur";
1845 case res_curg: return "CurGrp";
1847 case res_dlgex: return "Dlg";
1848 case res_fnt: return "Fnt";
1849 case res_fntdir:return "FntDir";
1850 case res_ico: return "Ico";
1851 case res_icog: return "IcoGrp";
1853 case res_menex: return "Men";
1854 case res_rdt: return "RCDat";
1855 case res_stt: return "StrTab";
1856 case res_usr: return "Usr";
1857 case res_msg: return "MsgTab";
1858 case res_ver: return "VerInf";
1859 case res_toolbar: return "TlBr";
1860 case res_dlginit: return "DlgInit";
1861 default: return "Oops";
1866 *****************************************************************************
1867 * Function : resources2res
1868 * Syntax : void resources2res(resource_t *top)
1870 * top - The resource-tree to convert
1872 * Description : Convert logical resource descriptors into binary data
1874 *****************************************************************************
1876 void resources2res(resource_t *top)
1884 top->binres = accelerator2res(top->name, top->res.acc);
1888 top->binres = bitmap2res(top->name, top->res.bmp);
1892 top->binres = cursor2res(top->res.cur);
1896 top->binres = cursorgroup2res(top->name, top->res.curg);
1900 top->binres = dialog2res(top->name, top->res.dlg);
1904 top->binres = dialogex2res(top->name, top->res.dlgex);
1908 top->binres = font2res(top->name, top->res.fnt);
1912 top->binres = fontdir2res(top->name, top->res.fnd);
1916 top->binres = icon2res(top->res.ico);
1920 top->binres = icongroup2res(top->name, top->res.icog);
1924 top->binres = menu2res(top->name, top->res.men);
1928 top->binres = menuex2res(top->name, top->res.menex);
1932 top->binres = rcdata2res(top->name, top->res.rdt);
1936 top->binres = stringtable2res(top->res.stt);
1940 top->binres = user2res(top->name, top->res.usr);
1944 top->binres = messagetable2res(top->name, top->res.msg);
1948 top->binres = versioninfo2res(top->name, top->res.ver);
1952 top->binres = toolbar2res(top->name, top->res.tbt);
1956 top->binres = dlginit2res(top->name, top->res.dlgi);
1961 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1964 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
1966 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);