| Index: net/quic/crypto/strike_register.cc
|
| diff --git a/net/quic/crypto/strike_register.cc b/net/quic/crypto/strike_register.cc
|
| index 2b995a48e60e80a1f6a3c6f1116db55702450be3..9cea0f11f8c765503d5dfe595d3df409940be3f3 100644
|
| --- a/net/quic/crypto/strike_register.cc
|
| +++ b/net/quic/crypto/strike_register.cc
|
| @@ -17,9 +17,9 @@ namespace net {
|
|
|
| namespace {
|
|
|
| -uint32 GetInitialHorizon(uint32 current_time_internal,
|
| - uint32 window_secs,
|
| - StrikeRegister::StartupType startup) {
|
| +uint32_t GetInitialHorizon(uint32_t current_time_internal,
|
| + uint32_t window_secs,
|
| + StrikeRegister::StartupType startup) {
|
| if (startup == StrikeRegister::DENY_REQUESTS_AT_STARTUP) {
|
| // The horizon is initially set |window_secs| into the future because, if
|
| // we just crashed, then we may have accepted nonces in the span
|
| @@ -36,50 +36,50 @@ uint32 GetInitialHorizon(uint32 current_time_internal,
|
| } // namespace
|
|
|
| // static
|
| -const uint32 StrikeRegister::kExternalNodeSize = 24;
|
| +const uint32_t StrikeRegister::kExternalNodeSize = 24;
|
| // static
|
| -const uint32 StrikeRegister::kNil = (1u << 31) | 1;
|
| +const uint32_t StrikeRegister::kNil = (1u << 31) | 1;
|
| // static
|
| -const uint32 StrikeRegister::kExternalFlag = 1 << 23;
|
| +const uint32_t StrikeRegister::kExternalFlag = 1 << 23;
|
|
|
| // InternalNode represents a non-leaf node in the critbit tree. See the comment
|
| // in the .h file for details.
|
| class StrikeRegister::InternalNode {
|
| public:
|
| - void SetChild(unsigned direction, uint32 child) {
|
| + void SetChild(unsigned direction, uint32_t child) {
|
| data_[direction] = (data_[direction] & 0xff) | (child << 8);
|
| }
|
|
|
| - void SetCritByte(uint8 critbyte) {
|
| + void SetCritByte(uint8_t critbyte) {
|
| data_[0] = (data_[0] & 0xffffff00) | critbyte;
|
| }
|
|
|
| - void SetOtherBits(uint8 otherbits) {
|
| + void SetOtherBits(uint8_t otherbits) {
|
| data_[1] = (data_[1] & 0xffffff00) | otherbits;
|
| }
|
|
|
| - void SetNextPtr(uint32 next) { data_[0] = next; }
|
| + void SetNextPtr(uint32_t next) { data_[0] = next; }
|
|
|
| - uint32 next() const { return data_[0]; }
|
| + uint32_t next() const { return data_[0]; }
|
|
|
| - uint32 child(unsigned n) const { return data_[n] >> 8; }
|
| + uint32_t child(unsigned n) const { return data_[n] >> 8; }
|
|
|
| - uint8 critbyte() const { return static_cast<uint8>(data_[0]); }
|
| + uint8_t critbyte() const { return static_cast<uint8_t>(data_[0]); }
|
|
|
| - uint8 otherbits() const { return static_cast<uint8>(data_[1]); }
|
| + uint8_t otherbits() const { return static_cast<uint8_t>(data_[1]); }
|
|
|
| // These bytes are organised thus:
|
| // <24 bits> left child
|
| // <8 bits> crit-byte
|
| // <24 bits> right child
|
| // <8 bits> other-bits
|
| - uint32 data_[2];
|
| + uint32_t data_[2];
|
| };
|
|
|
| // kCreationTimeFromInternalEpoch contains the number of seconds between the
|
| // start of the internal epoch and the creation time. This allows us
|
| // to consider times that are before the creation time.
|
| -static const uint32 kCreationTimeFromInternalEpoch = 63115200; // 2 years.
|
| +static const uint32_t kCreationTimeFromInternalEpoch = 63115200; // 2 years.
|
|
|
| void StrikeRegister::ValidateStrikeRegisterConfig(unsigned max_entries) {
|
| // We only have 23 bits of index available.
|
| @@ -89,9 +89,9 @@ void StrikeRegister::ValidateStrikeRegisterConfig(unsigned max_entries) {
|
| }
|
|
|
| StrikeRegister::StrikeRegister(unsigned max_entries,
|
| - uint32 current_time,
|
| - uint32 window_secs,
|
| - const uint8 orbit[8],
|
| + uint32_t current_time,
|
| + uint32_t window_secs,
|
| + const uint8_t orbit[8],
|
| StartupType startup)
|
| : max_entries_(max_entries),
|
| window_secs_(window_secs),
|
| @@ -105,7 +105,7 @@ StrikeRegister::StrikeRegister(unsigned max_entries,
|
|
|
| ValidateStrikeRegisterConfig(max_entries);
|
| internal_nodes_ = new InternalNode[max_entries];
|
| - external_nodes_.reset(new uint8[kExternalNodeSize * max_entries]);
|
| + external_nodes_.reset(new uint8_t[kExternalNodeSize * max_entries]);
|
|
|
| Reset();
|
| }
|
| @@ -133,31 +133,31 @@ void StrikeRegister::Reset() {
|
| internal_node_head_ = kNil;
|
| }
|
|
|
| -InsertStatus StrikeRegister::Insert(const uint8 nonce[32],
|
| - uint32 current_time_external) {
|
| +InsertStatus StrikeRegister::Insert(const uint8_t nonce[32],
|
| + uint32_t current_time_external) {
|
| // Make space for the insertion if the strike register is full.
|
| while (external_node_free_head_ == kNil || internal_node_free_head_ == kNil) {
|
| DropOldestNode();
|
| }
|
|
|
| - const uint32 current_time = ExternalTimeToInternal(current_time_external);
|
| + const uint32_t current_time = ExternalTimeToInternal(current_time_external);
|
|
|
| // Check to see if the orbit is correct.
|
| if (memcmp(nonce + sizeof(current_time), orbit_, sizeof(orbit_))) {
|
| return NONCE_INVALID_ORBIT_FAILURE;
|
| }
|
|
|
| - const uint32 nonce_time = ExternalTimeToInternal(TimeFromBytes(nonce));
|
| + const uint32_t nonce_time = ExternalTimeToInternal(TimeFromBytes(nonce));
|
|
|
| // Check that the timestamp is in the valid range.
|
| - pair<uint32, uint32> valid_range =
|
| + pair<uint32_t, uint32_t> valid_range =
|
| StrikeRegister::GetValidRange(current_time);
|
| if (nonce_time < valid_range.first || nonce_time > valid_range.second) {
|
| return NONCE_INVALID_TIME_FAILURE;
|
| }
|
|
|
| // We strip the orbit out of the nonce.
|
| - uint8 value[24];
|
| + uint8_t value[24];
|
| memcpy(value, nonce, sizeof(nonce_time));
|
| memcpy(value + sizeof(nonce_time),
|
| nonce + sizeof(nonce_time) + sizeof(orbit_),
|
| @@ -166,25 +166,25 @@ InsertStatus StrikeRegister::Insert(const uint8 nonce[32],
|
| // Find the best match to |value| in the crit-bit tree. The best match is
|
| // simply the value which /could/ match |value|, if any does, so we still
|
| // need a memcmp to check.
|
| - uint32 best_match_index = BestMatch(value);
|
| + uint32_t best_match_index = BestMatch(value);
|
| if (best_match_index == kNil) {
|
| // Empty tree. Just insert the new value at the root.
|
| - uint32 index = GetFreeExternalNode();
|
| + uint32_t index = GetFreeExternalNode();
|
| memcpy(external_node(index), value, sizeof(value));
|
| internal_node_head_ = (index | kExternalFlag) << 8;
|
| DCHECK_LE(horizon_, nonce_time);
|
| return NONCE_OK;
|
| }
|
|
|
| - const uint8* best_match = external_node(best_match_index);
|
| + const uint8_t* best_match = external_node(best_match_index);
|
| if (memcmp(best_match, value, sizeof(value)) == 0) {
|
| // We found the value in the tree.
|
| return NONCE_NOT_UNIQUE_FAILURE;
|
| }
|
|
|
| // We are going to insert a new entry into the tree, so get the nodes now.
|
| - uint32 internal_node_index = GetFreeInternalNode();
|
| - uint32 external_node_index = GetFreeExternalNode();
|
| + uint32_t internal_node_index = GetFreeInternalNode();
|
| + uint32_t external_node_index = GetFreeExternalNode();
|
|
|
| // If we just evicted the best match, then we have to try and match again.
|
| // We know that we didn't just empty the tree because we require that
|
| @@ -196,8 +196,8 @@ InsertStatus StrikeRegister::Insert(const uint8 nonce[32],
|
| }
|
|
|
| // Now we need to find the first bit where we differ from |best_match|.
|
| - uint8 differing_byte;
|
| - uint8 new_other_bits;
|
| + uint8_t differing_byte;
|
| + uint8_t new_other_bits;
|
| for (differing_byte = 0; differing_byte < arraysize(value);
|
| differing_byte++) {
|
| new_other_bits = value[differing_byte] ^ best_match[differing_byte];
|
| @@ -235,13 +235,13 @@ InsertStatus StrikeRegister::Insert(const uint8 nonce[32],
|
| inode->SetCritByte(differing_byte);
|
| inode->SetOtherBits(new_other_bits);
|
|
|
| - // |where_index| is a pointer to the uint32 which needs to be updated in
|
| + // |where_index| is a pointer to the uint32_t which needs to be updated in
|
| // order to insert the new internal node into the tree. The internal nodes
|
| // store the child indexes in the top 24-bits of a 32-bit word and, to keep
|
| // the code simple, we define that |internal_node_head_| is organised the
|
| // same way.
|
| DCHECK_EQ(internal_node_head_ & 0xff, 0u);
|
| - uint32* where_index = &internal_node_head_;
|
| + uint32_t* where_index = &internal_node_head_;
|
| while (((*where_index >> 8) & kExternalFlag) == 0) {
|
| InternalNode* node = &internal_nodes_[*where_index >> 8];
|
| if (node->critbyte() > differing_byte) {
|
| @@ -256,7 +256,7 @@ InsertStatus StrikeRegister::Insert(const uint8 nonce[32],
|
| CHECK(false);
|
| }
|
|
|
| - uint8 c = value[node->critbyte()];
|
| + uint8_t c = value[node->critbyte()];
|
| const int direction =
|
| (1 + static_cast<unsigned>(node->otherbits() | c)) >> 8;
|
| where_index = &node->data_[direction];
|
| @@ -269,14 +269,14 @@ InsertStatus StrikeRegister::Insert(const uint8 nonce[32],
|
| return NONCE_OK;
|
| }
|
|
|
| -const uint8* StrikeRegister::orbit() const {
|
| +const uint8_t* StrikeRegister::orbit() const {
|
| return orbit_;
|
| }
|
|
|
| -uint32 StrikeRegister::GetCurrentValidWindowSecs(
|
| - uint32 current_time_external) const {
|
| - uint32 current_time = ExternalTimeToInternal(current_time_external);
|
| - pair<uint32, uint32> valid_range =
|
| +uint32_t StrikeRegister::GetCurrentValidWindowSecs(
|
| + uint32_t current_time_external) const {
|
| + uint32_t current_time = ExternalTimeToInternal(current_time_external);
|
| + pair<uint32_t, uint32_t> valid_range =
|
| StrikeRegister::GetValidRange(current_time);
|
| if (valid_range.second >= valid_range.first) {
|
| return valid_range.second - current_time + 1;
|
| @@ -286,24 +286,24 @@ uint32 StrikeRegister::GetCurrentValidWindowSecs(
|
| }
|
|
|
| void StrikeRegister::Validate() {
|
| - set<uint32> free_internal_nodes;
|
| - for (uint32 i = internal_node_free_head_; i != kNil;
|
| + set<uint32_t> free_internal_nodes;
|
| + for (uint32_t i = internal_node_free_head_; i != kNil;
|
| i = internal_nodes_[i].next()) {
|
| CHECK_LT(i, max_entries_);
|
| CHECK_EQ(free_internal_nodes.count(i), 0u);
|
| free_internal_nodes.insert(i);
|
| }
|
|
|
| - set<uint32> free_external_nodes;
|
| - for (uint32 i = external_node_free_head_; i != kNil;
|
| + set<uint32_t> free_external_nodes;
|
| + for (uint32_t i = external_node_free_head_; i != kNil;
|
| i = external_node_next_ptr(i)) {
|
| CHECK_LT(i, max_entries_);
|
| CHECK_EQ(free_external_nodes.count(i), 0u);
|
| free_external_nodes.insert(i);
|
| }
|
|
|
| - set<uint32> used_external_nodes;
|
| - set<uint32> used_internal_nodes;
|
| + set<uint32_t> used_external_nodes;
|
| + set<uint32_t> used_internal_nodes;
|
|
|
| if (internal_node_head_ != kNil &&
|
| ((internal_node_head_ >> 8) & kExternalFlag) == 0) {
|
| @@ -315,19 +315,19 @@ void StrikeRegister::Validate() {
|
| }
|
|
|
| // static
|
| -uint32 StrikeRegister::TimeFromBytes(const uint8 d[4]) {
|
| - return static_cast<uint32>(d[0]) << 24 | static_cast<uint32>(d[1]) << 16 |
|
| - static_cast<uint32>(d[2]) << 8 | static_cast<uint32>(d[3]);
|
| +uint32_t StrikeRegister::TimeFromBytes(const uint8_t d[4]) {
|
| + return static_cast<uint32_t>(d[0]) << 24 | static_cast<uint32_t>(d[1]) << 16 |
|
| + static_cast<uint32_t>(d[2]) << 8 | static_cast<uint32_t>(d[3]);
|
| }
|
|
|
| -pair<uint32, uint32> StrikeRegister::GetValidRange(
|
| - uint32 current_time_internal) const {
|
| +pair<uint32_t, uint32_t> StrikeRegister::GetValidRange(
|
| + uint32_t current_time_internal) const {
|
| if (current_time_internal < horizon_) {
|
| // Empty valid range.
|
| - return std::make_pair(std::numeric_limits<uint32>::max(), 0);
|
| + return std::make_pair(std::numeric_limits<uint32_t>::max(), 0);
|
| }
|
|
|
| - uint32 lower_bound;
|
| + uint32_t lower_bound;
|
| if (current_time_internal >= window_secs_) {
|
| lower_bound = std::max(horizon_, current_time_internal - window_secs_);
|
| } else {
|
| @@ -340,25 +340,26 @@ pair<uint32, uint32> StrikeRegister::GetValidRange(
|
| // allows the strike server to degrade optimally in cases where the
|
| // insert rate exceeds |max_entries_ / (2 * window_secs_)| entries
|
| // per second.
|
| - uint32 upper_bound = current_time_internal +
|
| - std::min(current_time_internal - horizon_, window_secs_);
|
| + uint32_t upper_bound =
|
| + current_time_internal +
|
| + std::min(current_time_internal - horizon_, window_secs_);
|
|
|
| return std::make_pair(lower_bound, upper_bound);
|
| }
|
|
|
| -uint32 StrikeRegister::ExternalTimeToInternal(uint32 external_time) const {
|
| +uint32_t StrikeRegister::ExternalTimeToInternal(uint32_t external_time) const {
|
| return external_time - internal_epoch_;
|
| }
|
|
|
| -uint32 StrikeRegister::BestMatch(const uint8 v[24]) const {
|
| +uint32_t StrikeRegister::BestMatch(const uint8_t v[24]) const {
|
| if (internal_node_head_ == kNil) {
|
| return kNil;
|
| }
|
|
|
| - uint32 next = internal_node_head_ >> 8;
|
| + uint32_t next = internal_node_head_ >> 8;
|
| while ((next & kExternalFlag) == 0) {
|
| InternalNode* node = &internal_nodes_[next];
|
| - uint8 b = v[node->critbyte()];
|
| + uint8_t b = v[node->critbyte()];
|
| unsigned direction =
|
| (1 + static_cast<unsigned>(node->otherbits() | b)) >> 8;
|
| next = node->child(direction);
|
| @@ -367,23 +368,23 @@ uint32 StrikeRegister::BestMatch(const uint8 v[24]) const {
|
| return next & ~kExternalFlag;
|
| }
|
|
|
| -uint32& StrikeRegister::external_node_next_ptr(unsigned i) {
|
| - return *reinterpret_cast<uint32*>(&external_nodes_[i * kExternalNodeSize]);
|
| +uint32_t& StrikeRegister::external_node_next_ptr(unsigned i) {
|
| + return *reinterpret_cast<uint32_t*>(&external_nodes_[i * kExternalNodeSize]);
|
| }
|
|
|
| -uint8* StrikeRegister::external_node(unsigned i) {
|
| +uint8_t* StrikeRegister::external_node(unsigned i) {
|
| return &external_nodes_[i * kExternalNodeSize];
|
| }
|
|
|
| -uint32 StrikeRegister::GetFreeExternalNode() {
|
| - uint32 index = external_node_free_head_;
|
| +uint32_t StrikeRegister::GetFreeExternalNode() {
|
| + uint32_t index = external_node_free_head_;
|
| DCHECK(index != kNil);
|
| external_node_free_head_ = external_node_next_ptr(index);
|
| return index;
|
| }
|
|
|
| -uint32 StrikeRegister::GetFreeInternalNode() {
|
| - uint32 index = internal_node_free_head_;
|
| +uint32_t StrikeRegister::GetFreeInternalNode() {
|
| + uint32_t index = internal_node_free_head_;
|
| DCHECK(index != kNil);
|
| internal_node_free_head_ = internal_nodes_[index].next();
|
| return index;
|
| @@ -399,8 +400,8 @@ void StrikeRegister::DropOldestNode() {
|
| // to do that we keep pointers to the parent (wherep) and grandparent
|
| // (whereq) when walking down the tree.
|
|
|
| - uint32 p = internal_node_head_ >> 8, *wherep = &internal_node_head_,
|
| - *whereq = nullptr;
|
| + uint32_t p = internal_node_head_ >> 8, *wherep = &internal_node_head_,
|
| + *whereq = nullptr;
|
| while ((p & kExternalFlag) == 0) {
|
| whereq = wherep;
|
| InternalNode* inode = &internal_nodes_[p];
|
| @@ -410,9 +411,9 @@ void StrikeRegister::DropOldestNode() {
|
| p = (*wherep) >> 8;
|
| }
|
|
|
| - const uint32 ext_index = p & ~kExternalFlag;
|
| - const uint8* ext_node = external_node(ext_index);
|
| - uint32 new_horizon = ExternalTimeToInternal(TimeFromBytes(ext_node)) + 1;
|
| + const uint32_t ext_index = p & ~kExternalFlag;
|
| + const uint8_t* ext_node = external_node(ext_index);
|
| + uint32_t new_horizon = ExternalTimeToInternal(TimeFromBytes(ext_node)) + 1;
|
| DCHECK_LE(horizon_, new_horizon);
|
| horizon_ = new_horizon;
|
|
|
| @@ -425,29 +426,29 @@ void StrikeRegister::DropOldestNode() {
|
|
|
| // |wherep| points to the left child pointer in the parent so we can add
|
| // one and dereference to get the right child.
|
| - const uint32 other_child = wherep[1];
|
| + const uint32_t other_child = wherep[1];
|
| FreeInternalNode((*whereq) >> 8);
|
| *whereq = (*whereq & 0xff) | (other_child & 0xffffff00);
|
| FreeExternalNode(ext_index);
|
| }
|
|
|
| -void StrikeRegister::FreeExternalNode(uint32 index) {
|
| +void StrikeRegister::FreeExternalNode(uint32_t index) {
|
| external_node_next_ptr(index) = external_node_free_head_;
|
| external_node_free_head_ = index;
|
| }
|
|
|
| -void StrikeRegister::FreeInternalNode(uint32 index) {
|
| +void StrikeRegister::FreeInternalNode(uint32_t index) {
|
| internal_nodes_[index].SetNextPtr(internal_node_free_head_);
|
| internal_node_free_head_ = index;
|
| }
|
|
|
| -void StrikeRegister::ValidateTree(uint32 internal_node,
|
| +void StrikeRegister::ValidateTree(uint32_t internal_node,
|
| int last_bit,
|
| const vector<pair<unsigned, bool>>& bits,
|
| - const set<uint32>& free_internal_nodes,
|
| - const set<uint32>& free_external_nodes,
|
| - set<uint32>* used_internal_nodes,
|
| - set<uint32>* used_external_nodes) {
|
| + const set<uint32_t>& free_internal_nodes,
|
| + const set<uint32_t>& free_external_nodes,
|
| + set<uint32_t>* used_internal_nodes,
|
| + set<uint32_t>* used_external_nodes) {
|
| CHECK_LT(internal_node, max_entries_);
|
| const InternalNode* i = &internal_nodes_[internal_node];
|
| unsigned bit = 0;
|
| @@ -489,21 +490,21 @@ void StrikeRegister::ValidateTree(uint32 internal_node,
|
|
|
| for (unsigned child = 0; child < 2; child++) {
|
| if (i->child(child) & kExternalFlag) {
|
| - uint32 ext = i->child(child) & ~kExternalFlag;
|
| + uint32_t ext = i->child(child) & ~kExternalFlag;
|
| CHECK_EQ(free_external_nodes.count(ext), 0u);
|
| CHECK_EQ(used_external_nodes->count(ext), 0u);
|
| used_external_nodes->insert(ext);
|
| - const uint8* bytes = external_node(ext);
|
| + const uint8_t* bytes = external_node(ext);
|
| for (const pair<unsigned, bool>& pair : bits) {
|
| unsigned byte = pair.first / 8;
|
| DCHECK_LE(byte, 0xffu);
|
| unsigned bit_new = pair.first % 8;
|
| - static const uint8 kMasks[8] = {0x80, 0x40, 0x20, 0x10,
|
| - 0x08, 0x04, 0x02, 0x01};
|
| + static const uint8_t kMasks[8] = {0x80, 0x40, 0x20, 0x10,
|
| + 0x08, 0x04, 0x02, 0x01};
|
| CHECK_EQ((bytes[byte] & kMasks[bit_new]) != 0, pair.second);
|
| }
|
| } else {
|
| - uint32 inter = i->child(child);
|
| + uint32_t inter = i->child(child);
|
| vector<pair<unsigned, bool>> new_bits(bits);
|
| new_bits.push_back(pair<unsigned, bool>(bit, child != 0));
|
| CHECK_EQ(free_internal_nodes.count(inter), 0u);
|
|
|