SSPCPP-568 The INSTALL_DIR has to have a trailing backslash or else keygen fails.
[shibboleth/cpp-sp.git] / shibsp / attribute / BinaryAttribute.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * BinaryAttribute.cpp
23  *
24  * An Attribute whose values are binary data.
25  */
26
27 #include "internal.h"
28 #include "attribute/BinaryAttribute.h"
29
30 #include <xercesc/util/Base64.hpp>
31
32 using namespace shibsp;
33 using namespace std;
34
35 namespace shibsp {
36     SHIBSP_DLLLOCAL Attribute* BinaryAttributeFactory(DDF& in) {
37         return new BinaryAttribute(in);
38     }
39 };
40
41 BinaryAttribute::BinaryAttribute(const vector<string>& ids) : Attribute(ids)
42 {
43 }
44
45 BinaryAttribute::BinaryAttribute(DDF& in) : Attribute(in)
46 {
47     xsecsize_t x;
48     DDF val = in.first().first();
49     while (val.string()) {
50         m_serialized.push_back(val.string());
51         XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(val.string()), &x);
52         if (decoded) {
53             m_values.push_back(string(reinterpret_cast<char*>(decoded), x));
54 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
55             XMLString::release(&decoded);
56 #else
57             XMLString::release((char**)&decoded);
58 #endif
59         }
60         val = in.first().next();
61     }
62 }
63
64 BinaryAttribute::~BinaryAttribute()
65 {
66 }
67
68 vector<string>& BinaryAttribute::getValues()
69 {
70     return m_values;
71 }
72
73 const vector<string>& BinaryAttribute::getValues() const
74 {
75     return m_values;
76 }
77
78 size_t BinaryAttribute::valueCount() const
79 {
80     return m_values.size();
81 }
82
83 void BinaryAttribute::clearSerializedValues()
84 {
85     m_serialized.clear();
86 }
87
88 const char* BinaryAttribute::getString(size_t index) const
89 {
90     return m_values[index].c_str();
91 }
92
93 void BinaryAttribute::removeValue(size_t index)
94 {
95     Attribute::removeValue(index);
96     if (index < m_values.size())
97         m_values.erase(m_values.begin() + index);
98 }
99
100 const vector<string>& BinaryAttribute::getSerializedValues() const
101 {
102     xsecsize_t len;
103     XMLByte *pos, *pos2;
104     if (m_serialized.empty()) {
105         for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
106             XMLByte* enc = Base64::encode(reinterpret_cast<const XMLByte*>(i->data()), i->size(), &len);
107             if (enc) {
108                 for (pos=enc, pos2=enc; *pos2; pos2++)
109                     if (isgraph(*pos2))
110                         *pos++=*pos2;
111                 *pos=0;
112                 m_serialized.push_back(reinterpret_cast<char*>(enc));
113 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
114                 XMLString::release(&enc);
115 #else
116                 XMLString::release((char**)&enc);
117 #endif
118             }
119         }
120     }
121     return Attribute::getSerializedValues();
122 }
123
124 DDF BinaryAttribute::marshall() const
125 {
126     DDF ddf = Attribute::marshall();
127     ddf.name("Binary");
128     DDF vlist = ddf.first();
129     const vector<string>& encoded = getSerializedValues();
130     for (vector<string>::const_iterator i = encoded.begin(); i != encoded.end(); ++i) {
131         DDF val = DDF(nullptr).string(i->c_str());
132         vlist.add(val);
133     }
134     return ddf;
135 }