1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef BITSTREAMS_P_H
41#define BITSTREAMS_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of other Qt classes. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/qglobal.h>
55#include <QtCore/qdebug.h>
56
57#include <type_traits>
58#include <algorithm>
59#include <vector>
60
61QT_BEGIN_NAMESPACE
62
63class QByteArray;
64
65namespace HPack
66{
67
68// BitOStream works with an external buffer,
69// for example, HEADERS frame.
70class Q_AUTOTEST_EXPORT BitOStream
71{
72public:
73 BitOStream(std::vector<uchar> &buffer);
74
75 // Write 'bitLength' bits from the least significant
76 // bits in 'bits' to bitstream:
77 void writeBits(uchar bits, quint8 bitLength);
78 // HPACK data format, we support:
79 // * 32-bit integers
80 // * strings
81 void write(quint32 src);
82 void write(const QByteArray &src, bool compressed);
83
84 quint64 bitLength() const;
85 quint64 byteLength() const;
86 const uchar *begin() const;
87 const uchar *end() const;
88
89 void clear();
90
91private:
92 Q_DISABLE_COPY_MOVE(BitOStream);
93
94 std::vector<uchar> &buffer;
95 quint64 bitsSet;
96};
97
98class Q_AUTOTEST_EXPORT BitIStream
99{
100public:
101 // Error is set by 'read' functions.
102 // 'peek' does not set the error,
103 // since it just peeks some bits
104 // without the notion of wrong/right.
105 // 'read' functions only change 'streamOffset'
106 // on success.
107 enum class Error
108 {
109 NoError,
110 NotEnoughData,
111 CompressionError,
112 InvalidInteger
113 };
114
115 BitIStream();
116 BitIStream(const uchar *f, const uchar *l);
117
118 quint64 bitLength() const;
119 bool hasMoreBits() const;
120
121 // peekBits tries to read 'length' bits from the bitstream into
122 // 'dst' ('length' must be <= sizeof(dst) * 8), packing them
123 // starting from the most significant bit of the most significant
124 // byte. It's a template so that we can use it with different
125 // integer types. Returns the number of bits actually read.
126 // Does not change stream's offset.
127
128 template<class T>
129 quint64 peekBits(quint64 from, quint64 length, T *dstPtr) const
130 {
131 static_assert(std::is_unsigned<T>::value, "peekBits: unsigned integer type expected");
132
133 Q_ASSERT(dstPtr);
134 Q_ASSERT(length <= sizeof(T) * 8);
135
136 if (from >= bitLength() || !length)
137 return 0;
138
139 T &dst = *dstPtr;
140 dst = T();
141 length = std::min(a: length, b: bitLength() - from);
142
143 const uchar *srcByte = first + from / 8;
144 auto bitsToRead = length + from % 8;
145
146 while (bitsToRead > 8) {
147 dst = (dst << 8) | *srcByte;
148 bitsToRead -= 8;
149 ++srcByte;
150 }
151
152 dst <<= bitsToRead;
153 dst |= *srcByte >> (8 - bitsToRead);
154 dst <<= sizeof(T) * 8 - length;
155
156 return length;
157 }
158
159 quint64 streamOffset() const
160 {
161 return offset;
162 }
163
164 bool skipBits(quint64 nBits);
165 bool rewindOffset(quint64 nBits);
166
167 bool read(quint32 *dstPtr);
168 bool read(QByteArray *dstPtr);
169
170 Error error() const;
171
172private:
173 void setError(Error newState);
174
175 const uchar *first;
176 const uchar *last;
177 quint64 offset;
178 Error streamError;
179};
180
181} // namespace HPack
182
183QT_END_NAMESPACE
184
185#endif
186

source code of qtbase/src/network/access/http2/bitstreams_p.h