ReactOS 0.4.15-dev-5666-gc548b97
fileutils.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/file/fileutils.c
5 * PURPOSE: File utility function shared with kernel32_vista
6 * PROGRAMMER: Taken from wine
7 */
8
9/* INCLUDES *****************************************************************/
10
11#include <k32.h>
12#define NDEBUG
13#include <debug.h>
14
15/* FUNCTIONS ****************************************************************/
16
19{
22 PUNICODE_STRING pstrW;
24
25 //ASSERT(NtCurrentTeb()->StaticUnicodeString.Buffer == NtCurrentTeb()->StaticUnicodeBuffer);
26 ASSERT(NtCurrentTeb()->StaticUnicodeString.MaximumLength == sizeof(NtCurrentTeb()->StaticUnicodeBuffer));
27
28 RtlInitAnsiString(&str, NameA);
29 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
30
31 if (AreFileApisANSI())
33 else
35
36 if (NT_SUCCESS(Status))
37 return pstrW->Buffer;
38
41 else
43
44 return NULL;
45}
46
47
48/*
49No copy/conversion is done if the dest. buffer is too small.
50
51Returns:
52 Success: number of TCHARS copied into dest. buffer NOT including nullterm
53 Fail: size of buffer in TCHARS required to hold the converted filename, including nullterm
54*/
57 LPSTR DestA,
58 INT destLen, /* buffer size in TCHARS incl. nullchar */
59 PUNICODE_STRING SourceU
60 )
61{
62 DWORD ret;
63
64 /* destLen should never exceed MAX_PATH */
65 if (destLen > MAX_PATH) destLen = MAX_PATH;
66
68 /* ret incl. nullchar */
69
70 if (DestA && (INT)ret <= destLen)
71 {
73
74 str.Buffer = DestA;
75 str.MaximumLength = (USHORT)destLen;
76
77
78 if (AreFileApisANSI())
80 else
82
83 ret = str.Length; /* SUCCESS: length without terminating 0 */
84 }
85
86 return ret;
87}
88
89
90/*
91No copy/conversion is done if the dest. buffer is too small.
92
93Returns:
94 Success: number of TCHARS copied into dest. buffer NOT including nullterm
95 Fail: size of buffer in TCHARS required to hold the converted filename, including nullterm
96*/
99 LPSTR DestA,
100 INT destLen, /* buffer size in TCHARS incl. nullchar */
101 LPCWSTR SourceW,
102 INT sourceLen /* buffer size in TCHARS incl. nullchar */
103 )
104{
106
107 if (sourceLen < 0) sourceLen = wcslen(SourceW) + 1;
108
109 strW.Buffer = (PWCHAR)SourceW;
110 strW.MaximumLength = sourceLen * sizeof(WCHAR);
111 strW.Length = strW.MaximumLength - sizeof(WCHAR);
112
113 return FilenameU2A_FitOrFail(DestA, destLen, &strW);
114}
115
116
117/*
118Return: num. TCHARS copied into dest including nullterm
119*/
120DWORD
122 LPWSTR dest,
123 INT destlen, /* buffer size in TCHARS incl. nullchar */
124 LPCSTR src,
125 INT srclen /* buffer size in TCHARS incl. nullchar */
126 )
127{
128 DWORD ret;
129
130 if (srclen < 0) srclen = strlen( src ) + 1;
131
132 if (AreFileApisANSI())
133 RtlMultiByteToUnicodeN( dest, destlen* sizeof(WCHAR), &ret, (LPSTR)src, srclen );
134 else
135 RtlOemToUnicodeN( dest, destlen* sizeof(WCHAR), &ret, (LPSTR)src, srclen );
136
137 if (ret) dest[(ret/sizeof(WCHAR))-1]=0;
138
139 return ret/sizeof(WCHAR);
140}
141
142/*
143Return: num. TCHARS copied into dest including nullterm
144*/
145DWORD
147 LPSTR dest,
148 INT destlen, /* buffer size in TCHARS incl. nullchar */
149 LPCWSTR src,
150 INT srclen /* buffer size in TCHARS incl. nullchar */
151 )
152{
153 DWORD ret;
154
155 if (srclen < 0) srclen = wcslen( src ) + 1;
156
157 if (AreFileApisANSI())
158 RtlUnicodeToMultiByteN( dest, destlen, &ret, (LPWSTR) src, srclen * sizeof(WCHAR));
159 else
160 RtlUnicodeToOemN( dest, destlen, &ret, (LPWSTR) src, srclen * sizeof(WCHAR) );
161
162 if (ret) dest[ret-1]=0;
163
164 return ret;
165}
166
167/* EOF */
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:752
#define MAX_PATH
Definition: compat.h:34
PWCHAR FilenameA2W(LPCSTR NameA, BOOL alloc)
Definition: fileutils.c:18
DWORD FilenameW2A_FitOrFail(LPSTR DestA, INT destLen, LPCWSTR SourceW, INT sourceLen)
Definition: fileutils.c:98
DWORD FilenameW2A_N(LPSTR dest, INT destlen, LPCWSTR src, INT srclen)
Definition: fileutils.c:146
DWORD FilenameU2A_FitOrFail(LPSTR DestA, INT destLen, PUNICODE_STRING SourceU)
Definition: fileutils.c:56
DWORD FilenameA2W_N(LPWSTR dest, INT destlen, LPCSTR src, INT srclen)
Definition: fileutils.c:121
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
GLenum src
Definition: glext.h:6340
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define NtCurrentTeb
#define ASSERT(a)
Definition: mode.c:44
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:52
static char * dest
Definition: rtl.c:135
WCHAR strW[12]
Definition: clipboard.c:2029
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToOemString(POEM_STRING DestinationString, PCUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI NTSTATUS NTAPI RtlOemStringToUnicodeString(PUNICODE_STRING DestinationString, PCOEM_STRING SourceString, BOOLEAN AllocateDestinationString)
_Use_decl_annotations_ NTSTATUS NTAPI RtlUnicodeToOemN(_Out_ PCHAR OemString, _In_ ULONG OemSize, _Out_opt_ PULONG ResultSize, _In_ PCWCH UnicodeString, _In_ ULONG UnicodeSize)
Definition: nlsboot.c:263
_Use_decl_annotations_ NTSTATUS NTAPI RtlUnicodeToMultiByteN(_Out_ PCHAR MbString, _In_ ULONG MbSize, _Out_opt_ PULONG ResultSize, _In_ PCWCH UnicodeString, _In_ ULONG UnicodeSize)
Definition: nlsboot.c:107
_Use_decl_annotations_ NTSTATUS NTAPI RtlMultiByteToUnicodeN(_Out_ PWCH UnicodeString, _In_ ULONG UnicodeSize, _Out_opt_ PULONG ResultSize, _In_ PCCH MbString, _In_ ULONG MbSize)
Definition: nlsboot.c:62
_Use_decl_annotations_ NTSTATUS NTAPI RtlOemToUnicodeN(_Out_ PWCHAR UnicodeString, _In_ ULONG UnicodeSize, _Out_opt_ PULONG ResultSize, _In_ PCCH OemString, _In_ ULONG OemSize)
Definition: nlsboot.c:282
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
unsigned short USHORT
Definition: pedump.c:61
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define alloc
Definition: rosglue.h:13
const WCHAR * str
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
int32_t INT
Definition: typedefs.h:58
uint16_t * PWCHAR
Definition: typedefs.h:56
int ret
BOOL WINAPI AreFileApisANSI(void)
Definition: utils.c:866
#define ERROR_FILENAME_EXCED_RANGE
Definition: winerror.h:263
#define RtlUnicodeStringToOemSize(STRING)
#define RtlUnicodeStringToAnsiSize(String)
Definition: rtlfuncs.h:1005
const char * LPCSTR
Definition: xmlstorage.h:183
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185