2 * Copyright 2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * An Attribute whose values are binary data.
24 #include "attribute/BinaryAttribute.h"
26 #include <xercesc/util/Base64.hpp>
28 using namespace shibsp;
32 SHIBSP_DLLLOCAL Attribute* BinaryAttributeFactory(DDF& in) {
33 return new BinaryAttribute(in);
37 BinaryAttribute::BinaryAttribute(const vector<string>& ids) : Attribute(ids)
41 BinaryAttribute::BinaryAttribute(DDF& in) : Attribute(in)
44 DDF val = in.first().first();
45 while (val.string()) {
46 m_serialized.push_back(val.string());
47 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(val.string()), &x);
49 m_values.push_back(string(reinterpret_cast<char*>(decoded), x));
50 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
51 XMLString::release(&decoded);
53 XMLString::release((char**)&decoded);
56 val = in.first().next();
60 BinaryAttribute::~BinaryAttribute()
64 vector<string>& BinaryAttribute::getValues()
69 const vector<string>& BinaryAttribute::getValues() const
74 size_t BinaryAttribute::valueCount() const
76 return m_values.size();
79 void BinaryAttribute::clearSerializedValues()
84 const char* BinaryAttribute::getString(size_t index) const
86 return m_values[index].c_str();
89 void BinaryAttribute::removeValue(size_t index)
91 Attribute::removeValue(index);
92 if (index < m_values.size())
93 m_values.erase(m_values.begin() + index);
96 const vector<string>& BinaryAttribute::getSerializedValues() const
100 if (m_serialized.empty()) {
101 for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
102 XMLByte* enc = Base64::encode(reinterpret_cast<const XMLByte*>(i->data()), i->size(), &len);
104 for (pos=enc, pos2=enc; *pos2; pos2++)
108 m_serialized.push_back(reinterpret_cast<char*>(enc));
109 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
110 XMLString::release(&enc);
112 XMLString::release((char**)&enc);
117 return Attribute::getSerializedValues();
120 DDF BinaryAttribute::marshall() const
122 DDF ddf = Attribute::marshall();
124 DDF vlist = ddf.first();
125 const vector<string>& encoded = getSerializedValues();
126 for (vector<string>::const_iterator i = encoded.begin(); i != encoded.end(); ++i) {
127 DDF val = DDF(nullptr).string(i->c_str());