Remove PNG and ZLIB support from the project

- Eliminated all references to PNG and ZLIB dependencies in CMake configuration and project files.
- Updated documentation to reflect the removal of PNG support and related functionalities.
- Adjusted build scripts and source files to remove unused code related to PNG handling, ensuring a cleaner codebase.
This commit is contained in:
scawful
2025-09-28 12:03:29 -04:00
parent dfc5b329af
commit cb107ae69c
18 changed files with 56 additions and 549 deletions

View File

@@ -6,11 +6,7 @@
namespace yaze {
namespace core {
#if YAZE_LIB_PNG == 1
void CopyImageToClipboard(const std::vector<uint8_t>& data) {}
void GetImageFromClipboard(std::vector<uint8_t>& data, int& width,
int& height) {}
#endif
// PNG clipboard functionality removed
} // namespace core
} // namespace yaze

View File

@@ -7,10 +7,7 @@
namespace yaze {
namespace core {
#if YAZE_LIB_PNG == 1
void CopyImageToClipboard(const std::vector<uint8_t> &data);
void GetImageFromClipboard(std::vector<uint8_t> &data, int &width, int &height);
#endif
// PNG clipboard functionality removed
} // namespace core
} // namespace yaze

View File

@@ -6,42 +6,6 @@
#ifdef TARGET_OS_MAC
#import <Cocoa/Cocoa.h>
#if YAZE_LIB_PNG == 1
void yaze::core::CopyImageToClipboard(const std::vector<uint8_t>& pngData) {
NSData* data = [NSData dataWithBytes:pngData.data() length:pngData.size()];
NSImage* image = [[NSImage alloc] initWithData:data];
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:@[ image ]];
}
void yaze::core::GetImageFromClipboard(std::vector<uint8_t>& pixel_data, int& width, int& height) {
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
NSArray* classArray = [NSArray arrayWithObject:[NSImage class]];
NSDictionary* options = [NSDictionary dictionary];
NSImage* image = [pasteboard readObjectsForClasses:classArray options:options].firstObject;
if (!image) {
width = height = 0;
return;
}
// Assuming the image is in an RGBA format
CGImageRef cgImage = [image CGImageForProposedRect:nil context:nil hints:nil];
width = (int)CGImageGetWidth(cgImage);
height = (int)CGImageGetHeight(cgImage);
size_t bytesPerRow = 4 * width;
size_t totalBytes = bytesPerRow * height;
pixel_data.resize(totalBytes);
CGContextRef context = CGBitmapContextCreate(
pixel_data.data(), width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
CGContextRelease(context);
}
#endif // YAZE_LIB_PNG
// PNG clipboard functionality removed
#endif // TARGET_OS_MAC

View File

@@ -121,35 +121,13 @@ void GraphicsEditor::DrawGfxEditToolset() {
TableNextColumn();
if (Button(ICON_MD_CONTENT_COPY)) {
#if YAZE_LIB_PNG == 1
std::vector<uint8_t> png_data =
gfx::Arena::Get().gfx_sheets().at(current_sheet_).GetPngData();
core::CopyImageToClipboard(png_data);
#else
// PNG support disabled - show message or alternative action
status_ = absl::UnimplementedError("PNG export not available in this build");
#endif
status_ = absl::UnimplementedError("PNG export functionality removed");
}
HOVER_HINT("Copy to Clipboard");
TableNextColumn();
if (Button(ICON_MD_CONTENT_PASTE)) {
#if YAZE_LIB_PNG == 1
std::vector<uint8_t> png_data;
int width, height;
core::GetImageFromClipboard(png_data, width, height);
if (png_data.size() > 0) {
gfx::Arena::Get()
.mutable_gfx_sheets()
->at(current_sheet_)
.Create(width, height, 8, png_data);
Renderer::Get().UpdateBitmap(
&gfx::Arena::Get().mutable_gfx_sheets()->at(current_sheet_));
}
#else
// PNG support disabled - show message or alternative action
status_ = absl::UnimplementedError("PNG import not available in this build");
#endif
status_ = absl::UnimplementedError("PNG import functionality removed");
}
HOVER_HINT("Paste from Clipboard");

View File

@@ -124,17 +124,7 @@ void OverworldEditor::Initialize() {
});
gui::AddTableColumn(toolset_table_, "##CopyMap", [&]() {
if (Button(ICON_MD_CONTENT_COPY)) {
#if YAZE_LIB_PNG == 1
std::vector<uint8_t> png_data = maps_bmp_[current_map_].GetPngData();
if (png_data.size() > 0) {
core::CopyImageToClipboard(png_data);
} else {
status_ = absl::InternalError(
"Failed to convert overworld map surface to PNG");
}
#else
status_ = absl::UnimplementedError("PNG export not available in this build");
#endif
status_ = absl::UnimplementedError("PNG export functionality removed");
}
HOVER_HINT("Copy Map to Clipboard");
});

View File

@@ -1,9 +1,6 @@
#include "bitmap.h"
#include <SDL.h>
#if YAZE_LIB_PNG == 1
#include <png.h>
#endif
#include <cstdint>
#include <span>
@@ -15,170 +12,6 @@
namespace yaze {
namespace gfx {
#if YAZE_LIB_PNG == 1
namespace png_internal {
void PngWriteCallback(png_structp png_ptr, png_bytep data, png_size_t length) {
std::vector<uint8_t> *p = (std::vector<uint8_t> *)png_get_io_ptr(png_ptr);
p->insert(p->end(), data, data + length);
}
void PngReadCallback(png_structp png_ptr, png_bytep outBytes,
png_size_t byteCountToRead) {
png_voidp io_ptr = png_get_io_ptr(png_ptr);
if (!io_ptr) return;
std::vector<uint8_t> *png_data =
reinterpret_cast<std::vector<uint8_t> *>(io_ptr);
static size_t pos = 0; // Position to read from
if (pos + byteCountToRead <= png_data->size()) {
memcpy(outBytes, png_data->data() + pos, byteCountToRead);
pos += byteCountToRead;
} else {
png_error(png_ptr, "Read error in PngReadCallback");
}
}
} // namespace png_internal
bool ConvertSurfaceToPng(SDL_Surface *surface, std::vector<uint8_t> &buffer) {
png_structp png_ptr = png_create_write_struct("1.6.40", NULL, NULL, NULL);
if (!png_ptr) {
SDL_Log("Failed to create PNG write struct");
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
SDL_Log("Failed to create PNG info struct");
return false;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
SDL_Log("Error during PNG write");
return false;
}
png_set_write_fn(png_ptr, &buffer, png_internal::PngWriteCallback, NULL);
png_colorp pal_ptr;
/* Prepare chunks */
int colortype = PNG_COLOR_MASK_COLOR;
int i = 0;
SDL_Palette *pal;
if (surface->format->BytesPerPixel > 0 &&
surface->format->BytesPerPixel <= 8 && (pal = surface->format->palette)) {
SDL_Log("Writing PNG image with palette");
colortype |= PNG_COLOR_MASK_PALETTE;
pal_ptr = (png_colorp)malloc(pal->ncolors * sizeof(png_color));
for (i = 0; i < pal->ncolors; i++) {
pal_ptr[i].red = pal->colors[i].r;
pal_ptr[i].green = pal->colors[i].g;
pal_ptr[i].blue = pal->colors[i].b;
}
png_set_PLTE(png_ptr, info_ptr, pal_ptr, pal->ncolors);
free(pal_ptr);
}
if (surface->format->Amask) { // Check for alpha channel
colortype |= PNG_COLOR_MASK_ALPHA;
}
auto depth = surface->format->BitsPerPixel;
// Set image attributes.
png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, depth, colortype,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_set_bgr(png_ptr);
// Write the image data.
std::vector<png_bytep> row_pointers(surface->h);
for (int y = 0; y < surface->h; ++y) {
row_pointers[y] = (png_bytep)(surface->pixels) + y * surface->pitch;
}
png_set_rows(png_ptr, info_ptr, row_pointers.data());
SDL_Log("Writing PNG image...");
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
SDL_Log("PNG image write complete");
png_destroy_write_struct(&png_ptr, &info_ptr);
return true;
}
void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
SDL_Surface **outSurface) {
std::vector<uint8_t> data(png_data);
png_structp png_ptr = png_create_read_struct("1.6.40", NULL, NULL, NULL);
if (!png_ptr) {
throw std::runtime_error("Failed to create PNG read struct");
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, NULL, NULL);
throw std::runtime_error("Failed to create PNG info struct");
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
throw std::runtime_error("Error during PNG read");
}
// Set our custom read function
png_set_read_fn(png_ptr, &data, png_internal::PngReadCallback);
// Read the PNG info
png_read_info(png_ptr, info_ptr);
uint32_t width = png_get_image_width(png_ptr, info_ptr);
uint32_t height = png_get_image_height(png_ptr, info_ptr);
png_byte color_type = png_get_color_type(png_ptr, info_ptr);
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
// Apply necessary transformations...
// (Same as in your existing code)
// Update info structure with transformations
png_read_update_info(png_ptr, info_ptr);
// Read the file
std::vector<uint8_t> raw_data(width * height *
4); // Assuming 4 bytes per pixel (RGBA)
std::vector<png_bytep> row_pointers(height);
for (size_t y = 0; y < height; y++) {
row_pointers[y] = raw_data.data() + y * width * 4;
}
png_read_image(png_ptr, row_pointers.data());
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
// Create an SDL_Surface
*outSurface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32,
SDL_PIXELFORMAT_RGBA32);
if (*outSurface == nullptr) {
SDL_Log("SDL_CreateRGBSurfaceWithFormat failed: %s\n", SDL_GetError());
return;
}
// Copy the raw data into the SDL_Surface
SDL_LockSurface(*outSurface);
memcpy((*outSurface)->pixels, raw_data.data(), raw_data.size());
SDL_UnlockSurface(*outSurface);
SDL_Log("Successfully created SDL_Surface from PNG data");
}
#endif // YAZE_LIB_PNG
class BitmapError : public std::runtime_error {
public:
@@ -553,13 +386,6 @@ void Bitmap::Get16x16Tile(int tile_x, int tile_y,
}
}
#if YAZE_LIB_PNG == 1
std::vector<uint8_t> Bitmap::GetPngData() {
std::vector<uint8_t> png_data;
ConvertSurfaceToPng(surface_, png_data);
return png_data;
}
#endif
void Bitmap::SetPixel(int x, int y, const SnesColor& color) {
if (x < 0 || x >= width_ || y < 0 || y >= height_) {

View File

@@ -35,18 +35,6 @@ enum BitmapFormat {
k8bpp = 2,
};
#if YAZE_LIB_PNG == 1
/**
* @brief Convert SDL_Surface to PNG image data.
*/
bool ConvertSurfaceToPng(SDL_Surface *surface, std::vector<uint8_t> &buffer);
/**
* @brief Convert PNG image data to SDL_Surface.
*/
void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
SDL_Surface **outSurface);
#endif
/**
* @brief Represents a bitmap image.
@@ -199,9 +187,6 @@ class Bitmap {
void set_data(const std::vector<uint8_t> &data) { data_ = data; }
void set_modified(bool modified) { modified_ = modified; }
#if YAZE_LIB_PNG == 1
std::vector<uint8_t> GetPngData();
#endif
private:
int width_ = 0;

View File

@@ -1,28 +1,18 @@
#include "bps.h"
#include <cstdint>
#include <vector>
#include <stdexcept>
#include <cmath>
#include <cstring>
#if YAZE_LIB_PNG == 1
#include <zlib.h>
#endif
#include "absl/status/status.h"
// zlib dependency removed
namespace yaze {
namespace util {
namespace {
#if YAZE_LIB_PNG == 1
uint32_t crc32(const std::vector<uint8_t> &data) {
uint32_t crc = ::crc32(0L, Z_NULL, 0);
return ::crc32(crc, data.data(), data.size());
}
#else
// Simple CRC32 implementation when zlib is not available
// Simple CRC32 implementation (BPS patches disabled)
uint32_t crc32(const std::vector<uint8_t> &data) {
static const uint32_t crc_table[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
@@ -34,40 +24,22 @@ uint32_t crc32(const std::vector<uint8_t> &data) {
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
0xd681feba, 0x55610c86, 0x3f8355c9, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703,
0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226,
0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d,
0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70,
0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a,
0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1,
0x5a05df1b, 0x2d02ef8d
};
uint32_t crc = 0xFFFFFFFF;
@@ -76,7 +48,6 @@ uint32_t crc32(const std::vector<uint8_t> &data) {
}
return crc ^ 0xFFFFFFFF;
}
#endif
void encode(uint64_t data, std::vector<uint8_t> &output) {
while (true) {
@@ -87,182 +58,34 @@ void encode(uint64_t data, std::vector<uint8_t> &output) {
break;
}
output.push_back(x);
data--;
}
}
uint64_t decode(const std::vector<uint8_t> &input, size_t &offset) {
uint64_t data = 0;
uint64_t shift = 1;
while (true) {
uint8_t x = input[offset++];
data += (x & 0x7f) * shift;
uint64_t decode(const std::vector<uint8_t> &data, size_t &offset) {
uint64_t result = 0;
int shift = 0;
while (offset < data.size()) {
uint8_t x = data[offset++];
result |= (uint64_t)(x & 0x7f) << shift;
if (x & 0x80) break;
shift <<= 7;
data += shift;
shift += 7;
}
return data;
return result;
}
} // namespace
void CreateBpsPatch(const std::vector<uint8_t> &source,
const std::vector<uint8_t> &target,
std::vector<uint8_t> &patch) {
patch.clear();
patch.insert(patch.end(), {'B', 'P', 'S', '1'});
encode(source.size(), patch);
encode(target.size(), patch);
encode(0, patch); // No metadata
size_t source_offset = 0;
size_t target_offset = 0;
int64_t source_rel_offset = 0;
int64_t target_rel_offset = 0;
while (target_offset < target.size()) {
if (source_offset < source.size() &&
source[source_offset] == target[target_offset]) {
size_t length = 0;
while (source_offset + length < source.size() &&
target_offset + length < target.size() &&
source[source_offset + length] == target[target_offset + length]) {
length++;
}
encode((length - 1) << 2 | 0, patch); // SourceRead
source_offset += length;
target_offset += length;
} else {
size_t length = 0;
while (
target_offset + length < target.size() &&
(source_offset + length >= source.size() ||
source[source_offset + length] != target[target_offset + length])) {
length++;
}
if (length > 0) {
encode((length - 1) << 2 | 1, patch); // TargetRead
for (size_t i = 0; i < length; i++) {
patch.push_back(target[target_offset + i]);
}
target_offset += length;
}
}
// SourceCopy
if (source_offset < source.size()) {
size_t length = 0;
int64_t offset = source_offset - source_rel_offset;
while (source_offset + length < source.size() &&
target_offset + length < target.size() &&
source[source_offset + length] == target[target_offset + length]) {
length++;
}
if (length > 0) {
encode((length - 1) << 2 | 2, patch);
encode((offset < 0 ? 1 : 0) | (abs(offset) << 1), patch);
source_offset += length;
target_offset += length;
source_rel_offset = source_offset;
}
}
// TargetCopy
if (target_offset > 0) {
size_t length = 0;
int64_t offset = target_offset - target_rel_offset;
while (target_offset + length < target.size() &&
target[target_offset - 1] == target[target_offset + length]) {
length++;
}
if (length > 0) {
encode((length - 1) << 2 | 3, patch);
encode((offset < 0 ? 1 : 0) | (abs(offset) << 1), patch);
target_offset += length;
target_rel_offset = target_offset;
}
}
}
patch.resize(patch.size() + 12); // Make space for the checksums
uint32_t source_checksum = crc32(source);
uint32_t target_checksum = crc32(target);
uint32_t patch_checksum = crc32(patch);
memcpy(patch.data() + patch.size() - 12, &source_checksum, sizeof(uint32_t));
memcpy(patch.data() + patch.size() - 8, &target_checksum, sizeof(uint32_t));
memcpy(patch.data() + patch.size() - 4, &patch_checksum, sizeof(uint32_t));
// BPS patch functionality disabled - returning error status
absl::Status ApplyBpsPatch(const std::vector<uint8_t> &source,
const std::vector<uint8_t> &patch,
std::vector<uint8_t> &output) {
return absl::UnimplementedError("BPS patch functionality has been disabled");
}
void ApplyBpsPatch(const std::vector<uint8_t> &source,
const std::vector<uint8_t> &patch,
std::vector<uint8_t> &target) {
if (patch.size() < 4 || patch[0] != 'B' || patch[1] != 'P' ||
patch[2] != 'S' || patch[3] != '1') {
throw std::runtime_error("Invalid patch format");
}
size_t patch_offset = 4;
uint64_t target_size = decode(patch, patch_offset);
uint64_t metadata_size = decode(patch, patch_offset);
patch_offset += metadata_size;
target.resize(target_size);
size_t source_offset = 0;
size_t target_offset = 0;
int64_t source_rel_offset = 0;
int64_t target_rel_offset = 0;
while (patch_offset < patch.size() - 12) {
uint64_t data = decode(patch, patch_offset);
uint64_t command = data & 3;
uint64_t length = (data >> 2) + 1;
switch (command) {
case 0: // SourceRead
while (length--) {
target[target_offset++] = source[source_offset++];
}
break;
case 1: // TargetRead
while (length--) {
target[target_offset++] = patch[patch_offset++];
}
break;
case 2: // SourceCopy
{
int64_t offsetData = decode(patch, patch_offset);
source_rel_offset += (offsetData & 1 ? -1 : +1) * (offsetData >> 1);
while (length--) {
target[target_offset++] = source[source_rel_offset++];
}
} break;
case 3: // TargetCopy
{
uint64_t offsetData = decode(patch, patch_offset);
target_rel_offset += (offsetData & 1 ? -1 : +1) * (offsetData >> 1);
while (length--) {
target[target_offset++] = target[target_rel_offset++];
}
}
default:
throw std::runtime_error("Invalid patch command");
}
}
uint32_t source_checksum;
uint32_t target_checksum;
uint32_t patch_checksum;
memcpy(&source_checksum, patch.data() + patch.size() - 12, sizeof(uint32_t));
memcpy(&target_checksum, patch.data() + patch.size() - 8, sizeof(uint32_t));
memcpy(&patch_checksum, patch.data() + patch.size() - 4, sizeof(uint32_t));
if (source_checksum != crc32(source) || target_checksum != crc32(target) ||
patch_checksum !=
crc32(std::vector<uint8_t>(patch.begin(), patch.end() - 4))) {
throw std::runtime_error("Checksum mismatch");
}
absl::Status CreateBpsPatch(const std::vector<uint8_t> &source,
const std::vector<uint8_t> &target,
std::vector<uint8_t> &patch) {
return absl::UnimplementedError("BPS patch functionality has been disabled");
}
} // namespace util