fixed calls to NtDuplicateObject
[reactos.git] / reactos / drivers / net / ndis / ndis / config.c
index bfafbe4..bc9e943 100644 (file)
@@ -7,7 +7,7 @@
  * REVISIONS:
  *     Vizzini 07-28-2003 Created
  * NOTES:
- *     - Resource tracking has to be implemented here because of the design of the NDIS API. 
+ *     - Resource tracking has to be implemented here because of the design of the NDIS API.
  *       Whenever a read operation is performed, the NDIS library allocates space and returns
  *       it.  A linked list is kept associated with every handle of the memory allocated to
  *       it.  When the handle is closed, the resources are systematically released.
@@ -16,8 +16,8 @@
  *       pass this NDIS_HANDLE to things like ZwQueryValueKey().  I don't thknk they do (they
  *       certainly should not), but it should be kept in mind.
  *         UPDATE:  I just found this in the NTDDK:
- *         NdisOpenProtocolConfiguration returns a handle for the 
- *         HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\NICDriverInstance\Parameters\ProtocolName 
+ *         NdisOpenProtocolConfiguration returns a handle for the
+ *         HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\NICDriverInstance\Parameters\ProtocolName
  *         registry key.  XXX This is a problem.  Following that, the DDK instructs programmers
  *         to use NdisReadConfiguration and NdisWriteConfiguration.  No telling what the world's idiots
  *         have done with this.
  *       places that the DDK doesn't explicitly mention it, though.
  *     - There's a general reliance on the fact that UNICODE_STRING.Length doesn't include a trailing
  *       0, which it shouldn't
- *     - I added support for NdisParameterBinary.  It's at the end of the struct.  I wonder if 
+ *     - I added support for NdisParameterBinary.  It's at the end of the struct.  I wonder if
  *       it'll break things.
  *     - All the routines in this file are PASSIVE_LEVEL only, and all memory is PagedPool
  */
 
-#include <miniport.h>
+#include "ndissys.h"
 
 #define NDIS_VERSION 0x00040000          /* the version of NDIS we claim to be to miniport drivers */
 #define PARAMETERS_KEY L"Parameters"     /* The parameters subkey under the device-specific key */
