| 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_TRANSPORT_SECURITY_STATE_GENERATOR_HUFFMAN_BUILDER_H_ | 5 #ifndef NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_HUFFMAN_BUILDER_H_ |
| 6 #define NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_HUFFMAN_BUILDER_H_ | 6 #define NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_HUFFMAN_BUILDER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 // This class tracks the number of times each character is used and calculates | 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 | 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. | 35 // Huffman tree based on the number of times each character is seen. |
| 36 class HuffmanBuilder { | 36 class HuffmanBuilder { |
| 37 public: | 37 public: |
| 38 HuffmanBuilder(); | 38 HuffmanBuilder(); |
| 39 ~HuffmanBuilder(); | 39 ~HuffmanBuilder(); |
| 40 | 40 |
| 41 // Will increase the count for |character| by one, indicating it has been | 41 // Will increase the count for |character| by one, indicating it has been |
| 42 // used. | 42 // used. |character| must be in the range 0-127. |
| 43 void RecordUsage(uint8_t character); | 43 void RecordUsage(uint8_t character); |
| 44 | 44 |
| 45 // Returns a HuffmanRepresentationTable based on the usage data collected | 45 // Returns a HuffmanRepresentationTable based on the usage data collected |
| 46 // through RecordUsage(). | 46 // through RecordUsage(). |
| 47 HuffmanRepresentationTable ToTable(); | 47 HuffmanRepresentationTable ToTable(); |
| 48 | 48 |
| 49 // Outputs the Huffman representation as a vector of uint8_t's in a format | 49 // Outputs the Huffman representation as a vector of uint8_t's in a format |
| 50 // Chromium can use to reconstruct the tree. | 50 // Chromium can use to reconstruct the tree. |
| 51 // | 51 // |
| 52 // The nodes of the tree are pairs of uint8s. The last node in the array is | 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" | 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 | 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 | 55 // represents a literal leaf value. Otherwise it's a pointer to the n'th |
| 56 // element of the array. | 56 // element of the array. |
| 57 std::vector<uint8_t> ToVector(); | 57 std::vector<uint8_t> ToVector(); |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 // Determines the Huffman representation of the characters under |node| and | 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 | 61 // inserts them into |*table|. |bits| and |number_of_bits| are used as a |
| 62 // prefix. | 62 // prefix. |
| 63 void TreeToTable(HuffmanNode* node, | 63 void TreeToTable(HuffmanNode* node, |
| 64 uint32_t bits, | 64 uint32_t bits, |
| 65 uint32_t number_of_bits, | 65 uint32_t number_of_bits, |
| 66 HuffmanRepresentationTable* table); | 66 HuffmanRepresentationTable* table); |
| 67 | 67 |
| 68 // Converts the tree under |*node| into a byte representation in |*vector|. | 68 // Converts the tree under |*node| into a byte representation in |*vector|. |
| 69 // See ToVector() for more information on the format. | 69 // See ToVector() for more information on the format. |
| 70 uint32_t WriteToVector(HuffmanNode* node, std::vector<uint8_t>* vector); | 70 uint32_t WriteToVector(HuffmanNode* node, std::vector<uint8_t>* vector); |
| 71 | 71 |
| 72 // Constructs a Huffman tree based on |counts_|. | 72 // Constructs a Huffman tree based on |counts_|. Appends additional nodes to |
| 73 // the tree until it contains at least 2 leafs. |
| 73 std::unique_ptr<HuffmanNode> BuildTree(); | 74 std::unique_ptr<HuffmanNode> BuildTree(); |
| 74 | 75 |
| 75 // Holds usage information for the tracked characters. Maps the character to | 76 // Holds usage information for the tracked characters. Maps the character to |
| 76 // the number of times its usage has been recorded through RecordUsage(). | 77 // the number of times its usage has been recorded through RecordUsage(). |
| 77 std::map<uint8_t, uint32_t> counts_; | 78 std::map<uint8_t, uint32_t> counts_; |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 } // namespace transport_security_state | 81 } // namespace transport_security_state |
| 81 | 82 |
| 82 } // namespace net | 83 } // namespace net |
| 83 | 84 |
| 84 #endif // NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_HUFFMAN_BUILDER_H_ | 85 #endif // NET_TOOLS_TRANSPORT_SECURITY_STATE_GENERATOR_HUFFMAN_BUILDER_H_ |
| OLD | NEW |