|
| 1 | +/**************************************************************************/ |
| 2 | +/* delta_encoding.cpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#include "delta_encoding.h" |
| 32 | + |
| 33 | +#include <zstd.h> |
| 34 | + |
| 35 | +#define ERR_FAIL_ZSTD_V_MSG(m_result, m_retval, m_msg) \ |
| 36 | + ERR_FAIL_COND_V_MSG(ZSTD_isError(m_result), m_retval, vformat("%s Zstandard reported error code %d: '%s'.", m_msg, ZSTD_getErrorCode(m_result), ZSTD_getErrorString(ZSTD_getErrorCode(m_result)))) |
| 37 | + |
| 38 | +struct ZstdCompressionContext { |
| 39 | + ZSTD_CCtx *context = ZSTD_createCCtx(); |
| 40 | + ~ZstdCompressionContext() { ZSTD_freeCCtx(context); } |
| 41 | + operator ZSTD_CCtx *() { return context; } |
| 42 | +}; |
| 43 | + |
| 44 | +struct ZstdDecompressionContext { |
| 45 | + ZSTD_DCtx *context = ZSTD_createDCtx(); |
| 46 | + ~ZstdDecompressionContext() { ZSTD_freeDCtx(context); } |
| 47 | + operator ZSTD_DCtx *() { return context; } |
| 48 | +}; |
| 49 | + |
| 50 | +struct DeltaHeader { |
| 51 | + uint8_t magic[4]; |
| 52 | + uint8_t version; |
| 53 | +}; |
| 54 | + |
| 55 | +static_assert(alignof(DeltaHeader) == 1); // There shouldn't be any padding. |
| 56 | + |
| 57 | +static constexpr size_t DELTA_HEADER_SIZE = sizeof(DeltaHeader); |
| 58 | +static constexpr uint8_t DELTA_MAGIC[4] = { 'G', 'D', 'D', 'L' }; |
| 59 | +static constexpr int DELTA_VERSION_NUMBER = 1; |
| 60 | + |
| 61 | +Error DeltaEncoding::encode_delta(Span<uint8_t> p_old_data, Span<uint8_t> p_new_data, Vector<uint8_t> &r_delta, int p_compression_level) { |
| 62 | + size_t zstd_result = ZSTD_compressBound(p_new_data.size()); |
| 63 | + ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Calculating compression bounds failed."); |
| 64 | + |
| 65 | + r_delta.reserve_exact(DELTA_HEADER_SIZE + zstd_result); |
| 66 | + r_delta.resize(DELTA_HEADER_SIZE + zstd_result); |
| 67 | + |
| 68 | + DeltaHeader header; |
| 69 | + memcpy(header.magic, DELTA_MAGIC, 4); |
| 70 | + header.version = DELTA_VERSION_NUMBER; |
| 71 | + memcpy(r_delta.ptrw(), &header, DELTA_HEADER_SIZE); |
| 72 | + |
| 73 | + ZstdCompressionContext zstd_context; |
| 74 | + |
| 75 | + ZSTD_parameters zstd_params = ZSTD_getParams(p_compression_level, p_new_data.size(), p_old_data.size()); |
| 76 | + zstd_params.fParams.contentSizeFlag = 1; |
| 77 | + zstd_params.fParams.checksumFlag = 1; |
| 78 | + |
| 79 | + zstd_result = ZSTD_CCtx_setParams(zstd_context, zstd_params); |
| 80 | + ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Setting compression parameters failed."); |
| 81 | + |
| 82 | + zstd_result = ZSTD_CCtx_refPrefix(zstd_context, p_old_data.ptr(), p_old_data.size()); |
| 83 | + ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Setting prefix dictionary failed."); |
| 84 | + |
| 85 | + zstd_result = ZSTD_compress2(zstd_context, r_delta.ptrw() + DELTA_HEADER_SIZE, r_delta.size() - DELTA_HEADER_SIZE, p_new_data.ptr(), p_new_data.size()); |
| 86 | + ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Compression failed."); |
| 87 | + |
| 88 | + r_delta.resize(DELTA_HEADER_SIZE + zstd_result); |
| 89 | + |
| 90 | + return OK; |
| 91 | +} |
| 92 | + |
| 93 | +Error DeltaEncoding::decode_delta(Span<uint8_t> p_old_data, Span<uint8_t> p_delta, Vector<uint8_t> &r_new_data) { |
| 94 | + ERR_FAIL_COND_V_MSG(p_delta.size() < DELTA_HEADER_SIZE, ERR_INVALID_DATA, vformat("Failed to decode delta. File size (%d) is too small.", p_delta.size())); |
| 95 | + |
| 96 | + DeltaHeader header; |
| 97 | + memcpy(&header, p_delta.ptr(), DELTA_HEADER_SIZE); |
| 98 | + |
| 99 | + ERR_FAIL_COND_V_MSG(memcmp(header.magic, DELTA_MAGIC, 4) != 0, ERR_FILE_CORRUPT, "Failed to decode delta. Header is invalid."); |
| 100 | + ERR_FAIL_COND_V_MSG(header.version != DELTA_VERSION_NUMBER, ERR_FILE_UNRECOGNIZED, vformat("Failed to decode delta. Expected version %d but found %d.", DELTA_VERSION_NUMBER, header.version)); |
| 101 | + |
| 102 | + size_t zstd_result = ZSTD_findDecompressedSize(p_delta.ptr() + DELTA_HEADER_SIZE, p_delta.size() - DELTA_HEADER_SIZE); |
| 103 | + ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to decode delta. Unable to find decompressed size."); |
| 104 | + |
| 105 | + r_new_data.reserve_exact(zstd_result); |
| 106 | + r_new_data.resize(zstd_result); |
| 107 | + |
| 108 | + ZstdDecompressionContext zstd_context; |
| 109 | + |
| 110 | + zstd_result = ZSTD_DCtx_refPrefix(zstd_context, p_old_data.ptr(), p_old_data.size()); |
| 111 | + ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to decode delta. Setting prefix dictionary failed."); |
| 112 | + |
| 113 | + zstd_result = ZSTD_decompressDCtx(zstd_context, r_new_data.ptrw(), r_new_data.size(), p_delta.ptr() + DELTA_HEADER_SIZE, p_delta.size() - DELTA_HEADER_SIZE); |
| 114 | + ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to decode delta. Decompression failed."); |
| 115 | + ERR_FAIL_COND_V(zstd_result != (size_t)r_new_data.size(), ERR_FILE_CORRUPT); |
| 116 | + |
| 117 | + return OK; |
| 118 | +} |
0 commit comments