ReactOS  0.4.14-dev-323-g6fe6a88
common.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS VFAT filesystem library
4  * FILE: lib\fslib\vfatlib\common.c
5  * PURPOSE: Common code for Fat support
6  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7  * Eric Kohl
8  * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
9  */
10 
11 /* INCLUDES *******************************************************************/
12 
13 #include "vfatlib.h"
14 
15 #define NDEBUG
16 #include <debug.h>
17 
18 /* FUNCTIONS ******************************************************************/
19 
20 ULONG
22 {
23  ULONG i = 1;
24 
25  while (Value > 0)
26  {
27  i++;
28  Value /= 2;
29  }
30 
31  return i - 2;
32 }
33 
34 ULONG
36 {
37  LARGE_INTEGER SystemTime;
39  ULONG Serial;
40  PUCHAR Buffer;
41 
42  NtQuerySystemTime(&SystemTime);
43  RtlTimeToTimeFields(&SystemTime, &TimeFields);
44 
45  Buffer = (PUCHAR)&Serial;
46  Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF);
47  Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF);
48  Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF);
49  Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF);
50 
51  return Serial;
52 }
53 
54 /***** Wipe function for FAT12, FAT16 and FAT32 formats *****/
58  IN ULONG TotalSectors,
59  IN ULONG SectorsPerCluster,
60  IN ULONG BytesPerSector,
62 {
64  PUCHAR Buffer;
66  ULONGLONG Sector;
67  ULONG Length;
69 
70  Length = SectorsPerCluster * BytesPerSector;
71 
72  /* Allocate buffer for the cluster */
73  Buffer = (PUCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
75  Length);
76  if (Buffer == NULL)
78 
79  /* Wipe all clusters */
80  Sector = 0;
81  while (Sector + SectorsPerCluster < TotalSectors)
82  {
83  FileOffset.QuadPart = Sector * BytesPerSector;
84 
86  NULL,
87  NULL,
88  NULL,
90  Buffer,
91  Length,
92  &FileOffset,
93  NULL);
94  if (!NT_SUCCESS(Status))
95  {
96  DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
97  goto done;
98  }
99 
100  UpdateProgress(Context, SectorsPerCluster);
101 
102  Sector += SectorsPerCluster;
103  }
104 
105  /* Wipe the trailing space behind the last cluster */
106  if (Sector < TotalSectors)
107  {
108  DPRINT("Remaining sectors %lu\n", TotalSectors - Sector);
109 
110  FileOffset.QuadPart = Sector * BytesPerSector;
111  Length = (TotalSectors - Sector) * BytesPerSector;
112 
114  NULL,
115  NULL,
116  NULL,
117  &IoStatusBlock,
118  Buffer,
119  Length,
120  &FileOffset,
121  NULL);
122  if (!NT_SUCCESS(Status))
123  {
124  DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
125  goto done;
126  }
127 
128  UpdateProgress(Context, TotalSectors - Sector);
129  }
130 
131 done:
132  /* Free the buffer */
133  RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
134  return Status;
135 }
136 
137 /* EOF */
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
#define IN
Definition: typedefs.h:38
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
HANDLE FileHandle
Definition: stats.c:38
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
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
USHORT Milliseconds
Definition: env_spec_w32.h:717
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
BOOLEAN RtlTimeToTimeFields(IN PLARGE_INTEGER Time, IN PTIME_FIELDS TimeFields)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
uint64_t ULONGLONG
Definition: typedefs.h:65
NTSTATUS FatWipeSectors(IN HANDLE FileHandle, IN ULONG TotalSectors, IN ULONG SectorsPerCluster, IN ULONG BytesPerSector, IN OUT PFORMAT_CONTEXT Context)
Definition: common.c:56
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
ULONG GetShiftCount(IN ULONG Value)
Definition: common.c:21
unsigned char UCHAR
Definition: xmlstorage.h:181
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
Status
Definition: gdiplustypes.h:24
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:151
ULONG CalcVolumeSerialNumber(VOID)
Definition: common.c:35
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
NTSTATUS NTAPI NtQuerySystemTime(OUT PLARGE_INTEGER SystemTime)
Definition: time.c:417
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
static PTIME_FIELDS TimeFields
Definition: time.c:104
VOID UpdateProgress(PFORMAT_CONTEXT Context, ULONG Increment)
Definition: vfatlib.c:322