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.

415 lines
14 KiB

  1. /*
  2. * Microsoft Confidential
  3. * Copyright (C) Microsoft Corporation 1993,1994
  4. * All Rights Reserved.
  5. *
  6. * LDI.H - Diamond Memory Decompression Interface (LDI)
  7. *
  8. * History:
  9. * 03-Jul-1994 jforbes Initial version.
  10. *
  11. * Functions:
  12. * LDICreateDecompression - Create and reset an LDI decompression context
  13. * LDIDecompress - Decompress a block of data
  14. * LDIResetDecompression - Reset LDI decompression context
  15. * LDIDestroyDecompression - Destroy LDI Decompression context
  16. *
  17. * Types:
  18. * LDI_CONTEXT_HANDLE - Handle to an LDI decompression context
  19. * PFNALLOC - Memory allocation function for LDI
  20. * PFNFREE - Free memory function for LDI
  21. */
  22. /* --- types -------------------------------------------------------------- */
  23. #include <basetsd.h>
  24. #ifndef DIAMONDAPI
  25. #define DIAMONDAPI __cdecl
  26. #endif
  27. #ifndef _BYTE_DEFINED
  28. #define _BYTE_DEFINED
  29. typedef unsigned char BYTE;
  30. #endif
  31. #ifndef _UINT_DEFINED
  32. #define _UINT_DEFINED
  33. typedef unsigned int UINT;
  34. #endif
  35. #ifndef _ULONG_DEFINED
  36. #define _ULONG_DEFINED
  37. typedef unsigned long ULONG;
  38. #endif
  39. #ifndef NEAR
  40. # ifdef BIT16
  41. # define NEAR __near
  42. # else
  43. # define NEAR
  44. # endif
  45. #endif
  46. #ifndef FAR
  47. #ifdef BIT16
  48. #define FAR __far
  49. #else
  50. #define FAR
  51. #endif
  52. #endif
  53. #ifndef HUGE
  54. #ifdef BIT16
  55. #define HUGE __huge
  56. #else
  57. #define HUGE
  58. #endif
  59. #endif
  60. #ifndef _MI_MEMORY_DEFINED
  61. #define _MI_MEMORY_DEFINED
  62. typedef void HUGE * MI_MEMORY;
  63. #endif
  64. #ifndef _MHANDLE_DEFINED
  65. #define _MHANDLE_DEFINED
  66. #if defined(_AMD64_) || defined(IA64)
  67. typedef ULONG_PTR MHANDLE;
  68. #else
  69. typedef unsigned long MHANDLE;
  70. #endif
  71. #endif
  72. #ifndef UNALIGNED
  73. #ifndef NEEDS_ALIGNMENT
  74. #define UNALIGNED
  75. #else
  76. #define UNALIGNED __unaligned
  77. #endif
  78. #endif
  79. /*
  80. * LDI will try to create a virtual ring buffer on disk if the pfnalloc call
  81. * to create the buffer fails. These functions provide LDI the disk access
  82. * features needed.
  83. *
  84. * These are modeled after the C run-time routines _open, _read,
  85. * _write, _close, and _lseek. The values for the PFNOPEN oflag
  86. * and pmode calls are those defined for _open. LDI expects error
  87. * handling to be identical to these C run-time routines.
  88. *
  89. * As long as you faithfully copy these aspects, you can supply
  90. * any functions you like!
  91. *
  92. * For PFNOPEN, the pszFile parameter will take on a special form for LDI's
  93. * temporary file. The special form appears as a file named "*". Such a
  94. * name field should be cast into the struct below, which contains the
  95. * required file's size as shown in the RINGNAME structure below.
  96. *
  97. * Example open and close callbacks are provided. It is assumed that the
  98. * client will provide more adaptive code for determining the temporary
  99. * file's name and drive location, based on environment variables and the
  100. * amount of free disk space. This sample code has hard-coded the actual
  101. * path and fails if there is not enough free space. This code creates the
  102. * file, then attempts to expand it to the requested size by writing a byte
  103. * (any byte) at the requested size - 1. (This approach is not suitable for
  104. * a file system which can support sparse files.)
  105. *
  106. * The callback routine may create this file on any path, and with any name,
  107. * as appropriate. If the file cannot be created with the requested size,
  108. * the PFNOPEN should fail. The file really should be placed on a local
  109. * fixed disk. It would not be appropriate for the file to be placed on a
  110. * compressed drive or a floppy disk. If the client has access to alternate
  111. * memory, such as XMS or EMS, these operations could be emuluated.
  112. *
  113. * static int tempHandle = -1;
  114. *
  115. * int FAR DIAMONDAPI MyOpen(char FAR *pszFile,int oflag,int pmode)
  116. * {
  117. * if (*pszFile == '*')
  118. * {
  119. * PRINGNAME pringDescriptor;
  120. *
  121. * pringDescriptor = (PRINGNAME) pszFile;
  122. *
  123. * tempHandle = _open("C:\\ldi_temp.$$$",oflag,pmode);
  124. *
  125. * if (tempHandle != -1)
  126. * {
  127. * _lseek(tempHandle,(pringDescriptor->fileSize - 1),SEEK_SET);
  128. *
  129. * if (_write(tempHandle,&tempHandle,1) != 1)
  130. * {
  131. * _close(tempHandle);
  132. * remove("C:\\ldi_temp.$$$");
  133. * tempHandle = -1;
  134. * }
  135. * }
  136. *
  137. * return(tempHandle);
  138. * }
  139. * else
  140. * {
  141. * * LDI only will call with *pszFile == '*' *
  142. * }
  143. * }
  144. *
  145. * The callback provider must watch for the corresponding PFNCLOSE call on
  146. * the returned handle, and delete the created file after closing. (The
  147. * file handle and file name assigned to the temporary file must be tracked;
  148. * a close operation on that handle must be trapped, so the temporary file
  149. * can be deleted as well.)
  150. *
  151. * The client does not need to worry about multiple concurrent opens of the
  152. * temporary file, or more than a single temporary file (from LDI).
  153. *
  154. * int FAR DIAMONDAPI MyClose(int handle)
  155. * {
  156. * int result;
  157. *
  158. * result = _close(handle);
  159. *
  160. * if (handle == tempHandle)
  161. * {
  162. * remove("C:\\ldi_temp.$$$");
  163. * tempHandle = -1;
  164. * }
  165. *
  166. * return(result);
  167. * }
  168. */
  169. typedef int (FAR DIAMONDAPI *PFNOPEN) (char FAR *pszFile,int oflag,int pmode);
  170. typedef UINT (FAR DIAMONDAPI *PFNREAD) (int hf, void FAR *pv, UINT cb);
  171. typedef UINT (FAR DIAMONDAPI *PFNWRITE)(int hf, void FAR *pv, UINT cb);
  172. typedef int (FAR DIAMONDAPI *PFNCLOSE)(int hf);
  173. typedef long (FAR DIAMONDAPI *PFNSEEK) (int hf, long dist, int seektype);
  174. /* --- LDI-defined types -------------------------------------------------- */
  175. /* LDI_CONTEXT_HANDLE - Handle to a LDI decompression context */
  176. typedef MHANDLE LDI_CONTEXT_HANDLE; /* hmd */
  177. /*** PFNALLOC - Memory allocation function for LDI
  178. *
  179. * Entry:
  180. * cb - Size in bytes of memory block to allocate
  181. *
  182. * Exit-Success:
  183. * Returns !NULL pointer to memory block
  184. *
  185. * Exit-Failure:
  186. * Returns NULL; insufficient memory
  187. */
  188. #ifndef _PFNALLOC_DEFINED
  189. #define _PFNALLOC_DEFINED
  190. typedef MI_MEMORY (FAR DIAMONDAPI *PFNALLOC)(ULONG cb); /* pfnma */
  191. #endif
  192. /*** PFNFREE - Free memory function for LDI
  193. *
  194. * Entry:
  195. * pv - Memory block allocated by matching PFNALLOC function
  196. *
  197. * Exit:
  198. * Memory block freed.
  199. */
  200. #ifndef _PFNFREE_DEFINED
  201. #define _PFNFREE_DEFINED
  202. typedef void (FAR DIAMONDAPI *PFNFREE)(MI_MEMORY pv); /* pfnmf */
  203. #endif
  204. /* --- prototypes --------------------------------------------------------- */
  205. /*** LDICreateDecompression - Create LDI decompression context
  206. *
  207. * Entry:
  208. * pcbDataBlockMax *largest uncompressed data block size expected,
  209. * gets largest uncompressed data block allowed
  210. * pvConfiguration passes implementation-specific info to decompressor.
  211. * pfnma memory allocation function pointer
  212. * pfnmf memory free function pointer
  213. * pcbSrcBufferMin gets max compressed buffer size
  214. * pmdhHandle gets newly-created context's handle
  215. * pfnopen file open function pointer (or NULL)
  216. * pfnread file read function pointer (or don't care)
  217. * pfnwrite file write function pointer (or don't care)
  218. * pfnclose file close function pointer (or don't care)
  219. * pfnseek file seek function pointer (or don't care)
  220. *
  221. * If NULL is provided for pfnopen, and the ring buffer cannot be
  222. * created via pfnma, LDICreateDecompression will fail.
  223. *
  224. * If pmdhHandle==NULL, *pcbDataBlockMax and *pcbSrcBufferMin will be
  225. * filled in, but no context will be created. This query will allow
  226. * the caller to determine required buffer sizes before creating a
  227. * context.
  228. *
  229. * Exit-Success:
  230. * Returns MDI_ERROR_NO_ERROR;
  231. * *pcbDataBlockMax, *pcbSrcBufferMin, *pmdhHandle filled in.
  232. *
  233. * Exit-Failure:
  234. * MDI_ERROR_NOT_ENOUGH_MEMORY, could not allocate enough memory.
  235. * MDI_ERROR_BAD_PARAMETERS, something wrong with parameters.
  236. * *pcbDataBlockMax, *pcbSrcBufferMin, *pmdhHandle undefined.
  237. */
  238. int FAR DIAMONDAPI LDICreateDecompression(
  239. UINT FAR * pcbDataBlockMax, /* max uncompressed data block size */
  240. void FAR * pvConfiguration, /* implementation-defined */
  241. PFNALLOC pfnma, /* Memory allocation function ptr */
  242. PFNFREE pfnmf, /* Memory free function ptr */
  243. UINT FAR * pcbSrcBufferMin, /* gets max. comp. buffer size */
  244. LDI_CONTEXT_HANDLE FAR * pmdhHandle, /* gets newly-created handle */
  245. PFNOPEN pfnopen, /* open a file callback */
  246. PFNREAD pfnread, /* read a file callback */
  247. PFNWRITE pfnwrite, /* write a file callback */
  248. PFNCLOSE pfnclose, /* close a file callback */
  249. PFNSEEK pfnseek); /* seek in file callback */
  250. /*** LDIDecompress - Decompress a block of data
  251. *
  252. * Entry:
  253. * hmd handle to decompression context
  254. * pbSrc source buffer (compressed data)
  255. * cbSrc compressed size of data to be decompressed
  256. * pbDst destination buffer (for decompressed data)
  257. * *pcbDecompressed (ptr to UINT) the expected de-compressed size
  258. * of this data block. (same as cbSrc from the
  259. * LCICompress() call.).
  260. *
  261. * Exit-Success:
  262. * Returns MDI_ERROR_NO_ERROR;
  263. * *pcbDecompressed has size of decompressed data in pbDst.
  264. * Decompression context updated.
  265. *
  266. * Exit-Failure:
  267. * MDI_ERROR_BAD_PARAMETERS, something wrong with parameters.
  268. * MDI_ERROR_BUFFER_OVERFLOW, cbSrc is too small to yield the
  269. * requested *pcbDecompressed count. cbSrc before LDIDecompressed
  270. * should always equal *pcbResult after QCICompress(), and
  271. * *pcbDecompressed before LDIDecompress should always equal the
  272. * cbSrc before QCICompress().
  273. * MDI_ERROR_FAILED, either cbSrc is too small, *pcbDecompressed is too
  274. * large, or *pbSrc is corrupt.
  275. *
  276. * Note:
  277. * Set your cbDecompressed to the expected de-compressed size of this
  278. * data block, then call LDIDecompress() with the address of your
  279. * cbDecompressed.
  280. */
  281. int FAR DIAMONDAPI LDIDecompress(
  282. LDI_CONTEXT_HANDLE hmd, /* decompression context */
  283. void FAR * pbSrc, /* source buffer */
  284. UINT cbSrc, /* source data size */
  285. void FAR * pbDst, /* target buffer */
  286. UINT FAR * pcbDecompressed); /* target data size */
  287. /*** LDIResetDecompression - Reset decompression history (if any)
  288. *
  289. * De-compression can only be started on a block which was compressed
  290. * immediately following a MCICreateCompression() or MCIResetCompression()
  291. * call. This function provides notification to the decompressor that the
  292. * next compressed block begins on a compression boundary.
  293. *
  294. * Entry:
  295. * hmd - handle to decompression context
  296. *
  297. * Exit-Success:
  298. * Returns MDI_ERROR_NO_ERROR;
  299. * Decompression context reset.
  300. *
  301. * Exit-Failure:
  302. * Returns MDI_ERROR_BAD_PARAMETERS, invalid context handle.
  303. */
  304. int FAR DIAMONDAPI LDIResetDecompression(LDI_CONTEXT_HANDLE hmd);
  305. /*** LDIDestroyDecompression - Destroy LDI decompression context
  306. *
  307. * Entry:
  308. * hmd - handle to decompression context
  309. *
  310. * Exit-Success:
  311. * Returns MDI_ERROR_NO_ERROR;
  312. * Decompression context destroyed.
  313. *
  314. * Exit-Failure:
  315. * Returns MDI_ERROR_BAD_PARAMETERS, invalid context handle.
  316. */
  317. int FAR DIAMONDAPI LDIDestroyDecompression(LDI_CONTEXT_HANDLE hmd);
  318. #ifndef BIT16
  319. int FAR DIAMONDAPI LDIGetWindow(
  320. LDI_CONTEXT_HANDLE hmd, /* decompression context */
  321. BYTE FAR ** ppWindow, /* pointer to window start */
  322. long * pFileOffset, /* offset in folder */
  323. long * pWindowOffset, /* offset in window */
  324. long * pcbBytesAvail); /* bytes avail from window start */
  325. #endif
  326. /* --- constants ---------------------------------------------------------- */
  327. /* return codes */
  328. #define MDI_ERROR_NO_ERROR 0
  329. #define MDI_ERROR_NOT_ENOUGH_MEMORY 1
  330. #define MDI_ERROR_BAD_PARAMETERS 2
  331. #define MDI_ERROR_BUFFER_OVERFLOW 3
  332. #define MDI_ERROR_FAILED 4
  333. #define MDI_ERROR_CONFIGURATION 5
  334. /* --- LZX configuration details ------------------------------------- */
  335. /*** LZX pvConfiguration structure
  336. *
  337. * For the LZX decompressor, two parameters are configurable, the
  338. * "window bits", which defines the size of the buffer needed by the
  339. * the decompressor (must match the value used to compress), and the CPU
  340. * type, which controls whether 386 opcodes will be used or not. If
  341. * "unknown" is provided for the fCPUtype, LDI will attempt to determine
  342. * the CPU type itself, which could fail or produce system faults on
  343. * non-DOS platforms (like Windows.) Windows apps should use GetWinFlags()
  344. * or a similiar method, and never pass "unknown".
  345. *
  346. * pvConfiguration points to this structure.
  347. */
  348. #pragma pack (1)
  349. typedef struct {
  350. long WindowSize; /* buffersize */
  351. long fCPUtype; /* controls internal code selection */
  352. } LZXDECOMPRESS; /* qdec */
  353. #pragma pack ()
  354. typedef LZXDECOMPRESS *PLZXDECOMPRESS; /* pldec */
  355. typedef LZXDECOMPRESS FAR *PFLZXDECOMPRESS; /* pfldec */
  356. /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  357. *
  358. * LDI_CPU_UNKNOWN detection does *not* work when running under Windows
  359. * in 286 protected mode! Call GetWinFlags() to determine
  360. * the CPU type and pass it explicitly!
  361. */
  362. #define LDI_CPU_UNKNOWN (-1) /* internally determined */
  363. /*
  364. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  365. */
  366. #define LDI_CPU_80286 (0) /* '286 opcodes only */
  367. #define LDI_CPU_80386 (1) /* '386 opcodes used */
  368. #define LDI_CPU_CONSERVATIVE (LDI_CPU_80286)
  369. /* ----------------------------------------------------------------------- */