Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / src / datamodel / GSSBuffer.cpp
1 /*
2  * Copyright (c) 2014, 2015 JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdexcept>
36 #include <cstring>
37 #include "GSSBuffer.h"
38 #include "../GSSException.h"
39
40
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
45  *    to free memory.
46  * 4) Set the value of the buffer.
47  * 5) Set the value of gssInternal correctly
48  */
49 GSSBuffer::GSSBuffer ( const GSSBuffer &other )
50 {
51   gss_buffer_t otherBuf = other.buf;
52   initEmpty();
53   setValue( (char *)(otherBuf->value), otherBuf->length);
54   this->gssInternal = false;
55 }
56
57 GSSBuffer::GSSBuffer(std::string str, bool gssInternal)
58 {
59   initEmpty();
60   setValue(str);
61   this->gssInternal = gssInternal;
62 }
63
64 GSSBuffer::GSSBuffer(char *str, bool gssInternal)
65 {
66   initEmpty();
67   setValue(str);
68   this->gssInternal = gssInternal;
69 }
70
71 GSSBuffer::GSSBuffer(char *str, int len, bool gssInternal)
72 {
73   initEmpty();
74   setValue(str, len);
75   this->gssInternal = gssInternal;
76 }
77
78 GSSBuffer::GSSBuffer(gss_buffer_t gssbuf, bool gssInternal)
79 {
80   initEmpty();
81   setValue(gssbuf);
82   this->gssInternal = gssInternal;
83 }
84
85 GSSBuffer::~GSSBuffer(void)
86 {
87   OM_uint32 major, minor;
88
89   if (this->gssInternal) 
90   {
91     major = gss_release_buffer(&minor, buf);
92     if (GSS_ERROR(major))
93     {
94       throw GSSException("Error in releasing a buffer allocated by GSS", major, minor);
95     }
96   } else {
97     freeBufValue();
98   }
99 }
100
101
102 void GSSBuffer::initEmpty()
103 {
104   this->gssInternal = false;
105   this->buf = new gss_buffer_desc;
106   this->buf->length = 0;
107   this->buf->value = NULL;
108 }
109
110 void GSSBuffer::freeBufValue()
111 {
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;
116 }
117
118
119 /* setValue pattern:
120  * 1) Throw an exception when attempting to mutate a buffer supplied by 
121  *    GSS itself. 
122  * 2) Free the GSS buffer value, if needed.
123  * 3) Set the buffer value as needed.
124  */
125 void GSSBuffer::setValue(std::string str)
126 {
127   setValue((char *)str.c_str(), str.size());
128 }
129
130 void GSSBuffer::setValue(char *str)
131 {
132   setValue(str, std::strlen(str));
133 }
134
135 void GSSBuffer::setValue(char *str, size_t len)
136 {
137   /* Variables */
138   /* Error checking */
139   if (this->gssInternal)
140     throw std::logic_error("Attempting to modify an immutable GSSBuffer");
141   
142   /* Setup */
143   freeBufValue();
144   
145   /* Main */
146   char *value = new char[len + 1];
147   std::memcpy( (void *)(value), (void *)str, len);
148   value[len] = 0;
149
150   this->buf->value = value;
151   this->buf->length = len;
152   
153   /* Cleanup */
154   /* Return */
155 }
156
157 void GSSBuffer::setValue(gss_buffer_t gssbuf)
158 {
159   if (this->gssInternal)
160     throw std::logic_error("Attempting to modify an immutable GSSBuffer");
161   freeBufValue();
162   delete buf;
163   this->buf = gssbuf;
164 }