| 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 | static constexpr uint8_t DELTA_MAGIC[4] = { 'G', 'D', 'D', 'L' }; |
| 51 | static constexpr uint8_t DELTA_VERSION_NUMBER = 1; |
| 52 | static constexpr size_t DELTA_HEADER_SIZE = 5; |
| 53 | |
| 54 | 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) { |
| 55 | size_t zstd_result = ZSTD_compressBound(p_new_data.size()); |
| 56 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Calculating compression bounds failed."); |
| 57 | |
| 58 | r_delta.reserve_exact(DELTA_HEADER_SIZE + zstd_result); |
| 59 | r_delta.resize(DELTA_HEADER_SIZE + zstd_result); |
| 60 | |
| 61 | memcpy(r_delta.ptrw(), DELTA_MAGIC, 4); |
| 62 | r_delta.write[4] = DELTA_VERSION_NUMBER; |
| 63 | |
| 64 | ZstdCompressionContext zstd_context; |
| 65 | |
| 66 | zstd_result = ZSTD_CCtx_setParameter(zstd_context, ZSTD_c_compressionLevel, p_compression_level); |
| 67 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Setting compression level failed."); |
| 68 | |
| 69 | zstd_result = ZSTD_CCtx_setPledgedSrcSize(zstd_context, p_new_data.size()); |
| 70 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Setting pledged source size failed."); |
| 71 | |
| 72 | zstd_result = ZSTD_CCtx_refPrefix(zstd_context, p_old_data.ptr(), p_old_data.size()); |
| 73 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Setting prefix dictionary failed."); |
| 74 | |
| 75 | zstd_result = ZSTD_CCtx_setParameter(zstd_context, ZSTD_c_contentSizeFlag, 1); |
| 76 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Setting content size flag failed."); |
| 77 | |
| 78 | zstd_result = ZSTD_CCtx_setParameter(zstd_context, ZSTD_c_checksumFlag, 1); |
| 79 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Setting checksum flag failed."); |
| 80 | |
| 81 | 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()); |
| 82 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to encode delta. Compression failed."); |
| 83 | |
| 84 | r_delta.resize(DELTA_HEADER_SIZE + zstd_result); |
| 85 | |
| 86 | return OK; |
| 87 | } |
| 88 | |
| 89 | Error DeltaEncoding::decode_delta(Span<uint8_t> p_old_data, Span<uint8_t> p_delta, Vector<uint8_t> &r_new_data) { |
| 90 | 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())); |
| 91 | |
| 92 | uint8_t magic[4]; |
| 93 | memcpy(magic, p_delta.ptr(), sizeof(magic)); |
| 94 | uint8_t version = p_delta[4]; |
| 95 | |
| 96 | ERR_FAIL_COND_V_MSG(memcmp(magic, DELTA_MAGIC, 4) != 0, ERR_FILE_CORRUPT, "Failed to decode delta. Header is invalid."); |
| 97 | ERR_FAIL_COND_V_MSG(version != DELTA_VERSION_NUMBER, ERR_FILE_UNRECOGNIZED, vformat("Failed to decode delta. Expected version %d but found %d.", DELTA_VERSION_NUMBER, version)); |
| 98 | |
| 99 | size_t zstd_result = ZSTD_getFrameContentSize(p_delta.ptr() + DELTA_HEADER_SIZE, p_delta.size() - DELTA_HEADER_SIZE); |
| 100 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to decode delta. Unable to find decompressed size."); |
| 101 | |
| 102 | r_new_data.reserve_exact(zstd_result); |
| 103 | r_new_data.resize(zstd_result); |
| 104 | |
| 105 | ZstdDecompressionContext zstd_context; |
| 106 | |
| 107 | zstd_result = ZSTD_DCtx_refPrefix(zstd_context, p_old_data.ptr(), p_old_data.size()); |
| 108 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to decode delta. Setting prefix dictionary failed."); |
| 109 | |
| 110 | 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); |
| 111 | ERR_FAIL_ZSTD_V_MSG(zstd_result, FAILED, "Failed to decode delta. Decompression failed."); |
| 112 | ERR_FAIL_COND_V(zstd_result != (size_t)r_new_data.size(), ERR_FILE_CORRUPT); |
| 113 | |
| 114 | return OK; |
| 115 | } |