ReactOS  0.4.15-dev-5142-g967f5b9
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 /* I really want PCSZ strings as last arguments because
15  * PnP ids are ANSI-encoded in PnP device string
16  * identification */
20  ... /* list of PCSZ */)
21 {
22  va_list args;
23  PCSZ Source;
26  ULONG DestinationSize = 0;
28 
30 
31  /* Calculate length needed for destination unicode string */
33  Source = va_arg(args, PCSZ);
34  while (Source != NULL)
35  {
37  DestinationSize += RtlAnsiStringToUnicodeSize(&AnsiString)
38  + sizeof(WCHAR) /* final NULL */;
39  Source = va_arg(args, PCSZ);
40  }
41  va_end(args);
42  if (DestinationSize == 0)
43  {
45  return STATUS_SUCCESS;
46  }
47 
48  /* Initialize destination string */
49  DestinationSize += sizeof(WCHAR); // final NULL
51  if (!Destination->Buffer)
53  Destination->Length = 0;
54  Destination->MaximumLength = (USHORT)DestinationSize;
55 
56  /* Copy arguments to destination string */
57  /* Use a temporary unicode string, which buffer is shared with
58  * destination string, to copy arguments */
60  UnicodeString.MaximumLength = Destination->MaximumLength;
63  Source = va_arg(args, PCSZ);
64  while (Source != NULL)
65  {
68  if (!NT_SUCCESS(Status))
69  {
71  break;
72  }
73  Destination->Length += UnicodeString.Length + sizeof(WCHAR);
74  UnicodeString.MaximumLength -= UnicodeString.Length + sizeof(WCHAR);
75  UnicodeString.Buffer += UnicodeString.Length / sizeof(WCHAR) + 1;
76  UnicodeString.Length = 0;
77  Source = va_arg(args, PCSZ);
78  }
79  va_end(args);
80  if (NT_SUCCESS(Status))
81  {
82  /* Finish multi-sz string */
83  Destination->Buffer[Destination->Length / sizeof(WCHAR)] = L'\0';
84  Destination->Length += sizeof(WCHAR);
85  }
86  return Status;
87 }
88 
92  IN PIRP Irp)
93 {
94  PFDO_DEVICE_EXTENSION DeviceExtension;
95  PDEVICE_OBJECT LowerDevice;
96 
97  DeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
98  ASSERT(DeviceExtension->Common.IsFDO);
99 
100  LowerDevice = DeviceExtension->LowerDevice;
101  ASSERT(LowerDevice);
102  TRACE_(SERENUM, "Calling lower device 0x%p\n", LowerDevice);
104  return IoCallDriver(LowerDevice, Irp);
105 }
106 
110  IN PIRP Irp)
111 {
112  PPDO_DEVICE_EXTENSION DeviceExtension;
114 
115  DeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
116  ASSERT(!DeviceExtension->Common.IsFDO);
117 
118  Fdo = DeviceExtension->AttachedFdo;
119  ASSERT(Fdo);
120  TRACE_(SERENUM, "Calling attached Fdo 0x%p\n", Fdo);
122  return IoCallDriver(Fdo, Irp);
123 }
124 
128  IN PIRP Irp)
129 {
130  PDEVICE_OBJECT LowerDevice;
131 
132  ASSERT(((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO);
133  LowerDevice = ((PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->LowerDevice;
134  ASSERT(LowerDevice);
135 
137  return IoCallDriver(LowerDevice, Irp);
138 }
139 
140 NTSTATUS
142  IN ULONG Flags,
145 {
150  {
152  }
153 
154 
155  if ((SourceString->Length == 0)
158  {
162  }
163  else
164  {
165  USHORT DestMaxLength = SourceString->Length;
166 
168  DestMaxLength += sizeof(UNICODE_NULL);
169 
171  if (DestinationString->Buffer == NULL)
172  return STATUS_NO_MEMORY;
173 
176  DestinationString->MaximumLength = DestMaxLength;
177 
180  }
181 
182  return STATUS_SUCCESS;
183 }
CONST char * PCSZ
Definition: umtypes.h:125
#define IN
Definition: typedefs.h:39
NTSTATUS DuplicateUnicodeString(IN ULONG Flags, IN PCUNICODE_STRING SourceString, OUT PUNICODE_STRING DestinationString)
Definition: misc.c:31
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
NTSTATUS NTAPI ForwardIrpToLowerDeviceAndForget(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: misc.c:90
#define args
Definition: format.c:66
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
uint16_t * PWSTR
Definition: typedefs.h:56
#define RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING
Definition: green.h:16
NTSTATUS SerenumInitMultiSzString(OUT PUNICODE_STRING Destination,...)
Definition: misc.c:18
LONG NTSTATUS
Definition: precomp.h:26
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
unsigned short Length
Definition: sprintf.c:451
WDF_EXTERN_C_START typedef _Must_inspect_result_ _In_opt_ PCUNICODE_STRING UnicodeString
Definition: wdfstring.h:64
#define SERENUM_TAG
Definition: serenum.h:58
Definition: match.c:390
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
void * Buffer
Definition: sprintf.c:453
#define L(x)
Definition: ntvdm.h:50
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 FALSE
Definition: types.h:117
#define UNICODE_NULL
_In_ PIRP Irp
Definition: csq.h:116
NTSYSAPI DWORD WINAPI RtlAnsiStringToUnicodeSize(const STRING *)
_Out_ _Inout_ POEM_STRING DestinationString
Definition: rtlfuncs.h:1895
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:59
char * va_list
Definition: acmsvcex.h:78
Status
Definition: gdiplustypes.h:24
#define TRACE_(x)
Definition: compat.h:76
PDEVICE_OBJECT AttachedFdo
Definition: parport.h:59
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
#define ASSERT(a)
Definition: mode.c:44
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS NTAPI ForwardIrpToAttachedFdoAndForget(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: misc.c:108
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2977
va_start(ap, x)
#define RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
Definition: green.h:15
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
_Must_inspect_result_ _In_ WDFDEVICE Fdo
Definition: wdffdo.h:461
_Out_ _Inout_ POEM_STRING _In_ PCUNICODE_STRING SourceString
Definition: rtlfuncs.h:1895
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:84
#define va_arg(ap, T)
Definition: acmsvcex.h:89
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:260
NTSTATUS NTAPI ForwardIrpAndForget(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: misc.c:18
#define NULL
Definition: types.h:112
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:40
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
PDEVICE_OBJECT LowerDevice
Definition: i8042prt.h:130
#define STATUS_SUCCESS
Definition: shellext.h:65
unsigned short MaximumLength
Definition: sprintf.c:452
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099