hpatch_impl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright (c) 2024, sulfurandcu.github.io
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-10-24 liujitong first version
  9. */
  10. #include "hpatch_impl.h"
  11. #include "decompresser_demo.h"
  12. hpi_patch_result_t hpi_patch(hpatchi_listener_t *listener, int patch_cache_size, int decompress_cache_size, hpi_TInputStream_read _do_read_diff, read_old_t _do_read_old, write_new_t _do_write_new)
  13. {
  14. hpi_patch_result_t result = HPATCHI_SUCCESS;
  15. hpi_byte *pmem = 0;
  16. hpi_byte *patch_cache;
  17. hpi_compressType compress_type;
  18. hpi_pos_t new_size;
  19. hpi_pos_t uncompress_size;
  20. hpi_BOOL patch_result;
  21. patch_result = hpatch_lite_open(listener, _do_read_diff, &compress_type, &new_size, &uncompress_size);
  22. if (patch_result != hpi_TRUE)
  23. {
  24. result = HPATCHI_PATCH_OPEN_ERROR;
  25. goto clear;
  26. }
  27. listener->read_old = _do_read_old;
  28. listener->write_new = _do_write_new;
  29. switch (compress_type)
  30. {
  31. case hpi_compressType_no: // memory size: patch_cache_size
  32. {
  33. pmem = (hpi_byte *)hpi_malloc(patch_cache_size);
  34. if (!pmem)
  35. {
  36. result = HPATCHI_MEM_ERROR;
  37. goto clear;
  38. }
  39. patch_cache = pmem;
  40. listener->diff_data = listener;
  41. listener->read_diff = _do_read_diff;
  42. }
  43. break;
  44. #ifdef _CompressPlugin_tuz
  45. case hpi_compressType_tuz: // requirements memory size: patch_cache_size + decompress_cache_size + decompress_dict_size
  46. {
  47. tuz_TStream tuz_stream_handle;
  48. size_t decompress_dict_size = _tuz_TStream_getReservedMemSize(listener, _do_read_diff);
  49. if (decompress_dict_size <= 0)
  50. {
  51. result = HPATCHI_DECOMPRESSER_DICT_ERROR;
  52. goto clear;
  53. }
  54. pmem = (hpi_byte *)hpi_malloc(decompress_dict_size + decompress_cache_size + patch_cache_size);
  55. if (!pmem)
  56. {
  57. result = HPATCHI_MEM_ERROR;
  58. goto clear;
  59. }
  60. tuz_TResult tuz_result = tuz_TStream_open(&tuz_stream_handle, listener, _do_read_diff, pmem, (tuz_size_t)decompress_dict_size, (tuz_size_t)decompress_cache_size);
  61. if (tuz_result != tuz_OK)
  62. {
  63. result = HPATCHI_DECOMPRESSER_OPEN_ERROR;
  64. goto clear;
  65. }
  66. patch_cache = pmem + decompress_dict_size + decompress_cache_size;
  67. listener->diff_data = &tuz_stream_handle;
  68. listener->read_diff = _tuz_TStream_decompress;
  69. }
  70. break;
  71. #endif
  72. default:
  73. {
  74. result = HPATCHI_COMPRESSTYPE_ERROR;
  75. goto clear;
  76. }
  77. }
  78. patch_result = hpatch_lite_patch(listener, new_size, patch_cache, (hpi_size_t)patch_cache_size);
  79. if (patch_result != hpi_TRUE)
  80. {
  81. result = HPATCHI_PATCH_ERROR;
  82. goto clear;
  83. }
  84. clear:
  85. if (pmem)
  86. {
  87. hpi_free(pmem);
  88. pmem = 0;
  89. }
  90. return result;
  91. }