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 */
259 * COPYRIGHT: See COPYING in the top level directory
260 * PROJECT: ReactOS DNS Shared Library
261 * FILE: lib/dnslib/string.c
262 * PURPOSE: functions for string manipulation and conversion.
265 /* INCLUDES ******************************************************************/
268 /* DATA **********************************************************************/
270 /* FUNCTIONS *****************************************************************/
274 Dns_StringCopy(OUT PVOID Destination
,
275 IN OUT PULONG DestinationSize
,
277 IN ULONG StringSize OPTIONAL
,
282 ULONG OutputSize
= 0;
284 /* Check if the caller already gave us the string size */
287 /* He didn't, get the input type */
288 if (InputType
== UnicodeString
)
290 /* Unicode string, calculate the size */
291 StringSize
= (ULONG
)wcslen((LPWSTR
)String
);
295 /* ANSI or UTF-8 sting, get the size */
296 StringSize
= (ULONG
)strlen((LPSTR
)String
);
300 /* Check if we have a limit on the desination size */
303 /* Make sure that we can respect it */
304 DestSize
= Dns_GetBufferLengthForStringCopy(String
,
308 if (*DestinationSize
< DestSize
)
310 /* Fail due to missing buffer space */
311 SetLastError(ERROR_MORE_DATA
);
313 /* Return how much data we actually need */
314 *DestinationSize
= DestSize
;
319 /* Fail due to invalid data */
320 SetLastError(ERROR_INVALID_DATA
);
324 /* Return how much data we actually need */
325 *DestinationSize
= DestSize
;
328 /* Now check if this is a Unicode String as input */
329 if (InputType
== UnicodeString
)
331 /* Check if the output is ANSI */
332 if (OutputType
== AnsiString
)
334 /* Convert and return the final desination size */
335 OutputSize
= WideCharToMultiByte(CP_ACP
,
344 else if (OutputType
== UnicodeString
)
346 /* Copy the string */
347 StringSize
= StringSize
* sizeof(WCHAR
);
348 RtlMoveMemory(Destination
, String
, StringSize
);
350 /* Return output length */
351 OutputSize
= StringSize
+ 2;
353 else if (OutputType
== Utf8String
)
359 else if (InputType
== AnsiString
)
361 /* It's ANSI, is the output ansi too? */
362 if (OutputType
== AnsiString
)
364 /* Copy the string */
365 RtlMoveMemory(Destination
, String
, StringSize
);
367 /* Return output length */
368 OutputSize
= StringSize
+ 1;
370 else if (OutputType
== UnicodeString
)
372 /* Convert to Unicode and return size */
373 OutputSize
= MultiByteToWideChar(CP_ACP
,
378 -1) * sizeof(WCHAR
) + 2;
380 else if (OutputType
== Utf8String
)
386 else if (InputType
== Utf8String
)
392 /* Return the output size */
398 Dns_CreateStringCopy_W(IN LPWSTR Name
)
403 /* Make sure that we have a name */
407 SetLastError(ERROR_INVALID_PARAMETER
);
411 /* Find out the size of the string */
412 StringLength
= (wcslen(Name
) + 1) * sizeof(WCHAR
);
414 /* Allocate space for the copy */
415 NameCopy
= Dns_AllocZero(StringLength
);
419 RtlCopyMemory(NameCopy
, Name
, StringLength
);
424 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
427 /* Return the copy */
433 Dns_GetBufferLengthForStringCopy(IN PVOID String
,
434 IN ULONG Size OPTIONAL
,
438 ULONG OutputSize
= 0;
440 /* Check what kind of string this is */
441 if (InputType
== UnicodeString
)
443 /* Check if we have a size */
446 /* Get it ourselves */
447 Size
= (ULONG
)wcslen(String
);
450 /* Check the output type */
451 if (OutputType
== UnicodeString
)
453 /* Convert the size to bytes */
454 OutputSize
= (Size
+ 1) * sizeof(WCHAR
);
456 else if (OutputType
== Utf8String
)
463 /* Find out how much it will be in ANSI bytes */
464 OutputSize
= WideCharToMultiByte(CP_ACP
,
474 else if (InputType
== AnsiString
)
476 /* Check if we have a size */
479 /* Get it ourselves */
480 Size
= (ULONG
)strlen(String
);
483 /* Check the output type */
484 if (OutputType
== AnsiString
)
486 /* Just add a byte for the null char */
487 OutputSize
= Size
+ 1;
489 else if (OutputType
== UnicodeString
)
491 /* Calculate the bytes for a Unicode string */
492 OutputSize
= (MultiByteToWideChar(CP_ACP
,
497 0) + 1) * sizeof(WCHAR
);
499 else if (OutputType
== Utf8String
)
505 else if (InputType
== Utf8String
)
511 /* Return the size required */
516 * COPYRIGHT: See COPYING in the top level directory
517 * PROJECT: ReactOS DNS Shared Library
518 * FILE: lib/dnslib/string.c
519 * PURPOSE: functions for string manipulation and conversion.
522 /* INCLUDES ******************************************************************/
525 /* DATA **********************************************************************/
527 /* FUNCTIONS *****************************************************************/
531 Dns_StringCopy(OUT PVOID Destination
,
532 IN OUT PULONG DestinationSize
,
534 IN ULONG StringSize OPTIONAL
,
539 ULONG OutputSize
= 0;
541 /* Check if the caller already gave us the string size */
544 /* He didn't, get the input type */
545 if (InputType
== UnicodeString
)
547 /* Unicode string, calculate the size */
548 StringSize
= (ULONG
)wcslen((LPWSTR
)String
);
552 /* ANSI or UTF-8 sting, get the size */
553 StringSize
= (ULONG
)strlen((LPSTR
)String
);
557 /* Check if we have a limit on the desination size */
560 /* Make sure that we can respect it */
561 DestSize
= Dns_GetBufferLengthForStringCopy(String
,
565 if (*DestinationSize
< DestSize
)
567 /* Fail due to missing buffer space */
568 SetLastError(ERROR_MORE_DATA
);
570 /* Return how much data we actually need */
571 *DestinationSize
= DestSize
;
576 /* Fail due to invalid data */
577 SetLastError(ERROR_INVALID_DATA
);
581 /* Return how much data we actually need */
582 *DestinationSize
= DestSize
;
585 /* Now check if this is a Unicode String as input */
586 if (InputType
== UnicodeString
)
588 /* Check if the output is ANSI */
589 if (OutputType
== AnsiString
)
591 /* Convert and return the final desination size */
592 OutputSize
= WideCharToMultiByte(CP_ACP
,
601 else if (OutputType
== UnicodeString
)
603 /* Copy the string */
604 StringSize
= StringSize
* sizeof(WCHAR
);
605 RtlMoveMemory(Destination
, String
, StringSize
);
607 /* Return output length */
608 OutputSize
= StringSize
+ 2;
610 else if (OutputType
== Utf8String
)
616 else if (InputType
== AnsiString
)
618 /* It's ANSI, is the output ansi too? */
619 if (OutputType
== AnsiString
)
621 /* Copy the string */
622 RtlMoveMemory(Destination
, String
, StringSize
);
624 /* Return output length */
625 OutputSize
= StringSize
+ 1;
627 else if (OutputType
== UnicodeString
)
629 /* Convert to Unicode and return size */
630 OutputSize
= MultiByteToWideChar(CP_ACP
,
635 -1) * sizeof(WCHAR
) + 2;
637 else if (OutputType
== Utf8String
)
643 else if (InputType
== Utf8String
)
649 /* Return the output size */
655 Dns_CreateStringCopy_W(IN LPWSTR Name
)
660 /* Make sure that we have a name */
664 SetLastError(ERROR_INVALID_PARAMETER
);
668 /* Find out the size of the string */
669 StringLength
= (wcslen(Name
) + 1) * sizeof(WCHAR
);
671 /* Allocate space for the copy */
672 NameCopy
= Dns_AllocZero(StringLength
);
676 RtlCopyMemory(NameCopy
, Name
, StringLength
);
681 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
684 /* Return the copy */
690 Dns_GetBufferLengthForStringCopy(IN PVOID String
,
691 IN ULONG Size OPTIONAL
,
695 ULONG OutputSize
= 0;
697 /* Check what kind of string this is */
698 if (InputType
== UnicodeString
)
700 /* Check if we have a size */
703 /* Get it ourselves */
704 Size
= (ULONG
)wcslen(String
);
707 /* Check the output type */
708 if (OutputType
== UnicodeString
)
710 /* Convert the size to bytes */
711 OutputSize
= (Size
+ 1) * sizeof(WCHAR
);
713 else if (OutputType
== Utf8String
)
720 /* Find out how much it will be in ANSI bytes */
721 OutputSize
= WideCharToMultiByte(CP_ACP
,
731 else if (InputType
== AnsiString
)
733 /* Check if we have a size */
736 /* Get it ourselves */
737 Size
= (ULONG
)strlen(String
);
740 /* Check the output type */
741 if (OutputType
== AnsiString
)
743 /* Just add a byte for the null char */
744 OutputSize
= Size
+ 1;
746 else if (OutputType
== UnicodeString
)
748 /* Calculate the bytes for a Unicode string */
749 OutputSize
= (MultiByteToWideChar(CP_ACP
,
754 0) + 1) * sizeof(WCHAR
);
756 else if (OutputType
== Utf8String
)
762 else if (InputType
== Utf8String
)
768 /* Return the size required */
773 * COPYRIGHT: See COPYING in the top level directory
774 * PROJECT: ReactOS DNS Shared Library
775 * FILE: lib/dnslib/string.c
776 * PURPOSE: functions for string manipulation and conversion.
779 /* INCLUDES ******************************************************************/
782 /* DATA **********************************************************************/
784 /* FUNCTIONS *****************************************************************/
788 Dns_StringCopy(OUT PVOID Destination
,
789 IN OUT PULONG DestinationSize
,
791 IN ULONG StringSize OPTIONAL
,
796 ULONG OutputSize
= 0;
798 /* Check if the caller already gave us the string size */
801 /* He didn't, get the input type */
802 if (InputType
== UnicodeString
)
804 /* Unicode string, calculate the size */
805 StringSize
= (ULONG
)wcslen((LPWSTR
)String
);
809 /* ANSI or UTF-8 sting, get the size */
810 StringSize
= (ULONG
)strlen((LPSTR
)String
);
814 /* Check if we have a limit on the desination size */
817 /* Make sure that we can respect it */
818 DestSize
= Dns_GetBufferLengthForStringCopy(String
,
822 if (*DestinationSize
< DestSize
)
824 /* Fail due to missing buffer space */
825 SetLastError(ERROR_MORE_DATA
);
827 /* Return how much data we actually need */
828 *DestinationSize
= DestSize
;
833 /* Fail due to invalid data */
834 SetLastError(ERROR_INVALID_DATA
);
838 /* Return how much data we actually need */
839 *DestinationSize
= DestSize
;
842 /* Now check if this is a Unicode String as input */
843 if (InputType
== UnicodeString
)
845 /* Check if the output is ANSI */
846 if (OutputType
== AnsiString
)
848 /* Convert and return the final desination size */
849 OutputSize
= WideCharToMultiByte(CP_ACP
,
858 else if (OutputType
== UnicodeString
)
860 /* Copy the string */
861 StringSize
= StringSize
* sizeof(WCHAR
);
862 RtlMoveMemory(Destination
, String
, StringSize
);
864 /* Return output length */
865 OutputSize
= StringSize
+ 2;
867 else if (OutputType
== Utf8String
)
873 else if (InputType
== AnsiString
)
875 /* It's ANSI, is the output ansi too? */
876 if (OutputType
== AnsiString
)
878 /* Copy the string */
879 RtlMoveMemory(Destination
, String
, StringSize
);
881 /* Return output length */
882 OutputSize
= StringSize
+ 1;
884 else if (OutputType
== UnicodeString
)
886 /* Convert to Unicode and return size */
887 OutputSize
= MultiByteToWideChar(CP_ACP
,
892 -1) * sizeof(WCHAR
) + 2;
894 else if (OutputType
== Utf8String
)
900 else if (InputType
== Utf8String
)
906 /* Return the output size */
912 Dns_CreateStringCopy_W(IN LPWSTR Name
)
917 /* Make sure that we have a name */
921 SetLastError(ERROR_INVALID_PARAMETER
);
925 /* Find out the size of the string */
926 StringLength
= (wcslen(Name
) + 1) * sizeof(WCHAR
);
928 /* Allocate space for the copy */
929 NameCopy
= Dns_AllocZero(StringLength
);
933 RtlCopyMemory(NameCopy
, Name
, StringLength
);
938 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
941 /* Return the copy */
947 Dns_GetBufferLengthForStringCopy(IN PVOID String
,
948 IN ULONG Size OPTIONAL
,
952 ULONG OutputSize
= 0;
954 /* Check what kind of string this is */
955 if (InputType
== UnicodeString
)
957 /* Check if we have a size */
960 /* Get it ourselves */
961 Size
= (ULONG
)wcslen(String
);
964 /* Check the output type */
965 if (OutputType
== UnicodeString
)
967 /* Convert the size to bytes */
968 OutputSize
= (Size
+ 1) * sizeof(WCHAR
);
970 else if (OutputType
== Utf8String
)
977 /* Find out how much it will be in ANSI bytes */
978 OutputSize
= WideCharToMultiByte(CP_ACP
,
988 else if (InputType
== AnsiString
)
990 /* Check if we have a size */
993 /* Get it ourselves */
994 Size
= (ULONG
)strlen(String
);
997 /* Check the output type */
998 if (OutputType
== AnsiString
)
1000 /* Just add a byte for the null char */
1001 OutputSize
= Size
+ 1;
1003 else if (OutputType
== UnicodeString
)
1005 /* Calculate the bytes for a Unicode string */
1006 OutputSize
= (MultiByteToWideChar(CP_ACP
,
1011 0) + 1) * sizeof(WCHAR
);
1013 else if (OutputType
== Utf8String
)
1019 else if (InputType
== Utf8String
)
1025 /* Return the size required */