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.

287 lines
7.1 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1992 - 1996
  6. //
  7. // File: ntcalls.cxx
  8. //
  9. // Contents: Code for rtl support on Win95
  10. //
  11. //
  12. // History: 01-April-1997 Created ChandanS
  13. //
  14. //------------------------------------------------------------------------
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <windows.h>
  19. #ifndef SECURITY_WIN32
  20. #define SECURITY_WIN32
  21. #endif
  22. #include <security.h>
  23. NTSTATUS
  24. MyRtlUnicodeStringToOemString(
  25. OUT POEM_STRING DestinationString,
  26. IN PUNICODE_STRING SourceString,
  27. IN BOOLEAN AllocateDestinationString
  28. )
  29. /*++
  30. Routine Description:
  31. This functions converts the specified unicode source string into an
  32. oem string. The translation is done with respect to the OEM code
  33. page (OCP).
  34. Arguments:
  35. DestinationString - Returns an oem string that is equivalent to the
  36. unicode source string. If the translation can not be done,
  37. an error is returned. The maximum length field is only set if
  38. AllocateDestinationString is TRUE.
  39. SourceString - Supplies the unicode source string that is to be
  40. converted to oem.
  41. AllocateDestinationString - Supplies a flag that controls whether or
  42. not this API allocates the buffer space for the destination
  43. string. If it does, then the buffer must be deallocated using
  44. RtlFreeAnsiString (note that only storage for
  45. DestinationString->Buffer is allocated by this API).
  46. Return Value:
  47. SUCCESS - The conversion was successful
  48. !SUCCESS - The operation failed. No storage was allocated and no
  49. conversion was done. None.
  50. --*/
  51. {
  52. ULONG OemLength;
  53. ULONG Index;
  54. NTSTATUS st = STATUS_SUCCESS;
  55. // Get the size required. Being MBCS safe
  56. Index = WideCharToMultiByte(
  57. CP_OEMCP,
  58. 0,
  59. SourceString->Buffer,
  60. SourceString->Length / sizeof (WCHAR),
  61. DestinationString->Buffer,
  62. 0,
  63. NULL,
  64. NULL
  65. );
  66. OemLength = Index + 1; // Index is not necesarily SourceString->Length/2
  67. if ( OemLength > MAXUSHORT ) {
  68. return STATUS_INVALID_PARAMETER_2;
  69. }
  70. DestinationString->Length = (USHORT)(OemLength - 1);
  71. if ( AllocateDestinationString ) {
  72. DestinationString->MaximumLength = (USHORT)OemLength;
  73. DestinationString->Buffer = (LPSTR)LocalAlloc(0, OemLength);
  74. if ( !DestinationString->Buffer ) {
  75. return STATUS_NO_MEMORY;
  76. }
  77. }
  78. else {
  79. if ( DestinationString->Length >= DestinationString->MaximumLength ) {
  80. return STATUS_BUFFER_OVERFLOW;
  81. }
  82. }
  83. if (Index != 0) // If we don't have a string to translate, don't bother
  84. {
  85. Index = WideCharToMultiByte(
  86. CP_OEMCP,
  87. 0,
  88. SourceString->Buffer,
  89. SourceString->Length / sizeof (WCHAR),
  90. DestinationString->Buffer,
  91. DestinationString->MaximumLength,
  92. NULL,
  93. NULL
  94. );
  95. if (Index == 0)
  96. { // There was a problem. Save away the error code to return & cleanup
  97. st = GetLastError();
  98. if ( AllocateDestinationString ) {
  99. LocalFree(DestinationString->Buffer);
  100. DestinationString->Buffer = NULL;
  101. DestinationString->MaximumLength = 0;
  102. }
  103. DestinationString->Length = 0;
  104. }
  105. }
  106. if (DestinationString->Buffer)
  107. {
  108. DestinationString->Buffer[Index] = '\0';
  109. }
  110. return st;
  111. }
  112. NTSTATUS
  113. MyRtlUpcaseUnicodeStringToOemString(
  114. OUT POEM_STRING DestinationString,
  115. IN PUNICODE_STRING SourceString,
  116. IN BOOLEAN AllocateDestinationString
  117. )
  118. /*++
  119. Routine Description:
  120. This function converts the unicode string to an oem string and then
  121. upper cases it according to the locale specified at install time.
  122. Arguments:
  123. DestinationString - Returns an oem string that is equivalent to the
  124. unicode source string. The maximum length field is only set if
  125. AllocateDestinationString is TRUE.
  126. SourceString - Supplies the unicode source string that is to be
  127. converted to oem.
  128. AllocateDestinationString - Supplies a flag that controls whether or
  129. not this API allocates the buffer space for the destination
  130. string. If it does, then the buffer must be deallocated using
  131. RtlFreeAnsiString (note that only storage for
  132. DestinationString->Buffer is allocated by this API).
  133. Return Value:
  134. SUCCESS - The conversion was successful
  135. !SUCCESS - The operation failed. No storage was allocated and no
  136. conversion was done. None.
  137. --*/
  138. {
  139. ULONG OemLength;
  140. NTSTATUS st = STATUS_SUCCESS;
  141. // On NT, this function is implemented by upper casing the unicode
  142. // string and then converting it to an oem string.
  143. // On Win95, there does not appear to be a way to uppercase unicode
  144. // strings taking the locale into account. So, our best bet here
  145. // is to convert to an oem string and then do an inplace locale
  146. // dependent upper case.
  147. st = MyRtlUnicodeStringToOemString(DestinationString,
  148. SourceString,
  149. AllocateDestinationString);
  150. if (st == STATUS_SUCCESS)
  151. {
  152. // we may not be able to upper case all chars
  153. OemLength = CharUpperBuff (DestinationString->Buffer,
  154. DestinationString->Length);
  155. }
  156. return st;
  157. }
  158. VOID
  159. MyRtlFreeOemString(
  160. IN OUT POEM_STRING OemString
  161. )
  162. /*++
  163. Routine Description:
  164. This API is used to free storage allocated by
  165. RtlUnicodeStringToOemString. Note that only OemString->Buffer
  166. is free'd by this routine.
  167. Arguments:
  168. OemString - Supplies the address of the oem string whose buffer
  169. was previously allocated by RtlUnicodeStringToOemString.
  170. Return Value:
  171. None.
  172. --*/
  173. {
  174. if (OemString->Buffer) {
  175. LocalFree(OemString->Buffer);
  176. memset( OemString, 0, sizeof( *OemString ) );
  177. }
  178. }
  179. //
  180. // Inline functions to convert between FILETIME and TimeStamp
  181. //
  182. #pragma warning( disable : 4035) // Don't complain about no return
  183. TimeStamp __inline
  184. FileTimeToTimeStamp(
  185. const FILETIME *pft)
  186. {
  187. _asm {
  188. mov edx, pft
  189. mov eax, [edx].dwLowDateTime
  190. mov edx, [edx].dwHighDateTime
  191. }
  192. }
  193. #pragma warning( default : 4035) // Reenable warning
  194. NTSTATUS
  195. MyNtQuerySystemTime (
  196. OUT PTimeStamp SystemTimeStamp
  197. )
  198. /*++
  199. Routine Description:
  200. This routine returns the current system time (UTC), as a timestamp
  201. (a 64-bit unsigned integer, in 100-nanosecond increments).
  202. Arguments:
  203. None.
  204. Return Value:
  205. The current system time.
  206. --*/
  207. {
  208. SYSTEMTIME SystemTime;
  209. FILETIME FileTime;
  210. GetSystemTime(&SystemTime);
  211. SystemTimeToFileTime(&SystemTime, &FileTime);
  212. *SystemTimeStamp = FileTimeToTimeStamp(&FileTime);
  213. return STATUS_SUCCESS; // WIN32_CHICAGO do something useful here
  214. }