[MSPORTS]
[reactos.git] / reactos / dll / win32 / msports / comdb.c
index 2db51d0..20a6a80 100644 (file)
@@ -11,6 +11,8 @@
 WINE_DEFAULT_DEBUG_CHANNEL(msports);
 
 #define BITS_PER_BYTE 8
+#define BITMAP_SIZE_INCREMENT 0x400
+#define BITMAP_SIZE_INVALID_BITS 0x3FF
 
 typedef struct _COMDB
 {
@@ -276,6 +278,9 @@ ComDBGetCurrentPortUsage(IN HCOMDB hComDB,
     BYTE cMask;
     LONG lError = ERROR_SUCCESS;
 
+    TRACE("ComDBGetCurrentPortUsage(%p %p %lu %lu %p)\n",
+          hComDB, Buffer, BufferSize, ReportType, MaxPortsReported);
+
     if (hComDB == INVALID_HANDLE_VALUE ||
         hComDB == NULL ||
         (Buffer == NULL && MaxPortsReported == NULL) ||
@@ -581,8 +586,83 @@ WINAPI
 ComDBResizeDatabase(IN HCOMDB hComDB,
                     IN DWORD NewSize)
 {
-    FIXME("ComDBResizeDatabase(%p %lu)\n", hComDB, NewSize);
-    return ERROR_CALL_NOT_IMPLEMENTED;
+    PCOMDB pComDB;
+    PBYTE pBitmap = NULL;
+    DWORD dwSize;
+    DWORD dwNewSize;
+    DWORD dwType;
+    LONG lError;
+
+    TRACE("ComDBResizeDatabase(%p %lu)\n", hComDB, NewSize);
+
+    if (hComDB == INVALID_HANDLE_VALUE ||
+        hComDB == NULL ||
+        (NewSize & BITMAP_SIZE_INVALID_BITS))
+        return ERROR_INVALID_PARAMETER;
+
+    pComDB = (PCOMDB)hComDB;
+
+    /* Wait for the mutex */
+    WaitForSingleObject(pComDB->hMutex, INFINITE);
+
+    /* Get the required bitmap size */
+    lError = RegQueryValueExW(pComDB->hKey,
+                              L"ComDB",
+                              NULL,
+                              &dwType,
+                              NULL,
+                              &dwSize);
+    if (lError != ERROR_SUCCESS)
+        goto done;
+
+    /* Check the size limits */
+    if (NewSize > COMDB_MAX_PORTS_ARBITRATED ||
+        NewSize <= dwSize * BITS_PER_BYTE)
+    {
+        lError = ERROR_BAD_LENGTH;
+        goto done;
+    }
+
+    /* Calculate the new bitmap size */
+    dwNewSize = NewSize / BITS_PER_BYTE;
+
+    /* Allocate the new bitmap */
+    pBitmap = HeapAlloc(GetProcessHeap(),
+                        HEAP_ZERO_MEMORY,
+                        dwSize);
+    if (pBitmap == NULL)
+    {
+        ERR("Failed to allocate the bitmap!\n");
+        lError = ERROR_ACCESS_DENIED;
+        goto done;
+    }
+
+    /* Read the current bitmap */
+    lError = RegQueryValueExW(pComDB->hKey,
+                              L"ComDB",
+                              NULL,
+                              &dwType,
+                              pBitmap,
+                              &dwSize);
+    if (lError != ERROR_SUCCESS)
+        goto done;
+
+    /* Write the new bitmap */
+    lError = RegSetValueExW(pComDB->hKey,
+                            L"ComDB",
+                            0,
+                            REG_BINARY,
+                            pBitmap,
+                            dwNewSize);
+
+done:;
+    /* Release the mutex */
+    ReleaseMutex(pComDB->hMutex);
+
+    if (pBitmap != NULL)
+        HeapFree(GetProcessHeap(), 0, pBitmap);
+
+    return lError;
 }
 
 /* EOF */