2011-05-23 17:54:16 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: Ports installer library
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: dll\win32\msports\comdb.c
|
|
|
|
* PURPOSE: COM port database
|
|
|
|
* COPYRIGHT: Copyright 2011 Eric Kohl
|
|
|
|
*/
|
|
|
|
|
2011-08-11 15:06:36 +00:00
|
|
|
#include "precomp.h"
|
2011-05-23 17:54:16 +00:00
|
|
|
|
|
|
|
#define BITS_PER_BYTE 8
|
2011-09-12 14:24:24 +00:00
|
|
|
#define BITMAP_SIZE_INCREMENT 0x400
|
|
|
|
#define BITMAP_SIZE_INVALID_BITS 0x3FF
|
2011-05-23 17:54:16 +00:00
|
|
|
|
|
|
|
typedef struct _COMDB
|
|
|
|
{
|
2011-06-04 21:07:28 +00:00
|
|
|
HANDLE hMutex;
|
2011-05-23 17:54:16 +00:00
|
|
|
HKEY hKey;
|
|
|
|
} COMDB, *PCOMDB;
|
|
|
|
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
LONG
|
|
|
|
WINAPI
|
|
|
|
ComDBClaimNextFreePort(IN HCOMDB hComDB,
|
|
|
|
OUT LPDWORD ComNumber)
|
|
|
|
{
|
|
|
|
PCOMDB pComDB;
|
|
|
|
DWORD dwBitIndex;
|
|
|
|
DWORD dwByteIndex;
|
|
|
|
DWORD dwSize;
|
|
|
|
DWORD dwType;
|
|
|
|
PBYTE pBitmap = NULL;
|
|
|
|
BYTE cMask;
|
|
|
|
LONG lError;
|
|
|
|
|
|
|
|
TRACE("ComDBClaimNextFreePort(%p %p)\n", hComDB, ComNumber);
|
|
|
|
|
|
|
|
if (hComDB == INVALID_HANDLE_VALUE ||
|
|
|
|
hComDB == NULL ||
|
|
|
|
ComNumber == NULL)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
pComDB = (PCOMDB)hComDB;
|
|
|
|
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Wait for the mutex */
|
|
|
|
WaitForSingleObject(pComDB->hMutex, INFINITE);
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
/* Get the required bitmap size */
|
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
|
|
|
NULL,
|
|
|
|
&dwSize);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
2011-06-04 21:07:28 +00:00
|
|
|
{
|
|
|
|
ERR("Failed to query the bitmap size!\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2011-05-25 10:52:12 +00:00
|
|
|
|
|
|
|
/* Allocate the bitmap */
|
|
|
|
pBitmap = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
dwSize);
|
|
|
|
if (pBitmap == NULL)
|
|
|
|
{
|
|
|
|
ERR("Failed to allocate the bitmap!\n");
|
2011-06-04 21:07:28 +00:00
|
|
|
lError = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto done;
|
2011-05-25 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the bitmap */
|
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
|
|
|
pBitmap,
|
|
|
|
&dwSize);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
lError = ERROR_INVALID_PARAMETER;
|
|
|
|
for (dwBitIndex = 0; dwBitIndex < (dwSize * BITS_PER_BYTE); dwBitIndex++)
|
|
|
|
{
|
|
|
|
/* Calculate the byte index and a mask for the affected bit */
|
|
|
|
dwByteIndex = dwBitIndex / BITS_PER_BYTE;
|
|
|
|
cMask = 1 << (dwBitIndex % BITS_PER_BYTE);
|
|
|
|
|
|
|
|
if ((pBitmap[dwByteIndex] & cMask) == 0)
|
|
|
|
{
|
|
|
|
pBitmap[dwByteIndex] |= cMask;
|
|
|
|
*ComNumber = dwBitIndex + 1;
|
|
|
|
lError = ERROR_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the bitmap if it was modified */
|
|
|
|
if (lError == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
lError = RegSetValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
0,
|
|
|
|
REG_BINARY,
|
|
|
|
pBitmap,
|
|
|
|
dwSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:;
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Release the mutex */
|
|
|
|
ReleaseMutex(pComDB->hMutex);
|
|
|
|
|
|
|
|
/* Release the bitmap */
|
2011-05-25 10:52:12 +00:00
|
|
|
if (pBitmap != NULL)
|
|
|
|
HeapFree(GetProcessHeap(), 0, pBitmap);
|
|
|
|
|
|
|
|
return lError;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
LONG
|
|
|
|
WINAPI
|
|
|
|
ComDBClaimPort(IN HCOMDB hComDB,
|
|
|
|
IN DWORD ComNumber,
|
|
|
|
IN BOOL ForceClaim,
|
|
|
|
OUT PBOOL Forced)
|
|
|
|
{
|
|
|
|
PCOMDB pComDB;
|
|
|
|
DWORD dwBitIndex;
|
2011-05-25 10:52:12 +00:00
|
|
|
DWORD dwByteIndex;
|
2011-05-23 17:54:16 +00:00
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
2011-05-25 10:52:12 +00:00
|
|
|
PBYTE pBitmap = NULL;
|
|
|
|
BYTE cMask;
|
2011-05-23 17:54:16 +00:00
|
|
|
LONG lError;
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
TRACE("ComDBClaimPort(%p %lu)\n", hComDB, ComNumber);
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
if (hComDB == INVALID_HANDLE_VALUE ||
|
|
|
|
hComDB == NULL ||
|
|
|
|
ComNumber == 0 ||
|
|
|
|
ComNumber > COMDB_MAX_PORTS_ARBITRATED)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
pComDB = (PCOMDB)hComDB;
|
|
|
|
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Wait for the mutex */
|
|
|
|
WaitForSingleObject(pComDB->hMutex, INFINITE);
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
/* Get the required bitmap size */
|
2011-05-23 17:54:16 +00:00
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
2011-05-25 10:52:12 +00:00
|
|
|
NULL,
|
2011-05-23 17:54:16 +00:00
|
|
|
&dwSize);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
2011-06-04 21:07:28 +00:00
|
|
|
{
|
|
|
|
ERR("Failed to query the bitmap size!\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2011-05-23 17:54:16 +00:00
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
/* Allocate the bitmap */
|
|
|
|
pBitmap = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
dwSize);
|
|
|
|
if (pBitmap == NULL)
|
|
|
|
{
|
|
|
|
ERR("Failed to allocate the bitmap!\n");
|
2011-06-04 21:07:28 +00:00
|
|
|
lError = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto done;
|
2011-05-25 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the bitmap */
|
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
|
|
|
pBitmap,
|
|
|
|
&dwSize);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
/* Get the bit index */
|
|
|
|
dwBitIndex = ComNumber - 1;
|
|
|
|
|
|
|
|
/* Check if the bit to set fits into the bitmap */
|
2011-05-25 10:52:12 +00:00
|
|
|
if (dwBitIndex >= (dwSize * BITS_PER_BYTE))
|
2011-05-23 17:54:16 +00:00
|
|
|
{
|
2011-05-25 10:52:12 +00:00
|
|
|
FIXME("Resize the bitmap\n");
|
|
|
|
|
|
|
|
lError = ERROR_INVALID_PARAMETER;
|
|
|
|
goto done;
|
2011-05-23 17:54:16 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
/* Calculate the byte index and a mask for the affected bit */
|
|
|
|
dwByteIndex = dwBitIndex / BITS_PER_BYTE;
|
2011-05-23 17:54:16 +00:00
|
|
|
cMask = 1 << (dwBitIndex % BITS_PER_BYTE);
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
lError = ERROR_SHARING_VIOLATION;
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
/* Check if the bit is not set */
|
2011-05-25 10:52:12 +00:00
|
|
|
if ((pBitmap[dwByteIndex] & cMask) == 0)
|
2011-05-23 17:54:16 +00:00
|
|
|
{
|
|
|
|
/* Set the bit */
|
2011-05-25 10:52:12 +00:00
|
|
|
pBitmap[dwByteIndex] |= cMask;
|
2011-05-23 17:54:16 +00:00
|
|
|
lError = ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the bitmap if it was modified */
|
|
|
|
if (lError == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
lError = RegSetValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
0,
|
|
|
|
REG_BINARY,
|
2011-05-25 10:52:12 +00:00
|
|
|
pBitmap,
|
|
|
|
dwSize);
|
2011-05-23 17:54:16 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
done:
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Release the mutex */
|
|
|
|
ReleaseMutex(pComDB->hMutex);
|
|
|
|
|
|
|
|
/* Release the bitmap */
|
2011-05-25 10:52:12 +00:00
|
|
|
if (pBitmap != NULL)
|
|
|
|
HeapFree(GetProcessHeap(), 0, pBitmap);
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
return lError;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LONG
|
|
|
|
WINAPI
|
|
|
|
ComDBClose(IN HCOMDB hComDB)
|
|
|
|
{
|
|
|
|
PCOMDB pComDB;
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
TRACE("ComDBClose(%p)\n", hComDB);
|
|
|
|
|
2011-06-04 21:07:28 +00:00
|
|
|
if (hComDB == HCOMDB_INVALID_HANDLE_VALUE ||
|
|
|
|
hComDB == NULL)
|
2011-05-23 17:54:16 +00:00
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
pComDB = (PCOMDB)hComDB;
|
|
|
|
|
|
|
|
/* Close the registry key */
|
|
|
|
if (pComDB->hKey != NULL)
|
|
|
|
RegCloseKey(pComDB->hKey);
|
|
|
|
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Close the mutex */
|
|
|
|
if (pComDB->hMutex != NULL)
|
|
|
|
CloseHandle(pComDB->hMutex);
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
/* Release the database */
|
|
|
|
HeapFree(GetProcessHeap(), 0, pComDB);
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
LONG
|
|
|
|
WINAPI
|
|
|
|
ComDBGetCurrentPortUsage(IN HCOMDB hComDB,
|
|
|
|
OUT PBYTE Buffer,
|
|
|
|
IN DWORD BufferSize,
|
|
|
|
IN DWORD ReportType,
|
|
|
|
OUT LPDWORD MaxPortsReported)
|
|
|
|
{
|
2011-06-05 12:20:52 +00:00
|
|
|
PCOMDB pComDB;
|
|
|
|
DWORD dwBitIndex;
|
|
|
|
DWORD dwByteIndex;
|
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
|
|
|
PBYTE pBitmap = NULL;
|
|
|
|
BYTE cMask;
|
|
|
|
LONG lError = ERROR_SUCCESS;
|
|
|
|
|
2011-09-12 14:24:24 +00:00
|
|
|
TRACE("ComDBGetCurrentPortUsage(%p %p %lu %lu %p)\n",
|
|
|
|
hComDB, Buffer, BufferSize, ReportType, MaxPortsReported);
|
|
|
|
|
2011-06-05 12:20:52 +00:00
|
|
|
if (hComDB == INVALID_HANDLE_VALUE ||
|
|
|
|
hComDB == NULL ||
|
|
|
|
(Buffer == NULL && MaxPortsReported == NULL) ||
|
|
|
|
(ReportType != CDB_REPORT_BITS && ReportType != CDB_REPORT_BYTES))
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ERR("Failed to query the bitmap size!\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the bitmap */
|
|
|
|
pBitmap = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
dwSize);
|
|
|
|
if (pBitmap == NULL)
|
|
|
|
{
|
|
|
|
ERR("Failed to allocate the bitmap!\n");
|
|
|
|
lError = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the bitmap */
|
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
|
|
|
pBitmap,
|
|
|
|
&dwSize);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (Buffer == NULL)
|
|
|
|
{
|
|
|
|
*MaxPortsReported = dwSize * BITS_PER_BYTE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ReportType == CDB_REPORT_BITS)
|
|
|
|
{
|
|
|
|
/* Clear the buffer */
|
|
|
|
memset(Buffer, 0, BufferSize);
|
|
|
|
|
|
|
|
memcpy(Buffer,
|
|
|
|
pBitmap,
|
|
|
|
min(dwSize, BufferSize));
|
|
|
|
|
|
|
|
if (MaxPortsReported != NULL)
|
|
|
|
*MaxPortsReported = min(dwSize, BufferSize) * BITS_PER_BYTE;
|
|
|
|
}
|
|
|
|
else if (ReportType == CDB_REPORT_BYTES)
|
|
|
|
{
|
|
|
|
/* Clear the buffer */
|
|
|
|
memset(Buffer, 0, BufferSize);
|
|
|
|
|
|
|
|
for (dwBitIndex = 0; dwBitIndex < min(dwSize * BITS_PER_BYTE, BufferSize); dwBitIndex++)
|
|
|
|
{
|
|
|
|
/* Calculate the byte index and a mask for the affected bit */
|
|
|
|
dwByteIndex = dwBitIndex / BITS_PER_BYTE;
|
|
|
|
cMask = 1 << (dwBitIndex % BITS_PER_BYTE);
|
|
|
|
|
|
|
|
if ((pBitmap[dwByteIndex] & cMask) != 0)
|
|
|
|
Buffer[dwBitIndex] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:;
|
|
|
|
/* Release the mutex */
|
|
|
|
ReleaseMutex(pComDB->hMutex);
|
|
|
|
|
|
|
|
/* Release the bitmap */
|
|
|
|
HeapFree(GetProcessHeap(), 0, pBitmap);
|
|
|
|
|
|
|
|
return lError;
|
2011-05-25 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
LONG
|
|
|
|
WINAPI
|
|
|
|
ComDBOpen(OUT HCOMDB *phComDB)
|
|
|
|
{
|
|
|
|
PCOMDB pComDB;
|
|
|
|
DWORD dwDisposition;
|
|
|
|
DWORD dwType;
|
2011-05-25 10:52:12 +00:00
|
|
|
DWORD dwSize;
|
|
|
|
PBYTE pBitmap;
|
2011-05-23 17:54:16 +00:00
|
|
|
LONG lError;
|
|
|
|
|
|
|
|
TRACE("ComDBOpen(%p)\n", phComDB);
|
|
|
|
|
|
|
|
/* Allocate a new database */
|
|
|
|
pComDB = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
sizeof(COMDB));
|
|
|
|
if (pComDB == NULL)
|
|
|
|
{
|
2011-05-25 10:52:12 +00:00
|
|
|
ERR("Failed to allocate the database!\n");
|
2011-06-04 21:07:28 +00:00
|
|
|
*phComDB = HCOMDB_INVALID_HANDLE_VALUE;
|
2011-05-23 17:54:16 +00:00
|
|
|
return ERROR_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Create a mutex to protect the database */
|
|
|
|
pComDB->hMutex = CreateMutexW(NULL,
|
|
|
|
FALSE,
|
|
|
|
L"ComDBMutex");
|
|
|
|
if (pComDB->hMutex == NULL)
|
|
|
|
{
|
|
|
|
ERR("Failed to create the mutex!\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, pComDB);
|
|
|
|
*phComDB = HCOMDB_INVALID_HANDLE_VALUE;
|
|
|
|
return ERROR_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the mutex */
|
|
|
|
WaitForSingleObject(pComDB->hMutex, INFINITE);
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
/* Create or open the database key */
|
|
|
|
lError = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"System\\CurrentControlSet\\Control\\COM Name Arbiter",
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
KEY_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
&pComDB->hKey,
|
|
|
|
&dwDisposition);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Get the required bitmap size */
|
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
|
|
|
NULL,
|
2011-05-25 10:52:12 +00:00
|
|
|
&dwSize);
|
2011-05-23 17:54:16 +00:00
|
|
|
if (lError == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
|
|
|
/* Allocate a new bitmap */
|
2011-05-25 10:52:12 +00:00
|
|
|
dwSize = COMDB_MIN_PORTS_ARBITRATED / BITS_PER_BYTE;
|
|
|
|
pBitmap = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
dwSize);
|
|
|
|
if (pBitmap == NULL)
|
2011-05-23 17:54:16 +00:00
|
|
|
{
|
2011-05-25 10:52:12 +00:00
|
|
|
ERR("Failed to allocate the bitmap!\n");
|
2011-05-23 17:54:16 +00:00
|
|
|
lError = ERROR_ACCESS_DENIED;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
/* Write the bitmap to the registry */
|
2011-05-23 17:54:16 +00:00
|
|
|
lError = RegSetValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
0,
|
|
|
|
REG_BINARY,
|
2011-05-25 10:52:12 +00:00
|
|
|
pBitmap,
|
|
|
|
dwSize);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pBitmap);
|
2011-05-23 17:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:;
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Release the mutex */
|
|
|
|
ReleaseMutex(pComDB->hMutex);
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
if (lError != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Clean up in case of failure */
|
|
|
|
if (pComDB->hKey != NULL)
|
|
|
|
RegCloseKey(pComDB->hKey);
|
|
|
|
|
2011-06-04 21:07:28 +00:00
|
|
|
if (pComDB->hMutex != NULL)
|
|
|
|
CloseHandle(pComDB->hMutex);
|
|
|
|
|
2011-05-23 17:54:16 +00:00
|
|
|
HeapFree(GetProcessHeap(), 0, pComDB);
|
|
|
|
|
|
|
|
*phComDB = HCOMDB_INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Return the database handle */
|
|
|
|
*phComDB = (HCOMDB)pComDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("done (Error %lu)\n", lError);
|
|
|
|
|
|
|
|
return lError;
|
|
|
|
}
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
|
|
|
|
LONG
|
|
|
|
WINAPI
|
|
|
|
ComDBReleasePort(IN HCOMDB hComDB,
|
|
|
|
IN DWORD ComNumber)
|
|
|
|
{
|
|
|
|
PCOMDB pComDB;
|
|
|
|
DWORD dwByteIndex;
|
|
|
|
DWORD dwBitIndex;
|
|
|
|
DWORD dwType;
|
|
|
|
DWORD dwSize;
|
|
|
|
PBYTE pBitmap = NULL;
|
|
|
|
BYTE cMask;
|
|
|
|
LONG lError;
|
|
|
|
|
|
|
|
TRACE("ComDBReleasePort(%p %lu)\n", hComDB, ComNumber);
|
|
|
|
|
|
|
|
if (hComDB == INVALID_HANDLE_VALUE ||
|
|
|
|
ComNumber == 0 ||
|
|
|
|
ComNumber > COMDB_MAX_PORTS_ARBITRATED)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
pComDB = (PCOMDB)hComDB;
|
|
|
|
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Wait for the mutex */
|
|
|
|
WaitForSingleObject(pComDB->hMutex, INFINITE);
|
|
|
|
|
2011-05-25 10:52:12 +00:00
|
|
|
/* Get the required bitmap size */
|
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
|
|
|
NULL,
|
|
|
|
&dwSize);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
2011-06-04 21:07:28 +00:00
|
|
|
{
|
|
|
|
ERR("Failed to query the bitmap size!\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2011-05-25 10:52:12 +00:00
|
|
|
|
|
|
|
/* Allocate the bitmap */
|
|
|
|
pBitmap = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
dwSize);
|
|
|
|
if (pBitmap == NULL)
|
|
|
|
{
|
|
|
|
ERR("Failed to allocate the bitmap!\n");
|
2011-06-04 21:07:28 +00:00
|
|
|
lError = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto done;
|
2011-05-25 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the bitmap */
|
|
|
|
lError = RegQueryValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
NULL,
|
|
|
|
&dwType,
|
|
|
|
pBitmap,
|
|
|
|
&dwSize);
|
|
|
|
if (lError != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Get the bit index */
|
|
|
|
dwBitIndex = ComNumber - 1;
|
|
|
|
|
|
|
|
/* Check if the bit to set fits into the bitmap */
|
|
|
|
if (dwBitIndex >= (dwSize * BITS_PER_BYTE))
|
|
|
|
{
|
|
|
|
lError = ERROR_INVALID_PARAMETER;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the byte index and a mask for the affected bit */
|
|
|
|
dwByteIndex = dwBitIndex / BITS_PER_BYTE;
|
|
|
|
cMask = 1 << (dwBitIndex % BITS_PER_BYTE);
|
|
|
|
|
|
|
|
/* Release the port */
|
|
|
|
pBitmap[dwByteIndex] &= ~cMask;
|
|
|
|
|
|
|
|
lError = RegSetValueExW(pComDB->hKey,
|
|
|
|
L"ComDB",
|
|
|
|
0,
|
|
|
|
REG_BINARY,
|
|
|
|
pBitmap,
|
|
|
|
dwSize);
|
|
|
|
|
|
|
|
done:;
|
2011-06-04 21:07:28 +00:00
|
|
|
/* Release the mutex */
|
|
|
|
ReleaseMutex(pComDB->hMutex);
|
|
|
|
|
|
|
|
/* Release the bitmap */
|
2011-05-25 10:52:12 +00:00
|
|
|
if (pBitmap != NULL)
|
|
|
|
HeapFree(GetProcessHeap(), 0, pBitmap);
|
|
|
|
|
|
|
|
return lError;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LONG
|
|
|
|
WINAPI
|
|
|
|
ComDBResizeDatabase(IN HCOMDB hComDB,
|
|
|
|
IN DWORD NewSize)
|
|
|
|
{
|
2011-09-12 14:24:24 +00:00
|
|
|
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;
|
2011-05-25 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 18:22:24 +00:00
|
|
|
/* EOF */
|