com_olechar.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* $Id$ */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "ext/standard/info.h"
  26. #include "php_com_dotnet.h"
  27. #include "php_com_dotnet_internal.h"
  28. PHP_COM_DOTNET_API OLECHAR *php_com_string_to_olestring(char *string, uint string_len, int codepage TSRMLS_DC)
  29. {
  30. OLECHAR *olestring = NULL;
  31. DWORD flags = codepage == CP_UTF8 ? 0 : MB_PRECOMPOSED | MB_ERR_INVALID_CHARS;
  32. BOOL ok;
  33. if (string_len == -1) {
  34. /* determine required length for the buffer (includes NUL terminator) */
  35. string_len = MultiByteToWideChar(codepage, flags, string, -1, NULL, 0);
  36. } else {
  37. /* allow room for NUL terminator */
  38. string_len++;
  39. }
  40. if (string_len > 0) {
  41. olestring = (OLECHAR*)safe_emalloc(string_len, sizeof(OLECHAR), 0);
  42. /* XXX if that's a real multibyte string, olestring is obviously allocated excessively.
  43. This should be fixed by reallocating the olestring, but as emalloc is used, that doesn't
  44. matter much. */
  45. ok = MultiByteToWideChar(codepage, flags, string, string_len, olestring, string_len);
  46. if (ok > 0 && ok < string_len) {
  47. olestring[ok] = '\0';
  48. }
  49. } else {
  50. ok = FALSE;
  51. olestring = (OLECHAR*)emalloc(sizeof(OLECHAR));
  52. *olestring = 0;
  53. }
  54. if (!ok) {
  55. char *msg = php_win32_error_to_msg(GetLastError());
  56. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  57. "Could not convert string to unicode: `%s'", msg);
  58. LocalFree(msg);
  59. }
  60. return olestring;
  61. }
  62. PHP_COM_DOTNET_API char *php_com_olestring_to_string(OLECHAR *olestring, uint *string_len, int codepage TSRMLS_DC)
  63. {
  64. char *string;
  65. uint length = 0;
  66. BOOL ok;
  67. length = WideCharToMultiByte(codepage, 0, olestring, -1, NULL, 0, NULL, NULL);
  68. if (length) {
  69. string = (char*)safe_emalloc(length, sizeof(char), 0);
  70. length = WideCharToMultiByte(codepage, 0, olestring, -1, string, length, NULL, NULL);
  71. ok = length > 0;
  72. } else {
  73. string = (char*)emalloc(sizeof(char));
  74. *string = '\0';
  75. ok = FALSE;
  76. length = 0;
  77. }
  78. if (!ok) {
  79. char *msg = php_win32_error_to_msg(GetLastError());
  80. php_error_docref(NULL TSRMLS_CC, E_WARNING,
  81. "Could not convert string from unicode: `%s'", msg);
  82. LocalFree(msg);
  83. }
  84. if (string_len) {
  85. *string_len = length-1;
  86. }
  87. return string;
  88. }