Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
hdiff() ok
  • Loading branch information
sisong committed Apr 7, 2021
commit ff3c1a1bf70c16a0567c4e4d939958803b4f12d7
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ dist
# TernJS port file
.tern-port
.idea
.vscode/
1 change: 0 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"src/main.cc",
"src/hdiff.cpp",
"HDiffPatch/libHDiffPatch/HPatch/patch.c",
"HDiffPatch/file_for_patch.c",
"HDiffPatch/libHDiffPatch/HDiff/diff.cpp",
"HDiffPatch/libHDiffPatch/HDiff/private_diff/bytes_rle.cpp",
"HDiffPatch/libHDiffPatch/HDiff/private_diff/suffix_string.cpp",
Expand Down
26 changes: 26 additions & 0 deletions src/hdiff.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Created by housisong on 2021.04.07.
*/
#include "hdiff.h"
#include "HDiffPatch/libHDiffPatch/HDiff/diff.h"

#define _CompressPlugin_lzma2
#define _IsNeedIncludeDefaultCompressHead 0
#include "lzma/C/LzmaEnc.h"
#include "lzma/C/Lzma2Enc.h"
#include "lzma/C/MtCoder.h"
#include "HDiffPatch/compress_plugin_demo.h"

void hdiff(const uint8_t* old,size_t oldsize,const uint8_t* _new,size_t newsize,
std::vector<uint8_t>& out_codeBuf){
const int myBestSingleMatchScore=3;
const size_t myBestStepMemSize=kDefaultStepMemSize;
const size_t myBestDictSize=(1<<20)*8; //8MB mem

TCompressPlugin_lzma2 compressPlugin=lzma2CompressPlugin;
compressPlugin.compress_level=9;
compressPlugin.dict_size=myBestDictSize;
compressPlugin.thread_num=1;
create_single_compressed_diff(_new,_new+newsize,old,old+oldsize,out_codeBuf,0,
&compressPlugin.base,myBestSingleMatchScore,myBestStepMemSize);
}
13 changes: 13 additions & 0 deletions src/hdiff.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Created by housisong on 2021.04.07.
*/

#ifndef HDIFFPATCH_DIFF_H
#define HDIFFPATCH_DIFF_H
#include <stdint.h>
#include <vector>

voif hdiff(const uint8_t* old,size_t oldsize,const uint8_t* _new,size_t newsize,
std::vector<uint8_t>& out_codeBuf);

#endif
85 changes: 85 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Created by housisong on 2021.04.07.
*/
#include <nan.h>
#include <node.h>
#include <node_buffer.h>
#include <cstdlib>
#include "hdiff.h"

using namespace std;

namespace hdiffpatchNode
{
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Function;
using v8::MaybeLocal;
using v8::Null;
using v8::Boolean;
using v8::Exception;

struct DiffStreamOpaque {
Isolate* isolate;
Local<Function> cb;
};

static int callback_write(DiffStreamOpaque* opaque, const void* buffer, size_t size)
{

Local<Object> returnObj = node::Buffer::Copy(opaque->isolate, (const char*)buffer, size).ToLocalChecked();

Local<Value> argv[1] = { returnObj };
// opaque->cb->Call(Nan::GetCurrentContext()->Global(), Null(opaque->isolate), 1, argv);

Nan::MakeCallback(Nan::GetCurrentContext()->Global(), opaque->cb, 1, argv);

return 0;
}

void diff(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);

if (!node::Buffer::HasInstance(args[0]) || !node::Buffer::HasInstance(args[1]) || !args[2]->IsFunction()) {
Nan::ThrowError("Invalid arguments.");
}

char* oldData = node::Buffer::Data(args[0]);
size_t oldLength = node::Buffer::Length(args[0]);
char* newData = node::Buffer::Data(args[1]);
size_t newLength = node::Buffer::Length(args[1]);

DiffStreamOpaque streamOpaque;
streamOpaque.isolate = isolate;
streamOpaque.cb = Local<Function>::Cast(args[2]);

std::vector<uint8_t> codeBuf;
try{
hdiff((const uint8_t*)oldData,oldLength,(const uint8_t*)newData,newLength,codeBuf);
}catch(const std::exception& e){
Nan::ThrowError("Create hdiff failed.");
}
if (0!=callback_write(&streamOpaque,codeBuf.data(),codeBuf.size()))
Nan::ThrowError("Write DiffStreamOpaque failed.");

// args.GetReturnValue().Set(returnObj);
// args.GetReturnValue().Set(String::NewFromUtf8(isolate, bufferData, String::kNormalString, bufferLength));
}

void init(Local<Object> exports)
{
Isolate* isolate = exports->GetIsolate();
HandleScope scope(isolate);

NODE_SET_METHOD(exports, "diff", diff);
}

NODE_MODULE(hdiffpatch, init)

} // namespace hdiffpatch