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.

143 lines
3.4 KiB

  1. // slbZip.cpp
  2. //
  3. // Purpose: implement the public fns exported by this library
  4. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  5. // 1997. This computer program includes Confidential, Proprietary
  6. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  7. // use, disclosure, and/or reproduction is prohibited unless authorized
  8. // in writing. All Rights Reserved.
  9. #include <basetsd.h>
  10. #include <windows.h>
  11. #include <stdlib.h>
  12. #include "slbZip.h"
  13. #include "comppub.h"
  14. namespace
  15. {
  16. DWORD
  17. AsError(CompressStatus_t cs)
  18. {
  19. if (INSUFFICIENT_MEMORY == cs)
  20. return E_OUTOFMEMORY;
  21. else
  22. return ERROR_INVALID_PARAMETER;
  23. }
  24. struct AutoLPBYTE
  25. {
  26. explicit
  27. AutoLPBYTE(LPBYTE p = 0)
  28. : m_p(p)
  29. {}
  30. ~AutoLPBYTE()
  31. {
  32. if (m_p)
  33. free(m_p);
  34. }
  35. LPBYTE m_p;
  36. };
  37. } // namespace
  38. void __stdcall CompressBuffer(
  39. BYTE *pData,
  40. UINT uDataLen,
  41. BYTE **ppCompressedData,
  42. UINT * puCompressedDataLen)
  43. {
  44. AutoLPBYTE alpTemp;
  45. UINT uTempLen = 0;
  46. // Check parameters
  47. if(NULL==pData)
  48. throw ERROR_INVALID_PARAMETER;
  49. if(NULL==ppCompressedData)
  50. throw ERROR_INVALID_PARAMETER;
  51. if(NULL==puCompressedDataLen)
  52. throw ERROR_INVALID_PARAMETER;
  53. // Reset compressed data len
  54. *puCompressedDataLen = 0;
  55. // Compress the data
  56. CompressStatus_t cs =
  57. Compress(pData, uDataLen, &alpTemp.m_p, &uTempLen, 9);
  58. if (COMPRESS_OK != cs)
  59. {
  60. DWORD Error = AsError(cs);
  61. throw Error;
  62. }
  63. // Create a task memory bloc
  64. AutoLPBYTE
  65. alpCompressedData(reinterpret_cast<LPBYTE>(malloc(uTempLen)));
  66. if (0 == alpCompressedData.m_p)
  67. throw static_cast<HRESULT>(E_OUTOFMEMORY);
  68. // Copy the data to the created memory bloc
  69. CopyMemory(alpCompressedData.m_p, alpTemp.m_p, uTempLen);
  70. // Transfer ownership
  71. *ppCompressedData = alpCompressedData.m_p;
  72. alpCompressedData.m_p = 0;
  73. // Update the compressed data len
  74. *puCompressedDataLen = uTempLen;
  75. }
  76. void __stdcall DecompressBuffer(BYTE *pData,
  77. UINT uDataLen,
  78. BYTE **ppDecompressedData,
  79. UINT * puDecompressedDataLen)
  80. {
  81. AutoLPBYTE alpTemp;
  82. UINT uTempLen = 0;
  83. // Check parameters
  84. if(NULL==pData)
  85. throw ERROR_INVALID_PARAMETER;
  86. if(NULL==ppDecompressedData)
  87. throw ERROR_INVALID_PARAMETER;
  88. if(NULL==puDecompressedDataLen)
  89. throw ERROR_INVALID_PARAMETER;
  90. // Reset decompressed data len
  91. *puDecompressedDataLen = 0;
  92. // Decompress the data
  93. CompressStatus_t cs =
  94. Decompress(pData, uDataLen, &alpTemp.m_p, &uTempLen);
  95. if (COMPRESS_OK != cs)
  96. {
  97. DWORD Error = AsError(cs);
  98. throw Error;
  99. }
  100. // Create a task memory bloc
  101. AutoLPBYTE
  102. alpDecompressedData(reinterpret_cast<LPBYTE>(malloc(uTempLen)));
  103. if (0 == alpDecompressedData.m_p)
  104. throw static_cast<HRESULT>(E_OUTOFMEMORY);
  105. // Copy the data to the created memory bloc
  106. CopyMemory(alpDecompressedData.m_p, alpTemp.m_p, uTempLen);
  107. // Transfer ownership
  108. *ppDecompressedData = alpDecompressedData.m_p;
  109. alpDecompressedData.m_p = 0;
  110. // Update the compressed data len
  111. *puDecompressedDataLen = uTempLen;
  112. }