]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/include/beast/zlib/detail/window.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / Beast / include / beast / zlib / detail / window.hpp
1 //
2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // This is a derivative work based on Zlib, copyright below:
8 /*
9 Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
10
11 This software is provided 'as-is', without any express or implied
12 warranty. In no event will the authors be held liable for any damages
13 arising from the use of this software.
14
15 Permission is granted to anyone to use this software for any purpose,
16 including commercial applications, and to alter it and redistribute it
17 freely, subject to the following restrictions:
18
19 1. The origin of this software must not be misrepresented; you must not
20 claim that you wrote the original software. If you use this software
21 in a product, an acknowledgment in the product documentation would be
22 appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and must not be
24 misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source distribution.
26
27 Jean-loup Gailly Mark Adler
28 jloup@gzip.org madler@alumni.caltech.edu
29
30 The data format used by the zlib library is described by RFCs (Request for
31 Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
32 (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
33 */
34
35 #ifndef BEAST_ZLIB_DETAIL_WINDOW_HPP
36 #define BEAST_ZLIB_DETAIL_WINDOW_HPP
37
38 #include <boost/assert.hpp>
39 #include <cstdint>
40 #include <cstring>
41 #include <memory>
42
43 namespace beast {
44 namespace zlib {
45 namespace detail {
46
47 class window
48 {
49 std::unique_ptr<std::uint8_t[]> p_;
50 std::uint16_t i_ = 0;
51 std::uint16_t size_ = 0;
52 std::uint16_t capacity_ = 0;
53 std::uint8_t bits_ = 0;
54
55 public:
56 int
57 bits() const
58 {
59 return bits_;
60 }
61
62 unsigned
63 capacity() const
64 {
65 return capacity_;
66 }
67
68 unsigned
69 size() const
70 {
71 return size_;
72 }
73
74 void
75 reset(int bits);
76
77 void
78 read(std::uint8_t* out, std::size_t pos, std::size_t n);
79
80 template<class = void>
81 void
82 write(std::uint8_t const* in, std::size_t n);
83 };
84
85 inline
86 void
87 window::
88 reset(int bits)
89 {
90 if(bits_ != bits)
91 {
92 p_.reset();
93 bits_ = static_cast<std::uint8_t>(bits);
94 capacity_ = 1U << bits_;
95 }
96 i_ = 0;
97 size_ = 0;
98 }
99
100 inline
101 void
102 window::
103 read(std::uint8_t* out, std::size_t pos, std::size_t n)
104 {
105 if(i_ >= size_)
106 {
107 // window is contiguous
108 std::memcpy(out, &p_[i_ - pos], n);
109 return;
110 }
111 auto i = ((i_ - pos) + capacity_) % capacity_;
112 auto m = capacity_ - i;
113 if(n <= m)
114 {
115 std::memcpy(out, &p_[i], n);
116 return;
117 }
118 std::memcpy(out, &p_[i], m);
119 out += m;
120 std::memcpy(out, &p_[0], n - m);
121 }
122
123 template<class>
124 void
125 window::
126 write(std::uint8_t const* in, std::size_t n)
127 {
128 if(! p_)
129 p_.reset(new std::uint8_t[capacity_]);
130 if(n >= capacity_)
131 {
132 i_ = 0;
133 size_ = capacity_;
134 std::memcpy(&p_[0], in + (n - size_), size_);
135 return;
136 }
137 if(i_ + n <= capacity_)
138 {
139 std::memcpy(&p_[i_], in, n);
140 if(size_ >= capacity_ - n)
141 size_ = capacity_;
142 else
143 size_ = static_cast<std::uint16_t>(size_ + n);
144
145 i_ = static_cast<std::uint16_t>(
146 (i_ + n) % capacity_);
147 return;
148 }
149 auto m = capacity_ - i_;
150 std::memcpy(&p_[i_], in, m);
151 in += m;
152 i_ = static_cast<std::uint16_t>(n - m);
153 std::memcpy(&p_[0], in, i_);
154 size_ = capacity_;
155 }
156
157 } // detail
158 } // zlib
159 } // beast
160
161 #endif