@@ -51,7 +51,7 @@ NdisWriteConfiguration(
  * ARGUMENTS:
  *     Status: Pointer to a caller-supplied NDIS_STATUS where we return status
  *     ConfigurationHandle: The Configuration Handle passed back from the call to one of the Open functions
- *     Keyword: The registry value name to write 
+ *     Keyword: The registry value name to write
  *     ParameterValue: The value data to write
  * RETURNS:
  *     NDIS_STATUS_SUCCESS - the operation completed successfully
@@ -102,9 +102,13 @@ NdisWriteConfiguration(
             Data = ParameterValue->ParameterData.BinaryData.Buffer;
             DataSize = ParameterValue->ParameterData.BinaryData.Length;
             break;
+
+        default:
+            *Status = NDIS_STATUS_FAILURE;
+            return;
     }
 
-    *Status = ZwSetValueKey(((PMINIPORT_CONFIGURATION_CONTEXT)ConfigurationHandle)->Handle, 
+    *Status = ZwSetValueKey(((PMINIPORT_CONFIGURATION_CONTEXT)ConfigurationHandle)->Handle,
             Keyword, 0, ParameterType, Data, DataSize);
 
     if(*Status != STATUS_SUCCESS)
@@ -168,21 +172,19 @@ NdisOpenConfiguration(
  *     I think this is the parameters key; please verify.
  */
 {
-    OBJECT_ATTRIBUTES KeyAttributes;
-    UNICODE_STRING KeyNameU;
     HANDLE KeyHandle;
     PMINIPORT_CONFIGURATION_CONTEXT ConfigurationContext;
-    HANDLE RootKeyHandle = (HANDLE)WrapperConfigurationContext;
+    PNDIS_WRAPPER_CONTEXT WrapperContext = (PNDIS_WRAPPER_CONTEXT)WrapperConfigurationContext;
+    HANDLE RootKeyHandle = WrapperContext->RegistryHandle;
 
     NDIS_DbgPrint(MAX_TRACE, ("Called\n"));
 
-    RtlInitUnicodeString(&KeyNameU, PARAMETERS_KEY);
-    InitializeObjectAttributes(&KeyAttributes, &KeyNameU, OBJ_CASE_INSENSITIVE, RootKeyHandle, NULL);
-
-    *Status = ZwOpenKey(&KeyHandle, KEY_ALL_ACCESS, &KeyAttributes);
-    if(*Status != STATUS_SUCCESS)
+    *Status = ZwDuplicateObject(NtCurrentProcess(), RootKeyHandle,
+                                NtCurrentProcess(), &KeyHandle, 0, 0,
+                                DUPLICATE_SAME_ACCESS);
+    if(!NT_SUCCESS(*Status))
     {
-      NDIS_DbgPrint(MID_TRACE, ("Failed to open registry configuration for this miniport\n"));
+        NDIS_DbgPrint(MID_TRACE, ("Failed to open registry configuration for this miniport\n"));
         *ConfigurationHandle = NULL;
         *Status = NDIS_STATUS_FAILURE;
         return;
@@ -233,12 +235,13 @@ NdisOpenProtocolConfiguration(
 {
     OBJECT_ATTRIBUTES KeyAttributes;
     UNICODE_STRING KeyNameU;
-    WCHAR *KeyName;
     HANDLE KeyHandle;
     PMINIPORT_CONFIGURATION_CONTEXT ConfigurationContext;
 
-    KeyName = ExAllocatePool(PagedPool, ProtocolSection->Length + sizeof(PARAMETERS_KEY) + sizeof(WCHAR));
-    if(!KeyName)
+    KeyNameU.Length = 0;
+    KeyNameU.MaximumLength = ProtocolSection->Length + sizeof(PARAMETERS_KEY) + sizeof(UNICODE_NULL);
+    KeyNameU.Buffer = ExAllocatePool(PagedPool, KeyNameU.MaximumLength);
+    if(!KeyNameU.Buffer)
     {
         NDIS_DbgPrint(MIN_TRACE,("Insufficient resources.\n"));
         *ConfigurationHandle = NULL;
@@ -246,14 +249,13 @@ NdisOpenProtocolConfiguration(
         return;
     }
 
-    wcsncpy(KeyName, ProtocolSection->Buffer, ProtocolSection->Length/sizeof(WCHAR));
-    wcscpy(KeyName + ProtocolSection->Length, PARAMETERS_KEY);
-    RtlInitUnicodeString(&KeyNameU, KeyName);
+    RtlCopyUnicodeString(&KeyNameU, ProtocolSection);
+    RtlAppendUnicodeToString(&KeyNameU, PARAMETERS_KEY);
     InitializeObjectAttributes(&KeyAttributes, &KeyNameU, OBJ_CASE_INSENSITIVE, NULL, NULL);
 
     *Status = ZwOpenKey(&KeyHandle, KEY_ALL_ACCESS, &KeyAttributes);
 
-    ExFreePool(KeyName);
+    ExFreePool(KeyNameU.Buffer);
 
     if(*Status != NDIS_STATUS_SUCCESS)
     {
@@ -315,7 +317,7 @@ NdisReadConfiguration(
 
     *ParameterValue = NULL;
     *Status = NDIS_STATUS_FAILURE;
-    
+
     if(ParameterType != NdisParameterInteger &&
         ParameterType != NdisParameterHexInteger &&
         ParameterType != NdisParameterString &&
@@ -355,10 +357,10 @@ NdisReadConfiguration(
         MiniportResource->ResourceType = 0;
         MiniportResource->Resource = *ParameterValue;
 
-        NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n", 
+        NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n",
             MiniportResource->Resource));
 
-        ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead, 
+        ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead,
             &MiniportResource->ListEntry, &ConfigurationContext->ResourceLock);
 
         (*ParameterValue)->ParameterType = NdisParameterInteger;
@@ -394,7 +396,7 @@ NdisReadConfiguration(
         MiniportResource->ResourceType = 0;
         MiniportResource->Resource = *ParameterValue;
         NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n", MiniportResource->Resource));
-        ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead, 
+        ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead,
             &MiniportResource->ListEntry, &ConfigurationContext->ResourceLock);
 
         (*ParameterValue)->ParameterType = NdisParameterInteger;
@@ -430,7 +432,7 @@ NdisReadConfiguration(
         MiniportResource->ResourceType = 0;
         MiniportResource->Resource = *ParameterValue;
         NDIS_DbgPrint(MID_TRACE,("inserting 0x%x into the resource list\n", MiniportResource->Resource));
-        ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead, 
+        ExInterlockedInsertTailList(&ConfigurationContext->ResourceListHead,
             &MiniportResource->ListEntry, &ConfigurationContext->ResourceLock);
 
         (*ParameterValue)->ParameterType = NdisParameterInteger;
@@ -461,7 +463,7 @@ NdisReadConfiguration(
     }
 
     /* grab the value */
-    *Status = ZwQueryValueKey(ConfigurationContext->Handle, Keyword, KeyValuePartialInformation, 
+    *Status = ZwQueryValueKey(ConfigurationContext->Handle, Keyword, KeyValuePartialInformation,
         KeyInformation, KeyDataLength + sizeof(KEY_VALUE_PARTIAL_INFORMATION), &KeyDataLength);
     if(*Status != STATUS_SUCCESS)
     {
@@ -487,7 +489,7 @@ NdisReadConfiguration(
                 return;
             }
 
-            str.Length = str.MaximumLength = KeyInformation->DataLength;
+            str.Length = str.MaximumLength = (USHORT)KeyInformation->DataLength;
             str.Buffer = (PWCHAR)KeyInformation->Data;
 
             (*ParameterValue)->ParameterType = ParameterType;
@@ -506,7 +508,6 @@ NdisReadConfiguration(
         case NdisParameterString:
         case NdisParameterMultiString:
         {
-            PMINIPORT_RESOURCE MiniportResource = 0;
             PWCHAR RegData = 0;
 
             if(KeyInformation->Type != REG_SZ && KeyInformation->Type != REG_MULTI_SZ)
@@ -557,7 +558,7 @@ NdisReadConfiguration(
             memcpy(RegData, KeyInformation->Data, KeyInformation->DataLength);
 
             (*ParameterValue)->ParameterType = ParameterType;
-            (*ParameterValue)->ParameterData.StringData.Length = KeyInformation->DataLength;
+            (*ParameterValue)->ParameterData.StringData.Length = (USHORT)KeyInformation->DataLength;
             (*ParameterValue)->ParameterData.StringData.Buffer = RegData;
 
             ExFreePool(KeyInformation);
@@ -569,8 +570,6 @@ NdisReadConfiguration(
 
         case NdisParameterBinary:
         {
-            PMINIPORT_RESOURCE MiniportResource;
-
             if(KeyInformation->Type != REG_BINARY)
             {
                 NDIS_DbgPrint(MIN_TRACE,("requested type does not match actual value type\n"));
@@ -628,18 +627,18 @@ UCHAR UnicodeToHexByte(WCHAR chr)
 {
     switch(chr)
     {
-        case L'0': return 0; 
-        case L'1': return 1; 
-        case L'2': return 2; 
-        case L'3': return 3; 
-        case L'4': return 4; 
-        case L'5': return 5; 
+        case L'0': return 0;
+        case L'1': return 1;
+        case L'2': return 2;
+        case L'3': return 3;
+        case L'4': return 4;
+        case L'5': return 5;
         case L'6': return 6;
         case L'7': return 7;
         case L'8': return 8;
         case L'9': return 9;
-        case L'A': 
-        case L'a': 
+        case L'A':
+        case L'a':
             return 10;
         case L'B':
         case L'b':
@@ -790,7 +789,7 @@ NdisOpenConfigurationKeyByIndex(
         return;
     }
 
-    *Status = ZwEnumerateKey(ConfigurationHandle, Index, KeyBasicInformation, KeyInformation, 
+    *Status = ZwEnumerateKey(ConfigurationHandle, Index, KeyBasicInformation, KeyInformation,
         KeyInformationLength + sizeof(KEY_BASIC_INFORMATION), &KeyInformationLength);
 
     if(*Status != STATUS_SUCCESS)
@@ -802,7 +801,7 @@ NdisOpenConfigurationKeyByIndex(
 
     /* should i fail instead if the passed-in string isn't long enough? */
     wcsncpy(KeyName->Buffer, KeyInformation->Name, KeyName->MaximumLength/sizeof(WCHAR));
-    KeyName->Length = KeyInformation->NameLength;
+    KeyName->Length = (USHORT)KeyInformation->NameLength;
 
     InitializeObjectAttributes(&KeyAttributes, KeyName, OBJ_CASE_INSENSITIVE, ConfigurationHandle, NULL);