blob: 8e0b5d199cdd8cc42cadb2e24eeef93f49c42995 [file] [log] [blame]
Elliott Hughes72472942018-01-10 08:36:10 -08001/*
2 __ __ _
3 ___\ \/ /_ __ __ _| |_
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
7 |_| XML parser
8
9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
Elliott Hughes33bf9752021-08-10 17:33:34 -070010 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>
Elliott Hughes72472942018-01-10 08:36:10 -080015 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"
Haibo Huang40a71912019-10-11 11:13:39 -070038#include "internal.h" /* for UNUSED_P only */
Elliott Hughes72472942018-01-10 08:36:10 -080039
40#if defined(_WIN32)
Haibo Huang40a71912019-10-11 11:13:39 -070041# define STRICT 1
42# define WIN32_LEAN_AND_MEAN 1
Elliott Hughes72472942018-01-10 08:36:10 -080043
Haibo Huang40a71912019-10-11 11:13:39 -070044# include <windows.h>
Haibo Huangd1a324a2020-10-28 22:19:36 -070045#endif /* defined(_WIN32) */
Elliott Hughes72472942018-01-10 08:36:10 -080046
47int
Haibo Huang40a71912019-10-11 11:13:39 -070048codepageMap(int cp, int *map) {
Haibo Huangd1a324a2020-10-28 22:19:36 -070049#if defined(_WIN32)
Elliott Hughes72472942018-01-10 08:36:10 -080050 int i;
51 CPINFO info;
Haibo Huang40a71912019-10-11 11:13:39 -070052 if (! GetCPInfo(cp, &info) || info.MaxCharSize > 2)
Elliott Hughes72472942018-01-10 08:36:10 -080053 return 0;
54 for (i = 0; i < 256; i++)
55 map[i] = -1;
56 if (info.MaxCharSize > 1) {
Haibo Huang40a71912019-10-11 11:13:39 -070057 for (i = 0; i < MAX_LEADBYTES; i += 2) {
Elliott Hughes72472942018-01-10 08:36:10 -080058 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++) {
Haibo Huang40a71912019-10-11 11:13:39 -070067 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 }
Elliott Hughes72472942018-01-10 08:36:10 -080075 }
76 return 1;
Haibo Huangd1a324a2020-10-28 22:19:36 -070077#else
78 UNUSED_P(cp);
79 UNUSED_P(map);
80 return 0;
81#endif
Elliott Hughes72472942018-01-10 08:36:10 -080082}
83
84int
Haibo Huang40a71912019-10-11 11:13:39 -070085codepageConvert(int cp, const char *p) {
Haibo Huangd1a324a2020-10-28 22:19:36 -070086#if defined(_WIN32)
Elliott Hughes72472942018-01-10 08:36:10 -080087 unsigned short c;
Haibo Huang40a71912019-10-11 11:13:39 -070088 if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, p, 2, &c,
89 1)
90 == 1)
Elliott Hughes72472942018-01-10 08:36:10 -080091 return c;
92 return -1;
Haibo Huangd1a324a2020-10-28 22:19:36 -070093#else
Haibo Huang40a71912019-10-11 11:13:39 -070094 UNUSED_P(cp);
95 UNUSED_P(p);
Elliott Hughes72472942018-01-10 08:36:10 -080096 return -1;
Haibo Huangd1a324a2020-10-28 22:19:36 -070097#endif
Elliott Hughes72472942018-01-10 08:36:10 -080098}