SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / attribute / StringAttributeDecoder.cpp
index c19fc6b..b46af05 100644 (file)
@@ -1,23 +1,27 @@
-/*
- *  Copyright 2001-2009 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
  * StringAttributeDecoder.cpp
  *
- * Decodes SAML into SimpleAttributes
+ * Decodes SAML into SimpleAttributes.
  */
 
 #include "internal.h"
@@ -40,8 +44,15 @@ namespace shibsp {
         StringAttributeDecoder(const DOMElement* e) : AttributeDecoder(e) {}
         ~StringAttributeDecoder() {}
 
+        // deprecated method
+        shibsp::Attribute* decode(
+            const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
+            ) const {
+            return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
+        }
+
         shibsp::Attribute* decode(
-            const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL
+            const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
             ) const;
     };
 
@@ -52,22 +63,20 @@ namespace shibsp {
 };
 
 shibsp::Attribute* StringAttributeDecoder::decode(
-    const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
+    const GenericRequest* request, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
     ) const
 {
-    char* val;
     auto_ptr<SimpleAttribute> simple(new SimpleAttribute(ids));
     vector<string>& dest = simple->getValues();
-    vector<XMLObject*>::const_iterator v,stop;
+    pair<vector<XMLObject*>::const_iterator,vector<XMLObject*>::const_iterator> valrange;
 
-    Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.String");
+    Category& log = Category::getInstance(SHIBSP_LOGCAT ".AttributeDecoder.String");
 
     if (xmlObject && XMLString::equals(opensaml::saml1::Attribute::LOCAL_NAME,xmlObject->getElementQName().getLocalPart())) {
         const opensaml::saml2::Attribute* saml2attr = dynamic_cast<const opensaml::saml2::Attribute*>(xmlObject);
         if (saml2attr) {
             const vector<XMLObject*>& values = saml2attr->getAttributeValues();
-            v = values.begin();
-            stop = values.end();
+            valrange = valueRange(request, values);
             if (log.isDebugEnabled()) {
                 auto_ptr_char n(saml2attr->getName());
                 log.debug(
@@ -80,8 +89,7 @@ shibsp::Attribute* StringAttributeDecoder::decode(
             const opensaml::saml1::Attribute* saml1attr = dynamic_cast<const opensaml::saml1::Attribute*>(xmlObject);
             if (saml1attr) {
                 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
-                v = values.begin();
-                stop = values.end();
+                valrange = valueRange(request, values);
                 if (log.isDebugEnabled()) {
                     auto_ptr_char n(saml1attr->getAttributeName());
                 log.debug(
@@ -92,25 +100,24 @@ shibsp::Attribute* StringAttributeDecoder::decode(
             }
             else {
                 log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
-                return NULL;
+                return nullptr;
             }
         }
 
-        for (; v!=stop; ++v) {
-            if (!(*v)->hasChildren()) {
-                val = toUTF8((*v)->getTextContent());
-                if (val && *val)
-                    dest.push_back(val);
+        for (; valrange.first != valrange.second; ++valrange.first) {
+            if (!(*valrange.first)->hasChildren()) {
+                auto_arrayptr<char> val(toUTF8((*valrange.first)->getTextContent()));
+                if (val.get() && *val.get())
+                    dest.push_back(val.get());
                 else
                     log.warn("skipping empty AttributeValue");
-                delete[] val;
             }
             else {
                 log.warn("skipping complex AttributeValue");
             }
         }
 
-        return dest.empty() ? NULL : _decode(simple.release());
+        return dest.empty() ? nullptr : _decode(simple.release());
     }
 
     const NameID* saml2name = dynamic_cast<const NameID*>(xmlObject);
@@ -119,7 +126,11 @@ shibsp::Attribute* StringAttributeDecoder::decode(
             auto_ptr_char f(saml2name->getFormat());
             log.debug("decoding SimpleAttribute (%s) from SAML 2 NameID with Format (%s)", ids.front().c_str(), f.get() ? f.get() : "unspecified");
         }
-        val = toUTF8(saml2name->getName());
+        auto_arrayptr<char> val(toUTF8(saml2name->getName()));
+        if (val.get() && *val.get())
+            dest.push_back(val.get());
+        else
+            log.warn("ignoring empty NameID");
     }
     else {
         const NameIdentifier* saml1name = dynamic_cast<const NameIdentifier*>(xmlObject);
@@ -131,18 +142,17 @@ shibsp::Attribute* StringAttributeDecoder::decode(
                     ids.front().c_str(), f.get() ? f.get() : "unspecified"
                     );
             }
-            val = toUTF8(saml1name->getName());
+            auto_arrayptr<char> val(toUTF8(saml1name->getName()));
+            if (val.get() && *val.get())
+                dest.push_back(val.get());
+            else
+                log.warn("ignoring empty NameIdentifier");
         }
         else {
             log.warn("XMLObject type not recognized by StringAttributeDecoder, no values returned");
-            return NULL;
+            return nullptr;
         }
     }
 
-    if (val && *val)
-        dest.push_back(val);
-    else
-        log.warn("ignoring empty NameID");
-    delete[] val;
-    return dest.empty() ? NULL : _decode(simple.release());
+    return dest.empty() ? nullptr : _decode(simple.release());
 }