summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/path/include/PathAllocCombine.c
blob: abdbd293e98e7181bc59a52d721520667800a169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
#define DEFINE_UNICODE		FALSE
#define CUR_PATH_SEPARATOR_CHR	'\\'
#define CUR_PATH_SEPARATOR_STR	"\\"
#define PATH_ALLOC_COMBINE	PathAllocCombineA
*/

/**
 * FIXME: These implementations of the PathAllocCombine functions have
 * several issues:
 * - pszPathIn or pszMore may be NULL (but not both)
 * - no check if pszMore is fully qualified (if so, it must be directly
 *   copied to the output buffer without being combined with pszPathIn.
 * - if pszMore begins with a _single_ backslash it must be combined with
 *   only the root of the path pointed to by pszPathIn and there's no code
 *   to extract the root of pszPathIn.
 * - the function will crash with some short string lengths of the parameters
 */

#if DEFINE_UNICODE

HRESULT PATH_ALLOC_COMBINE(PCWSTR pszPathIn, PCWSTR pszMore, unsigned long dwFlags,
                           PWSTR* ppszPathOut)
{
	PWSTR pszPathOut;
	BOOL backslashIn;
	BOOL backslashMore;
	size_t pszMoreLength;
	size_t pszPathInLength;
	size_t pszPathOutLength;
	WLog_WARN(TAG, "has known bugs and needs fixing.");

	if (!ppszPathOut)
		return E_INVALIDARG;

	if (!pszPathIn && !pszMore)
		return E_INVALIDARG;

	if (!pszMore)
		return E_FAIL; /* valid but not implemented, see top comment */

	if (!pszPathIn)
		return E_FAIL; /* valid but not implemented, see top comment */

	pszPathInLength = _wcslen(pszPathIn);
	pszMoreLength = _wcslen(pszMore);

	/* prevent segfaults - the complete implementation below is buggy */
	if (pszPathInLength < 3)
		return E_FAIL;

	backslashIn = (pszPathIn[pszPathInLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
	backslashMore = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;

	if (backslashMore)
	{
		if ((pszPathIn[1] == ':') && (pszPathIn[2] == CUR_PATH_SEPARATOR_CHR))
		{
			const WCHAR colon[] = { ':', '\0' };
			size_t sizeOfBuffer;
			pszPathOutLength = sizeof(WCHAR) + pszMoreLength;
			sizeOfBuffer = (pszPathOutLength + 1) * sizeof(WCHAR);
			pszPathOut = (PWSTR)calloc(sizeOfBuffer, sizeof(WCHAR));

			if (!pszPathOut)
				return E_OUTOFMEMORY;

			_wcsncat(pszPathOut, &pszPathIn[0], 1);
			_wcsncat(pszPathOut, colon, ARRAYSIZE(colon));
			_wcsncat(pszPathOut, pszMore, pszMoreLength);
			*ppszPathOut = pszPathOut;
			return S_OK;
		}
	}
	else
	{
		const WCHAR sep[] = CUR_PATH_SEPARATOR_STR;
		size_t sizeOfBuffer;
		pszPathOutLength = pszPathInLength + pszMoreLength;
		sizeOfBuffer = (pszPathOutLength + 1) * 2;
		pszPathOut = (PWSTR)calloc(sizeOfBuffer, 2);

		if (!pszPathOut)
			return E_OUTOFMEMORY;

		_wcsncat(pszPathOut, pszPathIn, pszPathInLength);
		if (!backslashIn)
			_wcsncat(pszPathOut, sep, ARRAYSIZE(sep));
		_wcsncat(pszPathOut, pszMore, pszMoreLength);

		*ppszPathOut = pszPathOut;
		return S_OK;
	}

	return E_FAIL;
}

#else

HRESULT PATH_ALLOC_COMBINE(PCSTR pszPathIn, PCSTR pszMore, unsigned long dwFlags, PSTR* ppszPathOut)
{
	PSTR pszPathOut;
	BOOL backslashIn;
	BOOL backslashMore;
	int pszMoreLength;
	int pszPathInLength;
	int pszPathOutLength;
	WLog_WARN(TAG, "has known bugs and needs fixing.");

	if (!ppszPathOut)
		return E_INVALIDARG;

	if (!pszPathIn && !pszMore)
		return E_INVALIDARG;

	if (!pszMore)
		return E_FAIL; /* valid but not implemented, see top comment */

	if (!pszPathIn)
		return E_FAIL; /* valid but not implemented, see top comment */

	pszPathInLength = strlen(pszPathIn);
	pszMoreLength = strlen(pszMore);

	/* prevent segfaults - the complete implementation below is buggy */
	if (pszPathInLength < 3)
		return E_FAIL;

	backslashIn = (pszPathIn[pszPathInLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
	backslashMore = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;

	if (backslashMore)
	{
		if ((pszPathIn[1] == ':') && (pszPathIn[2] == CUR_PATH_SEPARATOR_CHR))
		{
			size_t sizeOfBuffer;
			pszPathOutLength = 2 + pszMoreLength;
			sizeOfBuffer = (pszPathOutLength + 1) * 2;
			pszPathOut = (PSTR)calloc(sizeOfBuffer, 2);

			if (!pszPathOut)
				return E_OUTOFMEMORY;

			sprintf_s(pszPathOut, sizeOfBuffer, "%c:%s", pszPathIn[0], pszMore);
			*ppszPathOut = pszPathOut;
			return S_OK;
		}
	}
	else
	{
		size_t sizeOfBuffer;
		pszPathOutLength = pszPathInLength + pszMoreLength;
		sizeOfBuffer = (pszPathOutLength + 1) * 2;
		pszPathOut = (PSTR)calloc(sizeOfBuffer, 2);

		if (!pszPathOut)
			return E_OUTOFMEMORY;

		if (backslashIn)
			sprintf_s(pszPathOut, sizeOfBuffer, "%s%s", pszPathIn, pszMore);
		else
			sprintf_s(pszPathOut, sizeOfBuffer, "%s" CUR_PATH_SEPARATOR_STR "%s", pszPathIn,
			          pszMore);

		*ppszPathOut = pszPathOut;
		return S_OK;
	}

	return E_FAIL;
}

#endif

/*
#undef DEFINE_UNICODE
#undef CUR_PATH_SEPARATOR_CHR
#undef CUR_PATH_SEPARATOR_STR
#undef PATH_ALLOC_COMBINE
*/