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);
279 static int parse_accel_string( const string_t *key, int flags )
283 if(key->type == str_char)
285 if (key->str.cstr[0] == '#') return 0; /* ignore message contexts */
286 if((flags & WRC_AF_VIRTKEY) &&
287 !((key->str.cstr[0] >= 'A' && key->str.cstr[0] <= 'Z') ||
288 (key->str.cstr[0] >= '0' && key->str.cstr[0] <= '9')))
290 print_location( &key->loc );
291 error("VIRTKEY code is not equal to ascii value\n");
294 if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
296 print_location( &key->loc );
297 error("Cannot use both '^' and CONTROL modifier\n");
299 else if(key->str.cstr[0] == '^')
301 keycode = toupper((unsigned char)key->str.cstr[1]) - '@';
304 print_location( &key->loc );
305 error("Control-code out of range\n");
309 keycode = key->str.cstr[0];
313 if (key->str.wstr[0] == '#') return 0; /* ignore message contexts */
314 if((flags & WRC_AF_VIRTKEY) &&
315 !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
316 (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
318 print_location( &key->loc );
319 error("VIRTKEY code is not equal to ascii value\n");
321 if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
323 print_location( &key->loc );
324 error("Cannot use both '^' and CONTROL modifier\n");
326 else if(key->str.wstr[0] == '^')
328 keycode = toupperW(key->str.wstr[1]) - '@';
331 print_location( &key->loc );
332 error("Control-code out of range\n");
336 keycode = key->str.wstr[0];
342 *****************************************************************************
343 * Function : put_string
344 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
345 * int isterm, const language_t *lang)
347 * res - Binary resource to put the data in
348 * str - String to put
349 * type - Data has to be written in either str_char or str_unicode
350 * isterm - The string is '\0' terminated (disregard the string's
355 *****************************************************************************
357 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
358 const language_t *lang)
366 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
367 else codepage = get_language_codepage( 0, 0 );
369 assert( codepage != -1 );
371 newstr = convert_string(str, type, codepage);
372 if (type == str_unicode)
374 if (str->type == str_char)
376 if (!check_unicode_conversion( str, newstr, codepage ))
378 print_location( &str->loc );
379 error( "String %s does not convert identically to Unicode and back in codepage %d. "
380 "Try using a Unicode string instead\n", str->str.cstr, codepage );
382 if (check_valid_utf8( str, codepage ))
384 print_location( &str->loc );
385 warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
386 str->str.cstr, codepage );
389 if (!isterm) put_word(res, newstr->size);
390 for(cnt = 0; cnt < newstr->size; cnt++)
392 WCHAR c = newstr->str.wstr[cnt];
393 if (isterm && !c) break;
396 if (isterm) put_word(res, 0);
400 if (!isterm) put_byte(res, newstr->size);
401 for(cnt = 0; cnt < newstr->size; cnt++)
403 char c = newstr->str.cstr[cnt];
404 if (isterm && !c) break;
407 if (isterm) put_byte(res, 0);
413 *****************************************************************************
414 * Function : put_name_id
415 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
420 *****************************************************************************
422 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
424 if(nid->type == name_ord)
427 put_word(res, 0xffff);
430 put_word(res, (WORD)nid->name.i_name);
432 else if(nid->type == name_str)
435 string_to_upper(nid->name.s_name);
436 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
440 internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
445 *****************************************************************************
447 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
452 *****************************************************************************
454 static void put_lvc(res_t *res, lvc_t *lvc)
456 if(lvc && lvc->language)
457 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
459 put_word(res, 0); /* Neutral */
460 if(lvc && lvc->version)
461 put_dword(res, *(lvc->version));
464 if(lvc && lvc->characts)
465 put_dword(res, *(lvc->characts));
471 *****************************************************************************
472 * Function : put_raw_data
473 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
478 *****************************************************************************
480 static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
482 unsigned int wsize = raw->size - offset;
483 if(res->allocsize - res->size < wsize)
484 grow_res(res, wsize);
485 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
490 *****************************************************************************
491 * Function : put_res_header
492 * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
493 * name_id_t *name, DWORD memopt, lvc_t *lvc)
496 * res - Binary resource descriptor to write to
497 * type - Resource identifier (if ntype == NULL)
498 * ntype - Name id of type
499 * name - Resource's name
500 * memopt - Resource's memory options to write
501 * lvc - Language, version and characteristics (win32 only)
502 * Output : An index to the resource size field. The resource size field
503 * contains the header size upon exit.
506 *****************************************************************************
508 static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
509 DWORD memopt, lvc_t *lvc)
513 put_dword(res, 0); /* We will overwrite these later */
517 put_word(res, 0xffff); /* ResType */
521 put_name_id(res, ntype, TRUE, lvc->language);
522 put_name_id(res, name, TRUE, lvc->language); /* ResName */
524 put_dword(res, 0); /* DataVersion */
525 put_word(res, memopt); /* Memory options */
526 put_lvc(res, lvc); /* Language, version and characts */
527 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
528 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
529 res->dataidx = res->size;
537 put_byte(res, 0xff); /* ResType */
541 put_name_id(res, ntype, TRUE, NULL);
542 put_name_id(res, name, TRUE, NULL); /* ResName */
543 put_word(res, memopt); /* Memory options */
545 put_dword(res, 0); /* ResSize overwritten later*/
546 set_dword(res, tag, res->size);
547 res->dataidx = res->size;
553 *****************************************************************************
554 * Function : accelerator2res
555 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
557 * name - Name/ordinal of the resource
558 * acc - The accelerator descriptor
559 * Output : New .res format structure
562 *****************************************************************************
564 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
569 assert(name != NULL);
576 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
580 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
581 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
583 put_word(res, ev->id);
584 put_word(res, 0); /* Padding */
591 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
595 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
596 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
598 put_word(res, ev->id);
602 /* Set ResourceSize */
603 SetResSize(res, restag);
608 *****************************************************************************
609 * Function : dialog2res
610 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
612 * name - Name/ordinal of the resource
613 * dlg - The dialog descriptor
614 * Output : New .res format structure
617 *****************************************************************************
619 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
626 assert(name != NULL);
629 ctrl = dlg->controls;
633 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
637 /* FIXME: MS doc says that the first word must contain 0xffff
638 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
639 * compiler reverses the two words.
640 * I don't know which one to choose, but I write it as Mr. B
643 put_word(res, 1); /* Signature */
644 put_word(res, 0xffff); /* DlgVer */
645 put_dword(res, dlg->gothelpid ? dlg->helpid : 0);
646 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
647 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
651 put_dword(res, dlg->style->or_mask);
652 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
654 tag_nctrl = res->size;
655 put_word(res, 0); /* Number of controls */
656 put_word(res, dlg->x);
657 put_word(res, dlg->y);
658 put_word(res, dlg->width);
659 put_word(res, dlg->height);
661 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
665 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
669 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
674 put_word(res, dlg->font->size);
677 put_word(res, dlg->font->weight);
678 /* FIXME: ? TRUE should be sufficient to say that it's
679 * italic, but Borland's compiler says it's 0x0101.
680 * I just copy it here, and hope for the best.
682 put_word(res, dlg->font->italic ? 0x0101 : 0);
684 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
692 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
693 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
694 /* FIXME: what is default control style? */
695 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
699 /* FIXME: what is default control style? */
700 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
701 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
703 put_word(res, ctrl->x);
704 put_word(res, ctrl->y);
705 put_word(res, ctrl->width);
706 put_word(res, ctrl->height);
708 put_dword(res, ctrl->id);
710 put_word(res, ctrl->id);
712 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
714 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
716 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
721 put_word(res, ctrl->extra->size+2);
723 put_raw_data(res, ctrl->extra, 0);
733 /* Set number of controls */
734 set_word(res, tag_nctrl, (WORD)nctrl);
738 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
740 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
741 tag_nctrl = res->size;
742 put_byte(res, 0); /* Number of controls */
743 put_word(res, dlg->x);
744 put_word(res, dlg->y);
745 put_word(res, dlg->width);
746 put_word(res, dlg->height);
748 put_name_id(res, dlg->menu, TRUE, NULL);
752 put_name_id(res, dlg->dlgclass, TRUE, NULL);
756 put_string(res, dlg->title, str_char, TRUE, NULL);
761 put_word(res, dlg->font->size);
762 put_string(res, dlg->font->name, str_char, TRUE, NULL);
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_word(res, ctrl->id);
772 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
775 if(ctrl->ctlclass->type == name_ord
776 && ctrl->ctlclass->name.i_name >= 0x80
777 && ctrl->ctlclass->name.i_name <= 0x85)
778 put_byte(res, ctrl->ctlclass->name.i_name);
779 else if(ctrl->ctlclass->type == name_str)
780 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
782 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
785 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
787 put_name_id(res, ctrl->title, FALSE, NULL);
791 /* FIXME: What is this extra byte doing here? */
797 /* Set number of controls */
798 ((char *)res->data)[tag_nctrl] = (char)nctrl;
800 /* Set ResourceSize */
801 SetResSize(res, restag);
806 *****************************************************************************
807 * Function : menuitem2res
808 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
812 * Remarks : Self recursive
813 *****************************************************************************
815 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
817 menu_item_t *itm = menitem;
822 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
824 put_word(res, itm->id);
826 put_string(res, itm->name, str_unicode, TRUE, lang);
830 menuitem2res(res, itm->popup, lang);
838 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
840 put_word(res, itm->id);
842 put_string(res, itm->name, str_char, TRUE, lang);
846 menuitem2res(res, itm->popup, lang);
854 *****************************************************************************
855 * Function : menuexitem2res
856 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
860 * Remarks : Self recursive
861 *****************************************************************************
863 static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
865 menu_item_t *itm = menitem;
869 put_dword(res, itm->gottype ? itm->type : 0);
870 put_dword(res, itm->gotstate ? itm->state : 0);
871 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
872 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
874 put_string(res, itm->name, str_unicode, TRUE, lang);
880 put_dword(res, itm->gothelpid ? itm->helpid : 0);
881 menuexitem2res(res, itm->popup, lang);
889 *****************************************************************************
890 * Function : menu2res
891 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
893 * name - Name/ordinal of the resource
894 * menex - The menuex descriptor
895 * Output : New .res format structure
898 *****************************************************************************
900 static res_t *menu2res(name_id_t *name, menu_t *men)
904 assert(name != NULL);
910 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &(men->lvc));
914 put_word(res, 1); /* Menuheader: Version */
915 put_word(res, 4); /* Offset */
916 put_dword(res, 0); /* HelpId */
918 menuexitem2res(res, men->items, men->lvc.language);
922 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
923 menuitem2res(res, men->items, men->lvc.language);
925 /* Set ResourceSize */
926 SetResSize(res, restag);
931 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, NULL);
933 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
934 menuitem2res(res, men->items, NULL);
935 /* Set ResourceSize */
936 SetResSize(res, restag);
942 *****************************************************************************
943 * Function : cursorgroup2res
944 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
946 * name - Name/ordinal of the resource
947 * curg - The cursor descriptor
948 * Output : New .res format structure
951 *****************************************************************************
953 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
958 assert(name != NULL);
959 assert(curg != NULL);
962 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
965 put_word(res, 0); /* Reserved */
966 /* FIXME: The ResType in the NEWHEADER structure should
967 * contain 14 according to the MS win32 doc. This is
968 * not the case with the BRC compiler and I really doubt
969 * the latter. Putting one here is compliant to win16 spec,
970 * but who knows the true value?
972 put_word(res, 2); /* ResType */
973 put_word(res, curg->ncursor);
975 for(cur = curg->cursorlist; cur; cur = cur->next)
977 cur = curg->cursorlist;
980 for(; cur; cur = cur->prev)
983 put_word(res, cur->width);
984 /* FIXME: The height of a cursor is half the size of
985 * the bitmap's height. BRC puts the height from the
986 * BITMAPINFOHEADER here instead of the cursorfile's
987 * height. MS doesn't seem to care...
989 put_word(res, cur->height);
990 /* FIXME: The next two are reversed in BRC and I don't
991 * know why. Probably a bug. But, we can safely ignore
992 * it because win16 does not support color cursors.
993 * A warning should have been generated by the parser.
995 put_word(res, cur->planes);
996 put_word(res, cur->bits);
997 /* FIXME: The +4 is the hotspot in the cursor resource.
998 * However, I could not find this in the documentation.
999 * The hotspot bytes must either be included or MS
1002 put_dword(res, cur->data->size +4);
1003 put_word(res, cur->id);
1008 put_word(res, 0); /* Reserved */
1009 put_word(res, 2); /* ResType */
1010 put_word(res, curg->ncursor);
1012 for(cur = curg->cursorlist; cur; cur = cur->next)
1014 cur = curg->cursorlist;
1017 for(; cur; cur = cur->prev)
1020 put_word(res, cur->width);
1021 /* FIXME: The height of a cursor is half the size of
1022 * the bitmap's height. BRC puts the height from the
1023 * BITMAPINFOHEADER here instead of the cursorfile's
1024 * height. MS doesn't seem to care...
1026 put_word(res, cur->height);
1027 /* FIXME: The next two are reversed in BRC and I don't
1028 * know why. Probably a bug. But, we can safely ignore
1029 * it because win16 does not support color cursors.
1030 * A warning should have been generated by the parser.
1032 put_word(res, cur->planes);
1033 put_word(res, cur->bits);
1034 /* FIXME: The +4 is the hotspot in the cursor resource.
1035 * However, I could not find this in the documentation.
1036 * The hotspot bytes must either be included or MS
1039 put_dword(res, cur->data->size +4);
1040 put_word(res, cur->id);
1043 SetResSize(res, restag); /* Set ResourceSize */
1051 *****************************************************************************
1052 * Function : cursor2res
1053 * Syntax : res_t *cursor2res(cursor_t *cur)
1055 * cur - The cursor descriptor
1056 * Output : New .res format structure
1059 *****************************************************************************
1061 static res_t *cursor2res(cursor_t *cur)
1067 assert(cur != NULL);
1070 name.type = name_ord;
1071 name.name.i_name = cur->id;
1072 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1073 put_word(res, cur->xhot);
1074 put_word(res, cur->yhot);
1075 put_raw_data(res, cur->data, 0);
1077 SetResSize(res, restag); /* Set ResourceSize */
1085 *****************************************************************************
1086 * Function : icongroup2res
1087 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1089 * name - Name/ordinal of the resource
1090 * icog - The icon group descriptor
1091 * Output : New .res format structure
1094 *****************************************************************************
1096 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1101 assert(name != NULL);
1102 assert(icog != NULL);
1105 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1108 put_word(res, 0); /* Reserved */
1109 /* FIXME: The ResType in the NEWHEADER structure should
1110 * contain 14 according to the MS win32 doc. This is
1111 * not the case with the BRC compiler and I really doubt
1112 * the latter. Putting one here is compliant to win16 spec,
1113 * but who knows the true value?
1115 put_word(res, 1); /* ResType */
1116 put_word(res, icog->nicon);
1117 for(ico = icog->iconlist; ico; ico = ico->next)
1119 put_byte(res, ico->width);
1120 put_byte(res, ico->height);
1121 put_byte(res, ico->nclr);
1122 put_byte(res, 0); /* Reserved */
1123 put_word(res, ico->planes);
1124 put_word(res, ico->bits);
1125 put_dword(res, ico->data->size);
1126 put_word(res, ico->id);
1131 put_word(res, 0); /* Reserved */
1132 put_word(res, 1); /* ResType */
1133 put_word(res, icog->nicon);
1134 for(ico = icog->iconlist; ico; ico = ico->next)
1136 put_byte(res, ico->width);
1137 put_byte(res, ico->height);
1138 put_byte(res, ico->nclr);
1139 put_byte(res, 0); /* Reserved */
1140 put_word(res, ico->planes);
1141 put_word(res, ico->bits);
1142 put_dword(res, ico->data->size);
1143 put_word(res, ico->id);
1146 SetResSize(res, restag); /* Set ResourceSize */
1154 *****************************************************************************
1155 * Function : icon2res
1156 * Syntax : res_t *icon2res(icon_t *ico)
1158 * ico - The icon descriptor
1159 * Output : New .res format structure
1162 *****************************************************************************
1164 static res_t *icon2res(icon_t *ico)
1170 assert(ico != NULL);
1173 name.type = name_ord;
1174 name.name.i_name = ico->id;
1175 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1176 put_raw_data(res, ico->data, 0);
1178 SetResSize(res, restag); /* Set ResourceSize */
1186 *****************************************************************************
1187 * Function : anicurico2res
1188 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1190 * name - Name/ordinal of the resource
1191 * ani - The animated object descriptor
1192 * Output : New .res format structure
1194 * Remarks : The endian of the object's structures have been converted
1196 * There are rumors that win311 could handle animated stuff.
1197 * That is why they are generated for both win16 and win32
1199 *****************************************************************************
1201 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1205 assert(name != NULL);
1206 assert(ani != NULL);
1209 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1210 NULL, name, ani->memopt, NULL);
1211 put_raw_data(res, ani->data, 0);
1212 /* Set ResourceSize */
1213 SetResSize(res, restag);
1220 *****************************************************************************
1221 * Function : bitmap2res
1222 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1224 * name - Name/ordinal of the resource
1225 * bmp - The bitmap descriptor
1226 * Output : New .res format structure
1228 * Remarks : The endian of the bitmap structures have been converted
1230 *****************************************************************************
1232 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1236 assert(name != NULL);
1237 assert(bmp != NULL);
1240 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1241 if(bmp->data->data[0] == 'B'
1242 && bmp->data->data[1] == 'M'
1243 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1244 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1246 /* The File header is still attached, don't write it */
1247 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1251 put_raw_data(res, bmp->data, 0);
1253 /* Set ResourceSize */
1254 SetResSize(res, restag);
1261 *****************************************************************************
1262 * Function : font2res
1263 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1265 * name - Name/ordinal of the resource
1266 * fnt - The font descriptor
1267 * Output : New .res format structure
1269 * Remarks : The data has been prepared just after parsing.
1270 *****************************************************************************
1272 static res_t *font2res(name_id_t *name, font_t *fnt)
1276 assert(name != NULL);
1277 assert(fnt != NULL);
1280 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1281 put_raw_data(res, fnt->data, 0);
1282 /* Set ResourceSize */
1283 SetResSize(res, restag);
1290 *****************************************************************************
1291 * Function : fontdir2res
1292 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1294 * name - Name/ordinal of the resource
1295 * fntdir - The fontdir descriptor
1296 * Output : New .res format structure
1298 * Remarks : The data has been prepared just after parsing.
1299 *****************************************************************************
1301 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1305 assert(name != NULL);
1306 assert(fnd != NULL);
1309 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1310 put_raw_data(res, fnd->data, 0);
1311 /* Set ResourceSize */
1312 SetResSize(res, restag);
1319 *****************************************************************************
1320 * Function : html2res
1321 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1323 * name - Name/ordinal of the resource
1324 * rdt - The html descriptor
1325 * Output : New .res format structure
1328 *****************************************************************************
1330 static res_t *html2res(name_id_t *name, html_t *html)
1334 assert(name != NULL);
1335 assert(html != NULL);
1338 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1339 put_raw_data(res, html->data, 0);
1340 /* Set ResourceSize */
1341 SetResSize(res, restag);
1348 *****************************************************************************
1349 * Function : rcdata2res
1350 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1352 * name - Name/ordinal of the resource
1353 * rdt - The rcdata descriptor
1354 * Output : New .res format structure
1357 *****************************************************************************
1359 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1363 assert(name != NULL);
1364 assert(rdt != NULL);
1367 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1368 put_raw_data(res, rdt->data, 0);
1369 /* Set ResourceSize */
1370 SetResSize(res, restag);
1377 *****************************************************************************
1378 * Function : messagetable2res
1379 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1381 * name - Name/ordinal of the resource
1382 * msg - The messagetable descriptor
1383 * Output : New .res format structure
1385 * Remarks : The data has been converted to the appropriate endian
1386 * after it was parsed.
1387 *****************************************************************************
1389 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1393 assert(name != NULL);
1394 assert(msg != NULL);
1397 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1398 put_raw_data(res, msg->data, 0);
1399 /* Set ResourceSize */
1400 SetResSize(res, restag);
1407 *****************************************************************************
1408 * Function : stringtable2res
1409 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1411 * stt - The stringtable descriptor
1412 * Output : New .res format structure
1415 *****************************************************************************
1417 static res_t *stringtable2res(stringtable_t *stt)
1425 assert(stt != NULL);
1428 for(; stt; stt = stt->next)
1432 warning("Empty internal stringtable\n");
1435 name.type = name_ord;
1436 name.name.i_name = (stt->idbase >> 4) + 1;
1437 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1438 for(i = 0; i < stt->nentries; i++)
1440 if(stt->entries[i].str && stt->entries[i].str->size)
1442 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1443 FALSE, win32 ? stt->lvc.language : NULL);
1453 /* Set ResourceSize */
1454 SetResSize(res, restag - lastsize);
1457 lastsize = res->size;
1463 *****************************************************************************
1464 * Function : user2res
1465 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1467 * name - Name/ordinal of the resource
1468 * usr - The userresource descriptor
1469 * Output : New .res format structure
1472 *****************************************************************************
1474 static res_t *user2res(name_id_t *name, user_t *usr)
1478 assert(name != NULL);
1479 assert(usr != NULL);
1482 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1483 put_raw_data(res, usr->data, 0);
1484 /* Set ResourceSize */
1485 SetResSize(res, restag);
1492 *****************************************************************************
1493 * Function : versionblock2res
1494 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1496 * res - Binary resource to write to
1497 * blk - The version block to be written
1500 * Remarks : Self recursive
1501 *****************************************************************************
1503 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1512 blksizetag = res->size;
1513 put_word(res, 0); /* Will be overwritten later */
1516 put_word(res, 0); /* level ? */
1517 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1519 for(val = blk->values; val; val = val->next)
1521 if(val->type == val_str)
1523 valblksizetag = res->size;
1524 put_word(res, 0); /* Will be overwritten later */
1525 valvalsizetag = res->size;
1526 put_word(res, 0); /* Will be overwritten later */
1529 put_word(res, level);
1531 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1534 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1536 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1538 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1539 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1542 else if(val->type == val_words)
1544 valblksizetag = res->size;
1545 put_word(res, 0); /* Will be overwritten later */
1546 valvalsizetag = res->size;
1547 put_word(res, 0); /* Will be overwritten later */
1550 put_word(res, level);
1552 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1555 for(i = 0; i < val->value.words->nwords; i++)
1557 put_word(res, val->value.words->words[i]);
1559 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1560 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1563 else if(val->type == val_block)
1565 versionblock2res(res, val->value.block, level+1, lang);
1569 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1574 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1578 *****************************************************************************
1579 * Function : versioninfo2res
1580 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1582 * name - Name/ordinal of the resource
1583 * ver - The versioninfo descriptor
1584 * Output : New .res format structure
1587 *****************************************************************************
1589 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1592 int rootblocksizetag;
1599 assert(name != NULL);
1600 assert(ver != NULL);
1602 vsvi.type = str_char;
1603 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1604 vsvi.size = 15; /* Excl. termination */
1607 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1608 rootblocksizetag = res->size;
1609 put_word(res, 0); /* BlockSize filled in later */
1610 valsizetag = res->size;
1611 put_word(res, 0); /* ValueSize filled in later*/
1613 put_word(res, 0); /* Tree-level ? */
1614 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1615 TRUE, win32 ? ver->lvc.language : NULL);
1619 put_dword(res, VS_FFI_SIGNATURE);
1620 put_dword(res, VS_FFI_STRUCVERSION);
1621 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1622 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1623 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1624 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1625 put_dword(res, ver->fileflagsmask);
1626 put_dword(res, ver->fileflags);
1627 put_dword(res, ver->fileos);
1628 put_dword(res, ver->filetype);
1629 put_dword(res, ver->filesubtype);
1630 put_dword(res, 0); /* FileDateMS */
1631 put_dword(res, 0); /* FileDateLS */
1633 set_word(res, valsizetag, (WORD)(res->size - tag));
1634 /* Descend into the blocks */
1635 for(blk = ver->blocks; blk; blk = blk->next)
1636 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1637 /* Set root block's size */
1638 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1640 SetResSize(res, restag);
1644 free(vsvi.str.cstr);
1649 *****************************************************************************
1650 * Function : toolbaritem2res
1651 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1655 * Remarks : Self recursive
1656 *****************************************************************************
1658 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1660 toolbar_item_t *itm = tbitem;
1664 put_word(res, itm->id);
1671 *****************************************************************************
1672 * Function : toolbar2res
1673 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1675 * name - Name/ordinal of the resource
1676 * toolbar - The toolbar descriptor
1677 * Output : New .res format structure
1680 *****************************************************************************
1682 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1686 assert(name != NULL);
1687 assert(toolbar != NULL);
1692 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1694 put_word(res, 1); /* Menuheader: Version */
1695 put_word(res, toolbar->button_width); /* (in pixels?) */
1696 put_word(res, toolbar->button_height); /* (in pixels?) */
1697 put_word(res, toolbar->nitems);
1699 toolbaritem2res(res, toolbar->items);
1700 /* Set ResourceSize */
1701 SetResSize(res, restag);
1706 /* Do not generate anything in 16-bit mode */
1715 *****************************************************************************
1716 * Function : dlginit2res
1717 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1719 * name - Name/ordinal of the resource
1720 * rdt - The dlginit descriptor
1721 * Output : New .res format structure
1724 *****************************************************************************
1726 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1730 assert(name != NULL);
1731 assert(dit != NULL);
1734 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1735 put_raw_data(res, dit->data, 0);
1736 /* Set ResourceSize */
1737 SetResSize(res, restag);
1744 *****************************************************************************
1745 * Function : prep_nid_for_label
1746 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1749 * Description : Converts a resource name into the first 32 (or less)
1750 * characters of the name with conversions.
1752 *****************************************************************************
1754 #define MAXNAMELEN 32
1755 char *prep_nid_for_label(const name_id_t *nid)
1757 static char buf[MAXNAMELEN+1];
1759 assert(nid != NULL);
1761 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1765 sptr = nid->name.s_name->str.wstr;
1767 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1769 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1772 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1776 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1780 cptr = nid->name.s_name->str.cstr;
1782 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1784 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1787 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1791 else if(nid->type == name_ord)
1793 sprintf(buf, "%u", nid->name.i_name);
1797 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1804 *****************************************************************************
1805 * Function : make_c_name
1806 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1809 * Description : Converts a resource name into a valid c-identifier in the
1812 *****************************************************************************
1814 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1816 char *buf = prep_nid_for_label(nid);
1817 return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
1821 *****************************************************************************
1822 * Function : get_c_typename
1823 * Syntax : const char *get_c_typename(enum res_e type)
1826 * Description : Convert resource enum to char string to be used in c-name
1829 *****************************************************************************
1831 const char *get_c_typename(enum res_e type)
1835 case res_acc: return "Acc";
1836 case res_anicur:return "AniCur";
1837 case res_aniico:return "AniIco";
1838 case res_bmp: return "Bmp";
1839 case res_cur: return "Cur";
1840 case res_curg: return "CurGrp";
1841 case res_dlg: return "Dlg";
1842 case res_fnt: return "Fnt";
1843 case res_fntdir:return "FntDir";
1844 case res_ico: return "Ico";
1845 case res_icog: return "IcoGrp";
1846 case res_men: return "Men";
1847 case res_rdt: return "RCDat";
1848 case res_stt: return "StrTab";
1849 case res_usr: return "Usr";
1850 case res_msg: return "MsgTab";
1851 case res_ver: return "VerInf";
1852 case res_toolbar: return "TlBr";
1853 case res_dlginit: return "DlgInit";
1854 default: return "Oops";
1859 *****************************************************************************
1860 * Function : resources2res
1861 * Syntax : void resources2res(resource_t *top)
1863 * top - The resource-tree to convert
1865 * Description : Convert logical resource descriptors into binary data
1867 *****************************************************************************
1869 void resources2res(resource_t *top)
1877 top->binres = accelerator2res(top->name, top->res.acc);
1881 top->binres = bitmap2res(top->name, top->res.bmp);
1885 top->binres = cursor2res(top->res.cur);
1889 top->binres = cursorgroup2res(top->name, top->res.curg);
1893 top->binres = dialog2res(top->name, top->res.dlg);
1897 top->binres = font2res(top->name, top->res.fnt);
1901 top->binres = fontdir2res(top->name, top->res.fnd);
1905 top->binres = icon2res(top->res.ico);
1909 top->binres = icongroup2res(top->name, top->res.icog);
1913 top->binres = menu2res(top->name, top->res.men);
1917 top->binres = html2res(top->name, top->res.html);
1921 top->binres = rcdata2res(top->name, top->res.rdt);
1925 top->binres = stringtable2res(top->res.stt);
1929 top->binres = user2res(top->name, top->res.usr);
1933 top->binres = messagetable2res(top->name, top->res.msg);
1937 top->binres = versioninfo2res(top->name, top->res.ver);
1941 top->binres = toolbar2res(top->name, top->res.tbt);
1945 top->binres = dlginit2res(top->name, top->res.dlgi);
1950 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1953 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
1955 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);