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.

171 lines
4.3 KiB

  1. /*
  2. ** init.c - Routines dealing with I/O and expansion buffers for LZCOPY() and
  3. ** 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. #include <io.h>
  13. #include <malloc.h>
  14. #include <stdio.h>
  15. #endif
  16. #include "lz_common.h"
  17. #include "lz_buffers.h"
  18. #include "lzcommon.h"
  19. PLZINFO InitGlobalBuffers(
  20. DWORD dwOutBufSize,
  21. DWORD dwRingBufSize,
  22. DWORD dwInBufSize)
  23. {
  24. PLZINFO pLZI;
  25. if (!(pLZI = (PLZINFO)LocalAlloc(LPTR, sizeof(LZINFO)))) {
  26. return(NULL);
  27. }
  28. // Set up ring buffer. N.b., extra (cbStrMax - 1) bytes used to
  29. // facilitate string comparisons near end of ring buffer.
  30. // (The size allocated for the ring buffer may be at most 4224, since
  31. // that's the ring buffer length embedded in the LZFile structs in
  32. // lzexpand.h.)
  33. if (dwRingBufSize == 0) {
  34. dwRingBufSize = MAX_RING_BUF_LEN;
  35. }
  36. if ((pLZI->rgbyteRingBuf = (BYTE FAR *)FALLOC(dwRingBufSize * sizeof(BYTE))) == NULL)
  37. // Bail out, since without the ring buffer, we can't decode anything.
  38. return(NULL);
  39. if (dwInBufSize == 0) {
  40. dwInBufSize = MAX_IN_BUF_SIZE;
  41. }
  42. if (dwOutBufSize == 0) {
  43. dwOutBufSize = MAX_OUT_BUF_SIZE;
  44. }
  45. for (pLZI->ucbInBufLen = dwInBufSize, pLZI->ucbOutBufLen = dwOutBufSize;
  46. pLZI->ucbInBufLen > 0U && pLZI->ucbOutBufLen > 0U;
  47. pLZI->ucbInBufLen -= IN_BUF_STEP, pLZI->ucbOutBufLen -= OUT_BUF_STEP)
  48. {
  49. // Try to set up input buffer. N.b., extra byte because rgbyteInBuf[0]
  50. // will be used to hold last byte from previous input buffer.
  51. if ((pLZI->rgbyteInBuf = (BYTE *)FALLOC(pLZI->ucbInBufLen + 1U)) == NULL)
  52. continue;
  53. // And try to set up output buffer...
  54. if ((pLZI->rgbyteOutBuf = (BYTE *)FALLOC(pLZI->ucbOutBufLen)) == NULL)
  55. {
  56. FFREE(pLZI->rgbyteInBuf);
  57. continue;
  58. }
  59. return(pLZI);
  60. }
  61. // Insufficient memory for I/O buffers.
  62. FFREE(pLZI->rgbyteRingBuf);
  63. return(NULL);
  64. }
  65. PLZINFO InitGlobalBuffersEx()
  66. {
  67. return(InitGlobalBuffers(MAX_OUT_BUF_SIZE, MAX_RING_BUF_LEN, MAX_IN_BUF_SIZE));
  68. }
  69. VOID FreeGlobalBuffers(
  70. PLZINFO pLZI)
  71. {
  72. // Sanity check
  73. if (!pLZI) {
  74. return;
  75. }
  76. if (pLZI->rgbyteRingBuf)
  77. {
  78. FFREE(pLZI->rgbyteRingBuf);
  79. pLZI->rgbyteRingBuf = NULL;
  80. }
  81. if (pLZI->rgbyteInBuf)
  82. {
  83. FFREE(pLZI->rgbyteInBuf);
  84. pLZI->rgbyteInBuf = NULL;
  85. }
  86. if (pLZI->rgbyteOutBuf)
  87. {
  88. FFREE(pLZI->rgbyteOutBuf);
  89. pLZI->rgbyteOutBuf = NULL;
  90. }
  91. // Buffers deallocated ok.
  92. // reset thread info
  93. LocalFree(pLZI);
  94. }
  95. /*
  96. ** int GetIOHandle(char ARG_PTR *pszFileName, BOOL bRead, int ARG_PTR *pdosh);
  97. **
  98. ** Opens input and output files.
  99. **
  100. ** Arguments: pszFileName - source file name
  101. ** bRead - mode for opening file TRUE for read and FALSE
  102. ** for write
  103. ** pdosh - pointer to buffer for DOS file handle to be
  104. ** filled in
  105. **
  106. ** Returns: int - TRUE if file opened successfully. LZERROR_BADINHANDLE
  107. ** if input file could not be opened. LZERROR_BADOUTHANDLE
  108. ** if output file could not be opened. Fills in
  109. ** *pdosh with open DOS file handle, or NO_DOSH if
  110. ** pszFileName is NULL.
  111. **
  112. ** Globals: cblInSize - set to length of input file
  113. */
  114. INT GetIOHandle(CHAR ARG_PTR *pszFileName, BOOL bRead, INT ARG_PTR *pdosh, LONG *pcblInSize)
  115. {
  116. if (pszFileName == NULL)
  117. *pdosh = NO_DOSH;
  118. else if (bRead == WRITE_IT)
  119. {
  120. // Set up output DOS file handle.
  121. if ((*pdosh = FCREATE(pszFileName)) == -1)
  122. return(LZERROR_BADOUTHANDLE);
  123. }
  124. else // (bRead == READ_IT)
  125. {
  126. if ((*pdosh = FOPEN(pszFileName)) == -1)
  127. return(LZERROR_BADINHANDLE);
  128. // Move to the end of the input file to find its length,
  129. // then return to the beginning.
  130. if ((*pcblInSize = FSEEK(*pdosh, 0L, SEEK_END)) < 0L ||
  131. FSEEK(*pdosh, 0L, SEEK_SET) != 0L)
  132. {
  133. FCLOSE(*pdosh);
  134. return(LZERROR_BADINHANDLE);
  135. }
  136. }
  137. return(TRUE);
  138. }