wined3d: Handle NULL ptrs (Coverity).
[wine.git] / tools / wrc / genres.c
blob131f6f79e50b5be7758b8de1154ab2a24ca89df0
1 /*
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
20 * History:
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.
28 #include "config.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <assert.h>
35 #include <ctype.h>
37 #include "wrc.h"
38 #include "genres.h"
39 #include "utils.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "wine/unicode.h"
46 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
48 res_t *new_res(void)
50 res_t *r;
51 r = xmalloc(sizeof(res_t));
52 r->allocsize = RES_BLOCKSIZE;
53 r->size = 0;
54 r->dataidx = 0;
55 r->data = xmalloc(RES_BLOCKSIZE);
56 return r;
59 res_t *grow_res(res_t *r, unsigned int add)
61 r->allocsize += add;
62 r->data = xrealloc(r->data, r->allocsize);
63 return r;
67 *****************************************************************************
68 * Function : put_byte
69 * put_word
70 * put_dword
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)
74 * Input :
75 * res - Binary resource to put the data in
76 * c, w, d - Data to put
77 * Output : nop
78 * Description : Put primitives that put an item in the binary resource.
79 * The data array grows automatically.
80 * Remarks :
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);
95 switch(byteorder)
97 #ifdef WORDS_BIGENDIAN
98 default:
99 #endif
100 case WRC_BO_BIG:
101 res->data[res->size+0] = HIBYTE(w);
102 res->data[res->size+1] = LOBYTE(w);
103 break;
105 #ifndef WORDS_BIGENDIAN
106 default:
107 #endif
108 case WRC_BO_LITTLE:
109 res->data[res->size+1] = HIBYTE(w);
110 res->data[res->size+0] = LOBYTE(w);
111 break;
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);
120 switch(byteorder)
122 #ifdef WORDS_BIGENDIAN
123 default:
124 #endif
125 case WRC_BO_BIG:
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));
130 break;
132 #ifndef WORDS_BIGENDIAN
133 default:
134 #endif
135 case WRC_BO_LITTLE:
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));
140 break;
142 res->size += sizeof(DWORD);
145 static void put_pad(res_t *res)
147 while(res->size & 0x3)
148 put_byte(res, 0);
152 *****************************************************************************
153 * Function : set_word
154 * set_dword
155 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
156 * void set_dword(res_t *res, int ofs, unsigned d)
157 * Input :
158 * res - Binary resource to put the data in
159 * ofs - Byte offset in data-array
160 * w, d - Data to put
161 * Output : nop
162 * Description : Set the value of a binary resource data array to a
163 * specific value.
164 * Remarks :
165 *****************************************************************************
167 static void set_word(res_t *res, int ofs, unsigned w)
169 switch(byteorder)
171 #ifdef WORDS_BIGENDIAN
172 default:
173 #endif
174 case WRC_BO_BIG:
175 res->data[ofs+0] = HIBYTE(w);
176 res->data[ofs+1] = LOBYTE(w);
177 break;
179 #ifndef WORDS_BIGENDIAN
180 default:
181 #endif
182 case WRC_BO_LITTLE:
183 res->data[ofs+1] = HIBYTE(w);
184 res->data[ofs+0] = LOBYTE(w);
185 break;
189 static void set_dword(res_t *res, int ofs, unsigned d)
191 switch(byteorder)
193 #ifdef WORDS_BIGENDIAN
194 default:
195 #endif
196 case WRC_BO_BIG:
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));
201 break;
203 #ifndef WORDS_BIGENDIAN
204 default:
205 #endif
206 case WRC_BO_LITTLE:
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));
211 break;
216 *****************************************************************************
217 * Function : get_dword
218 * Input :
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
223 * endian.
224 * Remarks :
225 *****************************************************************************
227 static DWORD get_dword(res_t *res, int ofs)
229 switch(byteorder)
231 #ifdef WORDS_BIGENDIAN
232 default:
233 #endif
234 case WRC_BO_BIG:
235 return (res->data[ofs+0] << 24)
236 | (res->data[ofs+1] << 16)
237 | (res->data[ofs+2] << 8)
238 | res->data[ofs+3];
240 #ifndef WORDS_BIGENDIAN
241 default:
242 #endif
243 case WRC_BO_LITTLE:
244 return (res->data[ofs+3] << 24)
245 | (res->data[ofs+2] << 16)
246 | (res->data[ofs+1] << 8)
247 | res->data[ofs+0];
252 *****************************************************************************
253 * Function : string_to_upper
254 * Syntax : void string_to_upper(string_t *str)
255 * Input :
256 * Output :
257 * Description :
258 * Remarks : FIXME: codepages...
259 *****************************************************************************
261 static void string_to_upper(string_t *str)
263 int i;
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]);
273 else
275 internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
280 *****************************************************************************
281 * Function : put_string
282 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
283 * int isterm, const language_t *lang)
284 * Input :
285 * res - Binary resource to put the data in
286 * str - String to put
287 * type - Data has to be written in either str_char or str_unicode
288 * isterm - The string is '\0' terminated (disregard the string's
289 * size member)
290 * Output : nop
291 * Description :
292 * Remarks :
293 *****************************************************************************
295 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
296 const language_t *lang)
298 int cnt, codepage;
299 string_t *newstr;
301 assert(res != NULL);
302 assert(str != NULL);
304 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
305 else codepage = get_language_codepage( 0, 0 );
307 assert( codepage != -1 );
309 newstr = convert_string(str, type, codepage);
310 if (type == str_unicode)
312 if (str->type == str_char)
314 if (!check_unicode_conversion( str, newstr, codepage ))
315 error( "String %s does not convert identically to Unicode and back in codepage %d. "
316 "Try using a Unicode string instead\n", str->str.cstr, codepage );
317 if (check_valid_utf8( str, codepage ))
318 warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
319 str->str.cstr, codepage );
321 if (!isterm) put_word(res, newstr->size);
322 for(cnt = 0; cnt < newstr->size; cnt++)
324 WCHAR c = newstr->str.wstr[cnt];
325 if (isterm && !c) break;
326 put_word(res, c);
328 if (isterm) put_word(res, 0);
330 else /* str_char */
332 if (!isterm) put_byte(res, newstr->size);
333 for(cnt = 0; cnt < newstr->size; cnt++)
335 char c = newstr->str.cstr[cnt];
336 if (isterm && !c) break;
337 put_byte(res, c);
339 if (isterm) put_byte(res, 0);
341 free_string(newstr);
345 *****************************************************************************
346 * Function : put_name_id
347 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
348 * Input :
349 * Output :
350 * Description :
351 * Remarks :
352 *****************************************************************************
354 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
356 if(nid->type == name_ord)
358 if(win32)
359 put_word(res, 0xffff);
360 else
361 put_byte(res, 0xff);
362 put_word(res, (WORD)nid->name.i_name);
364 else if(nid->type == name_str)
366 if(upcase)
367 string_to_upper(nid->name.s_name);
368 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
370 else
372 internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
377 *****************************************************************************
378 * Function : put_lvc
379 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
380 * Input :
381 * Output :
382 * Description :
383 * Remarks :
384 *****************************************************************************
386 static void put_lvc(res_t *res, lvc_t *lvc)
388 if(lvc && lvc->language)
389 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
390 else
391 put_word(res, 0); /* Neutral */
392 if(lvc && lvc->version)
393 put_dword(res, *(lvc->version));
394 else
395 put_dword(res, 0);
396 if(lvc && lvc->characts)
397 put_dword(res, *(lvc->characts));
398 else
399 put_dword(res, 0);
403 *****************************************************************************
404 * Function : put_raw_data
405 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
406 * Input :
407 * Output :
408 * Description :
409 * Remarks :
410 *****************************************************************************
412 static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
414 unsigned int wsize = raw->size - offset;
415 if(res->allocsize - res->size < wsize)
416 grow_res(res, wsize);
417 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
418 res->size += wsize;
422 *****************************************************************************
423 * Function : put_res_header
424 * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
425 * name_id_t *name, DWORD memopt, lvc_t *lvc)
427 * Input :
428 * res - Binary resource descriptor to write to
429 * type - Resource identifier (if ntype == NULL)
430 * ntype - Name id of type
431 * name - Resource's name
432 * memopt - Resource's memory options to write
433 * lvc - Language, version and characteristics (win32 only)
434 * Output : An index to the resource size field. The resource size field
435 * contains the header size upon exit.
436 * Description :
437 * Remarks :
438 *****************************************************************************
440 static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
441 DWORD memopt, lvc_t *lvc)
443 if(win32)
445 put_dword(res, 0); /* We will overwrite these later */
446 put_dword(res, 0);
447 if(!ntype)
449 put_word(res, 0xffff); /* ResType */
450 put_word(res, type);
452 else
453 put_name_id(res, ntype, TRUE, lvc->language);
454 put_name_id(res, name, TRUE, lvc->language); /* ResName */
455 put_pad(res);
456 put_dword(res, 0); /* DataVersion */
457 put_word(res, memopt); /* Memory options */
458 put_lvc(res, lvc); /* Language, version and characts */
459 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
460 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
461 res->dataidx = res->size;
462 return 0;
464 else /* win16 */
466 int tag;
467 if(!ntype)
469 put_byte(res, 0xff); /* ResType */
470 put_word(res, type);
472 else
473 put_name_id(res, ntype, TRUE, NULL);
474 put_name_id(res, name, TRUE, NULL); /* ResName */
475 put_word(res, memopt); /* Memory options */
476 tag = res->size;
477 put_dword(res, 0); /* ResSize overwritten later*/
478 set_dword(res, tag, res->size);
479 res->dataidx = res->size;
480 return tag;
485 *****************************************************************************
486 * Function : accelerator2res
487 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
488 * Input :
489 * name - Name/ordinal of the resource
490 * acc - The accelerator descriptor
491 * Output : New .res format structure
492 * Description :
493 * Remarks :
494 *****************************************************************************
496 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
498 int restag;
499 res_t *res;
500 event_t *ev;
501 assert(name != NULL);
502 assert(acc != NULL);
504 ev = acc->events;
505 res = new_res();
506 if(win32)
508 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
509 while(ev)
511 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
512 put_word(res, ev->key);
513 put_word(res, ev->id);
514 put_word(res, 0); /* Padding */
515 ev = ev->next;
517 put_pad(res);
519 else /* win16 */
521 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
522 while(ev)
524 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
525 put_word(res, ev->key);
526 put_word(res, ev->id);
527 ev = ev->next;
530 /* Set ResourceSize */
531 SetResSize(res, restag);
532 return res;
536 *****************************************************************************
537 * Function : dialog2res
538 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
539 * Input :
540 * name - Name/ordinal of the resource
541 * dlg - The dialog descriptor
542 * Output : New .res format structure
543 * Description :
544 * Remarks :
545 *****************************************************************************
547 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
549 int restag;
550 res_t *res;
551 control_t *ctrl;
552 int tag_nctrl;
553 int nctrl = 0;
554 assert(name != NULL);
555 assert(dlg != NULL);
557 ctrl = dlg->controls;
558 res = new_res();
559 if(win32)
561 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
563 put_dword(res, dlg->style->or_mask);
564 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
565 tag_nctrl = res->size;
566 put_word(res, 0); /* Number of controls */
567 put_word(res, dlg->x);
568 put_word(res, dlg->y);
569 put_word(res, dlg->width);
570 put_word(res, dlg->height);
571 if(dlg->menu)
572 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
573 else
574 put_word(res, 0);
575 if(dlg->dlgclass)
576 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
577 else
578 put_word(res, 0);
579 if(dlg->title)
580 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
581 else
582 put_word(res, 0);
583 if(dlg->font)
585 put_word(res, dlg->font->size);
586 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
589 put_pad(res);
590 while(ctrl)
592 /* FIXME: what is default control style? */
593 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
594 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
595 put_word(res, ctrl->x);
596 put_word(res, ctrl->y);
597 put_word(res, ctrl->width);
598 put_word(res, ctrl->height);
599 put_word(res, ctrl->id);
600 if(ctrl->ctlclass)
601 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
602 else
603 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
604 if(ctrl->title)
605 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
606 else
607 put_word(res, 0);
608 if(ctrl->extra)
610 put_word(res, ctrl->extra->size+2);
611 put_pad(res);
612 put_raw_data(res, ctrl->extra, 0);
614 else
615 put_word(res, 0);
617 if(ctrl->next)
618 put_pad(res);
619 nctrl++;
620 ctrl = ctrl->next;
622 /* Set number of controls */
623 set_word(res, tag_nctrl, (WORD)nctrl);
625 else /* win16 */
627 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
629 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
630 tag_nctrl = res->size;
631 put_byte(res, 0); /* Number of controls */
632 put_word(res, dlg->x);
633 put_word(res, dlg->y);
634 put_word(res, dlg->width);
635 put_word(res, dlg->height);
636 if(dlg->menu)
637 put_name_id(res, dlg->menu, TRUE, NULL);
638 else
639 put_byte(res, 0);
640 if(dlg->dlgclass)
641 put_name_id(res, dlg->dlgclass, TRUE, NULL);
642 else
643 put_byte(res, 0);
644 if(dlg->title)
645 put_string(res, dlg->title, str_char, TRUE, NULL);
646 else
647 put_byte(res, 0);
648 if(dlg->font)
650 put_word(res, dlg->font->size);
651 put_string(res, dlg->font->name, str_char, TRUE, NULL);
654 while(ctrl)
656 put_word(res, ctrl->x);
657 put_word(res, ctrl->y);
658 put_word(res, ctrl->width);
659 put_word(res, ctrl->height);
660 put_word(res, ctrl->id);
661 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
662 if(ctrl->ctlclass)
664 if(ctrl->ctlclass->type == name_ord
665 && ctrl->ctlclass->name.i_name >= 0x80
666 && ctrl->ctlclass->name.i_name <= 0x85)
667 put_byte(res, ctrl->ctlclass->name.i_name);
668 else if(ctrl->ctlclass->type == name_str)
669 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
670 else
671 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
673 else
674 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
675 if(ctrl->title)
676 put_name_id(res, ctrl->title, FALSE, NULL);
677 else
678 put_byte(res, 0);
680 /* FIXME: What is this extra byte doing here? */
681 put_byte(res, 0);
683 nctrl++;
684 ctrl = ctrl->next;
686 /* Set number of controls */
687 ((char *)res->data)[tag_nctrl] = (char)nctrl;
689 /* Set ResourceSize */
690 SetResSize(res, restag);
691 return res;
695 *****************************************************************************
696 * Function : dialogex2res
697 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
698 * Input :
699 * name - Name/ordinal of the resource
700 * dlgex - The dialogex descriptor
701 * Output : New .res format structure
702 * Description :
703 * Remarks :
704 *****************************************************************************
706 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
708 int restag;
709 res_t *res;
710 control_t *ctrl;
711 int tag_nctrl;
712 int nctrl = 0;
713 assert(name != NULL);
714 assert(dlgex != NULL);
716 ctrl = dlgex->controls;
717 res = new_res();
718 if(win32)
720 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
722 /* FIXME: MS doc says that the first word must contain 0xffff
723 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
724 * compiler reverses the two words.
725 * I don't know which one to choose, but I write it as Mr. B
726 * writes it.
728 put_word(res, 1); /* Signature */
729 put_word(res, 0xffff); /* DlgVer */
730 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
731 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
732 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
733 tag_nctrl = res->size;
734 put_word(res, 0); /* Number of controls */
735 put_word(res, dlgex->x);
736 put_word(res, dlgex->y);
737 put_word(res, dlgex->width);
738 put_word(res, dlgex->height);
739 if(dlgex->menu)
740 put_name_id(res, dlgex->menu, TRUE, dlgex->lvc.language);
741 else
742 put_word(res, 0);
743 if(dlgex->dlgclass)
744 put_name_id(res, dlgex->dlgclass, TRUE, dlgex->lvc.language);
745 else
746 put_word(res, 0);
747 if(dlgex->title)
748 put_string(res, dlgex->title, str_unicode, TRUE, dlgex->lvc.language);
749 else
750 put_word(res, 0);
751 if(dlgex->font)
753 put_word(res, dlgex->font->size);
754 put_word(res, dlgex->font->weight);
755 /* FIXME: ? TRUE should be sufficient to say that it's
756 * italic, but Borland's compiler says it's 0x0101.
757 * I just copy it here, and hope for the best.
759 put_word(res, dlgex->font->italic ? 0x0101 : 0);
760 put_string(res, dlgex->font->name, str_unicode, TRUE, dlgex->lvc.language);
763 put_pad(res);
764 while(ctrl)
766 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
767 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
768 /* FIXME: what is default control style? */
769 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
770 put_word(res, ctrl->x);
771 put_word(res, ctrl->y);
772 put_word(res, ctrl->width);
773 put_word(res, ctrl->height);
774 put_dword(res, ctrl->id);
775 if(ctrl->ctlclass)
776 put_name_id(res, ctrl->ctlclass, TRUE, dlgex->lvc.language);
777 else
778 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
779 if(ctrl->title)
780 put_name_id(res, ctrl->title, FALSE, dlgex->lvc.language);
781 else
782 put_word(res, 0);
783 if(ctrl->extra)
785 put_pad(res);
786 put_word(res, ctrl->extra->size);
787 put_raw_data(res, ctrl->extra, 0);
789 else
790 put_word(res, 0);
792 put_pad(res);
793 nctrl++;
794 ctrl = ctrl->next;
796 /* Set number of controls */
797 set_word(res, tag_nctrl, (WORD)nctrl);
798 /* Set ResourceSize */
799 SetResSize(res, restag);
800 put_pad(res);
802 else /* win16 */
804 /* Do not generate anything in 16-bit mode */
805 free(res->data);
806 free(res);
807 return NULL;
809 return res;
813 *****************************************************************************
814 * Function : menuitem2res
815 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
816 * Input :
817 * Output :
818 * Description :
819 * Remarks : Self recursive
820 *****************************************************************************
822 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
824 menu_item_t *itm = menitem;
825 if(win32)
827 while(itm)
829 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
830 if(!itm->popup)
831 put_word(res, itm->id);
832 if(itm->name)
833 put_string(res, itm->name, str_unicode, TRUE, lang);
834 else
835 put_word(res, 0);
836 if(itm->popup)
837 menuitem2res(res, itm->popup, lang);
838 itm = itm->next;
841 else /* win16 */
843 while(itm)
845 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
846 if(!itm->popup)
847 put_word(res, itm->id);
848 if(itm->name)
849 put_string(res, itm->name, str_char, TRUE, lang);
850 else
851 put_byte(res, 0);
852 if(itm->popup)
853 menuitem2res(res, itm->popup, lang);
854 itm = itm->next;
861 *****************************************************************************
862 * Function : menu2res
863 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
864 * Input :
865 * name - Name/ordinal of the resource
866 * men - The menu descriptor
867 * Output : New .res format structure
868 * Description :
869 * Remarks :
870 *****************************************************************************
872 static res_t *menu2res(name_id_t *name, menu_t *men)
874 int restag;
875 res_t *res;
876 assert(name != NULL);
877 assert(men != NULL);
879 res = new_res();
880 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
882 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
883 menuitem2res(res, men->items, win32 ? men->lvc.language : NULL);
884 /* Set ResourceSize */
885 SetResSize(res, restag);
886 if(win32)
887 put_pad(res);
888 return res;
892 *****************************************************************************
893 * Function : menuexitem2res
894 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
895 * Input :
896 * Output : nop
897 * Description :
898 * Remarks : Self recursive
899 *****************************************************************************
901 static void menuexitem2res(res_t *res, menuex_item_t *menitem, const language_t *lang)
903 menuex_item_t *itm = menitem;
904 assert(win32 != 0);
905 while(itm)
907 put_dword(res, itm->gottype ? itm->type : 0);
908 put_dword(res, itm->gotstate ? itm->state : 0);
909 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
910 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
911 if(itm->name)
912 put_string(res, itm->name, str_unicode, TRUE, lang);
913 else
914 put_word(res, 0);
915 put_pad(res);
916 if(itm->popup)
918 put_dword(res, itm->gothelpid ? itm->helpid : 0);
919 menuexitem2res(res, itm->popup, lang);
921 itm = itm->next;
927 *****************************************************************************
928 * Function : menuex2res
929 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
930 * Input :
931 * name - Name/ordinal of the resource
932 * menex - The menuex descriptor
933 * Output : New .res format structure
934 * Description :
935 * Remarks :
936 *****************************************************************************
938 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
940 int restag;
941 res_t *res;
942 assert(name != NULL);
943 assert(menex != NULL);
945 res = new_res();
946 if(win32)
948 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
950 put_word(res, 1); /* Menuheader: Version */
951 put_word(res, 4); /* Offset */
952 put_dword(res, 0); /* HelpId */
953 put_pad(res);
954 menuexitem2res(res, menex->items, menex->lvc.language);
955 /* Set ResourceSize */
956 SetResSize(res, restag);
957 put_pad(res);
959 else /* win16 */
961 /* Do not generate anything in 16-bit mode */
962 free(res->data);
963 free(res);
964 return NULL;
966 return res;
970 *****************************************************************************
971 * Function : cursorgroup2res
972 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
973 * Input :
974 * name - Name/ordinal of the resource
975 * curg - The cursor descriptor
976 * Output : New .res format structure
977 * Description :
978 * Remarks :
979 *****************************************************************************
981 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
983 int restag;
984 res_t *res;
985 cursor_t *cur;
986 assert(name != NULL);
987 assert(curg != NULL);
989 res = new_res();
990 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
991 if(win32)
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, 2); /* ResType */
1001 put_word(res, curg->ncursor);
1002 #if 0
1003 for(cur = curg->cursorlist; cur; cur = cur->next)
1004 #else
1005 cur = curg->cursorlist;
1006 while(cur->next)
1007 cur = cur->next;
1008 for(; cur; cur = cur->prev)
1009 #endif
1011 put_word(res, cur->width);
1012 /* FIXME: The height of a cursor is half the size of
1013 * the bitmap's height. BRC puts the height from the
1014 * BITMAPINFOHEADER here instead of the cursorfile's
1015 * height. MS doesn't seem to care...
1017 put_word(res, cur->height);
1018 /* FIXME: The next two are reversed in BRC and I don't
1019 * know why. Probably a bug. But, we can safely ignore
1020 * it because win16 does not support color cursors.
1021 * A warning should have been generated by the parser.
1023 put_word(res, cur->planes);
1024 put_word(res, cur->bits);
1025 /* FIXME: The +4 is the hotspot in the cursor resource.
1026 * However, I could not find this in the documentation.
1027 * The hotspot bytes must either be included or MS
1028 * doesn't care.
1030 put_dword(res, cur->data->size +4);
1031 put_word(res, cur->id);
1034 else /* win16 */
1036 put_word(res, 0); /* Reserved */
1037 put_word(res, 2); /* ResType */
1038 put_word(res, curg->ncursor);
1039 #if 0
1040 for(cur = curg->cursorlist; cur; cur = cur->next)
1041 #else
1042 cur = curg->cursorlist;
1043 while(cur->next)
1044 cur = cur->next;
1045 for(; cur; cur = cur->prev)
1046 #endif
1048 put_word(res, cur->width);
1049 /* FIXME: The height of a cursor is half the size of
1050 * the bitmap's height. BRC puts the height from the
1051 * BITMAPINFOHEADER here instead of the cursorfile's
1052 * height. MS doesn't seem to care...
1054 put_word(res, cur->height);
1055 /* FIXME: The next two are reversed in BRC and I don't
1056 * know why. Probably a bug. But, we can safely ignore
1057 * it because win16 does not support color cursors.
1058 * A warning should have been generated by the parser.
1060 put_word(res, cur->planes);
1061 put_word(res, cur->bits);
1062 /* FIXME: The +4 is the hotspot in the cursor resource.
1063 * However, I could not find this in the documentation.
1064 * The hotspot bytes must either be included or MS
1065 * doesn't care.
1067 put_dword(res, cur->data->size +4);
1068 put_word(res, cur->id);
1071 SetResSize(res, restag); /* Set ResourceSize */
1072 if(win32)
1073 put_pad(res);
1075 return res;
1079 *****************************************************************************
1080 * Function : cursor2res
1081 * Syntax : res_t *cursor2res(cursor_t *cur)
1082 * Input :
1083 * cur - The cursor descriptor
1084 * Output : New .res format structure
1085 * Description :
1086 * Remarks :
1087 *****************************************************************************
1089 static res_t *cursor2res(cursor_t *cur)
1091 int restag;
1092 res_t *res;
1093 name_id_t name;
1095 assert(cur != NULL);
1097 res = new_res();
1098 name.type = name_ord;
1099 name.name.i_name = cur->id;
1100 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1101 put_word(res, cur->xhot);
1102 put_word(res, cur->yhot);
1103 put_raw_data(res, cur->data, 0);
1105 SetResSize(res, restag); /* Set ResourceSize */
1106 if(win32)
1107 put_pad(res);
1109 return res;
1113 *****************************************************************************
1114 * Function : icongroup2res
1115 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1116 * Input :
1117 * name - Name/ordinal of the resource
1118 * icog - The icon group descriptor
1119 * Output : New .res format structure
1120 * Description :
1121 * Remarks :
1122 *****************************************************************************
1124 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1126 int restag;
1127 res_t *res;
1128 icon_t *ico;
1129 assert(name != NULL);
1130 assert(icog != NULL);
1132 res = new_res();
1133 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1134 if(win32)
1136 put_word(res, 0); /* Reserved */
1137 /* FIXME: The ResType in the NEWHEADER structure should
1138 * contain 14 according to the MS win32 doc. This is
1139 * not the case with the BRC compiler and I really doubt
1140 * the latter. Putting one here is compliant to win16 spec,
1141 * but who knows the true value?
1143 put_word(res, 1); /* ResType */
1144 put_word(res, icog->nicon);
1145 for(ico = icog->iconlist; ico; ico = ico->next)
1147 put_byte(res, ico->width);
1148 put_byte(res, ico->height);
1149 put_byte(res, ico->nclr);
1150 put_byte(res, 0); /* Reserved */
1151 put_word(res, ico->planes);
1152 put_word(res, ico->bits);
1153 put_dword(res, ico->data->size);
1154 put_word(res, ico->id);
1157 else /* win16 */
1159 put_word(res, 0); /* Reserved */
1160 put_word(res, 1); /* ResType */
1161 put_word(res, icog->nicon);
1162 for(ico = icog->iconlist; ico; ico = ico->next)
1164 put_byte(res, ico->width);
1165 put_byte(res, ico->height);
1166 put_byte(res, ico->nclr);
1167 put_byte(res, 0); /* Reserved */
1168 put_word(res, ico->planes);
1169 put_word(res, ico->bits);
1170 put_dword(res, ico->data->size);
1171 put_word(res, ico->id);
1174 SetResSize(res, restag); /* Set ResourceSize */
1175 if(win32)
1176 put_pad(res);
1178 return res;
1182 *****************************************************************************
1183 * Function : icon2res
1184 * Syntax : res_t *icon2res(icon_t *ico)
1185 * Input :
1186 * ico - The icon descriptor
1187 * Output : New .res format structure
1188 * Description :
1189 * Remarks :
1190 *****************************************************************************
1192 static res_t *icon2res(icon_t *ico)
1194 int restag;
1195 res_t *res;
1196 name_id_t name;
1198 assert(ico != NULL);
1200 res = new_res();
1201 name.type = name_ord;
1202 name.name.i_name = ico->id;
1203 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1204 put_raw_data(res, ico->data, 0);
1206 SetResSize(res, restag); /* Set ResourceSize */
1207 if(win32)
1208 put_pad(res);
1210 return res;
1214 *****************************************************************************
1215 * Function : anicurico2res
1216 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1217 * Input :
1218 * name - Name/ordinal of the resource
1219 * ani - The animated object descriptor
1220 * Output : New .res format structure
1221 * Description :
1222 * Remarks : The endian of the object's structures have been converted
1223 * by the loader.
1224 * There are rumors that win311 could handle animated stuff.
1225 * That is why they are generated for both win16 and win32
1226 * compile.
1227 *****************************************************************************
1229 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1231 int restag;
1232 res_t *res;
1233 assert(name != NULL);
1234 assert(ani != NULL);
1236 res = new_res();
1237 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1238 NULL, name, ani->memopt, NULL);
1239 put_raw_data(res, ani->data, 0);
1240 /* Set ResourceSize */
1241 SetResSize(res, restag);
1242 if(win32)
1243 put_pad(res);
1244 return res;
1248 *****************************************************************************
1249 * Function : bitmap2res
1250 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1251 * Input :
1252 * name - Name/ordinal of the resource
1253 * bmp - The bitmap descriptor
1254 * Output : New .res format structure
1255 * Description :
1256 * Remarks : The endian of the bitmap structures have been converted
1257 * by the loader.
1258 *****************************************************************************
1260 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1262 int restag;
1263 res_t *res;
1264 assert(name != NULL);
1265 assert(bmp != NULL);
1267 res = new_res();
1268 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1269 if(bmp->data->data[0] == 'B'
1270 && bmp->data->data[1] == 'M'
1271 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1272 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1274 /* The File header is still attached, don't write it */
1275 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1277 else
1279 put_raw_data(res, bmp->data, 0);
1281 /* Set ResourceSize */
1282 SetResSize(res, restag);
1283 if(win32)
1284 put_pad(res);
1285 return res;
1289 *****************************************************************************
1290 * Function : font2res
1291 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1292 * Input :
1293 * name - Name/ordinal of the resource
1294 * fnt - The font descriptor
1295 * Output : New .res format structure
1296 * Description :
1297 * Remarks : The data has been prepared just after parsing.
1298 *****************************************************************************
1300 static res_t *font2res(name_id_t *name, font_t *fnt)
1302 int restag;
1303 res_t *res;
1304 assert(name != NULL);
1305 assert(fnt != NULL);
1307 res = new_res();
1308 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1309 put_raw_data(res, fnt->data, 0);
1310 /* Set ResourceSize */
1311 SetResSize(res, restag);
1312 if(win32)
1313 put_pad(res);
1314 return res;
1318 *****************************************************************************
1319 * Function : fontdir2res
1320 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1321 * Input :
1322 * name - Name/ordinal of the resource
1323 * fntdir - The fontdir descriptor
1324 * Output : New .res format structure
1325 * Description :
1326 * Remarks : The data has been prepared just after parsing.
1327 *****************************************************************************
1329 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1331 int restag;
1332 res_t *res;
1333 assert(name != NULL);
1334 assert(fnd != NULL);
1336 res = new_res();
1337 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1338 put_raw_data(res, fnd->data, 0);
1339 /* Set ResourceSize */
1340 SetResSize(res, restag);
1341 if(win32)
1342 put_pad(res);
1343 return res;
1347 *****************************************************************************
1348 * Function : html2res
1349 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1350 * Input :
1351 * name - Name/ordinal of the resource
1352 * rdt - The html descriptor
1353 * Output : New .res format structure
1354 * Description :
1355 * Remarks :
1356 *****************************************************************************
1358 static res_t *html2res(name_id_t *name, html_t *html)
1360 int restag;
1361 res_t *res;
1362 assert(name != NULL);
1363 assert(html != NULL);
1365 res = new_res();
1366 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1367 put_raw_data(res, html->data, 0);
1368 /* Set ResourceSize */
1369 SetResSize(res, restag);
1370 if(win32)
1371 put_pad(res);
1372 return res;
1376 *****************************************************************************
1377 * Function : rcdata2res
1378 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1379 * Input :
1380 * name - Name/ordinal of the resource
1381 * rdt - The rcdata descriptor
1382 * Output : New .res format structure
1383 * Description :
1384 * Remarks :
1385 *****************************************************************************
1387 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1389 int restag;
1390 res_t *res;
1391 assert(name != NULL);
1392 assert(rdt != NULL);
1394 res = new_res();
1395 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1396 put_raw_data(res, rdt->data, 0);
1397 /* Set ResourceSize */
1398 SetResSize(res, restag);
1399 if(win32)
1400 put_pad(res);
1401 return res;
1405 *****************************************************************************
1406 * Function : messagetable2res
1407 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1408 * Input :
1409 * name - Name/ordinal of the resource
1410 * msg - The messagetable descriptor
1411 * Output : New .res format structure
1412 * Description :
1413 * Remarks : The data has been converted to the appropriate endian
1414 * after it was parsed.
1415 *****************************************************************************
1417 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1419 int restag;
1420 res_t *res;
1421 assert(name != NULL);
1422 assert(msg != NULL);
1424 res = new_res();
1425 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1426 put_raw_data(res, msg->data, 0);
1427 /* Set ResourceSize */
1428 SetResSize(res, restag);
1429 if(win32)
1430 put_pad(res);
1431 return res;
1435 *****************************************************************************
1436 * Function : stringtable2res
1437 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1438 * Input :
1439 * stt - The stringtable descriptor
1440 * Output : New .res format structure
1441 * Description :
1442 * Remarks :
1443 *****************************************************************************
1445 static res_t *stringtable2res(stringtable_t *stt)
1447 res_t *res;
1448 name_id_t name;
1449 int i;
1450 int restag;
1451 DWORD lastsize = 0;
1453 assert(stt != NULL);
1454 res = new_res();
1456 for(; stt; stt = stt->next)
1458 if(!stt->nentries)
1460 warning("Empty internal stringtable\n");
1461 continue;
1463 name.type = name_ord;
1464 name.name.i_name = (stt->idbase >> 4) + 1;
1465 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1466 for(i = 0; i < stt->nentries; i++)
1468 if(stt->entries[i].str && stt->entries[i].str->size)
1470 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1471 FALSE, win32 ? stt->lvc.language : NULL);
1473 else
1475 if (win32)
1476 put_word(res, 0);
1477 else
1478 put_byte(res, 0);
1481 /* Set ResourceSize */
1482 SetResSize(res, restag - lastsize);
1483 if(win32)
1484 put_pad(res);
1485 lastsize = res->size;
1487 return res;
1491 *****************************************************************************
1492 * Function : user2res
1493 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1494 * Input :
1495 * name - Name/ordinal of the resource
1496 * usr - The userresource descriptor
1497 * Output : New .res format structure
1498 * Description :
1499 * Remarks :
1500 *****************************************************************************
1502 static res_t *user2res(name_id_t *name, user_t *usr)
1504 int restag;
1505 res_t *res;
1506 assert(name != NULL);
1507 assert(usr != NULL);
1509 res = new_res();
1510 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1511 put_raw_data(res, usr->data, 0);
1512 /* Set ResourceSize */
1513 SetResSize(res, restag);
1514 if(win32)
1515 put_pad(res);
1516 return res;
1520 *****************************************************************************
1521 * Function : versionblock2res
1522 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1523 * Input :
1524 * res - Binary resource to write to
1525 * blk - The version block to be written
1526 * Output :
1527 * Description :
1528 * Remarks : Self recursive
1529 *****************************************************************************
1531 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1533 ver_value_t *val;
1534 int blksizetag;
1535 int valblksizetag;
1536 int valvalsizetag;
1537 int tag;
1538 int i;
1540 blksizetag = res->size;
1541 put_word(res, 0); /* Will be overwritten later */
1542 put_word(res, 0);
1543 if(win32)
1544 put_word(res, 0); /* level ? */
1545 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1546 put_pad(res);
1547 for(val = blk->values; val; val = val->next)
1549 if(val->type == val_str)
1551 valblksizetag = res->size;
1552 put_word(res, 0); /* Will be overwritten later */
1553 valvalsizetag = res->size;
1554 put_word(res, 0); /* Will be overwritten later */
1555 if(win32)
1557 put_word(res, level);
1559 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1560 put_pad(res);
1561 tag = res->size;
1562 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1563 if(win32)
1564 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1565 else
1566 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1567 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1568 put_pad(res);
1570 else if(val->type == val_words)
1572 valblksizetag = res->size;
1573 put_word(res, 0); /* Will be overwritten later */
1574 valvalsizetag = res->size;
1575 put_word(res, 0); /* Will be overwritten later */
1576 if(win32)
1578 put_word(res, level);
1580 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1581 put_pad(res);
1582 tag = res->size;
1583 for(i = 0; i < val->value.words->nwords; i++)
1585 put_word(res, val->value.words->words[i]);
1587 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1588 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1589 put_pad(res);
1591 else if(val->type == val_block)
1593 versionblock2res(res, val->value.block, level+1, lang);
1595 else
1597 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1601 /* Set blocksize */
1602 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1606 *****************************************************************************
1607 * Function : versioninfo2res
1608 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1609 * Input :
1610 * name - Name/ordinal of the resource
1611 * ver - The versioninfo descriptor
1612 * Output : New .res format structure
1613 * Description :
1614 * Remarks :
1615 *****************************************************************************
1617 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1619 int restag;
1620 int rootblocksizetag;
1621 int valsizetag;
1622 int tag;
1623 res_t *res;
1624 string_t vsvi;
1625 ver_block_t *blk;
1627 assert(name != NULL);
1628 assert(ver != NULL);
1630 vsvi.type = str_char;
1631 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1632 vsvi.size = 15; /* Excl. termination */
1634 res = new_res();
1635 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1636 rootblocksizetag = res->size;
1637 put_word(res, 0); /* BlockSize filled in later */
1638 valsizetag = res->size;
1639 put_word(res, 0); /* ValueSize filled in later*/
1640 if(win32)
1641 put_word(res, 0); /* Tree-level ? */
1642 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1643 TRUE, win32 ? ver->lvc.language : NULL);
1644 if(win32)
1645 put_pad(res);
1646 tag = res->size;
1647 put_dword(res, VS_FFI_SIGNATURE);
1648 put_dword(res, VS_FFI_STRUCVERSION);
1649 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1650 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1651 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1652 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1653 put_dword(res, ver->fileflagsmask);
1654 put_dword(res, ver->fileflags);
1655 put_dword(res, ver->fileos);
1656 put_dword(res, ver->filetype);
1657 put_dword(res, ver->filesubtype);
1658 put_dword(res, 0); /* FileDateMS */
1659 put_dword(res, 0); /* FileDateLS */
1660 /* Set ValueSize */
1661 set_word(res, valsizetag, (WORD)(res->size - tag));
1662 /* Descend into the blocks */
1663 for(blk = ver->blocks; blk; blk = blk->next)
1664 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1665 /* Set root block's size */
1666 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1668 SetResSize(res, restag);
1669 if(win32)
1670 put_pad(res);
1672 free(vsvi.str.cstr);
1673 return res;
1677 *****************************************************************************
1678 * Function : toolbaritem2res
1679 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1680 * Input :
1681 * Output : nop
1682 * Description :
1683 * Remarks : Self recursive
1684 *****************************************************************************
1686 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1688 toolbar_item_t *itm = tbitem;
1689 assert(win32 != 0);
1690 while(itm)
1692 put_word(res, itm->id);
1693 itm = itm->next;
1699 *****************************************************************************
1700 * Function : toolbar2res
1701 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1702 * Input :
1703 * name - Name/ordinal of the resource
1704 * toolbar - The toolbar descriptor
1705 * Output : New .res format structure
1706 * Description :
1707 * Remarks :
1708 *****************************************************************************
1710 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1712 int restag;
1713 res_t *res;
1714 assert(name != NULL);
1715 assert(toolbar != NULL);
1717 res = new_res();
1718 if(win32)
1720 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1722 put_word(res, 1); /* Menuheader: Version */
1723 put_word(res, toolbar->button_width); /* (in pixels?) */
1724 put_word(res, toolbar->button_height); /* (in pixels?) */
1725 put_word(res, toolbar->nitems);
1726 put_pad(res);
1727 toolbaritem2res(res, toolbar->items);
1728 /* Set ResourceSize */
1729 SetResSize(res, restag);
1730 put_pad(res);
1732 else /* win16 */
1734 /* Do not generate anything in 16-bit mode */
1735 free(res->data);
1736 free(res);
1737 return NULL;
1739 return res;
1743 *****************************************************************************
1744 * Function : dlginit2res
1745 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1746 * Input :
1747 * name - Name/ordinal of the resource
1748 * rdt - The dlginit descriptor
1749 * Output : New .res format structure
1750 * Description :
1751 * Remarks :
1752 *****************************************************************************
1754 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1756 int restag;
1757 res_t *res;
1758 assert(name != NULL);
1759 assert(dit != NULL);
1761 res = new_res();
1762 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1763 put_raw_data(res, dit->data, 0);
1764 /* Set ResourceSize */
1765 SetResSize(res, restag);
1766 if(win32)
1767 put_pad(res);
1768 return res;
1772 *****************************************************************************
1773 * Function : prep_nid_for_label
1774 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1775 * Input :
1776 * Output :
1777 * Description : Converts a resource name into the first 32 (or less)
1778 * characters of the name with conversions.
1779 * Remarks :
1780 *****************************************************************************
1782 #define MAXNAMELEN 32
1783 char *prep_nid_for_label(const name_id_t *nid)
1785 static char buf[MAXNAMELEN+1];
1787 assert(nid != NULL);
1789 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1791 WCHAR *sptr;
1792 int i;
1793 sptr = nid->name.s_name->str.wstr;
1794 buf[0] = '\0';
1795 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1797 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1798 buf[i] = *sptr++;
1799 else
1800 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1802 buf[i] = '\0';
1804 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1806 char *cptr;
1807 int i;
1808 cptr = nid->name.s_name->str.cstr;
1809 buf[0] = '\0';
1810 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1812 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1813 buf[i] = *cptr++;
1814 else
1815 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1817 buf[i] = '\0';
1819 else if(nid->type == name_ord)
1821 sprintf(buf, "%u", nid->name.i_name);
1823 else
1825 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1827 return buf;
1829 #undef MAXNAMELEN
1832 *****************************************************************************
1833 * Function : make_c_name
1834 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1835 * Input :
1836 * Output :
1837 * Description : Converts a resource name into a valid c-identifier in the
1838 * form "_base_nid".
1839 * Remarks :
1840 *****************************************************************************
1842 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1844 int nlen;
1845 char *buf;
1846 char *ret;
1847 char lanbuf[6];
1849 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1850 buf = prep_nid_for_label(nid);
1851 nlen = strlen(buf) + strlen(lanbuf);
1852 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1853 ret = xmalloc(nlen);
1854 strcpy(ret, "_");
1855 strcat(ret, base);
1856 strcat(ret, "_");
1857 strcat(ret, buf);
1858 strcat(ret, "_");
1859 strcat(ret, lanbuf);
1860 return ret;
1864 *****************************************************************************
1865 * Function : get_c_typename
1866 * Syntax : const char *get_c_typename(enum res_e type)
1867 * Input :
1868 * Output :
1869 * Description : Convert resource enum to char string to be used in c-name
1870 * creation.
1871 * Remarks :
1872 *****************************************************************************
1874 const char *get_c_typename(enum res_e type)
1876 switch(type)
1878 case res_acc: return "Acc";
1879 case res_anicur:return "AniCur";
1880 case res_aniico:return "AniIco";
1881 case res_bmp: return "Bmp";
1882 case res_cur: return "Cur";
1883 case res_curg: return "CurGrp";
1884 case res_dlg:
1885 case res_dlgex: return "Dlg";
1886 case res_fnt: return "Fnt";
1887 case res_fntdir:return "FntDir";
1888 case res_ico: return "Ico";
1889 case res_icog: return "IcoGrp";
1890 case res_men:
1891 case res_menex: return "Men";
1892 case res_rdt: return "RCDat";
1893 case res_stt: return "StrTab";
1894 case res_usr: return "Usr";
1895 case res_msg: return "MsgTab";
1896 case res_ver: return "VerInf";
1897 case res_toolbar: return "TlBr";
1898 case res_dlginit: return "DlgInit";
1899 default: return "Oops";
1904 *****************************************************************************
1905 * Function : resources2res
1906 * Syntax : void resources2res(resource_t *top)
1907 * Input :
1908 * top - The resource-tree to convert
1909 * Output :
1910 * Description : Convert logical resource descriptors into binary data
1911 * Remarks :
1912 *****************************************************************************
1914 void resources2res(resource_t *top)
1916 while(top)
1918 switch(top->type)
1920 case res_acc:
1921 if(!top->binres)
1922 top->binres = accelerator2res(top->name, top->res.acc);
1923 break;
1924 case res_bmp:
1925 if(!top->binres)
1926 top->binres = bitmap2res(top->name, top->res.bmp);
1927 break;
1928 case res_cur:
1929 if(!top->binres)
1930 top->binres = cursor2res(top->res.cur);
1931 break;
1932 case res_curg:
1933 if(!top->binres)
1934 top->binres = cursorgroup2res(top->name, top->res.curg);
1935 break;
1936 case res_dlg:
1937 if(!top->binres)
1938 top->binres = dialog2res(top->name, top->res.dlg);
1939 break;
1940 case res_dlgex:
1941 if(!top->binres)
1942 top->binres = dialogex2res(top->name, top->res.dlgex);
1943 break;
1944 case res_fnt:
1945 if(!top->binres)
1946 top->binres = font2res(top->name, top->res.fnt);
1947 break;
1948 case res_fntdir:
1949 if(!top->binres)
1950 top->binres = fontdir2res(top->name, top->res.fnd);
1951 break;
1952 case res_ico:
1953 if(!top->binres)
1954 top->binres = icon2res(top->res.ico);
1955 break;
1956 case res_icog:
1957 if(!top->binres)
1958 top->binres = icongroup2res(top->name, top->res.icog);
1959 break;
1960 case res_men:
1961 if(!top->binres)
1962 top->binres = menu2res(top->name, top->res.men);
1963 break;
1964 case res_menex:
1965 if(!top->binres)
1966 top->binres = menuex2res(top->name, top->res.menex);
1967 break;
1968 case res_html:
1969 if(!top->binres)
1970 top->binres = html2res(top->name, top->res.html);
1971 break;
1972 case res_rdt:
1973 if(!top->binres)
1974 top->binres = rcdata2res(top->name, top->res.rdt);
1975 break;
1976 case res_stt:
1977 if(!top->binres)
1978 top->binres = stringtable2res(top->res.stt);
1979 break;
1980 case res_usr:
1981 if(!top->binres)
1982 top->binres = user2res(top->name, top->res.usr);
1983 break;
1984 case res_msg:
1985 if(!top->binres)
1986 top->binres = messagetable2res(top->name, top->res.msg);
1987 break;
1988 case res_ver:
1989 if(!top->binres)
1990 top->binres = versioninfo2res(top->name, top->res.ver);
1991 break;
1992 case res_toolbar:
1993 if(!top->binres)
1994 top->binres = toolbar2res(top->name, top->res.tbt);
1995 break;
1996 case res_dlginit:
1997 if(!top->binres)
1998 top->binres = dlginit2res(top->name, top->res.dlgi);
1999 break;
2000 case res_anicur:
2001 case res_aniico:
2002 if(!top->binres)
2003 top->binres = anicurico2res(top->name, top->res.ani, top->type);
2004 break;
2005 default:
2006 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
2008 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
2009 top = top->next;