Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
5.8 KiB

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "dos.h"
  5. #include "fcntl.h"
  6. #include "process.h"
  7. #ifndef UINT16
  8. #define UINT16 unsigned short
  9. #endif
  10. #ifndef UINT32
  11. #define UINT32 unsigned long
  12. #endif
  13. #ifndef BYTE
  14. #define BYTE unsigned char
  15. #endif
  16. // this one needs to be investigated.
  17. #define READONLYLOCK 0
  18. #define READWRITELOCK 0
  19. #define MAXCACHE 600
  20. #define FOURGB 4294967295
  21. //
  22. // struct and type declarations
  23. //
  24. struct hlike
  25. {
  26. BYTE vl;
  27. BYTE vh;
  28. BYTE xvl;
  29. BYTE xvh;
  30. };
  31. struct xlike
  32. {
  33. UINT16 vx;
  34. UINT16 xvx;
  35. };
  36. struct elike
  37. {
  38. UINT32 evx;
  39. };
  40. union Conversion
  41. {
  42. struct hlike h;
  43. struct xlike x;
  44. struct elike e;
  45. };
  46. struct _BPBINFO
  47. {
  48. BYTE ReliableInfo; // if this value is 1 all other info are reliable
  49. BYTE Drive;
  50. UINT16 BytesPerSector;
  51. BYTE SectorsPerCluster;
  52. UINT16 ReservedBeforeFAT;
  53. BYTE FATCount;
  54. BYTE FATType;
  55. UINT16 MaxRootDirEntries;
  56. UINT16 TotalRootDirSectors; // FAT16 specific
  57. UINT16 TotalSectors;
  58. UINT32 TotalSystemSectors;
  59. BYTE MediaID;
  60. UINT32 SectorsPerFAT;
  61. UINT16 SectorsPerTrack;
  62. UINT16 Heads;
  63. UINT32 TotalClusters;
  64. UINT32 HiddenSectors;
  65. UINT32 BigTotalSectors;
  66. UINT32 RootDirCluster; // FAT32 specific
  67. UINT32 FirstRootDirSector;
  68. BYTE DriveType;
  69. BYTE ImproperShutDown;
  70. };
  71. typedef struct _BPBINFO BPBINFO;
  72. struct _FILEINFO
  73. {
  74. BYTE LFName[260];
  75. BYTE DOSName[8];
  76. BYTE DOSExt[3];
  77. BYTE Attribute;
  78. UINT32 HiSize;
  79. UINT32 Size;
  80. UINT32 StartCluster;
  81. UINT32 ParentCluster;
  82. UINT32 TotalClusters;
  83. BYTE EntriesTakenUp; //Entries occupied by this file in directory sector, vital for LFN processing
  84. BYTE LFNOrphaned;
  85. BYTE TrashedEntry;
  86. BYTE Second;
  87. BYTE Minute;
  88. BYTE Hour;
  89. BYTE Day;
  90. BYTE Month;
  91. UINT16 Year;
  92. };
  93. typedef struct _FILEINFO FILEINFO;
  94. struct _FILELOC
  95. {
  96. UINT32 InCluster; // value 1 means root directory is the where the entry was found
  97. UINT32 StartCluster;
  98. UINT32 NthSector; // Sector position in parentcluster
  99. UINT16 NthEntry; // Nth Entry in the sector
  100. UINT16 EntriesTakenUp; // Total entries taken up by this file
  101. UINT32 Size; // Size of the file
  102. BYTE Found; // Set to 1 if the file was found
  103. BYTE Attribute; // File attribute
  104. };
  105. typedef struct _FILELOC FILELOC;
  106. struct _ABSRW
  107. {
  108. UINT32 StartSector;
  109. UINT16 Count;
  110. UINT32 Buffer;
  111. };
  112. typedef struct _ABSRW ABSRW;
  113. struct _ABSPACKET
  114. {
  115. UINT16 SectorLow;
  116. UINT16 SectorHigh;
  117. UINT16 SectorCount;
  118. UINT16 BufferOffset;
  119. UINT16 BufferSegment;
  120. };
  121. typedef struct _ABSPACKET ABSPACKET;
  122. struct _TREENODE
  123. {
  124. UINT32 Sector;
  125. struct _TREENODE *LChild;
  126. struct _TREENODE *RChild;
  127. struct _TREENODE *Parent;
  128. BYTE *Buffer;
  129. char Dirty;
  130. };
  131. typedef struct _TREENODE BNODE, *PBNODE;
  132. struct _NODE
  133. {
  134. UINT32 Sector;
  135. struct _NODE *Back;
  136. struct _NODE *Next;
  137. BYTE *Buffer;
  138. char Dirty;
  139. };
  140. typedef struct _NODE NODE, *PNODE;
  141. struct _LMRU
  142. {
  143. PNODE Node;
  144. struct _LMRU *Next;
  145. };
  146. typedef struct _LMRU LMRU, *PLMRU;
  147. //
  148. // function declarations
  149. //
  150. UINT16 ProcessCommandLine(int argc, char *argv[]);
  151. UINT16 PureNumber(char *sNumStr);
  152. void DisplayUsage(void);
  153. void Mes(char *pMessage);
  154. UINT16 LockVolume(BYTE nDrive, BYTE nMode);
  155. UINT16 UnlockVolume(BYTE nDrive);
  156. BYTE GetCurrentDrive(void);
  157. BYTE GetCurrentDirectory(BYTE nDrive, BYTE *pBuffer);
  158. UINT16 ReadSector(BYTE nDrive, UINT32 nStartSector, UINT16 nCount, BYTE *pBuffer);
  159. UINT16 WriteSector(BYTE Drive, UINT32 nStartSector, UINT16 nCount, BYTE *pBuffer);
  160. UINT16 BuildDriveInfo(BYTE Drive, BPBINFO *pDrvInfo);
  161. UINT16 GetFATBPBInfo(BYTE *pBootSector, BPBINFO *pDrvInfo);
  162. UINT16 GetFAT32BPBInfo(BYTE *pBootSector, BPBINFO *pDrvInfo);
  163. void AddToMRU(PNODE pNode);
  164. void RemoveLRUMakeMRU(PNODE pNode);
  165. UINT16 AddNode(PNODE pNode);
  166. PNODE FindNode(UINT32 nSector);
  167. PNODE RemoveNode(void);
  168. void DeallocateLRUMRUList(void);
  169. void DeallocateFATCacheTree(PNODE pNode);
  170. void DeallocateFATCacheList(void);
  171. BYTE *CcReadFATSector(BPBINFO *pDrvInfo, UINT32 nFATSector);
  172. UINT16 CcWriteFATSector(BPBINFO *pDrvInfo, UINT32 nFATSector);
  173. void CcCommitFATSectors(BPBINFO *pDrvInfo);
  174. UINT32 FindNextCluster(BPBINFO *DrvInfo,UINT32 CurrentCluster);
  175. UINT16 UpdateFATLocation(BPBINFO *DrvInfo, UINT32 CurrentCluster,UINT32 PointingValue);
  176. UINT32 FindFreeCluster(BPBINFO *pDrvInfo);
  177. UINT32 QFindFreeCluster(BPBINFO *pDrvInfo);
  178. UINT32 GetFATEOF(BPBINFO *pDrvInfo);
  179. UINT32 GetFreeClusters(BPBINFO *pDrvInfo);
  180. UINT32 ConvertClusterUnit(BPBINFO *pDrvInfo);
  181. UINT32 GetClustersRequired(BPBINFO *pDrvInfo);
  182. UINT32 GetContigousStart(BPBINFO *pDrvInfo, UINT32 nClustersRequired);
  183. UINT32 OccupyClusters(BPBINFO *pDrvInof, UINT32 nStartCluster, UINT32 nTotalClusters);
  184. UINT16 ReadRootDirSector(BPBINFO *pDrvInfo, BYTE *pRootDirBuffer, UINT32 NthSector);
  185. UINT16 WriteRootDirSector(BPBINFO *pDrvInfo, BYTE *pRootDirBuffer, UINT32 NthSector);
  186. void FindFileLocation(BPBINFO *pDrvInfo, BYTE *TraversePath, FILELOC *FileLocation);
  187. void GetFileInfo(BPBINFO *pDrvInfo, BYTE *DirBuffer, UINT16 Offset, FILEINFO *FileInfo);
  188. BYTE GetAllInfoOfFile(BPBINFO *pDrvInfo, BYTE *FileName, FILELOC *pFileLoc, FILEINFO *pFileInfo);
  189. UINT16 SetFileInfo(BPBINFO *pDrvInfo, FILELOC *pFileLoc, FILEINFO *pFileInfo);
  190. //
  191. // Variable declarations, we declare some variables global to avoid hogging stack
  192. // if a function (like ReadSector) is called at high frequency
  193. //
  194. BPBINFO gsDrvInfo;
  195. FILELOC gsFileLoc;
  196. FILEINFO gsFileInfo;
  197. union Conversion Rx;
  198. union Conversion Tx;
  199. BYTE *PettyFATSector;
  200. UINT32 LastClusterAllocated;
  201. BYTE gsFileParam[300];
  202. BYTE gsFileName[300];
  203. UINT32 gnSize;
  204. UINT32 gnSizeInBytes;
  205. BYTE gnSizeUnit;
  206. BYTE gnContig;
  207. UINT32 gnFirstCluster;
  208. BYTE gbValidateFirstClusterParam;
  209. BYTE gnStrictLocation;
  210. BYTE gnClusterUnit;
  211. BYTE gcDrive;
  212. UINT32 gnClustersRequired;
  213. UINT32 gnAllocated;
  214. UINT32 gnClusterStart;
  215. BYTE gsCurrentDir[300];
  216. BYTE gnFreeSpaceDumpMode;
  217. BYTE gnDumpMode;
  218. UINT32 gnClusterFrom;
  219. UINT32 gnClustersCounted;
  220. UINT32 gnClusterProgress;
  221. UINT32 gnClusterProgressPrev;
  222. PNODE gpHeadNode;
  223. PNODE gpTailNode;
  224. UINT16 gpFATNodeCount;