codepage.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000 Clark Cooper <[email protected]>
  10. Copyright (c) 2002 Fred L. Drake, Jr. <[email protected]>
  11. Copyright (c) 2005-2006 Karl Waclawek <[email protected]>
  12. Copyright (c) 2016-2019 Sebastian Pipping <[email protected]>
  13. Copyright (c) 2019 David Loffredo <[email protected]>
  14. Licensed under the MIT license:
  15. Permission is hereby granted, free of charge, to any person obtaining
  16. a copy of this software and associated documentation files (the
  17. "Software"), to deal in the Software without restriction, including
  18. without limitation the rights to use, copy, modify, merge, publish,
  19. distribute, sublicense, and/or sell copies of the Software, and to permit
  20. persons to whom the Software is furnished to do so, subject to the
  21. following conditions:
  22. The above copyright notice and this permission notice shall be included
  23. in all copies or substantial portions of the Software.
  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. #include "codepage.h"
  33. #include "internal.h" /* for UNUSED_P only */
  34. #if defined(_WIN32)
  35. # define STRICT 1
  36. # define WIN32_LEAN_AND_MEAN 1
  37. # include <windows.h>
  38. #endif /* defined(_WIN32) */
  39. int
  40. codepageMap(int cp, int *map) {
  41. #if defined(_WIN32)
  42. int i;
  43. CPINFO info;
  44. if (! GetCPInfo(cp, &info) || info.MaxCharSize > 2)
  45. return 0;
  46. for (i = 0; i < 256; i++)
  47. map[i] = -1;
  48. if (info.MaxCharSize > 1) {
  49. for (i = 0; i < MAX_LEADBYTES; i += 2) {
  50. int j, lim;
  51. if (info.LeadByte[i] == 0 && info.LeadByte[i + 1] == 0)
  52. break;
  53. lim = info.LeadByte[i + 1];
  54. for (j = info.LeadByte[i]; j <= lim; j++)
  55. map[j] = -2;
  56. }
  57. }
  58. for (i = 0; i < 256; i++) {
  59. if (map[i] == -1) {
  60. char c = (char)i;
  61. unsigned short n;
  62. if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, &c, 1,
  63. &n, 1)
  64. == 1)
  65. map[i] = n;
  66. }
  67. }
  68. return 1;
  69. #else
  70. UNUSED_P(cp);
  71. UNUSED_P(map);
  72. return 0;
  73. #endif
  74. }
  75. int
  76. codepageConvert(int cp, const char *p) {
  77. #if defined(_WIN32)
  78. unsigned short c;
  79. if (MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, p, 2, &c,
  80. 1)
  81. == 1)
  82. return c;
  83. return -1;
  84. #else
  85. UNUSED_P(cp);
  86. UNUSED_P(p);
  87. return -1;
  88. #endif
  89. }