| 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 #include "net/base/pem_tokenizer.h" | 5 #include "net/base/pem_tokenizer.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 const char kPEMSearchBlock[] = "-----BEGIN "; | 12 const char kPEMSearchBlock[] = "-----BEGIN "; |
| 13 const char kPEMBeginBlock[] = "-----BEGIN %s-----"; | 13 const char kPEMBeginBlock[] = "-----BEGIN %s-----"; |
| 14 const char kPEMEndBlock[] = "-----END %s-----"; | 14 const char kPEMEndBlock[] = "-----END %s-----"; |
| 15 | 15 |
| 16 } // namespace | 16 } // namespace |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 using base::StringPiece; | 20 using base::StringPiece; |
| 21 | 21 |
| 22 struct PEMTokenizer::PEMType { |
| 23 std::string type; |
| 24 std::string header; |
| 25 std::string footer; |
| 26 }; |
| 27 |
| 22 PEMTokenizer::PEMTokenizer( | 28 PEMTokenizer::PEMTokenizer( |
| 23 const StringPiece& str, | 29 const StringPiece& str, |
| 24 const std::vector<std::string>& allowed_block_types) { | 30 const std::vector<std::string>& allowed_block_types) { |
| 25 Init(str, allowed_block_types); | 31 Init(str, allowed_block_types); |
| 26 } | 32 } |
| 27 | 33 |
| 34 PEMTokenizer::~PEMTokenizer() { |
| 35 } |
| 36 |
| 28 bool PEMTokenizer::GetNext() { | 37 bool PEMTokenizer::GetNext() { |
| 29 while (pos_ != StringPiece::npos) { | 38 while (pos_ != StringPiece::npos) { |
| 30 // Scan for the beginning of the next PEM encoded block. | 39 // Scan for the beginning of the next PEM encoded block. |
| 31 pos_ = str_.find(kPEMSearchBlock, pos_); | 40 pos_ = str_.find(kPEMSearchBlock, pos_); |
| 32 if (pos_ == StringPiece::npos) | 41 if (pos_ == StringPiece::npos) |
| 33 return false; // No more PEM blocks | 42 return false; // No more PEM blocks |
| 34 | 43 |
| 35 std::vector<PEMType>::const_iterator it; | 44 std::vector<PEMType>::const_iterator it; |
| 36 // Check to see if it is of an acceptable block type. | 45 // Check to see if it is of an acceptable block type. |
| 37 for (it = block_types_.begin(); it != block_types_.end(); ++it) { | 46 for (it = block_types_.begin(); it != block_types_.end(); ++it) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 allowed_block_types.begin(); it != allowed_block_types.end(); ++it) { | 95 allowed_block_types.begin(); it != allowed_block_types.end(); ++it) { |
| 87 PEMType allowed_type; | 96 PEMType allowed_type; |
| 88 allowed_type.type = *it; | 97 allowed_type.type = *it; |
| 89 allowed_type.header = StringPrintf(kPEMBeginBlock, it->c_str()); | 98 allowed_type.header = StringPrintf(kPEMBeginBlock, it->c_str()); |
| 90 allowed_type.footer = StringPrintf(kPEMEndBlock, it->c_str()); | 99 allowed_type.footer = StringPrintf(kPEMEndBlock, it->c_str()); |
| 91 block_types_.push_back(allowed_type); | 100 block_types_.push_back(allowed_type); |
| 92 } | 101 } |
| 93 } | 102 } |
| 94 | 103 |
| 95 } // namespace net | 104 } // namespace net |
| OLD | NEW |