xref: /reactos/win32ss/printing/base/spoolss/ports.c (revision c2c66aff)
1 /*
2  * PROJECT:     ReactOS Spooler Router
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Functions related to Ports of the Print Monitors
5  * COPYRIGHT:   Copyright 2015-2017 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 BOOL WINAPI
11 EnumPortsW(PWSTR pName, DWORD Level, PBYTE pPorts, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
12 {
13     BOOL bReturnValue = TRUE;
14     DWORD cbCallBuffer;
15     DWORD cbNeeded;
16     DWORD dwReturned;
17     PBYTE pCallBuffer;
18     PSPOOLSS_PRINT_PROVIDER pPrintProvider;
19     PLIST_ENTRY pEntry;
20 
21     // Sanity checks.
22     if (cbBuf && !pPorts)
23     {
24         SetLastError(ERROR_INVALID_USER_BUFFER);
25         return FALSE;
26     }
27 
28     // Begin counting.
29     *pcbNeeded = 0;
30     *pcReturned = 0;
31 
32     // At the beginning, we have the full buffer available.
33     cbCallBuffer = cbBuf;
34     pCallBuffer = pPorts;
35 
36     // Loop through all Print Provider.
37     for (pEntry = PrintProviderList.Flink; pEntry != &PrintProviderList; pEntry = pEntry->Flink)
38     {
39         pPrintProvider = CONTAINING_RECORD(pEntry, SPOOLSS_PRINT_PROVIDER, Entry);
40 
41         // Call the EnumPorts function of this Print Provider.
42         cbNeeded = 0;
43         dwReturned = 0;
44         bReturnValue = pPrintProvider->PrintProvider.fpEnumPorts(pName, Level, pCallBuffer, cbCallBuffer, &cbNeeded, &dwReturned);
45 
46         // Add the returned counts to the total values.
47         *pcbNeeded += cbNeeded;
48         *pcReturned += dwReturned;
49 
50         // Reduce the available buffer size for the next call without risking an underflow.
51         if (cbNeeded < cbCallBuffer)
52             cbCallBuffer -= cbNeeded;
53         else
54             cbCallBuffer = 0;
55 
56         // Advance the buffer if the caller provided it.
57         if (pCallBuffer)
58             pCallBuffer += cbNeeded;
59 
60         // Check if we shall not ask other Print Providers.
61         if (bReturnValue == ROUTER_STOP_ROUTING)
62             break;
63     }
64 
65     return bReturnValue;
66 }
67