php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
codepage.h
Go to the documentation of this file.
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
17#ifndef PHP_WIN32_CODEPAGE_H
18#define PHP_WIN32_CODEPAGE_H
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#ifdef PHP_EXPORTS
25# define PW32CP __declspec(dllexport)
26#else
27# define PW32CP __declspec(dllimport)
28#endif
29
30#define PHP_WIN32_CP_IGNORE_LEN (0)
31#define PHP_WIN32_CP_IGNORE_LEN_P ((size_t *)-1)
32
42
44PW32CP const struct php_win32_cp *php_win32_cp_do_setup(const char *);
45#define php_win32_cp_setup() php_win32_cp_do_setup(NULL)
46PW32CP const struct php_win32_cp *php_win32_cp_do_update(const char *);
47#define php_win32_cp_update() php_win32_cp_do_update(NULL)
48PW32CP const struct php_win32_cp *php_win32_cp_shutdown(void);
50PW32CP const struct php_win32_cp *php_win32_cp_get_orig(void);
53PW32CP const struct php_win32_cp *php_win32_cp_get_by_enc(const char *enc);
55#define php_win32_cp_cli_setup() php_win32_cp_cli_do_setup(0)
56#define php_win32_cp_cli_update() php_win32_cp_cli_do_setup(0)
58#define php_win32_cp_cli_restore() php_win32_cp_cli_do_restore(0)
59
60/* This API is binary safe and expects a \0 terminated input.
61 The returned out is \0 terminated, but the length doesn't count \0. */
62PW32CP wchar_t *php_win32_cp_conv_to_w(DWORD in_cp, DWORD flags, const char* in, size_t in_len, size_t *out_len);
63PW32CP wchar_t *php_win32_cp_conv_utf8_to_w(const char* in, size_t in_len, size_t *out_len);
64#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)
65PW32CP wchar_t *php_win32_cp_conv_cur_to_w(const char* in, size_t in_len, size_t *out_len);
66#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)
67PW32CP wchar_t *php_win32_cp_conv_ascii_to_w(const char* in, size_t in_len, size_t *out_len);
68#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)
69PW32CP char *php_win32_cp_conv_from_w(DWORD out_cp, DWORD flags, const wchar_t* in, size_t in_len, size_t *out_len);
70PW32CP char *php_win32_cp_conv_w_to_utf8(const wchar_t* in, size_t in_len, size_t *out_len);
71#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)
72PW32CP char *php_win32_cp_conv_w_to_cur(const wchar_t* in, size_t in_len, size_t *out_len);
73#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)
74PW32CP wchar_t *php_win32_cp_env_any_to_w(const char* env);
75
76/* This function tries to make the best guess to convert any
77 given string to a wide char, also preferring the fastest code
78 path to unicode. It returns NULL on fail. */
79__forceinline static wchar_t *php_win32_cp_conv_any_to_w(const char* in, size_t in_len, size_t *out_len)
80{/*{{{*/
81 wchar_t *ret = NULL;
82
84 /* First try the pure ascii conversion. This is the fastest way to do the
85 thing. Only applicable if the source string is UTF-8 in general.
86 While it could possibly be ok with European encodings, usage with
87 Asian encodings can cause unintended side effects. Lookup the term
88 "mojibake" if need more. */
89 ret = php_win32_cp_conv_ascii_to_w(in, in_len, out_len);
90
91 /* If that failed, try to convert to multibyte. */
92 if (!ret) {
93 ret = php_win32_cp_conv_utf8_to_w(in, in_len, out_len);
94
95 /* Still need this fallback with regard to possible broken data
96 in the existing scripts. Broken data might be hardcoded in
97 the user scripts, as UTF-8 settings was de facto ignored in
98 older PHP versions. The fallback can be removed later for
99 the sake of purity, keep now for BC reasons. */
100 if (!ret) {
101 const struct php_win32_cp *acp = php_win32_cp_get_by_id(GetACP());
102
103 if (acp) {
104 ret = php_win32_cp_conv_to_w(acp->id, acp->to_w_fl, in, in_len, out_len);
105 }
106 }
107 }
108 } else {
109 /* No unicode, convert from the current thread cp. */
110 ret = php_win32_cp_conv_cur_to_w(in, in_len, out_len);
111 }
112
113 return ret;
114}/*}}}*/
115#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)
116
117/* This function converts from unicode function output back to PHP. If
118 the PHP's current charset is not compatible with unicode, so the currently
119 configured CP will be used. */
120__forceinline static char *php_win32_cp_conv_w_to_any(const wchar_t* in, size_t in_len, size_t *out_len)
121{/*{{{*/
122 return php_win32_cp_conv_w_to_cur(in, in_len, out_len);
123}/*}}}*/
124#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)
125
126#define PHP_WIN32_CP_W_TO_ANY_ARRAY(aw, aw_len, aa, aa_len) do { \
127 int i; \
128 aa_len = aw_len; \
129 aa = (char **) malloc(aw_len * sizeof(char *)); \
130 if (!aa) { \
131 break; \
132 } \
133 for (i = 0; i < aw_len; i++) { \
134 aa[i] = php_win32_cp_w_to_any(aw[i]); \
135 } \
136} while (0);
137
138
139#define PHP_WIN32_CP_FREE_ARRAY(a, a_len) do { \
140 int i; \
141 for (i = 0; i < a_len; i++) { \
142 free(a[i]); \
143 } \
144 free(a); \
145} while (0);
146
147#ifdef __cplusplus
148}
149#endif
150
151#endif /* PHP_WIN32_CODEPAGE_H */
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)
Definition codepage.c:96
PW32CP const struct php_win32_cp * php_win32_cp_do_setup(const char *)
Definition codepage.c:497
PW32CP BOOL php_win32_cp_use_unicode(void)
Definition codepage.c:423
PW32CP const struct php_win32_cp * php_win32_cp_shutdown(void)
Definition codepage.c:540
PW32CP wchar_t * php_win32_cp_conv_ascii_to_w(const char *in, size_t in_len, size_t *out_len)
Definition codepage.c:106
PW32CP wchar_t * php_win32_cp_conv_cur_to_w(const char *in, size_t in_len, size_t *out_len)
Definition codepage.c:87
PW32CP const struct php_win32_cp * php_win32_cp_get_current(void)
Definition codepage.c:328
PW32CP const struct php_win32_cp * php_win32_cp_set_by_id(DWORD id)
Definition codepage.c:407
PW32CP char * php_win32_cp_conv_w_to_cur(const wchar_t *in, size_t in_len, size_t *out_len)
Definition codepage.c:294
PW32CP const struct php_win32_cp * php_win32_cp_get_orig(void)
Definition codepage.c:333
PW32CP wchar_t * php_win32_cp_conv_utf8_to_w(const char *in, size_t in_len, size_t *out_len)
Definition codepage.c:82
#define PW32CP
Definition codepage.h:27
PW32CP const struct php_win32_cp * php_win32_cp_cli_do_restore(DWORD)
Definition codepage.c:571
PW32CP const struct php_win32_cp * php_win32_cp_do_update(const char *)
Definition codepage.c:526
PW32CP const struct php_win32_cp * php_win32_cp_get_by_enc(const char *enc)
Definition codepage.c:364
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)
Definition codepage.c:303
PW32CP const struct php_win32_cp * php_win32_cp_get_by_id(DWORD id)
Definition codepage.c:338
PW32CP const struct php_win32_cp * php_win32_cp_cli_do_setup(DWORD)
Definition codepage.c:546
PW32CP wchar_t * php_win32_cp_env_any_to_w(const char *env)
Definition codepage.c:428
PW32CP char * php_win32_cp_conv_w_to_utf8(const wchar_t *in, size_t in_len, size_t *out_len)
Definition codepage.c:289
#define DWORD
Definition exif.c:1762
#define NULL
Definition gdcache.h:45
int BOOL
char * name
Definition codepage.h:38
DWORD from_w_fl
Definition codepage.h:36
DWORD char_size
Definition codepage.h:37
char * desc
Definition codepage.h:40
DWORD to_w_fl
Definition codepage.h:35
char * enc
Definition codepage.h:39
zval * ret