Skip to content

Commit ff3c1a1

Browse files
committed
hdiff() ok
1 parent 8552fdd commit ff3c1a1

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,4 @@ dist
104104
# TernJS port file
105105
.tern-port
106106
.idea
107+
.vscode/

binding.gyp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"src/main.cc",
77
"src/hdiff.cpp",
88
"HDiffPatch/libHDiffPatch/HPatch/patch.c",
9-
"HDiffPatch/file_for_patch.c",
109
"HDiffPatch/libHDiffPatch/HDiff/diff.cpp",
1110
"HDiffPatch/libHDiffPatch/HDiff/private_diff/bytes_rle.cpp",
1211
"HDiffPatch/libHDiffPatch/HDiff/private_diff/suffix_string.cpp",

src/hdiff.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Created by housisong on 2021.04.07.
3+
*/
4+
#include "hdiff.h"
5+
#include "HDiffPatch/libHDiffPatch/HDiff/diff.h"
6+
7+
#define _CompressPlugin_lzma2
8+
#define _IsNeedIncludeDefaultCompressHead 0
9+
#include "lzma/C/LzmaEnc.h"
10+
#include "lzma/C/Lzma2Enc.h"
11+
#include "lzma/C/MtCoder.h"
12+
#include "HDiffPatch/compress_plugin_demo.h"
13+
14+
void hdiff(const uint8_t* old,size_t oldsize,const uint8_t* _new,size_t newsize,
15+
std::vector<uint8_t>& out_codeBuf){
16+
const int myBestSingleMatchScore=3;
17+
const size_t myBestStepMemSize=kDefaultStepMemSize;
18+
const size_t myBestDictSize=(1<<20)*8; //8MB mem
19+
20+
TCompressPlugin_lzma2 compressPlugin=lzma2CompressPlugin;
21+
compressPlugin.compress_level=9;
22+
compressPlugin.dict_size=myBestDictSize;
23+
compressPlugin.thread_num=1;
24+
create_single_compressed_diff(_new,_new+newsize,old,old+oldsize,out_codeBuf,0,
25+
&compressPlugin.base,myBestSingleMatchScore,myBestStepMemSize);
26+
}

src/hdiff.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Created by housisong on 2021.04.07.
3+
*/
4+
5+
#ifndef HDIFFPATCH_DIFF_H
6+
#define HDIFFPATCH_DIFF_H
7+
#include <stdint.h>
8+
#include <vector>
9+
10+
voif hdiff(const uint8_t* old,size_t oldsize,const uint8_t* _new,size_t newsize,
11+
std::vector<uint8_t>& out_codeBuf);
12+
13+
#endif

src/main.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Created by housisong on 2021.04.07.
3+
*/
4+
#include <nan.h>
5+
#include <node.h>
6+
#include <node_buffer.h>
7+
#include <cstdlib>
8+
#include "hdiff.h"
9+
10+
using namespace std;
11+
12+
namespace hdiffpatchNode
13+
{
14+
using v8::FunctionCallbackInfo;
15+
using v8::HandleScope;
16+
using v8::Isolate;
17+
using v8::Local;
18+
using v8::Object;
19+
using v8::String;
20+
using v8::Value;
21+
using v8::Function;
22+
using v8::MaybeLocal;
23+
using v8::Null;
24+
using v8::Boolean;
25+
using v8::Exception;
26+
27+
struct DiffStreamOpaque {
28+
Isolate* isolate;
29+
Local<Function> cb;
30+
};
31+
32+
static int callback_write(DiffStreamOpaque* opaque, const void* buffer, size_t size)
33+
{
34+
35+
Local<Object> returnObj = node::Buffer::Copy(opaque->isolate, (const char*)buffer, size).ToLocalChecked();
36+
37+
Local<Value> argv[1] = { returnObj };
38+
// opaque->cb->Call(Nan::GetCurrentContext()->Global(), Null(opaque->isolate), 1, argv);
39+
40+
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), opaque->cb, 1, argv);
41+
42+
return 0;
43+
}
44+
45+
void diff(const FunctionCallbackInfo<Value>& args) {
46+
Isolate* isolate = args.GetIsolate();
47+
HandleScope scope(isolate);
48+
49+
if (!node::Buffer::HasInstance(args[0]) || !node::Buffer::HasInstance(args[1]) || !args[2]->IsFunction()) {
50+
Nan::ThrowError("Invalid arguments.");
51+
}
52+
53+
char* oldData = node::Buffer::Data(args[0]);
54+
size_t oldLength = node::Buffer::Length(args[0]);
55+
char* newData = node::Buffer::Data(args[1]);
56+
size_t newLength = node::Buffer::Length(args[1]);
57+
58+
DiffStreamOpaque streamOpaque;
59+
streamOpaque.isolate = isolate;
60+
streamOpaque.cb = Local<Function>::Cast(args[2]);
61+
62+
std::vector<uint8_t> codeBuf;
63+
try{
64+
hdiff((const uint8_t*)oldData,oldLength,(const uint8_t*)newData,newLength,codeBuf);
65+
}catch(const std::exception& e){
66+
Nan::ThrowError("Create hdiff failed.");
67+
}
68+
if (0!=callback_write(&streamOpaque,codeBuf.data(),codeBuf.size()))
69+
Nan::ThrowError("Write DiffStreamOpaque failed.");
70+
71+
// args.GetReturnValue().Set(returnObj);
72+
// args.GetReturnValue().Set(String::NewFromUtf8(isolate, bufferData, String::kNormalString, bufferLength));
73+
}
74+
75+
void init(Local<Object> exports)
76+
{
77+
Isolate* isolate = exports->GetIsolate();
78+
HandleScope scope(isolate);
79+
80+
NODE_SET_METHOD(exports, "diff", diff);
81+
}
82+
83+
NODE_MODULE(hdiffpatch, init)
84+
85+
} // namespace hdiffpatch

0 commit comments

Comments
 (0)