ReactOS  0.4.13-dev-52-g0efcfec
main.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Named Pipe FileSystem
3  * LICENSE: BSD - See COPYING.ARM in the top level directory
4  * FILE: drivers/filesystems/npfs/main.c
5  * PURPOSE: Named Pipe FileSystem Driver Initialization
6  * PROGRAMMERS: ReactOS Portable Systems Group
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include "npfs.h"
12 
13 // File ID number for NPFS bugchecking support
14 #define NPFS_BUGCHECK_FILE_ID (NPFS_BUGCHECK_MAIN)
15 
16 /* GLOBALS ********************************************************************/
17 
22 
24 {
25  sizeof(FAST_IO_DISPATCH),
26  NULL,
27  NpFastRead,
28  NpFastWrite,
29 };
30 
31 /* FUNCTIONS ******************************************************************/
32 
34 NTAPI
40  PVOID Context,
42 {
43  PNPFS_QUERY_VALUE_CONTEXT QueryContext = Context;
44  PWSTR CurrentString;
45  USHORT Length;
46  PNPFS_ALIAS CurrentAlias;
47  UNICODE_STRING TempString;
48  PUNICODE_STRING CurrentTargetName;
49 
50  /* Check if we have the expected type */
51  if (ValueType != REG_MULTI_SZ)
52  {
54  }
55 
56  /* Check if only the size is requested */
57  if (QueryContext->SizeOnly)
58  {
59  /* Count this entry */
60  QueryContext->NumberOfEntries++;
61 
62  /* Get the length of the value name (i.e. the target name). */
63  Length = wcslen(ValueName) * sizeof(WCHAR);
64 
65  /* Add the size of the name plus a '\' and a UNICODE_STRING structure */
66  QueryContext->FullSize += Length + sizeof(UNICODE_NULL) +
67  sizeof(OBJ_NAME_PATH_SEPARATOR) +
68  sizeof(UNICODE_STRING);
69 
70  /* Loop while we have alias names */
71  CurrentString = ValueData;
72  while (*CurrentString != UNICODE_NULL)
73  {
74  /* Count this alias */
75  QueryContext->NumberOfAliases++;
76 
77  /* Get the length of the current string (i.e. the alias name) */
78  Length = wcslen(CurrentString) * sizeof(WCHAR);
79 
80  /* Count the length plus the size of an NPFS_ALIAS structure */
81  QueryContext->FullSize += Length + sizeof(UNICODE_NULL) + sizeof(NPFS_ALIAS);
82 
83  /* Go to the next string */
84  CurrentString += (Length + sizeof(UNICODE_NULL)) / sizeof(WCHAR);
85  }
86  }
87  else
88  {
89  /* Get the next name string pointer */
90  CurrentTargetName = QueryContext->CurrentTargetName++;
91 
92  /* Get the length of the value name (i.e. the target name). */
93  Length = wcslen(ValueName) * sizeof(WCHAR);
94 
95  /* Initialize the current name string (one char more than the name) */
96  CurrentTargetName->Buffer = QueryContext->CurrentStringPointer;
97  CurrentTargetName->Length = Length + sizeof(OBJ_NAME_PATH_SEPARATOR);
98  CurrentTargetName->MaximumLength = CurrentTargetName->Length + sizeof(UNICODE_NULL);
99 
100  /* Update the current string pointer */
101  QueryContext->CurrentStringPointer +=
102  CurrentTargetName->MaximumLength / sizeof(WCHAR);
103 
104  /* Prepend a '\' before the name */
105  CurrentTargetName->Buffer[0] = OBJ_NAME_PATH_SEPARATOR;
106 
107  /* Append the value name (including the NULL termination) */
108  RtlCopyMemory(&CurrentTargetName->Buffer[1],
109  ValueName,
110  Length + sizeof(UNICODE_NULL));
111 
112  /* Upcase the target name */
113  RtlUpcaseUnicodeString(CurrentTargetName, CurrentTargetName, 0);
114 
115  /* Loop while we have alias names */
116  CurrentString = ValueData;
117  while (*CurrentString != UNICODE_NULL)
118  {
119  /* Get the next alias pointer */
120  CurrentAlias = QueryContext->CurrentAlias++;
121 
122  /* Get the length of the current string (i.e. the alias name) */
123  Length = wcslen(CurrentString) * sizeof(WCHAR);
124 
125  /* Setup the alias structure */
126  CurrentAlias->TargetName = CurrentTargetName;
127  CurrentAlias->Name.Buffer = QueryContext->CurrentStringPointer;
128  CurrentAlias->Name.Length = Length;
129  CurrentAlias->Name.MaximumLength = Length + sizeof(UNICODE_NULL);
130 
131  /* Upcase the alias name */
132  TempString.Buffer = CurrentString;
133  TempString.Length = Length;
134  RtlUpcaseUnicodeString(&CurrentAlias->Name,
135  &TempString,
136  FALSE);
137 
138  /* Update the current string pointer */
139  QueryContext->CurrentStringPointer +=
140  CurrentAlias->Name.MaximumLength / sizeof(WCHAR);
141 
142  /* Go to the next string */
143  CurrentString += (Length + sizeof(UNICODE_NULL)) / sizeof(WCHAR);
144  }
145  }
146 
147  return STATUS_SUCCESS;
148 }
149 
150 LONG
151 NTAPI
153  _In_ PCUNICODE_STRING String1,
155 {
156  ULONG Count;
157  PWCHAR P1, P2;
158 
159  /* First check if the string sizes match */
160  if (String1->Length != String2->Length)
161  {
162  /* They don't, return positive if the first is longer, negative otherwise */
163  return String1->Length - String2->Length;
164  }
165 
166  /* Now loop all characters */
167  Count = String1->Length / sizeof(WCHAR);
168  P1 = String1->Buffer;
169  P2 = String2->Buffer;
170  while (Count)
171  {
172  /* Check if they don't match */
173  if (*P1 != *P2)
174  {
175  /* Return positive if the first char is greater, negative otherwise */
176  return *P1 - *P2;
177  }
178 
179  /* Go to the next buffer position */
180  P1++;
181  P2++;
182  Count--;
183  }
184 
185  /* All characters matched, return 0 */
186  return 0;
187 }
188 
189 NTSTATUS
190 NTAPI
192 {
196  USHORT Length;
197  ULONG i;
198  PNPFS_ALIAS CurrentAlias, *AliasPointer;
199 
200  /* Initialize the query table */
203  QueryTable[0].Name = NULL;
207  QueryTable[0].DefaultLength = 0;
209  QueryTable[1].Flags = 0;
210  QueryTable[1].Name = NULL;
211 
212  /* Setup the query context */
213  Context.SizeOnly = 1;
214  Context.FullSize = 0;
215  Context.NumberOfAliases = 0;
216  Context.NumberOfEntries = 0;
217 
218  /* Query the registry values (calculate length only) */
220  L"Npfs\\Aliases",
221  QueryTable,
222  &Context,
223  NULL);
224  if (!NT_SUCCESS(Status))
225  {
227  return STATUS_SUCCESS;
228 
229  return Status;
230  }
231 
232  /* Check if there is anything */
233  if (Context.FullSize == 0)
234  {
235  /* Nothing to do, return success */
236  return STATUS_SUCCESS;
237  }
238 
239  /* Allocate a structure large enough to hold all the data */
241  if (NpAliases == NULL)
243 
244  /* Now setup the actual pointers in the context */
245  Context.CurrentTargetName = NpAliases;
246  CurrentAlias = (PNPFS_ALIAS)&Context.CurrentTargetName[Context.NumberOfEntries];
247  Context.CurrentAlias = CurrentAlias;
248  Context.CurrentStringPointer = (PWCHAR)&CurrentAlias[Context.NumberOfAliases];
249 
250  /* This time query the real data */
251  Context.SizeOnly = FALSE;
253  L"Npfs\\Aliases",
254  QueryTable,
255  &Context,
256  NULL);
257  if (!NT_SUCCESS(Status))
258  {
260  NpAliases = NULL;
261  return Status;
262  }
263 
264  /* Make sure we didn't go past the end of the allocation! */
265  NT_ASSERT((PUCHAR)Context.CurrentStringPointer <=
266  ((PUCHAR)NpAliases + Context.FullSize));
267 
268  /* Loop all aliases we got */
269  for (i = 0; i < Context.NumberOfAliases; i++)
270  {
271  /* Get the length and check what list to use */
272  Length = CurrentAlias->Name.Length;
273  if ((Length >= MIN_INDEXED_LENGTH * sizeof(WCHAR)) &&
274  (Length <= MAX_INDEXED_LENGTH * sizeof(WCHAR)))
275  {
276  /* For this length range, we use an indexed list */
277  AliasPointer = &NpAliasListByLength[(Length / sizeof(WCHAR)) - 5];
278  }
279  else
280  {
281  /* Length is outside of the range, use the default list */
282  AliasPointer = &NpAliasList;
283  }
284 
285  /* Loop through all aliases already in the list until we find one that
286  is greater than our current alias */
287  while ((*AliasPointer != NULL) &&
288  (NpCompareAliasNames(&CurrentAlias->Name,
289  &(*AliasPointer)->Name) > 0))
290  {
291  /* Go to the next alias */
292  AliasPointer = &(*AliasPointer)->Next;
293  }
294 
295  /* Insert the alias in the list */
296  CurrentAlias->Next = *AliasPointer;
297  *AliasPointer = CurrentAlias;
298 
299  /* Go to the next alias in the array */
300  CurrentAlias++;
301  }
302 
303  return STATUS_SUCCESS;
304 }
305 
306 
307 NTSTATUS
308 NTAPI
310  IN PIRP Irp)
311 {
312  TRACE("Entered\n");
314 
315  Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
316  Irp->IoStatus.Information = 0;
317 
319  return STATUS_NOT_IMPLEMENTED;
320 }
321 
322 
323 NTSTATUS
324 NTAPI
327 {
332 
333  DPRINT("Next-Generation NPFS-Advanced\n");
334 
336  if (!NT_SUCCESS(Status))
337  {
338  DPRINT1("Failed to initialize aliases!\n");
339  return Status;
340  }
341 
356 
358 
360 
361  RtlInitUnicodeString(&DeviceName, L"\\Device\\NamedPipe");
363  sizeof(NP_VCB),
364  &DeviceName,
366  0,
367  FALSE,
368  &DeviceObject);
369  if (!NT_SUCCESS(Status))
370  {
371  DPRINT1("Failed to create named pipe device! (Status %lx)\n", Status);
372  return Status;
373  }
374 
375  /* Initialize the device object */
378 
379  /* Initialize the Volume Control Block (VCB) */
381  NpInitializeVcb();
384  return STATUS_SUCCESS;
385 }
386 
387 /* EOF */
PNPFS_ALIAS CurrentAlias
Definition: npfs.h:305
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:3988
#define IN
Definition: typedefs.h:38
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define IRP_MJ_CREATE
Definition: rdpdr.c:44
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
NTSTATUS RtlUpcaseUnicodeString(PUNICODE_STRING dst, PUNICODE_STRING src, BOOLEAN Alloc)
Definition: string_lib.cpp:46
PDEVICE_OBJECT NpfsDeviceObject
Definition: main.c:18
PNPFS_ALIAS NpAliasList
Definition: main.c:20
#define IRP_MJ_QUERY_SECURITY
#define IRP_MJ_FLUSH_BUFFERS
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:225
USHORT MaximumLength
Definition: env_spec_w32.h:370
_In_ PIRP Irp
Definition: csq.h:116
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
uint16_t * PWSTR
Definition: typedefs.h:54
PNPFS_ALIAS NpAliasListByLength[MAX_INDEXED_LENGTH+1 - MIN_INDEXED_LENGTH]
Definition: main.c:21
#define IRP_MJ_SET_SECURITY
#define MAX_INDEXED_LENGTH
Definition: npfs.h:33
unsigned char * PUCHAR
Definition: retypes.h:3
#define FILE_DEVICE_NAMED_PIPE
Definition: winioctl.h:122
_In_ PCWSTR _In_z_ PCWSTR _In_ ULONG ValueType
Definition: rtlfuncs.h:4000
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NpFsdFileSystemControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fsctrl.c:857
NTSTATUS NTAPI NpFsdQueryVolumeInformation(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: volinfo.c:185
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define RTL_QUERY_REGISTRY_NOEXPAND
Definition: nt_native.h:139
Definition: npfs.h:277
uint16_t * PWCHAR
Definition: typedefs.h:54
WCHAR DeviceName[]
Definition: adapter.cpp:21
#define RTL_REGISTRY_SERVICES
Definition: nt_native.h:162
NTSTATUS NTAPI NpFsdCreateNamedPipe(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: create.c:791
struct _NPFS_ALIAS * PNPFS_ALIAS
FAST_IO_DISPATCH NpFastIoDispatch
Definition: main.c:23
_In_ PUNICODE_STRING ValueName
Definition: cmfuncs.h:264
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
PRTL_QUERY_REGISTRY_ROUTINE QueryRoutine
Definition: nt_native.h:109
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define UNICODE_NULL
long LONG
Definition: pedump.c:60
#define OBJ_NAME_PATH_SEPARATOR
Definition: arcname_tests.c:25
#define REG_MULTI_SZ
Definition: nt_native.h:1501
struct _FAST_IO_DISPATCH FAST_IO_DISPATCH
PVOID DeviceExtension
Definition: env_spec_w32.h:418
_In_ PCWSTR _Inout_ _At_ QueryTable EntryContext
Definition: rtlfuncs.h:3988
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
#define IoCompleteRequest
Definition: irp.c:1240
void DPRINT(...)
Definition: polytest.cpp:61
#define RTL_REGISTRY_OPTIONAL
Definition: nt_native.h:169
#define IRP_MJ_QUERY_VOLUME_INFORMATION
Definition: rdpdr.c:50
#define IRP_MJ_DIRECTORY_CONTROL
Definition: rdpdr.c:51
LONG NTAPI NpCompareAliasNames(_In_ PCUNICODE_STRING String1, _In_ PCUNICODE_STRING String2)
Definition: main.c:152
NTSTATUS NTAPI NpFsdQueryInformation(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fileinfo.c:462
#define TRACE(s)
Definition: solgame.cpp:4
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define IRP_MJ_FILE_SYSTEM_CONTROL
NTSTATUS NTAPI NpFsdCleanup(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: cleanup.c:58
NTSTATUS NTAPI NpFsdQuerySecurityInfo(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: seinfo.c:100
_In_ GUID _In_ PVOID ValueData
Definition: hubbusif.h:311
NTSYSAPI NTSTATUS WINAPI RtlQueryRegistryValues(ULONG, PCWSTR, PRTL_QUERY_REGISTRY_TABLE, PVOID, PVOID)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
PNP_VCB NpVcb
Definition: strucsup.c:19
struct _NPFS_ALIAS * Next
Definition: npfs.h:293
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
NTSTATUS NTAPI NpReadAlias(PWSTR ValueName, ULONG ValueType, PVOID ValueData, ULONG ValueLength, PVOID Context, PVOID EntryContext)
Definition: main.c:35
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
NTSTATUS NTAPI NpCreateRootDcb(VOID)
Definition: strucsup.c:165
NTSTATUS NTAPI NpFsdClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: close.c:59
#define IRP_MJ_CLOSE
Definition: rdpdr.c:45
static const WCHAR L[]
Definition: oid.c:1250
PDRIVER_UNLOAD DriverUnload
Definition: iotypes.h:2179
_In_ GUID _In_ PVOID _In_ ULONG ValueLength
Definition: hubbusif.h:311
NTSTATUS NTAPI NpFsdWrite(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: write.c:175
PUNICODE_STRING CurrentTargetName
Definition: npfs.h:306
VOID NTAPI NpInitializeVcb(VOID)
Definition: strucsup.c:130
UNICODE_STRING Name
Definition: npfs.h:295
Status
Definition: gdiplustypes.h:24
#define _In_
Definition: no_sal2.h:204
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
NTSTATUS NTAPI NpFsdSetSecurityInfo(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: seinfo.c:125
NTSTATUS NTAPI NpFsdFlushBuffers(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: flushbuf.c:70
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
unsigned short USHORT
Definition: pedump.c:61
PVOID NpAliases
Definition: main.c:19
NTSTATUS NTAPI DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
Definition: main.c:690
NTSTATUS NTAPI NpInitializeAliases(VOID)
Definition: main.c:191
#define MIN_INDEXED_LENGTH
Definition: npfs.h:32
NTSTATUS NTAPI NpFsdSetInformation(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fileinfo.c:114
NTSTATUS NTAPI NpFsdRead(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: read.c:159
PUNICODE_STRING TargetName
Definition: npfs.h:294
struct _UNICODE_STRING UNICODE_STRING
#define DPRINT1
Definition: precomp.h:8
#define IRP_MJ_READ
Definition: rdpdr.c:46
_In_ const STRING * String2
Definition: rtlfuncs.h:2245
NTSTATUS NTAPI NpFsdCreate(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: create.c:360
struct _FAST_IO_DISPATCH * FastIoDispatch
Definition: iotypes.h:2176
#define DO_LONG_TERM_REQUESTS
Definition: env_spec_w32.h:401
#define IRP_MJ_CLEANUP
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION+1]
Definition: iotypes.h:2180
struct tagContext Context
Definition: acpixf.h:1012
#define IRP_MJ_SET_INFORMATION
Definition: rdpdr.c:49
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:565
NTSTATUS NTAPI IoCreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1031
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define UNIMPLEMENTED
Definition: debug.h:114
_In_ PUNICODE_STRING RegistryPath
Definition: wmip.h:27
#define IRP_MJ_WRITE
Definition: rdpdr.c:47
#define IRP_MJ_QUERY_INFORMATION
Definition: rdpdr.c:48
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define REG_NONE
Definition: nt_native.h:1492
NTSTATUS NTAPI NpFsdDirectoryControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: main.c:309
return STATUS_SUCCESS
Definition: btrfs.c:2725
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define IRP_MJ_CREATE_NAMED_PIPE
#define NT_ASSERT
Definition: rtlfuncs.h:3312
PWCHAR CurrentStringPointer
Definition: npfs.h:307