| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_BASE_PEM_TOKENIZER_H_ | 5 #ifndef NET_BASE_PEM_TOKENIZER_H_ |
| 6 #define NET_BASE_PEM_TOKENIZER_H_ | 6 #define NET_BASE_PEM_TOKENIZER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 // PEMTokenizer is a utility class for the parsing of data encapsulated | 16 // PEMTokenizer is a utility class for the parsing of data encapsulated |
| 17 // using RFC 1421, Privacy Enhancement for Internet Electronic Mail. It | 17 // using RFC 1421, Privacy Enhancement for Internet Electronic Mail. It |
| 18 // does not implement the full specification, most notably it does not | 18 // does not implement the full specification, most notably it does not |
| 19 // support the Encapsulated Header Portion described in Section 4.4. | 19 // support the Encapsulated Header Portion described in Section 4.4. |
| 20 class PEMTokenizer { | 20 class PEMTokenizer { |
| 21 public: | 21 public: |
| 22 // Create a new PEMTokenizer that iterates through |str| searching for | 22 // Create a new PEMTokenizer that iterates through |str| searching for |
| 23 // instances of PEM encoded blocks that are of the |allowed_block_types|. | 23 // instances of PEM encoded blocks that are of the |allowed_block_types|. |
| 24 // |str| must remain valid for the duration of the PEMTokenizer. | 24 // |str| must remain valid for the duration of the PEMTokenizer. |
| 25 PEMTokenizer(const base::StringPiece& str, | 25 PEMTokenizer(const base::StringPiece& str, |
| 26 const std::vector<std::string>& allowed_block_types); | 26 const std::vector<std::string>& allowed_block_types); |
| 27 ~PEMTokenizer(); |
| 27 | 28 |
| 28 // Attempts to decode the next PEM block in the string. Returns false if no | 29 // Attempts to decode the next PEM block in the string. Returns false if no |
| 29 // PEM blocks can be decoded. The decoded PEM block will be available via | 30 // PEM blocks can be decoded. The decoded PEM block will be available via |
| 30 // data(). | 31 // data(). |
| 31 bool GetNext(); | 32 bool GetNext(); |
| 32 | 33 |
| 33 // Returns the PEM block type (eg: CERTIFICATE) of the last successfully | 34 // Returns the PEM block type (eg: CERTIFICATE) of the last successfully |
| 34 // decoded PEM block. | 35 // decoded PEM block. |
| 35 // GetNext() must have returned true before calling this method. | 36 // GetNext() must have returned true before calling this method. |
| 36 const std::string& block_type() const { return block_type_; } | 37 const std::string& block_type() const { return block_type_; } |
| 37 | 38 |
| 38 // Returns the raw, Base64-decoded data of the last successfully decoded | 39 // Returns the raw, Base64-decoded data of the last successfully decoded |
| 39 // PEM block. | 40 // PEM block. |
| 40 // GetNext() must have returned true before calling this method. | 41 // GetNext() must have returned true before calling this method. |
| 41 const std::string& data() const { return data_; } | 42 const std::string& data() const { return data_; } |
| 42 | 43 |
| 43 private: | 44 private: |
| 44 void Init(const base::StringPiece& str, | 45 void Init(const base::StringPiece& str, |
| 45 const std::vector<std::string>& allowed_block_types); | 46 const std::vector<std::string>& allowed_block_types); |
| 46 | 47 |
| 47 // A simple cache of the allowed PEM header and footer for a given PEM | 48 // A simple cache of the allowed PEM header and footer for a given PEM |
| 48 // block type, so that it is only computed once. | 49 // block type, so that it is only computed once. |
| 49 struct PEMType { | 50 struct PEMType; |
| 50 std::string type; | |
| 51 std::string header; | |
| 52 std::string footer; | |
| 53 }; | |
| 54 | 51 |
| 55 // The string to search, which must remain valid for as long as this class | 52 // The string to search, which must remain valid for as long as this class |
| 56 // is around. | 53 // is around. |
| 57 base::StringPiece str_; | 54 base::StringPiece str_; |
| 58 | 55 |
| 59 // The current position within |str_| that searching should begin from, | 56 // The current position within |str_| that searching should begin from, |
| 60 // or StringPiece::npos if iteration is complete | 57 // or StringPiece::npos if iteration is complete |
| 61 base::StringPiece::size_type pos_; | 58 base::StringPiece::size_type pos_; |
| 62 | 59 |
| 63 // The type of data that was encoded, as indicated in the PEM | 60 // The type of data that was encoded, as indicated in the PEM |
| 64 // Pre-Encapsulation Boundary (eg: CERTIFICATE, PKCS7, or | 61 // Pre-Encapsulation Boundary (eg: CERTIFICATE, PKCS7, or |
| 65 // PRIVACY-ENHANCED MESSAGE). | 62 // PRIVACY-ENHANCED MESSAGE). |
| 66 std::string block_type_; | 63 std::string block_type_; |
| 67 | 64 |
| 68 // The types of PEM blocks that are allowed. PEM blocks that are not of | 65 // The types of PEM blocks that are allowed. PEM blocks that are not of |
| 69 // one of these types will be skipped. | 66 // one of these types will be skipped. |
| 70 std::vector<PEMType> block_types_; | 67 std::vector<PEMType> block_types_; |
| 71 | 68 |
| 72 // The raw (Base64-decoded) data of the last successfully decoded block. | 69 // The raw (Base64-decoded) data of the last successfully decoded block. |
| 73 std::string data_; | 70 std::string data_; |
| 74 | 71 |
| 75 DISALLOW_COPY_AND_ASSIGN(PEMTokenizer); | 72 DISALLOW_COPY_AND_ASSIGN(PEMTokenizer); |
| 76 }; | 73 }; |
| 77 | 74 |
| 78 } // namespace net | 75 } // namespace net |
| 79 | 76 |
| 80 #endif // NET_BASE_PEM_TOKENIZER_H_ | 77 #endif // NET_BASE_PEM_TOKENIZER_H_ |
| OLD | NEW |