|
1 | 1 | #include "hdiff.h" |
2 | 2 | #include "../HDiffPatch/libHDiffPatch/HDiff/diff.h" |
| 3 | +#include "../HDiffPatch/file_for_patch.h" |
3 | 4 | #include <stdexcept> |
4 | 5 |
|
5 | 6 | #define _CompressPlugin_lzma2 |
@@ -30,3 +31,79 @@ void hdiff(const uint8_t* old, size_t oldsize, const uint8_t* _new, size_t newsi |
30 | 31 | throw std::runtime_error("check_single_compressed_diff() failed, diff code error!"); |
31 | 32 | } |
32 | 33 | } |
| 34 | + |
| 35 | +void hdiff_stream(const char* oldPath,const char* newPath,const char* outDiffPath){ |
| 36 | + if (!oldPath || !newPath || !outDiffPath) { |
| 37 | + throw std::runtime_error("Invalid file path."); |
| 38 | + } |
| 39 | + |
| 40 | + const size_t myBestDictSize = (1 << 20) * 8; // 固定 8MB,与 v1.0.6 一致 |
| 41 | + hpatch_TDecompress* decompressPlugin = &lzma2DecompressPlugin; |
| 42 | + TCompressPlugin_lzma2 compressPlugin = lzma2CompressPlugin; |
| 43 | + compressPlugin.compress_level = 9; |
| 44 | + compressPlugin.dict_size = myBestDictSize; |
| 45 | + compressPlugin.thread_num = 1; |
| 46 | + |
| 47 | + hpatch_TFileStreamInput oldStream; |
| 48 | + hpatch_TFileStreamInput newStream; |
| 49 | + hpatch_TFileStreamOutput diffOutStream; |
| 50 | + hpatch_TFileStreamInput diffInStream; |
| 51 | + hpatch_TFileStreamInput_init(&oldStream); |
| 52 | + hpatch_TFileStreamInput_init(&newStream); |
| 53 | + hpatch_TFileStreamOutput_init(&diffOutStream); |
| 54 | + hpatch_TFileStreamInput_init(&diffInStream); |
| 55 | + |
| 56 | + bool oldOpened = false; |
| 57 | + bool newOpened = false; |
| 58 | + bool diffOutOpened = false; |
| 59 | + bool diffInOpened = false; |
| 60 | + |
| 61 | + try { |
| 62 | + if (!hpatch_TFileStreamInput_open(&oldStream, oldPath)) { |
| 63 | + throw std::runtime_error("open old file failed."); |
| 64 | + } |
| 65 | + oldOpened = true; |
| 66 | + if (!hpatch_TFileStreamInput_open(&newStream, newPath)) { |
| 67 | + throw std::runtime_error("open new file failed."); |
| 68 | + } |
| 69 | + newOpened = true; |
| 70 | + if (!hpatch_TFileStreamOutput_open(&diffOutStream, outDiffPath, ~(hpatch_StreamPos_t)0)) { |
| 71 | + throw std::runtime_error("open diff file for write failed."); |
| 72 | + } |
| 73 | + diffOutOpened = true; |
| 74 | + hpatch_TFileStreamOutput_setRandomOut(&diffOutStream, hpatch_TRUE); |
| 75 | + |
| 76 | + create_compressed_diff_stream(&newStream.base, &oldStream.base, &diffOutStream.base, |
| 77 | + &compressPlugin.base, kMatchBlockSize_default); |
| 78 | + |
| 79 | + if (!hpatch_TFileStreamOutput_close(&diffOutStream)) { |
| 80 | + throw std::runtime_error("close diff file failed."); |
| 81 | + } |
| 82 | + diffOutOpened = false; |
| 83 | + |
| 84 | + if (!hpatch_TFileStreamInput_open(&diffInStream, outDiffPath)) { |
| 85 | + throw std::runtime_error("open diff file for read failed."); |
| 86 | + } |
| 87 | + diffInOpened = true; |
| 88 | + if (!check_compressed_diff_stream(&newStream.base, &oldStream.base, |
| 89 | + &diffInStream.base, decompressPlugin)) { |
| 90 | + throw std::runtime_error("check_compressed_diff_stream() failed, diff code error!"); |
| 91 | + } |
| 92 | + } catch (...) { |
| 93 | + if (diffInOpened) hpatch_TFileStreamInput_close(&diffInStream); |
| 94 | + if (diffOutOpened) hpatch_TFileStreamOutput_close(&diffOutStream); |
| 95 | + if (newOpened) hpatch_TFileStreamInput_close(&newStream); |
| 96 | + if (oldOpened) hpatch_TFileStreamInput_close(&oldStream); |
| 97 | + throw; |
| 98 | + } |
| 99 | + |
| 100 | + if (diffInOpened && !hpatch_TFileStreamInput_close(&diffInStream)) { |
| 101 | + throw std::runtime_error("close diff file failed."); |
| 102 | + } |
| 103 | + if (newOpened && !hpatch_TFileStreamInput_close(&newStream)) { |
| 104 | + throw std::runtime_error("close new file failed."); |
| 105 | + } |
| 106 | + if (oldOpened && !hpatch_TFileStreamInput_close(&oldStream)) { |
| 107 | + throw std::runtime_error("close old file failed."); |
| 108 | + } |
| 109 | +} |
0 commit comments