1 /* winduni.c -- unicode support for the windres program.
2    Copyright 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor, Cygnus Support.
4 
5    This file is part of GNU Binutils.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21 
22 /* This file contains unicode support routines for the windres
23    program.  Ideally, we would have generic unicode support which
24    would work on all systems.  However, we don't.  Instead, on a
25    Windows host, we are prepared to call some Windows routines.  This
26    means that we will generate different output on Windows and Unix
27    hosts, but that seems better than not really supporting unicode at
28    all.  */
29 
30 #include "bfd.h"
31 #include "bucomm.h"
32 #include "winduni.h"
33 #include "safe-ctype.h"
34 
35 #ifdef _WIN32
36 #include <windows.h>
37 #endif
38 
39 /* Convert an ASCII string to a unicode string.  We just copy it,
40    expanding chars to shorts, rather than doing something intelligent.  */
41 
42 void
unicode_from_ascii(int * length,unichar ** unicode,const char * ascii)43 unicode_from_ascii (int *length, unichar **unicode, const char *ascii)
44 {
45   int len;
46 #ifndef _WIN32
47   const char *s;
48   unsigned short *w;
49 
50   len = strlen (ascii);
51   *unicode = ((unichar *) res_alloc ((len + 1) * sizeof (unichar)));
52   for (s = ascii, w = *unicode; *s != '\0'; s++, w++)
53     *w = *s & 0xff;
54   *w = 0;
55 #else
56   /* We use  MultiByteToWideChar rather than strlen to get the unicode
57      string length to allow multibyte "ascii" chars. The value returned
58      by this function includes the trailing '\0'.  */
59   len = MultiByteToWideChar (CP_ACP, 0, ascii, -1, NULL, 0);
60   if (len)
61     {
62       *unicode = ((unichar *) res_alloc (len * sizeof (unichar)));
63       MultiByteToWideChar (CP_ACP, 0, ascii, -1, *unicode, len);
64     }
65   /* Discount the trailing '/0'.  If MultiByteToWideChar failed,
66      this will set *length to -1.  */
67   len--;
68 #endif
69 
70   if (length != NULL)
71     *length = len;
72 }
73 
74 /* Print the unicode string UNICODE to the file E.  LENGTH is the
75    number of characters to print, or -1 if we should print until the
76    end of the string.  FIXME: On a Windows host, we should be calling
77    some Windows function, probably WideCharToMultiByte.  */
78 
79 void
unicode_print(FILE * e,const unichar * unicode,int length)80 unicode_print (FILE *e, const unichar *unicode, int length)
81 {
82   while (1)
83     {
84       unichar ch;
85 
86       if (length == 0)
87 	return;
88       if (length > 0)
89 	--length;
90 
91       ch = *unicode;
92 
93       if (ch == 0 && length < 0)
94 	return;
95 
96       ++unicode;
97 
98       if ((ch & 0x7f) == ch)
99 	{
100 	  if (ch == '\\')
101 	    fputs ("\\", e);
102 	  else if (ISPRINT (ch))
103 	    putc (ch, e);
104 	  else
105 	    {
106 	      switch (ch)
107 		{
108 		case ESCAPE_A:
109 		  fputs ("\\a", e);
110 		  break;
111 
112 		case ESCAPE_B:
113 		  fputs ("\\b", e);
114 		  break;
115 
116 		case ESCAPE_F:
117 		  fputs ("\\f", e);
118 		  break;
119 
120 		case ESCAPE_N:
121 		  fputs ("\\n", e);
122 		  break;
123 
124 		case ESCAPE_R:
125 		  fputs ("\\r", e);
126 		  break;
127 
128 		case ESCAPE_T:
129 		  fputs ("\\t", e);
130 		  break;
131 
132 		case ESCAPE_V:
133 		  fputs ("\\v", e);
134 		  break;
135 
136 		default:
137 		  fprintf (e, "\\%03o", (unsigned int) ch);
138 		  break;
139 		}
140 	    }
141 	}
142       else if ((ch & 0xff) == ch)
143 	fprintf (e, "\\%03o", (unsigned int) ch);
144       else
145 	fprintf (e, "\\x%x", (unsigned int) ch);
146     }
147 }
148