islzma.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. islzma.c, by Jordan Russell for Inno Setup
  3. This file is public domain (like the LZMA SDK)
  4. */
  5. #include <stddef.h>
  6. #include <windows.h>
  7. #include "../../../../Components/Lzma2/Alloc.h"
  8. #include "../../../../Components/Lzma2/LzmaEnc.h"
  9. #include "../../../../Components/Lzma2/Lzma2Enc.h"
  10. #include "islzma.h"
  11. // Private definition of a handle; callers of the DLL should use void*
  12. struct LZMAHandle {
  13. int marker;
  14. BOOL LZMA2;
  15. CLzmaEncHandle encoder1;
  16. CLzma2EncHandle encoder2;
  17. };
  18. #define LZMA_HANDLE_MARKER 0x3E1A981F
  19. #define LZMA_HANDLE_VALID(h) ((h) && (h)->marker == LZMA_HANDLE_MARKER)
  20. SRes __stdcall LZMA_Init(BOOL LZMA2, struct LZMAHandle **handle)
  21. {
  22. struct LZMAHandle *h = calloc(1, sizeof(*h));
  23. if (!h) return SZ_ERROR_MEM;
  24. h->marker = LZMA_HANDLE_MARKER;
  25. h->LZMA2 = LZMA2;
  26. if (LZMA2) {
  27. if (!(h->encoder2 = Lzma2Enc_Create(&g_Alloc, &g_BigAlloc))) {
  28. free(h);
  29. return SZ_ERROR_MEM;
  30. }
  31. } else {
  32. if (!(h->encoder1 = LzmaEnc_Create(&g_Alloc))) {
  33. free(h);
  34. return SZ_ERROR_MEM;
  35. }
  36. }
  37. *handle = h;
  38. return SZ_OK;
  39. }
  40. SRes __stdcall LZMA_SetProps(struct LZMAHandle *handle,
  41. struct LZMAEncoderProps *encProps, size_t encPropsSize)
  42. {
  43. CLzmaEncProps props1;
  44. CLzma2EncProps props2;
  45. CLzmaEncProps *props;
  46. if (!LZMA_HANDLE_VALID(handle) || encPropsSize != sizeof(*encProps)) {
  47. return SZ_ERROR_PARAM;
  48. }
  49. if (handle->LZMA2) {
  50. Lzma2EncProps_Init(&props2);
  51. props = &props2.lzmaProps;
  52. } else {
  53. LzmaEncProps_Init(&props1);
  54. props = &props1;
  55. }
  56. props->algo = encProps->Algorithm;
  57. props->dictSize = encProps->DictionarySize;
  58. props->fb = encProps->NumFastBytes;
  59. props->btMode = encProps->BTMode;
  60. props->numHashBytes = encProps->NumHashBytes;
  61. props->numThreads = encProps->NumThreads;
  62. if (handle->LZMA2) {
  63. props2.numBlockThreads_Max = encProps->NumBlockThreads;
  64. props2.blockSize = encProps->BlockSize;
  65. props2.numThreadGroups = encProps->NumThreadGroups;
  66. return Lzma2Enc_SetProps(handle->encoder2, &props2);
  67. } else {
  68. props1.writeEndMark = 1;
  69. return LzmaEnc_SetProps(handle->encoder1, &props1);
  70. }
  71. }
  72. SRes __stdcall LZMA_Encode(struct LZMAHandle *handle, ISeqInStream *inStream,
  73. ISeqOutStream *outStream, ICompressProgress *progress)
  74. {
  75. if (!LZMA_HANDLE_VALID(handle)) return SZ_ERROR_PARAM;
  76. if (handle->LZMA2) {
  77. Byte props;
  78. SizeT propsSize = sizeof(props);
  79. props = Lzma2Enc_WriteProperties(handle->encoder2);
  80. if (outStream->Write(outStream, &props, propsSize) != propsSize) {
  81. return SZ_ERROR_WRITE;
  82. };
  83. return Lzma2Enc_Encode2(handle->encoder2, outStream, NULL, 0, inStream, NULL, 0, progress);
  84. } else {
  85. Byte props[LZMA_PROPS_SIZE];
  86. SizeT propsSize = sizeof(props);
  87. RINOK(LzmaEnc_WriteProperties(handle->encoder1, props, &propsSize));
  88. if (outStream->Write(outStream, &props, propsSize) != propsSize) {
  89. return SZ_ERROR_WRITE;
  90. };
  91. return LzmaEnc_Encode(handle->encoder1, outStream, inStream, progress,
  92. &g_Alloc, &g_BigAlloc);
  93. }
  94. }
  95. SRes __stdcall LZMA_End(struct LZMAHandle *handle)
  96. {
  97. if (!LZMA_HANDLE_VALID(handle)) return SZ_ERROR_PARAM;
  98. handle->marker = 0;
  99. if (handle->LZMA2) {
  100. if (handle->encoder2) {
  101. Lzma2Enc_Destroy(handle->encoder2);
  102. }
  103. } else {
  104. if (handle->encoder1) {
  105. LzmaEnc_Destroy(handle->encoder1, &g_Alloc, &g_BigAlloc);
  106. }
  107. }
  108. free(handle);
  109. return SZ_OK;
  110. }