ReactOS  0.4.14-dev-315-gbb6fece
misc.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Serial enumerator driver
4  * FILE: drivers/dd/serenum/misc.c
5  * PURPOSE: Miscellaneous operations
6  *
7  * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.com)
8  */
9 
10 #include "serenum.h"
11 
12 #include <debug.h>
13 
14 static IO_COMPLETION_ROUTINE ForwardIrpAndWaitCompletion;
15 
16 /* I really want PCSZ strings as last arguments because
17  * PnP ids are ANSI-encoded in PnP device string
18  * identification */
22  ... /* list of PCSZ */)
23 {
24  va_list args;
25  PCSZ Source;
28  ULONG DestinationSize = 0;
30 
32 
33  /* Calculate length needed for destination unicode string */
35  Source = va_arg(args, PCSZ);
36  while (Source != NULL)
37  {
39  DestinationSize += RtlAnsiStringToUnicodeSize(&AnsiString)
40  + sizeof(WCHAR) /* final NULL */;
41  Source = va_arg(args, PCSZ);
42  }
43  va_end(args);
44  if (DestinationSize == 0)
45  {
47  return STATUS_SUCCESS;
48  }
49 
50  /* Initialize destination string */
51  DestinationSize += sizeof(WCHAR); // final NULL
53  if (!Destination->Buffer)
55  Destination->Length = 0;
56  Destination->MaximumLength = (USHORT)DestinationSize;
57 
58  /* Copy arguments to destination string */
59  /* Use a temporary unicode string, which buffer is shared with
60  * destination string, to copy arguments */
62  UnicodeString.MaximumLength = Destination->MaximumLength;
65  Source = va_arg(args, PCSZ);
66  while (Source != NULL)
67  {
70  if (!NT_SUCCESS(Status))
71  {
73  break;
74  }
75  Destination->Length += UnicodeString.Length + sizeof(WCHAR);
76  UnicodeString.MaximumLength -= UnicodeString.Length + sizeof(WCHAR);
77  UnicodeString.Buffer += UnicodeString.Length / sizeof(WCHAR) + 1;
78  UnicodeString.Length = 0;
79  Source = va_arg(args, PCSZ);
80  }
81  va_end(args);
82  if (NT_SUCCESS(Status))
83  {
84  /* Finish multi-sz string */
85  Destination->Buffer[Destination->Length / sizeof(WCHAR)] = L'\0';
86  Destination->Length += sizeof(WCHAR);
87  }
88  return Status;
89 }
90 
91 static NTSTATUS NTAPI
94  IN PIRP Irp,
96 {
97  if (Irp->PendingReturned)
100 }
101 
102 NTSTATUS
105  IN PIRP Irp)
106 {
107  PDEVICE_OBJECT LowerDevice;
108  KEVENT Event;
110 
112  LowerDevice = ((PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->LowerDevice;
113 
114  ASSERT(LowerDevice);
115 
118 
119  TRACE_(SERENUM, "Calling lower device %p\n", LowerDevice);
121 
122  Status = IoCallDriver(LowerDevice, Irp);
123  if (Status == STATUS_PENDING)
124  {
126  if (NT_SUCCESS(Status))
127  Status = Irp->IoStatus.Status;
128  }
129 
130  return Status;
131 }
132 
136  IN PIRP Irp)
137 {
138  PFDO_DEVICE_EXTENSION DeviceExtension;
139  PDEVICE_OBJECT LowerDevice;
140 
142  ASSERT(DeviceExtension->Common.IsFDO);
143 
144  LowerDevice = DeviceExtension->LowerDevice;
145  ASSERT(LowerDevice);
146  TRACE_(SERENUM, "Calling lower device 0x%p\n", LowerDevice);
148  return IoCallDriver(LowerDevice, Irp);
149 }
150 
154  IN PIRP Irp)
155 {
156  PPDO_DEVICE_EXTENSION DeviceExtension;
157  PDEVICE_OBJECT Fdo;
158 
160  ASSERT(!DeviceExtension->Common.IsFDO);
161 
162  Fdo = DeviceExtension->AttachedFdo;
163  ASSERT(Fdo);
164  TRACE_(SERENUM, "Calling attached Fdo 0x%p\n", Fdo);
166  return IoCallDriver(Fdo, Irp);
167 }
168 
172  IN PIRP Irp)
173 {
174  PDEVICE_OBJECT LowerDevice;
175 
177  LowerDevice = ((PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->LowerDevice;
178  ASSERT(LowerDevice);
179 
181  return IoCallDriver(LowerDevice, Irp);
182 }
183 
184 NTSTATUS
186  IN ULONG Flags,
189 {
194  {
196  }
197 
198 
199  if ((SourceString->Length == 0)
202  {
206  }
207  else
208  {
209  USHORT DestMaxLength = SourceString->Length;
210 
212  DestMaxLength += sizeof(UNICODE_NULL);
213 
215  if (DestinationString->Buffer == NULL)
216  return STATUS_NO_MEMORY;
217 
220  DestinationString->MaximumLength = DestMaxLength;
221 
224  }
225 
226  return STATUS_SUCCESS;
227 }
CONST char * PCSZ
Definition: umtypes.h:125
#define IN
Definition: typedefs.h:38
NTSTATUS DuplicateUnicodeString(IN ULONG Flags, IN PCUNICODE_STRING SourceString, OUT PUNICODE_STRING DestinationString)
Definition: misc.c:72
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
NTSTATUS NTAPI ForwardIrpToLowerDeviceAndForget(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: misc.c:134
*BytesInUnicodeString PWCH UnicodeString
Definition: rtlfuncs.h:1980
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
_In_ PIRP Irp
Definition: csq.h:116
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
uint16_t * PWSTR
Definition: typedefs.h:54
#define RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING
Definition: green.h:16
static IO_COMPLETION_ROUTINE ForwardIrpAndWaitCompletion
Definition: misc.c:14
NTSTATUS SerenumInitMultiSzString(OUT PUNICODE_STRING Destination,...)
Definition: misc.c:20
LONG NTSTATUS
Definition: precomp.h:26
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
unsigned short Length
Definition: sprintf.c:451
#define SERENUM_TAG
Definition: serenum.h:58
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
Definition: match.c:390
NTSTATUS NTAPI ForwardIrpAndWait(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: misc.c:33
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:515
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
FORCEINLINE VOID IoCopyCurrentIrpStackLocationToNext(_Inout_ PIRP Irp)
Definition: iofuncs.h:2820
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
void * Buffer
Definition: sprintf.c:453
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define va_end(ap)
Definition: acmsvcex.h:90
#define UNICODE_NULL
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:435
PVOID DeviceExtension
Definition: env_spec_w32.h:418
_Out_ _Inout_ POEM_STRING DestinationString
Definition: rtlfuncs.h:1869
smooth NULL
Definition: ftsmooth.c:416
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:55
char * va_list
Definition: acmsvcex.h:78
#define TRACE_(x)
Definition: compat.h:66
PDEVICE_OBJECT AttachedFdo
Definition: parport.h:59
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
NTSTATUS NTAPI ForwardIrpToAttachedFdoAndForget(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: misc.c:152
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2891
#define RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
Definition: green.h:15
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
_Out_ _Inout_ POEM_STRING _In_ PCUNICODE_STRING SourceString
Definition: rtlfuncs.h:1869
static const WCHAR L[]
Definition: oid.c:1250
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:80
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
#define va_arg(ap, T)
Definition: acmsvcex.h:89
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSYSAPI DWORD WINAPI RtlAnsiStringToUnicodeSize(const STRING *)
unsigned short USHORT
Definition: pedump.c:61
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
#define va_start(ap, A)
Definition: acmsvcex.h:91
NTSTATUS NTAPI ForwardIrpAndForget(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: misc.c:59
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define IoSkipCurrentIrpStackLocation(Irp)
Definition: ntifs_ex.h:421
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:566
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
PDEVICE_OBJECT LowerDevice
Definition: i8042prt.h:130
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:2938
unsigned short MaximumLength
Definition: sprintf.c:452
#define args
Definition: format.c:66