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