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 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
49 r = (res_t *)xmalloc(sizeof(res_t));
50 r->allocsize = RES_BLOCKSIZE;
52 r->data = (char *)xmalloc(RES_BLOCKSIZE);
56 res_t *grow_res(res_t *r, int add)
59 r->data = (char *)xrealloc(r->data, r->allocsize);
64 *****************************************************************************
68 * Syntax : void put_byte(res_t *res, unsigned c)
69 * void put_word(res_t *res, unsigned w)
70 * void put_dword(res_t *res, unsigned d)
72 * res - Binary resource to put the data in
73 * c, w, d - Data to put
75 * Description : Put primitives that put an item in the binary resource.
76 * The data array grows automatically.
78 *****************************************************************************
80 void put_byte(res_t *res, unsigned c)
82 if(res->allocsize - res->size < sizeof(char))
83 grow_res(res, RES_BLOCKSIZE);
84 res->data[res->size] = (char)c;
85 res->size += sizeof(char);
88 void put_word(res_t *res, unsigned w)
90 if(res->allocsize - res->size < sizeof(WORD))
91 grow_res(res, RES_BLOCKSIZE);
94 #ifdef WORDS_BIGENDIAN
98 res->data[res->size+0] = HIBYTE(w);
99 res->data[res->size+1] = LOBYTE(w);
102 #ifndef WORDS_BIGENDIAN
106 res->data[res->size+1] = HIBYTE(w);
107 res->data[res->size+0] = LOBYTE(w);
110 res->size += sizeof(WORD);
113 void put_dword(res_t *res, unsigned d)
115 if(res->allocsize - res->size < sizeof(DWORD))
116 grow_res(res, RES_BLOCKSIZE);
119 #ifdef WORDS_BIGENDIAN
123 res->data[res->size+0] = HIBYTE(HIWORD(d));
124 res->data[res->size+1] = LOBYTE(HIWORD(d));
125 res->data[res->size+2] = HIBYTE(LOWORD(d));
126 res->data[res->size+3] = LOBYTE(LOWORD(d));
129 #ifndef WORDS_BIGENDIAN
133 res->data[res->size+3] = HIBYTE(HIWORD(d));
134 res->data[res->size+2] = LOBYTE(HIWORD(d));
135 res->data[res->size+1] = HIBYTE(LOWORD(d));
136 res->data[res->size+0] = LOBYTE(LOWORD(d));
139 res->size += sizeof(DWORD);
142 void put_pad(res_t *res)
144 while(res->size & 0x3)
149 *****************************************************************************
150 * Function : set_word
152 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
153 * void set_dword(res_t *res, int ofs, unsigned d)
155 * res - Binary resource to put the data in
156 * ofs - Byte offset in data-array
159 * Description : Set the value of a binary resource data array to a
162 *****************************************************************************
164 void set_word(res_t *res, int ofs, unsigned w)
168 #ifdef WORDS_BIGENDIAN
172 res->data[ofs+0] = HIBYTE(w);
173 res->data[ofs+1] = LOBYTE(w);
176 #ifndef WORDS_BIGENDIAN
180 res->data[ofs+1] = HIBYTE(w);
181 res->data[ofs+0] = LOBYTE(w);
186 void set_dword(res_t *res, int ofs, unsigned d)
190 #ifdef WORDS_BIGENDIAN
194 res->data[ofs+0] = HIBYTE(HIWORD(d));
195 res->data[ofs+1] = LOBYTE(HIWORD(d));
196 res->data[ofs+2] = HIBYTE(LOWORD(d));
197 res->data[ofs+3] = LOBYTE(LOWORD(d));
200 #ifndef WORDS_BIGENDIAN
204 res->data[ofs+3] = HIBYTE(HIWORD(d));
205 res->data[ofs+2] = LOBYTE(HIWORD(d));
206 res->data[ofs+1] = HIBYTE(LOWORD(d));
207 res->data[ofs+0] = LOBYTE(LOWORD(d));
213 *****************************************************************************
214 * Function : get_word
216 * Syntax : WORD get_word(res_t *res, int ofs)
217 * DWORD get_dword(res_t *res, int ofs)
219 * res - Binary resource to put the data in
220 * ofs - Byte offset in data-array
221 * Output : The data in native endian
222 * Description : Get the value of a binary resource data array in native
225 *****************************************************************************
227 WORD get_word(res_t *res, int ofs)
231 #ifdef WORDS_BIGENDIAN
235 return (res->data[ofs+0] << 8)
238 #ifndef WORDS_BIGENDIAN
242 return (res->data[ofs+1] << 8)
247 DWORD get_dword(res_t *res, int ofs)
251 #ifdef WORDS_BIGENDIAN
255 return (res->data[ofs+0] << 24)
256 | (res->data[ofs+1] << 16)
257 | (res->data[ofs+2] << 8)
260 #ifndef WORDS_BIGENDIAN
264 return (res->data[ofs+3] << 24)
265 | (res->data[ofs+2] << 16)
266 | (res->data[ofs+1] << 8)
272 *****************************************************************************
273 * Function : string_to_upper
274 * Syntax : void string_to_upper(string_t *str)
278 * Remarks : FIXME: codepages...
279 *****************************************************************************
281 void string_to_upper(string_t *str)
283 if(str->type == str_char)
285 char *cptr = str->str.cstr;
287 *cptr = (char)toupper(*cptr);
289 else if(str->type == str_unicode)
291 short *sptr = str->str.wstr;
294 *sptr = (short)toupper(*sptr);
298 internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
303 *****************************************************************************
304 * Function : put_string
305 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
308 * res - Binary resource to put the data in
309 * str - String to put
310 * type - Data has to be written in either str_char or str_unicode
311 * isterm - The string is '\0' terminated (disregard the string's
316 *****************************************************************************
318 static void put_string(res_t *res, string_t *str, enum str_e type, int isterm)
324 if(!isterm && str->size == 0)
326 warning("String length is zero, not written");
330 str = convert_string(str, type);
331 if(str->type == str_unicode && type == str_unicode)
333 for(cnt = 0; cnt < str->size; cnt++)
335 c = str->str.wstr[cnt];
340 if(isterm && (str->size == 0 || (cnt == str->size && c)))
343 else if(str->type == str_char && type == str_char)
345 for(cnt = 0; cnt < str->size; cnt++)
347 c = str->str.cstr[cnt];
352 if(isterm && (str->size == 0 || (cnt == str->size && c)))
355 else if(str->type == str_unicode && type == str_char)
357 for(cnt = 0; cnt < str->size; cnt++)
359 c = str->str.wstr[cnt];
364 if(isterm && (str->size == 0 || (cnt == str->size && c)))
367 else /* str->type == str_char && type == str_unicode */
369 for(cnt = 0; cnt < str->size; cnt++)
371 c = str->str.cstr[cnt];
372 put_word(res, c & 0xff);
376 if(isterm && (str->size == 0 || (cnt == str->size && c)))
383 *****************************************************************************
384 * Function : put_name_id
385 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase)
390 *****************************************************************************
392 void put_name_id(res_t *res, name_id_t *nid, int upcase)
394 if(nid->type == name_ord)
397 put_word(res, 0xffff);
400 put_word(res, (WORD)nid->name.i_name);
402 else if(nid->type == name_str)
405 string_to_upper(nid->name.s_name);
406 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE);
410 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
415 *****************************************************************************
417 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
422 *****************************************************************************
424 void put_lvc(res_t *res, lvc_t *lvc)
426 if(lvc && lvc->language)
427 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
429 put_word(res, 0); /* Neutral */
430 if(lvc && lvc->version)
431 put_dword(res, *(lvc->version));
434 if(lvc && lvc->characts)
435 put_dword(res, *(lvc->characts));
438 if(lvc && lvc->language)
439 set_language( lvc->language->id, lvc->language->sub );
440 else if (currentlanguage)
441 set_language( currentlanguage->id, currentlanguage->sub );
443 set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
447 *****************************************************************************
448 * Function : put_raw_data
449 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
454 *****************************************************************************
456 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
458 int wsize = raw->size - offset;
459 if(res->allocsize - res->size < wsize)
460 grow_res(res, wsize);
461 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
466 *****************************************************************************
467 * Function : put_res_header
468 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
469 * name_id_t *name, DWORD memopt, lvc_t *lvc)
472 * res - Binary resource descriptor to write to
473 * type - Resource identifier (if ntype == NULL)
474 * ntype - Name id of type
475 * name - Resource's name
476 * memopt - Resource's memory options to write
477 * lvc - Language, version and characteristics (win32 only)
478 * Output : An index to the resource size field. The resource size field
479 * contains the header size upon exit.
482 *****************************************************************************
484 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
485 DWORD memopt, lvc_t *lvc)
489 put_dword(res, 0); /* We will overwrite these later */
493 put_word(res, 0xffff); /* ResType */
497 put_name_id(res, ntype, TRUE);
498 put_name_id(res, name, TRUE); /* ResName */
500 put_dword(res, 0); /* DataVersion */
501 put_word(res, memopt); /* Memory options */
502 put_lvc(res, lvc); /* Language, version and characts */
503 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
504 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
505 res->dataidx = res->size;
513 put_byte(res, 0xff); /* ResType */
517 put_name_id(res, ntype, TRUE);
518 put_name_id(res, name, TRUE); /* ResName */
519 put_word(res, memopt); /* Memory options */
521 put_dword(res, 0); /* ResSize overwritten later*/
522 set_dword(res, tag, res->size);
523 res->dataidx = res->size;
529 *****************************************************************************
530 * Function : accelerator2res
531 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
533 * name - Name/ordinal of the resource
534 * acc - The accelerator descriptor
535 * Output : New .res format structure
538 *****************************************************************************
540 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
545 assert(name != NULL);
552 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
555 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
556 put_word(res, ev->key);
557 put_word(res, ev->id);
558 put_word(res, 0); /* Padding */
565 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
568 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
569 put_word(res, ev->key);
570 put_word(res, ev->id);
574 /* Set ResourceSize */
575 SetResSize(res, restag);
580 *****************************************************************************
581 * Function : dialog2res
582 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
584 * name - Name/ordinal of the resource
585 * dlg - The dialog descriptor
586 * Output : New .res format structure
589 *****************************************************************************
591 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
598 assert(name != NULL);
601 ctrl = dlg->controls;
605 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
607 put_dword(res, dlg->style->or_mask);
608 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
609 tag_nctrl = res->size;
610 put_word(res, 0); /* Number of controls */
611 put_word(res, dlg->x);
612 put_word(res, dlg->y);
613 put_word(res, dlg->width);
614 put_word(res, dlg->height);
616 put_name_id(res, dlg->menu, TRUE);
620 put_name_id(res, dlg->dlgclass, TRUE);
624 put_string(res, dlg->title, str_unicode, TRUE);
629 put_word(res, dlg->font->size);
630 put_string(res, dlg->font->name, str_unicode, TRUE);
636 /* FIXME: what is default control style? */
637 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
638 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
639 put_word(res, ctrl->x);
640 put_word(res, ctrl->y);
641 put_word(res, ctrl->width);
642 put_word(res, ctrl->height);
643 put_word(res, ctrl->id);
645 put_name_id(res, ctrl->ctlclass, TRUE);
647 internal_error(__FILE__, __LINE__, "Control has no control-class");
649 put_name_id(res, ctrl->title, FALSE);
654 put_word(res, ctrl->extra->size+2);
656 put_raw_data(res, ctrl->extra, 0);
666 /* Set number of controls */
667 set_word(res, tag_nctrl, (WORD)nctrl);
671 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
673 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
674 tag_nctrl = res->size;
675 put_byte(res, 0); /* Number of controls */
676 put_word(res, dlg->x);
677 put_word(res, dlg->y);
678 put_word(res, dlg->width);
679 put_word(res, dlg->height);
681 put_name_id(res, dlg->menu, TRUE);
685 put_name_id(res, dlg->dlgclass, TRUE);
689 put_string(res, dlg->title, str_char, TRUE);
694 put_word(res, dlg->font->size);
695 put_string(res, dlg->font->name, str_char, TRUE);
700 put_word(res, ctrl->x);
701 put_word(res, ctrl->y);
702 put_word(res, ctrl->width);
703 put_word(res, ctrl->height);
704 put_word(res, ctrl->id);
705 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
708 if(ctrl->ctlclass->type == name_ord
709 && ctrl->ctlclass->name.i_name >= 0x80
710 && ctrl->ctlclass->name.i_name <= 0x85)
711 put_byte(res, ctrl->ctlclass->name.i_name);
712 else if(ctrl->ctlclass->type == name_str)
713 put_name_id(res, ctrl->ctlclass, FALSE);
715 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
718 internal_error(__FILE__, __LINE__, "Control has no control-class");
720 put_name_id(res, ctrl->title, FALSE);
724 /* FIXME: What is this extra byte doing here? */
730 /* Set number of controls */
731 ((char *)res->data)[tag_nctrl] = (char)nctrl;
733 /* Set ResourceSize */
734 SetResSize(res, restag);
739 *****************************************************************************
740 * Function : dialogex2res
741 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
743 * name - Name/ordinal of the resource
744 * dlgex - The dialogex descriptor
745 * Output : New .res format structure
748 *****************************************************************************
750 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
757 assert(name != NULL);
758 assert(dlgex != NULL);
760 ctrl = dlgex->controls;
764 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
766 /* FIXME: MS doc says thet the first word must contain 0xffff
767 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
768 * compiler reverses the two words.
769 * I don't know which one to choose, but I write it as Mr. B
772 put_word(res, 1); /* Signature */
773 put_word(res, 0xffff); /* DlgVer */
774 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
775 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
776 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
777 tag_nctrl = res->size;
778 put_word(res, 0); /* Number of controls */
779 put_word(res, dlgex->x);
780 put_word(res, dlgex->y);
781 put_word(res, dlgex->width);
782 put_word(res, dlgex->height);
784 put_name_id(res, dlgex->menu, TRUE);
788 put_name_id(res, dlgex->dlgclass, TRUE);
792 put_string(res, dlgex->title, str_unicode, TRUE);
797 put_word(res, dlgex->font->size);
798 put_word(res, dlgex->font->weight);
799 /* FIXME: ? TRUE should be sufficient to say that its
800 * italic, but Borland's compiler says its 0x0101.
801 * I just copy it here, and hope for the best.
803 put_word(res, dlgex->font->italic ? 0x0101 : 0);
804 put_string(res, dlgex->font->name, str_unicode, TRUE);
810 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
811 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
812 /* FIXME: what is default control style? */
813 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
814 put_word(res, ctrl->x);
815 put_word(res, ctrl->y);
816 put_word(res, ctrl->width);
817 put_word(res, ctrl->height);
818 put_word(res, ctrl->id);
819 /* FIXME: Pad is _NOT_ documented!?! */
822 put_name_id(res, ctrl->ctlclass, TRUE);
824 internal_error(__FILE__, __LINE__, "Control has no control-class");
826 put_name_id(res, ctrl->title, FALSE);
832 put_word(res, ctrl->extra->size);
833 put_raw_data(res, ctrl->extra, 0);
842 /* Set number of controls */
843 set_word(res, tag_nctrl, (WORD)nctrl);
844 /* Set ResourceSize */
845 SetResSize(res, restag);
850 /* Do not generate anything in 16-bit mode */
859 *****************************************************************************
860 * Function : menuitem2res
861 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
865 * Remarks : Self recursive
866 *****************************************************************************
868 static void menuitem2res(res_t *res, menu_item_t *menitem)
870 menu_item_t *itm = menitem;
875 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
877 put_word(res, itm->id);
879 put_string(res, itm->name, str_unicode, TRUE);
883 menuitem2res(res, itm->popup);
891 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
893 put_word(res, itm->id);
895 put_string(res, itm->name, str_char, TRUE);
899 menuitem2res(res, itm->popup);
907 *****************************************************************************
908 * Function : menu2res
909 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
911 * name - Name/ordinal of the resource
912 * men - The menu descriptor
913 * Output : New .res format structure
916 *****************************************************************************
918 static res_t *menu2res(name_id_t *name, menu_t *men)
922 assert(name != NULL);
926 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
928 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
929 menuitem2res(res, men->items);
930 /* Set ResourceSize */
931 SetResSize(res, restag);
938 *****************************************************************************
939 * Function : menuexitem2res
940 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
944 * Remarks : Self recursive
945 *****************************************************************************
947 static void menuexitem2res(res_t *res, menuex_item_t *menitem)
949 menuex_item_t *itm = menitem;
953 put_dword(res, itm->gottype ? itm->type : 0);
954 put_dword(res, itm->gotstate ? itm->state : 0);
955 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
956 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
958 put_string(res, itm->name, str_unicode, TRUE);
964 put_dword(res, itm->gothelpid ? itm->helpid : 0);
965 menuexitem2res(res, itm->popup);
973 *****************************************************************************
974 * Function : menuex2res
975 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
977 * name - Name/ordinal of the resource
978 * menex - The menuex descriptor
979 * Output : New .res format structure
982 *****************************************************************************
984 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
988 assert(name != NULL);
989 assert(menex != NULL);
994 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
996 put_word(res, 1); /* Menuheader: Version */
997 put_word(res, 4); /* Offset */
998 put_dword(res, 0); /* HelpId */
1000 menuexitem2res(res, menex->items);
1001 /* Set ResourceSize */
1002 SetResSize(res, restag);
1007 /* Do not generate anything in 16-bit mode */
1016 *****************************************************************************
1017 * Function : cursorgroup2res
1018 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
1020 * name - Name/ordinal of the resource
1021 * curg - The cursor descriptor
1022 * Output : New .res format structure
1025 *****************************************************************************
1027 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
1032 assert(name != NULL);
1033 assert(curg != NULL);
1036 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
1039 put_word(res, 0); /* Reserved */
1040 /* FIXME: The ResType in the NEWHEADER structure should
1041 * contain 14 according to the MS win32 doc. This is
1042 * not the case with the BRC compiler and I really doubt
1043 * the latter. Putting one here is compliant to win16 spec,
1044 * but who knows the true value?
1046 put_word(res, 2); /* ResType */
1047 put_word(res, curg->ncursor);
1049 for(cur = curg->cursorlist; cur; cur = cur->next)
1051 cur = curg->cursorlist;
1054 for(; cur; cur = cur->prev)
1057 put_word(res, cur->width);
1058 /* FIXME: The height of a cursor is half the size of
1059 * the bitmap's height. BRC puts the height from the
1060 * BITMAPINFOHEADER here instead of the cursorfile's
1061 * height. MS doesn't seem to care...
1063 put_word(res, cur->height);
1064 /* FIXME: The next two are reversed in BRC and I don't
1065 * know why. Probably a bug. But, we can safely ignore
1066 * it because win16 does not support color cursors.
1067 * A warning should have been generated by the parser.
1069 put_word(res, cur->planes);
1070 put_word(res, cur->bits);
1071 /* FIXME: The +4 is the hotspot in the cursor resource.
1072 * However, I cound not find this in the documentation.
1073 * The hotspot bytes must either be included or MS
1076 put_dword(res, cur->data->size +4);
1077 put_word(res, cur->id);
1082 put_word(res, 0); /* Reserved */
1083 put_word(res, 2); /* ResType */
1084 put_word(res, curg->ncursor);
1086 for(cur = curg->cursorlist; cur; cur = cur->next)
1088 cur = curg->cursorlist;
1091 for(; cur; cur = cur->prev)
1094 put_word(res, cur->width);
1095 /* FIXME: The height of a cursor is half the size of
1096 * the bitmap's height. BRC puts the height from the
1097 * BITMAPINFOHEADER here instead of the cursorfile's
1098 * height. MS doesn't seem to care...
1100 put_word(res, cur->height);
1101 /* FIXME: The next two are reversed in BRC and I don't
1102 * know why. Probably a bug. But, we can safely ignore
1103 * it because win16 does not support color cursors.
1104 * A warning should have been generated by the parser.
1106 put_word(res, cur->planes);
1107 put_word(res, cur->bits);
1108 /* FIXME: The +4 is the hotspot in the cursor resource.
1109 * However, I cound not find this in the documentation.
1110 * The hotspot bytes must either be included or MS
1113 put_dword(res, cur->data->size +4);
1114 put_word(res, cur->id);
1117 SetResSize(res, restag); /* Set ResourceSize */
1125 *****************************************************************************
1126 * Function : cursor2res
1127 * Syntax : res_t *cursor2res(cursor_t *cur)
1129 * cur - The cursor descriptor
1130 * Output : New .res format structure
1133 *****************************************************************************
1135 static res_t *cursor2res(cursor_t *cur)
1141 assert(cur != NULL);
1144 name.type = name_ord;
1145 name.name.i_name = cur->id;
1146 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1147 put_word(res, cur->xhot);
1148 put_word(res, cur->yhot);
1149 put_raw_data(res, cur->data, 0);
1151 SetResSize(res, restag); /* Set ResourceSize */
1159 *****************************************************************************
1160 * Function : icongroup2res
1161 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1163 * name - Name/ordinal of the resource
1164 * icog - The icon group descriptor
1165 * Output : New .res format structure
1168 *****************************************************************************
1170 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1175 assert(name != NULL);
1176 assert(icog != NULL);
1179 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1182 put_word(res, 0); /* Reserved */
1183 /* FIXME: The ResType in the NEWHEADER structure should
1184 * contain 14 according to the MS win32 doc. This is
1185 * not the case with the BRC compiler and I really doubt
1186 * the latter. Putting one here is compliant to win16 spec,
1187 * but who knows the true value?
1189 put_word(res, 1); /* ResType */
1190 put_word(res, icog->nicon);
1191 for(ico = icog->iconlist; ico; ico = ico->next)
1193 put_byte(res, ico->width);
1194 put_byte(res, ico->height);
1195 put_byte(res, ico->nclr);
1196 put_byte(res, 0); /* Reserved */
1197 put_word(res, ico->planes);
1198 put_word(res, ico->bits);
1199 put_dword(res, ico->data->size);
1200 put_word(res, ico->id);
1205 put_word(res, 0); /* Reserved */
1206 put_word(res, 1); /* ResType */
1207 put_word(res, icog->nicon);
1208 for(ico = icog->iconlist; ico; ico = ico->next)
1210 put_byte(res, ico->width);
1211 put_byte(res, ico->height);
1212 put_byte(res, ico->nclr);
1213 put_byte(res, 0); /* Reserved */
1214 put_word(res, ico->planes);
1215 put_word(res, ico->bits);
1216 put_dword(res, ico->data->size);
1217 put_word(res, ico->id);
1220 SetResSize(res, restag); /* Set ResourceSize */
1228 *****************************************************************************
1229 * Function : icon2res
1230 * Syntax : res_t *icon2res(icon_t *ico)
1232 * ico - The icon descriptor
1233 * Output : New .res format structure
1236 *****************************************************************************
1238 static res_t *icon2res(icon_t *ico)
1244 assert(ico != NULL);
1247 name.type = name_ord;
1248 name.name.i_name = ico->id;
1249 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1250 put_raw_data(res, ico->data, 0);
1252 SetResSize(res, restag); /* Set ResourceSize */
1260 *****************************************************************************
1261 * Function : anicurico2res
1262 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1264 * name - Name/ordinal of the resource
1265 * ani - The animated object descriptor
1266 * Output : New .res format structure
1268 * Remarks : The endian of the object's structures have been converted
1270 * There are rumors that win311 could handle animated stuff.
1271 * That is why they are generated for both win16 and win32
1273 *****************************************************************************
1275 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1279 assert(name != NULL);
1280 assert(ani != NULL);
1283 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1284 NULL, name, ani->memopt, NULL);
1285 put_raw_data(res, ani->data, 0);
1286 /* Set ResourceSize */
1287 SetResSize(res, restag);
1294 *****************************************************************************
1295 * Function : bitmap2res
1296 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1298 * name - Name/ordinal of the resource
1299 * bmp - The bitmap descriptor
1300 * Output : New .res format structure
1302 * Remarks : The endian of the bitmap structures have been converted
1304 *****************************************************************************
1306 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1310 assert(name != NULL);
1311 assert(bmp != NULL);
1314 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1315 if(bmp->data->data[0] == 'B'
1316 && bmp->data->data[1] == 'M'
1317 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1318 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1320 /* The File header is still attached, don't write it */
1321 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1325 put_raw_data(res, bmp->data, 0);
1327 /* Set ResourceSize */
1328 SetResSize(res, restag);
1335 *****************************************************************************
1336 * Function : font2res
1337 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1339 * name - Name/ordinal of the resource
1340 * fnt - The font descriptor
1341 * Output : New .res format structure
1343 * Remarks : The data has been prepared just after parsing.
1344 *****************************************************************************
1346 static res_t *font2res(name_id_t *name, font_t *fnt)
1350 assert(name != NULL);
1351 assert(fnt != NULL);
1354 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1355 put_raw_data(res, fnt->data, 0);
1356 /* Set ResourceSize */
1357 SetResSize(res, restag);
1364 *****************************************************************************
1365 * Function : fontdir2res
1366 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1368 * name - Name/ordinal of the resource
1369 * fntdir - The fontdir descriptor
1370 * Output : New .res format structure
1372 * Remarks : The data has been prepared just after parsing.
1373 *****************************************************************************
1375 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1379 assert(name != NULL);
1380 assert(fnd != NULL);
1383 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1384 put_raw_data(res, fnd->data, 0);
1385 /* Set ResourceSize */
1386 SetResSize(res, restag);
1393 *****************************************************************************
1394 * Function : rcdata2res
1395 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1397 * name - Name/ordinal of the resource
1398 * rdt - The rcdata descriptor
1399 * Output : New .res format structure
1402 *****************************************************************************
1404 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1408 assert(name != NULL);
1409 assert(rdt != NULL);
1412 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1413 put_raw_data(res, rdt->data, 0);
1414 /* Set ResourceSize */
1415 SetResSize(res, restag);
1422 *****************************************************************************
1423 * Function : messagetable2res
1424 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1426 * name - Name/ordinal of the resource
1427 * msg - The messagetable descriptor
1428 * Output : New .res format structure
1430 * Remarks : The data has been converted to the appropriate endian
1431 * after is was parsed.
1432 *****************************************************************************
1434 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1438 assert(name != NULL);
1439 assert(msg != NULL);
1442 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1443 put_raw_data(res, msg->data, 0);
1444 /* Set ResourceSize */
1445 SetResSize(res, restag);
1452 *****************************************************************************
1453 * Function : stringtable2res
1454 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1456 * stt - The stringtable descriptor
1457 * Output : New .res format structure
1460 *****************************************************************************
1462 static res_t *stringtable2res(stringtable_t *stt)
1470 assert(stt != NULL);
1473 for(; stt; stt = stt->next)
1477 warning("Empty internal stringtable");
1480 name.type = name_ord;
1481 name.name.i_name = (stt->idbase >> 4) + 1;
1482 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1483 for(i = 0; i < stt->nentries; i++)
1485 if(stt->entries[i].str && stt->entries[i].str->size)
1487 string_t *str = convert_string(stt->entries[i].str, win32 ? str_unicode : str_char);
1489 put_word(res, str->size);
1491 put_byte(res, str->size);
1492 put_string(res, str, win32 ? str_unicode : str_char, FALSE);
1503 /* Set ResourceSize */
1504 SetResSize(res, restag - lastsize);
1507 lastsize = res->size;
1513 *****************************************************************************
1514 * Function : user2res
1515 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1517 * name - Name/ordinal of the resource
1518 * usr - The userresource descriptor
1519 * Output : New .res format structure
1522 *****************************************************************************
1524 static res_t *user2res(name_id_t *name, user_t *usr)
1528 assert(name != NULL);
1529 assert(usr != NULL);
1532 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1533 put_raw_data(res, usr->data, 0);
1534 /* Set ResourceSize */
1535 SetResSize(res, restag);
1542 *****************************************************************************
1543 * Function : versionblock2res
1544 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1546 * res - Binary resource to write to
1547 * blk - The version block to be written
1550 * Remarks : Self recursive
1551 *****************************************************************************
1553 static void versionblock2res(res_t *res, ver_block_t *blk, int level)
1562 blksizetag = res->size;
1563 put_word(res, 0); /* Will be overwritten later */
1566 put_word(res, 0); /* level ? */
1567 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE);
1569 for(val = blk->values; val; val = val->next)
1571 if(val->type == val_str)
1573 valblksizetag = res->size;
1574 put_word(res, 0); /* Will be overwritten later */
1575 valvalsizetag = res->size;
1576 put_word(res, 0); /* Will be overwritten later */
1579 put_word(res, level);
1581 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1584 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE);
1586 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1588 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1589 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1592 else if(val->type == val_words)
1594 valblksizetag = res->size;
1595 put_word(res, 0); /* Will be overwritten later */
1596 valvalsizetag = res->size;
1597 put_word(res, 0); /* Will be overwritten later */
1600 put_word(res, level);
1602 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1605 for(i = 0; i < val->value.words->nwords; i++)
1607 put_word(res, val->value.words->words[i]);
1609 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1610 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1613 else if(val->type == val_block)
1615 versionblock2res(res, val->value.block, level+1);
1619 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1624 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1628 *****************************************************************************
1629 * Function : versioninfo2res
1630 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1632 * name - Name/ordinal of the resource
1633 * ver - The versioninfo descriptor
1634 * Output : New .res format structure
1637 *****************************************************************************
1639 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1642 int rootblocksizetag;
1649 assert(name != NULL);
1650 assert(ver != NULL);
1652 vsvi.type = str_char;
1653 vsvi.str.cstr = "VS_VERSION_INFO";
1654 vsvi.size = 15; /* Excl. termination */
1657 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1658 rootblocksizetag = res->size;
1659 put_word(res, 0); /* BlockSize filled in later */
1660 valsizetag = res->size;
1661 put_word(res, 0); /* ValueSize filled in later*/
1663 put_word(res, 0); /* Tree-level ? */
1664 put_string(res, &vsvi, win32 ? str_unicode : str_char, TRUE);
1668 put_dword(res, VS_FFI_SIGNATURE);
1669 put_dword(res, VS_FFI_STRUCVERSION);
1670 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1671 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1672 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1673 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1674 put_dword(res, ver->fileflagsmask);
1675 put_dword(res, ver->fileflags);
1676 put_dword(res, ver->fileos);
1677 put_dword(res, ver->filetype);
1678 put_dword(res, ver->filesubtype);
1679 put_dword(res, 0); /* FileDateMS */
1680 put_dword(res, 0); /* FileDateLS */
1682 set_word(res, valsizetag, (WORD)(res->size - tag));
1683 /* Descend into the blocks */
1684 for(blk = ver->blocks; blk; blk = blk->next)
1685 versionblock2res(res, blk, 0);
1686 /* Set root block's size */
1687 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1689 SetResSize(res, restag);
1697 *****************************************************************************
1698 * Function : toolbaritem2res
1699 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1703 * Remarks : Self recursive
1704 *****************************************************************************
1706 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1708 toolbar_item_t *itm = tbitem;
1712 put_word(res, itm->id);
1719 *****************************************************************************
1720 * Function : toolbar2res
1721 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1723 * name - Name/ordinal of the resource
1724 * toolbar - The toolbar descriptor
1725 * Output : New .res format structure
1728 *****************************************************************************
1730 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1734 assert(name != NULL);
1735 assert(toolbar != NULL);
1740 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1742 put_word(res, 1); /* Menuheader: Version */
1743 put_word(res, toolbar->button_width); /* (in pixels?) */
1744 put_word(res, toolbar->button_height); /* (in pixels?) */
1745 put_word(res, toolbar->nitems);
1747 toolbaritem2res(res, toolbar->items);
1748 /* Set ResourceSize */
1749 SetResSize(res, restag);
1754 /* Do not generate anything in 16-bit mode */
1763 *****************************************************************************
1764 * Function : dlginit2res
1765 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1767 * name - Name/ordinal of the resource
1768 * rdt - The dlginit descriptor
1769 * Output : New .res format structure
1772 *****************************************************************************
1774 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1778 assert(name != NULL);
1779 assert(dit != NULL);
1782 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1783 put_raw_data(res, dit->data, 0);
1784 /* Set ResourceSize */
1785 SetResSize(res, restag);
1792 *****************************************************************************
1793 * Function : prep_nid_for_label
1794 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1797 * Description : Converts a resource name into the first 32 (or less)
1798 * characters of the name with conversions.
1800 *****************************************************************************
1802 #define MAXNAMELEN 32
1803 char *prep_nid_for_label(name_id_t *nid)
1805 static char buf[MAXNAMELEN+1];
1807 assert(nid != NULL);
1809 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1813 sptr = nid->name.s_name->str.wstr;
1815 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1817 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1820 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1824 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1828 cptr = nid->name.s_name->str.cstr;
1830 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1832 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1835 warning("Resourcename (str_char) contain unprintable characters, ignored");
1839 else if(nid->type == name_ord)
1841 sprintf(buf, "%u", nid->name.i_name);
1845 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1852 *****************************************************************************
1853 * Function : make_c_name
1854 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1857 * Description : Converts a resource name into a valid c-identifier in the
1860 *****************************************************************************
1862 char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1869 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1870 buf = prep_nid_for_label(nid);
1871 nlen = strlen(buf) + strlen(lanbuf);
1872 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1873 ret = (char *)xmalloc(nlen);
1879 strcat(ret, lanbuf);
1884 *****************************************************************************
1885 * Function : get_c_typename
1886 * Syntax : char *get_c_typename(enum res_e type)
1889 * Description : Convert resource enum to char string to be used in c-name
1892 *****************************************************************************
1894 char *get_c_typename(enum res_e type)
1898 case res_acc: return "Acc";
1899 case res_anicur:return "AniCur";
1900 case res_aniico:return "AniIco";
1901 case res_bmp: return "Bmp";
1902 case res_cur: return "Cur";
1903 case res_curg: return "CurGrp";
1905 case res_dlgex: return "Dlg";
1906 case res_fnt: return "Fnt";
1907 case res_fntdir:return "FntDir";
1908 case res_ico: return "Ico";
1909 case res_icog: return "IcoGrp";
1911 case res_menex: return "Men";
1912 case res_rdt: return "RCDat";
1913 case res_stt: return "StrTab";
1914 case res_usr: return "Usr";
1915 case res_msg: return "MsgTab";
1916 case res_ver: return "VerInf";
1917 case res_toolbar: return "TlBr";
1918 case res_dlginit: return "DlgInit";
1919 default: return "Oops";
1924 *****************************************************************************
1925 * Function : resources2res
1926 * Syntax : void resources2res(resource_t *top)
1928 * top - The resource-tree to convert
1930 * Description : Convert logical resource descriptors into binary data
1932 *****************************************************************************
1934 void resources2res(resource_t *top)
1942 top->binres = accelerator2res(top->name, top->res.acc);
1946 top->binres = bitmap2res(top->name, top->res.bmp);
1950 top->binres = cursor2res(top->res.cur);
1954 top->binres = cursorgroup2res(top->name, top->res.curg);
1958 top->binres = dialog2res(top->name, top->res.dlg);
1962 top->binres = dialogex2res(top->name, top->res.dlgex);
1966 top->binres = font2res(top->name, top->res.fnt);
1970 top->binres = fontdir2res(top->name, top->res.fnd);
1974 top->binres = icon2res(top->res.ico);
1978 top->binres = icongroup2res(top->name, top->res.icog);
1982 top->binres = menu2res(top->name, top->res.men);
1986 top->binres = menuex2res(top->name, top->res.menex);
1990 top->binres = rcdata2res(top->name, top->res.rdt);
1994 top->binres = stringtable2res(top->res.stt);
1998 top->binres = user2res(top->name, top->res.usr);
2002 top->binres = messagetable2res(top->name, top->res.msg);
2006 top->binres = versioninfo2res(top->name, top->res.ver);
2010 top->binres = toolbar2res(top->name, top->res.tbt);
2014 top->binres = dlginit2res(top->name, top->res.dlgi);
2019 top->binres = anicurico2res(top->name, top->res.ani, top->type);
2022 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
2024 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);