1 | /* |
2 | * Copyright 2018-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #pragma once |
18 | |
19 | #include <memory.h> |
20 | |
21 | #include <folly/Memory.h> |
22 | #include <folly/Portability.h> |
23 | #include <folly/compression/Compression.h> |
24 | |
25 | #if FOLLY_HAVE_LIBZSTD |
26 | |
27 | #ifndef ZSTD_STATIC_LINKING_ONLY |
28 | #define ZSTD_STATIC_LINKING_ONLY |
29 | #endif |
30 | #include <zstd.h> |
31 | |
32 | namespace folly { |
33 | namespace io { |
34 | namespace zstd { |
35 | |
36 | /** |
37 | * Interface for zstd-specific codec initialization. |
38 | */ |
39 | class Options { |
40 | public: |
41 | /* Create an Options struct with the default options for the given `level`. |
42 | * NOTE: This is the zstd level, COMPRESSION_LEVEL_DEFAULT and such aren't |
43 | * supported, since zstd supports negative compression levels. |
44 | */ |
45 | explicit Options(int level); |
46 | |
47 | /** |
48 | * Set the compression `param` to `value`. |
49 | * See the zstd documentation for ZSTD_CCtx_setParameter() for details, this |
50 | * is just a thin wrapper. |
51 | */ |
52 | void set(ZSTD_cParameter param, unsigned value); |
53 | |
54 | /** |
55 | * Set the maximum allowed window size during decompression. |
56 | * `maxWindowSize == 0` means don't set the maximum window size. |
57 | * zstd's current default limit is 2^27. |
58 | * See the zstd documentation for ZSTD_DCtx_setMaxWindowSize() for details. |
59 | */ |
60 | void setMaxWindowSize(size_t maxWindowSize) { |
61 | maxWindowSize_ = maxWindowSize; |
62 | } |
63 | |
64 | /// Get a reference to the ZSTD_CCtx_params. |
65 | ZSTD_CCtx_params const* params() const { |
66 | return params_.get(); |
67 | } |
68 | |
69 | /// Get the compression level. |
70 | int level() const { |
71 | return level_; |
72 | } |
73 | |
74 | /// Get the maximum window size. |
75 | size_t maxWindowSize() const { |
76 | return maxWindowSize_; |
77 | } |
78 | |
79 | private: |
80 | static void freeCCtxParams(ZSTD_CCtx_params* params); |
81 | std::unique_ptr< |
82 | ZSTD_CCtx_params, |
83 | folly::static_function_deleter<ZSTD_CCtx_params, &freeCCtxParams>> |
84 | params_; |
85 | size_t maxWindowSize_{0}; |
86 | int level_; |
87 | }; |
88 | |
89 | /// Get a zstd Codec with the given options. |
90 | std::unique_ptr<Codec> getCodec(Options options); |
91 | /// Get a zstd StreamCodec with the given options. |
92 | std::unique_ptr<StreamCodec> getStreamCodec(Options options); |
93 | |
94 | } // namespace zstd |
95 | } // namespace io |
96 | } // namespace folly |
97 | |
98 | #endif |
99 | |