mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-12-05 22:18:27 +00:00
Track gzip member boundaries with a single state
Replace the three flags in gzip_decompressor with one State enum, so combinations that cannot happen can no longer be written, and start the next member as soon as both ID bytes have been seen. No behavior change.
This commit is contained in:
1 parent
da4a0a6f33
commit
6c2324ee59
1 file changed
+20
-22
@@ -4187,11 +4187,10 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool is_valid_ = false;
|
bool is_valid_ = false;
|
||||||
// RFC 1952 2.2: a gzip body is a series of members. Once one ends, the bytes
|
// RFC 1952 2.2: a gzip body is a series of members. After one ends, the next
|
||||||
// after it either start another one or are ignored.
|
// starts with ID1 ID2, and anything else is ignored.
|
||||||
bool member_ended_ = false;
|
enum class State { Inflating, MemberEnded, SawId1, Ignoring };
|
||||||
bool ignore_rest_ = false;
|
State state_ = State::Inflating;
|
||||||
size_t next_id_bytes_ = 0; // bytes of the next member's ID1 ID2 seen so far
|
|
||||||
z_stream strm_;
|
z_stream strm_;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
@@ -8894,7 +8893,7 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
|||||||
Callback callback) {
|
Callback callback) {
|
||||||
assert(is_valid_);
|
assert(is_valid_);
|
||||||
|
|
||||||
if (ignore_rest_) { return true; }
|
if (state_ == State::Ignoring) { return true; }
|
||||||
|
|
||||||
auto ret = Z_OK;
|
auto ret = Z_OK;
|
||||||
|
|
||||||
@@ -8914,22 +8913,21 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
|||||||
strm_.avail_out = static_cast<uInt>(buff.size());
|
strm_.avail_out = static_cast<uInt>(buff.size());
|
||||||
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
|
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
|
||||||
|
|
||||||
if (member_ended_) {
|
if (state_ != State::Inflating) {
|
||||||
// Another member starts with ID1 ID2, which may come split across
|
|
||||||
// calls. Anything else after a member is ignored, as it always was.
|
|
||||||
static const Bytef id[] = {0x1f, 0x8b};
|
static const Bytef id[] = {0x1f, 0x8b};
|
||||||
while (next_id_bytes_ < sizeof(id) && strm_.avail_in > 0) {
|
auto i = state_ == State::MemberEnded ? 0 : 1;
|
||||||
if (*strm_.next_in != id[next_id_bytes_]) {
|
if (*strm_.next_in != id[i]) {
|
||||||
ignore_rest_ = true;
|
state_ = State::Ignoring;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
next_id_bytes_++;
|
strm_.next_in++;
|
||||||
strm_.next_in++;
|
strm_.avail_in--;
|
||||||
strm_.avail_in--;
|
if (i == 0) {
|
||||||
|
state_ = State::SawId1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (next_id_bytes_ < sizeof(id) || strm_.avail_in == 0) { continue; }
|
|
||||||
|
|
||||||
// Hand the ID bytes taken above to inflate before the rest
|
// The ID bytes may have come split across calls, so feed them from id
|
||||||
inflateReset(&strm_);
|
inflateReset(&strm_);
|
||||||
auto next_in = strm_.next_in;
|
auto next_in = strm_.next_in;
|
||||||
auto avail_in = strm_.avail_in;
|
auto avail_in = strm_.avail_in;
|
||||||
@@ -8938,12 +8936,12 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
|||||||
ret = inflate(&strm_, Z_NO_FLUSH);
|
ret = inflate(&strm_, Z_NO_FLUSH);
|
||||||
strm_.next_in = next_in;
|
strm_.next_in = next_in;
|
||||||
strm_.avail_in = avail_in;
|
strm_.avail_in = avail_in;
|
||||||
member_ended_ = false;
|
state_ = State::Inflating;
|
||||||
next_id_bytes_ = 0;
|
|
||||||
if (ret != Z_OK) {
|
if (ret != Z_OK) {
|
||||||
inflateEnd(&strm_);
|
inflateEnd(&strm_);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = inflate(&strm_, Z_NO_FLUSH);
|
ret = inflate(&strm_, Z_NO_FLUSH);
|
||||||
@@ -8960,7 +8958,7 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ret == Z_STREAM_END) {
|
if (ret == Z_STREAM_END) {
|
||||||
member_ended_ = true;
|
state_ = State::MemberEnded;
|
||||||
ret = Z_OK;
|
ret = Z_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user