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