Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | __ __ _ |
| 3 | ___\ \/ /_ __ __ _| |_ |
| 4 | / _ \\ /| '_ \ / _` | __| |
| 5 | | __// \| |_) | (_| | |_ |
| 6 | \___/_/\_\ .__/ \__,_|\__| |
| 7 | |_| XML parser |
| 8 | |
| 9 | Copyright (c) 1997-2000 Thai Open Source Software Center Ltd |
Elliott Hughes | 33bf975 | 2021-08-10 17:33:34 -0700 | [diff] [blame] | 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> |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 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" |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 38 | #include "internal.h" /* for UNUSED_P only */ |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 39 | |
| 40 | #if defined(_WIN32) |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 41 | # define STRICT 1 |
| 42 | # define WIN32_LEAN_AND_MEAN 1 |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 43 | |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 44 | # include <windows.h> |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 45 | #endif /* defined(_WIN32) */ |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 46 | |
| 47 | int |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 48 | codepageMap(int cp, int *map) { |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 49 | #if defined(_WIN32) |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 50 | int i; |
| 51 | CPINFO info; |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 52 | if (! GetCPInfo(cp, &info) || info.MaxCharSize > 2) |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 53 | return 0; |
| 54 | for (i = 0; i < 256; i++) |
| 55 | map[i] = -1; |
| 56 | if (info.MaxCharSize > 1) { |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 57 | for (i = 0; i < MAX_LEADBYTES; i += 2) { |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 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++) { |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 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 | } |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 75 | } |
| 76 | return 1; |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 77 | #else |
| 78 | UNUSED_P(cp); |
| 79 | UNUSED_P(map); |
| 80 | return 0; |
| 81 | #endif |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | int |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 85 | codepageConvert(int cp, const char *p) { |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 86 | #if defined(_WIN32) |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 87 | unsigned short c; |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 88 | if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, p, 2, &c, |
| 89 | 1) |
| 90 | == 1) |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 91 | return c; |
| 92 | return -1; |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 93 | #else |
Haibo Huang | 40a7191 | 2019-10-11 11:13:39 -0700 | [diff] [blame] | 94 | UNUSED_P(cp); |
| 95 | UNUSED_P(p); |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 96 | return -1; |
Haibo Huang | d1a324a | 2020-10-28 22:19:36 -0700 | [diff] [blame] | 97 | #endif |
Elliott Hughes | 7247294 | 2018-01-10 08:36:10 -0800 | [diff] [blame] | 98 | } |