static BOOL decompress(LPVOID src, DWORD src_sz, HANDLE out, IProgressDialog *pd) { elzma_decompress_handle h; struct DataStream ds; int rc; h = elzma_decompress_alloc(); if (h == NULL) { show_error(L"Out of memory"); return false; } ds.in_data = (unsigned char*)src; ds.in_len = src_sz; ds.out = out; ds.pd = pd; rc = elzma_decompress_run(h, input_callback, (void *) &ds, output_callback, (void *) &ds, ELZMA_lzip); if (rc != ELZMA_E_OK) { if (!output_error_shown) show_detailed_error(L"Failed to decompress portable data", L"", rc); elzma_decompress_free(&h); return false; } elzma_decompress_free(&h); return true; }
int simpleDecompress(elzma_file_format format, const unsigned char * inData, size_t inLen, unsigned char ** outData, size_t * outLen) { int rc = 0; elzma_decompress_handle hand; hand = elzma_decompress_alloc(); /* now run the compression */ { struct dataStream ds; ds.inData = inData; ds.inLen = inLen; ds.outData = NULL; ds.outLen = 0; rc = elzma_decompress_run(hand, inputCallback, (void *) &ds, outputCallback, (void *) &ds, format); if (rc != ELZMA_E_OK) { if (ds.outData != NULL) free(ds.outData); elzma_decompress_free(&hand); return rc; } *outData = ds.outData; *outLen = ds.outLen; } return rc; }
/////////////////////////////////////////////////////////////////////////// // Reads packaged files. /////////////////////////////////////////////////////////////////////////// void Binpressor::ReadFiles() { if (m_packages.empty()) return; std::cout << "Reading Package File(s)..." << std::endl; for (auto it = m_packages.begin(); it != m_packages.end();) { std::ifstream fstream; fstream.open(*it, std::fstream::out | std::fstream::binary); if (fstream.is_open()) { // Read header data char majVersion[4]; char minVersion[4]; char nameBuffer[8]; char extBuffer[8]; char sizeBuffer[8]; fstream.read(majVersion, 4); fstream.read(minVersion, 4); fstream.read(nameBuffer, 8); fstream.read(extBuffer, 8); fstream.read(sizeBuffer, 8); if (atoi(majVersion) != MAJOR_VERSION || atoi(minVersion) != MINOR_VERSION) { std::cout << "Incompatible package version." << std::endl; fstream.close(); return; } int nameBuff = atoi(nameBuffer); int extBuff = atoi(extBuffer); int sizeBuff = atoi(sizeBuffer); #if !defined(_WIN64) if (sizeBuff < 0 || sizeBuff >= 2140000000) // Leave some space for descriptors. { std::cout << "Total file size too large to read." << std::endl; std::cout << "Please use 64-bit version instead." << std::endl; std::cout << std::endl; std::cout << "Unpackaging canceled." << std::endl; return; } #endif while (!fstream.eof()) { InDescriptor* descriptor = new InDescriptor(); descriptor->name = new char[nameBuff]; descriptor->ext = new char[extBuff]; descriptor->size = new char[sizeBuff]; memset(descriptor->name, 0, nameBuff); memset(descriptor->ext, 0, extBuff); memset(descriptor->size, 0, sizeBuff); fstream.read(descriptor->name, nameBuff); fstream.read(descriptor->ext, extBuff); fstream.read(descriptor->size, sizeBuff); if (strcmp(descriptor->size, "") == 0) { delete[] descriptor->name; delete[] descriptor->ext; delete[] descriptor->size; delete descriptor; break; } descriptor->data = new char[strtoul(descriptor->size, NULL, 0)]; memset(descriptor->data, 0, strtoul(descriptor->size, NULL, 0)); fstream.read(descriptor->data, strtoul(descriptor->size, NULL, 0)); std::cout << "[Reading - 100%] " << descriptor->name << descriptor->ext << std::endl; dataStream ds; ds.inData = descriptor->data; ds.inLen = strtoul(descriptor->size, NULL, 0); ds.outData = NULL; ds.outLen = 0; elzma_decompress_handle handle = elzma_decompress_alloc(); int rc = elzma_decompress_run(handle, inputCallback, (void *) &ds, outputCallback, (void *) &ds, ELZMA_lzma); if (rc != ELZMA_E_OK) { if (ds.outData != NULL) free(ds.outData); elzma_decompress_free(&handle); return; } std::cout << "[Uncompressed - 100%] " << descriptor->name << descriptor->ext << " - " << descriptor->size << " -> " << ds.outLen << std::endl; memset(descriptor->size, 0, SIZE_BUFFER); sprintf_s(descriptor->size, SIZE_BUFFER, "%i", ds.outLen); delete[] descriptor->data; descriptor->data = new char[ds.outLen]; memcpy(descriptor->data, ds.outData, ds.outLen); elzma_decompress_free(&handle); m_inFiles.push_back(descriptor); } std::cout << std::endl; it = m_packages.erase(it); fstream.close(); } else { std::cout << "ERROR: Could not open package for reading:" << std::endl; std::cout << "package.bin" << std::endl; } } std::cout << "Reading complete." << std::endl << std::endl; }