19fd04a41Smaya /*
29fd04a41Smaya __ __ _
39fd04a41Smaya ___\ \/ /_ __ __ _| |_
49fd04a41Smaya / _ \\ /| '_ \ / _` | __|
59fd04a41Smaya | __// \| |_) | (_| | |_
69fd04a41Smaya \___/_/\_\ .__/ \__,_|\__|
79fd04a41Smaya |_| XML parser
89fd04a41Smaya
99fd04a41Smaya Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10*0315d311Schristos Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net>
11*0315d311Schristos Copyright (c) 2002 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
12*0315d311Schristos Copyright (c) 2005-2006 Karl Waclawek <karl@waclawek.net>
13*0315d311Schristos Copyright (c) 2016-2019 Sebastian Pipping <sebastian@pipping.org>
14*0315d311Schristos Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
159fd04a41Smaya Licensed under the MIT license:
169fd04a41Smaya
179fd04a41Smaya Permission is hereby granted, free of charge, to any person obtaining
189fd04a41Smaya a copy of this software and associated documentation files (the
199fd04a41Smaya "Software"), to deal in the Software without restriction, including
209fd04a41Smaya without limitation the rights to use, copy, modify, merge, publish,
219fd04a41Smaya distribute, sublicense, and/or sell copies of the Software, and to permit
229fd04a41Smaya persons to whom the Software is furnished to do so, subject to the
239fd04a41Smaya following conditions:
249fd04a41Smaya
259fd04a41Smaya The above copyright notice and this permission notice shall be included
269fd04a41Smaya in all copies or substantial portions of the Software.
279fd04a41Smaya
289fd04a41Smaya THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
299fd04a41Smaya EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
309fd04a41Smaya MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
319fd04a41Smaya NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
329fd04a41Smaya DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
339fd04a41Smaya OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
349fd04a41Smaya USE OR OTHER DEALINGS IN THE SOFTWARE.
35d50d1d7fStron */
36d50d1d7fStron
37d50d1d7fStron #include "codepage.h"
38024103ffSspz #include "internal.h" /* for UNUSED_P only */
39d50d1d7fStron
404c74f82dSchristos #if defined(_WIN32)
41d50d1d7fStron # define STRICT 1
42d50d1d7fStron # define WIN32_LEAN_AND_MEAN 1
43d50d1d7fStron
44d50d1d7fStron # include <windows.h>
45*0315d311Schristos #endif /* defined(_WIN32) */
46d50d1d7fStron
47d50d1d7fStron int
codepageMap(int cp,int * map)489fd04a41Smaya codepageMap(int cp, int *map) {
49*0315d311Schristos #if defined(_WIN32)
50d50d1d7fStron int i;
51d50d1d7fStron CPINFO info;
52d50d1d7fStron if (! GetCPInfo(cp, &info) || info.MaxCharSize > 2)
53d50d1d7fStron return 0;
54d50d1d7fStron for (i = 0; i < 256; i++)
55d50d1d7fStron map[i] = -1;
56d50d1d7fStron if (info.MaxCharSize > 1) {
57d50d1d7fStron for (i = 0; i < MAX_LEADBYTES; i += 2) {
58d50d1d7fStron int j, lim;
59d50d1d7fStron if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
60d50d1d7fStron break;
61d50d1d7fStron lim = info.LeadByte[i + 1];
62d50d1d7fStron for (j = info.LeadByte[i]; j <= lim; j++)
63d50d1d7fStron map[j] = -2;
64d50d1d7fStron }
65d50d1d7fStron }
66d50d1d7fStron for (i = 0; i < 256; i++) {
67d50d1d7fStron if (map[i] == -1) {
68d50d1d7fStron char c = (char)i;
69d50d1d7fStron unsigned short n;
709fd04a41Smaya if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, &c, 1,
719fd04a41Smaya &n, 1)
729fd04a41Smaya == 1)
73d50d1d7fStron map[i] = n;
74d50d1d7fStron }
75d50d1d7fStron }
76d50d1d7fStron return 1;
77*0315d311Schristos #else
78*0315d311Schristos UNUSED_P(cp);
79*0315d311Schristos UNUSED_P(map);
80*0315d311Schristos return 0;
81*0315d311Schristos #endif
82d50d1d7fStron }
83d50d1d7fStron
84d50d1d7fStron int
codepageConvert(int cp,const char * p)859fd04a41Smaya codepageConvert(int cp, const char *p) {
86*0315d311Schristos #if defined(_WIN32)
87d50d1d7fStron unsigned short c;
889fd04a41Smaya if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, p, 2, &c,
899fd04a41Smaya 1)
909fd04a41Smaya == 1)
91d50d1d7fStron return c;
92d50d1d7fStron return -1;
93*0315d311Schristos #else
949fd04a41Smaya UNUSED_P(cp);
959fd04a41Smaya UNUSED_P(p);
96d50d1d7fStron return -1;
97*0315d311Schristos #endif
98d50d1d7fStron }
99