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.

394 lines
11 KiB

  1. /****************************************************************************
  2. Unit Bufio; Implementation
  3. *****************************************************************************
  4. Bufio implements the structured reading of the imput stream. As such, it
  5. will handle the necessary byte-swapping that must occur when reading a
  6. native Macintosh file.
  7. This interface will also shield the calling application from knowledge of
  8. the source format (file vs. memory).
  9. Module Prefix: IO
  10. ****************************************************************************/
  11. #include "headers.c"
  12. #pragma hdrstop
  13. #include "filesys.h"
  14. #ifndef _OLECNV32_
  15. //#include "status.h"
  16. #endif // _OLECNV32_
  17. /*********************** Exported Data **************************************/
  18. /*********************** Private Data ***************************************/
  19. #define UNKNOWN 0
  20. #define FILE 1
  21. #define MEMORY 2
  22. #define RTF 3
  23. #define BUFFERSIZE 1024
  24. private LongInt numBytesRead;
  25. private LongInt pictureSize;
  26. private LongInt beginOffset;
  27. private LongInt bufferCount;
  28. private Byte buffer[BUFFERSIZE];
  29. private Byte * nextCharPtr;
  30. private Byte huge * nextCharHPtr;
  31. private Byte sourceType = UNKNOWN;
  32. private Integer fileHandle = (Integer)0;
  33. private Str255 fileName;
  34. private Boolean openFile;
  35. private Byte huge * memoryHPtr;
  36. private Handle memoryHandle;
  37. private Handle dialogHandle;
  38. /*********************** Private Function Definitions ***********************/
  39. private void ReadNextBuffer( void );
  40. /* Replenish the i/o buffer with the next set of characters */
  41. /* Memory operations - check return values on usage */
  42. #define MDisposHandle( h ) ((void) GlobalFree( h ))
  43. #define MLock( h ) ((LPtr) GlobalLock( h ))
  44. #define MUnlock( h ) ((void) GlobalUnlock( h ))
  45. #define MDR( h ) ((LPtr) GlobalLock( h ))
  46. #define MUR( h ) ((void) GlobalUnlock( h ))
  47. #define MNewHandle( s ) GlobalAlloc( GMEM_MOVEABLE, s )
  48. /*********************** Function Implementation ****************************/
  49. void IOGetByte( Byte far * byteLPtr )
  50. /*============*/
  51. /* Read a byte from the input stream. If the buffer is empty, then
  52. it is replenished. */
  53. {
  54. /* Make sure that no global error code has been set before read */
  55. if (ErGetGlobalError() != NOERR )
  56. {
  57. *byteLPtr = 0;
  58. return;
  59. }
  60. /* Check for an attempt to read past the EOF or memory block. This
  61. would indicate that the opcode parsing was thrown off somewhere. */
  62. if (numBytesRead >= pictureSize)
  63. {
  64. ErSetGlobalError( ErReadPastEOF );
  65. *byteLPtr = 0;
  66. return;
  67. }
  68. /* Check to see if we need to replenish the read buffer */
  69. if (bufferCount <= 0)
  70. {
  71. ReadNextBuffer();
  72. }
  73. /* Decrement the count of characters in the buffer, increment the total
  74. number of bytes read from the file, and return the next character. */
  75. bufferCount--;
  76. numBytesRead++;
  77. /* determine where to read the next byte from - use short or huge ptrs */
  78. *byteLPtr = (sourceType == FILE) ? *nextCharPtr++ : *nextCharHPtr++;
  79. } /* IOGetByte */
  80. void IOSkipBytes( LongInt byteCount )
  81. /*==============*/
  82. /* Skip the designated number of bytes */
  83. {
  84. /* make sure we are skipping a valid number of bytes */
  85. if (byteCount <= 0)
  86. {
  87. return;
  88. }
  89. /* Check for an attempt to read past the EOF or memory block. This
  90. would indicate that the opcode parsing was thrown off somewhere. */
  91. if (numBytesRead + byteCount >= pictureSize)
  92. {
  93. ErSetGlobalError( ErReadPastEOF );
  94. }
  95. else
  96. {
  97. /* determine if there are sufficient bytes remaining in the buffer */
  98. if (bufferCount >= byteCount)
  99. {
  100. /* decrement # bytes remaining, increment # bytes read and pointer */
  101. bufferCount -= byteCount;
  102. numBytesRead += byteCount;
  103. /* increment the appropriate pointer based on media type */
  104. if (sourceType == FILE)
  105. {
  106. /* increment near pointer to data segment buffer */
  107. nextCharPtr += byteCount;
  108. }
  109. else
  110. {
  111. /* increment huge pointer to global memory block */
  112. nextCharHPtr += byteCount;
  113. }
  114. }
  115. else /* sourceType == FILE and buffer needs to be replenished */
  116. {
  117. Byte unusedByte;
  118. /* continue calling IOGetByte() until desired number are skipped */
  119. while (byteCount--)
  120. {
  121. /* call IOGetByte to make sure the cache is replenished */
  122. IOGetByte( &unusedByte );
  123. }
  124. }
  125. }
  126. } /* IOSkipBytes */
  127. void IOAlignToWordOffset( void )
  128. /*======================*/
  129. /* Align next memory read to Word boundary. */
  130. {
  131. /* check to see if we have read an odd number of bytes so far. Skip
  132. the ensuing byte if necessary to align. */
  133. if (numBytesRead & 0x0001)
  134. {
  135. IOSkipBytes( 1 );
  136. }
  137. } /* IOAlignToWordOffset */
  138. #ifndef _OLECNV32_
  139. void IOSetFileName( StringLPtr pictFileName )
  140. /*================*/
  141. /* Interface routine to set the source filename */
  142. {
  143. lstrcpy( fileName, pictFileName );
  144. sourceType = FILE;
  145. openFile = TRUE;
  146. } /* IOSetFileName */
  147. void IOSetFileHandleAndSize( Integer pictFileHandle, LongInt pictFileSize )
  148. /*=========================*/
  149. /* Interface routine to set the source file Handle */
  150. {
  151. fileHandle = pictFileHandle;
  152. pictureSize = pictFileSize;
  153. sourceType = FILE;
  154. openFile = FALSE;
  155. } /* IOSetFIleHandle */
  156. #endif // !_OLECNV32_
  157. void IOSetMemoryHandle( HANDLE pictMemoryHandle )
  158. /*==================*/
  159. /* Interface routine to set the source file Handle */
  160. {
  161. memoryHandle = ( Handle ) pictMemoryHandle;
  162. sourceType = MEMORY;
  163. } /* IOSetMemoryHandle */
  164. void IOSetReadOffset( LongInt readOffset )
  165. /*==================*/
  166. /* Set the beginning offset to seek to when the file is opened */
  167. {
  168. beginOffset = readOffset;
  169. }
  170. void IOOpenPicture( Handle dialog )
  171. /*================*/
  172. /* Open the input stream depending on the source type set by a previous
  173. IOSet___ interface routine. Determine the size of the picture image. */
  174. {
  175. #ifndef _OLECNV32_
  176. OSErr openError;
  177. #endif // !_OLECNV32_
  178. /* if the type isn't set, return error */
  179. if (sourceType == UNKNOWN)
  180. {
  181. ErSetGlobalError( ErNoSourceFormat );
  182. return;
  183. }
  184. /* initialize the various reader variables */
  185. numBytesRead = 0;
  186. bufferCount = 0;
  187. /* determine how to open the soure data stream */
  188. #ifndef _OLECNV32_
  189. if (sourceType == FILE)
  190. {
  191. /* if we are openning and converting an entire file */
  192. if (openFile)
  193. {
  194. /* open the file */
  195. openError = FSOpen( (StringLPtr)fileName, OF_READ | OF_SHARE_DENY_WRITE, &fileHandle );
  196. if (openError)
  197. {
  198. ErSetGlobalError( ErOpenFail);
  199. }
  200. else
  201. {
  202. /* and determine the file length */
  203. FSSetFPos( fileHandle, FSFROMLEOF, 0L );
  204. FSGetFPos( fileHandle, &pictureSize );
  205. }
  206. }
  207. /* set position to the designated start position */
  208. FSSetFPos( fileHandle, FSFROMSTART, beginOffset );
  209. numBytesRead = beginOffset;
  210. }
  211. else /* if (sourceType == MEMORY) */
  212. #endif // !_OLECNV32_
  213. {
  214. /* lock the memory block */
  215. memoryHPtr = (Byte huge *) MLock( memoryHandle );
  216. if (memoryHPtr == NULL)
  217. {
  218. ErSetGlobalError( ErMemoryFail );
  219. return;
  220. }
  221. else
  222. {
  223. /* and determine the overall memory block size */
  224. pictureSize = (ULONG) GlobalSize( memoryHandle );
  225. }
  226. /* set the huge character read pointer, bytes read, and buffer count */
  227. nextCharHPtr = memoryHPtr + beginOffset;
  228. bufferCount = pictureSize - beginOffset;
  229. numBytesRead = beginOffset;
  230. }
  231. #ifndef _OLECNV32_
  232. /* make sure that a dialog handle was supplied for update */
  233. if (dialog)
  234. {
  235. /* save off the dialog box handle */
  236. dialogHandle = dialog;
  237. /* calculate the interval to update the status dialog */
  238. SendMessage( dialogHandle, SM_SETRANGE, 0, pictureSize );
  239. }
  240. #endif // !OLECNV32
  241. } /* IOOpenPicture */
  242. void IOClosePicture( void )
  243. /*=================*/
  244. /* Close the source input stream */
  245. {
  246. /* if this is a file-based metafile */
  247. #ifndef _OLECNV32_
  248. if (sourceType == FILE)
  249. {
  250. /* make sure this isn't the ImportEmbeddedGr() entry point */
  251. if (openFile)
  252. {
  253. /* close the file if necessary */
  254. FSCloseFile( fileHandle );
  255. fileHandle = ( Integer ) 0;
  256. }
  257. }
  258. else
  259. #endif // !_OLECNV32_
  260. {
  261. /* unlock the global memory block */
  262. MUnlock( memoryHandle );
  263. memoryHandle = NULL;
  264. }
  265. /* de-initialize the module variables */
  266. sourceType = UNKNOWN;
  267. dialogHandle = NULL;
  268. } /* IOClosePicture */
  269. void IOUpdateStatus( void )
  270. /*=================*/
  271. /* Update the status bar dialog to reflect current progress */
  272. {
  273. #ifndef _OLECNV32_
  274. /* update only if a dialog box was created */
  275. if (dialogHandle)
  276. {
  277. /* calculate the interval to update the status dialog */
  278. SendMessage( dialogHandle, SM_SETPOSITION, 0, numBytesRead );
  279. }
  280. #endif // !_OLECNV32_
  281. } /* IOUpdateStatus */
  282. /******************************* Private Routines ***************************/
  283. private void ReadNextBuffer( void )
  284. /*-------------------------*/
  285. /* Replenish the i/o buffer with the next set of characters. This should
  286. only be called if performing buffered I/O - not with MEMORY-based file */
  287. {
  288. #ifndef _OLECNV32_
  289. OSErr fileError;
  290. /* Read the required number of bytes from the file. Check the error
  291. code return and set the global status error if the read failed. */
  292. if (sourceType == FILE)
  293. {
  294. /* Calculate the number of bytes that should be read into the buffer.
  295. This needs to be done, since this may be a memory source picture,
  296. in which an invalid read could produce a GP violation */
  297. if (numBytesRead + BUFFERSIZE > pictureSize)
  298. bufferCount = pictureSize - numBytesRead;
  299. else
  300. bufferCount = BUFFERSIZE;
  301. /* read the bytes from the file */
  302. fileError = FSRead( fileHandle, &bufferCount, &buffer);
  303. /* if there is any error, notify the error module */
  304. if (fileError != 0)
  305. {
  306. ErSetGlobalError( ErReadFail );
  307. return;
  308. }
  309. /* reset the character read pointer to the beginning of the buffer */
  310. nextCharPtr = buffer;
  311. }
  312. #endif // _OLECNV32_
  313. } /* ReadNextBuffer */