Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: net/tools/domain_security_preload_generator/trie/trie_writer.h

Issue 2551153003: Add static domain security state generator tool. (Closed)
Patch Set: fix base64 issue and accidental replace. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_WRITER_H_
6 #define NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_WRITER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "net/tools/domain_security_preload_generator/bit_writer.h"
12 #include "net/tools/domain_security_preload_generator/domain_security_entry.h"
13 #include "net/tools/domain_security_preload_generator/huffman/huffman_frequency_ tracker.h"
14
15 namespace net {
16
17 class DomainSecurityEntry;
18 class TrieBitBuffer;
19
20 namespace {
21
22 struct ReversedEntry {
23 ReversedEntry(std::vector<uint8_t> reversed_name,
24 const DomainSecurityEntry* entry);
25 ~ReversedEntry();
26
27 std::vector<uint8_t> reversed_name;
28 const DomainSecurityEntry* entry;
29 };
30
31 } // namespace
32
33 // Maps a name to an index. This is used to track the index of several values
34 // in the C++ index. The index refers the the array index of the name
35 // (e.g. pinsets) are outputted as an C++ array and the index for the pinset
36 // is encoded in the trie.
37 using NameIDMap = std::map<std::string, uint32_t>;
38 using NameIDPair = std::pair<std::string, uint32_t>;
39 using ReversedEntries = std::vector<std::unique_ptr<ReversedEntry>>;
40
41 class TrieWriter {
42 public:
43 enum : uint8_t { kTerminalValue = 0, kEndOfTableValue = 127 };
44
45 TrieWriter(const HuffmanRepresentationTable& huffman_table,
46 const NameIDMap& domain_ids_map,
47 const NameIDMap& expect_ct_report_uri_map,
48 const NameIDMap& expect_staple_report_uri_map,
49 const NameIDMap& pinsets_map,
50 HuffmanFrequencyTracker* frequency_tracker);
51 ~TrieWriter();
52
53 // Construct a trie containing all |entries|. The output is written to
54 // |buffer_|. Returns the position of the trie root.
55 int WriteEntries(const DomainSecurityEntries& entries);
56
57 // Returns the position |buffer_| is currently at. The returned value is in
58 // bits.
59 uint32_t position() const;
60
61 // Close the BitWriter to ensure any buffered bits are written to the vector.
62 void close();
63
64 // Returns the trie bytes.
65 const std::vector<uint8_t>& bytes() const { return buffer_.bytes(); }
66
67 private:
68 int WriteDispatchTables(ReversedEntries::iterator start,
69 ReversedEntries::iterator end,
70 int depth);
71
72 // Serializes |*entry| and writes the output to |*writer|.
73 void WriteSecurityEntry(const DomainSecurityEntry* entry,
74 TrieBitBuffer* writer);
75
76 // Removes the first |length| characters from all entries between |start| and
77 // |end|.
78 void RemovePrefix(size_t length,
79 ReversedEntries::iterator start,
80 ReversedEntries::iterator end);
81
82 // Searches for the longest common prefix for all entries between |start| and
83 // |end|.
84 std::vector<uint8_t> LongestCommonPrefix(ReversedEntries::iterator start,
85 ReversedEntries::iterator end) const;
86
87 // Returns the reversed |hostname| as a vector of bytes. The reversed hostname
88 // will be terminated by |kTerminalValue|.
89 std::vector<uint8_t> ReverseName(const std::string& hostname) const;
90
91 BitWriter buffer_;
92 const HuffmanRepresentationTable& huffman_table_;
93 const NameIDMap& domain_ids_map_;
94 const NameIDMap& expect_ct_report_uri_map_;
95 const NameIDMap& expect_staple_report_uri_map_;
96 const NameIDMap& pinsets_map_;
97 HuffmanFrequencyTracker* frequency_tracker_;
98 };
99
100 } // namespace net
101
102 #endif // NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_WRITER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698