| OLD | NEW |
| (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_HUFFMAN_FREQUENCY_TRACKER_H_ | |
| 6 #define NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_HUFFMAN_FREQUENCY_TRACKER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <vector> | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace transport_security_state { | |
| 17 | |
| 18 namespace { | |
| 19 class HuffmanNode; | |
| 20 } // namespace | |
| 21 | |
| 22 struct HuffmanRepresentation { | |
| 23 uint32_t bits; | |
| 24 uint32_t number_of_bits; | |
| 25 }; | |
| 26 | |
| 27 // A HuffmanRepresentationTable maps the original characters to their Huffman | |
| 28 // representation. The Huffman representation consists of the number of bits | |
| 29 // needed to represent the character and the actual bits. | |
| 30 using HuffmanRepresentationTable = std::map<uint8_t, HuffmanRepresentation>; | |
| 31 using HuffmanRepresentationPair = std::pair<uint8_t, HuffmanRepresentation>; | |
| 32 | |
| 33 // This class tracks the number of times each character is used and calculates | |
| 34 // a space efficient way to represent all tracked characters by constructing a | |
| 35 // Huffman tree based on the number of times each character is seen. | |
| 36 class HuffmanFrequencyTracker { | |
| 37 public: | |
| 38 HuffmanFrequencyTracker(); | |
| 39 ~HuffmanFrequencyTracker(); | |
| 40 | |
| 41 // Will increase the count for |character| by one, indicating it has been | |
| 42 // used. | |
| 43 void RecordUsage(uint8_t character); | |
| 44 | |
| 45 // Returns a HuffmanRepresentationTable based on the usage data collected | |
| 46 // through RecordUsage(). | |
| 47 HuffmanRepresentationTable ToTable(); | |
| 48 | |
| 49 // Outputs the Huffman representation as a vector of uint8_t's in a format | |
| 50 // Chromium can use to reconstruct the tree. | |
| 51 // | |
| 52 // The nodes of the tree are pairs of uint8s. The last node in the array is | |
| 53 // the root of the tree. Each pair is two uint8_t values, the first is "left" | |
| 54 // and the second is "right". If a uint8_t value has the MSB set then it | |
| 55 // represents a literal leaf value. Otherwise it's a pointer to the n'th | |
| 56 // element of the array. | |
| 57 std::vector<uint8_t> ToVector(); | |
| 58 | |
| 59 private: | |
| 60 // Determines the Huffman representation of the characters under |node| and | |
| 61 // inserts them into |*table|. |bits| and |number_of_bits| are used as a | |
| 62 // prefix. | |
| 63 void TreeToTable(HuffmanNode* node, | |
| 64 uint32_t bits, | |
| 65 uint32_t number_of_bits, | |
| 66 HuffmanRepresentationTable* table); | |
| 67 | |
| 68 // Converts the tree under |*node| into a byte representation in |*vector|. | |
| 69 // See ToVector() for more information on the format. | |
| 70 uint32_t WriteToVector(HuffmanNode* node, std::vector<uint8_t>* vector); | |
| 71 | |
| 72 // Constructs a Huffman tree based on |counts_|. | |
| 73 std::unique_ptr<HuffmanNode> BuildTree(); | |
| 74 | |
| 75 // Holds usage information for the tracked characters. Maps the character to | |
| 76 // the number of times its usage has been recorded through RecordUsage(). | |
| 77 std::map<uint8_t, uint32_t> counts_; | |
| 78 }; | |
| 79 | |
| 80 } // namespace transport_security_state | |
| 81 | |
| 82 } // namespace net | |
| 83 | |
| 84 #endif // NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_HUFFMAN_FREQUENCY_TRACKER
_H_ | |
| OLD | NEW |