2 * Generate .res format from a resource-tree
4 * Copyright 1998 Bertho A. Stultiens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * 05-May-2000 BS - Added code to support endian conversions. The
22 * extra functions also aid unaligned access, but
23 * this is not yet implemented.
24 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
25 * names in .s and .h files.
43 #include "wine/unicode.h"
45 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
50 r = (res_t *)xmalloc(sizeof(res_t));
51 r->allocsize = RES_BLOCKSIZE;
53 r->data = (char *)xmalloc(RES_BLOCKSIZE);
57 res_t *grow_res(res_t *r, int add)
60 r->data = (char *)xrealloc(r->data, r->allocsize);
65 *****************************************************************************
69 * Syntax : void put_byte(res_t *res, unsigned c)
70 * void put_word(res_t *res, unsigned w)
71 * void put_dword(res_t *res, unsigned d)
73 * res - Binary resource to put the data in
74 * c, w, d - Data to put
76 * Description : Put primitives that put an item in the binary resource.
77 * The data array grows automatically.
79 *****************************************************************************
81 void put_byte(res_t *res, unsigned c)
83 if(res->allocsize - res->size < sizeof(char))
84 grow_res(res, RES_BLOCKSIZE);
85 res->data[res->size] = (char)c;
86 res->size += sizeof(char);
89 void put_word(res_t *res, unsigned w)
91 if(res->allocsize - res->size < sizeof(WORD))
92 grow_res(res, RES_BLOCKSIZE);
95 #ifdef WORDS_BIGENDIAN
99 res->data[res->size+0] = HIBYTE(w);
100 res->data[res->size+1] = LOBYTE(w);
103 #ifndef WORDS_BIGENDIAN
107 res->data[res->size+1] = HIBYTE(w);
108 res->data[res->size+0] = LOBYTE(w);
111 res->size += sizeof(WORD);
114 void put_dword(res_t *res, unsigned d)
116 if(res->allocsize - res->size < sizeof(DWORD))
117 grow_res(res, RES_BLOCKSIZE);
120 #ifdef WORDS_BIGENDIAN
124 res->data[res->size+0] = HIBYTE(HIWORD(d));
125 res->data[res->size+1] = LOBYTE(HIWORD(d));
126 res->data[res->size+2] = HIBYTE(LOWORD(d));
127 res->data[res->size+3] = LOBYTE(LOWORD(d));
130 #ifndef WORDS_BIGENDIAN
134 res->data[res->size+3] = HIBYTE(HIWORD(d));
135 res->data[res->size+2] = LOBYTE(HIWORD(d));
136 res->data[res->size+1] = HIBYTE(LOWORD(d));
137 res->data[res->size+0] = LOBYTE(LOWORD(d));
140 res->size += sizeof(DWORD);
143 void put_pad(res_t *res)
145 while(res->size & 0x3)
150 *****************************************************************************
151 * Function : set_word
153 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
154 * void set_dword(res_t *res, int ofs, unsigned d)
156 * res - Binary resource to put the data in
157 * ofs - Byte offset in data-array
160 * Description : Set the value of a binary resource data array to a
163 *****************************************************************************
165 void set_word(res_t *res, int ofs, unsigned w)
169 #ifdef WORDS_BIGENDIAN
173 res->data[ofs+0] = HIBYTE(w);
174 res->data[ofs+1] = LOBYTE(w);
177 #ifndef WORDS_BIGENDIAN
181 res->data[ofs+1] = HIBYTE(w);
182 res->data[ofs+0] = LOBYTE(w);
187 void set_dword(res_t *res, int ofs, unsigned d)
191 #ifdef WORDS_BIGENDIAN
195 res->data[ofs+0] = HIBYTE(HIWORD(d));
196 res->data[ofs+1] = LOBYTE(HIWORD(d));
197 res->data[ofs+2] = HIBYTE(LOWORD(d));
198 res->data[ofs+3] = LOBYTE(LOWORD(d));
201 #ifndef WORDS_BIGENDIAN
205 res->data[ofs+3] = HIBYTE(HIWORD(d));
206 res->data[ofs+2] = LOBYTE(HIWORD(d));
207 res->data[ofs+1] = HIBYTE(LOWORD(d));
208 res->data[ofs+0] = LOBYTE(LOWORD(d));
214 *****************************************************************************
215 * Function : get_word
217 * Syntax : WORD get_word(res_t *res, int ofs)
218 * DWORD get_dword(res_t *res, int ofs)
220 * res - Binary resource to put the data in
221 * ofs - Byte offset in data-array
222 * Output : The data in native endian
223 * Description : Get the value of a binary resource data array in native
226 *****************************************************************************
228 WORD get_word(res_t *res, int ofs)
232 #ifdef WORDS_BIGENDIAN
236 return (res->data[ofs+0] << 8)
239 #ifndef WORDS_BIGENDIAN
243 return (res->data[ofs+1] << 8)
248 DWORD get_dword(res_t *res, int ofs)
252 #ifdef WORDS_BIGENDIAN
256 return (res->data[ofs+0] << 24)
257 | (res->data[ofs+1] << 16)
258 | (res->data[ofs+2] << 8)
261 #ifndef WORDS_BIGENDIAN
265 return (res->data[ofs+3] << 24)
266 | (res->data[ofs+2] << 16)
267 | (res->data[ofs+1] << 8)
273 *****************************************************************************
274 * Function : string_to_upper
275 * Syntax : void string_to_upper(string_t *str)
279 * Remarks : FIXME: codepages...
280 *****************************************************************************
282 static void string_to_upper(string_t *str)
286 if(str->type == str_char)
288 for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
290 else if(str->type == str_unicode)
292 for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
296 internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
301 *****************************************************************************
302 * Function : put_string
303 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
304 * int isterm, const language_t *lang)
306 * res - Binary resource to put the data in
307 * str - String to put
308 * type - Data has to be written in either str_char or str_unicode
309 * isterm - The string is '\0' terminated (disregard the string's
314 *****************************************************************************
316 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
317 const language_t *lang)
325 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
326 else codepage = get_language_codepage( 0, 0 );
328 assert( codepage != -1 );
330 newstr = convert_string(str, type, codepage);
331 if (type == str_unicode)
333 if (str->type == str_char)
335 if (!check_unicode_conversion( str, newstr, codepage ))
336 error( "String %s does not convert identically to Unicode and back in codepage %d. "
337 "Try using a Unicode string instead.", str->str.cstr, codepage );
339 if (!isterm) put_word(res, newstr->size);
340 for(cnt = 0; cnt < newstr->size; cnt++)
342 WCHAR c = newstr->str.wstr[cnt];
343 if (isterm && !c) break;
346 if (isterm) put_word(res, 0);
350 if (!isterm) put_byte(res, newstr->size);
351 for(cnt = 0; cnt < newstr->size; cnt++)
353 char c = newstr->str.cstr[cnt];
354 if (isterm && !c) break;
357 if (isterm) put_byte(res, 0);
363 *****************************************************************************
364 * Function : put_name_id
365 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
370 *****************************************************************************
372 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
374 if(nid->type == name_ord)
377 put_word(res, 0xffff);
380 put_word(res, (WORD)nid->name.i_name);
382 else if(nid->type == name_str)
385 string_to_upper(nid->name.s_name);
386 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
390 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
395 *****************************************************************************
397 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
402 *****************************************************************************
404 static void put_lvc(res_t *res, lvc_t *lvc)
406 if(lvc && lvc->language)
407 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
409 put_word(res, 0); /* Neutral */
410 if(lvc && lvc->version)
411 put_dword(res, *(lvc->version));
414 if(lvc && lvc->characts)
415 put_dword(res, *(lvc->characts));
421 *****************************************************************************
422 * Function : put_raw_data
423 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
428 *****************************************************************************
430 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
432 int wsize = raw->size - offset;
433 if(res->allocsize - res->size < wsize)
434 grow_res(res, wsize);
435 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
440 *****************************************************************************
441 * Function : put_res_header
442 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
443 * name_id_t *name, DWORD memopt, lvc_t *lvc)
446 * res - Binary resource descriptor to write to
447 * type - Resource identifier (if ntype == NULL)
448 * ntype - Name id of type
449 * name - Resource's name
450 * memopt - Resource's memory options to write
451 * lvc - Language, version and characteristics (win32 only)
452 * Output : An index to the resource size field. The resource size field
453 * contains the header size upon exit.
456 *****************************************************************************
458 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
459 DWORD memopt, lvc_t *lvc)
463 put_dword(res, 0); /* We will overwrite these later */
467 put_word(res, 0xffff); /* ResType */
471 put_name_id(res, ntype, TRUE, lvc->language);
472 put_name_id(res, name, TRUE, lvc->language); /* ResName */
474 put_dword(res, 0); /* DataVersion */
475 put_word(res, memopt); /* Memory options */
476 put_lvc(res, lvc); /* Language, version and characts */
477 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
478 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
479 res->dataidx = res->size;
487 put_byte(res, 0xff); /* ResType */
491 put_name_id(res, ntype, TRUE, NULL);
492 put_name_id(res, name, TRUE, NULL); /* ResName */
493 put_word(res, memopt); /* Memory options */
495 put_dword(res, 0); /* ResSize overwritten later*/
496 set_dword(res, tag, res->size);
497 res->dataidx = res->size;
503 *****************************************************************************
504 * Function : accelerator2res
505 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
507 * name - Name/ordinal of the resource
508 * acc - The accelerator descriptor
509 * Output : New .res format structure
512 *****************************************************************************
514 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
519 assert(name != NULL);
526 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
529 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
530 put_word(res, ev->key);
531 put_word(res, ev->id);
532 put_word(res, 0); /* Padding */
539 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
542 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
543 put_word(res, ev->key);
544 put_word(res, ev->id);
548 /* Set ResourceSize */
549 SetResSize(res, restag);
554 *****************************************************************************
555 * Function : dialog2res
556 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
558 * name - Name/ordinal of the resource
559 * dlg - The dialog descriptor
560 * Output : New .res format structure
563 *****************************************************************************
565 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
572 assert(name != NULL);
575 ctrl = dlg->controls;
579 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
581 put_dword(res, dlg->style->or_mask);
582 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
583 tag_nctrl = res->size;
584 put_word(res, 0); /* Number of controls */
585 put_word(res, dlg->x);
586 put_word(res, dlg->y);
587 put_word(res, dlg->width);
588 put_word(res, dlg->height);
590 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
594 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
598 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
603 put_word(res, dlg->font->size);
604 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
610 /* FIXME: what is default control style? */
611 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
612 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
613 put_word(res, ctrl->x);
614 put_word(res, ctrl->y);
615 put_word(res, ctrl->width);
616 put_word(res, ctrl->height);
617 put_word(res, ctrl->id);
619 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
621 internal_error(__FILE__, __LINE__, "Control has no control-class");
623 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
628 put_word(res, ctrl->extra->size+2);
630 put_raw_data(res, ctrl->extra, 0);
640 /* Set number of controls */
641 set_word(res, tag_nctrl, (WORD)nctrl);
645 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
647 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
648 tag_nctrl = res->size;
649 put_byte(res, 0); /* Number of controls */
650 put_word(res, dlg->x);
651 put_word(res, dlg->y);
652 put_word(res, dlg->width);
653 put_word(res, dlg->height);
655 put_name_id(res, dlg->menu, TRUE, NULL);
659 put_name_id(res, dlg->dlgclass, TRUE, NULL);
663 put_string(res, dlg->title, str_char, TRUE, NULL);
668 put_word(res, dlg->font->size);
669 put_string(res, dlg->font->name, str_char, TRUE, NULL);
674 put_word(res, ctrl->x);
675 put_word(res, ctrl->y);
676 put_word(res, ctrl->width);
677 put_word(res, ctrl->height);
678 put_word(res, ctrl->id);
679 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
682 if(ctrl->ctlclass->type == name_ord
683 && ctrl->ctlclass->name.i_name >= 0x80
684 && ctrl->ctlclass->name.i_name <= 0x85)
685 put_byte(res, ctrl->ctlclass->name.i_name);
686 else if(ctrl->ctlclass->type == name_str)
687 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
689 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
692 internal_error(__FILE__, __LINE__, "Control has no control-class");
694 put_name_id(res, ctrl->title, FALSE, NULL);
698 /* FIXME: What is this extra byte doing here? */
704 /* Set number of controls */
705 ((char *)res->data)[tag_nctrl] = (char)nctrl;
707 /* Set ResourceSize */
708 SetResSize(res, restag);
713 *****************************************************************************
714 * Function : dialogex2res
715 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
717 * name - Name/ordinal of the resource
718 * dlgex - The dialogex descriptor
719 * Output : New .res format structure
722 *****************************************************************************
724 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
731 assert(name != NULL);
732 assert(dlgex != NULL);
734 ctrl = dlgex->controls;
738 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
740 /* FIXME: MS doc says thet the first word must contain 0xffff
741 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
742 * compiler reverses the two words.
743 * I don't know which one to choose, but I write it as Mr. B
746 put_word(res, 1); /* Signature */
747 put_word(res, 0xffff); /* DlgVer */
748 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
749 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
750 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
751 tag_nctrl = res->size;
752 put_word(res, 0); /* Number of controls */
753 put_word(res, dlgex->x);
754 put_word(res, dlgex->y);
755 put_word(res, dlgex->width);
756 put_word(res, dlgex->height);
758 put_name_id(res, dlgex->menu, TRUE, dlgex->lvc.language);
762 put_name_id(res, dlgex->dlgclass, TRUE, dlgex->lvc.language);
766 put_string(res, dlgex->title, str_unicode, TRUE, dlgex->lvc.language);
771 put_word(res, dlgex->font->size);
772 put_word(res, dlgex->font->weight);
773 /* FIXME: ? TRUE should be sufficient to say that its
774 * italic, but Borland's compiler says its 0x0101.
775 * I just copy it here, and hope for the best.
777 put_word(res, dlgex->font->italic ? 0x0101 : 0);
778 put_string(res, dlgex->font->name, str_unicode, TRUE, dlgex->lvc.language);
784 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
785 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
786 /* FIXME: what is default control style? */
787 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
788 put_word(res, ctrl->x);
789 put_word(res, ctrl->y);
790 put_word(res, ctrl->width);
791 put_word(res, ctrl->height);
792 put_word(res, ctrl->id);
793 /* FIXME: Pad is _NOT_ documented!?! */
796 put_name_id(res, ctrl->ctlclass, TRUE, dlgex->lvc.language);
798 internal_error(__FILE__, __LINE__, "Control has no control-class");
800 put_name_id(res, ctrl->title, FALSE, dlgex->lvc.language);
806 put_word(res, ctrl->extra->size);
807 put_raw_data(res, ctrl->extra, 0);
816 /* Set number of controls */
817 set_word(res, tag_nctrl, (WORD)nctrl);
818 /* Set ResourceSize */
819 SetResSize(res, restag);
824 /* Do not generate anything in 16-bit mode */
833 *****************************************************************************
834 * Function : menuitem2res
835 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
839 * Remarks : Self recursive
840 *****************************************************************************
842 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
844 menu_item_t *itm = menitem;
849 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
851 put_word(res, itm->id);
853 put_string(res, itm->name, str_unicode, TRUE, lang);
857 menuitem2res(res, itm->popup, lang);
865 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
867 put_word(res, itm->id);
869 put_string(res, itm->name, str_char, TRUE, lang);
873 menuitem2res(res, itm->popup, lang);
881 *****************************************************************************
882 * Function : menu2res
883 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
885 * name - Name/ordinal of the resource
886 * men - The menu descriptor
887 * Output : New .res format structure
890 *****************************************************************************
892 static res_t *menu2res(name_id_t *name, menu_t *men)
896 assert(name != NULL);
900 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
902 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
903 menuitem2res(res, men->items, win32 ? men->lvc.language : NULL);
904 /* Set ResourceSize */
905 SetResSize(res, restag);
912 *****************************************************************************
913 * Function : menuexitem2res
914 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
918 * Remarks : Self recursive
919 *****************************************************************************
921 static void menuexitem2res(res_t *res, menuex_item_t *menitem, const language_t *lang)
923 menuex_item_t *itm = menitem;
927 put_dword(res, itm->gottype ? itm->type : 0);
928 put_dword(res, itm->gotstate ? itm->state : 0);
929 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
930 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
932 put_string(res, itm->name, str_unicode, TRUE, lang);
938 put_dword(res, itm->gothelpid ? itm->helpid : 0);
939 menuexitem2res(res, itm->popup, lang);
947 *****************************************************************************
948 * Function : menuex2res
949 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
951 * name - Name/ordinal of the resource
952 * menex - The menuex descriptor
953 * Output : New .res format structure
956 *****************************************************************************
958 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
962 assert(name != NULL);
963 assert(menex != NULL);
968 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
970 put_word(res, 1); /* Menuheader: Version */
971 put_word(res, 4); /* Offset */
972 put_dword(res, 0); /* HelpId */
974 menuexitem2res(res, menex->items, menex->lvc.language);
975 /* Set ResourceSize */
976 SetResSize(res, restag);
981 /* Do not generate anything in 16-bit mode */
990 *****************************************************************************
991 * Function : cursorgroup2res
992 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
994 * name - Name/ordinal of the resource
995 * curg - The cursor descriptor
996 * Output : New .res format structure
999 *****************************************************************************
1001 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
1006 assert(name != NULL);
1007 assert(curg != NULL);
1010 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
1013 put_word(res, 0); /* Reserved */
1014 /* FIXME: The ResType in the NEWHEADER structure should
1015 * contain 14 according to the MS win32 doc. This is
1016 * not the case with the BRC compiler and I really doubt
1017 * the latter. Putting one here is compliant to win16 spec,
1018 * but who knows the true value?
1020 put_word(res, 2); /* ResType */
1021 put_word(res, curg->ncursor);
1023 for(cur = curg->cursorlist; cur; cur = cur->next)
1025 cur = curg->cursorlist;
1028 for(; cur; cur = cur->prev)
1031 put_word(res, cur->width);
1032 /* FIXME: The height of a cursor is half the size of
1033 * the bitmap's height. BRC puts the height from the
1034 * BITMAPINFOHEADER here instead of the cursorfile's
1035 * height. MS doesn't seem to care...
1037 put_word(res, cur->height);
1038 /* FIXME: The next two are reversed in BRC and I don't
1039 * know why. Probably a bug. But, we can safely ignore
1040 * it because win16 does not support color cursors.
1041 * A warning should have been generated by the parser.
1043 put_word(res, cur->planes);
1044 put_word(res, cur->bits);
1045 /* FIXME: The +4 is the hotspot in the cursor resource.
1046 * However, I cound not find this in the documentation.
1047 * The hotspot bytes must either be included or MS
1050 put_dword(res, cur->data->size +4);
1051 put_word(res, cur->id);
1056 put_word(res, 0); /* Reserved */
1057 put_word(res, 2); /* ResType */
1058 put_word(res, curg->ncursor);
1060 for(cur = curg->cursorlist; cur; cur = cur->next)
1062 cur = curg->cursorlist;
1065 for(; cur; cur = cur->prev)
1068 put_word(res, cur->width);
1069 /* FIXME: The height of a cursor is half the size of
1070 * the bitmap's height. BRC puts the height from the
1071 * BITMAPINFOHEADER here instead of the cursorfile's
1072 * height. MS doesn't seem to care...
1074 put_word(res, cur->height);
1075 /* FIXME: The next two are reversed in BRC and I don't
1076 * know why. Probably a bug. But, we can safely ignore
1077 * it because win16 does not support color cursors.
1078 * A warning should have been generated by the parser.
1080 put_word(res, cur->planes);
1081 put_word(res, cur->bits);
1082 /* FIXME: The +4 is the hotspot in the cursor resource.
1083 * However, I cound not find this in the documentation.
1084 * The hotspot bytes must either be included or MS
1087 put_dword(res, cur->data->size +4);
1088 put_word(res, cur->id);
1091 SetResSize(res, restag); /* Set ResourceSize */
1099 *****************************************************************************
1100 * Function : cursor2res
1101 * Syntax : res_t *cursor2res(cursor_t *cur)
1103 * cur - The cursor descriptor
1104 * Output : New .res format structure
1107 *****************************************************************************
1109 static res_t *cursor2res(cursor_t *cur)
1115 assert(cur != NULL);
1118 name.type = name_ord;
1119 name.name.i_name = cur->id;
1120 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1121 put_word(res, cur->xhot);
1122 put_word(res, cur->yhot);
1123 put_raw_data(res, cur->data, 0);
1125 SetResSize(res, restag); /* Set ResourceSize */
1133 *****************************************************************************
1134 * Function : icongroup2res
1135 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1137 * name - Name/ordinal of the resource
1138 * icog - The icon group descriptor
1139 * Output : New .res format structure
1142 *****************************************************************************
1144 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1149 assert(name != NULL);
1150 assert(icog != NULL);
1153 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1156 put_word(res, 0); /* Reserved */
1157 /* FIXME: The ResType in the NEWHEADER structure should
1158 * contain 14 according to the MS win32 doc. This is
1159 * not the case with the BRC compiler and I really doubt
1160 * the latter. Putting one here is compliant to win16 spec,
1161 * but who knows the true value?
1163 put_word(res, 1); /* ResType */
1164 put_word(res, icog->nicon);
1165 for(ico = icog->iconlist; ico; ico = ico->next)
1167 put_byte(res, ico->width);
1168 put_byte(res, ico->height);
1169 put_byte(res, ico->nclr);
1170 put_byte(res, 0); /* Reserved */
1171 put_word(res, ico->planes);
1172 put_word(res, ico->bits);
1173 put_dword(res, ico->data->size);
1174 put_word(res, ico->id);
1179 put_word(res, 0); /* Reserved */
1180 put_word(res, 1); /* ResType */
1181 put_word(res, icog->nicon);
1182 for(ico = icog->iconlist; ico; ico = ico->next)
1184 put_byte(res, ico->width);
1185 put_byte(res, ico->height);
1186 put_byte(res, ico->nclr);
1187 put_byte(res, 0); /* Reserved */
1188 put_word(res, ico->planes);
1189 put_word(res, ico->bits);
1190 put_dword(res, ico->data->size);
1191 put_word(res, ico->id);
1194 SetResSize(res, restag); /* Set ResourceSize */
1202 *****************************************************************************
1203 * Function : icon2res
1204 * Syntax : res_t *icon2res(icon_t *ico)
1206 * ico - The icon descriptor
1207 * Output : New .res format structure
1210 *****************************************************************************
1212 static res_t *icon2res(icon_t *ico)
1218 assert(ico != NULL);
1221 name.type = name_ord;
1222 name.name.i_name = ico->id;
1223 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1224 put_raw_data(res, ico->data, 0);
1226 SetResSize(res, restag); /* Set ResourceSize */
1234 *****************************************************************************
1235 * Function : anicurico2res
1236 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1238 * name - Name/ordinal of the resource
1239 * ani - The animated object descriptor
1240 * Output : New .res format structure
1242 * Remarks : The endian of the object's structures have been converted
1244 * There are rumors that win311 could handle animated stuff.
1245 * That is why they are generated for both win16 and win32
1247 *****************************************************************************
1249 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1253 assert(name != NULL);
1254 assert(ani != NULL);
1257 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1258 NULL, name, ani->memopt, NULL);
1259 put_raw_data(res, ani->data, 0);
1260 /* Set ResourceSize */
1261 SetResSize(res, restag);
1268 *****************************************************************************
1269 * Function : bitmap2res
1270 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1272 * name - Name/ordinal of the resource
1273 * bmp - The bitmap descriptor
1274 * Output : New .res format structure
1276 * Remarks : The endian of the bitmap structures have been converted
1278 *****************************************************************************
1280 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1284 assert(name != NULL);
1285 assert(bmp != NULL);
1288 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1289 if(bmp->data->data[0] == 'B'
1290 && bmp->data->data[1] == 'M'
1291 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1292 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1294 /* The File header is still attached, don't write it */
1295 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1299 put_raw_data(res, bmp->data, 0);
1301 /* Set ResourceSize */
1302 SetResSize(res, restag);
1309 *****************************************************************************
1310 * Function : font2res
1311 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1313 * name - Name/ordinal of the resource
1314 * fnt - The font descriptor
1315 * Output : New .res format structure
1317 * Remarks : The data has been prepared just after parsing.
1318 *****************************************************************************
1320 static res_t *font2res(name_id_t *name, font_t *fnt)
1324 assert(name != NULL);
1325 assert(fnt != NULL);
1328 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1329 put_raw_data(res, fnt->data, 0);
1330 /* Set ResourceSize */
1331 SetResSize(res, restag);
1338 *****************************************************************************
1339 * Function : fontdir2res
1340 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1342 * name - Name/ordinal of the resource
1343 * fntdir - The fontdir descriptor
1344 * Output : New .res format structure
1346 * Remarks : The data has been prepared just after parsing.
1347 *****************************************************************************
1349 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1353 assert(name != NULL);
1354 assert(fnd != NULL);
1357 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1358 put_raw_data(res, fnd->data, 0);
1359 /* Set ResourceSize */
1360 SetResSize(res, restag);
1367 *****************************************************************************
1368 * Function : rcdata2res
1369 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1371 * name - Name/ordinal of the resource
1372 * rdt - The rcdata descriptor
1373 * Output : New .res format structure
1376 *****************************************************************************
1378 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1382 assert(name != NULL);
1383 assert(rdt != NULL);
1386 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1387 put_raw_data(res, rdt->data, 0);
1388 /* Set ResourceSize */
1389 SetResSize(res, restag);
1396 *****************************************************************************
1397 * Function : messagetable2res
1398 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1400 * name - Name/ordinal of the resource
1401 * msg - The messagetable descriptor
1402 * Output : New .res format structure
1404 * Remarks : The data has been converted to the appropriate endian
1405 * after is was parsed.
1406 *****************************************************************************
1408 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1412 assert(name != NULL);
1413 assert(msg != NULL);
1416 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1417 put_raw_data(res, msg->data, 0);
1418 /* Set ResourceSize */
1419 SetResSize(res, restag);
1426 *****************************************************************************
1427 * Function : stringtable2res
1428 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1430 * stt - The stringtable descriptor
1431 * Output : New .res format structure
1434 *****************************************************************************
1436 static res_t *stringtable2res(stringtable_t *stt)
1444 assert(stt != NULL);
1447 for(; stt; stt = stt->next)
1451 warning("Empty internal stringtable");
1454 name.type = name_ord;
1455 name.name.i_name = (stt->idbase >> 4) + 1;
1456 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1457 for(i = 0; i < stt->nentries; i++)
1459 if(stt->entries[i].str && stt->entries[i].str->size)
1461 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1462 FALSE, win32 ? stt->lvc.language : NULL);
1472 /* Set ResourceSize */
1473 SetResSize(res, restag - lastsize);
1476 lastsize = res->size;
1482 *****************************************************************************
1483 * Function : user2res
1484 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1486 * name - Name/ordinal of the resource
1487 * usr - The userresource descriptor
1488 * Output : New .res format structure
1491 *****************************************************************************
1493 static res_t *user2res(name_id_t *name, user_t *usr)
1497 assert(name != NULL);
1498 assert(usr != NULL);
1501 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1502 put_raw_data(res, usr->data, 0);
1503 /* Set ResourceSize */
1504 SetResSize(res, restag);
1511 *****************************************************************************
1512 * Function : versionblock2res
1513 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1515 * res - Binary resource to write to
1516 * blk - The version block to be written
1519 * Remarks : Self recursive
1520 *****************************************************************************
1522 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1531 blksizetag = res->size;
1532 put_word(res, 0); /* Will be overwritten later */
1535 put_word(res, 0); /* level ? */
1536 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1538 for(val = blk->values; val; val = val->next)
1540 if(val->type == val_str)
1542 valblksizetag = res->size;
1543 put_word(res, 0); /* Will be overwritten later */
1544 valvalsizetag = res->size;
1545 put_word(res, 0); /* Will be overwritten later */
1548 put_word(res, level);
1550 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1553 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1555 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1557 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1558 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1561 else if(val->type == val_words)
1563 valblksizetag = res->size;
1564 put_word(res, 0); /* Will be overwritten later */
1565 valvalsizetag = res->size;
1566 put_word(res, 0); /* Will be overwritten later */
1569 put_word(res, level);
1571 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1574 for(i = 0; i < val->value.words->nwords; i++)
1576 put_word(res, val->value.words->words[i]);
1578 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1579 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1582 else if(val->type == val_block)
1584 versionblock2res(res, val->value.block, level+1, lang);
1588 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1593 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1597 *****************************************************************************
1598 * Function : versioninfo2res
1599 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1601 * name - Name/ordinal of the resource
1602 * ver - The versioninfo descriptor
1603 * Output : New .res format structure
1606 *****************************************************************************
1608 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1611 int rootblocksizetag;
1618 assert(name != NULL);
1619 assert(ver != NULL);
1621 vsvi.type = str_char;
1622 vsvi.str.cstr = "VS_VERSION_INFO";
1623 vsvi.size = 15; /* Excl. termination */
1626 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1627 rootblocksizetag = res->size;
1628 put_word(res, 0); /* BlockSize filled in later */
1629 valsizetag = res->size;
1630 put_word(res, 0); /* ValueSize filled in later*/
1632 put_word(res, 0); /* Tree-level ? */
1633 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1634 TRUE, win32 ? ver->lvc.language : NULL);
1638 put_dword(res, VS_FFI_SIGNATURE);
1639 put_dword(res, VS_FFI_STRUCVERSION);
1640 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1641 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1642 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1643 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1644 put_dword(res, ver->fileflagsmask);
1645 put_dword(res, ver->fileflags);
1646 put_dword(res, ver->fileos);
1647 put_dword(res, ver->filetype);
1648 put_dword(res, ver->filesubtype);
1649 put_dword(res, 0); /* FileDateMS */
1650 put_dword(res, 0); /* FileDateLS */
1652 set_word(res, valsizetag, (WORD)(res->size - tag));
1653 /* Descend into the blocks */
1654 for(blk = ver->blocks; blk; blk = blk->next)
1655 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1656 /* Set root block's size */
1657 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1659 SetResSize(res, restag);
1667 *****************************************************************************
1668 * Function : toolbaritem2res
1669 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1673 * Remarks : Self recursive
1674 *****************************************************************************
1676 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1678 toolbar_item_t *itm = tbitem;
1682 put_word(res, itm->id);
1689 *****************************************************************************
1690 * Function : toolbar2res
1691 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1693 * name - Name/ordinal of the resource
1694 * toolbar - The toolbar descriptor
1695 * Output : New .res format structure
1698 *****************************************************************************
1700 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1704 assert(name != NULL);
1705 assert(toolbar != NULL);
1710 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1712 put_word(res, 1); /* Menuheader: Version */
1713 put_word(res, toolbar->button_width); /* (in pixels?) */
1714 put_word(res, toolbar->button_height); /* (in pixels?) */
1715 put_word(res, toolbar->nitems);
1717 toolbaritem2res(res, toolbar->items);
1718 /* Set ResourceSize */
1719 SetResSize(res, restag);
1724 /* Do not generate anything in 16-bit mode */
1733 *****************************************************************************
1734 * Function : dlginit2res
1735 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1737 * name - Name/ordinal of the resource
1738 * rdt - The dlginit descriptor
1739 * Output : New .res format structure
1742 *****************************************************************************
1744 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1748 assert(name != NULL);
1749 assert(dit != NULL);
1752 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1753 put_raw_data(res, dit->data, 0);
1754 /* Set ResourceSize */
1755 SetResSize(res, restag);
1762 *****************************************************************************
1763 * Function : prep_nid_for_label
1764 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1767 * Description : Converts a resource name into the first 32 (or less)
1768 * characters of the name with conversions.
1770 *****************************************************************************
1772 #define MAXNAMELEN 32
1773 char *prep_nid_for_label(name_id_t *nid)
1775 static char buf[MAXNAMELEN+1];
1777 assert(nid != NULL);
1779 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1783 sptr = nid->name.s_name->str.wstr;
1785 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1787 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1790 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1794 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1798 cptr = nid->name.s_name->str.cstr;
1800 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1802 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1805 warning("Resourcename (str_char) contain unprintable characters, ignored");
1809 else if(nid->type == name_ord)
1811 sprintf(buf, "%u", nid->name.i_name);
1815 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1822 *****************************************************************************
1823 * Function : make_c_name
1824 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1827 * Description : Converts a resource name into a valid c-identifier in the
1830 *****************************************************************************
1832 char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1839 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1840 buf = prep_nid_for_label(nid);
1841 nlen = strlen(buf) + strlen(lanbuf);
1842 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1843 ret = (char *)xmalloc(nlen);
1849 strcat(ret, lanbuf);
1854 *****************************************************************************
1855 * Function : get_c_typename
1856 * Syntax : char *get_c_typename(enum res_e type)
1859 * Description : Convert resource enum to char string to be used in c-name
1862 *****************************************************************************
1864 char *get_c_typename(enum res_e type)
1868 case res_acc: return "Acc";
1869 case res_anicur:return "AniCur";
1870 case res_aniico:return "AniIco";
1871 case res_bmp: return "Bmp";
1872 case res_cur: return "Cur";
1873 case res_curg: return "CurGrp";
1875 case res_dlgex: return "Dlg";
1876 case res_fnt: return "Fnt";
1877 case res_fntdir:return "FntDir";
1878 case res_ico: return "Ico";
1879 case res_icog: return "IcoGrp";
1881 case res_menex: return "Men";
1882 case res_rdt: return "RCDat";
1883 case res_stt: return "StrTab";
1884 case res_usr: return "Usr";
1885 case res_msg: return "MsgTab";
1886 case res_ver: return "VerInf";
1887 case res_toolbar: return "TlBr";
1888 case res_dlginit: return "DlgInit";
1889 default: return "Oops";
1894 *****************************************************************************
1895 * Function : resources2res
1896 * Syntax : void resources2res(resource_t *top)
1898 * top - The resource-tree to convert
1900 * Description : Convert logical resource descriptors into binary data
1902 *****************************************************************************
1904 void resources2res(resource_t *top)
1912 top->binres = accelerator2res(top->name, top->res.acc);
1916 top->binres = bitmap2res(top->name, top->res.bmp);
1920 top->binres = cursor2res(top->res.cur);
1924 top->binres = cursorgroup2res(top->name, top->res.curg);
1928 top->binres = dialog2res(top->name, top->res.dlg);
1932 top->binres = dialogex2res(top->name, top->res.dlgex);
1936 top->binres = font2res(top->name, top->res.fnt);
1940 top->binres = fontdir2res(top->name, top->res.fnd);
1944 top->binres = icon2res(top->res.ico);
1948 top->binres = icongroup2res(top->name, top->res.icog);
1952 top->binres = menu2res(top->name, top->res.men);
1956 top->binres = menuex2res(top->name, top->res.menex);
1960 top->binres = rcdata2res(top->name, top->res.rdt);
1964 top->binres = stringtable2res(top->res.stt);
1968 top->binres = user2res(top->name, top->res.usr);
1972 top->binres = messagetable2res(top->name, top->res.msg);
1976 top->binres = versioninfo2res(top->name, top->res.ver);
1980 top->binres = toolbar2res(top->name, top->res.tbt);
1984 top->binres = dlginit2res(top->name, top->res.dlgi);
1989 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1992 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
1994 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);