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