WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context) { WCHAR* nextToken; WCHAR value; if (!strToken) strToken = *context; Data_Read_UINT16(strToken, value); while (*strToken && _wcschr(strDelimit, value)) { strToken++; Data_Read_UINT16(strToken, value); } if (!*strToken) return NULL; nextToken = strToken++; Data_Read_UINT16(strToken, value); while (*strToken && !(_wcschr(strDelimit, value))) { strToken++; Data_Read_UINT16(strToken, value); } if (*strToken) *strToken++ = 0; *context = strToken; return nextToken; }
WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context) { WCHAR* nextToken; if (!strToken) strToken = *context; while (*strToken && _wcschr(strDelimit, *strToken)) strToken++; if (!*strToken) return NULL; nextToken = strToken++; while (*strToken && !(_wcschr(strDelimit, *strToken))) strToken++; if (*strToken) *strToken++ = 0; *context = strToken; return nextToken; }
BOOL freerdp_client_parse_rdp_file_buffer_unicode(rdpFile* file, BYTE* buffer, size_t size) { int length; WCHAR* line; WCHAR* type; WCHAR* context; WCHAR *d1, *d2; WCHAR *beg, *end; WCHAR *name, *value; line = wcstok_s((WCHAR*) buffer, CR_LF_STR_W, &context); while (line != NULL) { length = _wcslen(line); if (length > 1) { beg = line; end = &line[length - 1]; if (beg[0] == '/') { /* FreeRDP option */ freerdp_client_parse_rdp_file_option_unicode(file, line); goto next_line; } d1 = _wcschr(line, ':'); if (!d1) goto next_line; /* not first delimiter */ type = &d1[1]; d2 = _wcschr(type, ':'); if (!d2) goto next_line; /* no second delimiter */ if ((d2 - d1) != 2) goto next_line; /* improper type length */ if (d2 == end) goto next_line; /* no value */ *d1 = 0; *d2 = 0; name = beg; value = &d2[1]; if (*type == 'i') { /* integer type */ freerdp_client_parse_rdp_file_integer_unicode(file, name, value); } else if (*type == 's') { /* string type */ freerdp_client_parse_rdp_file_string_unicode(file, name, value); } else if (*type == 'b') { /* binary type */ } } next_line: line = wcstok_s(NULL, CR_LF_STR_W, &context); } return TRUE; }
static BOOL freerdp_client_parse_rdp_file_buffer_unicode(rdpFile* file, const BYTE* buffer, size_t size) { BOOL rc = FALSE; int index; int length; const WCHAR* line; WCHAR* type; WCHAR* context; WCHAR* d1, *d2; const WCHAR* name, *value; WCHAR* copy = (WCHAR*)calloc(1, size + sizeof(WCHAR)); if (!copy) return FALSE; memcpy(copy, buffer, size); index = 0; line = wcstok_s(copy, CR_LF_STR_W, &context); while (line != NULL) { length = (int) _wcslen(line); if (length > 1) { const WCHAR* beg = line; if (!freerdp_client_parse_rdp_file_add_line_unicode(file, line, index)) goto fail; if (beg[0] == '/') { /* FreeRDP option */ freerdp_client_parse_rdp_file_option_unicode(file, line, index); goto next_line; } d1 = _wcschr(line, ':'); if (!d1) goto next_line; /* not first delimiter */ type = &d1[1]; d2 = _wcschr(type, ':'); if (!d2) goto next_line; /* no second delimiter */ if ((d2 - d1) != 2) goto next_line; /* improper type length */ *d1 = 0; *d2 = 0; name = beg; value = &d2[1]; if (*type == 'i') { /* integer type */ if (!freerdp_client_parse_rdp_file_integer_unicode(file, name, value, index)) goto fail; } else if (*type == 's') { /* string type */ if (!freerdp_client_parse_rdp_file_string_unicode(file, name, value, index)) goto fail; } else if (*type == 'b') { /* binary type */ } } next_line: line = wcstok_s(NULL, CR_LF_STR_W, &context); index++; } rc = TRUE; fail: free(copy); return rc; }
int TestString(int argc, char* argv[]) { WCHAR* p; size_t pos; size_t length; WCHAR* context; /* _wcslen */ length = _wcslen(testStringW); if (length != testStringW_Length) { printf("_wcslen error: length mismatch: Actual: %lu, Expected: %lu\n", (unsigned long) length, (unsigned long) testStringW_Length); return -1; } /* _wcschr */ p = _wcschr(testStringW, 'r'); pos = (p - testStringW); if (pos != 11) { printf("_wcschr error: position mismatch: Actual: %lu, Expected: %u\n", (unsigned long)pos, 11); return -1; } p = _wcschr(&testStringW[pos + 1], 'r'); pos = (p - testStringW); if (pos != 29) { printf("_wcschr error: position mismatch: Actual: %lu, Expected: %u\n", (unsigned long)pos, 29); return -1; } p = _wcschr(&testStringW[pos + 1], 'r'); if (p != NULL) { printf("_wcschr error: return value mismatch: Actual: 0x%08lX, Expected: 0x%08lX\n", (unsigned long) p, (unsigned long) NULL); return -1; } /* wcstok_s */ p = wcstok_s(testTokensW, testDelimiter, &context); if (memcmp(p, testToken1W, sizeof(testToken1W)) != 0) { printf("wcstok_s error: token #1 mismatch\n"); return -1; } p = wcstok_s(NULL, testDelimiter, &context); if (memcmp(p, testToken2W, sizeof(testToken2W)) != 0) { printf("wcstok_s error: token #2 mismatch\n"); return -1; } p = wcstok_s(NULL, testDelimiter, &context); if (memcmp(p, testToken3W, sizeof(testToken3W)) != 0) { printf("wcstok_s error: token #3 mismatch\n"); return -1; } p = wcstok_s(NULL, testDelimiter, &context); if (p != NULL) { printf("wcstok_s error: return value is not NULL\n"); return -1; } return 0; }