SubjectName.cpp

Go to the documentation of this file.
00001 #include "SubjectName.h"
00002 
00003 #include <iostream>
00004 
00005 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00006 
00007 namespace OpenDDS {
00008 namespace Security {
00009 namespace SSL {
00010 
00011   SubjectName::SubjectName() {}
00012 
00013   SubjectName::SubjectName(const std::string& in, bool permissive)
00014   {
00015     parse(in, permissive);
00016   }
00017 
00018   SubjectName::SubjectName(const char* in, bool permissive)
00019   {
00020     parse(in, permissive);
00021   }
00022 
00023   int SubjectName::parse(const std::string& in, bool permissive)
00024   {
00025     return parse(in.data(), permissive);
00026   }
00027 
00028   int SubjectName::parse(const char* in, bool permissive)
00029   {
00030     if (in == NULL) {
00031       return -1;
00032     }
00033 
00034     // For now, assume ASCII encoding and not UTF-8
00035     if (permissive) {
00036       return parse_permissive(in);
00037     } else {
00038       if (in[0] == '/') {
00039         return parse_dce(in);
00040       } else {
00041         return parse_ldap_v3(in);
00042       }
00043     }
00044   }
00045 
00046   int SubjectName::simple_avp_seq_parse(const char* in, const char* s_del,
00047                                         const char* a_del, const char* s_trim,
00048                                         const char* a_trim, bool push_back)
00049   {
00050     std::string input(in);
00051     size_t input_end = input.size() - 1;
00052     map_.clear();
00053 
00054     ACE_UNUSED_ARG(push_back);
00055 
00056     // This parser is meant only to cover basic cases, more advanced cases
00057     // will need something different Specifically, this won't correctly handle
00058     // all escaped characters or all UTF-8 strings
00059 
00060     // Use size_t variables to mark positions of token beginnings and ends
00061 
00062     // We'll use "st" to mark positions for sequence tokens
00063     size_t st_begin = 0;
00064     size_t st_end = input.find_first_of(s_del);
00065 
00066     // Loop over all the sequence tokens
00067     while (st_begin != std::string::npos) {
00068       std::string st = input.substr(
00069         st_begin,
00070         (st_end == std::string::npos ? input_end + 1 : st_end) - st_begin);
00071 
00072       // Use once we've found a sequnce token, trim the beginning and end to
00073       // get a clean token
00074       size_t st_begin_clean = st.find_first_not_of(s_trim);
00075       size_t st_end_clean = st.find_last_not_of(s_trim);
00076 
00077       // If we've found a clean sequence token
00078       if (st_begin_clean != std::string::npos &&
00079           st_end_clean != std::string::npos) {
00080         std::string st_clean =
00081           st.substr(st_begin_clean, st_end_clean - st_begin_clean + 1);
00082 
00083         // We'll use "nt" to mark positions for name tokens
00084         size_t nt_begin = 0;
00085         size_t nt_end = st_clean.find_first_of(a_del);
00086 
00087         // If we actually found a delimiter
00088         if (nt_end != std::string::npos) {
00089           --nt_end;
00090 
00091           std::string nt = st_clean.substr(nt_begin, nt_end - nt_begin + 1);
00092 
00093           size_t nt_begin_clean = nt.find_first_not_of(a_trim);
00094           size_t nt_end_clean = nt.find_last_not_of(a_trim);
00095 
00096           // If we found a clean name token
00097           if (nt_begin_clean != std::string::npos &&
00098               nt_end_clean != std::string::npos) {
00099             std::string nt_clean =
00100               nt.substr(nt_begin_clean, nt_end_clean - nt_begin_clean + 1);
00101 
00102             // We'll use "vt" to mark positions for value tokens
00103             size_t vt_begin = nt_end + 2;  // Skip over the (single) delimiter
00104             size_t vt_end = st_clean.size() - 1;
00105 
00106             std::string vt = st_clean.substr(vt_begin, vt_end - vt_begin + 1);
00107 
00108             size_t vt_begin_clean = vt.find_first_not_of(a_trim);
00109             size_t vt_end_clean = vt.find_last_not_of(a_trim);
00110 
00111             // If we found a clean value token
00112             if (vt_begin_clean != std::string::npos &&
00113                 vt_end_clean != std::string::npos) {
00114               std::string vt_clean =
00115                 vt.substr(vt_begin_clean, vt_end_clean - vt_begin_clean + 1);
00116 
00117               // Push our clean pair into the map
00118               map_[nt_clean] = vt_clean;
00119             }
00120           }
00121         }
00122       }
00123 
00124       // Prepare for next iteration of loop
00125       if (st_end == std::string::npos) {
00126         st_begin = std::string::npos;
00127       } else {
00128         st_begin = st_end + 1;
00129         st_end = input.find_first_of(s_del, st_begin);
00130       }
00131     }
00132 
00133     return map_.empty() ? 1 : 0;
00134   }
00135 
00136   int SubjectName::parse_permissive(const char* in)
00137   {
00138     return simple_avp_seq_parse(in, ",/", "=", " ", " ", false);
00139   }
00140 
00141   int SubjectName::parse_dce(const char* in)
00142   {
00143     return simple_avp_seq_parse(in, "/", "=", " ", " ", true);
00144   }
00145 
00146   int SubjectName::parse_ldap_v3(const char* in)
00147   {
00148     return simple_avp_seq_parse(in, ",", "=", " ", " ", false);
00149   }
00150 
00151   bool SubjectName::operator==(const SubjectName& rhs) const
00152   {
00153     bool result = (map_.size() == rhs.map_.size());
00154     for (AttrMap::const_iterator i1 = map_.begin(), i2 = rhs.map_.begin();
00155          result == true && i1 != map_.end() && i2 != rhs.map_.end();
00156          ++i1, ++i2) {
00157       if (i1->first.compare(i2->first) != 0 ||
00158           i1->second.compare(i2->second) != 0) {
00159         result = false;
00160       }
00161     }
00162     return result;
00163   }
00164 
00165   bool SubjectName::operator!=(const SubjectName& rhs) const
00166   {
00167     return !(*this == rhs);
00168   }
00169 
00170 }  // namespace SSL
00171 }  // namespace Security
00172 }  // namespace OpenDDS
00173 
00174 OPENDDS_END_VERSIONED_NAMESPACE_DECL
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1