ReactOS 0.4.15-dev-7897-g78dc504
misc.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYRIGHT.TXT
3 * PROJECT: Ext2 File System Driver for WinNT/2K/XP
4 * FILE: misc.c
5 * PROGRAMMER: Matt Wu <mattwu@163.com>
6 * HOMEPAGE: http://www.ext2fsd.com
7 * UPDATE HISTORY:
8 */
9
10/* INCLUDES *****************************************************************/
11
12#include "ext2fs.h"
13
14/* GLOBALS ***************************************************************/
15
17
18/* DEFINITIONS *************************************************************/
19
20#ifdef ALLOC_PRAGMA
21#pragma alloc_text(PAGE, Ext2Sleep)
22#endif
23
26{
27 ULONG Order = 0;
28
29 ASSERT(Value > 0);
30
31 while (Value) {
32 Order++;
33 Value >>= 1;
34 }
35
36 return (Order - 1);
37}
38
41{
42 LARGE_INTEGER SysTime;
43
44 SysTime.QuadPart = 0;
45 RtlSecondsSince1970ToTime(i_time, &SysTime);
46
47 return SysTime;
48}
49
52{
53 ULONG Ext2Time = 0;
54
55 if (!RtlTimeToSecondsSince1970(&SysTime, &Ext2Time)) {
59 }
60
61 return Ext2Time;
62}
63
64
67 struct nls_table * PageTable,
68 IN OUT PUNICODE_STRING Unicode,
69 IN PANSI_STRING Mbs )
70{
71 ULONG Length = 0;
72 int i, mbc = 0;
73 WCHAR uc;
74
75 /* Count the length of the resulting Unicode. */
76 for (i = 0; i < Mbs->Length; i += mbc) {
77
78 mbc = PageTable->char2uni(
79 (PUCHAR)&(Mbs->Buffer[i]),
80 Mbs->Length - i,
81 &uc
82 );
83
84 if (mbc <= 0) {
85
86 /* invalid character. */
87 if (mbc == 0 && Length > 0) {
88 break;
89 }
90 return 0;
91 }
92
93 Length += 2;
94 }
95
96 if (Unicode) {
97 if (Unicode->MaximumLength < Length) {
98
99 DbgBreak();
100 return 0;
101 }
102
103 Unicode->Length = 0;
104 mbc = 0;
105
106 for (i = 0; i < Mbs->Length; i += mbc) {
107
108 mbc = PageTable->char2uni(
109 (PUCHAR)&(Mbs->Buffer[i]),
110 Mbs->Length - i,
111 &uc
112 );
113 Unicode->Buffer[Unicode->Length/2] = uc;
114 Unicode->Length += 2;
115 }
116 }
117
118 return Length;
119}
120
121ULONG
123 struct nls_table * PageTable,
124 IN OUT PANSI_STRING Mbs,
125 IN PUNICODE_STRING Unicode)
126{
127 ULONG Length = 0;
128 UCHAR mbs[0x10];
129 int i, mbc;
130
131 /* Count the length of the resulting mbc-8. */
132 for (i = 0; i < (Unicode->Length / 2); i++) {
133
134 RtlZeroMemory(mbs, 0x10);
135 mbc = PageTable->uni2char(
136 Unicode->Buffer[i],
137 mbs,
138 0x10
139 );
140
141 if (mbc <= 0) {
142
143 /* Invalid character. */
144 return 0;
145 }
146
147 Length += mbc;
148 }
149
150 if (Mbs) {
151
152 if (Mbs->MaximumLength < Length) {
153
154 DbgBreak();
155 return 0;
156 }
157
158 Mbs->Length = 0;
159
160 for (i = 0; i < (Unicode->Length / 2); i++) {
161
162 mbc = PageTable->uni2char(
163 Unicode->Buffer[i],
164 mbs,
165 0x10
166 );
167
169 (PUCHAR)&(Mbs->Buffer[Mbs->Length]),
170 &mbs[0],
171 mbc
172 );
173
174 Mbs->Length += (USHORT)mbc;
175 }
176 }
177
178 return Length;
179}
180
181
182ULONG
185 IN PANSI_STRING Oem
186)
187{
188 ULONG Length = 0;
189
190 if (Vcb->Codepage.PageTable) {
191 Length = Ext2MbsToUnicode(Vcb->Codepage.PageTable, NULL, Oem);
192 if (Length > 0) {
193 goto errorout;
194 }
195 }
196
198
199errorout:
200
201 return Length;
202}
203
204
208 IN OUT PUNICODE_STRING Unicode,
209 IN POEM_STRING Oem
210)
211{
213
214
215 if (Vcb->Codepage.PageTable) {
216 Status = Ext2MbsToUnicode(Vcb->Codepage.PageTable,
217 Unicode, Oem);
218
219 if (Status >0 && Status == Unicode->Length) {
221 goto errorout;
222 }
223 }
224
226 Unicode, Oem, FALSE );
227
228 if (!NT_SUCCESS(Status)) {
229 DbgBreak();
230 goto errorout;
231 }
232
233errorout:
234
235 return Status;
236}
237
238ULONG
241 IN PUNICODE_STRING Unicode
242)
243{
244 ULONG Length = 0;
245
246 if (Vcb->Codepage.PageTable) {
247 Length = Ext2UnicodeToMbs(Vcb->Codepage.PageTable,
248 NULL, Unicode);
249 if (Length > 0) {
250 return Length;
251 }
252
253 DbgBreak();
254 }
255
256 return RtlxUnicodeStringToOemSize(Unicode);
257}
258
259
263 IN OUT POEM_STRING Oem,
264 IN PUNICODE_STRING Unicode)
265{
267
268 if (Vcb->Codepage.PageTable) {
269
270 Status = Ext2UnicodeToMbs(Vcb->Codepage.PageTable,
271 Oem, Unicode);
272 if (Status > 0 && Status == Oem->Length) {
274 } else {
276 DbgBreak();
277 }
278
279 goto errorout;
280 }
281
283 Oem, Unicode, FALSE );
284
285 if (!NT_SUCCESS(Status))
286 {
287 DbgBreak();
288 goto errorout;
289 }
290
291errorout:
292
293 return Status;
294}
295
296VOID
298{
300 Timeout.QuadPart = (LONGLONG)ms*1000*(-10); /* ms/1000 sec*/
302}
303
305{
306 switch (Status) {
308 return (-EACCES);
309
311 return (-EFAULT);
312
314 return (-ETOOSMALL);
315
317 return (-EINVAL);
318
321 return (-EOPNOTSUPP);
322
325 return (-EADDRNOTAVAIL);
326
334 return (-ENETDOWN);
335
339 return (-ENETUNREACH);
340
344 return (-ECONNABORTED);
345
351 return (-ECONNRESET);
352
354 return (-ENOMEM);
355
357 case STATUS_NO_MEMORY:
365 return (-ENOBUFS);
366
368 return (-ENOTCONN);
369
371 return (-ESHUTDOWN);
372
373 case STATUS_TIMEOUT:
376 return (-ETIMEDOUT);
377
380 return (-ECONNREFUSED);
381
383 return (-EHOSTUNREACH);
384
385 case STATUS_CANT_WAIT:
386 case STATUS_PENDING:
387 return (-EAGAIN);
388
390 return (-EIO);
391
392 case STATUS_CANCELLED:
394 return (-EINTR);
395
398 return (-EMSGSIZE);
399
401 return (-EADDRINUSE);
402 }
403
404 if (NT_SUCCESS (Status))
405 return 0;
406
407 return (-EINVAL);
408}
409
411{
412 switch (rc) {
413
414 case 0:
415 return STATUS_SUCCESS;
416
417 case -EPERM:
418 case -EACCES:
420
421 case -ENOENT:
423
424 case -EFAULT:
426
427 case -ETOOSMALL:
429
430 case -EBADMSG:
431 case -EBADF:
432 case -EINVAL:
433 case -EFBIG:
435
436 case -EBUSY:
437 return STATUS_DEVICE_BUSY;
438
439 case -ENOSYS:
441
442 case -ENOSPC:
443 return STATUS_DISK_FULL;
444
445 case -EOPNOTSUPP:
447
448 case -EDEADLK:
450
451 case -EEXIST:
453
454 case -EIO:
456
457 case -ENOTDIR:
459
460 case -EISDIR:
462
463 case -ENOTEMPTY:
465
466 case -ENODEV:
468
469 case -ENXIO:
471
472 case -EADDRNOTAVAIL:
474
475 case -ENETDOWN:
477
478 case -ENETUNREACH:
480
481 case -ECONNABORTED:
483
484 case -ECONNRESET:
486
487 case -ENOMEM:
489
490 case -ENOBUFS:
491 return STATUS_NO_MEMORY;
492
493 case -ENOTCONN:
495
496 case -ESHUTDOWN:
498
499 case -ETIMEDOUT:
500 return STATUS_TIMEOUT;
501
502 case -ECONNREFUSED:
504
505 case -EHOSTUNREACH:
507
508 case -EAGAIN:
509 return STATUS_CANT_WAIT;
510
511 case -EINTR:
512 return STATUS_CANCELLED;
513
514 case -EMSGSIZE:
516
517 case -EADDRINUSE:
519 }
520
521 return STATUS_UNSUCCESSFUL;
522}
523
525{
526 return (name->Length == 2 && name->Buffer[0] == L'.');
527}
528
530{
531 return (name->Length == 4 && name->Buffer[0] == L'.' &&
532 name->Buffer[1] == L'.');
533}
unsigned char BOOLEAN
#define ENOENT
Definition: acclib.h:79
#define EEXIST
Definition: acclib.h:88
#define EINVAL
Definition: acclib.h:90
#define EFAULT
Definition: acclib.h:86
#define EINTR
Definition: acclib.h:80
#define ENOMEM
Definition: acclib.h:84
#define EIO
Definition: acclib.h:81
#define EBUSY
Definition: acclib.h:87
#define ENODEV
Definition: acclib.h:89
#define EACCES
Definition: acclib.h:85
#define EBADF
Definition: acclib.h:82
#define EPERM
Definition: acclib.h:78
#define EAGAIN
Definition: acclib.h:83
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ENOSPC
Definition: errno.h:34
#define ETOOSMALL
Definition: errno.h:149
#define EBADMSG
Definition: errno.h:80
#define ESHUTDOWN
Definition: errno.h:119
#define ENXIO
Definition: errno.h:12
#define ENETUNREACH
Definition: errno.h:112
#define ECONNABORTED
Definition: errno.h:114
#define EOPNOTSUPP
Definition: errno.h:104
#define EHOSTUNREACH
Definition: errno.h:124
#define EADDRNOTAVAIL
Definition: errno.h:110
#define ETIMEDOUT
Definition: errno.h:121
#define EADDRINUSE
Definition: errno.h:109
#define ENOTDIR
Definition: errno.h:26
#define ENOBUFS
Definition: errno.h:116
#define ENETDOWN
Definition: errno.h:111
#define ECONNREFUSED
Definition: errno.h:122
#define ECONNRESET
Definition: errno.h:115
#define EISDIR
Definition: errno.h:27
#define EMSGSIZE
Definition: errno.h:97
#define ENOTCONN
Definition: errno.h:118
#define EFBIG
Definition: errno.h:33
NTSTATUS Ext2WinntError(int rc)
Definition: misc.c:410
BOOLEAN Ext2IsDotDot(PUNICODE_STRING name)
Definition: misc.c:529
ULONG Ext2MbsToUnicode(struct nls_table *PageTable, IN OUT PUNICODE_STRING Unicode, IN PANSI_STRING Mbs)
Definition: misc.c:66
LARGE_INTEGER Ext2NtTime(IN ULONG i_time)
Definition: misc.c:40
PEXT2_GLOBAL Ext2Global
Definition: init.c:16
VOID Ext2Sleep(ULONG ms)
Definition: misc.c:297
NTSTATUS Ext2OEMToUnicode(IN PEXT2_VCB Vcb, IN OUT PUNICODE_STRING Unicode, IN POEM_STRING Oem)
Definition: misc.c:206
BOOLEAN Ext2IsDot(PUNICODE_STRING name)
Definition: misc.c:524
ULONG Ext2Log2(ULONG Value)
Definition: misc.c:25
ULONG Ext2UnicodeToOEMSize(IN PEXT2_VCB Vcb, IN PUNICODE_STRING Unicode)
Definition: misc.c:239
ULONG Ext2LinuxTime(IN LARGE_INTEGER SysTime)
Definition: misc.c:51
NTSTATUS Ext2UnicodeToOEM(IN PEXT2_VCB Vcb, IN OUT POEM_STRING Oem, IN PUNICODE_STRING Unicode)
Definition: misc.c:261
ULONG Ext2OEMToUnicodeSize(IN PEXT2_VCB Vcb, IN PANSI_STRING Oem)
Definition: misc.c:183
int Ext2LinuxError(NTSTATUS Status)
Definition: misc.c:304
ULONG Ext2UnicodeToMbs(struct nls_table *PageTable, IN OUT PANSI_STRING Mbs, IN PUNICODE_STRING Unicode)
Definition: misc.c:122
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
#define KeDelayExecutionThread(mode, foo, t)
Definition: env_spec_w32.h:484
#define DbgBreak()
Definition: ext2fs.h:46
_In_ PLARGE_INTEGER NtTime
Definition: fatprocs.h:1914
Status
Definition: gdiplustypes.h:25
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
BOOLEAN NTAPI RtlTimeToSecondsSince1970(PLARGE_INTEGER Time, PULONG ElapsedSeconds)
#define ASSERT(a)
Definition: mode.c:44
#define KernelMode
Definition: asm.h:34
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToOemString(POEM_STRING DestinationString, PCUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI NTSTATUS NTAPI RtlOemStringToUnicodeString(PUNICODE_STRING DestinationString, PCOEM_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI ULONG NTAPI RtlxUnicodeStringToOemSize(IN PCUNICODE_STRING UnicodeString)
Definition: unicode.c:1091
NTSYSAPI VOID NTAPI RtlSecondsSince1970ToTime(_In_ ULONG SecondsSince1970, _Out_ PLARGE_INTEGER Time)
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_TIMEOUT
Definition: ntstatus.h:81
#define STATUS_PIPE_DISCONNECTED
Definition: ntstatus.h:412
#define STATUS_INVALID_ADDRESS
Definition: ntstatus.h:557
#define STATUS_QUOTA_EXCEEDED
Definition: ntstatus.h:304
#define STATUS_POSSIBLE_DEADLOCK
Definition: ntstatus.h:637
#define STATUS_NETWORK_UNREACHABLE
Definition: ntstatus.h:704
#define STATUS_CANT_WAIT
Definition: ntstatus.h:452
#define STATUS_PROTOCOL_UNREACHABLE
Definition: ntstatus.h:706
#define STATUS_ADDRESS_ALREADY_EXISTS
Definition: ntstatus.h:654
#define STATUS_INVALID_BUFFER_SIZE
Definition: ntstatus.h:650
#define STATUS_TRANSACTION_ABORTED
Definition: ntstatus.h:659
#define STATUS_HOST_UNREACHABLE
Definition: ntstatus.h:705
#define STATUS_WORKING_SET_QUOTA
Definition: ntstatus.h:397
#define STATUS_REMOTE_DISCONNECT
Definition: ntstatus.h:552
#define STATUS_INVALID_CONNECTION
Definition: ntstatus.h:556
#define STATUS_PORT_UNREACHABLE
Definition: ntstatus.h:707
#define STATUS_PENDING
Definition: ntstatus.h:82
#define STATUS_NETWORK_BUSY
Definition: ntstatus.h:427
#define STATUS_INVALID_NETWORK_RESPONSE
Definition: ntstatus.h:431
#define STATUS_CONNECTION_RESET
Definition: ntstatus.h:657
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_CONNECTION_ABORTED
Definition: ntstatus.h:709
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
#define STATUS_BAD_NETWORK_PATH
Definition: ntstatus.h:426
#define STATUS_CONNECTION_REFUSED
Definition: ntstatus.h:698
#define STATUS_INVALID_ADDRESS_COMPONENT
Definition: ntstatus.h:651
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:239
#define STATUS_CONNECTION_DISCONNECTED
Definition: ntstatus.h:656
#define STATUS_LINK_TIMEOUT
Definition: ntstatus.h:555
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
#define STATUS_REMOTE_NOT_LISTENING
Definition: ntstatus.h:424
#define STATUS_REMOTE_RESOURCES
Definition: ntstatus.h:553
#define STATUS_UNEXPECTED_IO_ERROR
Definition: ntstatus.h:469
#define STATUS_LINK_FAILED
Definition: ntstatus.h:554
#define STATUS_UNEXPECTED_NETWORK_ERROR
Definition: ntstatus.h:432
#define STATUS_LOCAL_DISCONNECT
Definition: ntstatus.h:551
#define STATUS_PAGEFILE_QUOTA
Definition: ntstatus.h:244
#define STATUS_TOO_MANY_ADDRESSES
Definition: ntstatus.h:653
#define STATUS_TOO_MANY_PAGING_FILES
Definition: ntstatus.h:387
#define STATUS_COMMITMENT_LIMIT
Definition: ntstatus.h:537
#define L(x)
Definition: ntvdm.h:50
unsigned short USHORT
Definition: pedump.c:61
#define Vcb
Definition: cdprocs.h:1415
static ULONG Timeout
Definition: ping.c:61
#define ENOSYS
Definition: errno.h:57
#define EDEADLK
Definition: errno.h:54
#define ENOTEMPTY
Definition: errno.h:58
#define STATUS_DEVICE_NOT_READY
Definition: shellext.h:70
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
Definition: name.c:39
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:43
int64_t LONGLONG
Definition: typedefs.h:68
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define STATUS_DIRECTORY_NOT_EMPTY
Definition: udferr_usr.h:167
#define STATUS_FILE_IS_A_DIRECTORY
Definition: udferr_usr.h:164
#define STATUS_NOT_A_DIRECTORY
Definition: udferr_usr.h:169
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
#define STATUS_OBJECT_PATH_NOT_FOUND
Definition: udferr_usr.h:151
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_IO_TIMEOUT
Definition: udferr_usr.h:163
#define STATUS_NO_SUCH_DEVICE
Definition: udferr_usr.h:136
#define STATUS_REQUEST_ABORTED
Definition: udferr_usr.h:183
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define STATUS_NO_SUCH_FILE
Definition: udferr_usr.h:137
#define STATUS_OBJECT_NAME_COLLISION
Definition: udferr_usr.h:150
#define STATUS_DEVICE_BUSY
Definition: udferr_usr.h:129
#define STATUS_DISK_FULL
Definition: udferr_usr.h:155
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
#define STATUS_CANCELLED
Definition: udferr_usr.h:170
LONGLONG QuadPart
Definition: typedefs.h:114
char mbs[5]
char mbc
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
#define RtlOemStringToCountedUnicodeSize(STRING)
unsigned char UCHAR
Definition: xmlstorage.h:181
__wchar_t WCHAR
Definition: xmlstorage.h:180