2 * Generate .res format from a resource-tree
4 * Copyright 1998 Bertho A. Stultiens
6 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
7 * names in .s and .h files.
23 #define SetResSize(res, tag) *(DWORD *)&((res)->data[(tag)]) = \
24 (res)->size - *(DWORD *)&((res)->data[(tag)])
29 r = (res_t *)xmalloc(sizeof(res_t));
30 r->allocsize = RES_BLOCKSIZE;
32 r->data = (char *)xmalloc(RES_BLOCKSIZE);
36 res_t *grow_res(res_t *r, int add)
39 r->data = (char *)xrealloc(r->data, r->allocsize);
44 *****************************************************************************
48 * Syntax : void put_byte(res_t *res, unsigned c)
49 * void put_word(res_t *res, unsigned w)
50 * void put_dword(res_t *res, unsigned d)
52 * res - Binary resource to put the data in
53 * c, w, d - Data to put
55 * Description : Put primitives that put an item in the binary resource.
56 * The data array grows automatically.
58 *****************************************************************************
60 void put_byte(res_t *res, unsigned c)
62 if(res->allocsize - res->size < sizeof(char))
63 grow_res(res, RES_BLOCKSIZE);
64 *(char *)&(res->data[res->size]) = (char)c;
65 res->size += sizeof(char);
68 void put_word(res_t *res, unsigned w)
70 if(res->allocsize - res->size < sizeof(WORD))
71 grow_res(res, RES_BLOCKSIZE);
72 *(WORD *)&(res->data[res->size]) = (WORD)w;
73 res->size += sizeof(WORD);
76 void put_dword(res_t *res, unsigned d)
78 if(res->allocsize - res->size < sizeof(DWORD))
79 grow_res(res, RES_BLOCKSIZE);
80 *(DWORD *)&(res->data[res->size]) = (DWORD)d;
81 res->size += sizeof(DWORD);
84 void put_pad(res_t *res)
86 while(res->size & 0x3)
91 *****************************************************************************
92 * Function : string_to_upper
93 * Syntax : void string_to_upper(string_t *str)
97 * Remarks : FIXME: codepages...
98 *****************************************************************************
100 void string_to_upper(string_t *str)
102 if(str->type == str_char)
104 char *cptr = str->str.cstr;
106 *cptr = (char)toupper(*cptr);
108 else if(str->type == str_unicode)
110 short *sptr = str->str.wstr;
113 *sptr = (short)toupper(*sptr);
117 internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
122 *****************************************************************************
123 * Function : put_string
124 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
127 * res - Binary resource to put the data in
128 * str - String to put
129 * type - Data has to be written in either str_char or str_unicode
130 * isterm - The string is '\0' terminated (disregard the string's
135 *****************************************************************************
137 void put_string(res_t *res, string_t *str, enum str_e type, int isterm)
143 if(!isterm && str->size == 0)
145 warning("String length is zero, not written");
149 if(str->type == str_unicode && type == str_unicode)
151 for(cnt = 0; cnt < str->size; cnt++)
153 c = str->str.wstr[cnt];
158 if(isterm && (str->size == 0 || (cnt == str->size && c)))
161 else if(str->type == str_char && type == str_char)
163 for(cnt = 0; cnt < str->size; cnt++)
165 c = str->str.cstr[cnt];
170 if(isterm && (str->size == 0 || (cnt == str->size && c)))
173 else if(str->type == str_unicode && type == str_char)
175 for(cnt = 0; cnt < str->size; cnt++)
177 c = str->str.wstr[cnt];
182 if(isterm && (str->size == 0 || (cnt == str->size && c)))
185 else /* str->type == str_char && type == str_unicode */
187 for(cnt = 0; cnt < str->size; cnt++)
189 c = str->str.cstr[cnt];
190 put_word(res, c & 0xff);
194 if(isterm && (str->size == 0 || (cnt == str->size && c)))
200 *****************************************************************************
201 * Function : put_name_id
202 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase)
207 *****************************************************************************
209 void put_name_id(res_t *res, name_id_t *nid, int upcase)
211 if(nid->type == name_ord)
214 put_word(res, 0xffff);
217 put_word(res, (WORD)nid->name.i_name);
219 else if(nid->type == name_str)
222 string_to_upper(nid->name.s_name);
223 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE);
227 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
232 *****************************************************************************
234 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
239 *****************************************************************************
241 void put_lvc(res_t *res, lvc_t *lvc)
243 if(lvc && lvc->language)
244 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
246 put_word(res, 0); /* Neutral */
247 if(lvc && lvc->version)
248 put_dword(res, *(lvc->version));
251 if(lvc && lvc->characts)
252 put_dword(res, *(lvc->characts));
258 *****************************************************************************
259 * Function : put_raw_data
260 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
265 *****************************************************************************
267 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
269 int wsize = raw->size - offset;
270 if(res->allocsize - res->size < wsize)
271 grow_res(res, wsize);
272 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
277 *****************************************************************************
278 * Function : put_res_header
279 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
280 * name_id_t *name, DWORD memopt, lvc_t *lvc)
283 * res - Binary resource descriptor to write to
284 * type - Resource identifier (if ntype == NULL)
285 * ntype - Name id of type
286 * name - Resource's name
287 * memopt - Resource's memory options to write
288 * lvc - Language, version and characteristics (win32 only)
289 * Output : An index to the resource size field. The resource size field
290 * contains the header size upon exit.
293 *****************************************************************************
295 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
296 DWORD memopt, lvc_t *lvc)
300 put_dword(res, 0); /* We will overwrite these later */
304 put_word(res, 0xffff); /* ResType */
308 put_name_id(res, ntype, TRUE);
309 put_name_id(res, name, TRUE); /* ResName */
311 put_dword(res, 0); /* DataVersion */
312 put_word(res, memopt); /* Memory options */
313 put_lvc(res, lvc); /* Language, version and characts */
314 ((DWORD *)res->data)[0] = res->size; /* Set preliminary resource */
315 ((DWORD *)res->data)[1] = res->size; /* Set HeaderSize */
316 res->dataidx = res->size;
324 put_byte(res, 0xff); /* ResType */
328 put_name_id(res, ntype, TRUE);
329 put_name_id(res, name, TRUE); /* ResName */
330 put_word(res, memopt); /* Memory options */
332 put_dword(res, 0); /* ResSize overwritten later*/
333 *(DWORD *)&(res->data[tag]) = res->size;
334 res->dataidx = res->size;
340 *****************************************************************************
341 * Function : accelerator2res
342 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
344 * name - Name/ordinal of the resource
345 * acc - The accelerator descriptor
346 * Output : New .res format structure
349 *****************************************************************************
351 res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
356 assert(name != NULL);
363 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
366 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
367 put_word(res, ev->key);
368 put_word(res, ev->id);
369 put_word(res, 0); /* Padding */
376 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
379 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
380 put_word(res, ev->key);
381 put_word(res, ev->id);
385 /* Set ResourceSize */
386 SetResSize(res, restag);
391 *****************************************************************************
392 * Function : dialog2res
393 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
395 * name - Name/ordinal of the resource
396 * dlg - The dialog descriptor
397 * Output : New .res format structure
400 *****************************************************************************
402 res_t *dialog2res(name_id_t *name, dialog_t *dlg)
409 assert(name != NULL);
412 ctrl = dlg->controls;
416 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
418 put_dword(res, dlg->style);
419 put_dword(res, dlg->gotexstyle ? dlg->exstyle : 0);
420 tag_nctrl = res->size;
421 put_word(res, 0); /* Number of controls */
422 put_word(res, dlg->x);
423 put_word(res, dlg->y);
424 put_word(res, dlg->width);
425 put_word(res, dlg->height);
427 put_name_id(res, dlg->menu, TRUE);
431 put_name_id(res, dlg->dlgclass, TRUE);
435 put_string(res, dlg->title, str_unicode, TRUE);
440 put_word(res, dlg->font->size);
441 put_string(res, dlg->font->name, str_unicode, TRUE);
447 /* FIXME: what is default control style? */
448 put_dword(res, ctrl->gotstyle ? ctrl->style : WS_CHILD);
449 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle : 0);
450 put_word(res, ctrl->x);
451 put_word(res, ctrl->y);
452 put_word(res, ctrl->width);
453 put_word(res, ctrl->height);
454 put_word(res, ctrl->id);
456 put_name_id(res, ctrl->ctlclass, TRUE);
458 internal_error(__FILE__, __LINE__, "Control has no control-class");
460 put_name_id(res, ctrl->title, TRUE);
465 put_word(res, ctrl->extra->size+2);
467 put_raw_data(res, ctrl->extra, 0);
477 /* Set number of controls */
478 *(WORD *)&((char *)res->data)[tag_nctrl] = (WORD)nctrl;
482 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
484 put_dword(res, dlg->gotstyle ? dlg->style : WS_POPUPWINDOW);
485 tag_nctrl = res->size;
486 put_byte(res, 0); /* Number of controls */
487 put_word(res, dlg->x);
488 put_word(res, dlg->y);
489 put_word(res, dlg->width);
490 put_word(res, dlg->height);
492 put_name_id(res, dlg->menu, TRUE);
496 put_name_id(res, dlg->dlgclass, TRUE);
500 put_string(res, dlg->title, str_char, TRUE);
505 put_word(res, dlg->font->size);
506 put_string(res, dlg->font->name, str_char, TRUE);
511 put_word(res, ctrl->x);
512 put_word(res, ctrl->y);
513 put_word(res, ctrl->width);
514 put_word(res, ctrl->height);
515 put_word(res, ctrl->id);
516 put_dword(res, ctrl->gotstyle ? ctrl->style : WS_CHILD);
519 if(ctrl->ctlclass->type == name_ord
520 && ctrl->ctlclass->name.i_name >= 0x80
521 && ctrl->ctlclass->name.i_name <= 0x85)
522 put_byte(res, ctrl->ctlclass->name.i_name);
523 else if(ctrl->ctlclass->type == name_str)
524 put_name_id(res, ctrl->ctlclass, FALSE);
526 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
529 internal_error(__FILE__, __LINE__, "Control has no control-class");
531 put_name_id(res, ctrl->title, FALSE);
535 /* FIXME: What is this extra byte doing here? */
541 /* Set number of controls */
542 ((char *)res->data)[tag_nctrl] = (char)nctrl;
544 /* Set ResourceSize */
545 SetResSize(res, restag);
550 *****************************************************************************
551 * Function : dialogex2res
552 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
554 * name - Name/ordinal of the resource
555 * dlgex - The dialogex descriptor
556 * Output : New .res format structure
559 *****************************************************************************
561 res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
568 assert(name != NULL);
569 assert(dlgex != NULL);
571 ctrl = dlgex->controls;
575 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
577 /* FIXME: MS doc says thet the first word must contain 0xffff
578 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
579 * compiler reverses the two words.
580 * I don't know which one to choose, but I write it as Mr. B
583 put_word(res, 1); /* Signature */
584 put_word(res, 0xffff); /* DlgVer */
585 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
586 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle : 0);
587 put_dword(res, dlgex->gotstyle ? dlgex->style : WS_POPUPWINDOW);
588 tag_nctrl = res->size;
589 put_word(res, 0); /* Number of controls */
590 put_word(res, dlgex->x);
591 put_word(res, dlgex->y);
592 put_word(res, dlgex->width);
593 put_word(res, dlgex->height);
595 put_name_id(res, dlgex->menu, TRUE);
599 put_name_id(res, dlgex->dlgclass, TRUE);
603 put_string(res, dlgex->title, str_unicode, TRUE);
608 put_word(res, dlgex->font->size);
609 put_word(res, dlgex->font->weight);
610 /* FIXME: ? TRUE should be sufficient to say that its
611 * italic, but Borland's compiler says its 0x0101.
612 * I just copy it here, and hope for the best.
614 put_word(res, dlgex->font->italic ? 0x0101 : 0);
615 put_string(res, dlgex->font->name, str_unicode, TRUE);
621 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
622 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle : 0);
623 /* FIXME: what is default control style? */
624 put_dword(res, ctrl->gotstyle ? ctrl->style : WS_CHILD | WS_VISIBLE);
625 put_word(res, ctrl->x);
626 put_word(res, ctrl->y);
627 put_word(res, ctrl->width);
628 put_word(res, ctrl->height);
629 put_word(res, ctrl->id);
630 /* FIXME: Pad is _NOT_ documented!?! */
633 put_name_id(res, ctrl->ctlclass, TRUE);
635 internal_error(__FILE__, __LINE__, "Control has no control-class");
637 put_name_id(res, ctrl->title, TRUE);
643 put_word(res, ctrl->extra->size);
644 put_raw_data(res, ctrl->extra, 0);
653 /* Set number of controls */
654 *(WORD *)&((char *)res->data)[tag_nctrl] = (WORD)nctrl;
655 /* Set ResourceSize */
656 SetResSize(res, restag);
661 /* Do not generate anything in 16-bit mode */
670 *****************************************************************************
671 * Function : menuitem2res
672 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
676 * Remarks : Self recursive
677 *****************************************************************************
679 void menuitem2res(res_t *res, menu_item_t *menitem)
681 menu_item_t *itm = menitem;
686 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
688 put_word(res, itm->id);
690 put_string(res, itm->name, str_unicode, TRUE);
694 menuitem2res(res, itm->popup);
702 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
704 put_word(res, itm->id);
706 put_string(res, itm->name, str_char, TRUE);
710 menuitem2res(res, itm->popup);
718 *****************************************************************************
719 * Function : menu2res
720 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
722 * name - Name/ordinal of the resource
723 * men - The menu descriptor
724 * Output : New .res format structure
727 *****************************************************************************
729 res_t *menu2res(name_id_t *name, menu_t *men)
733 assert(name != NULL);
737 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
739 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
740 menuitem2res(res, men->items);
741 /* Set ResourceSize */
742 SetResSize(res, restag);
749 *****************************************************************************
750 * Function : menuexitem2res
751 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
755 * Remarks : Self recursive
756 *****************************************************************************
758 void menuexitem2res(res_t *res, menuex_item_t *menitem)
760 menuex_item_t *itm = menitem;
764 put_dword(res, itm->gottype ? itm->type : 0);
765 put_dword(res, itm->gotstate ? itm->state : 0);
766 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
767 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
769 put_string(res, itm->name, str_unicode, TRUE);
775 put_dword(res, itm->gothelpid ? itm->helpid : 0);
776 menuexitem2res(res, itm->popup);
784 *****************************************************************************
785 * Function : menuex2res
786 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
788 * name - Name/ordinal of the resource
789 * menex - The menuex descriptor
790 * Output : New .res format structure
793 *****************************************************************************
795 res_t *menuex2res(name_id_t *name, menuex_t *menex)
799 assert(name != NULL);
800 assert(menex != NULL);
805 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
807 put_word(res, 1); /* Menuheader: Version */
808 put_word(res, 4); /* Offset */
809 put_dword(res, 0); /* HelpId */
811 menuexitem2res(res, menex->items);
812 /* Set ResourceSize */
813 SetResSize(res, restag);
818 /* Do not generate anything in 16-bit mode */
827 *****************************************************************************
828 * Function : cursorgroup2res
829 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
831 * name - Name/ordinal of the resource
832 * curg - The cursor descriptor
833 * Output : New .res format structure
836 *****************************************************************************
838 res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
843 assert(name != NULL);
844 assert(curg != NULL);
847 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
850 put_word(res, 0); /* Reserved */
851 /* FIXME: The ResType in the NEWHEADER structure should
852 * contain 14 according to the MS win32 doc. This is
853 * not the case with the BRC compiler and I really doubt
854 * the latter. Putting one here is compliant to win16 spec,
855 * but who knows the true value?
857 put_word(res, 2); /* ResType */
858 put_word(res, curg->ncursor);
860 for(cur = curg->cursorlist; cur; cur = cur->next)
862 cur = curg->cursorlist;
865 for(; cur; cur = cur->prev)
868 put_word(res, cur->width);
869 /* FIXME: The height of a cursor is half the size of
870 * the bitmap's height. BRC puts the height from the
871 * BITMAPINFOHEADER here instead of the cursorfile's
872 * height. MS doesn't seem to care...
874 put_word(res, cur->height);
875 /* FIXME: The next two are reversed in BRC and I don't
876 * know why. Probably a bug. But, we can safely ignore
877 * it because win16 does not support color cursors.
878 * A warning should have been generated by the parser.
880 put_word(res, cur->planes);
881 put_word(res, cur->bits);
882 /* FIXME: The +4 is the hotspot in the cursor resource.
883 * However, I cound not find this in the documentation.
884 * The hotspot bytes must either be included or MS
887 put_dword(res, cur->data->size +4);
888 put_word(res, cur->id);
893 put_word(res, 0); /* Reserved */
894 put_word(res, 2); /* ResType */
895 put_word(res, curg->ncursor);
897 for(cur = curg->cursorlist; cur; cur = cur->next)
899 cur = curg->cursorlist;
902 for(; cur; cur = cur->prev)
905 put_word(res, cur->width);
906 /* FIXME: The height of a cursor is half the size of
907 * the bitmap's height. BRC puts the height from the
908 * BITMAPINFOHEADER here instead of the cursorfile's
909 * height. MS doesn't seem to care...
911 put_word(res, cur->height);
912 /* FIXME: The next two are reversed in BRC and I don't
913 * know why. Probably a bug. But, we can safely ignore
914 * it because win16 does not support color cursors.
915 * A warning should have been generated by the parser.
917 put_word(res, cur->planes);
918 put_word(res, cur->bits);
919 /* FIXME: The +4 is the hotspot in the cursor resource.
920 * However, I cound not find this in the documentation.
921 * The hotspot bytes must either be included or MS
924 put_dword(res, cur->data->size +4);
925 put_word(res, cur->id);
928 SetResSize(res, restag); /* Set ResourceSize */
936 *****************************************************************************
937 * Function : cursor2res
938 * Syntax : res_t *cursor2res(cursor_t *cur)
940 * cur - The cursor descriptor
941 * Output : New .res format structure
944 *****************************************************************************
946 res_t *cursor2res(cursor_t *cur)
955 name.type = name_ord;
956 name.name.i_name = cur->id;
957 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
958 put_word(res, cur->xhot);
959 put_word(res, cur->yhot);
960 put_raw_data(res, cur->data, 0);
962 SetResSize(res, restag); /* Set ResourceSize */
970 *****************************************************************************
971 * Function : icongroup2res
972 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
974 * name - Name/ordinal of the resource
975 * icog - The icon group descriptor
976 * Output : New .res format structure
979 *****************************************************************************
981 res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
986 assert(name != NULL);
987 assert(icog != NULL);
990 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
993 put_word(res, 0); /* Reserved */
994 /* FIXME: The ResType in the NEWHEADER structure should
995 * contain 14 according to the MS win32 doc. This is
996 * not the case with the BRC compiler and I really doubt
997 * the latter. Putting one here is compliant to win16 spec,
998 * but who knows the true value?
1000 put_word(res, 1); /* ResType */
1001 put_word(res, icog->nicon);
1002 for(ico = icog->iconlist; ico; ico = ico->next)
1004 put_byte(res, ico->width);
1005 put_byte(res, ico->height);
1006 put_byte(res, ico->nclr);
1007 put_byte(res, 0); /* Reserved */
1008 put_word(res, ico->planes);
1009 put_word(res, ico->bits);
1010 put_dword(res, ico->data->size);
1011 put_word(res, ico->id);
1016 put_word(res, 0); /* Reserved */
1017 put_word(res, 1); /* ResType */
1018 put_word(res, icog->nicon);
1019 for(ico = icog->iconlist; ico; ico = ico->next)
1021 put_byte(res, ico->width);
1022 put_byte(res, ico->height);
1023 put_byte(res, ico->nclr);
1024 put_byte(res, 0); /* Reserved */
1025 put_word(res, ico->planes);
1026 put_word(res, ico->bits);
1027 put_dword(res, ico->data->size);
1028 put_word(res, ico->id);
1031 SetResSize(res, restag); /* Set ResourceSize */
1039 *****************************************************************************
1040 * Function : icon2res
1041 * Syntax : res_t *icon2res(icon_t *ico)
1043 * ico - The icon descriptor
1044 * Output : New .res format structure
1047 *****************************************************************************
1049 res_t *icon2res(icon_t *ico)
1055 assert(ico != NULL);
1058 name.type = name_ord;
1059 name.name.i_name = ico->id;
1060 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1061 put_raw_data(res, ico->data, 0);
1063 SetResSize(res, restag); /* Set ResourceSize */
1071 *****************************************************************************
1072 * Function : bitmap2res
1073 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1075 * name - Name/ordinal of the resource
1076 * bmp - The bitmap descriptor
1077 * Output : New .res format structure
1080 *****************************************************************************
1082 res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1086 assert(name != NULL);
1087 assert(bmp != NULL);
1092 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, NULL);
1094 if(bmp->data->data[0] == 'B'
1095 && bmp->data->data[1] == 'M'
1096 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1097 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1099 /* The File header is still attached, don't write it */
1100 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1104 put_raw_data(res, bmp->data, 0);
1107 /* Set ResourceSize */
1108 SetResSize(res, restag);
1117 *****************************************************************************
1118 * Function : font2res
1119 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1121 * name - Name/ordinal of the resource
1122 * fnt - The font descriptor
1123 * Output : New .res format structure
1126 *****************************************************************************
1128 res_t *font2res(name_id_t *name, font_t *fnt)
1130 assert(name != NULL);
1131 assert(fnt != NULL);
1132 warning("Fonts not yet implemented");
1137 *****************************************************************************
1138 * Function : rcdata2res
1139 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1141 * name - Name/ordinal of the resource
1142 * rdt - The rcdata descriptor
1143 * Output : New .res format structure
1146 *****************************************************************************
1148 res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1152 assert(name != NULL);
1153 assert(rdt != NULL);
1156 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, NULL);
1157 put_raw_data(res, rdt->data, 0);
1158 /* Set ResourceSize */
1159 SetResSize(res, restag);
1166 *****************************************************************************
1167 * Function : messagetable2res
1168 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1170 * name - Name/ordinal of the resource
1171 * msg - The messagetable descriptor
1172 * Output : New .res format structure
1175 *****************************************************************************
1177 res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1179 assert(name != NULL);
1180 assert(msg != NULL);
1181 warning("Messagetable not yet implemented");
1186 *****************************************************************************
1187 * Function : stringtable2res
1188 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1190 * stt - The stringtable descriptor
1191 * Output : New .res format structure
1194 *****************************************************************************
1196 res_t *stringtable2res(stringtable_t *stt)
1204 assert(stt != NULL);
1207 for(; stt; stt = stt->next)
1211 warning("Empty internal stringtable");
1214 name.type = name_ord;
1215 name.name.i_name = (stt->idbase >> 4) + 1;
1216 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1217 for(i = 0; i < stt->nentries; i++)
1219 if(stt->entries[i].str)
1222 put_word(res, stt->entries[i].str->size);
1224 put_byte(res, stt->entries[i].str->size);
1225 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char, FALSE);
1235 /* Set ResourceSize */
1236 SetResSize(res, restag - lastsize);
1239 lastsize = res->size;
1245 *****************************************************************************
1246 * Function : user2res
1247 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1249 * name - Name/ordinal of the resource
1250 * usr - The userresource descriptor
1251 * Output : New .res format structure
1254 *****************************************************************************
1256 res_t *user2res(name_id_t *name, user_t *usr)
1260 assert(name != NULL);
1261 assert(usr != NULL);
1264 restag = put_res_header(res, 0, usr->type, name, usr->memopt, NULL);
1265 put_raw_data(res, usr->data, 0);
1266 /* Set ResourceSize */
1267 SetResSize(res, restag);
1274 *****************************************************************************
1275 * Function : versionblock2res
1276 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1278 * res - Binary resource to write to
1279 * blk - The version block to be written
1282 * Remarks : Self recursive
1283 *****************************************************************************
1285 void versionblock2res(res_t *res, ver_block_t *blk, int level)
1294 blksizetag = res->size;
1295 put_word(res, 0); /* Will be overwritten later */
1298 put_word(res, 0); /* level ? */
1299 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE);
1301 for(val = blk->values; val; val = val->next)
1303 if(val->type == val_str)
1305 valblksizetag = res->size;
1306 put_word(res, 0); /* Will be overwritten later */
1307 valvalsizetag = res->size;
1308 put_word(res, 0); /* Will be overwritten later */
1311 put_word(res, level);
1313 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1316 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE);
1318 *(WORD *)&(res->data[valvalsizetag]) = (WORD)((res->size - tag) >> 1);
1320 *(WORD *)&(res->data[valvalsizetag]) = (WORD)(res->size - tag);
1321 *(WORD *)&(res->data[valblksizetag]) = (WORD)(res->size - valblksizetag);
1324 else if(val->type == val_words)
1326 valblksizetag = res->size;
1327 put_word(res, 0); /* Will be overwritten later */
1328 valvalsizetag = res->size;
1329 put_word(res, 0); /* Will be overwritten later */
1332 put_word(res, level);
1334 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE);
1337 for(i = 0; i < val->value.words->nwords; i++)
1339 put_word(res, val->value.words->words[i]);
1341 *(WORD *)&(res->data[valvalsizetag]) = (WORD)(res->size - tag);
1342 *(WORD *)&(res->data[valblksizetag]) = (WORD)(res->size - valblksizetag);
1345 else if(val->type == val_block)
1347 versionblock2res(res, val->value.block, level+1);
1351 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1356 *(WORD *)&(res->data[blksizetag]) = (WORD)(res->size - blksizetag);
1360 *****************************************************************************
1361 * Function : versioninfo2res
1362 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1364 * name - Name/ordinal of the resource
1365 * ver - The versioninfo descriptor
1366 * Output : New .res format structure
1369 *****************************************************************************
1371 res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1374 int rootblocksizetag;
1381 assert(name != NULL);
1382 assert(ver != NULL);
1384 vsvi.type = str_char;
1385 vsvi.str.cstr = "VS_VERSION_INFO";
1386 vsvi.size = 15; /* Excl. termination */
1389 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, WRC_MO_MOVEABLE | WRC_MO_PURE, NULL);
1390 rootblocksizetag = res->size;
1391 put_word(res, 0); /* BlockSize filled in later */
1392 valsizetag = res->size;
1393 put_word(res, 0); /* ValueSize filled in later*/
1395 put_word(res, 0); /* Tree-level ? */
1396 put_string(res, &vsvi, win32 ? str_unicode : str_char, TRUE);
1400 put_dword(res, VS_FFI_SIGNATURE);
1401 put_dword(res, VS_FFI_STRUCVERSION);
1402 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1403 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1404 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1405 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1406 put_dword(res, ver->fileflagsmask);
1407 put_dword(res, ver->fileflags);
1408 put_dword(res, ver->fileos);
1409 put_dword(res, ver->filetype);
1410 put_dword(res, ver->filesubtype);
1411 put_dword(res, 0); /* FileDateMS */
1412 put_dword(res, 0); /* FileDateLS */
1414 *(WORD *)&(res->data[valsizetag]) = (WORD)(res->size - tag);
1415 /* Descend into the blocks */
1416 for(blk = ver->blocks; blk; blk = blk->next)
1417 versionblock2res(res, blk, 0);
1418 /* Set root block's size */
1419 *(WORD *)&(res->data[rootblocksizetag]) = (WORD)(res->size - rootblocksizetag);
1421 SetResSize(res, restag);
1429 *****************************************************************************
1430 * Function : toolbaritem2res
1431 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1435 * Remarks : Self recursive
1436 *****************************************************************************
1438 void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1440 toolbar_item_t *itm = tbitem;
1444 put_word(res, itm->id);
1451 *****************************************************************************
1452 * Function : toolbar2res
1453 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1455 * name - Name/ordinal of the resource
1456 * toolbar - The toolbar descriptor
1457 * Output : New .res format structure
1460 *****************************************************************************
1462 res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1466 assert(name != NULL);
1467 assert(toolbar != NULL);
1472 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1474 put_word(res, 1); /* Menuheader: Version */
1475 put_word(res, toolbar->button_width); /* (in pixels?) */
1476 put_word(res, toolbar->button_height); /* (in pixels?) */
1477 put_word(res, toolbar->nitems);
1479 toolbaritem2res(res, toolbar->items);
1480 /* Set ResourceSize */
1481 SetResSize(res, restag);
1486 /* Do not generate anything in 16-bit mode */
1495 *****************************************************************************
1496 * Function : dlginit2res
1497 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1499 * name - Name/ordinal of the resource
1500 * rdt - The dlginit descriptor
1501 * Output : New .res format structure
1504 *****************************************************************************
1506 res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1510 assert(name != NULL);
1511 assert(dit != NULL);
1514 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->lvc));
1515 put_raw_data(res, dit->data, 0);
1516 /* Set ResourceSize */
1517 SetResSize(res, restag);
1524 *****************************************************************************
1525 * Function : prep_nid_for_label
1526 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1529 * Description : Converts a resource name into the first 32 (or less)
1530 * characters of the name with conversions.
1532 *****************************************************************************
1534 #define MAXNAMELEN 32
1535 char *prep_nid_for_label(name_id_t *nid)
1537 static char buf[MAXNAMELEN+1];
1539 assert(nid != NULL);
1541 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1545 sptr = nid->name.s_name->str.wstr;
1547 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1549 if((unsigned)*sptr < 0x80 && isprint((char)*sptr))
1552 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1556 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1560 cptr = nid->name.s_name->str.cstr;
1562 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1564 if((unsigned)*cptr < 0x80 && isprint(*cptr))
1567 warning("Resourcename (str_char) contain unprintable characters, ignored");
1571 else if(nid->type == name_ord)
1573 sprintf(buf, "%u", nid->name.i_name);
1577 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1584 *****************************************************************************
1585 * Function : make_c_name
1586 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1589 * Description : Converts a resource name into a valid c-identifier in the
1592 *****************************************************************************
1594 char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1601 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1602 buf = prep_nid_for_label(nid);
1603 nlen = strlen(buf) + strlen(lanbuf);
1604 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1605 ret = (char *)xmalloc(nlen);
1611 strcat(ret, lanbuf);
1616 *****************************************************************************
1617 * Function : get_c_typename
1618 * Syntax : char *get_c_typename(enum res_e type)
1621 * Description : Convert resource enum to char string to be used in c-name
1624 *****************************************************************************
1626 char *get_c_typename(enum res_e type)
1630 case res_acc: return "Acc";
1631 case res_bmp: return "Bmp";
1632 case res_cur: return "Cur";
1633 case res_curg: return "CurGrp";
1635 case res_dlgex: return "Dlg";
1636 case res_fnt: return "Fnt";
1637 case res_ico: return "Ico";
1638 case res_icog: return "IcoGrp";
1640 case res_menex: return "Men";
1641 case res_rdt: return "RCDat";
1642 case res_stt: return "StrTab";
1643 case res_usr: return "Usr";
1644 case res_msg: return "MsgTab";
1645 case res_ver: return "VerInf";
1646 case res_toolbar: return "TlBr";
1647 case res_dlginit: return "DlgInit";
1648 default: return "Oops";
1653 *****************************************************************************
1654 * Function : resources2res
1655 * Syntax : void resources2res(resource_t *top)
1657 * top - The resource-tree to convert
1659 * Description : Convert logical resource descriptors into binary data
1661 *****************************************************************************
1663 void resources2res(resource_t *top)
1671 top->binres = accelerator2res(top->name, top->res.acc);
1675 top->binres = bitmap2res(top->name, top->res.bmp);
1679 top->binres = cursor2res(top->res.cur);
1683 top->binres = cursorgroup2res(top->name, top->res.curg);
1687 top->binres = dialog2res(top->name, top->res.dlg);
1691 top->binres = dialogex2res(top->name, top->res.dlgex);
1695 top->binres = font2res(top->name, top->res.fnt);
1699 top->binres = icon2res(top->res.ico);
1703 top->binres = icongroup2res(top->name, top->res.icog);
1707 top->binres = menu2res(top->name, top->res.men);
1711 top->binres = menuex2res(top->name, top->res.menex);
1715 top->binres = rcdata2res(top->name, top->res.rdt);
1719 top->binres = stringtable2res(top->res.stt);
1723 top->binres = user2res(top->name, top->res.usr);
1727 top->binres = messagetable2res(top->name, top->res.msg);
1731 top->binres = versioninfo2res(top->name, top->res.ver);
1735 top->binres = toolbar2res(top->name, top->res.tbt);
1739 top->binres = dlginit2res(top->name, top->res.dlgi);
1743 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
1745 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);