2
* Copyright 2002-2005 The Apache Software Foundation.
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
* XSECCanon := Base (virtual) class for canonicalisation objects
22
* Author(s): Berin Lautenbach
30
#include <xsec/canon/XSECCanon.hpp>
31
#include <xsec/utils/XSECDOMUtils.hpp>
35
XERCES_CPP_NAMESPACE_USE
37
// --------------------------------------------------------------------------------
38
// XSECCanon Virtual Class implementation
39
// --------------------------------------------------------------------------------
44
XSECCanon::XSECCanon() {};
46
XSECCanon::XSECCanon(DOMDocument *newDoc) : m_buffer() {
50
mp_startNode = mp_nextNode = newDoc; // By default, start from startNode
51
m_bufferLength = m_bufferPoint = 0; // Start with an empty buffer
52
m_allNodesDone = false;
56
XSECCanon::XSECCanon(DOMDocument *newDoc, DOMNode *newStartNode) {
59
mp_startNode = mp_nextNode = newStartNode;
60
m_bufferLength = m_bufferPoint = 0; // Start with an empty buffer
61
m_allNodesDone = false;
67
XSECCanon::~XSECCanon() {};
71
int XSECCanon::outputBuffer(unsigned char *outBuffer, int numBytes) {
73
// numBytes of data are required to be placed in outBuffer.
75
// Calculate amount left in buffer
77
int remaining = m_bufferLength - m_bufferPoint;
78
int bytesToGo = numBytes;
79
int i = 0; // current point in outBuffer
82
// While we don't have enough, and have not completed -
84
while (!m_allNodesDone && (remaining < bytesToGo)) {
86
// Copy what we have and get some more in the buffer
87
memcpy(&outBuffer[i], &m_buffer[m_bufferPoint], remaining);
89
m_bufferPoint += remaining;
90
bytesToGo -= remaining;
96
remaining = m_bufferLength - m_bufferPoint; // This will be reset by processNextElement
97
// "-bufferPoint" is just in case.
101
if (m_allNodesDone && (remaining < bytesToGo)) {
103
// Was not enough data to fill everything up
104
memcpy (&outBuffer[i], &m_buffer[m_bufferPoint], remaining);
105
m_bufferPoint += remaining;
106
return i + remaining;
109
// Copy the tail of the buffer
111
memcpy(&outBuffer[i], &m_buffer[m_bufferPoint], bytesToGo);
112
m_bufferPoint += bytesToGo;
113
return (bytesToGo + i);
117
// setStartNode sets the starting point for the output if it is a sub-document
118
// that needs canonicalisation and we want to re-start
120
bool XSECCanon::setStartNode(DOMNode *newStartNode) {
122
mp_startNode = newStartNode;
125
mp_nextNode = mp_startNode;
127
m_allNodesDone = false; // Restart
129
return true; // Should check to ensure that the StartNode is part of the doc.