2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
10 #include "GSSBuffer.h"
11 #include "../GSSException.h"
14 /* Constructor pattern:
15 * 1) Set gssInternal to false, so that setting values is permitted.
16 * 2) Allocate memory for a new gss_buffer_desc
17 * 3) Set the buffer length to 0, so that setting values doesn't try
19 * 4) Set the value of the buffer.
20 * 5) Set the value of gssInternal correctly
22 GSSBuffer::GSSBuffer ( const GSSBuffer &other )
24 gss_buffer_t otherBuf = other.buf;
26 setValue( (char *)(otherBuf->value), otherBuf->length);
27 this->gssInternal = false;
30 GSSBuffer::GSSBuffer(std::string str, bool gssInternal)
34 this->gssInternal = gssInternal;
37 GSSBuffer::GSSBuffer(char *str, bool gssInternal)
41 this->gssInternal = gssInternal;
44 GSSBuffer::GSSBuffer(char *str, int len, bool gssInternal)
48 this->gssInternal = gssInternal;
51 GSSBuffer::GSSBuffer(gss_buffer_t gssbuf, bool gssInternal)
55 this->gssInternal = gssInternal;
58 GSSBuffer::~GSSBuffer(void)
60 OM_uint32 major, minor;
62 if (this->gssInternal)
64 major = gss_release_buffer(&minor, buf);
67 throw GSSException("Error in releasing a buffer allocated by GSS", major, minor);
75 void GSSBuffer::initEmpty()
77 this->gssInternal = false;
78 this->buf = new gss_buffer_desc;
79 this->buf->length = 0;
80 this->buf->value = NULL;
83 void GSSBuffer::freeBufValue()
85 if (this->buf->length == 0 && this->buf->value != NULL)
86 delete[] ( (char *)(this->buf->value) );
87 this->buf->length = 0;
88 this->buf->value = NULL;
93 * 1) Throw an exception when attempting to mutate a buffer supplied by
95 * 2) Free the GSS buffer value, if needed.
96 * 3) Set the buffer value as needed.
98 void GSSBuffer::setValue(std::string str)
100 setValue((char *)str.c_str(), str.size());
103 void GSSBuffer::setValue(char *str)
105 setValue(str, std::strlen(str));
108 void GSSBuffer::setValue(char *str, size_t len)
112 if (this->gssInternal)
113 throw std::logic_error("Attempting to modify an immutable GSSBuffer");
119 char *value = new char[len + 1];
120 std::memcpy( (void *)(value), (void *)str, len);
123 this->buf->value = value;
124 this->buf->length = len;
130 void GSSBuffer::setValue(gss_buffer_t gssbuf)
132 if (this->gssInternal)
133 throw std::logic_error("Attempting to modify an immutable GSSBuffer");