Leaked source code of windows server 2003
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.

297 lines
7.3 KiB

  1. /***
  2. **
  3. ** Module: FileIO
  4. **
  5. ** Description:
  6. ** This is a module of the T1 to TT font converter. The module
  7. ** is the interface towards all low level I/O functions that are
  8. ** are available on the current platform.
  9. ** This version of the module is written specifically for Win32,
  10. ** and is based on "memory mapped files".
  11. **
  12. ** Author: Michael Jansson
  13. **
  14. ** Created: 5/26/93
  15. **
  16. ***/
  17. /**** INCLUDES */
  18. /* General types and definitions. */
  19. #include <windows.h>
  20. #undef IN
  21. /* Special types and definitions. */
  22. #include "t1instal.h"
  23. #include "types.h"
  24. #include "safemem.h"
  25. #include "fileio.h"
  26. /* Module dependent types and prototypes. */
  27. /*-none-*/
  28. /***** LOCAL TYPES */
  29. struct ioFile {
  30. HANDLE file;
  31. HANDLE mapping;
  32. LPVOID data;
  33. UBYTE *ptr;
  34. UBYTE *max;
  35. DWORD length;
  36. boolean output;
  37. };
  38. /***** CONSTANTS */
  39. #define FILESIZE 65535L
  40. #define BUFSIZE 8L * 1024L
  41. #define BADSET_ERROR 0xffffffff
  42. /***** MACROS */
  43. #ifndef FASTCALL
  44. # ifdef MSDOS
  45. # define FASTCALL __fastcall
  46. # else
  47. # define FASTCALL
  48. # endif
  49. #endif
  50. #define TRY if (1)
  51. #define EXCEPT(v) else
  52. /***** STATIC FUNCTIONS */
  53. /*-none-*/
  54. /***** FUNCTIONS */
  55. struct ioFile *io_OpenFile(const char *name, const int mode)
  56. {
  57. DWORD access;
  58. DWORD create;
  59. DWORD attr;
  60. DWORD prot;
  61. DWORD lowsize;
  62. DWORD mapaccess;
  63. SECURITY_ATTRIBUTES sa;
  64. struct ioFile *file;
  65. if ((file = Malloc(sizeof(struct ioFile)))!=NULL) {
  66. file->file = NULL;
  67. file->mapping = NULL;
  68. file->data = NULL;
  69. file->ptr = NULL;
  70. file->length = 0;
  71. if (mode == READONLY) {
  72. access = GENERIC_READ;
  73. create = OPEN_EXISTING;
  74. attr = FILE_ATTRIBUTE_NORMAL /*FILE_FLAG_SEQUENTIAL_SCAN*/;
  75. prot = PAGE_READONLY;
  76. lowsize = 0;
  77. mapaccess = FILE_MAP_READ;
  78. file->output = FALSE;
  79. } else {
  80. access = GENERIC_READ | GENERIC_WRITE;
  81. create = CREATE_ALWAYS;
  82. attr = FILE_ATTRIBUTE_NORMAL;
  83. prot = PAGE_READWRITE;
  84. lowsize = FILESIZE;
  85. mapaccess = FILE_MAP_ALL_ACCESS;
  86. file->output = TRUE;
  87. }
  88. sa.nLength = sizeof(sa);
  89. sa.lpSecurityDescriptor = NULL;
  90. sa.bInheritHandle = FALSE;
  91. if ((file->file = CreateFile(name, access, 0, &sa, create,
  92. attr, NULL))==INVALID_HANDLE_VALUE) {
  93. (void)io_CloseFile(file);
  94. SetLastError(0);
  95. file = NULL;
  96. } else {
  97. if ((file->mapping = CreateFileMapping(file->file, NULL,
  98. prot, 0, lowsize,
  99. NULL))==INVALID_HANDLE_VALUE) {
  100. (void)io_CloseFile(file);
  101. file = NULL;
  102. } else {
  103. if ((file->data = MapViewOfFile(file->mapping,
  104. mapaccess, 0, 0, 0))==NULL) {
  105. (void)io_CloseFile(file);
  106. file = NULL;
  107. } else {
  108. file->ptr = (UBYTE *)file->data;
  109. file->max = file->ptr;
  110. file->max = file->max + GetFileSize(file->file, NULL);
  111. }
  112. }
  113. }
  114. }
  115. return file;
  116. }
  117. errcode io_CloseFile(struct ioFile *file)
  118. {
  119. errcode status = SUCCESS;
  120. if (file==NULL || file->data==NULL || file->file==0)
  121. status = FAILURE;
  122. if (file) {
  123. if ((DWORD)(file->ptr - (UBYTE *)file->data)>file->length)
  124. file->length = (long)(file->ptr - (UBYTE *)file->data);
  125. if (file->data){
  126. UnmapViewOfFile(file->data);
  127. file->data = NULL;
  128. }
  129. if (file->mapping) {
  130. CloseHandle(file->mapping);
  131. file->mapping = NULL;
  132. }
  133. if (file->file) {
  134. if (file->output) {
  135. if (SetFilePointer(file->file,
  136. file->length,
  137. 0,
  138. FILE_BEGIN)==BADSET_ERROR)
  139. status = FAILURE;
  140. else if (SetEndOfFile(file->file)==FALSE)
  141. status = FAILURE;
  142. }
  143. CloseHandle(file->file);
  144. file->file = NULL;
  145. }
  146. Free(file);
  147. }
  148. return status;
  149. }
  150. USHORT FASTCALL io_ReadOneByte(struct ioFile *file)
  151. {
  152. USHORT byte;
  153. if (file->ptr<=file->max) {
  154. byte = (USHORT)*(file->ptr++);
  155. } else {
  156. SetLastError(ERROR_READ_FAULT);
  157. byte = ERROR_READ_FAULT;
  158. }
  159. return byte;
  160. }
  161. USHORT FASTCALL io_WriteBytes(const UBYTE *buf,
  162. USHORT len,
  163. struct ioFile *file)
  164. {
  165. if ((file->ptr+len)<=file->max) {
  166. memcpy(file->ptr, buf, len);
  167. file->ptr = file->ptr + len;
  168. } else if (file->data) {
  169. long pos = io_FileTell(file);
  170. long size = MAX(GetFileSize(file->file, NULL),
  171. MAX(file->length, (ULONG)(file->ptr -
  172. (UBYTE *)file->data)));
  173. /* Get rid of the old file mapping. */
  174. UnmapViewOfFile(file->data);
  175. file->data = NULL;
  176. CloseHandle(file->mapping);
  177. file->mapping = NULL;
  178. /* Get a new file mapping. */
  179. if ((file->mapping = CreateFileMapping(file->file, NULL,
  180. PAGE_READWRITE, 0,
  181. size + BUFSIZE,
  182. NULL))==INVALID_HANDLE_VALUE) {
  183. SetLastError(ERROR_WRITE_FAULT);
  184. file->ptr = file->max;
  185. len = 0;
  186. } else if ((file->data = MapViewOfFile(file->mapping,
  187. FILE_MAP_ALL_ACCESS,
  188. 0, 0, 0))==NULL) {
  189. SetLastError(ERROR_WRITE_FAULT);
  190. file->ptr = file->max;
  191. len = 0;
  192. } else {
  193. file->ptr = (UBYTE *)file->data;
  194. file->max = (UBYTE *)file->data;
  195. file->max = file->max + size + BUFSIZE;
  196. io_FileSeek(file, pos);
  197. io_WriteBytes(buf, len, file);
  198. }
  199. }
  200. return len;
  201. }
  202. USHORT FASTCALL io_ReadBytes(UBYTE *buf, USHORT len, struct ioFile *file)
  203. {
  204. if ((file->ptr+len)<=file->max) {
  205. memcpy(buf, file->ptr, len);
  206. file->ptr = file->ptr + len;
  207. } else {
  208. SetLastError(ERROR_READ_FAULT);
  209. len = 0;
  210. }
  211. return len;
  212. }
  213. boolean io_FileError(struct ioFile *file)
  214. {
  215. return (boolean)GetLastError();
  216. }
  217. long FASTCALL io_FileTell(struct ioFile *file)
  218. {
  219. return (long)(file->ptr - (UBYTE *)file->data);
  220. }
  221. long FASTCALL io_FileSeek(struct ioFile *file, long where)
  222. {
  223. DWORD oldpos = (DWORD)(file->ptr - (UBYTE *)file->data);
  224. /* Keep track of the length of the file. */
  225. if (oldpos>file->length)
  226. file->length = oldpos;
  227. /* Fail if file is not mapped, or if we are jumping out of bounds. */
  228. if (file->data && (where>=0) &&
  229. ((UBYTE *)file->data+where) <= file->max) {
  230. file->ptr = (UBYTE *)file->data;
  231. file->ptr = file->ptr + where;
  232. } else {
  233. SetLastError(ERROR_SEEK);
  234. }
  235. return (long)oldpos;
  236. }
  237. /***
  238. ** Function: FileSeek
  239. **
  240. ** Description:
  241. ***/
  242. void FASTCALL io_RemoveFile(const char *name)
  243. {
  244. DeleteFile(name);
  245. }