MCPcopy Create free account
hub / github.com/diasurgical/devilution / SCompCompress

Function SCompCompress

3rdParty/StormLib/src/SCompression.cpp:777–905  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

775};
776
777int STORMAPI SCompCompress(void * pvOutBuffer, int * pcbOutBuffer, void * pvInBuffer, int cbInBuffer, unsigned uCompressionMask, int nCmpType, int nCmpLevel)
778{
779 COMPRESS CompressFuncArray[0x10]; // Array of compression functions, applied sequentially
780 unsigned char CompressByte[0x10]; // CompressByte for each method in the CompressFuncArray array
781 unsigned char * pbWorkBuffer = NULL; // Temporary storage for decompressed data
782 unsigned char * pbOutBuffer = (unsigned char *)pvOutBuffer;
783 unsigned char * pbOutput = (unsigned char *)pvOutBuffer;// Current output buffer
784 unsigned char * pbInput = (unsigned char *)pvInBuffer; // Current input buffer
785 int nCompressCount = 0;
786 int nCompressIndex = 0;
787 int nAtLeastOneCompressionDone = 0;
788 int cbOutBuffer = 0;
789 int cbInLength = cbInBuffer;
790 int nResult = 1;
791
792 // Check for valid parameters
793 if(!pcbOutBuffer || *pcbOutBuffer < cbInBuffer || !pvOutBuffer || !pvInBuffer)
794 {
795 SetLastError(ERROR_INVALID_PARAMETER);
796 return 0;
797 }
798
799 // Zero input length brings zero output length
800 if(cbInBuffer == 0)
801 {
802 *pcbOutBuffer = 0;
803 return true;
804 }
805
806 // Setup the compression function array
807 if(uCompressionMask == MPQ_COMPRESSION_LZMA)
808 {
809#ifdef FULL
810 CompressFuncArray[0] = Compress_LZMA;
811 CompressByte[0] = (char)uCompressionMask;
812 nCompressCount = 1;
813#else
814 assert(0);
815#endif
816 }
817 else
818 {
819 // Fill the compressions array
820 for(size_t i = 0; i < (sizeof(cmp_table) / sizeof(TCompressTable)); i++)
821 {
822 // If the mask agrees, insert the compression function to the array
823 if(uCompressionMask & cmp_table[i].uMask)
824 {
825 CompressFuncArray[nCompressCount] = cmp_table[i].Compress;
826 CompressByte[nCompressCount] = (unsigned char)cmp_table[i].uMask;
827 uCompressionMask &= ~cmp_table[i].uMask;
828 nCompressCount++;
829 }
830 }
831
832 // If at least one of the compressions remaing unknown, return an error
833 if(uCompressionMask != 0)
834 {

Callers 3

SaveMpqTableFunction · 0.85
SaveExtTableFunction · 0.85
WriteSectorChecksumsFunction · 0.85

Calls 1

SetLastErrorFunction · 0.85

Tested by

no test coverage detected