1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Wez Furlong <wez@thebrainroom.com> |
16 | Harald Radi <h.radi@nme.at> |
17 +----------------------------------------------------------------------+
18 */
19
20 /* $Id$ */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "php.h"
27 #include "php_ini.h"
28 #include "ext/standard/info.h"
29 #include "php_com_dotnet.h"
30 #include "php_com_dotnet_internal.h"
31
32
php_com_string_to_olestring(char * string,size_t string_len,int codepage)33 PHP_COM_DOTNET_API OLECHAR *php_com_string_to_olestring(char *string, size_t string_len, int codepage)
34 {
35 OLECHAR *olestring = NULL;
36 DWORD flags = codepage == CP_UTF8 ? 0 : MB_PRECOMPOSED | MB_ERR_INVALID_CHARS;
37 BOOL ok;
38
39 if (string_len == -1) {
40 /* determine required length for the buffer (includes NUL terminator) */
41 string_len = MultiByteToWideChar(codepage, flags, string, -1, NULL, 0);
42 } else {
43 /* allow room for NUL terminator */
44 string_len++;
45 }
46
47 if (string_len > 0) {
48 olestring = (OLECHAR*)safe_emalloc(string_len, sizeof(OLECHAR), 0);
49 /* XXX if that's a real multibyte string, olestring is obviously allocated excessively.
50 This should be fixed by reallocating the olestring, but as emalloc is used, that doesn't
51 matter much. */
52 ok = MultiByteToWideChar(codepage, flags, string, (int)string_len, olestring, (int)string_len);
53 if (ok > 0 && ok < string_len) {
54 olestring[ok] = '\0';
55 }
56 } else {
57 ok = FALSE;
58 olestring = (OLECHAR*)emalloc(sizeof(OLECHAR));
59 *olestring = 0;
60 }
61
62 if (!ok) {
63 char *msg = php_win32_error_to_msg(GetLastError());
64
65 php_error_docref(NULL, E_WARNING,
66 "Could not convert string to unicode: `%s'", msg);
67
68 LocalFree(msg);
69 }
70
71 return olestring;
72 }
73
php_com_olestring_to_string(OLECHAR * olestring,size_t * string_len,int codepage)74 PHP_COM_DOTNET_API char *php_com_olestring_to_string(OLECHAR *olestring, size_t *string_len, int codepage)
75 {
76 char *string;
77 uint32_t length = 0;
78 BOOL ok;
79
80 length = WideCharToMultiByte(codepage, 0, olestring, -1, NULL, 0, NULL, NULL);
81
82 if (length) {
83 string = (char*)safe_emalloc(length, sizeof(char), 0);
84 length = WideCharToMultiByte(codepage, 0, olestring, -1, string, length, NULL, NULL);
85 ok = length > 0;
86 } else {
87 string = (char*)emalloc(sizeof(char));
88 *string = '\0';
89 ok = FALSE;
90 length = 0;
91 }
92
93 if (!ok) {
94 char *msg = php_win32_error_to_msg(GetLastError());
95
96 php_error_docref(NULL, E_WARNING,
97 "Could not convert string from unicode: `%s'", msg);
98
99 LocalFree(msg);
100 }
101
102 if (string_len) {
103 *string_len = length-1;
104 }
105
106 return string;
107 }
108