codepage.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Anatol Belski <ab@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef PHP_WIN32_CODEPAGE_H
  17. #define PHP_WIN32_CODEPAGE_H
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #ifdef PHP_EXPORTS
  22. # define PW32CP __declspec(dllexport)
  23. #else
  24. # define PW32CP __declspec(dllimport)
  25. #endif
  26. #define PHP_WIN32_CP_IGNORE_LEN (0)
  27. #define PHP_WIN32_CP_IGNORE_LEN_P ((size_t *)-1)
  28. struct php_win32_cp {
  29. DWORD id;
  30. DWORD to_w_fl;
  31. DWORD from_w_fl;
  32. DWORD char_size;
  33. char *name;
  34. char *enc;
  35. char *desc;
  36. };
  37. PW32CP BOOL php_win32_cp_use_unicode(void);
  38. PW32CP const struct php_win32_cp *php_win32_cp_do_setup(const char *);
  39. #define php_win32_cp_setup() php_win32_cp_do_setup(NULL)
  40. PW32CP const struct php_win32_cp *php_win32_cp_do_update(const char *);
  41. #define php_win32_cp_update() php_win32_cp_do_update(NULL)
  42. PW32CP const struct php_win32_cp *php_win32_cp_shutdown(void);
  43. PW32CP const struct php_win32_cp *php_win32_cp_get_current(void);
  44. PW32CP const struct php_win32_cp *php_win32_cp_get_orig(void);
  45. PW32CP const struct php_win32_cp *php_win32_cp_get_by_id(DWORD id);
  46. PW32CP const struct php_win32_cp *php_win32_cp_set_by_id(DWORD id);
  47. PW32CP const struct php_win32_cp *php_win32_cp_get_by_enc(const char *enc);
  48. PW32CP const struct php_win32_cp *php_win32_cp_cli_do_setup(DWORD);
  49. #define php_win32_cp_cli_setup() php_win32_cp_cli_do_setup(0)
  50. #define php_win32_cp_cli_update() php_win32_cp_cli_do_setup(0)
  51. PW32CP const struct php_win32_cp *php_win32_cp_cli_do_restore(DWORD);
  52. #define php_win32_cp_cli_restore() php_win32_cp_cli_do_restore(0)
  53. /* This API is binary safe and expects a \0 terminated input.
  54. The returned out is \0 terminated, but the length doesn't count \0. */
  55. PW32CP wchar_t *php_win32_cp_conv_to_w(DWORD in_cp, DWORD flags, const char* in, size_t in_len, size_t *out_len);
  56. PW32CP wchar_t *php_win32_cp_conv_utf8_to_w(const char* in, size_t in_len, size_t *out_len);
  57. #define php_win32_cp_utf8_to_w(in) php_win32_cp_conv_utf8_to_w(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
  58. PW32CP wchar_t *php_win32_cp_conv_cur_to_w(const char* in, size_t in_len, size_t *out_len);
  59. #define php_win32_cp_cur_to_w(in) php_win32_cp_conv_cur_to_w(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
  60. PW32CP wchar_t *php_win32_cp_conv_ascii_to_w(const char* in, size_t in_len, size_t *out_len);
  61. #define php_win32_cp_ascii_to_w(in) php_win32_cp_conv_ascii_to_w(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
  62. PW32CP char *php_win32_cp_conv_from_w(DWORD out_cp, DWORD flags, const wchar_t* in, size_t in_len, size_t *out_len);
  63. PW32CP char *php_win32_cp_conv_w_to_utf8(const wchar_t* in, size_t in_len, size_t *out_len);
  64. #define php_win32_cp_w_to_utf8(in) php_win32_cp_conv_w_to_utf8(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
  65. PW32CP char *php_win32_cp_conv_w_to_cur(const wchar_t* in, size_t in_len, size_t *out_len);
  66. #define php_win32_cp_w_to_cur(in) php_win32_cp_conv_w_to_cur(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
  67. PW32CP wchar_t *php_win32_cp_env_any_to_w(const char* env);
  68. /* This function tries to make the best guess to convert any
  69. given string to a wide char, also preferring the fastest code
  70. path to unicode. It returns NULL on fail. */
  71. __forceinline static wchar_t *php_win32_cp_conv_any_to_w(const char* in, size_t in_len, size_t *out_len)
  72. {/*{{{*/
  73. wchar_t *ret = NULL;
  74. if (php_win32_cp_use_unicode()) {
  75. /* First try the pure ascii conversion. This is the fastest way to do the
  76. thing. Only applicable if the source string is UTF-8 in general.
  77. While it could possibly be ok with European encodings, usage with
  78. Asian encodings can cause unintended side effects. Lookup the term
  79. "mojibake" if need more. */
  80. ret = php_win32_cp_conv_ascii_to_w(in, in_len, out_len);
  81. /* If that failed, try to convert to multibyte. */
  82. if (!ret) {
  83. ret = php_win32_cp_conv_utf8_to_w(in, in_len, out_len);
  84. /* Still need this fallback with regard to possible broken data
  85. in the existing scripts. Broken data might be hardcoded in
  86. the user scripts, as UTF-8 settings was de facto ignored in
  87. older PHP versions. The fallback can be removed later for
  88. the sake of purity, keep now for BC reasons. */
  89. if (!ret) {
  90. const struct php_win32_cp *acp = php_win32_cp_get_by_id(GetACP());
  91. if (acp) {
  92. ret = php_win32_cp_conv_to_w(acp->id, acp->to_w_fl, in, in_len, out_len);
  93. }
  94. }
  95. }
  96. } else {
  97. /* No unicode, convert from the current thread cp. */
  98. ret = php_win32_cp_conv_cur_to_w(in, in_len, out_len);
  99. }
  100. return ret;
  101. }/*}}}*/
  102. #define php_win32_cp_any_to_w(in) php_win32_cp_conv_any_to_w(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
  103. /* This function converts from unicode function output back to PHP. If
  104. the PHP's current charset is not compatible with unicode, so the currently
  105. configured CP will be used. */
  106. __forceinline static char *php_win32_cp_conv_w_to_any(const wchar_t* in, size_t in_len, size_t *out_len)
  107. {/*{{{*/
  108. return php_win32_cp_conv_w_to_cur(in, in_len, out_len);
  109. }/*}}}*/
  110. #define php_win32_cp_w_to_any(in) php_win32_cp_conv_w_to_any(in, PHP_WIN32_CP_IGNORE_LEN, PHP_WIN32_CP_IGNORE_LEN_P)
  111. #define PHP_WIN32_CP_W_TO_ANY_ARRAY(aw, aw_len, aa, aa_len) do { \
  112. int i; \
  113. aa_len = aw_len; \
  114. aa = (char **) malloc(aw_len * sizeof(char *)); \
  115. if (!aa) { \
  116. break; \
  117. } \
  118. for (i = 0; i < aw_len; i++) { \
  119. aa[i] = php_win32_cp_w_to_any(aw[i]); \
  120. } \
  121. } while (0);
  122. #define PHP_WIN32_CP_FREE_ARRAY(a, a_len) do { \
  123. int i; \
  124. for (i = 0; i < a_len; i++) { \
  125. free(a[i]); \
  126. } \
  127. free(a); \
  128. } while (0);
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif /* PHP_WIN32_CODEPAGE_H */