+static
+NET_API_STATUS
+BuildWorkstationsList(
+ _Out_ PWSTR *pWorkstationsList,
+ _In_ PWSTR pRaw)
+{
+ BOOL isLastSep, isSep;
+ INT i, j;
+ WCHAR c;
+ INT nLength = 0;
+ INT nArgs = 0;
+ INT nRawLength;
+ PWSTR pList;
+
+ /* Check for invalid characters in the raw string */
+ if (wcspbrk(pRaw, L"/[]=?\\+:.") != NULL)
+ return 3952;
+
+ /* Count the number of workstations in the list and
+ * the required buffer size */
+ isLastSep = FALSE;
+ isSep = FALSE;
+ nRawLength = wcslen(pRaw);
+ for (i = 0; i < nRawLength; i++)
+ {
+ c = pRaw[i];
+ if (c == L',' || c == L';')
+ isSep = TRUE;
+
+ if (isSep == TRUE)
+ {
+ if ((isLastSep == FALSE) && (i != 0) && (i != nRawLength - 1))
+ nLength++;
+ }
+ else
+ {
+ nLength++;
+
+ if (isLastSep == TRUE || (isLastSep == FALSE && i == 0))
+ nArgs++;
+ }
+
+ isLastSep = isSep;
+ isSep = FALSE;
+ }
+
+ nLength++;
+
+ /* Leave, if there are no workstations in the list */
+ if (nArgs == 0)
+ {
+ pWorkstationsList = NULL;
+ return NERR_Success;
+ }
+
+ /* Fail if there are more than eight workstations in the list */
+ if (nArgs > 8)
+ return 3951;
+
+ /* Allocate the buffer for the clean workstation list */
+ pList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength * sizeof(WCHAR));
+ if (pList == NULL)
+ return ERROR_NOT_ENOUGH_MEMORY;
+
+ /* Build the clean workstation list */
+ isLastSep = FALSE;
+ isSep = FALSE;
+ nRawLength = wcslen(pRaw);
+ for (i = 0, j = 0; i < nRawLength; i++)
+ {
+ c = pRaw[i];
+ if (c == L',' || c == L';')
+ isSep = TRUE;
+
+ if (isSep == TRUE)
+ {
+ if ((isLastSep == FALSE) && (i != 0) && (i != nRawLength - 1))
+ {
+ pList[j] = L',';
+ j++;
+ }
+ }
+ else
+ {
+ pList[j] = c;
+ j++;
+
+ if (isLastSep == TRUE || (isLastSep == FALSE && i == 0))
+ nArgs++;
+ }
+
+ isLastSep = isSep;
+ isSep = FALSE;
+ }
+
+ *pWorkstationsList = pList;
+
+ return NERR_Success;
+}
+
+