Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
3.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iasutil.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file implements assorted utility functions, etc.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 11/14/1997 Original version.
  16. // 08/11/1998 Major overhaul and consolidation of utility functions.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #include <ias.h>
  20. #include <iasutil.h>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // String functions.
  24. //
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // Duplicates a WSTR using new[].
  27. PWSTR
  28. WINAPI
  29. ias_wcsdup(PCWSTR str)
  30. {
  31. LPWSTR sz = NULL;
  32. if (str)
  33. {
  34. size_t len = wcslen(str) + 1;
  35. if (sz = new (std::nothrow) WCHAR[len])
  36. {
  37. memcpy(sz, str, len * sizeof(WCHAR));
  38. }
  39. }
  40. return sz;
  41. }
  42. // Duplicates a STR using CoTaskMemAlloc.
  43. PSTR
  44. WINAPI
  45. com_strdup(PCSTR str)
  46. {
  47. LPSTR sz = NULL;
  48. if (str)
  49. {
  50. size_t size = sizeof(CHAR) * (strlen(str) + 1);
  51. if (sz = (LPSTR)CoTaskMemAlloc(size))
  52. {
  53. memcpy(sz, str, size);
  54. }
  55. }
  56. return sz;
  57. }
  58. // Duplicates a WSTR using CoTaskMemAlloc.
  59. PWSTR
  60. WINAPI
  61. com_wcsdup(PCWSTR str)
  62. {
  63. LPWSTR sz = NULL;
  64. if (str)
  65. {
  66. size_t size = sizeof(WCHAR) * (wcslen(str) + 1);
  67. if (sz = (LPWSTR)CoTaskMemAlloc(size))
  68. {
  69. memcpy(sz, str, size);
  70. }
  71. }
  72. return sz;
  73. }
  74. // Compares two WSTR's allowing for null pointers.
  75. INT
  76. WINAPI
  77. ias_wcscmp(PCWSTR str1, PCWSTR str2)
  78. {
  79. if (str1 != NULL && str2 != NULL) return wcscmp(str1, str2);
  80. return str1 == str2 ? 0 : (str1 > str2 ? 1 : -1);
  81. }
  82. // Concatenate a null-terminated list of strings.
  83. LPWSTR
  84. WINAPIV
  85. ias_makewcs(LPCWSTR str1, ...)
  86. {
  87. size_t len = 0;
  88. //////////
  89. // Iterate through the arguments and calculate how much space we need.
  90. //////////
  91. va_list marker;
  92. va_start(marker, str1);
  93. LPCWSTR sz = str1;
  94. while (sz)
  95. {
  96. len += wcslen(sz);
  97. sz = va_arg(marker, LPCWSTR);
  98. }
  99. va_end(marker);
  100. // Add room for the null-terminator.
  101. ++len;
  102. //////////
  103. // Allocate memory to hold the concatentated string.
  104. //////////
  105. LPWSTR rv = new (std::nothrow) WCHAR[len];
  106. if (!rv) return NULL;
  107. // Initialize the string so wcscat will work.
  108. *rv = L'\0';
  109. //////////
  110. // Concatenate the strings.
  111. //////////
  112. va_start(marker, str1);
  113. sz = str1;
  114. while (sz)
  115. {
  116. wcscat(rv, sz);
  117. sz = va_arg(marker, LPCWSTR);
  118. }
  119. va_end(marker);
  120. return rv;
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////
  123. //
  124. // Functions to move integers to and from a buffer.
  125. //
  126. ///////////////////////////////////////////////////////////////////////////////
  127. VOID
  128. WINAPI
  129. IASInsertDWORD(
  130. PBYTE pBuffer,
  131. DWORD dwValue
  132. )
  133. {
  134. *pBuffer++ = (BYTE)((dwValue >> 24) & 0xFF);
  135. *pBuffer++ = (BYTE)((dwValue >> 16) & 0xFF);
  136. *pBuffer++ = (BYTE)((dwValue >> 8) & 0xFF);
  137. *pBuffer = (BYTE)((dwValue ) & 0xFF);
  138. }
  139. DWORD
  140. WINAPI
  141. IASExtractDWORD(
  142. CONST BYTE *pBuffer
  143. )
  144. {
  145. return (DWORD)(pBuffer[0] << 24) | (DWORD)(pBuffer[1] << 16) |
  146. (DWORD)(pBuffer[2] << 8) | (DWORD)(pBuffer[3] );
  147. }
  148. VOID
  149. WINAPI
  150. IASInsertWORD(
  151. PBYTE pBuffer,
  152. WORD wValue
  153. )
  154. {
  155. *pBuffer++ = (BYTE)((wValue >> 8) & 0xFF);
  156. *pBuffer = (BYTE)((wValue ) & 0xFF);
  157. }
  158. WORD
  159. WINAPI
  160. IASExtractWORD(
  161. CONST BYTE *pBuffer
  162. )
  163. {
  164. return (WORD)(pBuffer[0] << 8) | (WORD)(pBuffer[1]);
  165. }
  166. ///////////////////////////////////////////////////////////////////////////////
  167. //
  168. // Extensions to _com_error to handle Win32 errors.
  169. //
  170. ///////////////////////////////////////////////////////////////////////////////
  171. void __stdcall _w32_issue_error(DWORD errorCode) throw (_w32_error)
  172. {
  173. throw _w32_error(errorCode);
  174. }