FreeRDP
dsparse.c
1 
20 #include <winpr/config.h>
21 
22 #include <winpr/dsparse.h>
23 #include <winpr/assert.h>
24 
45 #if !defined(_WIN32) || defined(_UWP)
46 
47 DWORD DsMakeSpnW(LPCWSTR ServiceClass, LPCWSTR ServiceName, LPCWSTR InstanceName,
48  USHORT InstancePort, LPCWSTR Referrer, DWORD* pcSpnLength, LPWSTR pszSpn)
49 {
50  DWORD res = ERROR_OUTOFMEMORY;
51  char* ServiceClassA = NULL;
52  char* ServiceNameA = NULL;
53  char* InstanceNameA = NULL;
54  char* ReferrerA = NULL;
55  char* pszSpnA = NULL;
56  size_t length = 0;
57 
58  WINPR_ASSERT(ServiceClass);
59  WINPR_ASSERT(ServiceName);
60  WINPR_ASSERT(pcSpnLength);
61 
62  length = *pcSpnLength;
63  if ((length > 0) && pszSpn)
64  pszSpnA = calloc(length + 1, sizeof(char));
65 
66  if (ServiceClass)
67  {
68  ServiceClassA = ConvertWCharToUtf8Alloc(ServiceClass, NULL);
69  if (!ServiceClassA)
70  goto fail;
71  }
72  if (ServiceName)
73  {
74  ServiceNameA = ConvertWCharToUtf8Alloc(ServiceName, NULL);
75  if (!ServiceNameA)
76  goto fail;
77  }
78  if (InstanceName)
79  {
80  InstanceNameA = ConvertWCharToUtf8Alloc(InstanceName, NULL);
81  if (!InstanceNameA)
82  goto fail;
83  }
84  if (Referrer)
85  {
86  ReferrerA = ConvertWCharToUtf8Alloc(Referrer, NULL);
87  if (!ReferrerA)
88  goto fail;
89  }
90  res = DsMakeSpnA(ServiceClassA, ServiceNameA, InstanceNameA, InstancePort, ReferrerA,
91  pcSpnLength, pszSpnA);
92 
93  if (res == ERROR_SUCCESS)
94  {
95  if (ConvertUtf8NToWChar(pszSpnA, *pcSpnLength, pszSpn, length) < 0)
96  res = ERROR_OUTOFMEMORY;
97  }
98 
99 fail:
100  free(ServiceClassA);
101  free(ServiceNameA);
102  free(InstanceNameA);
103  free(ReferrerA);
104  free(pszSpnA);
105  return res;
106 }
107 
108 DWORD DsMakeSpnA(LPCSTR ServiceClass, LPCSTR ServiceName, LPCSTR InstanceName, USHORT InstancePort,
109  LPCSTR Referrer, DWORD* pcSpnLength, LPSTR pszSpn)
110 {
111  DWORD SpnLength = 0;
112  DWORD ServiceClassLength = 0;
113  DWORD ServiceNameLength = 0;
114 
115  WINPR_ASSERT(ServiceClass);
116  WINPR_ASSERT(ServiceName);
117  WINPR_ASSERT(pcSpnLength);
118 
119  WINPR_UNUSED(InstanceName);
120  WINPR_UNUSED(InstancePort);
121  WINPR_UNUSED(Referrer);
122 
123  if ((*pcSpnLength != 0) && (pszSpn == NULL))
124  return ERROR_INVALID_PARAMETER;
125 
126  ServiceClassLength = (DWORD)strlen(ServiceClass);
127  ServiceNameLength = (DWORD)strlen(ServiceName);
128 
129  SpnLength = ServiceClassLength + 1 + ServiceNameLength + 1;
130 
131  if ((*pcSpnLength == 0) || (*pcSpnLength < SpnLength))
132  {
133  *pcSpnLength = SpnLength;
134  return ERROR_BUFFER_OVERFLOW;
135  }
136 
137  (void)sprintf_s(pszSpn, *pcSpnLength, "%s/%s", ServiceClass, ServiceName);
138 
139  return ERROR_SUCCESS;
140 }
141 
142 #endif