Skip to content

Commit

Permalink
feat:not use lz4
Browse files Browse the repository at this point in the history
  • Loading branch information
qiang101.wang authored and andycall committed Sep 13, 2024
1 parent 2328d97 commit a21fce2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3,508 deletions.
5 changes: 0 additions & 5 deletions bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs")
add_library(modb STATIC
third_party/modp_b64/modp_b64.cc
)
add_library(lz4 STATIC
third_party/lz4/lz4.c
)

if (NOT MSVC)
list(APPEND QUICK_JS_SOURCE third_party/quickjs/src/libbf.c)
Expand Down Expand Up @@ -241,10 +238,8 @@ if ($ENV{WEBF_JS_ENGINE} MATCHES "quickjs")

list(APPEND BRIDGE_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/third_party)
list(APPEND BRIDGE_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/modp_b64/include)
list(APPEND BRIDGE_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/lz4)
list(APPEND BRIDGE_LINK_LIBS quickjs)
list(APPEND BRIDGE_LINK_LIBS modb)
list(APPEND BRIDGE_LINK_LIBS lz4)

list(APPEND BRIDGE_SOURCE
# Binding files
Expand Down
27 changes: 6 additions & 21 deletions bridge/core/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "core/page.h"
#include "foundation/wbc.h"
#include "multiple_threading/dispatcher.h"
#include "third_party/lz4/lz4.h"

namespace webf {

Expand Down Expand Up @@ -100,35 +99,21 @@ void evaluateWbcInternal(void* page_,

page->dartIsolateContext()->profiler()->StartTrackEvaluation(profile_id);

size_t dataBlockSize;
size_t dataBlockStart;
size_t dataBlockEnd;
bool is_success;
webf::Wbc wbc = webf::Wbc();
uint8_t* dataBlockBytes = wbc.prepareWbc(bytes, byte_len, &dataBlockSize);
if (dataBlockBytes == nullptr) {
bool prepareWbcRes = wbc.prepareWbc(bytes, byte_len, &dataBlockStart, &dataBlockEnd);
if (!prepareWbcRes) {
#if ENABLE_LOG
WEBF_LOG(ERROR) << "prepareWbc error" << std::endl;
#endif
is_success = false;
} else {
std::vector<char> decompressedBytes;
decompressedBytes.reserve(webf::Wbc::NODE_LZ4_BLOCK_MAX_SIZE);
int decompressedSize = LZ4_decompress_safe(reinterpret_cast<const char*>(dataBlockBytes), &decompressedBytes[0],
dataBlockSize, webf::Wbc::NODE_LZ4_BLOCK_MAX_SIZE);

free(dataBlockBytes);
dataBlockBytes = NULL;

if (decompressedSize < 0) {
#if ENABLE_LOG
WEBF_LOG(ERROR) << "LZ4 decompression failed with error code: " << decompressedSize << std::endl;
#endif
is_success = false;
} else {
#if ENABLE_LOG
WEBF_LOG(VERBOSE) << "LZ4 decompression success! " << decompressedSize << std::endl;
WEBF_LOG(VERBOSE) << "prepareWbc success! " << std::endl;
#endif
is_success = page->evaluateByteCode(reinterpret_cast<uint8_t*>(decompressedBytes.data()), decompressedSize);
}
is_success = page->evaluateByteCode(bytes + dataBlockStart, dataBlockEnd-dataBlockStart);
}

page->dartIsolateContext()->profiler()->FinishTrackEvaluation(profile_id);
Expand Down
38 changes: 11 additions & 27 deletions bridge/foundation/wbc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ uint32_t Wbc::calculateAdler32(const uint8_t* array, size_t len, uint32_t adler)
// ├───────────────────┬────┴─────────────┬─────┘────────
// │ Length │ CHUNK_TYPE │ END
// └───────────────────┴──────────────────┘ ────────
uint8_t* Wbc::prepareWbc(const uint8_t* bytes, size_t length, size_t* targetSize) {
bool Wbc::prepareWbc(const uint8_t *bytes, size_t length, size_t *targetStart, size_t *targetEnd) {
uint32_t signatureSize = sizeof(Wbc::WBC_SIGNATURE);
if (length < signatureSize || !verifySignature(bytes)) {
WEBF_LOG(ERROR) << "prepareWbc signatureSize is wrong" << std::endl;
return nullptr;
return false;
}

// Extracting header length
if (length < (signatureSize + Wbc::WBC_HEADER_LENGTH)) {
WEBF_LOG(ERROR) << "prepareWbc header length is wrong" << std::endl;
return nullptr;
return false;
}

uint32_t headerLength = convertBigEndianToUint32(bytes, signatureSize);
Expand All @@ -83,20 +83,20 @@ uint8_t* Wbc::prepareWbc(const uint8_t* bytes, size_t length, size_t* targetSize
// Calculating Adler32 checksum for header content
if (length < bodyOffset) {
WEBF_LOG(ERROR) << "prepareWbc header is wrong" << std::endl;
return nullptr;
return false;
}

uint32_t headerContentAdler32 = calculateAdler32(bytes + signatureSize, headerChecksumOffset - signatureSize);
uint32_t headerAdler32 = convertBigEndianToUint32(bytes, headerChecksumOffset);
if (headerContentAdler32 != headerAdler32) {
WEBF_LOG(ERROR) << "prepareWbc headerAdler32 is wrong" << std::endl;
return nullptr;
return false;
}

// Extracting body length
if (length < (bodyOffset + Wbc::WBC_BODY_LENGTH)) {
WEBF_LOG(ERROR) << "prepareWbc body length is wrong" << std::endl;
return nullptr;
return false;
}

uint32_t bodyLength = convertBigEndianToUint32(bytes, bodyOffset);
Expand All @@ -106,37 +106,21 @@ uint8_t* Wbc::prepareWbc(const uint8_t* bytes, size_t length, size_t* targetSize
// Calculating Adler32 checksum for body content
if (length < endOffset) {
WEBF_LOG(ERROR) << "prepareWbc body is wrong" << std::endl;
return nullptr;
return false;
}

uint32_t bodyContentAdler32 = calculateAdler32(bytes + bodyOffset, bodyChecksumOffset - bodyOffset);
uint32_t bodyAdler32 = convertBigEndianToUint32(bytes, bodyChecksumOffset);
if (bodyContentAdler32 != bodyAdler32) {
WEBF_LOG(ERROR) << "prepareWbc bodyAdler32 is wrong" << std::endl;
return nullptr;
return false;
}

uint32_t bodyChunkOffset = bodyOffset + Wbc::WBC_BODY_LENGTH + Wbc::WBC_BODY_CHUNK_TYPE_LENGTH;

// Parsed according to the format of node-lz4
// https://github.com/pierrec/node-lz4
uint32_t dataBlockSizeOffset =
bodyChunkOffset + Wbc::NODE_LZ4_MAGIC + Wbc::NODE_LZ4_DESCRIPTOR + Wbc::NODE_LZ4_DESCRIPTOR_CHECKSUM;
uint32_t dataBlockOffset = dataBlockSizeOffset + Wbc::NODE_LZ4_DATABLOCK_SIZE;

uint32_t dataBlockSize;
memcpy(&dataBlockSize, &bytes[dataBlockSizeOffset], Wbc::NODE_LZ4_DATABLOCK_SIZE);
*targetSize = dataBlockSize;

auto* dataBlockBytes = (uint8_t*)malloc(dataBlockSize);
if (!dataBlockBytes) {
WEBF_LOG(ERROR) << "prepareWbc malloc dataBlockBytes failed!" << std::endl;
return nullptr;
}

memcpy(dataBlockBytes, bytes + dataBlockOffset, dataBlockSize);

return dataBlockBytes;
*targetStart = bodyChunkOffset;
*targetEnd = endOffset - Wbc::WBC_BODY_CHECKSUM_LENGTH;
return true;
}

} // namespace webf
10 changes: 5 additions & 5 deletions bridge/foundation/wbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ class Wbc {
static const int32_t WBC_BODY_CHECKSUM_LENGTH = 4;

// The length of node-lz4 file signature
static const int32_t NODE_LZ4_MAGIC = 4;
// static const int32_t NODE_LZ4_MAGIC = 4;

// The file descriptor length of node-lz4
static const int32_t NODE_LZ4_DESCRIPTOR = 2;
// static const int32_t NODE_LZ4_DESCRIPTOR = 2;

// File CHECKSUM length of node-lz4
static const int32_t NODE_LZ4_DESCRIPTOR_CHECKSUM = 1;
// static const int32_t NODE_LZ4_DESCRIPTOR_CHECKSUM = 1;

// The length of node-lz4’s real compressed content size
static const int32_t NODE_LZ4_DATABLOCK_SIZE = 4;
// static const int32_t NODE_LZ4_DATABLOCK_SIZE = 4;

// Check whether the wbc file is correct and return dataBlockBytes
// https://github.com/openwebf/rfc/blob/main/working/wbc1.en-US.md
uint8_t* prepareWbc(const uint8_t* bytes, size_t length, size_t* targetSize);
bool prepareWbc(const uint8_t *bytes, size_t length, size_t *targetStart, size_t *targetEnd);

// Function to check if the beginning of `bytes` matches the `WBC_SIGNATURE`
bool verifySignature(const uint8_t* bytes);
Expand Down
Loading

0 comments on commit a21fce2

Please sign in to comment.