2 * Generate .res format from a resource-tree
4 * Copyright 1998 Bertho A. Stultiens
6 * 05-May-2000 BS - Added code to support endian conversions. The
7 * extra functions also aid unaligned access, but
8 * this is not yet implemented.
9 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
10 * names in .s and .h files.
29 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
34 r = (res_t *)xmalloc(sizeof(res_t));
35 r->allocsize = RES_BLOCKSIZE;
37 r->data = (char *)xmalloc(RES_BLOCKSIZE);
41 res_t *grow_res(res_t *r, int add)
44 r->data = (char *)xrealloc(r->data, r->allocsize);
49 *****************************************************************************
53 * Syntax : void put_byte(res_t *res, unsigned c)
54 * void put_word(res_t *res, unsigned w)
55 * void put_dword(res_t *res, unsigned d)
57 * res - Binary resource to put the data in
58 * c, w, d - Data to put
60 * Description : Put primitives that put an item in the binary resource.
61 * The data array grows automatically.
63 *****************************************************************************
65 void put_byte(res_t *res, unsigned c)
67 if(res->allocsize - res->size < sizeof(char))
68 grow_res(res, RES_BLOCKSIZE);
69 res->data[res->size] = (char)c;
70 res->size += sizeof(char);
73 void put_word(res_t *res, unsigned w)
75 if(res->allocsize - res->size < sizeof(WORD))
76 grow_res(res, RES_BLOCKSIZE);
79 #ifdef WORDS_BIGENDIAN
83 res->data[res->size+0] = HIBYTE(w);
84 res->data[res->size+1] = LOBYTE(w);
87 #ifndef WORDS_BIGENDIAN
91 res->data[res->size+1] = HIBYTE(w);
92 res->data[res->size+0] = LOBYTE(w);
95 res->size += sizeof(WORD);
98 void put_dword(res_t *res, unsigned d)
100 if(res->allocsize - res->size < sizeof(DWORD))
101 grow_res(res, RES_BLOCKSIZE);
104 #ifdef WORDS_BIGENDIAN
108 res->data[res->size+0] = HIBYTE(HIWORD(d));
109 res->data[res->size+1] = LOBYTE(HIWORD(d));
110 res->data[res->size+2] = HIBYTE(LOWORD(d));
111 res->data[res->size+3] = LOBYTE(LOWORD(d));
114 #ifndef WORDS_BIGENDIAN
118 res->data[res->size+3] = HIBYTE(HIWORD(d));
119 res->data[res->size+2] = LOBYTE(HIWORD(d));
120 res->data[res->size+1] = HIBYTE(LOWORD(d));
121 res->data[res->size+0] = LOBYTE(LOWORD(d));
124 res->size += sizeof(DWORD);
127 void put_pad(res_t *res)
129 while(res->size & 0x3)
134 *****************************************************************************
135 * Function : set_word
137 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
138 * void set_dword(res_t *res, int ofs, unsigned d)
140 * res - Binary resource to put the data in
141 * ofs - Byte offset in data-array
144 * Description : Set the value of a binary resource data array to a
147 *****************************************************************************
149 void set_word(res_t *res, int ofs, unsigned w)
153 #ifdef WORDS_BIGENDIAN
157 res->data[ofs+0] = HIBYTE(w);
158 res->data[ofs+1] = LOBYTE(w);
161 #ifndef WORDS_BIGENDIAN
165 res->data[ofs+1] = HIBYTE(w);
166 res->data[ofs+0] = LOBYTE(w);
171 void set_dword(res_t *res, int ofs, unsigned d)
175 #ifdef WORDS_BIGENDIAN
179 res->data[ofs+0] = HIBYTE(HIWORD(d));
180 res->data[ofs+1] = LOBYTE(HIWORD(d));
181 res->data[ofs+2] = HIBYTE(LOWORD(d));
182 res->data[ofs+3] = LOBYTE(LOWORD(d));
185 #ifndef WORDS_BIGENDIAN
189 res->data[ofs+3] = HIBYTE(HIWORD(d));
190 res->data[ofs+2] = LOBYTE(HIWORD(d));
191 res->data[ofs+1] = HIBYTE(LOWORD(d));
192 res->data[ofs+0] = LOBYTE(LOWORD(d));
198 *****************************************************************************
199 * Function : get_word
201 * Syntax : WORD get_word(res_t *res, int ofs)
202 * DWORD get_dword(res_t *res, int ofs)
204 * res - Binary resource to put the data in
205 * ofs - Byte offset in data-array
206 * Output : The data in native endian
207 * Description : Get the value of a binary resource data array in native
210 *****************************************************************************
212 WORD get_word(res_t *res, int ofs)
216 #ifdef WORDS_BIGENDIAN
220 return (res->data[ofs+0] << 8)
223 #ifndef WORDS_BIGENDIAN
227 return (res->data[ofs+1] << 8)
232 DWORD get_dword(res_t *res, int ofs)
236 #ifdef WORDS_BIGENDIAN
240 return (res->data[ofs+0] << 24)
241 | (res->data[ofs+1] << 16)
242 | (res->data[ofs+2] << 8)
245 #ifndef WORDS_BIGENDIAN
249 return (res->data[ofs+3] << 24)
250 | (res->data[ofs+2] << 16)
251 | (res->data[ofs+1] << 8)
257 *****************************************************************************
258 * Function : string_to_upper
259 * Syntax : void string_to_upper(string_t *str)
263 * Remarks : FIXME: codepages...
264 *****************************************************************************
266 void string_to_upper(string_t *str)
268 if(str->type == str_char)
270 char *cptr = str->str.cstr;
272 *cptr = (char)toupper(*cptr);
274 else if(str->type == str_unicode)
276 short *sptr = str->str.wstr;
279 *sptr = (short)toupper(*sptr);
283 internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
288 *****************************************************************************
289 * Function : put_string
290 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
293 * res - Binary resource to put the data in
294 * str - String to put
295 * type - Data has to be written in either str_char or str_unicode
296 * isterm - The string is '\0' terminated (disregard the string's
301 *****************************************************************************
303 static void put_string(res_t *res, string_t *str, enum str_e type, int isterm)
309 if(!isterm && str->size == 0)
311 warning("String length is zero, not written");
315 str = convert_string(str, type);
316 if(str->type == str_unicode && type == str_unicode)
318 for(cnt = 0; cnt < str->size; cnt++)
320 c = str->str.wstr[cnt];
325 if(isterm && (str->size == 0 || (cnt == str->size && c)))
328 else if(str->type == str_char && type == str_char)
330 for(cnt = 0; cnt < str->size; cnt++)
332 c = str->str.cstr[cnt];
337 if(isterm && (str->size == 0 || (cnt == str->size && c)))
340 else if(str->type == str_unicode && type == str_char)
342 for(cnt = 0; cnt < str->size; cnt++)
344 c = str->str.wstr[cnt];
349 if(isterm && (str->size == 0 || (cnt == str->size && c)))
352 else /* str->type == str_char && type == str_unicode */
354 for(cnt = 0; cnt < str->size; cnt++)
356 c = str->str.cstr[cnt];
357 put_word(res, c & 0xff);
361 if(isterm && (str->size == 0 || (cnt == str->size && c)))
368 *****************************************************************************
369 * Function : put_name_id
370 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase)
375 *****************************************************************************
377 void put_name_id(res_t *res, name_id_t *nid, int upcase)
379 if(nid->type == name_ord)
382 put_word(res, 0xffff);
385 put_word(res, (WORD)nid->name.i_name);
387 else if(nid->type == name_str)
390 string_to_upper(nid->name.s_name);
391 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE);
395 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
400 *****************************************************************************
402 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
407 *****************************************************************************
409 void put_lvc(res_t *res, lvc_t *lvc)
411 if(lvc && lvc->language)
412 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
414 put_word(res, 0); /* Neutral */
415 if(lvc && lvc->version)
416 put_dword(res, *(lvc->version));
419 if(lvc && lvc->characts)
420 put_dword(res, *(lvc->characts));
423 if(lvc && lvc->language)
424 set_language( lvc->language->id, lvc->language->sub );
425 else if (currentlanguage)
426 set_language( currentlanguage->id, currentlanguage->sub );
428 set_language( LANG_NEUTRAL, SUBLANG_NEUTRAL );
432 *****************************************************************************
433 * Function : put_raw_data
434 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
439 *****************************************************************************
441 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
443 int wsize = raw->size - offset;
444 if(res->allocsize - res->size < wsize)
445 grow_res(res, wsize);
446 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
451 *****************************************************************************
452 * Function : put_res_header
453 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
454 * name_id_t *name, DWORD memopt, lvc_t *lvc)
457 * res - Binary resource descriptor to write to
458 * type - Resource identifier (if ntype == NULL)
459 * ntype - Name id of type
460 * name - Resource's name
461 * memopt - Resource's memory options to write
462 * lvc - Language, version and characteristics (win32 only)
463 * Output : An index to the resource size field. The resource size field
464 * contains the header size upon exit.
467 *****************************************************************************
469 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
470 DWORD memopt, lvc_t *lvc)
474 put_dword(res, 0); /* We will overwrite these later */
478 put_word(res, 0xffff); /* ResType */
482 put_name_id(res, ntype, TRUE);
483 put_name_id(res, name, TRUE); /* ResName */
485 put_dword(res, 0); /* DataVersion */
486 put_word(res, memopt); /* Memory options */
487 put_lvc(res, lvc); /* Language, version and characts */
488 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
489 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
490 res->dataidx = res->size;
498 put_byte(res, 0xff); /* ResType */
502 put_name_id(res, ntype, TRUE);
503 put_name_id(res, name, TRUE); /* ResName */
504 put_word(res, memopt); /* Memory options */
506 put_dword(res, 0); /* ResSize overwritten later*/
507 set_dword(res, tag, res->size);
508 res->dataidx = res->size;
514 *****************************************************************************
515 * Function : accelerator2res
516 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
518 * name - Name/ordinal of the resource
519 * acc - The accelerator descriptor
520 * Output : New .res format structure
523 *****************************************************************************
525 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
530 assert(name != NULL);
537 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
540 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
541 put_word(res, ev->key);
542 put_word(res, ev->id);
543 put_word(res, 0); /* Padding */
550 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
553 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
554 put_word(res, ev->key);
555 put_word(res, ev->id);
559 /* Set ResourceSize */
560 SetResSize(res, restag);
565 *****************************************************************************
566 * Function : dialog2res
567 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
569 * name - Name/ordinal of the resource
570 * dlg - The dialog descriptor
571 * Output : New .res format structure
574 *****************************************************************************
576 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
583 assert(name != NULL);
586 ctrl = dlg->controls;
590 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
592 put_dword(res, dlg->style->or_mask);
593 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
594 tag_nctrl = res->size;
595 put_word(res, 0); /* Number of controls */
596 put_word(res, dlg->x);
597 put_word(res, dlg->y);
598 put_word(res, dlg->width);
599 put_word(res, dlg->height);
601 put_name_id(res, dlg->menu, TRUE);
605 put_name_id(res, dlg->dlgclass, TRUE);
609 put_string(res, dlg->title, str_unicode, TRUE);
614 put_word(res, dlg->font->size);
615 put_string(res, dlg->font->name, str_unicode, TRUE);
621 /* FIXME: what is default control style? */
622 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
623 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
624 put_word(res, ctrl->x);
625 put_word(res, ctrl->y);
626 put_word(res, ctrl->width);
627 put_word(res, ctrl->height);
628 put_word(res, ctrl->id);
630 put_name_id(res, ctrl->ctlclass, TRUE);
632 internal_error(__FILE__, __LINE__, "Control has no control-class");
634 put_name_id(res, ctrl->title, FALSE);
639 put_word(res, ctrl->extra->size+2);
641 put_raw_data(res, ctrl->extra, 0);
651 /* Set number of controls */
652 set_word(res, tag_nctrl, (WORD)nctrl);
656 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
658 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
659 tag_nctrl = res->size;
660 put_byte(res, 0); /* Number of controls */
661 put_word(res, dlg->x);
662 put_word(res, dlg->y);
663 put_word(res, dlg->width);
664 put_word(res, dlg->height);
666 put_name_id(res, dlg->menu, TRUE);
670 put_name_id(res, dlg->dlgclass, TRUE);
674 put_string(res, dlg->title, str_char, TRUE);
679 put_word(res, dlg->font->size);
680 put_string(res, dlg->font->name, str_char, TRUE);
685 put_word(res, ctrl->x);
686 put_word(res, ctrl->y);
687 put_word(res, ctrl->width);
688 put_word(res, ctrl->height);
689 put_word(res, ctrl->id);
690 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
693 if(ctrl->ctlclass->type == name_ord
694 && ctrl->ctlclass->name.i_name >= 0x80
695 && ctrl->ctlclass->name.i_name <= 0x85)
696 put_byte(res, ctrl->ctlclass->name.i_name);
697 else if(ctrl->ctlclass->type == name_str)
698 put_name_id(res, ctrl->ctlclass, FALSE);
700 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
703 internal_error(__FILE__, __LINE__, "Control has no control-class");
705 put_name_id(res, ctrl->title, FALSE);
709 /* FIXME: What is this extra byte doing here? */
715 /* Set number of controls */
716 ((char *)res->data)[tag_nctrl] = (char)nctrl;
718 /* Set ResourceSize */
719 SetResSize(res, restag);
724 *****************************************************************************
725 * Function : dialogex2res
726 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
728 * name - Name/ordinal of the resource
729 * dlgex - The dialogex descriptor
730 * Output : New .res format structure
733 *****************************************************************************
735 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
742 assert(name != NULL);
743 assert(dlgex != NULL);
745 ctrl = dlgex->controls;
749 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
751 /* FIXME: MS doc says thet the first word must contain 0xffff
752 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
753 * compiler reverses the two words.
754 * I don't know which one to choose, but I write it as Mr. B
757 put_word(res, 1); /* Signature */
758 put_word(res, 0xffff); /* DlgVer */
759 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
760 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
761 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
762 tag_nctrl = res->size;
763 put_word(res, 0); /* Number of controls */
764 put_word(res, dlgex->x);
765 put_word(res, dlgex->y);
766 put_word(res, dlgex->width);
767 put_word(res, dlgex->height);
769 put_name_id(res, dlgex->menu, TRUE);
773 put_name_id(res, dlgex->dlgclass, TRUE);
777 put_string(res, dlgex->title, str_unicode, TRUE);
782 put_word(res, dlgex->font->size);
783 put_word(res, dlgex->font->weight);
784 /* FIXME: ? TRUE should be sufficient to say that its
785 * italic, but Borland's compiler says its 0x0101.
786 * I just copy it here, and hope for the best.
788 put_word(res, dlgex->font->italic ? 0x0101 : 0);
789 put_string(res, dlgex->font->name, str_unicode, TRUE);
795 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
796 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
797 /* FIXME: what is default control style? */
798 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
799 put_word(res, ctrl->x);
800 put_word(res, ctrl->y);
801 put_word(res, ctrl->width);
802 put_word(res, ctrl->height);
803 put_word(res, ctrl->id);
804 /* FIXME: Pad is _NOT_ documented!?! */
807 put_name_id(res, ctrl->ctlclass, TRUE);
809 internal_error(__FILE__, __LINE__, "Control has no control-class");
811 put_name_id(res, ctrl->title, FALSE);
817 put_word(res, ctrl->extra->size);
818 put_raw_data(res, ctrl->extra, 0);
827 /* Set number of controls */
828 set_word(res, tag_nctrl, (WORD)nctrl);
829 /* Set ResourceSize */
830 SetResSize(res, restag);
835 /* Do not generate anything in 16-bit mode */
844 *****************************************************************************
845 * Function : menuitem2res
846 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
850 * Remarks : Self recursive
851 *****************************************************************************
853 static void menuitem2res(res_t *res, menu_item_t *menitem)
855 menu_item_t *itm = menitem;
860 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
862 put_word(res, itm->id);
864 put_string(res, itm->name, str_unicode, TRUE);
868 menuitem2res(res, itm->popup);
876 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
878 put_word(res, itm->id);
880 put_string(res, itm->name, str_char, TRUE);
884 menuitem2res(res, itm->popup);
892 *****************************************************************************
893 * Function : menu2res
894 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
896 * name - Name/ordinal of the resource
897 * men - The menu descriptor
898 * Output : New .res format structure
901 *****************************************************************************
903 static res_t *menu2res(name_id_t *name, menu_t *men)
907 assert(name != NULL);
911 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
913 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
914 menuitem2res(res, men->items);
915 /* Set ResourceSize */
916 SetResSize(res, restag);
923 *****************************************************************************
924 * Function : menuexitem2res
925 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
929 * Remarks : Self recursive
930 *****************************************************************************
932 static void menuexitem2res(res_t *res, menuex_item_t *menitem)
934 menuex_item_t *itm = menitem;
938 put_dword(res, itm->gottype ? itm->type : 0);
939 put_dword(res, itm->gotstate ? itm->state : 0);
940 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
941 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
943 put_string(res, itm->name, str_unicode, TRUE);
949 put_dword(res, itm->gothelpid ? itm->helpid : 0);
950 menuexitem2res(res, itm->popup);
958 *****************************************************************************
959 * Function : menuex2res
960 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
962 * name - Name/ordinal of the resource
963 * menex - The menuex descriptor
964 * Output : New .res format structure
967 *****************************************************************************
969 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
973 assert(name != NULL);
974 assert(menex != NULL);
979 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
981 put_word(res, 1); /* Menuheader: Version */
982 put_word(res, 4); /* Offset */
983 put_dword(res, 0); /* HelpId */
985 menuexitem2res(res, menex->items);
986 /* Set ResourceSize */
987 SetResSize(res, restag);
992 /* Do not generate anything in 16-bit mode */
1001 *****************************************************************************
1002 * Function : cursorgroup2res
1003 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
1005 * name - Name/ordinal of the resource
1006 * curg - The cursor descriptor
1007 * Output : New .res format structure
1010 *****************************************************************************
1012 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
1017 assert(name != NULL);
1018 assert(curg != NULL);
1021 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
1024 put_word(res, 0); /* Reserved */
1025 /* FIXME: The ResType in the NEWHEADER structure should
1026 * contain 14 according to the MS win32 doc. This is
1027 * not the case with the BRC compiler and I really doubt
1028 * the latter. Putting one here is compliant to win16 spec,
1029 * but who knows the true value?
1031 put_word(res, 2); /* ResType */
1032 put_word(res, curg->ncursor);
1034 for(cur = curg->cursorlist; cur; cur = cur->next)
1036 cur = curg->cursorlist;
1039 for(; cur; cur = cur->prev)
1042 put_word(res, cur->width);
1043 /* FIXME: The height of a cursor is half the size of
1044 * the bitmap's height. BRC puts the height from the
1045 * BITMAPINFOHEADER here instead of the cursorfile's
1046 * height. MS doesn't seem to care...
1048 put_word(res, cur->height);
1049 /* FIXME: The next two are reversed in BRC and I don't
1050 * know why. Probably a bug. But, we can safely ignore
1051 * it because win16 does not support color cursors.
1052 * A warning should have been generated by the parser.
1054 put_word(res, cur->planes);
1055 put_word(res, cur->bits);
1056 /* FIXME: The +4 is the hotspot in the cursor resource.
1057 * However, I cound not find this in the documentation.
1058 * The hotspot bytes must either be included or MS
1061 put_dword(res, cur->data->size +4);
1062 put_word(res, cur->id);
1067 put_word(res, 0); /* Reserved */
1068 put_word(res, 2); /* ResType */
1069 put_word(res, curg->ncursor);
1071 for(cur = curg->cursorlist; cur; cur = cur->next)
1073 cur = curg->cursorlist;
1076 for(; cur; cur = cur->prev)
1079 put_word(res, cur->width);
1080 /* FIXME: The height of a cursor is half the size of
1081 * the bitmap's height. BRC puts the height from the
1082 * BITMAPINFOHEADER here instead of the cursorfile's
1083 * height. MS doesn't seem to care...
1085 put_word(res, cur->height);
1086 /* FIXME: The next two are reversed in BRC and I don't
1087 * know why. Probably a bug. But, we can safely ignore
1088 * it because win16 does not support color cursors.
1089 * A warning should have been generated by the parser.
1091 put_word(res, cur->planes);
1092 put_word(res, cur->bits);
1093 /* FIXME: The +4 is the hotspot in the cursor resource.
1094 * However, I cound not find this in the documentation.
1095 * The hotspot bytes must either be included or MS
1098 put_dword(res, cur->data->size +4);
1099 put_word(res, cur->id);
1102 SetResSize(res, restag); /* Set ResourceSize */
1110 *****************************************************************************
1111 * Function : cursor2res
1112 * Syntax : res_t *cursor2res(cursor_t *cur)
1114 * cur - The cursor descriptor
1115 * Output : New .res format structure
1118 *****************************************************************************
1120 static res_t *cursor2res(cursor_t *cur)
1126 assert(cur != NULL);
1129 name.type = name_ord;
1130 name.name.i_name = cur->id;
1131 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1132 put_word(res, cur->xhot);
1133 put_word(res, cur->yhot);
1134 put_raw_data(res, cur->data, 0);
1136 SetResSize(res, restag); /* Set ResourceSize */
1144 *****************************************************************************
1145 * Function : icongroup2res
1146 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1148 * name - Name/ordinal of the resource
1149 * icog - The icon group descriptor
1150 * Output : New .res format structure
1153 *****************************************************************************
1155 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1160 assert(name != NULL);
1161 assert(icog != NULL);
1164 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1167 put_word(res, 0); /* Reserved */
1168 /* FIXME: The ResType in the NEWHEADER structure should
1169 * contain 14 according to the MS win32 doc. This is
1170 * not the case with the BRC compiler and I really doubt
1171 * the latter. Putting one here is compliant to win16 spec,
1172 * but who knows the true value?
1174 put_word(res, 1); /* ResType */
1175 put_word(res, icog->nicon);
1176 for(ico = icog->iconlist; ico; ico = ico->next)
1178 put_byte(res, ico->width);
1179 put_byte(res, ico->height);
1180 put_byte(res, ico->nclr);
1181 put_byte(res, 0); /* Reserved */
1182 put_word(res, ico->planes);
1183 put_word(res, ico->bits);
1184 put_dword(res, ico->data->size);
1185 put_word(res, ico->id);
1190 put_word(res, 0); /* Reserved */
1191 put_word(res, 1); /* ResType */
1192 put_word(res, icog->nicon);
1193 for(ico = icog->iconlist; ico; ico = ico->next)
1195 put_byte(res, ico->width);
1196 put_byte(res, ico->height);
1197 put_byte(res, ico->nclr);
1198 put_byte(res, 0); /* Reserved */
1199 put_word(res, ico->planes);
1200 put_word(res, ico->bits);
1201 put_dword(res, ico->data->size);
1202 put_word(res, ico->id);
1205 SetResSize(res, restag); /* Set ResourceSize */
1213 *****************************************************************************
1214 * Function : icon2res
1215 * Syntax : res_t *icon2res(icon_t *ico)
1217 * ico - The icon descriptor
1218 * Output : New .res format structure
1221 *****************************************************************************
1223 static res_t *icon2res(icon_t *ico)
1229 assert(ico != NULL);
1232 name.type = name_ord;
1233 name.name.i_name = ico->id;
1234 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1235 put_raw_data(res, ico->data, 0);
1237 SetResSize(res, restag); /* Set ResourceSize */
1245 *****************************************************************************
1246 * Function : anicurico2res
1247 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1249 * name - Name/ordinal of the resource
1250 * ani - The animated object descriptor
1251 * Output : New .res format structure
1253 * Remarks : The endian of the object's structures have been converted
1255 * There are rumors that win311 could handle animated stuff.
1256 * That is why they are generated for both win16 and win32
1258 *****************************************************************************
1260 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1264 assert(name != NULL);
1265 assert(ani != NULL);
1268 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1269 NULL, name, ani->memopt, NULL);
1270 put_raw_data(res, ani->data, 0);
1271 /* Set ResourceSize */
1272 SetResSize(res, restag);
1279 *****************************************************************************
1280 * Function : bitmap2res
1281 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1283 * name - Name/ordinal of the resource
1284 * bmp - The bitmap descriptor
1285 * Output : New .res format structure
1287 * Remarks : The endian of the bitmap structures have been converted
1289 *****************************************************************************
1291 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1295 assert(name != NULL);
1296 assert(bmp != NULL);
1299 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1300 if(bmp->data->data[0] == 'B'
1301 && bmp->data->data[1] == 'M'
1302 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1303 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1305 /* The File header is still attached, don't write it */
1306 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1310 put_raw_data(res, bmp->data, 0);
1312 /* Set ResourceSize */
1313 SetResSize(res, restag);
1320 *****************************************************************************
1321 * Function : font2res
1322 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1324 * name - Name/ordinal of the resource
1325 * fnt - The font descriptor
1326 * Output : New .res format structure
1328 * Remarks : The data has been prepared just after parsing.
1329 *****************************************************************************
1331 static res_t *font2res(name_id_t *name, font_t *fnt)
1335 assert(name != NULL);
1336 assert(fnt != NULL);
1339 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1340 put_raw_data(res, fnt->data, 0);
1341 /* Set ResourceSize */
1342 SetResSize(res, restag);
1349 *****************************************************************************
1350 * Function : fontdir2res
1351 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1353 * name - Name/ordinal of the resource
1354 * fntdir - The fontdir descriptor
1355 * Output : New .res format structure
1357 * Remarks : The data has been prepared just after parsing.
1358 *****************************************************************************
1360 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1364 assert(name != NULL);
1365 assert(fnd != NULL);
1368 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1369 put_raw_data(res, fnd->data, 0);
1370 /* Set ResourceSize */
1371 SetResSize(res, restag);
1378 *****************************************************************************
1379 * Function : rcdata2res
1380 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1382 * name - Name/ordinal of the resource
1383 * rdt - The rcdata descriptor
1384 * Output : New .res format structure
1387 *****************************************************************************
1389 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1393 assert(name != NULL);
1394 assert(rdt != NULL);
1397 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1398 put_raw_data(res, rdt->data, 0);
1399 /* Set ResourceSize */
1400 SetResSize(res, restag);
1407 *****************************************************************************
1408 * Function : messagetable2res
1409 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1411 * name - Name/ordinal of the resource
1412 * msg - The messagetable descriptor
1413 * Output : New .res format structure
1415 * Remarks : The data has been converted to the appropriate endian
1416 * after is was parsed.
1417 *****************************************************************************
1419 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1423 assert(name != NULL);
1424 assert(msg != NULL);
1427 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1428 put_raw_data(res, msg->data, 0);
1429 /* Set ResourceSize */
1430 SetResSize(res, restag);
1437 *****************************************************************************
1438 * Function : stringtable2res
1439 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1441 * stt - The stringtable descriptor
1442 * Output : New .res format structure
1445 *****************************************************************************
1447 static res_t *stringtable2res(stringtable_t *stt)
1455 assert(stt != NULL);
1458 for(; stt; stt = stt->next)
1462 warning("Empty internal stringtable");
1465 name.type = name_ord;
1466 name.name.i_name = (stt->idbase >> 4) + 1;
1467 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1468 for(i = 0; i < stt->nentries; i++)
1470 if(stt->entries[i].str && stt->entries[i].str->size)
1472 string_t *str = convert_string(stt->entries[i].str, win32 ? str_unicode : str_char);
1474 put_word(res, str->size);
1476 put_byte(res, str->size);
1477 put_string(res, str, win32 ? str_unicode : str_char, FALSE);
1488 /* Set ResourceSize */
1489 SetResSize(res, restag - lastsize);
1492 lastsize = res->size;
1498 *****************************************************************************
1499 * Function : user2res
1500 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1502 * name - Name/ordinal of the resource
1503 * usr - The userresource descriptor
1504 * Output : New .res format structure
1507 *****************************************************************************
1509 static res_t *user2res(name_id_t *name, user_t *usr)
1513 assert(name != NULL);
1514 assert(usr != NULL);
1517 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1518 put_raw_data(res, usr->data, 0);
1519 /* Set ResourceSize */
1520 SetResSize(res, restag);
1527 *****************************************************************************
1528 * Function : versionblock2res
1529 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1531 * res - Binary resource to write to
1532 * blk - The version block to be written
1535 * Remarks : Self recursive
1536 *****************************************************************************
1538 static void versionblock2res(res_t *res, ver_block_t *blk, int level)
1547 blksizetag = res->size;
1548 put_word(res, 0); /* Will be overwritten later */
1551 put_word(res, 0); /* level ? */
1552 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE);
1554 for(val = blk->values; val; val = val->next)
1556 if(val->type == val_str)
1558 valblksizetag = res->size;
1559 put_word(res, 0); /* Will be overwritten later */
1560 valvalsizetag = res->size;
1561 put_word(res, 0); /* Will be overwritten later */
1564 put_word(res, level);
1566 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1569 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE);
1571 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1573 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1574 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1577 else if(val->type == val_words)
1579 valblksizetag = res->size;
1580 put_word(res, 0); /* Will be overwritten later */
1581 valvalsizetag = res->size;
1582 put_word(res, 0); /* Will be overwritten later */
1585 put_word(res, level);
1587 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1590 for(i = 0; i < val->value.words->nwords; i++)
1592 put_word(res, val->value.words->words[i]);
1594 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1595 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1598 else if(val->type == val_block)
1600 versionblock2res(res, val->value.block, level+1);
1604 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1609 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1613 *****************************************************************************
1614 * Function : versioninfo2res
1615 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1617 * name - Name/ordinal of the resource
1618 * ver - The versioninfo descriptor
1619 * Output : New .res format structure
1622 *****************************************************************************
1624 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1627 int rootblocksizetag;
1634 assert(name != NULL);
1635 assert(ver != NULL);
1637 vsvi.type = str_char;
1638 vsvi.str.cstr = "VS_VERSION_INFO";
1639 vsvi.size = 15; /* Excl. termination */
1642 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1643 rootblocksizetag = res->size;
1644 put_word(res, 0); /* BlockSize filled in later */
1645 valsizetag = res->size;
1646 put_word(res, 0); /* ValueSize filled in later*/
1648 put_word(res, 0); /* Tree-level ? */
1649 put_string(res, &vsvi, win32 ? str_unicode : str_char, TRUE);
1653 put_dword(res, VS_FFI_SIGNATURE);
1654 put_dword(res, VS_FFI_STRUCVERSION);
1655 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1656 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1657 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1658 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1659 put_dword(res, ver->fileflagsmask);
1660 put_dword(res, ver->fileflags);
1661 put_dword(res, ver->fileos);
1662 put_dword(res, ver->filetype);
1663 put_dword(res, ver->filesubtype);
1664 put_dword(res, 0); /* FileDateMS */
1665 put_dword(res, 0); /* FileDateLS */
1667 set_word(res, valsizetag, (WORD)(res->size - tag));
1668 /* Descend into the blocks */
1669 for(blk = ver->blocks; blk; blk = blk->next)
1670 versionblock2res(res, blk, 0);
1671 /* Set root block's size */
1672 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1674 SetResSize(res, restag);
1682 *****************************************************************************
1683 * Function : toolbaritem2res
1684 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1688 * Remarks : Self recursive
1689 *****************************************************************************
1691 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1693 toolbar_item_t *itm = tbitem;
1697 put_word(res, itm->id);
1704 *****************************************************************************
1705 * Function : toolbar2res
1706 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1708 * name - Name/ordinal of the resource
1709 * toolbar - The toolbar descriptor
1710 * Output : New .res format structure
1713 *****************************************************************************
1715 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1719 assert(name != NULL);
1720 assert(toolbar != NULL);
1725 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1727 put_word(res, 1); /* Menuheader: Version */
1728 put_word(res, toolbar->button_width); /* (in pixels?) */
1729 put_word(res, toolbar->button_height); /* (in pixels?) */
1730 put_word(res, toolbar->nitems);
1732 toolbaritem2res(res, toolbar->items);
1733 /* Set ResourceSize */
1734 SetResSize(res, restag);
1739 /* Do not generate anything in 16-bit mode */
1748 *****************************************************************************
1749 * Function : dlginit2res
1750 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1752 * name - Name/ordinal of the resource
1753 * rdt - The dlginit descriptor
1754 * Output : New .res format structure
1757 *****************************************************************************
1759 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1763 assert(name != NULL);
1764 assert(dit != NULL);
1767 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1768 put_raw_data(res, dit->data, 0);
1769 /* Set ResourceSize */
1770 SetResSize(res, restag);
1777 *****************************************************************************
1778 * Function : prep_nid_for_label
1779 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1782 * Description : Converts a resource name into the first 32 (or less)
1783 * characters of the name with conversions.
1785 *****************************************************************************
1787 #define MAXNAMELEN 32
1788 char *prep_nid_for_label(name_id_t *nid)
1790 static char buf[MAXNAMELEN+1];
1792 assert(nid != NULL);
1794 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1798 sptr = nid->name.s_name->str.wstr;
1800 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1802 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1805 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1809 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1813 cptr = nid->name.s_name->str.cstr;
1815 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1817 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1820 warning("Resourcename (str_char) contain unprintable characters, ignored");
1824 else if(nid->type == name_ord)
1826 sprintf(buf, "%u", nid->name.i_name);
1830 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1837 *****************************************************************************
1838 * Function : make_c_name
1839 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1842 * Description : Converts a resource name into a valid c-identifier in the
1845 *****************************************************************************
1847 char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1854 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1855 buf = prep_nid_for_label(nid);
1856 nlen = strlen(buf) + strlen(lanbuf);
1857 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1858 ret = (char *)xmalloc(nlen);
1864 strcat(ret, lanbuf);
1869 *****************************************************************************
1870 * Function : get_c_typename
1871 * Syntax : char *get_c_typename(enum res_e type)
1874 * Description : Convert resource enum to char string to be used in c-name
1877 *****************************************************************************
1879 char *get_c_typename(enum res_e type)
1883 case res_acc: return "Acc";
1884 case res_anicur:return "AniCur";
1885 case res_aniico:return "AniIco";
1886 case res_bmp: return "Bmp";
1887 case res_cur: return "Cur";
1888 case res_curg: return "CurGrp";
1890 case res_dlgex: return "Dlg";
1891 case res_fnt: return "Fnt";
1892 case res_fntdir:return "FntDir";
1893 case res_ico: return "Ico";
1894 case res_icog: return "IcoGrp";
1896 case res_menex: return "Men";
1897 case res_rdt: return "RCDat";
1898 case res_stt: return "StrTab";
1899 case res_usr: return "Usr";
1900 case res_msg: return "MsgTab";
1901 case res_ver: return "VerInf";
1902 case res_toolbar: return "TlBr";
1903 case res_dlginit: return "DlgInit";
1904 default: return "Oops";
1909 *****************************************************************************
1910 * Function : resources2res
1911 * Syntax : void resources2res(resource_t *top)
1913 * top - The resource-tree to convert
1915 * Description : Convert logical resource descriptors into binary data
1917 *****************************************************************************
1919 void resources2res(resource_t *top)
1927 top->binres = accelerator2res(top->name, top->res.acc);
1931 top->binres = bitmap2res(top->name, top->res.bmp);
1935 top->binres = cursor2res(top->res.cur);
1939 top->binres = cursorgroup2res(top->name, top->res.curg);
1943 top->binres = dialog2res(top->name, top->res.dlg);
1947 top->binres = dialogex2res(top->name, top->res.dlgex);
1951 top->binres = font2res(top->name, top->res.fnt);
1955 top->binres = fontdir2res(top->name, top->res.fnd);
1959 top->binres = icon2res(top->res.ico);
1963 top->binres = icongroup2res(top->name, top->res.icog);
1967 top->binres = menu2res(top->name, top->res.men);
1971 top->binres = menuex2res(top->name, top->res.menex);
1975 top->binres = rcdata2res(top->name, top->res.rdt);
1979 top->binres = stringtable2res(top->res.stt);
1983 top->binres = user2res(top->name, top->res.usr);
1987 top->binres = messagetable2res(top->name, top->res.msg);
1991 top->binres = versioninfo2res(top->name, top->res.ver);
1995 top->binres = toolbar2res(top->name, top->res.tbt);
1999 top->binres = dlginit2res(top->name, top->res.dlgi);
2004 top->binres = anicurico2res(top->name, top->res.ani, top->type);
2007 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
2009 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);