legacy_zstd06.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <library/cpp/blockcodecs/core/codecs.h>
  2. #include <library/cpp/blockcodecs/core/common.h>
  3. #include <library/cpp/blockcodecs/core/register.h>
  4. #include <contrib/libs/zstd06/common/zstd.h>
  5. #include <contrib/libs/zstd06/common/zstd_static.h>
  6. using namespace NBlockCodecs;
  7. namespace {
  8. struct TZStd06Codec: public TAddLengthCodec<TZStd06Codec> {
  9. inline TZStd06Codec(unsigned level)
  10. : Level(level)
  11. , MyName(TStringBuf("zstd06_") + ToString(Level))
  12. {
  13. }
  14. static inline size_t CheckError(size_t ret, const char* what) {
  15. if (ZSTD_isError(ret)) {
  16. ythrow yexception() << what << TStringBuf(" zstd error: ") << ZSTD_getErrorName(ret);
  17. }
  18. return ret;
  19. }
  20. static inline size_t DoMaxCompressedLength(size_t l) noexcept {
  21. return ZSTD_compressBound(l);
  22. }
  23. inline size_t DoCompress(const TData& in, void* out) const {
  24. return CheckError(ZSTD_compress(out, DoMaxCompressedLength(in.size()), in.data(), in.size(), Level), "compress");
  25. }
  26. inline void DoDecompress(const TData& in, void* out, size_t dsize) const {
  27. const size_t res = CheckError(ZSTD_decompress(out, dsize, in.data(), in.size()), "decompress");
  28. if (res != dsize) {
  29. ythrow TDecompressError(dsize, res);
  30. }
  31. }
  32. TStringBuf Name() const noexcept override {
  33. return MyName;
  34. }
  35. const unsigned Level;
  36. const TString MyName;
  37. };
  38. struct TZStd06Registrar {
  39. TZStd06Registrar() {
  40. for (unsigned i = 1; i <= ZSTD_maxCLevel(); ++i) {
  41. RegisterCodec(MakeHolder<TZStd06Codec>(i));
  42. }
  43. }
  44. };
  45. const TZStd06Registrar Registrar{};
  46. }