|
| 1 | +/** |
| 2 | + * hpatch - Apply patch to restore original data |
| 3 | + * Created based on HDiffPatch library |
| 4 | + */ |
| 5 | +#include "hpatch.h" |
| 6 | +#include "../HDiffPatch/libHDiffPatch/HPatch/patch.h" |
| 7 | +#include <stdexcept> |
| 8 | + |
| 9 | +#define _CompressPlugin_lzma2 |
| 10 | +#define _IsNeedIncludeDefaultCompressHead 0 |
| 11 | +#include "../lzma/C/LzmaDec.h" |
| 12 | +#include "../lzma/C/Lzma2Dec.h" |
| 13 | +#include "../HDiffPatch/decompress_plugin_demo.h" |
| 14 | + |
| 15 | +// Listener for patch_single_stream_by |
| 16 | +struct PatchListener { |
| 17 | + hpatch_TDecompress* decompressPlugin; |
| 18 | + std::vector<uint8_t>* tempCache; |
| 19 | +}; |
| 20 | + |
| 21 | +static hpatch_BOOL onDiffInfo(sspatch_listener_t* listener, |
| 22 | + const hpatch_singleCompressedDiffInfo* info, |
| 23 | + hpatch_TDecompress** out_decompressPlugin, |
| 24 | + unsigned char** out_temp_cache, |
| 25 | + unsigned char** out_temp_cacheEnd) { |
| 26 | + PatchListener* self = (PatchListener*)listener->import; |
| 27 | + |
| 28 | + // Allocate temp cache: stepMemSize + I/O cache |
| 29 | + size_t cacheSize = (size_t)info->stepMemSize + hpatch_kStreamCacheSize * 4; |
| 30 | + self->tempCache->resize(cacheSize); |
| 31 | + |
| 32 | + *out_decompressPlugin = self->decompressPlugin; |
| 33 | + *out_temp_cache = self->tempCache->data(); |
| 34 | + *out_temp_cacheEnd = self->tempCache->data() + cacheSize; |
| 35 | + |
| 36 | + return hpatch_TRUE; |
| 37 | +} |
| 38 | + |
| 39 | +void hpatch(const uint8_t* old, size_t oldsize, |
| 40 | + const uint8_t* diff, size_t diffsize, |
| 41 | + std::vector<uint8_t>& out_newBuf) { |
| 42 | + |
| 43 | + // Get diff info to determine output size |
| 44 | + hpatch_singleCompressedDiffInfo diffInfo; |
| 45 | + if (!getSingleCompressedDiffInfo_mem(&diffInfo, diff, diff + diffsize)) { |
| 46 | + throw std::runtime_error("getSingleCompressedDiffInfo_mem() failed, invalid diff data!"); |
| 47 | + } |
| 48 | + |
| 49 | + // Verify old data size matches |
| 50 | + if (diffInfo.oldDataSize != oldsize) { |
| 51 | + throw std::runtime_error("Old data size mismatch!"); |
| 52 | + } |
| 53 | + |
| 54 | + // Allocate output buffer |
| 55 | + out_newBuf.resize((size_t)diffInfo.newDataSize); |
| 56 | + |
| 57 | + // Setup decompressor |
| 58 | + hpatch_TDecompress* decompressPlugin = &lzma2DecompressPlugin; |
| 59 | + |
| 60 | + // Setup listener |
| 61 | + std::vector<uint8_t> tempCache; |
| 62 | + PatchListener patchListener; |
| 63 | + patchListener.decompressPlugin = decompressPlugin; |
| 64 | + patchListener.tempCache = &tempCache; |
| 65 | + |
| 66 | + sspatch_listener_t listener; |
| 67 | + listener.import = &patchListener; |
| 68 | + listener.onDiffInfo = onDiffInfo; |
| 69 | + listener.onPatchFinish = nullptr; |
| 70 | + |
| 71 | + // Execute patch |
| 72 | + if (!patch_single_stream_by_mem(&listener, |
| 73 | + out_newBuf.data(), out_newBuf.data() + out_newBuf.size(), |
| 74 | + old, old + oldsize, |
| 75 | + diff, diff + diffsize)) { |
| 76 | + throw std::runtime_error("patch_single_stream_by_mem() failed!"); |
| 77 | + } |
| 78 | +} |
0 commit comments