Skip to content

Commit b74cb6b

Browse files
committed
hpatch
1 parent 064b4ad commit b74cb6b

File tree

10 files changed

+559
-123
lines changed

10 files changed

+559
-123
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"sources": [
66
"src/main.cc",
77
"src/hdiff.cpp",
8+
"src/hpatch.cpp",
89
"HDiffPatch/libHDiffPatch/HPatch/patch.c",
910
"HDiffPatch/libHDiffPatch/HDiff/diff.cpp",
1011
"HDiffPatch/libHDiffPatch/HDiff/private_diff/bytes_rle.cpp",

index.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* Created by housisong on 2021.04.06.
3-
*/
4-
51
var native;
62
try {
73
native = require('./build/Release/hdiffpatch');
@@ -10,11 +6,25 @@ try {
106
}
117
exports.native = native;
128

13-
exports.diff = function(oldBuf, newBuf) {
14-
var buffers = [];
15-
native.diff(oldBuf, newBuf, function(output){
16-
buffers.push(output);
9+
// 同步版本
10+
exports.diff = native.diff;
11+
exports.patch = native.patch;
12+
13+
// 异步版本 (Promise)
14+
exports.diffAsync = function(oldBuf, newBuf) {
15+
return new Promise((resolve, reject) => {
16+
native.diff(oldBuf, newBuf, (err, result) => {
17+
if (err) reject(err);
18+
else resolve(result);
19+
});
1720
});
21+
};
1822

19-
return Buffer.concat(buffers);
20-
}
23+
exports.patchAsync = function(oldBuf, diffBuf) {
24+
return new Promise((resolve, reject) => {
25+
native.patch(oldBuf, diffBuf, (err, result) => {
26+
if (err) reject(err);
27+
else resolve(result);
28+
});
29+
});
30+
};

index_test.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "node test/test.js",
8+
"benchmark": "node --expose-gc test/benchmark.js",
89
"prepack": "git submodule update --init --recursive"
910
},
1011
"gypfile": true,

src/hdiff.cpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/hpatch.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

src/hpatch.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* hpatch - Apply patch to restore original data
3+
* Created based on HDiffPatch library
4+
*/
5+
6+
#ifndef HDIFFPATCH_PATCH_H
7+
#define HDIFFPATCH_PATCH_H
8+
#include <stddef.h>
9+
#include <stdint.h>
10+
#include <vector>
11+
12+
void hpatch(const uint8_t* old, size_t oldsize,
13+
const uint8_t* diff, size_t diffsize,
14+
std::vector<uint8_t>& out_newBuf);
15+
16+
#endif

0 commit comments

Comments
 (0)