2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS DNS Shared Library
4 * FILE: lib/dnslib/string.c
5 * PURPOSE: functions for string manipulation and conversion.
8 /* INCLUDES ******************************************************************/
11 /* DATA **********************************************************************/
13 /* FUNCTIONS *****************************************************************/
17 Dns_StringCopy(OUT PVOID Destination
,
18 IN OUT PULONG DestinationSize
,
20 IN ULONG StringSize OPTIONAL
,
27 /* Check if the caller already gave us the string size */
30 /* He didn't, get the input type */
31 if (InputType
== UnicodeString
)
33 /* Unicode string, calculate the size */
34 StringSize
= (ULONG
)wcslen((LPWSTR
)String
);
38 /* ANSI or UTF-8 sting, get the size */
39 StringSize
= (ULONG
)strlen((LPSTR
)String
);
43 /* Check if we have a limit on the desination size */
46 /* Make sure that we can respect it */
47 DestSize
= Dns_GetBufferLengthForStringCopy(String
,
51 if (*DestinationSize
< DestSize
)
53 /* Fail due to missing buffer space */
54 SetLastError(ERROR_MORE_DATA
);
56 /* Return how much data we actually need */
57 *DestinationSize
= DestSize
;
62 /* Fail due to invalid data */
63 SetLastError(ERROR_INVALID_DATA
);
67 /* Return how much data we actually need */
68 *DestinationSize
= DestSize
;
71 /* Now check if this is a Unicode String as input */
72 if (InputType
== UnicodeString
)
74 /* Check if the output is ANSI */
75 if (OutputType
== AnsiString
)
77 /* Convert and return the final desination size */
78 OutputSize
= WideCharToMultiByte(CP_ACP
,
87 else if (OutputType
== UnicodeString
)
90 StringSize
= StringSize
* sizeof(WCHAR
);
91 RtlMoveMemory(Destination
, String
, StringSize
);
93 /* Return output length */
94 OutputSize
= StringSize
+ 2;
96 else if (OutputType
== Utf8String
)
102 else if (InputType
== AnsiString
)
104 /* It's ANSI, is the output ansi too? */
105 if (OutputType
== AnsiString
)
107 /* Copy the string */
108 RtlMoveMemory(Destination
, String
, StringSize
);
110 /* Return output length */
111 OutputSize
= StringSize
+ 1;
113 else if (OutputType
== UnicodeString
)
115 /* Convert to Unicode and return size */
116 OutputSize
= MultiByteToWideChar(CP_ACP
,
121 -1) * sizeof(WCHAR
) + 2;
123 else if (OutputType
== Utf8String
)
129 else if (InputType
== Utf8String
)
135 /* Return the output size */
141 Dns_CreateStringCopy_W(IN LPWSTR Name
)
146 /* Make sure that we have a name */
150 SetLastError(ERROR_INVALID_PARAMETER
);
154 /* Find out the size of the string */
155 StringLength
= (wcslen(Name
) + 1) * sizeof(WCHAR
);
157 /* Allocate space for the copy */
158 NameCopy
= Dns_AllocZero(StringLength
);
162 RtlCopyMemory(NameCopy
, Name
, StringLength
);
167 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
170 /* Return the copy */
176 Dns_GetBufferLengthForStringCopy(IN PVOID String
,
177 IN ULONG Size OPTIONAL
,
181 ULONG OutputSize
= 0;
183 /* Check what kind of string this is */
184 if (InputType
== UnicodeString
)
186 /* Check if we have a size */
189 /* Get it ourselves */
190 Size
= (ULONG
)wcslen(String
);
193 /* Check the output type */
194 if (OutputType
== UnicodeString
)
196 /* Convert the size to bytes */
197 OutputSize
= (Size
+ 1) * sizeof(WCHAR
);
199 else if (OutputType
== Utf8String
)
206 /* Find out how much it will be in ANSI bytes */
207 OutputSize
= WideCharToMultiByte(CP_ACP
,
217 else if (InputType
== AnsiString
)
219 /* Check if we have a size */
222 /* Get it ourselves */
223 Size
= (ULONG
)strlen(String
);
226 /* Check the output type */
227 if (OutputType
== AnsiString
)
229 /* Just add a byte for the null char */
230 OutputSize
= Size
+ 1;
232 else if (OutputType
== UnicodeString
)
234 /* Calculate the bytes for a Unicode string */
235 OutputSize
= (MultiByteToWideChar(CP_ACP
,
240 0) + 1) * sizeof(WCHAR
);
242 else if (OutputType
== Utf8String
)
248 else if (InputType
== Utf8String
)
254 /* Return the size required */