xref: /PHP-5.3/ext/com_dotnet/com_olechar.c (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 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,uint string_len,int codepage TSRMLS_DC)33 PHP_COM_DOTNET_API OLECHAR *php_com_string_to_olestring(char *string, uint string_len, int codepage TSRMLS_DC)
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 		ok = MultiByteToWideChar(codepage, flags, string, string_len, olestring, string_len);
50 	} else {
51 		ok = FALSE;
52 		olestring = (OLECHAR*)emalloc(sizeof(OLECHAR));
53 		*olestring = 0;
54 	}
55 
56 	if (!ok) {
57 		char *msg = php_win_err(GetLastError());
58 
59 		php_error_docref(NULL TSRMLS_CC, E_WARNING,
60 			"Could not convert string to unicode: `%s'", msg);
61 
62 		LocalFree(msg);
63 	}
64 
65 	return olestring;
66 }
67 
php_com_olestring_to_string(OLECHAR * olestring,uint * string_len,int codepage TSRMLS_DC)68 PHP_COM_DOTNET_API char *php_com_olestring_to_string(OLECHAR *olestring, uint *string_len, int codepage TSRMLS_DC)
69 {
70 	char *string;
71 	uint length = 0;
72 	BOOL ok;
73 
74 	length = WideCharToMultiByte(codepage, 0, olestring, -1, NULL, 0, NULL, NULL);
75 
76 	if (length) {
77 		string = (char*)safe_emalloc(length, sizeof(char), 0);
78 		length = WideCharToMultiByte(codepage, 0, olestring, -1, string, length, NULL, NULL);
79 		ok = length > 0;
80 	} else {
81 		string = (char*)emalloc(sizeof(char));
82 		*string = '\0';
83 		ok = FALSE;
84 		length = 0;
85 	}
86 
87 	if (!ok) {
88 		char *msg = php_win_err(GetLastError());
89 
90 		php_error_docref(NULL TSRMLS_CC, E_WARNING,
91 			"Could not convert string from unicode: `%s'", msg);
92 
93 		LocalFree(msg);
94 	}
95 
96 	if (string_len) {
97 		*string_len = length-1;
98 	}
99 
100 	return string;
101 }
102