Leaked source code of windows server 2003
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.

266 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 1989-1997 Microsoft Corporation
  3. Module Name:
  4. LongStream.cxx
  5. Abstract:
  6. Create some long stream names
  7. --*/
  8. extern "C" {
  9. #include <nt.h>
  10. #include <ntioapi.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. }
  14. #include <windows.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #define DEFAULT_DATA_STREAM "::$DATA"
  18. //
  19. // Simple wrapper for NtCreateFile
  20. //
  21. NTSTATUS
  22. OpenObject (
  23. WCHAR const *pwszFile,
  24. HANDLE RelatedObject,
  25. ULONG CreateOptions,
  26. ULONG DesiredAccess,
  27. ULONG ShareAccess,
  28. ULONG CreateDisposition,
  29. HANDLE *ph)
  30. {
  31. NTSTATUS Status;
  32. OBJECT_ATTRIBUTES oa;
  33. UNICODE_STRING str;
  34. IO_STATUS_BLOCK isb;
  35. if (RelatedObject == NULL) {
  36. RtlDosPathNameToNtPathName_U(pwszFile, &str, NULL, NULL);
  37. } else {
  38. RtlInitUnicodeString(&str, pwszFile);
  39. }
  40. InitializeObjectAttributes(
  41. &oa,
  42. &str,
  43. OBJ_CASE_INSENSITIVE,
  44. RelatedObject,
  45. NULL);
  46. Status = NtCreateFile(
  47. ph,
  48. DesiredAccess | SYNCHRONIZE,
  49. &oa,
  50. &isb,
  51. NULL, // pallocationsize (none!)
  52. FILE_ATTRIBUTE_NORMAL,
  53. ShareAccess,
  54. CreateDisposition,
  55. CreateOptions,
  56. NULL, // EA buffer (none!)
  57. 0);
  58. RtlFreeHeap(RtlProcessHeap(), 0, str.Buffer);
  59. return(Status);
  60. }
  61. void
  62. SzToWsz (
  63. OUT WCHAR *Unicode,
  64. IN char *Ansi
  65. )
  66. {
  67. while (*Unicode++ = *Ansi++)
  68. ;
  69. }
  70. void DumpStreams(
  71. char *FileName
  72. )
  73. {
  74. WCHAR UnicodeFileName[2*MAX_PATH];
  75. NTSTATUS Status;
  76. HANDLE FileHandle;
  77. printf( "%s:\n", FileName );
  78. //
  79. // Make the filename into something unicode
  80. //
  81. SzToWsz( UnicodeFileName, FileName );
  82. //
  83. // Create the base file
  84. //
  85. Status = OpenObject( UnicodeFileName,
  86. NULL,
  87. FILE_SYNCHRONOUS_IO_NONALERT,
  88. FILE_READ_DATA,
  89. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  90. FILE_OPEN_IF,
  91. &FileHandle );
  92. if (!NT_SUCCESS( Status )) {
  93. printf( "unable to open filename - %x\n", Status );
  94. return;
  95. }
  96. //
  97. // Dump out all stream names
  98. //
  99. ULONG Length = 32768;
  100. PBYTE BigBuffer = new BYTE[Length];
  101. IO_STATUS_BLOCK IoStatusBlock;
  102. Status = NtQueryInformationFile( FileHandle, &IoStatusBlock, BigBuffer, Length, FileStreamInformation );
  103. if (!NT_SUCCESS( Status )) {
  104. printf( "Can't get stream names - %x\n", Status );
  105. } else {
  106. PFILE_STREAM_INFORMATION FileStreamInformation = (PFILE_STREAM_INFORMATION) BigBuffer;
  107. while ((ULONG)((PBYTE)FileStreamInformation - BigBuffer) < IoStatusBlock.Information) {
  108. printf( "%16I64x %16I64x %.*ws\n",
  109. FileStreamInformation->StreamSize,
  110. FileStreamInformation->StreamAllocationSize,
  111. FileStreamInformation->StreamNameLength,
  112. FileStreamInformation->StreamName
  113. );
  114. if (FileStreamInformation->NextEntryOffset == 0) {
  115. break;
  116. }
  117. FileStreamInformation = (PFILE_STREAM_INFORMATION)((PBYTE)FileStreamInformation + FileStreamInformation->NextEntryOffset);
  118. }
  119. }
  120. NtClose( FileHandle );
  121. }
  122. void
  123. LongStream (
  124. char *FileName
  125. )
  126. {
  127. WCHAR UnicodeFileName[2*MAX_PATH];
  128. NTSTATUS Status;
  129. HANDLE FileHandle;
  130. //
  131. // Make the filename into something unicode
  132. //
  133. SzToWsz( UnicodeFileName, FileName );
  134. //
  135. // Create the base file
  136. //
  137. Status = OpenObject( UnicodeFileName,
  138. NULL,
  139. FILE_SYNCHRONOUS_IO_NONALERT,
  140. FILE_READ_DATA | FILE_WRITE_DATA,
  141. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  142. FILE_OPEN_IF,
  143. &FileHandle );
  144. if (!NT_SUCCESS( Status )) {
  145. printf( "unable to open filename - %x\n", Status );
  146. return;
  147. }
  148. //
  149. // Binary loop creating stream names trying to find the max length allowed
  150. //
  151. ULONG StartLength = 1024;
  152. ULONG GrowLength = 1024;
  153. while (GrowLength != 0) {
  154. PWCHAR UnicodeStreamName = new WCHAR[StartLength + 2];
  155. HANDLE StreamHandle;
  156. UnicodeStreamName[0] = L':';
  157. for (ULONG i = 0; i < StartLength; i++) {
  158. UnicodeStreamName[i + 1] = L'Z';
  159. }
  160. UnicodeStreamName[StartLength + 1] = L'\0';
  161. Status = OpenObject( UnicodeStreamName,
  162. FileHandle,
  163. FILE_SYNCHRONOUS_IO_NONALERT,
  164. FILE_READ_DATA | FILE_WRITE_DATA,
  165. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  166. FILE_OPEN_IF,
  167. &StreamHandle );
  168. delete [] UnicodeStreamName;
  169. if (!NT_SUCCESS( Status )) {
  170. printf( "Failure at %d\n", StartLength );
  171. StartLength -= GrowLength;
  172. GrowLength /= 2;
  173. StartLength += GrowLength;
  174. } else {
  175. printf( "Success at %d\n", StartLength );
  176. GrowLength /= 2;
  177. StartLength += GrowLength;
  178. }
  179. }
  180. NtClose( FileHandle );
  181. }
  182. int __cdecl
  183. main (
  184. int argc,
  185. char **argv)
  186. {
  187. argc--;
  188. argv++;
  189. if (argc == 0) {
  190. return 0;
  191. }
  192. if (!_stricmp( "-d", argv[0])) {
  193. DbgPrint( "--------------------------------------------\n" );
  194. while (--argc != 0) {
  195. DumpStreams( *++argv );
  196. }
  197. DbgPrint( "--------------------------------------------\n" );
  198. } else {
  199. DbgPrint( "--------------------------------------------\n" );
  200. while (argc-- != 0) {
  201. LongStream( *argv++ );
  202. }
  203. DbgPrint( "--------------------------------------------\n" );
  204. }
  205. return 0;
  206. }