2 * Copyright (c) 2014, 2015 JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "GSSBuffer.h"
38 #include "../GSSException.h"
41 /* Constructor pattern:
42 * 1) Set gssInternal to false, so that setting values is permitted.
43 * 2) Allocate memory for a new gss_buffer_desc
44 * 3) Set the buffer length to 0, so that setting values doesn't try
46 * 4) Set the value of the buffer.
47 * 5) Set the value of gssInternal correctly
49 GSSBuffer::GSSBuffer ( const GSSBuffer &other )
51 gss_buffer_t otherBuf = other.buf;
53 setValue( (char *)(otherBuf->value), otherBuf->length);
54 this->gssInternal = false;
57 GSSBuffer::GSSBuffer(std::string str, bool gssInternal)
61 this->gssInternal = gssInternal;
64 GSSBuffer::GSSBuffer(char *str, bool gssInternal)
68 this->gssInternal = gssInternal;
71 GSSBuffer::GSSBuffer(char *str, int len, bool gssInternal)
75 this->gssInternal = gssInternal;
78 GSSBuffer::GSSBuffer(gss_buffer_t gssbuf, bool gssInternal)
82 this->gssInternal = gssInternal;
85 GSSBuffer::~GSSBuffer(void)
87 OM_uint32 major, minor;
89 if (this->gssInternal)
91 major = gss_release_buffer(&minor, buf);
94 throw GSSException("Error in releasing a buffer allocated by GSS", major, minor);
102 void GSSBuffer::initEmpty()
104 this->gssInternal = false;
105 this->buf = new gss_buffer_desc;
106 this->buf->length = 0;
107 this->buf->value = NULL;
110 void GSSBuffer::freeBufValue()
112 if (this->buf->length == 0 && this->buf->value != NULL)
113 delete[] ( (char *)(this->buf->value) );
114 this->buf->length = 0;
115 this->buf->value = NULL;
120 * 1) Throw an exception when attempting to mutate a buffer supplied by
122 * 2) Free the GSS buffer value, if needed.
123 * 3) Set the buffer value as needed.
125 void GSSBuffer::setValue(std::string str)
127 setValue((char *)str.c_str(), str.size());
130 void GSSBuffer::setValue(char *str)
132 setValue(str, std::strlen(str));
135 void GSSBuffer::setValue(char *str, size_t len)
139 if (this->gssInternal)
140 throw std::logic_error("Attempting to modify an immutable GSSBuffer");
146 char *value = new char[len + 1];
147 std::memcpy( (void *)(value), (void *)str, len);
150 this->buf->value = value;
151 this->buf->length = len;
157 void GSSBuffer::setValue(gss_buffer_t gssbuf)
159 if (this->gssInternal)
160 throw std::logic_error("Attempting to modify an immutable GSSBuffer");