1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*
21  * XSEC
22  *
23  * XSECSafeBuffer := a class for storing expanding amounts of information.
24  *
25  * Author(s): Berin Lautenbach
26  *
27  * $Id: XSECSafeBuffer.hpp 1808174 2017-09-12 21:50:30Z scantor $
28  *
29  */
30 
31 
32 #ifndef XSECSAFEBUFFER_INCLUDE
33 #define XSECSAFEBUFFER_INCLUDE
34 
35 #include <xsec/framework/XSECDefs.hpp>
36 #include <xercesc/util/XMLString.hpp>
37 
38 class TXFMBase;
39 
40 /**
41  * \addtogroup internal
42  * @{
43  */
44 
45 
46 #define DEFAULT_SAFE_BUFFER_SIZE		1024		// Default size for a safe Buffer
47 
48  /**
49  *\brief Manage buffers of arbitrary size
50  *
51  * The safeBuffer class is used internally in the library
52  * to manage buffers of bytes or UTF-16 characters.
53  *
54  * It's a fairly innefficient class, as buffers are continually
55  * being wrapped, coppied and enlarged, but given the nature of the
56  * library, a single class that manipulates buffers of variable
57  * size was felt to be preferable,
58  *
59  * The safeBuffer is not exposed through interface classes that
60  * might be used by external functions.  In these cases, a
61  * pointer to a XMLCh * buffer is used by preference.
62  */
63 
64 class XSEC_EXPORT safeBuffer {
65 
66 public:
67 
68 	// For checking we are operating on the buffer correctly
69 	enum bufferType {
70 
71 		BUFFER_UNKNOWN		= 0,
72 		BUFFER_CHAR			= 1,
73 		BUFFER_UNICODE		= 2
74 	};
75 
76 
77 	safeBuffer();
78     safeBuffer(const safeBuffer & other);
79 	safeBuffer(XMLSize_t initialSize);
80 	safeBuffer(const char * inStr, XMLSize_t initialSize = DEFAULT_SAFE_BUFFER_SIZE);
81 	~safeBuffer();
82 
83 	static void init(void);
84 
85 	// "IN" functions - these read in information to the buffer
86 
87 	void sbStrcpyIn(const char * inStr);
88 	void sbStrcpyIn(const safeBuffer & inStr);
89     void sbStrncpyIn(const char * inStr, XMLSize_t n);
90     void sbStrncpyIn(const safeBuffer & inStr, XMLSize_t n);
91 
92 	void sbStrcatIn(const char * inStr);
93 	void sbStrcatIn(const safeBuffer & inStr);
94     void sbStrncatIn(const char * inStr, XMLSize_t n);
95     void sbStrinsIn(const char * inStr, XMLSize_t offset);
96 
97     void sbMemcpyIn(const void * inBuf, XMLSize_t n);
98     void sbMemcpyIn(XMLSize_t offset, const void * inBuf, XMLSize_t n);
99 
100     void sbMemcpyOut(void * outBuf, XMLSize_t n) const;
101     void sbMemshift(XMLSize_t toOffset, XMLSize_t fromOffset, XMLSize_t len);
102 
103 	// Comparison functions
104 
105     int sbStrncmp(const char * inStr, XMLSize_t n) const;
106     int sbOffsetStrcmp(const char * inStr, XMLSize_t offset) const;
107     int sbStrcmp(const char * inStr) const;
108     int sbStrcmp(const safeBuffer & inStr) const;
109 
110     XMLSSize_t sbStrstr(const char * inStr) const;
111     XMLSSize_t sbOffsetStrstr(const char * inStr, XMLSize_t offset) const;
112 
113 	// XMLCh and char common functions
114 	void sbStrlwr(void);		// Lowercase the string
115 
116 	// Operators
117 
118     unsigned char & operator[](XMLSize_t n);
119 	safeBuffer & operator= (const safeBuffer & cpy);
120 	safeBuffer & operator= (const XMLCh * inStr);
121 	safeBuffer & operator << (TXFMBase * t);
122 
123 	// Get functions
124 
125 	XMLSize_t sbStrlen(void) const;
126 	XMLSize_t sbRawBufferSize(void) const;
127 
128 	// raw buffer manipulation
129 
130 	const unsigned char * rawBuffer() const;
131 	const char * rawCharBuffer() const;
132 	const XMLCh * rawXMLChBuffer() const;
133     void resize(XMLSize_t sz);                 // NOTE : Only grows
134 	void setBufferType(bufferType bt);		    // Use with care
135 
136 	// Unicode (UTF-16 manipulation)
137 	const XMLCh * sbStrToXMLCh(void) const;		// Note does not affect internal buffer
138 	void sbTranscodeIn(const XMLCh * inStr);	// Create a local string from UTF-16
139 	void sbTranscodeIn(const char * inStr);		// Create a UTF-16 string from local
140 	void sbXMLChIn(const XMLCh * in);			// Buffer holds XMLCh *
141 	void sbXMLChAppendCh(const XMLCh c);		// Append a Unicode character to the buffer
142 	void sbXMLChCat(const XMLCh *str);			// Append a UTF-16 string to the buffer
143 	void sbXMLChCat(const char * str);			// Append a (transcoded) local string to the buffer
144 	void sbXMLChCat8(const char * str);			// Append a (transcoded) UTF-8 string to the buffer
145 
146 	// Sensitive data functions
147 	void isSensitive(void);
148 	void cleanseBuffer(void);
149 
150 private:
151 
152 	// Internal function that is used to get a string size and
153 	// then re-allocate if necessary
154 
155     void checkAndExpand(XMLSize_t size);
156 	void checkBufferType(bufferType bt) const;
157 
158 	unsigned char * buffer;
159 	XMLSize_t      bufferSize;
160 	mutable XMLCh   * mp_XMLCh;
161 	bufferType		m_bufferType;
162 
163 	// For XMLCh manipulation
164 	static size_t	size_XMLCh;
165 
166 	// For sensitive data
167 	bool			m_isSensitive;
168 };
169 
170 /** @} */
171 
172 #endif /* XSECSAFEBUFFER_INCLUDE */
173 
174