xref: /freebsd/contrib/expat/xmlwf/codepage.c (revision ec4deee4e4f2aef1b97d9424f25d04e91fd7dc10)
1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10    Copyright (c) 2000-2017 Expat development team
11    Licensed under the MIT license:
12 
13    Permission is  hereby granted,  free of charge,  to any  person obtaining
14    a  copy  of  this  software   and  associated  documentation  files  (the
15    "Software"),  to  deal in  the  Software  without restriction,  including
16    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17    distribute, sublicense, and/or sell copies of the Software, and to permit
18    persons  to whom  the Software  is  furnished to  do so,  subject to  the
19    following conditions:
20 
21    The above copyright  notice and this permission notice  shall be included
22    in all copies or substantial portions of the Software.
23 
24    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30    USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32 
33 #include "codepage.h"
34 #include "internal.h" /* for UNUSED_P only */
35 
36 #if defined(_WIN32)
37 #  define STRICT 1
38 #  define WIN32_LEAN_AND_MEAN 1
39 
40 #  include <windows.h>
41 
42 int
43 codepageMap(int cp, int *map) {
44   int i;
45   CPINFO info;
46   if (! GetCPInfo(cp, &info) || info.MaxCharSize > 2)
47     return 0;
48   for (i = 0; i < 256; i++)
49     map[i] = -1;
50   if (info.MaxCharSize > 1) {
51     for (i = 0; i < MAX_LEADBYTES; i += 2) {
52       int j, lim;
53       if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
54         break;
55       lim = info.LeadByte[i + 1];
56       for (j = info.LeadByte[i]; j <= lim; j++)
57         map[j] = -2;
58     }
59   }
60   for (i = 0; i < 256; i++) {
61     if (map[i] == -1) {
62       char c = (char)i;
63       unsigned short n;
64       if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, &c, 1,
65                               &n, 1)
66           == 1)
67         map[i] = n;
68     }
69   }
70   return 1;
71 }
72 
73 int
74 codepageConvert(int cp, const char *p) {
75   unsigned short c;
76   if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, p, 2, &c,
77                           1)
78       == 1)
79     return c;
80   return -1;
81 }
82 
83 #else /* not _WIN32 */
84 
85 int
86 codepageMap(int cp, int *map) {
87   UNUSED_P(cp);
88   UNUSED_P(map);
89   return 0;
90 }
91 
92 int
93 codepageConvert(int cp, const char *p) {
94   UNUSED_P(cp);
95   UNUSED_P(p);
96   return -1;
97 }
98 
99 #endif /* not _WIN32 */
100