| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_WRITER_H_ | 5 #ifndef NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_TRIE_TRIE_WRITER_H_ |
| 6 #define NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_WRITER_H_ | 6 #define NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_TRIE_TRIE_WRITER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "net/tools/domain_security_preload_generator/bit_writer.h" | 11 #include "net/tools/transport_security_state_generator/bit_writer.h" |
| 12 #include "net/tools/domain_security_preload_generator/domain_security_entry.h" | 12 #include "net/tools/transport_security_state_generator/huffman/huffman_builder.h
" |
| 13 #include "net/tools/domain_security_preload_generator/huffman/huffman_frequency_
tracker.h" | 13 #include "net/tools/transport_security_state_generator/transport_security_state_
entry.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 namespace transport_security_state { | 17 namespace transport_security_state { |
| 18 | 18 |
| 19 struct DomainSecurityEntry; | 19 struct TransportSecurityStateEntry; |
| 20 class TrieBitBuffer; | 20 class TrieBitBuffer; |
| 21 | 21 |
| 22 // Maps a name to an index. This is used to track the index of several values | 22 // Maps a name to an index. This is used to track the index of several values |
| 23 // in the C++ code. The trie refers to the array index of the values. For | 23 // in the C++ code. The trie refers to the array index of the values. For |
| 24 // example; the pinsets are outputted as a C++ array and the index for the | 24 // example; the pinsets are outputted as a C++ array and the index for the |
| 25 // pinset in that array is placed in the trie. | 25 // pinset in that array is placed in the trie. |
| 26 using NameIDMap = std::map<std::string, uint32_t>; | 26 using NameIDMap = std::map<std::string, uint32_t>; |
| 27 using NameIDPair = std::pair<std::string, uint32_t>; | 27 using NameIDPair = std::pair<std::string, uint32_t>; |
| 28 | 28 |
| 29 class TrieWriter { | 29 class TrieWriter { |
| 30 public: | 30 public: |
| 31 enum : uint8_t { kTerminalValue = 0, kEndOfTableValue = 127 }; | 31 enum : uint8_t { kTerminalValue = 0, kEndOfTableValue = 127 }; |
| 32 | 32 |
| 33 TrieWriter(const HuffmanRepresentationTable& huffman_table, | 33 TrieWriter(const HuffmanRepresentationTable& huffman_table, |
| 34 const NameIDMap& domain_ids_map, | 34 const NameIDMap& domain_ids_map, |
| 35 const NameIDMap& expect_ct_report_uri_map, | 35 const NameIDMap& expect_ct_report_uri_map, |
| 36 const NameIDMap& expect_staple_report_uri_map, | 36 const NameIDMap& expect_staple_report_uri_map, |
| 37 const NameIDMap& pinsets_map, | 37 const NameIDMap& pinsets_map, |
| 38 HuffmanFrequencyTracker* frequency_tracker); | 38 HuffmanBuilder* huffman_builder); |
| 39 ~TrieWriter(); | 39 ~TrieWriter(); |
| 40 | 40 |
| 41 // Constructs a trie containing all |entries|. The output is written to | 41 // Constructs a trie containing all |entries|. The output is written to |
| 42 // |buffer_|. Returns the position of the trie root. | 42 // |buffer_|. Returns the position of the trie root. |
| 43 uint32_t WriteEntries(const DomainSecurityEntries& entries); | 43 uint32_t WriteEntries(const TransportSecurityStateEntries& entries); |
| 44 | 44 |
| 45 // Returns the position |buffer_| is currently at. The returned value | 45 // Returns the position |buffer_| is currently at. The returned value |
| 46 // represents the number of bits. | 46 // represents the number of bits. |
| 47 uint32_t position() const; | 47 uint32_t position() const; |
| 48 | 48 |
| 49 // Flushes |buffer_|. | 49 // Flushes |buffer_|. |
| 50 void Flush(); | 50 void Flush(); |
| 51 | 51 |
| 52 // Returns the trie bytes. Call Flush() first to ensure the buffer is | 52 // Returns the trie bytes. Call Flush() first to ensure the buffer is |
| 53 // complete. | 53 // complete. |
| 54 const std::vector<uint8_t>& bytes() const { return buffer_.bytes(); } | 54 const std::vector<uint8_t>& bytes() const { return buffer_.bytes(); } |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 uint32_t WriteDispatchTables(ReversedEntries::iterator start, | 57 uint32_t WriteDispatchTables(ReversedEntries::iterator start, |
| 58 ReversedEntries::iterator end); | 58 ReversedEntries::iterator end); |
| 59 | 59 |
| 60 // Serializes |*entry| and writes it to |*writer|. | 60 // Serializes |*entry| and writes it to |*writer|. |
| 61 void WriteSecurityEntry(const DomainSecurityEntry* entry, | 61 void WriteEntry(const TransportSecurityStateEntry* entry, |
| 62 TrieBitBuffer* writer); | 62 TrieBitBuffer* writer); |
| 63 | 63 |
| 64 // Removes the first |length| characters from all entries between |start| and | 64 // Removes the first |length| characters from all entries between |start| and |
| 65 // |end|. | 65 // |end|. |
| 66 void RemovePrefix(size_t length, | 66 void RemovePrefix(size_t length, |
| 67 ReversedEntries::iterator start, | 67 ReversedEntries::iterator start, |
| 68 ReversedEntries::iterator end); | 68 ReversedEntries::iterator end); |
| 69 | 69 |
| 70 // Searches for the longest common prefix for all entries between |start| and | 70 // Searches for the longest common prefix for all entries between |start| and |
| 71 // |end|. | 71 // |end|. |
| 72 std::vector<uint8_t> LongestCommonPrefix(ReversedEntries::iterator start, | 72 std::vector<uint8_t> LongestCommonPrefix(ReversedEntries::iterator start, |
| 73 ReversedEntries::iterator end) const; | 73 ReversedEntries::iterator end) const; |
| 74 | 74 |
| 75 // Returns the reversed |hostname| as a vector of bytes. The reversed hostname | 75 // Returns the reversed |hostname| as a vector of bytes. The reversed hostname |
| 76 // will be terminated by |kTerminalValue|. | 76 // will be terminated by |kTerminalValue|. |
| 77 std::vector<uint8_t> ReverseName(const std::string& hostname) const; | 77 std::vector<uint8_t> ReverseName(const std::string& hostname) const; |
| 78 | 78 |
| 79 BitWriter buffer_; | 79 BitWriter buffer_; |
| 80 const HuffmanRepresentationTable& huffman_table_; | 80 const HuffmanRepresentationTable& huffman_table_; |
| 81 const NameIDMap& domain_ids_map_; | 81 const NameIDMap& domain_ids_map_; |
| 82 const NameIDMap& expect_ct_report_uri_map_; | 82 const NameIDMap& expect_ct_report_uri_map_; |
| 83 const NameIDMap& expect_staple_report_uri_map_; | 83 const NameIDMap& expect_staple_report_uri_map_; |
| 84 const NameIDMap& pinsets_map_; | 84 const NameIDMap& pinsets_map_; |
| 85 HuffmanFrequencyTracker* frequency_tracker_; | 85 HuffmanBuilder* huffman_builder_; |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 } // namespace transport_security_state | 88 } // namespace transport_security_state |
| 89 | 89 |
| 90 } // namespace net | 90 } // namespace net |
| 91 | 91 |
| 92 #endif // NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_WRITER_H_ | 92 #endif // NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_TRIE_TRIE_WRITER_H_ |
| OLD | NEW |