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.
44 #include "wine/unicode.h"
46 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
51 r = (res_t *)xmalloc(sizeof(res_t));
52 r->allocsize = RES_BLOCKSIZE;
54 r->data = (char *)xmalloc(RES_BLOCKSIZE);
58 res_t *grow_res(res_t *r, int add)
61 r->data = (char *)xrealloc(r->data, r->allocsize);
66 *****************************************************************************
70 * Syntax : void put_byte(res_t *res, unsigned c)
71 * void put_word(res_t *res, unsigned w)
72 * void put_dword(res_t *res, unsigned d)
74 * res - Binary resource to put the data in
75 * c, w, d - Data to put
77 * Description : Put primitives that put an item in the binary resource.
78 * The data array grows automatically.
80 *****************************************************************************
82 void put_byte(res_t *res, unsigned c)
84 if(res->allocsize - res->size < sizeof(char))
85 grow_res(res, RES_BLOCKSIZE);
86 res->data[res->size] = (char)c;
87 res->size += sizeof(char);
90 void put_word(res_t *res, unsigned w)
92 if(res->allocsize - res->size < sizeof(WORD))
93 grow_res(res, RES_BLOCKSIZE);
96 #ifdef WORDS_BIGENDIAN
100 res->data[res->size+0] = HIBYTE(w);
101 res->data[res->size+1] = LOBYTE(w);
104 #ifndef WORDS_BIGENDIAN
108 res->data[res->size+1] = HIBYTE(w);
109 res->data[res->size+0] = LOBYTE(w);
112 res->size += sizeof(WORD);
115 void put_dword(res_t *res, unsigned d)
117 if(res->allocsize - res->size < sizeof(DWORD))
118 grow_res(res, RES_BLOCKSIZE);
121 #ifdef WORDS_BIGENDIAN
125 res->data[res->size+0] = HIBYTE(HIWORD(d));
126 res->data[res->size+1] = LOBYTE(HIWORD(d));
127 res->data[res->size+2] = HIBYTE(LOWORD(d));
128 res->data[res->size+3] = LOBYTE(LOWORD(d));
131 #ifndef WORDS_BIGENDIAN
135 res->data[res->size+3] = HIBYTE(HIWORD(d));
136 res->data[res->size+2] = LOBYTE(HIWORD(d));
137 res->data[res->size+1] = HIBYTE(LOWORD(d));
138 res->data[res->size+0] = LOBYTE(LOWORD(d));
141 res->size += sizeof(DWORD);
144 void put_pad(res_t *res)
146 while(res->size & 0x3)
151 *****************************************************************************
152 * Function : set_word
154 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
155 * void set_dword(res_t *res, int ofs, unsigned d)
157 * res - Binary resource to put the data in
158 * ofs - Byte offset in data-array
161 * Description : Set the value of a binary resource data array to a
164 *****************************************************************************
166 void set_word(res_t *res, int ofs, unsigned w)
170 #ifdef WORDS_BIGENDIAN
174 res->data[ofs+0] = HIBYTE(w);
175 res->data[ofs+1] = LOBYTE(w);
178 #ifndef WORDS_BIGENDIAN
182 res->data[ofs+1] = HIBYTE(w);
183 res->data[ofs+0] = LOBYTE(w);
188 void set_dword(res_t *res, int ofs, unsigned d)
192 #ifdef WORDS_BIGENDIAN
196 res->data[ofs+0] = HIBYTE(HIWORD(d));
197 res->data[ofs+1] = LOBYTE(HIWORD(d));
198 res->data[ofs+2] = HIBYTE(LOWORD(d));
199 res->data[ofs+3] = LOBYTE(LOWORD(d));
202 #ifndef WORDS_BIGENDIAN
206 res->data[ofs+3] = HIBYTE(HIWORD(d));
207 res->data[ofs+2] = LOBYTE(HIWORD(d));
208 res->data[ofs+1] = HIBYTE(LOWORD(d));
209 res->data[ofs+0] = LOBYTE(LOWORD(d));
215 *****************************************************************************
216 * Function : get_word
218 * Syntax : WORD get_word(res_t *res, int ofs)
219 * DWORD get_dword(res_t *res, int ofs)
221 * res - Binary resource to put the data in
222 * ofs - Byte offset in data-array
223 * Output : The data in native endian
224 * Description : Get the value of a binary resource data array in native
227 *****************************************************************************
229 WORD get_word(res_t *res, int ofs)
233 #ifdef WORDS_BIGENDIAN
237 return (res->data[ofs+0] << 8)
240 #ifndef WORDS_BIGENDIAN
244 return (res->data[ofs+1] << 8)
249 DWORD get_dword(res_t *res, int ofs)
253 #ifdef WORDS_BIGENDIAN
257 return (res->data[ofs+0] << 24)
258 | (res->data[ofs+1] << 16)
259 | (res->data[ofs+2] << 8)
262 #ifndef WORDS_BIGENDIAN
266 return (res->data[ofs+3] << 24)
267 | (res->data[ofs+2] << 16)
268 | (res->data[ofs+1] << 8)
274 *****************************************************************************
275 * Function : string_to_upper
276 * Syntax : void string_to_upper(string_t *str)
280 * Remarks : FIXME: codepages...
281 *****************************************************************************
283 static void string_to_upper(string_t *str)
287 if(str->type == str_char)
289 for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
291 else if(str->type == str_unicode)
293 for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
297 internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
302 *****************************************************************************
303 * Function : put_string
304 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
305 * int isterm, const language_t *lang)
307 * res - Binary resource to put the data in
308 * str - String to put
309 * type - Data has to be written in either str_char or str_unicode
310 * isterm - The string is '\0' terminated (disregard the string's
315 *****************************************************************************
317 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
318 const language_t *lang)
326 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
327 else codepage = get_language_codepage( 0, 0 );
329 assert( codepage != -1 );
331 newstr = convert_string(str, type, codepage);
332 if (type == str_unicode)
334 if (str->type == str_char)
336 if (!check_unicode_conversion( str, newstr, codepage ))
337 error( "String %s does not convert identically to Unicode and back in codepage %d. "
338 "Try using a Unicode string instead.", str->str.cstr, codepage );
340 if (!isterm) put_word(res, newstr->size);
341 for(cnt = 0; cnt < newstr->size; cnt++)
343 WCHAR c = newstr->str.wstr[cnt];
344 if (isterm && !c) break;
347 if (isterm) put_word(res, 0);
351 if (!isterm) put_byte(res, newstr->size);
352 for(cnt = 0; cnt < newstr->size; cnt++)
354 char c = newstr->str.cstr[cnt];
355 if (isterm && !c) break;
358 if (isterm) put_byte(res, 0);
364 *****************************************************************************
365 * Function : put_name_id
366 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
371 *****************************************************************************
373 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
375 if(nid->type == name_ord)
378 put_word(res, 0xffff);
381 put_word(res, (WORD)nid->name.i_name);
383 else if(nid->type == name_str)
386 string_to_upper(nid->name.s_name);
387 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
391 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
396 *****************************************************************************
398 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
403 *****************************************************************************
405 static void put_lvc(res_t *res, lvc_t *lvc)
407 if(lvc && lvc->language)
408 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
410 put_word(res, 0); /* Neutral */
411 if(lvc && lvc->version)
412 put_dword(res, *(lvc->version));
415 if(lvc && lvc->characts)
416 put_dword(res, *(lvc->characts));
422 *****************************************************************************
423 * Function : put_raw_data
424 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
429 *****************************************************************************
431 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
433 int wsize = raw->size - offset;
434 if(res->allocsize - res->size < wsize)
435 grow_res(res, wsize);
436 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
441 *****************************************************************************
442 * Function : put_res_header
443 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
444 * name_id_t *name, DWORD memopt, lvc_t *lvc)
447 * res - Binary resource descriptor to write to
448 * type - Resource identifier (if ntype == NULL)
449 * ntype - Name id of type
450 * name - Resource's name
451 * memopt - Resource's memory options to write
452 * lvc - Language, version and characteristics (win32 only)
453 * Output : An index to the resource size field. The resource size field
454 * contains the header size upon exit.
457 *****************************************************************************
459 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
460 DWORD memopt, lvc_t *lvc)
464 put_dword(res, 0); /* We will overwrite these later */
468 put_word(res, 0xffff); /* ResType */
472 put_name_id(res, ntype, TRUE, lvc->language);
473 put_name_id(res, name, TRUE, lvc->language); /* ResName */
475 put_dword(res, 0); /* DataVersion */
476 put_word(res, memopt); /* Memory options */
477 put_lvc(res, lvc); /* Language, version and characts */
478 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
479 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
480 res->dataidx = res->size;
488 put_byte(res, 0xff); /* ResType */
492 put_name_id(res, ntype, TRUE, NULL);
493 put_name_id(res, name, TRUE, NULL); /* ResName */
494 put_word(res, memopt); /* Memory options */
496 put_dword(res, 0); /* ResSize overwritten later*/
497 set_dword(res, tag, res->size);
498 res->dataidx = res->size;
504 *****************************************************************************
505 * Function : accelerator2res
506 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
508 * name - Name/ordinal of the resource
509 * acc - The accelerator descriptor
510 * Output : New .res format structure
513 *****************************************************************************
515 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
520 assert(name != NULL);
527 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
530 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
531 put_word(res, ev->key);
532 put_word(res, ev->id);
533 put_word(res, 0); /* Padding */
540 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
543 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
544 put_word(res, ev->key);
545 put_word(res, ev->id);
549 /* Set ResourceSize */
550 SetResSize(res, restag);
555 *****************************************************************************
556 * Function : dialog2res
557 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
559 * name - Name/ordinal of the resource
560 * dlg - The dialog descriptor
561 * Output : New .res format structure
564 *****************************************************************************
566 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
573 assert(name != NULL);
576 ctrl = dlg->controls;
580 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
582 put_dword(res, dlg->style->or_mask);
583 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
584 tag_nctrl = res->size;
585 put_word(res, 0); /* Number of controls */
586 put_word(res, dlg->x);
587 put_word(res, dlg->y);
588 put_word(res, dlg->width);
589 put_word(res, dlg->height);
591 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
595 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
599 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
604 put_word(res, dlg->font->size);
605 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
611 /* FIXME: what is default control style? */
612 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
613 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
614 put_word(res, ctrl->x);
615 put_word(res, ctrl->y);
616 put_word(res, ctrl->width);
617 put_word(res, ctrl->height);
618 put_word(res, ctrl->id);
620 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
622 internal_error(__FILE__, __LINE__, "Control has no control-class");
624 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
629 put_word(res, ctrl->extra->size+2);
631 put_raw_data(res, ctrl->extra, 0);
641 /* Set number of controls */
642 set_word(res, tag_nctrl, (WORD)nctrl);
646 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
648 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
649 tag_nctrl = res->size;
650 put_byte(res, 0); /* Number of controls */
651 put_word(res, dlg->x);
652 put_word(res, dlg->y);
653 put_word(res, dlg->width);
654 put_word(res, dlg->height);
656 put_name_id(res, dlg->menu, TRUE, NULL);
660 put_name_id(res, dlg->dlgclass, TRUE, NULL);
664 put_string(res, dlg->title, str_char, TRUE, NULL);
669 put_word(res, dlg->font->size);
670 put_string(res, dlg->font->name, str_char, TRUE, NULL);
675 put_word(res, ctrl->x);
676 put_word(res, ctrl->y);
677 put_word(res, ctrl->width);
678 put_word(res, ctrl->height);
679 put_word(res, ctrl->id);
680 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
683 if(ctrl->ctlclass->type == name_ord
684 && ctrl->ctlclass->name.i_name >= 0x80
685 && ctrl->ctlclass->name.i_name <= 0x85)
686 put_byte(res, ctrl->ctlclass->name.i_name);
687 else if(ctrl->ctlclass->type == name_str)
688 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
690 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
693 internal_error(__FILE__, __LINE__, "Control has no control-class");
695 put_name_id(res, ctrl->title, FALSE, NULL);
699 /* FIXME: What is this extra byte doing here? */
705 /* Set number of controls */
706 ((char *)res->data)[tag_nctrl] = (char)nctrl;
708 /* Set ResourceSize */
709 SetResSize(res, restag);
714 *****************************************************************************
715 * Function : dialogex2res
716 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
718 * name - Name/ordinal of the resource
719 * dlgex - The dialogex descriptor
720 * Output : New .res format structure
723 *****************************************************************************
725 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
732 assert(name != NULL);
733 assert(dlgex != NULL);
735 ctrl = dlgex->controls;
739 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
741 /* FIXME: MS doc says thet the first word must contain 0xffff
742 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
743 * compiler reverses the two words.
744 * I don't know which one to choose, but I write it as Mr. B
747 put_word(res, 1); /* Signature */
748 put_word(res, 0xffff); /* DlgVer */
749 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
750 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
751 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
752 tag_nctrl = res->size;
753 put_word(res, 0); /* Number of controls */
754 put_word(res, dlgex->x);
755 put_word(res, dlgex->y);
756 put_word(res, dlgex->width);
757 put_word(res, dlgex->height);
759 put_name_id(res, dlgex->menu, TRUE, dlgex->lvc.language);
763 put_name_id(res, dlgex->dlgclass, TRUE, dlgex->lvc.language);
767 put_string(res, dlgex->title, str_unicode, TRUE, dlgex->lvc.language);
772 put_word(res, dlgex->font->size);
773 put_word(res, dlgex->font->weight);
774 /* FIXME: ? TRUE should be sufficient to say that its
775 * italic, but Borland's compiler says its 0x0101.
776 * I just copy it here, and hope for the best.
778 put_word(res, dlgex->font->italic ? 0x0101 : 0);
779 put_string(res, dlgex->font->name, str_unicode, TRUE, dlgex->lvc.language);
785 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
786 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
787 /* FIXME: what is default control style? */
788 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
789 put_word(res, ctrl->x);
790 put_word(res, ctrl->y);
791 put_word(res, ctrl->width);
792 put_word(res, ctrl->height);
793 put_word(res, ctrl->id);
794 /* FIXME: Pad is _NOT_ documented!?! */
797 put_name_id(res, ctrl->ctlclass, TRUE, dlgex->lvc.language);
799 internal_error(__FILE__, __LINE__, "Control has no control-class");
801 put_name_id(res, ctrl->title, FALSE, dlgex->lvc.language);
807 put_word(res, ctrl->extra->size);
808 put_raw_data(res, ctrl->extra, 0);
817 /* Set number of controls */
818 set_word(res, tag_nctrl, (WORD)nctrl);
819 /* Set ResourceSize */
820 SetResSize(res, restag);
825 /* Do not generate anything in 16-bit mode */
834 *****************************************************************************
835 * Function : menuitem2res
836 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
840 * Remarks : Self recursive
841 *****************************************************************************
843 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
845 menu_item_t *itm = menitem;
850 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
852 put_word(res, itm->id);
854 put_string(res, itm->name, str_unicode, TRUE, lang);
858 menuitem2res(res, itm->popup, lang);
866 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
868 put_word(res, itm->id);
870 put_string(res, itm->name, str_char, TRUE, lang);
874 menuitem2res(res, itm->popup, lang);
882 *****************************************************************************
883 * Function : menu2res
884 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
886 * name - Name/ordinal of the resource
887 * men - The menu descriptor
888 * Output : New .res format structure
891 *****************************************************************************
893 static res_t *menu2res(name_id_t *name, menu_t *men)
897 assert(name != NULL);
901 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
903 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
904 menuitem2res(res, men->items, win32 ? men->lvc.language : NULL);
905 /* Set ResourceSize */
906 SetResSize(res, restag);
913 *****************************************************************************
914 * Function : menuexitem2res
915 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
919 * Remarks : Self recursive
920 *****************************************************************************
922 static void menuexitem2res(res_t *res, menuex_item_t *menitem, const language_t *lang)
924 menuex_item_t *itm = menitem;
928 put_dword(res, itm->gottype ? itm->type : 0);
929 put_dword(res, itm->gotstate ? itm->state : 0);
930 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
931 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
933 put_string(res, itm->name, str_unicode, TRUE, lang);
939 put_dword(res, itm->gothelpid ? itm->helpid : 0);
940 menuexitem2res(res, itm->popup, lang);
948 *****************************************************************************
949 * Function : menuex2res
950 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
952 * name - Name/ordinal of the resource
953 * menex - The menuex descriptor
954 * Output : New .res format structure
957 *****************************************************************************
959 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
963 assert(name != NULL);
964 assert(menex != NULL);
969 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
971 put_word(res, 1); /* Menuheader: Version */
972 put_word(res, 4); /* Offset */
973 put_dword(res, 0); /* HelpId */
975 menuexitem2res(res, menex->items, menex->lvc.language);
976 /* Set ResourceSize */
977 SetResSize(res, restag);
982 /* Do not generate anything in 16-bit mode */
991 *****************************************************************************
992 * Function : cursorgroup2res
993 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
995 * name - Name/ordinal of the resource
996 * curg - The cursor descriptor
997 * Output : New .res format structure
1000 *****************************************************************************
1002 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
1007 assert(name != NULL);
1008 assert(curg != NULL);
1011 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
1014 put_word(res, 0); /* Reserved */
1015 /* FIXME: The ResType in the NEWHEADER structure should
1016 * contain 14 according to the MS win32 doc. This is
1017 * not the case with the BRC compiler and I really doubt
1018 * the latter. Putting one here is compliant to win16 spec,
1019 * but who knows the true value?
1021 put_word(res, 2); /* ResType */
1022 put_word(res, curg->ncursor);
1024 for(cur = curg->cursorlist; cur; cur = cur->next)
1026 cur = curg->cursorlist;
1029 for(; cur; cur = cur->prev)
1032 put_word(res, cur->width);
1033 /* FIXME: The height of a cursor is half the size of
1034 * the bitmap's height. BRC puts the height from the
1035 * BITMAPINFOHEADER here instead of the cursorfile's
1036 * height. MS doesn't seem to care...
1038 put_word(res, cur->height);
1039 /* FIXME: The next two are reversed in BRC and I don't
1040 * know why. Probably a bug. But, we can safely ignore
1041 * it because win16 does not support color cursors.
1042 * A warning should have been generated by the parser.
1044 put_word(res, cur->planes);
1045 put_word(res, cur->bits);
1046 /* FIXME: The +4 is the hotspot in the cursor resource.
1047 * However, I cound not find this in the documentation.
1048 * The hotspot bytes must either be included or MS
1051 put_dword(res, cur->data->size +4);
1052 put_word(res, cur->id);
1057 put_word(res, 0); /* Reserved */
1058 put_word(res, 2); /* ResType */
1059 put_word(res, curg->ncursor);
1061 for(cur = curg->cursorlist; cur; cur = cur->next)
1063 cur = curg->cursorlist;
1066 for(; cur; cur = cur->prev)
1069 put_word(res, cur->width);
1070 /* FIXME: The height of a cursor is half the size of
1071 * the bitmap's height. BRC puts the height from the
1072 * BITMAPINFOHEADER here instead of the cursorfile's
1073 * height. MS doesn't seem to care...
1075 put_word(res, cur->height);
1076 /* FIXME: The next two are reversed in BRC and I don't
1077 * know why. Probably a bug. But, we can safely ignore
1078 * it because win16 does not support color cursors.
1079 * A warning should have been generated by the parser.
1081 put_word(res, cur->planes);
1082 put_word(res, cur->bits);
1083 /* FIXME: The +4 is the hotspot in the cursor resource.
1084 * However, I cound not find this in the documentation.
1085 * The hotspot bytes must either be included or MS
1088 put_dword(res, cur->data->size +4);
1089 put_word(res, cur->id);
1092 SetResSize(res, restag); /* Set ResourceSize */
1100 *****************************************************************************
1101 * Function : cursor2res
1102 * Syntax : res_t *cursor2res(cursor_t *cur)
1104 * cur - The cursor descriptor
1105 * Output : New .res format structure
1108 *****************************************************************************
1110 static res_t *cursor2res(cursor_t *cur)
1116 assert(cur != NULL);
1119 name.type = name_ord;
1120 name.name.i_name = cur->id;
1121 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1122 put_word(res, cur->xhot);
1123 put_word(res, cur->yhot);
1124 put_raw_data(res, cur->data, 0);
1126 SetResSize(res, restag); /* Set ResourceSize */
1134 *****************************************************************************
1135 * Function : icongroup2res
1136 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1138 * name - Name/ordinal of the resource
1139 * icog - The icon group descriptor
1140 * Output : New .res format structure
1143 *****************************************************************************
1145 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1150 assert(name != NULL);
1151 assert(icog != NULL);
1154 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1157 put_word(res, 0); /* Reserved */
1158 /* FIXME: The ResType in the NEWHEADER structure should
1159 * contain 14 according to the MS win32 doc. This is
1160 * not the case with the BRC compiler and I really doubt
1161 * the latter. Putting one here is compliant to win16 spec,
1162 * but who knows the true value?
1164 put_word(res, 1); /* ResType */
1165 put_word(res, icog->nicon);
1166 for(ico = icog->iconlist; ico; ico = ico->next)
1168 put_byte(res, ico->width);
1169 put_byte(res, ico->height);
1170 put_byte(res, ico->nclr);
1171 put_byte(res, 0); /* Reserved */
1172 put_word(res, ico->planes);
1173 put_word(res, ico->bits);
1174 put_dword(res, ico->data->size);
1175 put_word(res, ico->id);
1180 put_word(res, 0); /* Reserved */
1181 put_word(res, 1); /* ResType */
1182 put_word(res, icog->nicon);
1183 for(ico = icog->iconlist; ico; ico = ico->next)
1185 put_byte(res, ico->width);
1186 put_byte(res, ico->height);
1187 put_byte(res, ico->nclr);
1188 put_byte(res, 0); /* Reserved */
1189 put_word(res, ico->planes);
1190 put_word(res, ico->bits);
1191 put_dword(res, ico->data->size);
1192 put_word(res, ico->id);
1195 SetResSize(res, restag); /* Set ResourceSize */
1203 *****************************************************************************
1204 * Function : icon2res
1205 * Syntax : res_t *icon2res(icon_t *ico)
1207 * ico - The icon descriptor
1208 * Output : New .res format structure
1211 *****************************************************************************
1213 static res_t *icon2res(icon_t *ico)
1219 assert(ico != NULL);
1222 name.type = name_ord;
1223 name.name.i_name = ico->id;
1224 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1225 put_raw_data(res, ico->data, 0);
1227 SetResSize(res, restag); /* Set ResourceSize */
1235 *****************************************************************************
1236 * Function : anicurico2res
1237 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1239 * name - Name/ordinal of the resource
1240 * ani - The animated object descriptor
1241 * Output : New .res format structure
1243 * Remarks : The endian of the object's structures have been converted
1245 * There are rumors that win311 could handle animated stuff.
1246 * That is why they are generated for both win16 and win32
1248 *****************************************************************************
1250 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1254 assert(name != NULL);
1255 assert(ani != NULL);
1258 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1259 NULL, name, ani->memopt, NULL);
1260 put_raw_data(res, ani->data, 0);
1261 /* Set ResourceSize */
1262 SetResSize(res, restag);
1269 *****************************************************************************
1270 * Function : bitmap2res
1271 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1273 * name - Name/ordinal of the resource
1274 * bmp - The bitmap descriptor
1275 * Output : New .res format structure
1277 * Remarks : The endian of the bitmap structures have been converted
1279 *****************************************************************************
1281 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1285 assert(name != NULL);
1286 assert(bmp != NULL);
1289 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1290 if(bmp->data->data[0] == 'B'
1291 && bmp->data->data[1] == 'M'
1292 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1293 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1295 /* The File header is still attached, don't write it */
1296 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1300 put_raw_data(res, bmp->data, 0);
1302 /* Set ResourceSize */
1303 SetResSize(res, restag);
1310 *****************************************************************************
1311 * Function : font2res
1312 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1314 * name - Name/ordinal of the resource
1315 * fnt - The font descriptor
1316 * Output : New .res format structure
1318 * Remarks : The data has been prepared just after parsing.
1319 *****************************************************************************
1321 static res_t *font2res(name_id_t *name, font_t *fnt)
1325 assert(name != NULL);
1326 assert(fnt != NULL);
1329 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1330 put_raw_data(res, fnt->data, 0);
1331 /* Set ResourceSize */
1332 SetResSize(res, restag);
1339 *****************************************************************************
1340 * Function : fontdir2res
1341 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1343 * name - Name/ordinal of the resource
1344 * fntdir - The fontdir descriptor
1345 * Output : New .res format structure
1347 * Remarks : The data has been prepared just after parsing.
1348 *****************************************************************************
1350 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1354 assert(name != NULL);
1355 assert(fnd != NULL);
1358 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1359 put_raw_data(res, fnd->data, 0);
1360 /* Set ResourceSize */
1361 SetResSize(res, restag);
1368 *****************************************************************************
1369 * Function : rcdata2res
1370 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1372 * name - Name/ordinal of the resource
1373 * rdt - The rcdata descriptor
1374 * Output : New .res format structure
1377 *****************************************************************************
1379 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1383 assert(name != NULL);
1384 assert(rdt != NULL);
1387 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1388 put_raw_data(res, rdt->data, 0);
1389 /* Set ResourceSize */
1390 SetResSize(res, restag);
1397 *****************************************************************************
1398 * Function : messagetable2res
1399 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1401 * name - Name/ordinal of the resource
1402 * msg - The messagetable descriptor
1403 * Output : New .res format structure
1405 * Remarks : The data has been converted to the appropriate endian
1406 * after is was parsed.
1407 *****************************************************************************
1409 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1413 assert(name != NULL);
1414 assert(msg != NULL);
1417 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1418 put_raw_data(res, msg->data, 0);
1419 /* Set ResourceSize */
1420 SetResSize(res, restag);
1427 *****************************************************************************
1428 * Function : stringtable2res
1429 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1431 * stt - The stringtable descriptor
1432 * Output : New .res format structure
1435 *****************************************************************************
1437 static res_t *stringtable2res(stringtable_t *stt)
1445 assert(stt != NULL);
1448 for(; stt; stt = stt->next)
1452 warning("Empty internal stringtable");
1455 name.type = name_ord;
1456 name.name.i_name = (stt->idbase >> 4) + 1;
1457 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1458 for(i = 0; i < stt->nentries; i++)
1460 if(stt->entries[i].str && stt->entries[i].str->size)
1462 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1463 FALSE, win32 ? stt->lvc.language : NULL);
1473 /* Set ResourceSize */
1474 SetResSize(res, restag - lastsize);
1477 lastsize = res->size;
1483 *****************************************************************************
1484 * Function : user2res
1485 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1487 * name - Name/ordinal of the resource
1488 * usr - The userresource descriptor
1489 * Output : New .res format structure
1492 *****************************************************************************
1494 static res_t *user2res(name_id_t *name, user_t *usr)
1498 assert(name != NULL);
1499 assert(usr != NULL);
1502 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1503 put_raw_data(res, usr->data, 0);
1504 /* Set ResourceSize */
1505 SetResSize(res, restag);
1512 *****************************************************************************
1513 * Function : versionblock2res
1514 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1516 * res - Binary resource to write to
1517 * blk - The version block to be written
1520 * Remarks : Self recursive
1521 *****************************************************************************
1523 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1532 blksizetag = res->size;
1533 put_word(res, 0); /* Will be overwritten later */
1536 put_word(res, 0); /* level ? */
1537 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1539 for(val = blk->values; val; val = val->next)
1541 if(val->type == val_str)
1543 valblksizetag = res->size;
1544 put_word(res, 0); /* Will be overwritten later */
1545 valvalsizetag = res->size;
1546 put_word(res, 0); /* Will be overwritten later */
1549 put_word(res, level);
1551 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1554 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1556 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1558 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1559 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1562 else if(val->type == val_words)
1564 valblksizetag = res->size;
1565 put_word(res, 0); /* Will be overwritten later */
1566 valvalsizetag = res->size;
1567 put_word(res, 0); /* Will be overwritten later */
1570 put_word(res, level);
1572 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1575 for(i = 0; i < val->value.words->nwords; i++)
1577 put_word(res, val->value.words->words[i]);
1579 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1580 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1583 else if(val->type == val_block)
1585 versionblock2res(res, val->value.block, level+1, lang);
1589 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1594 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1598 *****************************************************************************
1599 * Function : versioninfo2res
1600 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1602 * name - Name/ordinal of the resource
1603 * ver - The versioninfo descriptor
1604 * Output : New .res format structure
1607 *****************************************************************************
1609 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1612 int rootblocksizetag;
1619 assert(name != NULL);
1620 assert(ver != NULL);
1622 vsvi.type = str_char;
1623 vsvi.str.cstr = "VS_VERSION_INFO";
1624 vsvi.size = 15; /* Excl. termination */
1627 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1628 rootblocksizetag = res->size;
1629 put_word(res, 0); /* BlockSize filled in later */
1630 valsizetag = res->size;
1631 put_word(res, 0); /* ValueSize filled in later*/
1633 put_word(res, 0); /* Tree-level ? */
1634 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1635 TRUE, win32 ? ver->lvc.language : NULL);
1639 put_dword(res, VS_FFI_SIGNATURE);
1640 put_dword(res, VS_FFI_STRUCVERSION);
1641 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1642 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1643 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1644 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1645 put_dword(res, ver->fileflagsmask);
1646 put_dword(res, ver->fileflags);
1647 put_dword(res, ver->fileos);
1648 put_dword(res, ver->filetype);
1649 put_dword(res, ver->filesubtype);
1650 put_dword(res, 0); /* FileDateMS */
1651 put_dword(res, 0); /* FileDateLS */
1653 set_word(res, valsizetag, (WORD)(res->size - tag));
1654 /* Descend into the blocks */
1655 for(blk = ver->blocks; blk; blk = blk->next)
1656 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1657 /* Set root block's size */
1658 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1660 SetResSize(res, restag);
1668 *****************************************************************************
1669 * Function : toolbaritem2res
1670 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1674 * Remarks : Self recursive
1675 *****************************************************************************
1677 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1679 toolbar_item_t *itm = tbitem;
1683 put_word(res, itm->id);
1690 *****************************************************************************
1691 * Function : toolbar2res
1692 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1694 * name - Name/ordinal of the resource
1695 * toolbar - The toolbar descriptor
1696 * Output : New .res format structure
1699 *****************************************************************************
1701 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1705 assert(name != NULL);
1706 assert(toolbar != NULL);
1711 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1713 put_word(res, 1); /* Menuheader: Version */
1714 put_word(res, toolbar->button_width); /* (in pixels?) */
1715 put_word(res, toolbar->button_height); /* (in pixels?) */
1716 put_word(res, toolbar->nitems);
1718 toolbaritem2res(res, toolbar->items);
1719 /* Set ResourceSize */
1720 SetResSize(res, restag);
1725 /* Do not generate anything in 16-bit mode */
1734 *****************************************************************************
1735 * Function : dlginit2res
1736 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1738 * name - Name/ordinal of the resource
1739 * rdt - The dlginit descriptor
1740 * Output : New .res format structure
1743 *****************************************************************************
1745 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1749 assert(name != NULL);
1750 assert(dit != NULL);
1753 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1754 put_raw_data(res, dit->data, 0);
1755 /* Set ResourceSize */
1756 SetResSize(res, restag);
1763 *****************************************************************************
1764 * Function : prep_nid_for_label
1765 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1768 * Description : Converts a resource name into the first 32 (or less)
1769 * characters of the name with conversions.
1771 *****************************************************************************
1773 #define MAXNAMELEN 32
1774 char *prep_nid_for_label(name_id_t *nid)
1776 static char buf[MAXNAMELEN+1];
1778 assert(nid != NULL);
1780 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1784 sptr = nid->name.s_name->str.wstr;
1786 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1788 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1791 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1795 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1799 cptr = nid->name.s_name->str.cstr;
1801 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1803 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1806 warning("Resourcename (str_char) contain unprintable characters, ignored");
1810 else if(nid->type == name_ord)
1812 sprintf(buf, "%u", nid->name.i_name);
1816 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1823 *****************************************************************************
1824 * Function : make_c_name
1825 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1828 * Description : Converts a resource name into a valid c-identifier in the
1831 *****************************************************************************
1833 char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1840 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1841 buf = prep_nid_for_label(nid);
1842 nlen = strlen(buf) + strlen(lanbuf);
1843 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1844 ret = (char *)xmalloc(nlen);
1850 strcat(ret, lanbuf);
1855 *****************************************************************************
1856 * Function : get_c_typename
1857 * Syntax : char *get_c_typename(enum res_e type)
1860 * Description : Convert resource enum to char string to be used in c-name
1863 *****************************************************************************
1865 char *get_c_typename(enum res_e type)
1869 case res_acc: return "Acc";
1870 case res_anicur:return "AniCur";
1871 case res_aniico:return "AniIco";
1872 case res_bmp: return "Bmp";
1873 case res_cur: return "Cur";
1874 case res_curg: return "CurGrp";
1876 case res_dlgex: return "Dlg";
1877 case res_fnt: return "Fnt";
1878 case res_fntdir:return "FntDir";
1879 case res_ico: return "Ico";
1880 case res_icog: return "IcoGrp";
1882 case res_menex: return "Men";
1883 case res_rdt: return "RCDat";
1884 case res_stt: return "StrTab";
1885 case res_usr: return "Usr";
1886 case res_msg: return "MsgTab";
1887 case res_ver: return "VerInf";
1888 case res_toolbar: return "TlBr";
1889 case res_dlginit: return "DlgInit";
1890 default: return "Oops";
1895 *****************************************************************************
1896 * Function : resources2res
1897 * Syntax : void resources2res(resource_t *top)
1899 * top - The resource-tree to convert
1901 * Description : Convert logical resource descriptors into binary data
1903 *****************************************************************************
1905 void resources2res(resource_t *top)
1913 top->binres = accelerator2res(top->name, top->res.acc);
1917 top->binres = bitmap2res(top->name, top->res.bmp);
1921 top->binres = cursor2res(top->res.cur);
1925 top->binres = cursorgroup2res(top->name, top->res.curg);
1929 top->binres = dialog2res(top->name, top->res.dlg);
1933 top->binres = dialogex2res(top->name, top->res.dlgex);
1937 top->binres = font2res(top->name, top->res.fnt);
1941 top->binres = fontdir2res(top->name, top->res.fnd);
1945 top->binres = icon2res(top->res.ico);
1949 top->binres = icongroup2res(top->name, top->res.icog);
1953 top->binres = menu2res(top->name, top->res.men);
1957 top->binres = menuex2res(top->name, top->res.menex);
1961 top->binres = rcdata2res(top->name, top->res.rdt);
1965 top->binres = stringtable2res(top->res.stt);
1969 top->binres = user2res(top->name, top->res.usr);
1973 top->binres = messagetable2res(top->name, top->res.msg);
1977 top->binres = versioninfo2res(top->name, top->res.ver);
1981 top->binres = toolbar2res(top->name, top->res.tbt);
1985 top->binres = dlginit2res(top->name, top->res.dlgi);
1990 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1993 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
1995 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);