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 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 if(str->type == str_unicode && type == str_unicode)
274 for(cnt = 0; cnt < str->size; cnt++)
276 c = str->str.wstr[cnt];
281 if(isterm && (str->size == 0 || (cnt == str->size && c)))
284 else if(str->type == str_char && type == str_char)
286 for(cnt = 0; cnt < str->size; cnt++)
288 c = str->str.cstr[cnt];
293 if(isterm && (str->size == 0 || (cnt == str->size && c)))
296 else if(str->type == str_unicode && type == str_char)
298 for(cnt = 0; cnt < str->size; cnt++)
300 c = str->str.wstr[cnt];
305 if(isterm && (str->size == 0 || (cnt == str->size && c)))
308 else /* str->type == str_char && type == str_unicode */
310 for(cnt = 0; cnt < str->size; cnt++)
312 c = str->str.cstr[cnt];
313 put_word(res, c & 0xff);
317 if(isterm && (str->size == 0 || (cnt == str->size && c)))
323 *****************************************************************************
324 * Function : put_name_id
325 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase)
330 *****************************************************************************
332 void put_name_id(res_t *res, name_id_t *nid, int upcase)
334 if(nid->type == name_ord)
337 put_word(res, 0xffff);
340 put_word(res, (WORD)nid->name.i_name);
342 else if(nid->type == name_str)
345 string_to_upper(nid->name.s_name);
346 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE);
350 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
355 *****************************************************************************
357 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
362 *****************************************************************************
364 void put_lvc(res_t *res, lvc_t *lvc)
366 if(lvc && lvc->language)
367 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
369 put_word(res, 0); /* Neutral */
370 if(lvc && lvc->version)
371 put_dword(res, *(lvc->version));
374 if(lvc && lvc->characts)
375 put_dword(res, *(lvc->characts));
381 *****************************************************************************
382 * Function : put_raw_data
383 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
388 *****************************************************************************
390 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
392 int wsize = raw->size - offset;
393 if(res->allocsize - res->size < wsize)
394 grow_res(res, wsize);
395 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
400 *****************************************************************************
401 * Function : put_res_header
402 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
403 * name_id_t *name, DWORD memopt, lvc_t *lvc)
406 * res - Binary resource descriptor to write to
407 * type - Resource identifier (if ntype == NULL)
408 * ntype - Name id of type
409 * name - Resource's name
410 * memopt - Resource's memory options to write
411 * lvc - Language, version and characteristics (win32 only)
412 * Output : An index to the resource size field. The resource size field
413 * contains the header size upon exit.
416 *****************************************************************************
418 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
419 DWORD memopt, lvc_t *lvc)
423 put_dword(res, 0); /* We will overwrite these later */
427 put_word(res, 0xffff); /* ResType */
431 put_name_id(res, ntype, TRUE);
432 put_name_id(res, name, TRUE); /* ResName */
434 put_dword(res, 0); /* DataVersion */
435 put_word(res, memopt); /* Memory options */
436 put_lvc(res, lvc); /* Language, version and characts */
437 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
438 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
439 res->dataidx = res->size;
447 put_byte(res, 0xff); /* ResType */
451 put_name_id(res, ntype, TRUE);
452 put_name_id(res, name, TRUE); /* ResName */
453 put_word(res, memopt); /* Memory options */
455 put_dword(res, 0); /* ResSize overwritten later*/
456 set_dword(res, tag, res->size);
457 res->dataidx = res->size;
463 *****************************************************************************
464 * Function : accelerator2res
465 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
467 * name - Name/ordinal of the resource
468 * acc - The accelerator descriptor
469 * Output : New .res format structure
472 *****************************************************************************
474 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
479 assert(name != NULL);
486 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
489 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
490 put_word(res, ev->key);
491 put_word(res, ev->id);
492 put_word(res, 0); /* Padding */
499 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
502 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
503 put_word(res, ev->key);
504 put_word(res, ev->id);
508 /* Set ResourceSize */
509 SetResSize(res, restag);
514 *****************************************************************************
515 * Function : dialog2res
516 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
518 * name - Name/ordinal of the resource
519 * dlg - The dialog descriptor
520 * Output : New .res format structure
523 *****************************************************************************
525 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
532 assert(name != NULL);
535 ctrl = dlg->controls;
539 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
541 put_dword(res, dlg->style->or_mask);
542 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
543 tag_nctrl = res->size;
544 put_word(res, 0); /* Number of controls */
545 put_word(res, dlg->x);
546 put_word(res, dlg->y);
547 put_word(res, dlg->width);
548 put_word(res, dlg->height);
550 put_name_id(res, dlg->menu, TRUE);
554 put_name_id(res, dlg->dlgclass, TRUE);
558 put_string(res, dlg->title, str_unicode, TRUE);
563 put_word(res, dlg->font->size);
564 put_string(res, dlg->font->name, str_unicode, TRUE);
570 /* FIXME: what is default control style? */
571 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
572 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
573 put_word(res, ctrl->x);
574 put_word(res, ctrl->y);
575 put_word(res, ctrl->width);
576 put_word(res, ctrl->height);
577 put_word(res, ctrl->id);
579 put_name_id(res, ctrl->ctlclass, TRUE);
581 internal_error(__FILE__, __LINE__, "Control has no control-class");
583 put_name_id(res, ctrl->title, FALSE);
588 put_word(res, ctrl->extra->size+2);
590 put_raw_data(res, ctrl->extra, 0);
600 /* Set number of controls */
601 set_word(res, tag_nctrl, (WORD)nctrl);
605 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
607 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
608 tag_nctrl = res->size;
609 put_byte(res, 0); /* Number of controls */
610 put_word(res, dlg->x);
611 put_word(res, dlg->y);
612 put_word(res, dlg->width);
613 put_word(res, dlg->height);
615 put_name_id(res, dlg->menu, TRUE);
619 put_name_id(res, dlg->dlgclass, TRUE);
623 put_string(res, dlg->title, str_char, TRUE);
628 put_word(res, dlg->font->size);
629 put_string(res, dlg->font->name, str_char, TRUE);
634 put_word(res, ctrl->x);
635 put_word(res, ctrl->y);
636 put_word(res, ctrl->width);
637 put_word(res, ctrl->height);
638 put_word(res, ctrl->id);
639 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
642 if(ctrl->ctlclass->type == name_ord
643 && ctrl->ctlclass->name.i_name >= 0x80
644 && ctrl->ctlclass->name.i_name <= 0x85)
645 put_byte(res, ctrl->ctlclass->name.i_name);
646 else if(ctrl->ctlclass->type == name_str)
647 put_name_id(res, ctrl->ctlclass, FALSE);
649 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
652 internal_error(__FILE__, __LINE__, "Control has no control-class");
654 put_name_id(res, ctrl->title, FALSE);
658 /* FIXME: What is this extra byte doing here? */
664 /* Set number of controls */
665 ((char *)res->data)[tag_nctrl] = (char)nctrl;
667 /* Set ResourceSize */
668 SetResSize(res, restag);
673 *****************************************************************************
674 * Function : dialogex2res
675 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
677 * name - Name/ordinal of the resource
678 * dlgex - The dialogex descriptor
679 * Output : New .res format structure
682 *****************************************************************************
684 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
691 assert(name != NULL);
692 assert(dlgex != NULL);
694 ctrl = dlgex->controls;
698 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
700 /* FIXME: MS doc says thet the first word must contain 0xffff
701 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
702 * compiler reverses the two words.
703 * I don't know which one to choose, but I write it as Mr. B
706 put_word(res, 1); /* Signature */
707 put_word(res, 0xffff); /* DlgVer */
708 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
709 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
710 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
711 tag_nctrl = res->size;
712 put_word(res, 0); /* Number of controls */
713 put_word(res, dlgex->x);
714 put_word(res, dlgex->y);
715 put_word(res, dlgex->width);
716 put_word(res, dlgex->height);
718 put_name_id(res, dlgex->menu, TRUE);
722 put_name_id(res, dlgex->dlgclass, TRUE);
726 put_string(res, dlgex->title, str_unicode, TRUE);
731 put_word(res, dlgex->font->size);
732 put_word(res, dlgex->font->weight);
733 /* FIXME: ? TRUE should be sufficient to say that its
734 * italic, but Borland's compiler says its 0x0101.
735 * I just copy it here, and hope for the best.
737 put_word(res, dlgex->font->italic ? 0x0101 : 0);
738 put_string(res, dlgex->font->name, str_unicode, TRUE);
744 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
745 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
746 /* FIXME: what is default control style? */
747 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
748 put_word(res, ctrl->x);
749 put_word(res, ctrl->y);
750 put_word(res, ctrl->width);
751 put_word(res, ctrl->height);
752 put_word(res, ctrl->id);
753 /* FIXME: Pad is _NOT_ documented!?! */
756 put_name_id(res, ctrl->ctlclass, TRUE);
758 internal_error(__FILE__, __LINE__, "Control has no control-class");
760 put_name_id(res, ctrl->title, FALSE);
766 put_word(res, ctrl->extra->size);
767 put_raw_data(res, ctrl->extra, 0);
776 /* Set number of controls */
777 set_word(res, tag_nctrl, (WORD)nctrl);
778 /* Set ResourceSize */
779 SetResSize(res, restag);
784 /* Do not generate anything in 16-bit mode */
793 *****************************************************************************
794 * Function : menuitem2res
795 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
799 * Remarks : Self recursive
800 *****************************************************************************
802 static void menuitem2res(res_t *res, menu_item_t *menitem)
804 menu_item_t *itm = menitem;
809 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
811 put_word(res, itm->id);
813 put_string(res, itm->name, str_unicode, TRUE);
817 menuitem2res(res, itm->popup);
825 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
827 put_word(res, itm->id);
829 put_string(res, itm->name, str_char, TRUE);
833 menuitem2res(res, itm->popup);
841 *****************************************************************************
842 * Function : menu2res
843 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
845 * name - Name/ordinal of the resource
846 * men - The menu descriptor
847 * Output : New .res format structure
850 *****************************************************************************
852 static res_t *menu2res(name_id_t *name, menu_t *men)
856 assert(name != NULL);
860 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
862 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
863 menuitem2res(res, men->items);
864 /* Set ResourceSize */
865 SetResSize(res, restag);
872 *****************************************************************************
873 * Function : menuexitem2res
874 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
878 * Remarks : Self recursive
879 *****************************************************************************
881 static void menuexitem2res(res_t *res, menuex_item_t *menitem)
883 menuex_item_t *itm = menitem;
887 put_dword(res, itm->gottype ? itm->type : 0);
888 put_dword(res, itm->gotstate ? itm->state : 0);
889 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
890 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
892 put_string(res, itm->name, str_unicode, TRUE);
898 put_dword(res, itm->gothelpid ? itm->helpid : 0);
899 menuexitem2res(res, itm->popup);
907 *****************************************************************************
908 * Function : menuex2res
909 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
911 * name - Name/ordinal of the resource
912 * menex - The menuex descriptor
913 * Output : New .res format structure
916 *****************************************************************************
918 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
922 assert(name != NULL);
923 assert(menex != NULL);
928 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
930 put_word(res, 1); /* Menuheader: Version */
931 put_word(res, 4); /* Offset */
932 put_dword(res, 0); /* HelpId */
934 menuexitem2res(res, menex->items);
935 /* Set ResourceSize */
936 SetResSize(res, restag);
941 /* Do not generate anything in 16-bit mode */
950 *****************************************************************************
951 * Function : cursorgroup2res
952 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
954 * name - Name/ordinal of the resource
955 * curg - The cursor descriptor
956 * Output : New .res format structure
959 *****************************************************************************
961 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
966 assert(name != NULL);
967 assert(curg != NULL);
970 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
973 put_word(res, 0); /* Reserved */
974 /* FIXME: The ResType in the NEWHEADER structure should
975 * contain 14 according to the MS win32 doc. This is
976 * not the case with the BRC compiler and I really doubt
977 * the latter. Putting one here is compliant to win16 spec,
978 * but who knows the true value?
980 put_word(res, 2); /* ResType */
981 put_word(res, curg->ncursor);
983 for(cur = curg->cursorlist; cur; cur = cur->next)
985 cur = curg->cursorlist;
988 for(; cur; cur = cur->prev)
991 put_word(res, cur->width);
992 /* FIXME: The height of a cursor is half the size of
993 * the bitmap's height. BRC puts the height from the
994 * BITMAPINFOHEADER here instead of the cursorfile's
995 * height. MS doesn't seem to care...
997 put_word(res, cur->height);
998 /* FIXME: The next two are reversed in BRC and I don't
999 * know why. Probably a bug. But, we can safely ignore
1000 * it because win16 does not support color cursors.
1001 * A warning should have been generated by the parser.
1003 put_word(res, cur->planes);
1004 put_word(res, cur->bits);
1005 /* FIXME: The +4 is the hotspot in the cursor resource.
1006 * However, I cound not find this in the documentation.
1007 * The hotspot bytes must either be included or MS
1010 put_dword(res, cur->data->size +4);
1011 put_word(res, cur->id);
1016 put_word(res, 0); /* Reserved */
1017 put_word(res, 2); /* ResType */
1018 put_word(res, curg->ncursor);
1020 for(cur = curg->cursorlist; cur; cur = cur->next)
1022 cur = curg->cursorlist;
1025 for(; cur; cur = cur->prev)
1028 put_word(res, cur->width);
1029 /* FIXME: The height of a cursor is half the size of
1030 * the bitmap's height. BRC puts the height from the
1031 * BITMAPINFOHEADER here instead of the cursorfile's
1032 * height. MS doesn't seem to care...
1034 put_word(res, cur->height);
1035 /* FIXME: The next two are reversed in BRC and I don't
1036 * know why. Probably a bug. But, we can safely ignore
1037 * it because win16 does not support color cursors.
1038 * A warning should have been generated by the parser.
1040 put_word(res, cur->planes);
1041 put_word(res, cur->bits);
1042 /* FIXME: The +4 is the hotspot in the cursor resource.
1043 * However, I cound not find this in the documentation.
1044 * The hotspot bytes must either be included or MS
1047 put_dword(res, cur->data->size +4);
1048 put_word(res, cur->id);
1051 SetResSize(res, restag); /* Set ResourceSize */
1059 *****************************************************************************
1060 * Function : cursor2res
1061 * Syntax : res_t *cursor2res(cursor_t *cur)
1063 * cur - The cursor descriptor
1064 * Output : New .res format structure
1067 *****************************************************************************
1069 static res_t *cursor2res(cursor_t *cur)
1075 assert(cur != NULL);
1078 name.type = name_ord;
1079 name.name.i_name = cur->id;
1080 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1081 put_word(res, cur->xhot);
1082 put_word(res, cur->yhot);
1083 put_raw_data(res, cur->data, 0);
1085 SetResSize(res, restag); /* Set ResourceSize */
1093 *****************************************************************************
1094 * Function : icongroup2res
1095 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1097 * name - Name/ordinal of the resource
1098 * icog - The icon group descriptor
1099 * Output : New .res format structure
1102 *****************************************************************************
1104 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1109 assert(name != NULL);
1110 assert(icog != NULL);
1113 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1116 put_word(res, 0); /* Reserved */
1117 /* FIXME: The ResType in the NEWHEADER structure should
1118 * contain 14 according to the MS win32 doc. This is
1119 * not the case with the BRC compiler and I really doubt
1120 * the latter. Putting one here is compliant to win16 spec,
1121 * but who knows the true value?
1123 put_word(res, 1); /* ResType */
1124 put_word(res, icog->nicon);
1125 for(ico = icog->iconlist; ico; ico = ico->next)
1127 put_byte(res, ico->width);
1128 put_byte(res, ico->height);
1129 put_byte(res, ico->nclr);
1130 put_byte(res, 0); /* Reserved */
1131 put_word(res, ico->planes);
1132 put_word(res, ico->bits);
1133 put_dword(res, ico->data->size);
1134 put_word(res, ico->id);
1139 put_word(res, 0); /* Reserved */
1140 put_word(res, 1); /* ResType */
1141 put_word(res, icog->nicon);
1142 for(ico = icog->iconlist; ico; ico = ico->next)
1144 put_byte(res, ico->width);
1145 put_byte(res, ico->height);
1146 put_byte(res, ico->nclr);
1147 put_byte(res, 0); /* Reserved */
1148 put_word(res, ico->planes);
1149 put_word(res, ico->bits);
1150 put_dword(res, ico->data->size);
1151 put_word(res, ico->id);
1154 SetResSize(res, restag); /* Set ResourceSize */
1162 *****************************************************************************
1163 * Function : icon2res
1164 * Syntax : res_t *icon2res(icon_t *ico)
1166 * ico - The icon descriptor
1167 * Output : New .res format structure
1170 *****************************************************************************
1172 static res_t *icon2res(icon_t *ico)
1178 assert(ico != NULL);
1181 name.type = name_ord;
1182 name.name.i_name = ico->id;
1183 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1184 put_raw_data(res, ico->data, 0);
1186 SetResSize(res, restag); /* Set ResourceSize */
1194 *****************************************************************************
1195 * Function : anicurico2res
1196 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1198 * name - Name/ordinal of the resource
1199 * ani - The animated object descriptor
1200 * Output : New .res format structure
1202 * Remarks : The endian of the object's structures have been converted
1204 * There are rumors that win311 could handle animated stuff.
1205 * That is why they are generated for both win16 and win32
1207 *****************************************************************************
1209 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1213 assert(name != NULL);
1214 assert(ani != NULL);
1217 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1218 NULL, name, ani->memopt, NULL);
1219 put_raw_data(res, ani->data, 0);
1220 /* Set ResourceSize */
1221 SetResSize(res, restag);
1228 *****************************************************************************
1229 * Function : bitmap2res
1230 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1232 * name - Name/ordinal of the resource
1233 * bmp - The bitmap descriptor
1234 * Output : New .res format structure
1236 * Remarks : The endian of the bitmap structures have been converted
1238 *****************************************************************************
1240 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1244 assert(name != NULL);
1245 assert(bmp != NULL);
1248 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1249 if(bmp->data->data[0] == 'B'
1250 && bmp->data->data[1] == 'M'
1251 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1252 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1254 /* The File header is still attached, don't write it */
1255 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1259 put_raw_data(res, bmp->data, 0);
1261 /* Set ResourceSize */
1262 SetResSize(res, restag);
1269 *****************************************************************************
1270 * Function : font2res
1271 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1273 * name - Name/ordinal of the resource
1274 * fnt - The font descriptor
1275 * Output : New .res format structure
1277 * Remarks : The data has been prepared just after parsing.
1278 *****************************************************************************
1280 static res_t *font2res(name_id_t *name, font_t *fnt)
1284 assert(name != NULL);
1285 assert(fnt != NULL);
1288 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1289 put_raw_data(res, fnt->data, 0);
1290 /* Set ResourceSize */
1291 SetResSize(res, restag);
1298 *****************************************************************************
1299 * Function : fontdir2res
1300 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1302 * name - Name/ordinal of the resource
1303 * fntdir - The fontdir descriptor
1304 * Output : New .res format structure
1306 * Remarks : The data has been prepared just after parsing.
1307 *****************************************************************************
1309 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1313 assert(name != NULL);
1314 assert(fnd != NULL);
1317 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1318 put_raw_data(res, fnd->data, 0);
1319 /* Set ResourceSize */
1320 SetResSize(res, restag);
1327 *****************************************************************************
1328 * Function : rcdata2res
1329 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1331 * name - Name/ordinal of the resource
1332 * rdt - The rcdata descriptor
1333 * Output : New .res format structure
1336 *****************************************************************************
1338 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1342 assert(name != NULL);
1343 assert(rdt != NULL);
1346 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1347 put_raw_data(res, rdt->data, 0);
1348 /* Set ResourceSize */
1349 SetResSize(res, restag);
1356 *****************************************************************************
1357 * Function : messagetable2res
1358 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1360 * name - Name/ordinal of the resource
1361 * msg - The messagetable descriptor
1362 * Output : New .res format structure
1364 * Remarks : The data has been converted to the appropriate endian
1365 * after is was parsed.
1366 *****************************************************************************
1368 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1372 assert(name != NULL);
1373 assert(msg != NULL);
1376 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1377 put_raw_data(res, msg->data, 0);
1378 /* Set ResourceSize */
1379 SetResSize(res, restag);
1386 *****************************************************************************
1387 * Function : stringtable2res
1388 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1390 * stt - The stringtable descriptor
1391 * Output : New .res format structure
1394 *****************************************************************************
1396 static res_t *stringtable2res(stringtable_t *stt)
1404 assert(stt != NULL);
1407 for(; stt; stt = stt->next)
1411 warning("Empty internal stringtable");
1414 name.type = name_ord;
1415 name.name.i_name = (stt->idbase >> 4) + 1;
1416 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1417 for(i = 0; i < stt->nentries; i++)
1419 if(stt->entries[i].str && stt->entries[i].str->size)
1422 put_word(res, stt->entries[i].str->size);
1424 put_byte(res, stt->entries[i].str->size);
1425 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char, FALSE);
1435 /* Set ResourceSize */
1436 SetResSize(res, restag - lastsize);
1439 lastsize = res->size;
1445 *****************************************************************************
1446 * Function : user2res
1447 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1449 * name - Name/ordinal of the resource
1450 * usr - The userresource descriptor
1451 * Output : New .res format structure
1454 *****************************************************************************
1456 static res_t *user2res(name_id_t *name, user_t *usr)
1460 assert(name != NULL);
1461 assert(usr != NULL);
1464 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1465 put_raw_data(res, usr->data, 0);
1466 /* Set ResourceSize */
1467 SetResSize(res, restag);
1474 *****************************************************************************
1475 * Function : versionblock2res
1476 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1478 * res - Binary resource to write to
1479 * blk - The version block to be written
1482 * Remarks : Self recursive
1483 *****************************************************************************
1485 static void versionblock2res(res_t *res, ver_block_t *blk, int level)
1494 blksizetag = res->size;
1495 put_word(res, 0); /* Will be overwritten later */
1498 put_word(res, 0); /* level ? */
1499 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE);
1501 for(val = blk->values; val; val = val->next)
1503 if(val->type == val_str)
1505 valblksizetag = res->size;
1506 put_word(res, 0); /* Will be overwritten later */
1507 valvalsizetag = res->size;
1508 put_word(res, 0); /* Will be overwritten later */
1511 put_word(res, level);
1513 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1516 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE);
1518 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1520 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1521 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1524 else if(val->type == val_words)
1526 valblksizetag = res->size;
1527 put_word(res, 0); /* Will be overwritten later */
1528 valvalsizetag = res->size;
1529 put_word(res, 0); /* Will be overwritten later */
1532 put_word(res, level);
1534 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1537 for(i = 0; i < val->value.words->nwords; i++)
1539 put_word(res, val->value.words->words[i]);
1541 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1542 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1545 else if(val->type == val_block)
1547 versionblock2res(res, val->value.block, level+1);
1551 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1556 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1560 *****************************************************************************
1561 * Function : versioninfo2res
1562 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1564 * name - Name/ordinal of the resource
1565 * ver - The versioninfo descriptor
1566 * Output : New .res format structure
1569 *****************************************************************************
1571 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1574 int rootblocksizetag;
1581 assert(name != NULL);
1582 assert(ver != NULL);
1584 vsvi.type = str_char;
1585 vsvi.str.cstr = "VS_VERSION_INFO";
1586 vsvi.size = 15; /* Excl. termination */
1589 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1590 rootblocksizetag = res->size;
1591 put_word(res, 0); /* BlockSize filled in later */
1592 valsizetag = res->size;
1593 put_word(res, 0); /* ValueSize filled in later*/
1595 put_word(res, 0); /* Tree-level ? */
1596 put_string(res, &vsvi, win32 ? str_unicode : str_char, TRUE);
1600 put_dword(res, VS_FFI_SIGNATURE);
1601 put_dword(res, VS_FFI_STRUCVERSION);
1602 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1603 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1604 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1605 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1606 put_dword(res, ver->fileflagsmask);
1607 put_dword(res, ver->fileflags);
1608 put_dword(res, ver->fileos);
1609 put_dword(res, ver->filetype);
1610 put_dword(res, ver->filesubtype);
1611 put_dword(res, 0); /* FileDateMS */
1612 put_dword(res, 0); /* FileDateLS */
1614 set_word(res, valsizetag, (WORD)(res->size - tag));
1615 /* Descend into the blocks */
1616 for(blk = ver->blocks; blk; blk = blk->next)
1617 versionblock2res(res, blk, 0);
1618 /* Set root block's size */
1619 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1621 SetResSize(res, restag);
1629 *****************************************************************************
1630 * Function : toolbaritem2res
1631 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1635 * Remarks : Self recursive
1636 *****************************************************************************
1638 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1640 toolbar_item_t *itm = tbitem;
1644 put_word(res, itm->id);
1651 *****************************************************************************
1652 * Function : toolbar2res
1653 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1655 * name - Name/ordinal of the resource
1656 * toolbar - The toolbar descriptor
1657 * Output : New .res format structure
1660 *****************************************************************************
1662 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1666 assert(name != NULL);
1667 assert(toolbar != NULL);
1672 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1674 put_word(res, 1); /* Menuheader: Version */
1675 put_word(res, toolbar->button_width); /* (in pixels?) */
1676 put_word(res, toolbar->button_height); /* (in pixels?) */
1677 put_word(res, toolbar->nitems);
1679 toolbaritem2res(res, toolbar->items);
1680 /* Set ResourceSize */
1681 SetResSize(res, restag);
1686 /* Do not generate anything in 16-bit mode */
1695 *****************************************************************************
1696 * Function : dlginit2res
1697 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1699 * name - Name/ordinal of the resource
1700 * rdt - The dlginit descriptor
1701 * Output : New .res format structure
1704 *****************************************************************************
1706 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1710 assert(name != NULL);
1711 assert(dit != NULL);
1714 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1715 put_raw_data(res, dit->data, 0);
1716 /* Set ResourceSize */
1717 SetResSize(res, restag);
1724 *****************************************************************************
1725 * Function : prep_nid_for_label
1726 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1729 * Description : Converts a resource name into the first 32 (or less)
1730 * characters of the name with conversions.
1732 *****************************************************************************
1734 #define MAXNAMELEN 32
1735 char *prep_nid_for_label(name_id_t *nid)
1737 static char buf[MAXNAMELEN+1];
1739 assert(nid != NULL);
1741 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1745 sptr = nid->name.s_name->str.wstr;
1747 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1749 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1752 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1756 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1760 cptr = nid->name.s_name->str.cstr;
1762 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1764 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1767 warning("Resourcename (str_char) contain unprintable characters, ignored");
1771 else if(nid->type == name_ord)
1773 sprintf(buf, "%u", nid->name.i_name);
1777 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1784 *****************************************************************************
1785 * Function : make_c_name
1786 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1789 * Description : Converts a resource name into a valid c-identifier in the
1792 *****************************************************************************
1794 char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1801 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1802 buf = prep_nid_for_label(nid);
1803 nlen = strlen(buf) + strlen(lanbuf);
1804 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1805 ret = (char *)xmalloc(nlen);
1811 strcat(ret, lanbuf);
1816 *****************************************************************************
1817 * Function : get_c_typename
1818 * Syntax : char *get_c_typename(enum res_e type)
1821 * Description : Convert resource enum to char string to be used in c-name
1824 *****************************************************************************
1826 char *get_c_typename(enum res_e type)
1830 case res_acc: return "Acc";
1831 case res_anicur:return "AniCur";
1832 case res_aniico:return "AniIco";
1833 case res_bmp: return "Bmp";
1834 case res_cur: return "Cur";
1835 case res_curg: return "CurGrp";
1837 case res_dlgex: return "Dlg";
1838 case res_fnt: return "Fnt";
1839 case res_fntdir:return "FntDir";
1840 case res_ico: return "Ico";
1841 case res_icog: return "IcoGrp";
1843 case res_menex: return "Men";
1844 case res_rdt: return "RCDat";
1845 case res_stt: return "StrTab";
1846 case res_usr: return "Usr";
1847 case res_msg: return "MsgTab";
1848 case res_ver: return "VerInf";
1849 case res_toolbar: return "TlBr";
1850 case res_dlginit: return "DlgInit";
1851 default: return "Oops";
1856 *****************************************************************************
1857 * Function : resources2res
1858 * Syntax : void resources2res(resource_t *top)
1860 * top - The resource-tree to convert
1862 * Description : Convert logical resource descriptors into binary data
1864 *****************************************************************************
1866 void resources2res(resource_t *top)
1874 top->binres = accelerator2res(top->name, top->res.acc);
1878 top->binres = bitmap2res(top->name, top->res.bmp);
1882 top->binres = cursor2res(top->res.cur);
1886 top->binres = cursorgroup2res(top->name, top->res.curg);
1890 top->binres = dialog2res(top->name, top->res.dlg);
1894 top->binres = dialogex2res(top->name, top->res.dlgex);
1898 top->binres = font2res(top->name, top->res.fnt);
1902 top->binres = fontdir2res(top->name, top->res.fnd);
1906 top->binres = icon2res(top->res.ico);
1910 top->binres = icongroup2res(top->name, top->res.icog);
1914 top->binres = menu2res(top->name, top->res.men);
1918 top->binres = menuex2res(top->name, top->res.menex);
1922 top->binres = rcdata2res(top->name, top->res.rdt);
1926 top->binres = stringtable2res(top->res.stt);
1930 top->binres = user2res(top->name, top->res.usr);
1934 top->binres = messagetable2res(top->name, top->res.msg);
1938 top->binres = versioninfo2res(top->name, top->res.ver);
1942 top->binres = toolbar2res(top->name, top->res.tbt);
1946 top->binres = dlginit2res(top->name, top->res.dlgi);
1951 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1954 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
1956 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);