php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
com_olechar.c
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: Wez Furlong <wez@thebrainroom.com> |
14 | Harald Radi <h.radi@nme.at> |
15 +----------------------------------------------------------------------+
16 */
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include "php.h"
23#include "php_ini.h"
24#include "ext/standard/info.h"
25#include "php_com_dotnet.h"
27
28
29PHP_COM_DOTNET_API OLECHAR *php_com_string_to_olestring(const char *string, size_t string_len, int codepage)
30{
31 OLECHAR *olestring = NULL;
32 DWORD flags = codepage == CP_UTF8 ? 0 : MB_PRECOMPOSED | MB_ERR_INVALID_CHARS;
33 BOOL ok;
34
35 if (string_len == -1) {
36 /* determine required length for the buffer (includes NUL terminator) */
37 string_len = MultiByteToWideChar(codepage, flags, string, -1, NULL, 0);
38 } else {
39 /* allow room for NUL terminator */
40 string_len++;
41 }
42
43 if (string_len > 0) {
44 olestring = (OLECHAR*)safe_emalloc(string_len, sizeof(OLECHAR), 0);
45 /* XXX if that's a real multibyte string, olestring is obviously allocated excessively.
46 This should be fixed by reallocating the olestring, but as emalloc is used, that doesn't
47 matter much. */
48 ok = MultiByteToWideChar(codepage, flags, string, (int)string_len, olestring, (int)string_len);
49 if (ok > 0 && (size_t)ok < string_len) {
50 olestring[ok] = '\0';
51 }
52 } else {
53 ok = FALSE;
54 olestring = (OLECHAR*)emalloc(sizeof(OLECHAR));
55 *olestring = 0;
56 }
57
58 if (!ok) {
59 char *msg = php_win32_error_to_msg(GetLastError());
60
62 "Could not convert string to unicode: `%s'", msg);
63
65 }
66
67 return olestring;
68}
69
71{
72 zend_string *string;
73 uint32_t length = 0;
74
75 length = WideCharToMultiByte(codepage, 0, olestring, -1, NULL, 0, NULL, NULL);
76
77 if (length) {
78 /* We remove 1 from the length as it takes into account the terminating null byte
79 * which zend_string alloc already takes into consideration */
80 /* TODO Should use safe alloc? */
81 string = zend_string_alloc(length - 1, /* persistent */ false);
82 length = WideCharToMultiByte(codepage, 0, olestring, -1, ZSTR_VAL(string), length, NULL, NULL);
83 } else {
84 string = ZSTR_EMPTY_ALLOC();
85 }
86
87 /* Failure to determine length of WideChar */
88 if (length == 0) {
89 char *msg = php_win32_error_to_msg(GetLastError());
90
92 "Could not convert string from unicode: `%s'", msg);
93
95 }
96
97 return string;
98}
99
100BSTR php_com_string_to_bstr(zend_string *string, int codepage)
101{
102 BSTR bstr = NULL;
103 DWORD flags = codepage == CP_UTF8 ? 0 : MB_PRECOMPOSED | MB_ERR_INVALID_CHARS;
104 size_t mb_len = ZSTR_LEN(string);
105 int wc_len;
106
107 if ((wc_len = MultiByteToWideChar(codepage, flags, ZSTR_VAL(string), (int)mb_len + 1, NULL, 0)) <= 0) {
108 goto fail;
109 }
110 if ((bstr = SysAllocStringLen(NULL, (UINT)(wc_len - 1))) == NULL) {
111 goto fail;
112 }
113 if ((wc_len = MultiByteToWideChar(codepage, flags, ZSTR_VAL(string), (int)mb_len + 1, bstr, wc_len)) <= 0) {
114 goto fail;
115 }
116 return bstr;
117
118fail:
119 char *msg = php_win32_error_to_msg(GetLastError());
121 "Could not convert string to unicode: `%s'", msg);
122 LocalFree(msg);
123 SysFreeString(bstr);
124 return SysAllocString(L"");
125}
126
127zend_string *php_com_bstr_to_string(BSTR bstr, int codepage)
128{
129 zend_string *string = NULL;
130 UINT wc_len = SysStringLen(bstr);
131 int mb_len;
132
133 mb_len = WideCharToMultiByte(codepage, 0, bstr, wc_len + 1, NULL, 0, NULL, NULL);
134 if (mb_len > 0) {
135 string = zend_string_alloc(mb_len - 1, 0);
136 mb_len = WideCharToMultiByte(codepage, 0, bstr, wc_len + 1, ZSTR_VAL(string), mb_len, NULL, NULL);
137 }
138
139 if (mb_len <= 0) {
140 char *msg = php_win32_error_to_msg(GetLastError());
141
143 "Could not convert string from unicode: `%s'", msg);
144 LocalFree(msg);
145
146 if (string != NULL) {
147 zend_string_release(string);
148 }
149 string = ZSTR_EMPTY_ALLOC();
150 }
151
152 return string;
153}
const CP_UTF8
PHP_COM_DOTNET_API zend_string * php_com_olestring_to_string(OLECHAR *olestring, int codepage)
Definition com_olechar.c:70
zend_string * php_com_bstr_to_string(BSTR bstr, int codepage)
PHP_COM_DOTNET_API OLECHAR * php_com_string_to_olestring(const char *string, size_t string_len, int codepage)
Definition com_olechar.c:29
BSTR php_com_string_to_bstr(zend_string *string, int codepage)
#define DWORD
Definition exif.c:1762
#define FALSE
Definition gd_gd.c:8
#define NULL
Definition gdcache.h:45
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
int BOOL
#define PHP_COM_DOTNET_API
char * msg
Definition phpdbg.h:289
bool fail
Definition session.c:1065
PHP_WINUTIL_API char * php_win32_error_to_msg(HRESULT error)
Definition winutil.c:25
PHP_WINUTIL_API void php_win32_error_msg_free(char *msg)
Definition winutil.c:50
#define safe_emalloc(nmemb, size, offset)
Definition zend_alloc.h:154
#define emalloc(size)
Definition zend_alloc.h:151
#define E_WARNING
Definition zend_errors.h:24
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_EMPTY_ALLOC()
#define ZSTR_LEN(zstr)
Definition zend_string.h:69