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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 = xmalloc(sizeof(res_t));
52 r->allocsize = RES_BLOCKSIZE;
55 r->data = xmalloc(RES_BLOCKSIZE);
59 res_t *grow_res(res_t *r, unsigned int add)
62 r->data = xrealloc(r->data, r->allocsize);
67 *****************************************************************************
71 * Syntax : void put_byte(res_t *res, unsigned c)
72 * void put_word(res_t *res, unsigned w)
73 * void put_dword(res_t *res, unsigned d)
75 * res - Binary resource to put the data in
76 * c, w, d - Data to put
78 * Description : Put primitives that put an item in the binary resource.
79 * The data array grows automatically.
81 *****************************************************************************
83 void put_byte(res_t *res, unsigned c)
85 if(res->allocsize - res->size < sizeof(char))
86 grow_res(res, RES_BLOCKSIZE);
87 res->data[res->size] = (char)c;
88 res->size += sizeof(char);
91 void put_word(res_t *res, unsigned w)
93 if(res->allocsize - res->size < sizeof(WORD))
94 grow_res(res, RES_BLOCKSIZE);
97 #ifdef WORDS_BIGENDIAN
101 res->data[res->size+0] = HIBYTE(w);
102 res->data[res->size+1] = LOBYTE(w);
105 #ifndef WORDS_BIGENDIAN
109 res->data[res->size+1] = HIBYTE(w);
110 res->data[res->size+0] = LOBYTE(w);
113 res->size += sizeof(WORD);
116 void put_dword(res_t *res, unsigned d)
118 if(res->allocsize - res->size < sizeof(DWORD))
119 grow_res(res, RES_BLOCKSIZE);
122 #ifdef WORDS_BIGENDIAN
126 res->data[res->size+0] = HIBYTE(HIWORD(d));
127 res->data[res->size+1] = LOBYTE(HIWORD(d));
128 res->data[res->size+2] = HIBYTE(LOWORD(d));
129 res->data[res->size+3] = LOBYTE(LOWORD(d));
132 #ifndef WORDS_BIGENDIAN
136 res->data[res->size+3] = HIBYTE(HIWORD(d));
137 res->data[res->size+2] = LOBYTE(HIWORD(d));
138 res->data[res->size+1] = HIBYTE(LOWORD(d));
139 res->data[res->size+0] = LOBYTE(LOWORD(d));
142 res->size += sizeof(DWORD);
145 static void put_pad(res_t *res)
147 while(res->size & 0x3)
152 *****************************************************************************
153 * Function : set_word
155 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
156 * void set_dword(res_t *res, int ofs, unsigned d)
158 * res - Binary resource to put the data in
159 * ofs - Byte offset in data-array
162 * Description : Set the value of a binary resource data array to a
165 *****************************************************************************
167 static void set_word(res_t *res, int ofs, unsigned w)
171 #ifdef WORDS_BIGENDIAN
175 res->data[ofs+0] = HIBYTE(w);
176 res->data[ofs+1] = LOBYTE(w);
179 #ifndef WORDS_BIGENDIAN
183 res->data[ofs+1] = HIBYTE(w);
184 res->data[ofs+0] = LOBYTE(w);
189 static void set_dword(res_t *res, int ofs, unsigned d)
193 #ifdef WORDS_BIGENDIAN
197 res->data[ofs+0] = HIBYTE(HIWORD(d));
198 res->data[ofs+1] = LOBYTE(HIWORD(d));
199 res->data[ofs+2] = HIBYTE(LOWORD(d));
200 res->data[ofs+3] = LOBYTE(LOWORD(d));
203 #ifndef WORDS_BIGENDIAN
207 res->data[ofs+3] = HIBYTE(HIWORD(d));
208 res->data[ofs+2] = LOBYTE(HIWORD(d));
209 res->data[ofs+1] = HIBYTE(LOWORD(d));
210 res->data[ofs+0] = LOBYTE(LOWORD(d));
216 *****************************************************************************
217 * Function : get_dword
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 static DWORD get_dword(res_t *res, int ofs)
231 #ifdef WORDS_BIGENDIAN
235 return (res->data[ofs+0] << 24)
236 | (res->data[ofs+1] << 16)
237 | (res->data[ofs+2] << 8)
240 #ifndef WORDS_BIGENDIAN
244 return (res->data[ofs+3] << 24)
245 | (res->data[ofs+2] << 16)
246 | (res->data[ofs+1] << 8)
252 *****************************************************************************
253 * Function : string_to_upper
254 * Syntax : void string_to_upper(string_t *str)
258 * Remarks : FIXME: codepages...
259 *****************************************************************************
261 static void string_to_upper(string_t *str)
265 if(str->type == str_char)
267 for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
269 else if(str->type == str_unicode)
271 for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
275 internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
280 *****************************************************************************
281 * Function : put_string
282 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
283 * int isterm, const language_t *lang)
285 * res - Binary resource to put the data in
286 * str - String to put
287 * type - Data has to be written in either str_char or str_unicode
288 * isterm - The string is '\0' terminated (disregard the string's
293 *****************************************************************************
295 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
296 const language_t *lang)
304 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
305 else codepage = get_language_codepage( 0, 0 );
307 assert( codepage != -1 );
309 newstr = convert_string(str, type, codepage);
310 if (type == str_unicode)
312 if (str->type == str_char)
314 if (!check_unicode_conversion( str, newstr, codepage ))
315 error( "String %s does not convert identically to Unicode and back in codepage %d. "
316 "Try using a Unicode string instead\n", str->str.cstr, codepage );
318 if (!isterm) put_word(res, newstr->size);
319 for(cnt = 0; cnt < newstr->size; cnt++)
321 WCHAR c = newstr->str.wstr[cnt];
322 if (isterm && !c) break;
325 if (isterm) put_word(res, 0);
329 if (!isterm) put_byte(res, newstr->size);
330 for(cnt = 0; cnt < newstr->size; cnt++)
332 char c = newstr->str.cstr[cnt];
333 if (isterm && !c) break;
336 if (isterm) put_byte(res, 0);
342 *****************************************************************************
343 * Function : put_name_id
344 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
349 *****************************************************************************
351 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
353 if(nid->type == name_ord)
356 put_word(res, 0xffff);
359 put_word(res, (WORD)nid->name.i_name);
361 else if(nid->type == name_str)
364 string_to_upper(nid->name.s_name);
365 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
369 internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
374 *****************************************************************************
376 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
381 *****************************************************************************
383 static void put_lvc(res_t *res, lvc_t *lvc)
385 if(lvc && lvc->language)
386 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
388 put_word(res, 0); /* Neutral */
389 if(lvc && lvc->version)
390 put_dword(res, *(lvc->version));
393 if(lvc && lvc->characts)
394 put_dword(res, *(lvc->characts));
400 *****************************************************************************
401 * Function : put_raw_data
402 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
407 *****************************************************************************
409 static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
411 unsigned int wsize = raw->size - offset;
412 if(res->allocsize - res->size < wsize)
413 grow_res(res, wsize);
414 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
419 *****************************************************************************
420 * Function : put_res_header
421 * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
422 * name_id_t *name, DWORD memopt, lvc_t *lvc)
425 * res - Binary resource descriptor to write to
426 * type - Resource identifier (if ntype == NULL)
427 * ntype - Name id of type
428 * name - Resource's name
429 * memopt - Resource's memory options to write
430 * lvc - Language, version and characteristics (win32 only)
431 * Output : An index to the resource size field. The resource size field
432 * contains the header size upon exit.
435 *****************************************************************************
437 static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
438 DWORD memopt, lvc_t *lvc)
442 put_dword(res, 0); /* We will overwrite these later */
446 put_word(res, 0xffff); /* ResType */
450 put_name_id(res, ntype, TRUE, lvc->language);
451 put_name_id(res, name, TRUE, lvc->language); /* ResName */
453 put_dword(res, 0); /* DataVersion */
454 put_word(res, memopt); /* Memory options */
455 put_lvc(res, lvc); /* Language, version and characts */
456 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
457 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
458 res->dataidx = res->size;
466 put_byte(res, 0xff); /* ResType */
470 put_name_id(res, ntype, TRUE, NULL);
471 put_name_id(res, name, TRUE, NULL); /* ResName */
472 put_word(res, memopt); /* Memory options */
474 put_dword(res, 0); /* ResSize overwritten later*/
475 set_dword(res, tag, res->size);
476 res->dataidx = res->size;
482 *****************************************************************************
483 * Function : accelerator2res
484 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
486 * name - Name/ordinal of the resource
487 * acc - The accelerator descriptor
488 * Output : New .res format structure
491 *****************************************************************************
493 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
498 assert(name != NULL);
505 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
508 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
509 put_word(res, ev->key);
510 put_word(res, ev->id);
511 put_word(res, 0); /* Padding */
518 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
521 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
522 put_word(res, ev->key);
523 put_word(res, ev->id);
527 /* Set ResourceSize */
528 SetResSize(res, restag);
533 *****************************************************************************
534 * Function : dialog2res
535 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
537 * name - Name/ordinal of the resource
538 * dlg - The dialog descriptor
539 * Output : New .res format structure
542 *****************************************************************************
544 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
551 assert(name != NULL);
554 ctrl = dlg->controls;
558 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
560 put_dword(res, dlg->style->or_mask);
561 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
562 tag_nctrl = res->size;
563 put_word(res, 0); /* Number of controls */
564 put_word(res, dlg->x);
565 put_word(res, dlg->y);
566 put_word(res, dlg->width);
567 put_word(res, dlg->height);
569 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
573 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
577 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
582 put_word(res, dlg->font->size);
583 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
589 /* FIXME: what is default control style? */
590 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
591 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
592 put_word(res, ctrl->x);
593 put_word(res, ctrl->y);
594 put_word(res, ctrl->width);
595 put_word(res, ctrl->height);
596 put_word(res, ctrl->id);
598 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
600 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
602 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
607 put_word(res, ctrl->extra->size+2);
609 put_raw_data(res, ctrl->extra, 0);
619 /* Set number of controls */
620 set_word(res, tag_nctrl, (WORD)nctrl);
624 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
626 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
627 tag_nctrl = res->size;
628 put_byte(res, 0); /* Number of controls */
629 put_word(res, dlg->x);
630 put_word(res, dlg->y);
631 put_word(res, dlg->width);
632 put_word(res, dlg->height);
634 put_name_id(res, dlg->menu, TRUE, NULL);
638 put_name_id(res, dlg->dlgclass, TRUE, NULL);
642 put_string(res, dlg->title, str_char, TRUE, NULL);
647 put_word(res, dlg->font->size);
648 put_string(res, dlg->font->name, str_char, TRUE, NULL);
653 put_word(res, ctrl->x);
654 put_word(res, ctrl->y);
655 put_word(res, ctrl->width);
656 put_word(res, ctrl->height);
657 put_word(res, ctrl->id);
658 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
661 if(ctrl->ctlclass->type == name_ord
662 && ctrl->ctlclass->name.i_name >= 0x80
663 && ctrl->ctlclass->name.i_name <= 0x85)
664 put_byte(res, ctrl->ctlclass->name.i_name);
665 else if(ctrl->ctlclass->type == name_str)
666 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
668 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
671 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
673 put_name_id(res, ctrl->title, FALSE, NULL);
677 /* FIXME: What is this extra byte doing here? */
683 /* Set number of controls */
684 ((char *)res->data)[tag_nctrl] = (char)nctrl;
686 /* Set ResourceSize */
687 SetResSize(res, restag);
692 *****************************************************************************
693 * Function : dialogex2res
694 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
696 * name - Name/ordinal of the resource
697 * dlgex - The dialogex descriptor
698 * Output : New .res format structure
701 *****************************************************************************
703 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
710 assert(name != NULL);
711 assert(dlgex != NULL);
713 ctrl = dlgex->controls;
717 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
719 /* FIXME: MS doc says that the first word must contain 0xffff
720 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
721 * compiler reverses the two words.
722 * I don't know which one to choose, but I write it as Mr. B
725 put_word(res, 1); /* Signature */
726 put_word(res, 0xffff); /* DlgVer */
727 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
728 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
729 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
730 tag_nctrl = res->size;
731 put_word(res, 0); /* Number of controls */
732 put_word(res, dlgex->x);
733 put_word(res, dlgex->y);
734 put_word(res, dlgex->width);
735 put_word(res, dlgex->height);
737 put_name_id(res, dlgex->menu, TRUE, dlgex->lvc.language);
741 put_name_id(res, dlgex->dlgclass, TRUE, dlgex->lvc.language);
745 put_string(res, dlgex->title, str_unicode, TRUE, dlgex->lvc.language);
750 put_word(res, dlgex->font->size);
751 put_word(res, dlgex->font->weight);
752 /* FIXME: ? TRUE should be sufficient to say that it's
753 * italic, but Borland's compiler says it's 0x0101.
754 * I just copy it here, and hope for the best.
756 put_word(res, dlgex->font->italic ? 0x0101 : 0);
757 put_string(res, dlgex->font->name, str_unicode, TRUE, dlgex->lvc.language);
763 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
764 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
765 /* FIXME: what is default control style? */
766 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
767 put_word(res, ctrl->x);
768 put_word(res, ctrl->y);
769 put_word(res, ctrl->width);
770 put_word(res, ctrl->height);
771 put_dword(res, ctrl->id);
773 put_name_id(res, ctrl->ctlclass, TRUE, dlgex->lvc.language);
775 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
777 put_name_id(res, ctrl->title, FALSE, dlgex->lvc.language);
783 put_word(res, ctrl->extra->size);
784 put_raw_data(res, ctrl->extra, 0);
793 /* Set number of controls */
794 set_word(res, tag_nctrl, (WORD)nctrl);
795 /* Set ResourceSize */
796 SetResSize(res, restag);
801 /* Do not generate anything in 16-bit mode */
810 *****************************************************************************
811 * Function : menuitem2res
812 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
816 * Remarks : Self recursive
817 *****************************************************************************
819 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
821 menu_item_t *itm = menitem;
826 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
828 put_word(res, itm->id);
830 put_string(res, itm->name, str_unicode, TRUE, lang);
834 menuitem2res(res, itm->popup, lang);
842 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
844 put_word(res, itm->id);
846 put_string(res, itm->name, str_char, TRUE, lang);
850 menuitem2res(res, itm->popup, lang);
858 *****************************************************************************
859 * Function : menu2res
860 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
862 * name - Name/ordinal of the resource
863 * men - The menu descriptor
864 * Output : New .res format structure
867 *****************************************************************************
869 static res_t *menu2res(name_id_t *name, menu_t *men)
873 assert(name != NULL);
877 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
879 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
880 menuitem2res(res, men->items, win32 ? men->lvc.language : NULL);
881 /* Set ResourceSize */
882 SetResSize(res, restag);
889 *****************************************************************************
890 * Function : menuexitem2res
891 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
895 * Remarks : Self recursive
896 *****************************************************************************
898 static void menuexitem2res(res_t *res, menuex_item_t *menitem, const language_t *lang)
900 menuex_item_t *itm = menitem;
904 put_dword(res, itm->gottype ? itm->type : 0);
905 put_dword(res, itm->gotstate ? itm->state : 0);
906 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
907 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
909 put_string(res, itm->name, str_unicode, TRUE, lang);
915 put_dword(res, itm->gothelpid ? itm->helpid : 0);
916 menuexitem2res(res, itm->popup, lang);
924 *****************************************************************************
925 * Function : menuex2res
926 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
928 * name - Name/ordinal of the resource
929 * menex - The menuex descriptor
930 * Output : New .res format structure
933 *****************************************************************************
935 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
939 assert(name != NULL);
940 assert(menex != NULL);
945 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
947 put_word(res, 1); /* Menuheader: Version */
948 put_word(res, 4); /* Offset */
949 put_dword(res, 0); /* HelpId */
951 menuexitem2res(res, menex->items, menex->lvc.language);
952 /* Set ResourceSize */
953 SetResSize(res, restag);
958 /* Do not generate anything in 16-bit mode */
967 *****************************************************************************
968 * Function : cursorgroup2res
969 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
971 * name - Name/ordinal of the resource
972 * curg - The cursor descriptor
973 * Output : New .res format structure
976 *****************************************************************************
978 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
983 assert(name != NULL);
984 assert(curg != NULL);
987 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
990 put_word(res, 0); /* Reserved */
991 /* FIXME: The ResType in the NEWHEADER structure should
992 * contain 14 according to the MS win32 doc. This is
993 * not the case with the BRC compiler and I really doubt
994 * the latter. Putting one here is compliant to win16 spec,
995 * but who knows the true value?
997 put_word(res, 2); /* ResType */
998 put_word(res, curg->ncursor);
1000 for(cur = curg->cursorlist; cur; cur = cur->next)
1002 cur = curg->cursorlist;
1005 for(; cur; cur = cur->prev)
1008 put_word(res, cur->width);
1009 /* FIXME: The height of a cursor is half the size of
1010 * the bitmap's height. BRC puts the height from the
1011 * BITMAPINFOHEADER here instead of the cursorfile's
1012 * height. MS doesn't seem to care...
1014 put_word(res, cur->height);
1015 /* FIXME: The next two are reversed in BRC and I don't
1016 * know why. Probably a bug. But, we can safely ignore
1017 * it because win16 does not support color cursors.
1018 * A warning should have been generated by the parser.
1020 put_word(res, cur->planes);
1021 put_word(res, cur->bits);
1022 /* FIXME: The +4 is the hotspot in the cursor resource.
1023 * However, I could not find this in the documentation.
1024 * The hotspot bytes must either be included or MS
1027 put_dword(res, cur->data->size +4);
1028 put_word(res, cur->id);
1033 put_word(res, 0); /* Reserved */
1034 put_word(res, 2); /* ResType */
1035 put_word(res, curg->ncursor);
1037 for(cur = curg->cursorlist; cur; cur = cur->next)
1039 cur = curg->cursorlist;
1042 for(; cur; cur = cur->prev)
1045 put_word(res, cur->width);
1046 /* FIXME: The height of a cursor is half the size of
1047 * the bitmap's height. BRC puts the height from the
1048 * BITMAPINFOHEADER here instead of the cursorfile's
1049 * height. MS doesn't seem to care...
1051 put_word(res, cur->height);
1052 /* FIXME: The next two are reversed in BRC and I don't
1053 * know why. Probably a bug. But, we can safely ignore
1054 * it because win16 does not support color cursors.
1055 * A warning should have been generated by the parser.
1057 put_word(res, cur->planes);
1058 put_word(res, cur->bits);
1059 /* FIXME: The +4 is the hotspot in the cursor resource.
1060 * However, I could not find this in the documentation.
1061 * The hotspot bytes must either be included or MS
1064 put_dword(res, cur->data->size +4);
1065 put_word(res, cur->id);
1068 SetResSize(res, restag); /* Set ResourceSize */
1076 *****************************************************************************
1077 * Function : cursor2res
1078 * Syntax : res_t *cursor2res(cursor_t *cur)
1080 * cur - The cursor descriptor
1081 * Output : New .res format structure
1084 *****************************************************************************
1086 static res_t *cursor2res(cursor_t *cur)
1092 assert(cur != NULL);
1095 name.type = name_ord;
1096 name.name.i_name = cur->id;
1097 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1098 put_word(res, cur->xhot);
1099 put_word(res, cur->yhot);
1100 put_raw_data(res, cur->data, 0);
1102 SetResSize(res, restag); /* Set ResourceSize */
1110 *****************************************************************************
1111 * Function : icongroup2res
1112 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1114 * name - Name/ordinal of the resource
1115 * icog - The icon group descriptor
1116 * Output : New .res format structure
1119 *****************************************************************************
1121 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1126 assert(name != NULL);
1127 assert(icog != NULL);
1130 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1133 put_word(res, 0); /* Reserved */
1134 /* FIXME: The ResType in the NEWHEADER structure should
1135 * contain 14 according to the MS win32 doc. This is
1136 * not the case with the BRC compiler and I really doubt
1137 * the latter. Putting one here is compliant to win16 spec,
1138 * but who knows the true value?
1140 put_word(res, 1); /* ResType */
1141 put_word(res, icog->nicon);
1142 for(ico = icog->iconlist; ico; ico = ico->next)
1144 put_byte(res, ico->width);
1145 put_byte(res, ico->height);
1146 put_byte(res, ico->nclr);
1147 put_byte(res, 0); /* Reserved */
1148 put_word(res, ico->planes);
1149 put_word(res, ico->bits);
1150 put_dword(res, ico->data->size);
1151 put_word(res, ico->id);
1156 put_word(res, 0); /* Reserved */
1157 put_word(res, 1); /* ResType */
1158 put_word(res, icog->nicon);
1159 for(ico = icog->iconlist; ico; ico = ico->next)
1161 put_byte(res, ico->width);
1162 put_byte(res, ico->height);
1163 put_byte(res, ico->nclr);
1164 put_byte(res, 0); /* Reserved */
1165 put_word(res, ico->planes);
1166 put_word(res, ico->bits);
1167 put_dword(res, ico->data->size);
1168 put_word(res, ico->id);
1171 SetResSize(res, restag); /* Set ResourceSize */
1179 *****************************************************************************
1180 * Function : icon2res
1181 * Syntax : res_t *icon2res(icon_t *ico)
1183 * ico - The icon descriptor
1184 * Output : New .res format structure
1187 *****************************************************************************
1189 static res_t *icon2res(icon_t *ico)
1195 assert(ico != NULL);
1198 name.type = name_ord;
1199 name.name.i_name = ico->id;
1200 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1201 put_raw_data(res, ico->data, 0);
1203 SetResSize(res, restag); /* Set ResourceSize */
1211 *****************************************************************************
1212 * Function : anicurico2res
1213 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1215 * name - Name/ordinal of the resource
1216 * ani - The animated object descriptor
1217 * Output : New .res format structure
1219 * Remarks : The endian of the object's structures have been converted
1221 * There are rumors that win311 could handle animated stuff.
1222 * That is why they are generated for both win16 and win32
1224 *****************************************************************************
1226 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1230 assert(name != NULL);
1231 assert(ani != NULL);
1234 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1235 NULL, name, ani->memopt, NULL);
1236 put_raw_data(res, ani->data, 0);
1237 /* Set ResourceSize */
1238 SetResSize(res, restag);
1245 *****************************************************************************
1246 * Function : bitmap2res
1247 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1249 * name - Name/ordinal of the resource
1250 * bmp - The bitmap descriptor
1251 * Output : New .res format structure
1253 * Remarks : The endian of the bitmap structures have been converted
1255 *****************************************************************************
1257 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1261 assert(name != NULL);
1262 assert(bmp != NULL);
1265 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1266 if(bmp->data->data[0] == 'B'
1267 && bmp->data->data[1] == 'M'
1268 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1269 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1271 /* The File header is still attached, don't write it */
1272 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1276 put_raw_data(res, bmp->data, 0);
1278 /* Set ResourceSize */
1279 SetResSize(res, restag);
1286 *****************************************************************************
1287 * Function : font2res
1288 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1290 * name - Name/ordinal of the resource
1291 * fnt - The font descriptor
1292 * Output : New .res format structure
1294 * Remarks : The data has been prepared just after parsing.
1295 *****************************************************************************
1297 static res_t *font2res(name_id_t *name, font_t *fnt)
1301 assert(name != NULL);
1302 assert(fnt != NULL);
1305 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1306 put_raw_data(res, fnt->data, 0);
1307 /* Set ResourceSize */
1308 SetResSize(res, restag);
1315 *****************************************************************************
1316 * Function : fontdir2res
1317 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1319 * name - Name/ordinal of the resource
1320 * fntdir - The fontdir descriptor
1321 * Output : New .res format structure
1323 * Remarks : The data has been prepared just after parsing.
1324 *****************************************************************************
1326 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1330 assert(name != NULL);
1331 assert(fnd != NULL);
1334 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1335 put_raw_data(res, fnd->data, 0);
1336 /* Set ResourceSize */
1337 SetResSize(res, restag);
1344 *****************************************************************************
1345 * Function : html2res
1346 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1348 * name - Name/ordinal of the resource
1349 * rdt - The html descriptor
1350 * Output : New .res format structure
1353 *****************************************************************************
1355 static res_t *html2res(name_id_t *name, html_t *html)
1359 assert(name != NULL);
1360 assert(html != NULL);
1363 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1364 put_raw_data(res, html->data, 0);
1365 /* Set ResourceSize */
1366 SetResSize(res, restag);
1373 *****************************************************************************
1374 * Function : rcdata2res
1375 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1377 * name - Name/ordinal of the resource
1378 * rdt - The rcdata descriptor
1379 * Output : New .res format structure
1382 *****************************************************************************
1384 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1388 assert(name != NULL);
1389 assert(rdt != NULL);
1392 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1393 put_raw_data(res, rdt->data, 0);
1394 /* Set ResourceSize */
1395 SetResSize(res, restag);
1402 *****************************************************************************
1403 * Function : messagetable2res
1404 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1406 * name - Name/ordinal of the resource
1407 * msg - The messagetable descriptor
1408 * Output : New .res format structure
1410 * Remarks : The data has been converted to the appropriate endian
1411 * after it was parsed.
1412 *****************************************************************************
1414 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1418 assert(name != NULL);
1419 assert(msg != NULL);
1422 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1423 put_raw_data(res, msg->data, 0);
1424 /* Set ResourceSize */
1425 SetResSize(res, restag);
1432 *****************************************************************************
1433 * Function : stringtable2res
1434 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1436 * stt - The stringtable descriptor
1437 * Output : New .res format structure
1440 *****************************************************************************
1442 static res_t *stringtable2res(stringtable_t *stt)
1450 assert(stt != NULL);
1453 for(; stt; stt = stt->next)
1457 warning("Empty internal stringtable\n");
1460 name.type = name_ord;
1461 name.name.i_name = (stt->idbase >> 4) + 1;
1462 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1463 for(i = 0; i < stt->nentries; i++)
1465 if(stt->entries[i].str && stt->entries[i].str->size)
1467 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1468 FALSE, win32 ? stt->lvc.language : NULL);
1478 /* Set ResourceSize */
1479 SetResSize(res, restag - lastsize);
1482 lastsize = res->size;
1488 *****************************************************************************
1489 * Function : user2res
1490 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1492 * name - Name/ordinal of the resource
1493 * usr - The userresource descriptor
1494 * Output : New .res format structure
1497 *****************************************************************************
1499 static res_t *user2res(name_id_t *name, user_t *usr)
1503 assert(name != NULL);
1504 assert(usr != NULL);
1507 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1508 put_raw_data(res, usr->data, 0);
1509 /* Set ResourceSize */
1510 SetResSize(res, restag);
1517 *****************************************************************************
1518 * Function : versionblock2res
1519 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1521 * res - Binary resource to write to
1522 * blk - The version block to be written
1525 * Remarks : Self recursive
1526 *****************************************************************************
1528 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1537 blksizetag = res->size;
1538 put_word(res, 0); /* Will be overwritten later */
1541 put_word(res, 0); /* level ? */
1542 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1544 for(val = blk->values; val; val = val->next)
1546 if(val->type == val_str)
1548 valblksizetag = res->size;
1549 put_word(res, 0); /* Will be overwritten later */
1550 valvalsizetag = res->size;
1551 put_word(res, 0); /* Will be overwritten later */
1554 put_word(res, level);
1556 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1559 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1561 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1563 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1564 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1567 else if(val->type == val_words)
1569 valblksizetag = res->size;
1570 put_word(res, 0); /* Will be overwritten later */
1571 valvalsizetag = res->size;
1572 put_word(res, 0); /* Will be overwritten later */
1575 put_word(res, level);
1577 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1580 for(i = 0; i < val->value.words->nwords; i++)
1582 put_word(res, val->value.words->words[i]);
1584 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1585 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1588 else if(val->type == val_block)
1590 versionblock2res(res, val->value.block, level+1, lang);
1594 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1599 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1603 *****************************************************************************
1604 * Function : versioninfo2res
1605 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1607 * name - Name/ordinal of the resource
1608 * ver - The versioninfo descriptor
1609 * Output : New .res format structure
1612 *****************************************************************************
1614 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1617 int rootblocksizetag;
1624 assert(name != NULL);
1625 assert(ver != NULL);
1627 vsvi.type = str_char;
1628 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1629 vsvi.size = 15; /* Excl. termination */
1632 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1633 rootblocksizetag = res->size;
1634 put_word(res, 0); /* BlockSize filled in later */
1635 valsizetag = res->size;
1636 put_word(res, 0); /* ValueSize filled in later*/
1638 put_word(res, 0); /* Tree-level ? */
1639 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1640 TRUE, win32 ? ver->lvc.language : NULL);
1644 put_dword(res, VS_FFI_SIGNATURE);
1645 put_dword(res, VS_FFI_STRUCVERSION);
1646 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1647 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1648 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1649 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1650 put_dword(res, ver->fileflagsmask);
1651 put_dword(res, ver->fileflags);
1652 put_dword(res, ver->fileos);
1653 put_dword(res, ver->filetype);
1654 put_dword(res, ver->filesubtype);
1655 put_dword(res, 0); /* FileDateMS */
1656 put_dword(res, 0); /* FileDateLS */
1658 set_word(res, valsizetag, (WORD)(res->size - tag));
1659 /* Descend into the blocks */
1660 for(blk = ver->blocks; blk; blk = blk->next)
1661 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1662 /* Set root block's size */
1663 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1665 SetResSize(res, restag);
1669 free(vsvi.str.cstr);
1674 *****************************************************************************
1675 * Function : toolbaritem2res
1676 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1680 * Remarks : Self recursive
1681 *****************************************************************************
1683 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1685 toolbar_item_t *itm = tbitem;
1689 put_word(res, itm->id);
1696 *****************************************************************************
1697 * Function : toolbar2res
1698 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1700 * name - Name/ordinal of the resource
1701 * toolbar - The toolbar descriptor
1702 * Output : New .res format structure
1705 *****************************************************************************
1707 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1711 assert(name != NULL);
1712 assert(toolbar != NULL);
1717 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1719 put_word(res, 1); /* Menuheader: Version */
1720 put_word(res, toolbar->button_width); /* (in pixels?) */
1721 put_word(res, toolbar->button_height); /* (in pixels?) */
1722 put_word(res, toolbar->nitems);
1724 toolbaritem2res(res, toolbar->items);
1725 /* Set ResourceSize */
1726 SetResSize(res, restag);
1731 /* Do not generate anything in 16-bit mode */
1740 *****************************************************************************
1741 * Function : dlginit2res
1742 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1744 * name - Name/ordinal of the resource
1745 * rdt - The dlginit descriptor
1746 * Output : New .res format structure
1749 *****************************************************************************
1751 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1755 assert(name != NULL);
1756 assert(dit != NULL);
1759 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1760 put_raw_data(res, dit->data, 0);
1761 /* Set ResourceSize */
1762 SetResSize(res, restag);
1769 *****************************************************************************
1770 * Function : prep_nid_for_label
1771 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1774 * Description : Converts a resource name into the first 32 (or less)
1775 * characters of the name with conversions.
1777 *****************************************************************************
1779 #define MAXNAMELEN 32
1780 char *prep_nid_for_label(const name_id_t *nid)
1782 static char buf[MAXNAMELEN+1];
1784 assert(nid != NULL);
1786 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1790 sptr = nid->name.s_name->str.wstr;
1792 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1794 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1797 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1801 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1805 cptr = nid->name.s_name->str.cstr;
1807 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1809 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1812 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1816 else if(nid->type == name_ord)
1818 sprintf(buf, "%u", nid->name.i_name);
1822 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1829 *****************************************************************************
1830 * Function : make_c_name
1831 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1834 * Description : Converts a resource name into a valid c-identifier in the
1837 *****************************************************************************
1839 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1846 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1847 buf = prep_nid_for_label(nid);
1848 nlen = strlen(buf) + strlen(lanbuf);
1849 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1850 ret = xmalloc(nlen);
1856 strcat(ret, lanbuf);
1861 *****************************************************************************
1862 * Function : get_c_typename
1863 * Syntax : const char *get_c_typename(enum res_e type)
1866 * Description : Convert resource enum to char string to be used in c-name
1869 *****************************************************************************
1871 const char *get_c_typename(enum res_e type)
1875 case res_acc: return "Acc";
1876 case res_anicur:return "AniCur";
1877 case res_aniico:return "AniIco";
1878 case res_bmp: return "Bmp";
1879 case res_cur: return "Cur";
1880 case res_curg: return "CurGrp";
1882 case res_dlgex: return "Dlg";
1883 case res_fnt: return "Fnt";
1884 case res_fntdir:return "FntDir";
1885 case res_ico: return "Ico";
1886 case res_icog: return "IcoGrp";
1888 case res_menex: return "Men";
1889 case res_rdt: return "RCDat";
1890 case res_stt: return "StrTab";
1891 case res_usr: return "Usr";
1892 case res_msg: return "MsgTab";
1893 case res_ver: return "VerInf";
1894 case res_toolbar: return "TlBr";
1895 case res_dlginit: return "DlgInit";
1896 default: return "Oops";
1901 *****************************************************************************
1902 * Function : resources2res
1903 * Syntax : void resources2res(resource_t *top)
1905 * top - The resource-tree to convert
1907 * Description : Convert logical resource descriptors into binary data
1909 *****************************************************************************
1911 void resources2res(resource_t *top)
1919 top->binres = accelerator2res(top->name, top->res.acc);
1923 top->binres = bitmap2res(top->name, top->res.bmp);
1927 top->binres = cursor2res(top->res.cur);
1931 top->binres = cursorgroup2res(top->name, top->res.curg);
1935 top->binres = dialog2res(top->name, top->res.dlg);
1939 top->binres = dialogex2res(top->name, top->res.dlgex);
1943 top->binres = font2res(top->name, top->res.fnt);
1947 top->binres = fontdir2res(top->name, top->res.fnd);
1951 top->binres = icon2res(top->res.ico);
1955 top->binres = icongroup2res(top->name, top->res.icog);
1959 top->binres = menu2res(top->name, top->res.men);
1963 top->binres = menuex2res(top->name, top->res.menex);
1967 top->binres = html2res(top->name, top->res.html);
1971 top->binres = rcdata2res(top->name, top->res.rdt);
1975 top->binres = stringtable2res(top->res.stt);
1979 top->binres = user2res(top->name, top->res.usr);
1983 top->binres = messagetable2res(top->name, top->res.msg);
1987 top->binres = versioninfo2res(top->name, top->res.ver);
1991 top->binres = toolbar2res(top->name, top->res.tbt);
1995 top->binres = dlginit2res(top->name, top->res.dlgi);
2000 top->binres = anicurico2res(top->name, top->res.ani, top->type);
2003 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
2005 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);