LzmaRamDecode.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* LzmaRamDecode.c */
  2. #include "LzmaRamDecode.h"
  3. #ifdef _SZ_ONE_DIRECTORY
  4. #include "LzmaDecode.h"
  5. #include "BranchX86.h"
  6. #else
  7. #include "../../../../C/Compress/Lzma/LzmaDecode.h"
  8. #include "../../../../C/Compress/Branch/BranchX86.h"
  9. #endif
  10. #define LZMA_PROPS_SIZE 14
  11. #define LZMA_SIZE_OFFSET 6
  12. int LzmaRamGetUncompressedSize(
  13. const unsigned char *inBuffer,
  14. size_t inSize,
  15. size_t *outSize)
  16. {
  17. unsigned int i;
  18. if (inSize < LZMA_PROPS_SIZE)
  19. return 1;
  20. *outSize = 0;
  21. for(i = 0; i < sizeof(size_t); i++)
  22. *outSize += ((size_t)inBuffer[LZMA_SIZE_OFFSET + i]) << (8 * i);
  23. for(; i < 8; i++)
  24. if (inBuffer[LZMA_SIZE_OFFSET + i] != 0)
  25. return 1;
  26. return 0;
  27. }
  28. #define SZE_DATA_ERROR (1)
  29. #define SZE_OUTOFMEMORY (2)
  30. int LzmaRamDecompress(
  31. const unsigned char *inBuffer,
  32. size_t inSize,
  33. unsigned char *outBuffer,
  34. size_t outSize,
  35. size_t *outSizeProcessed,
  36. void * (*allocFunc)(size_t size),
  37. void (*freeFunc)(void *))
  38. {
  39. CLzmaDecoderState state; /* it's about 24 bytes structure, if int is 32-bit */
  40. int result;
  41. SizeT outSizeProcessedLoc;
  42. SizeT inProcessed;
  43. int useFilter;
  44. if (inSize < LZMA_PROPS_SIZE)
  45. return 1;
  46. useFilter = inBuffer[0];
  47. *outSizeProcessed = 0;
  48. if (useFilter > 1)
  49. return 1;
  50. if (LzmaDecodeProperties(&state.Properties, inBuffer + 1, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
  51. return 1;
  52. state.Probs = (CProb *)allocFunc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
  53. if (state.Probs == 0)
  54. return SZE_OUTOFMEMORY;
  55. result = LzmaDecode(&state,
  56. inBuffer + LZMA_PROPS_SIZE, (SizeT)inSize - LZMA_PROPS_SIZE, &inProcessed,
  57. outBuffer, (SizeT)outSize, &outSizeProcessedLoc);
  58. freeFunc(state.Probs);
  59. if (result != LZMA_RESULT_OK)
  60. return 1;
  61. *outSizeProcessed = (size_t)outSizeProcessedLoc;
  62. if (useFilter == 1)
  63. {
  64. UInt32 x86State;
  65. x86_Convert_Init(x86State);
  66. x86_Convert(outBuffer, (SizeT)outSizeProcessedLoc, 0, &x86State, 0);
  67. }
  68. return 0;
  69. }