ReactOS  0.4.15-dev-3440-g915569a
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 
17 PWCHAR
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 /*
49 No copy/conversion is done if the dest. buffer is too small.
50 
51 Returns:
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 */
55 DWORD
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 /*
91 No copy/conversion is done if the dest. buffer is too small.
92 
93 Returns:
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 */
97 DWORD
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 /*
118 Return: num. TCHARS copied into dest including nullterm
119 */
120 DWORD
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 /*
143 Return: num. TCHARS copied into dest including nullterm
144 */
145 DWORD
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 */
NTSYSAPI NTSTATUS WINAPI RtlOemToUnicodeN(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD)
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
LONG NTSTATUS
Definition: precomp.h:26
uint16_t * PWCHAR
Definition: typedefs.h:56
char * LPSTR
Definition: xmlstorage.h:182
DWORD FilenameW2A_N(LPSTR dest, INT destlen, LPCWSTR src, INT srclen)
Definition: fileutils.c:146
int32_t INT
Definition: typedefs.h:58
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
DWORD FilenameU2A_FitOrFail(LPSTR DestA, INT destLen, PUNICODE_STRING SourceU)
Definition: fileutils.c:56
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
NTSYSAPI NTSTATUS NTAPI RtlUnicodeToOemN(PCHAR OemString, ULONG OemSize, PULONG ResultSize, PCWCH UnicodeString, ULONG UnicodeSize)
WCHAR strW[12]
Definition: clipboard.c:2029
BOOL WINAPI AreFileApisANSI(VOID)
Definition: utils.c:863
const WCHAR * str
unsigned char BOOLEAN
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
const char * LPCSTR
Definition: xmlstorage.h:183
Status
Definition: gdiplustypes.h:24
NTSYSAPI NTSTATUS WINAPI RtlMultiByteToUnicodeN(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD)
#define ASSERT(a)
Definition: mode.c:44
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static DWORD LPDWORD LPCSTR DWORD srclen
Definition: directory.c:51
#define MAX_PATH
Definition: compat.h:34
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
PWCHAR FilenameA2W(LPCSTR NameA, BOOL alloc)
Definition: fileutils.c:18
DWORD FilenameA2W_N(LPWSTR dest, INT destlen, LPCSTR src, INT srclen)
Definition: fileutils.c:121
int ret
NTSYSAPI NTSTATUS NTAPI RtlUnicodeToMultiByteN(PCHAR MbString, ULONG MbSize, PULONG ResultSize, PCWCH UnicodeString, ULONG UnicodeSize)
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToOemString(POEM_STRING DestinationString, PCUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
GLenum src
Definition: glext.h:6340
NTSYSAPI NTSTATUS NTAPI RtlOemStringToUnicodeString(PUNICODE_STRING DestinationString, PCOEM_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI DWORD WINAPI RtlUnicodeStringToAnsiSize(const UNICODE_STRING *)
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
unsigned short USHORT
Definition: pedump.c:61
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
#define alloc
Definition: rosglue.h:13
#define NULL
Definition: types.h:112
NTSYSAPI DWORD WINAPI RtlUnicodeStringToOemSize(const UNICODE_STRING *)
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
DWORD FilenameW2A_FitOrFail(LPSTR DestA, INT destLen, LPCWSTR SourceW, INT sourceLen)
Definition: fileutils.c:98
static char * dest
Definition: rtl.c:135
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define ERROR_FILENAME_EXCED_RANGE
Definition: winerror.h:263