3 * Copyright Martin von Loewis, 1994
12 #include <sys/types.h>
18 char usage[]="winerc -bdvc -p prefix -o outfile < infile \n"
19 " -b Create a C array from a binary .res file\n"
20 " -c Add 'const' prefix to C constants\n"
21 " -d Output debugging information\n"
22 " -h Also generate a .h file\n"
23 " -p prefix Give a prefix for the generated names\n"
24 " -v Show each resource as it is processed\n"
25 " -o file Output to file.c and file.h\n"
26 " -w 16|32 Select win16 or win32 output\n";
28 /*might be overwritten by command line*/
29 char *prefix="_Resource";
33 static FILE *header = NULL, *code = NULL;
35 int transform_binary_file(void);
38 static void *xmalloc (size_t size)
42 res = malloc (size ? size : 1);
45 fprintf (stderr, "Virtual memory exhausted.\n");
52 int main(int argc,char *argv[])
56 int optc,lose = 0, ret, binary = 0, output_header = 0;
57 char output_name[256];
59 while((optc=getopt(argc,argv,"bcdhp:vo:w:"))!=EOF)
62 /* bison will print state transitions on stderr */
69 case 'h':output_header=1; break;
70 case 'p':prefix=strdup(optarg); break;
71 case 'c':constant=1;break;
75 case 'o':sprintf(output_name,"%s.c",optarg); break;
76 case 'w':if(!strcmp(optarg,"16"))win32=0;
77 else if(!strcmp(optarg,"32"))win32=1;
80 default: lose++;break;
82 if(lose)return fprintf(stderr,usage),1;
86 code = fopen( output_name, "w" );
89 output_name[strlen(output_name)-1] = 'h';
90 header = fopen( output_name, "w" );
93 if (!code) code = stdout;
95 ret=transform_binary_file();
98 if (header) fclose(header);
100 if (ret) /* There was an error */
102 if (header) unlink( output_name );
103 output_name[strlen(output_name)-1] = 'c';
104 unlink( output_name );
109 int transform_binary_file()
112 if (header) fprintf(header,"#define APPLICATION_HAS_RESOURCES 1\n");
113 fprintf(code,"char _Application_resources[]={");
118 if(i%16==0)fputc('\n',code);
119 fprintf(code,"%3d,",c);
121 fprintf(code,"\n 0};\nint _Application_resources_size=%d;\n",i);
125 /* SunOS' memcpy is wrong for overlapping arrays */
126 char *save_memcpy(char *d,char* s,int l)
129 for(;l;l--)*d++=*s++;
131 for(d+=l-1,s+=l-1;l;l--)*d--=*s--;
135 /*allow unaligned access*/
136 void put_WORD(unsigned char* p,WORD w)
142 void put_DWORD(unsigned char* p,DWORD d)
144 put_WORD(p,d&0xFFFF);
148 WORD get_WORD(unsigned char* p)
150 return *p|(*(p+1)<<8);
153 DWORD get_DWORD(unsigned char* p)
155 return get_WORD(p)|(get_WORD(p+2)<<16);
159 /*create a new gen_res, initial size 100*/
161 { gen_res* ret=xmalloc(sizeof(gen_res)+100);
163 for(i=0;i<sizeof(gen_res)+100;i++)*((char*)ret+i)='\0';
172 gen_res* grow(gen_res* res)
174 res=realloc(res,sizeof(gen_res)+2*res->space);
176 fprintf(stderr,"Out of memory\n"),exit(1);
177 if(!res->g_prev)g_start=res;
178 else res->g_prev->g_next=res;
179 if(res->g_next)res->g_next->g_prev=res;
180 res->space=2*res->space;
185 /* insert bytes at offset 0, increase num_entries */
186 gen_res* insert_at_beginning(gen_res* res,char* entry,int size)
188 while(res->size+size>res->space)res=grow(res);
189 save_memcpy(res->res+size,res->res,res->size);
190 save_memcpy(res->res,entry,size);
196 /* insert length characters from bytes into res, starting at start */
197 gen_res* insert_bytes(gen_res* res,char* bytes,int start,int length)
199 while(res->size+length>res->space)res=grow(res);
200 save_memcpy(res->res+start+length,res->res+start,res->size-start);
201 save_memcpy(res->res+start,bytes,length);
206 /* insert string into res, starting at start */
207 gen_res* insert_string(gen_res* res,unsigned char* string,int start,int terminating0)
210 int lengthA = strlen(string) + (terminating0 ? 1 : 0);
211 int length = (win32 ? 2 : 1) * lengthA;
212 while(res->size+length>res->space)res=grow(res);
213 save_memcpy(res->res+start+length,res->res+start,res->size-start);
218 put_WORD(p, *string++);
227 /* insert string at offset 0, increase num_entries */
228 gen_res* insert_string_at_beginning(gen_res* res,char* entry,int terminating0)
230 res=insert_string(res,entry,0,terminating0);
235 /*delete len bytes from res, starting at start*/
236 gen_res* delete_bytes(gen_res* res,int start,int len)
238 save_memcpy(res->res+start,res->res+start+len,res->size-start-len);
243 /*create a new style*/
244 rc_style *new_style()
246 rc_style *ret=xmalloc(sizeof(rc_style));
247 /*initially, no bits have to be reset*/
249 /*initially, no bits are set*/
250 ret->or=WS_CHILD | WS_VISIBLE;
254 /* entries are inserted at the beginning, starting from the last one */
255 gen_res* add_accelerator(int ev, int id, int flags, gen_res* prev)
258 if(prev->num_entries==0)flags|=0x80; /* last entry */
259 accel_entry[0]=flags;
260 put_WORD(accel_entry+1,ev);
261 put_WORD(accel_entry+3,id);
262 return insert_at_beginning(prev,accel_entry,5);
266 /* create an integer from the event, taking things as "^c" into account
267 add this as new entry */
268 gen_res* add_string_accelerator(char *ev, int id, int flags, gen_res* prev)
275 return add_accelerator(event,id,flags,prev);
278 /*is there a difference between ASCII and VIRTKEY accelerators? */
280 gen_res* add_ascii_accelerator(int ev, int id, int flags, gen_res* prev)
282 return add_accelerator(ev,id,flags,prev);
285 gen_res* add_vk_accelerator(int ev, int id, int flags, gen_res* prev)
287 return add_accelerator(ev,id,flags,prev);
290 /* create a new dialog header, set all items to 0 */
291 gen_res* new_dialog()
292 { gen_res* ret=new_res();
293 ret->size=win32?24:16; /*all strings "\0", no font*/
297 /* the STYLE option was specified */
298 gen_res* dialog_style(rc_style* style, gen_res* attr)
300 /* default dialog styles? Do we need style->and? */
301 /* DS_SETFONT might have been specified before */
302 put_DWORD(attr->res,get_DWORD(attr->res)|style->or);
306 /* menu name is at offset 13 (win32: 18) */
307 int dialog_get_menu(gen_res* attr)
312 /* the class is after the menu name */
313 int dialog_get_class(gen_res* attr)
315 int offs=dialog_get_menu(attr);
316 while(attr->res[offs]||(win32&&attr->res[offs+1]))offs+=win32?2:1;
321 /* the caption is after the class */
322 int dialog_get_caption(gen_res* attr)
324 int offs=dialog_get_class(attr);
325 while(attr->res[offs]||(win32&&attr->res[offs+1]))offs+=win32?2:1;
330 /* the fontsize, if present, is after the caption, followed by the font name */
331 int dialog_get_fontsize(gen_res* attr)
333 int offs=dialog_get_caption(attr);
334 while(attr->res[offs]||(win32&&attr->res[offs+1]))offs+=win32?2:1;
340 /* the CAPTION option was specified */
341 gen_res* dialog_caption(char* cap, gen_res*attr)
343 /* we don't need the terminating 0 as it's already there */
344 return insert_string(attr,cap,dialog_get_caption(attr),0);
348 /* the FONT option was specified, set the DS_SETFONT flag */
349 gen_res* dialog_font(short size,char* font,gen_res *attr)
352 int offs=dialog_get_fontsize(attr);
353 put_DWORD(attr->res,get_DWORD(attr->res)|DS_SETFONT);
354 put_WORD(c_size,size);
355 attr=insert_bytes(attr,c_size,offs,2);
357 /* as there is no font name by default, copy the '\0' */
358 return insert_string(attr,font,offs,1);
361 gen_res* dialog_class(char* cap, gen_res*attr)
363 return insert_string(attr,cap,dialog_get_class(attr),0);
366 gen_res* dialog_menu_id(short nr, gen_res*attr)
369 int offs=dialog_get_menu(attr);
370 attr->res[offs] = 0xff;
371 if (win32) attr->res[offs+1] = 0xff;
373 return insert_bytes(attr,c_nr,offs+(win32?2:1),2);
375 gen_res* dialog_menu_str(char* cap, gen_res*attr)
377 return insert_string(attr,cap,dialog_get_menu(attr),0);
380 /* set the dialogs id, position, extent, and style */
381 gen_res* create_control_desc(int id,int x,int y,int cx, int cy,rc_style *style)
382 { gen_res* ret=new_res();
383 int s=WS_VISIBLE|WS_CHILD; /*defaults styles for any control*/
386 if(style)s=(s|style->or)&style->and;
387 put_DWORD(ret->res+0,s);
389 /* put_WORD(ret->res+4, exStyle); */
390 put_WORD(ret->res+8,x);
391 put_WORD(ret->res+10,y);
392 put_WORD(ret->res+12,cx);
393 put_WORD(ret->res+14,cy);
394 put_WORD(ret->res+16,id);
395 ret->size=24; /*empty strings, unused byte*/
399 put_WORD(ret->res+0,x);
400 put_WORD(ret->res+2,y);
401 put_WORD(ret->res+4,cx);
402 put_WORD(ret->res+6,cy);
403 put_WORD(ret->res+8,id);
404 if(style)s=(s|style->or)&style->and;
405 put_DWORD(ret->res+10,s);
406 ret->size=17; /*empty strings, unused byte*/
411 /* insert the control's label */
412 gen_res* label_control_desc(char* label,gen_res* cd)
416 if(get_WORD(cd->res+18)==0xffff)offs=20; /* one-character class */
418 for(offs=18;get_WORD(cd->res+offs);offs+=2);
423 if(cd->res[14]&0x80)offs=15; /* one-character class */
425 for(offs=14;cd->res[offs];offs++);
429 return insert_string(cd,label,offs,0);
432 /* a CONTROL was specified */
433 gen_res* create_generic_control(char* label,int id,char* class,
434 rc_style*style,int x,int y,int cx,int cy)
435 { gen_res* ret=new_res();
436 int s=WS_VISIBLE|WS_CHILD; /*default styles for any control*/
437 if(style)s=(s|style->or)&style->and;
441 put_DWORD(ret->res+0,s);
443 /* put_DWORD(ret->res+4,exstyle->or); */
444 put_WORD(ret->res+8,x);
445 put_WORD(ret->res+10,y);
446 put_WORD(ret->res+12,cx);
447 put_WORD(ret->res+14,cy);
448 put_WORD(ret->res+16,id);
450 ret=insert_string(ret,label,20,0);
451 /* is it a predefined class? */
453 if(!strcasecmp(class,"BUTTON"))cl=CT_BUTTON;
454 if(!strcasecmp(class,"EDIT"))cl=CT_EDIT;
455 if(!strcasecmp(class,"STATIC"))cl=CT_STATIC;
456 if(!strcasecmp(class,"LISTBOX"))cl=CT_LISTBOX;
457 if(!strcasecmp(class,"SCROLLBAR"))cl=CT_SCROLLBAR;
458 if(!strcasecmp(class,"COMBOBOX"))cl=CT_COMBOBOX;
460 char ffff[2]={0xff, 0xff};
461 ret=insert_bytes(ret,ffff,18,2);
462 put_WORD(ret->res+20,cl);
464 else ret=insert_string(ret,class,18,0);
469 put_WORD(ret->res+0,x);
470 put_WORD(ret->res+2,y);
471 put_WORD(ret->res+4,cx);
472 put_WORD(ret->res+6,cy);
473 put_WORD(ret->res+8,id);
474 put_DWORD(ret->res+10,s);
476 ret=insert_string(ret,label,15,0);
477 /* is it a predefined class? */
479 if(!strcasecmp(class,"BUTTON"))cl=CT_BUTTON;
480 if(!strcasecmp(class,"EDIT"))cl=CT_EDIT;
481 if(!strcasecmp(class,"STATIC"))cl=CT_STATIC;
482 if(!strcasecmp(class,"LISTBOX"))cl=CT_LISTBOX;
483 if(!strcasecmp(class,"SCROLLBAR"))cl=CT_SCROLLBAR;
484 if(!strcasecmp(class,"COMBOBOX"))cl=CT_COMBOBOX;
485 if(cl)ret->res[14]=cl;
486 else ret=insert_string(ret,class,14,0);
491 /* insert cd into rest, set the type, add flags */
492 gen_res* add_control(int type,int flags,gen_res*cd,gen_res* rest)
494 char zeros[4]={0,0,0,0};
497 char ffff[2]={0xff, 0xff};
498 put_DWORD(cd->res+0,get_DWORD(cd->res+0)|flags);
499 cd=insert_bytes(cd,ffff,18,2);
500 put_WORD(cd->res+20,type);
504 put_DWORD(cd->res+10,get_DWORD(cd->res+10)|flags);
507 /* WIN32: First control is on dword boundary */
508 if(win32 && cd->size%4)
509 cd=insert_bytes(cd,zeros,cd->size,4-cd->size%4);
510 return insert_at_beginning(rest,cd->res,cd->size);
513 /* an ICON control was specified, whf contains width, height, and flags */
514 gen_res* add_icon(char* name,int id,int x,int y,gen_res* whf,gen_res* rest)
518 put_WORD(whf->res+8,x);
519 put_WORD(whf->res+10,y);
520 put_WORD(whf->res+16,id);
524 put_WORD(whf->res+0,x);
525 put_WORD(whf->res+2,y);
526 put_WORD(whf->res+8,id);
528 whf=label_control_desc(name,whf);
529 return add_control(CT_STATIC,SS_ICON,whf,rest);
532 /* insert the generic control into rest */
533 gen_res* add_generic_control(gen_res* ctl, gen_res* rest)
535 char zeros[4]={0,0,0,0};
536 /* WIN32: Control is on dword boundary */
537 if(win32 && ctl->size%4)
538 ctl=insert_bytes(ctl,zeros,ctl->size,4-ctl->size%4);
539 return insert_at_beginning(rest,ctl->res,ctl->size);
542 /* create a dialog resource by inserting the header into the controls.
543 Set position and extent */
544 gen_res* make_dialog(gen_res* header,int x,int y,int cx,int cy,gen_res* ctls)
546 char zeros[4]={0,0,0,0};
549 put_WORD(header->res+8, ctls->num_entries);
551 put_WORD(header->res+10,x);
552 put_WORD(header->res+12,y);
553 put_WORD(header->res+14,cx);
554 put_WORD(header->res+16,cy);
558 header->res[4]=ctls->num_entries;
560 put_WORD(header->res+5,x);
561 put_WORD(header->res+7,y);
562 put_WORD(header->res+9,cx);
563 put_WORD(header->res+11,cy);
565 /* WIN32: First control is on dword boundary */
566 if(win32 && header->size%4)
567 header=insert_bytes(header,zeros,header->size,4-header->size%4);
568 return insert_bytes(header,ctls->res,header->size,ctls->size);
571 /* create {0x15,0x16,0xFF} from '15 16 FF' */
572 gen_res *hex_to_raw(char *hex, gen_res*rest)
576 for(i=0;*hex!='\'';i++)r2[i]=strtoul(hex,&hex,16);
577 return insert_bytes(rest,r2,0,i);
580 /* create a bitmap resource */
581 gen_res *make_bitmap(gen_res* res)
583 res=delete_bytes(res,0,14); /* skip bitmap file header*/
588 gen_res *make_icon(gen_res* res)
594 gen_res *make_cursor(gen_res* res)
600 /* load resource bytes from the file name */
601 gen_res *load_file(char* name)
605 int f=open(name,O_RDONLY);
613 while(res->space<st.st_size)res=grow(res);
614 read(f,res->res,st.st_size);
615 res->size=st.st_size;
620 /* insert a normal menu item into res, starting from the last item */
621 gen_res *add_menuitem(char* name,int id,int flags,gen_res *res)
624 if(res->num_entries==0)flags|=MF_END;
625 put_WORD(item,flags);
627 res=insert_string_at_beginning(res,name,1);
628 res=insert_bytes(res,item,0,4);
632 /* insert a popup item into res */
633 gen_res *add_popup(char *name,short flags, gen_res* body, gen_res*res)
637 if(res->num_entries==0)flags|=MF_END;
638 put_WORD(c_flags,flags);
639 res=insert_at_beginning(res,body->res,body->size);
640 res=insert_string(res,name,0,1);
641 res=insert_bytes(res,c_flags,0,2);
645 /* prefix the menu header into res */
646 gen_res *make_menu(gen_res* res)
648 static char header[4]={0,0,0,0};
649 res=insert_at_beginning(res,header,4);
654 /* link top-level resources */
655 gen_res *add_resource(gen_res* first,gen_res *rest)
666 typedef struct str_tbl_elm{
668 struct str_tbl_elm *next;
672 str_tbl_elm* string_table=NULL; /* sorted by group */
674 void add_str_tbl_elm(int id,char* str)
679 str_tbl_elm** elm=&string_table;
680 while(*elm && (*elm)->group<group) elm=&(*elm)->next;
681 if(!*elm || (*elm)->group!=group)
684 str_tbl_elm* new=xmalloc(sizeof(str_tbl_elm));
685 for(i=0; i<16; i++) new->strings[i] = NULL;
690 (*elm)->strings[idx]=str;
693 gen_res* add_string_table(gen_res* t)
701 if(!string_table) return t;
702 for(ste=string_table; ste; ste=ste->next)
704 for(size=0,i=0; i<16; i++)
705 size += (win32 ? 2 : 1) * (ste->strings[i] ? strlen(ste->strings[i])+1 : 1);
707 while(res->space<size)res=grow(res);
709 res->n.i_name=ste->group;
713 for(p=res->res,i=0; i<16; i++)
714 if((q=ste->strings[i])==NULL)
721 put_WORD(p, strlen(q));
730 for(p=res->res,i=0; i<16; i++)
731 if((q=ste->strings[i])==NULL)
736 while(*q) *p++ = *q++;
738 t=add_resource(res,t);
743 char *get_typename(gen_res* t)
746 case acc:return "ACCELERATOR";
747 case bmp:return "BITMAP";
748 case cur:return "CURSOR";
749 case dlg:return "DIALOG";
750 case fnt:return "FONT";
751 case ico:return "ICON";
752 case men:return "MENU";
753 case rdt:return "RCDATA";
754 case str:return "STRINGTABLE";
755 default: return "UNKNOWN";
759 /* create strings like _Sysres_DIALOG_2 */
760 char *get_resource_name(gen_res*it)
762 static char buf[1000];
764 sprintf(buf,"%s_%s_%s",prefix,get_typename(it),it->n.s_name);
766 sprintf(buf,"%s_%s_%d",prefix,get_typename(it),it->n.i_name);
770 #define ISCONSTANT (constant ? "const " : "")
772 /* create the final output */
773 void create_output(gen_res* top)
777 top=add_string_table(top);
779 /* Generate the header */
785 " * This file is automatically generated. Do not edit!\n"
789 "struct resource;\n\n",
792 /* Declare the resources */
793 for (it=top;it;it=it->next)
794 fprintf( header,"extern %sstruct resource %s;\n",
795 ISCONSTANT, get_resource_name(it) );
796 fprintf( header,"\nextern %sstruct resource * %s%s_Table[];\n\n",
797 ISCONSTANT, ISCONSTANT, prefix );
798 fprintf( header, "#endif /* __%s_H */\n", prefix );
801 /* Print the resources bytes */
803 fprintf( code, "/*\n"
804 " * This file is automatically generated. Do not edit!\n"
806 "struct resource {\n"
809 "\tconst char *name;\n"
810 "\tconst unsigned char* bytes;\n"
814 for(it=top;it;it=it->next)
817 fprintf( code, "static %sunsigned char %s__bytes[]%s = {\n",
818 ISCONSTANT, get_resource_name(it),
819 win32?"\n__attribute__ ((aligned (4)))":"");
820 for (i=0;i<it->size-1;i++)
822 fprintf(code,"0x%02x, ",it->res[i]);
823 if ((i&7)==7)fputc('\n',code);
825 fprintf(code,"0x%02x };\n\n",it->res[i]);
828 /* Print the resources names */
831 for(it=top;it;it=it->next)
834 char s_buffer[20], *s_name=s_buffer;
835 if(it->n_type) s_name=it->n.s_name;
836 else sprintf(s_name,"@%d",it->n.i_name);
837 fprintf( code, "static %sunsigned char %s__name[] = {\n",
838 ISCONSTANT, get_resource_name(it) );
839 for (i=0;*s_name;i++,s_name++)
841 fprintf(code,"0x%02x, 0x00, ",*s_name);
842 if ((i&3)==3)fputc('\n',code);
844 fprintf(code,"0x00, 0x00};\n\n");
847 /* Print the resources */
848 for (it=top;it;it=it->next)
853 case acc:type=(int)RT_ACCELERATOR16;break;
854 case bmp:type=(int)RT_BITMAP16;break;
855 case cur:type=(int)RT_CURSOR16;break;
856 case dlg:type=(int)RT_DIALOG16;break;
857 case fnt:type=(int)RT_FONT16;break;
858 case ico:type=(int)RT_ICON16;break;
859 case men:type=(int)RT_MENU16;break;
860 case rdt:type=(int)RT_RCDATA16;break;
861 case str:type=(int)RT_STRING16;break;
862 default:fprintf(stderr,"Unknown restype\n");type=-1;break;
867 fprintf(code,"%sstruct resource %s = {0,%d,%s__name,%s__bytes,%d};\n",
868 ISCONSTANT, get_resource_name(it), type, get_resource_name(it),
869 get_resource_name(it), it->size );
871 fprintf(code,"%sstruct resource %s = {%d,%d,%s__name,%s__bytes,%d};\n",
872 ISCONSTANT, get_resource_name(it), it->n.i_name, type,
873 get_resource_name(it), get_resource_name(it), it->size );
878 fprintf(code,"%sstruct resource %s = {0,%d,\"%s\",%s__bytes,%d};\n",
879 ISCONSTANT, get_resource_name(it), type, it->n.s_name,
880 get_resource_name(it), it->size );
882 fprintf(code,"%sstruct resource %s = {%d,%d,\"@%d\",%s__bytes,%d};\n",
883 ISCONSTANT, get_resource_name(it), it->n.i_name, type,
884 it->n.i_name, get_resource_name(it), it->size );
888 /* Print the resource table (NULL terminated) */
890 fprintf(code,"\n%sstruct resource * %s%s_Table[] = {\n",
891 ISCONSTANT, ISCONSTANT, prefix);
892 for (it=top;it;it=it->next)
893 fprintf( code, " &%s,\n", get_resource_name(it) );
894 fprintf( code, " 0\n};\n\n\n" );
896 /* Perform autoregistration */
899 "#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ >= 7)\n"
900 "static void DoIt(void) __attribute__((constructor));\n"
902 "static void DoIt(void);\n"
903 "void LIBWINE_Register_%s(void) { DoIt(); }\n"
905 "static void DoIt(void)\n"
907 "\textern void LIBRES_RegisterResources(const struct resource* const * Res);\n"
908 "\tLIBRES_RegisterResources(%s_Table);\n"
910 "#endif /* __WINE__ */\n"
914 gen_res* make_font(gen_res* res)
916 fprintf(stderr,"Fonts not supported\n");
920 gen_res* make_raw(gen_res* res)
922 fprintf(stderr,"RCData not supported\n");
926 gen_res* int_to_raw(int i,gen_res* res)
928 fprintf(stderr,"IntToRaw not supported\n");
932 /* translate "Hello,\\tworld!\\10" to "Hello,\tworld!\n" */
933 char *parse_c_string(char *in)
935 char *out=xmalloc(strlen(in)-1);
938 for(it=out,in++;*in;in++)
941 {case 't':*it++='\t';break;
942 case 'r':*it++='\r';break;
943 case 'n':*it++='\n';break;
944 case 'a':*it++='\a';break;
946 memset(tmp,0,5);/*make sure it doesn't use more than 4 chars*/
948 *it++=strtoul(tmp,&tend,0);
951 case '1':case '2':case '3':case '4':case '5':
952 case '6':case '7':case '8':case '9':
955 *it++=strtoul(tmp,&tend,10);
961 *it++=strtoul(tmp,&tend,16);