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.

139 lines
4.0 KiB

  1. /*
  2. ** buffers.c - Routines dealing with I/O and expansion buffers for LZCopy()
  3. ** and DOS command-line programs.
  4. **
  5. ** Author: DavidDi
  6. */
  7. // Headers
  8. ///////////
  9. #ifndef LZA_DLL
  10. #include <dos.h>
  11. #include <fcntl.h>
  12. #endif
  13. #include "lz_common.h"
  14. #include "lz_buffers.h"
  15. /*
  16. ** int ReadInBuf(BYTE ARG_PTR *pbyte, int doshSource);
  17. **
  18. ** Read input file into input buffer.
  19. **
  20. ** Arguments: pbyte - pointer to storage for first byte read from file
  21. ** into buffer
  22. ** doshSource - DOS file handle to open input file
  23. **
  24. ** Returns: int - TRUE or END_OF_INPUT if successful. LZERROR_BADINHANDLE
  25. ** if not.
  26. **
  27. ** Globals: rgbyteInBuf[0] - holds last byte from previous buffer
  28. ** pbyteInBufEnd - set to point to first byte beyond end of data
  29. ** in input buffer
  30. ** bLastUsed - reset to FALSE if currently TRUE
  31. */
  32. INT ReadInBuf(BYTE ARG_PTR *pbyte, INT doshSource, PLZINFO pLZI)
  33. {
  34. DWORD ucbRead; // number of bytes actually read
  35. // !!! Assumes pLZI parm is valid. No sanity check (should be done above in caller).
  36. pLZI->rgbyteInBuf[0] = *(pLZI->pbyteInBufEnd - 1);
  37. if ((ucbRead = FREAD(doshSource, &pLZI->rgbyteInBuf[1], pLZI->ucbInBufLen))
  38. != pLZI->ucbInBufLen)
  39. {
  40. #ifdef LZA_DLL
  41. if (ucbRead == (DWORD)(-1)) {
  42. #else
  43. if (_error != 0U) {
  44. #endif
  45. // We were handed a bad input file handle.
  46. return(LZERROR_BADINHANDLE);
  47. }
  48. else if (ucbRead > 0U)
  49. // Read last ucbRead bytes of input file. Change input buffer end
  50. // to account for shorter read.
  51. pLZI->pbyteInBufEnd = &pLZI->rgbyteInBuf[1] + ucbRead;
  52. else { // (ucbRead == 0U) {
  53. // We couldn't read any bytes from input file (EOF reached).
  54. return(END_OF_INPUT);
  55. }
  56. }
  57. // Reset read pointer to beginning of input buffer.
  58. pLZI->pbyteInBuf = &pLZI->rgbyteInBuf[1];
  59. // Was an UnreadByte() done at the beginning of the last buffer?
  60. if (pLZI->bLastUsed)
  61. {
  62. // Return the last byte from the previous input buffer
  63. *pbyte = pLZI->rgbyteInBuf[0];
  64. pLZI->bLastUsed = FALSE;
  65. }
  66. else
  67. // Return the first byte from the new input buffer.
  68. *pbyte = *pLZI->pbyteInBuf++;
  69. return(TRUE);
  70. }
  71. /*
  72. ** int WriteOutBuf(BYTE byteNext, int doshDest);
  73. **
  74. ** Dumps output buffer to output file. Prompts for new floppy disk if the
  75. ** old one if full. Continues dumping to output file of same name on new
  76. ** floppy disk.
  77. **
  78. ** Arguments: byteNext - first byte to be added to empty buffer after buffer
  79. ** is written
  80. ** doshDest - output DOS file handle
  81. **
  82. ** Returns: int - TRUE if successful. LZERROR_BADOUTHANDLE or
  83. ** LZERROR_WRITE if unsuccessful.
  84. **
  85. ** Globals: pbyteOutBuf - reset to point to free byte after byteNext in
  86. ** rgbyteOutBuf
  87. */
  88. INT WriteOutBuf(BYTE byteNext, INT doshDest, PLZINFO pLZI)
  89. {
  90. DWORD ucbToWrite, // number of bytes to write from buffer
  91. ucbWritten, // number of bytes actually written
  92. ucbTotWritten; // total number of bytes written to output
  93. // !!! Assumes pLZI parm is valid. No sanity check (should be done above in caller).
  94. // How much of the buffer should be written to the output file?
  95. ucbTotWritten = ucbToWrite = (DWORD)(pLZI->pbyteOutBuf - pLZI->rgbyteOutBuf);
  96. // Reset pointer to beginning of buffer.
  97. pLZI->pbyteOutBuf = pLZI->rgbyteOutBuf;
  98. // Write to ouput file.
  99. if (doshDest != NO_DOSH &&
  100. (ucbWritten = FWRITE(doshDest, pLZI->pbyteOutBuf, ucbToWrite)) != ucbToWrite)
  101. {
  102. #ifdef LZA_DLL
  103. if (ucbWritten == (DWORD)(-1)) {
  104. #else
  105. if (_error != 0U) {
  106. #endif
  107. // Bad DOS file handle.
  108. return(LZERROR_BADOUTHANDLE);
  109. }
  110. else {
  111. // Insufficient space on destination drive.
  112. return(LZERROR_WRITE);
  113. }
  114. }
  115. // Add the next byte to the buffer.
  116. *pLZI->pbyteOutBuf++ = byteNext;
  117. return(TRUE);
  118. }