https://bugs.internet2.edu/jira/browse/SSPCPP-365
[shibboleth/sp.git] / shibsp / attribute / BinaryAttribute.cpp
1 /*
2  *  Copyright 2009 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * BinaryAttribute.cpp
19  *
20  * An Attribute whose values are binary data.
21  */
22
23 #include "internal.h"
24 #include "attribute/BinaryAttribute.h"
25
26 #include <xercesc/util/Base64.hpp>
27
28 using namespace shibsp;
29 using namespace std;
30
31 namespace shibsp {
32     SHIBSP_DLLLOCAL Attribute* BinaryAttributeFactory(DDF& in) {
33         return new BinaryAttribute(in);
34     }
35 };
36
37 BinaryAttribute::BinaryAttribute(const vector<string>& ids) : Attribute(ids)
38 {
39 }
40
41 BinaryAttribute::BinaryAttribute(DDF& in) : Attribute(in)
42 {
43     xsecsize_t x;
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);
48         if (decoded) {
49             m_values.push_back(string(reinterpret_cast<char*>(decoded), x));
50 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
51             XMLString::release(&decoded);
52 #else
53             XMLString::release((char**)&decoded);
54 #endif
55         }
56         val = in.first().next();
57     }
58 }
59
60 BinaryAttribute::~BinaryAttribute()
61 {
62 }
63
64 vector<string>& BinaryAttribute::getValues()
65 {
66     return m_values;
67 }
68
69 const vector<string>& BinaryAttribute::getValues() const
70 {
71     return m_values;
72 }
73
74 size_t BinaryAttribute::valueCount() const
75 {
76     return m_values.size();
77 }
78
79 void BinaryAttribute::clearSerializedValues()
80 {
81     m_serialized.clear();
82 }
83
84 const char* BinaryAttribute::getString(size_t index) const
85 {
86     return m_values[index].c_str();
87 }
88
89 void BinaryAttribute::removeValue(size_t index)
90 {
91     Attribute::removeValue(index);
92     if (index < m_values.size())
93         m_values.erase(m_values.begin() + index);
94 }
95
96 const vector<string>& BinaryAttribute::getSerializedValues() const
97 {
98     xsecsize_t len;
99     XMLByte *pos, *pos2;
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);
103             if (enc) {
104                 for (pos=enc, pos2=enc; *pos2; pos2++)
105                     if (isgraph(*pos2))
106                         *pos++=*pos2;
107                 *pos=0;
108                 m_serialized.push_back(reinterpret_cast<char*>(enc));
109 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
110                 XMLString::release(&enc);
111 #else
112                 XMLString::release((char**)&enc);
113 #endif
114             }
115         }
116     }
117     return Attribute::getSerializedValues();
118 }
119
120 DDF BinaryAttribute::marshall() const
121 {
122     DDF ddf = Attribute::marshall();
123     ddf.name("Binary");
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());
128         vlist.add(val);
129     }
130     return ddf;
131 }