fix int to size_t warnings in GSSBuffer
[gssweb.git] / json_gssapi / src / datamodel / GSSBuffer.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  * 
4  * For license details, see the LICENSE file in the root of this project.
5  * 
6  */
7
8 #include <stdexcept>
9 #include <cstring>
10 #include "GSSBuffer.h"
11 #include "../GSSException.h"
12
13
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
18  *    to free memory.
19  * 4) Set the value of the buffer.
20  * 5) Set the value of gssInternal correctly
21  */
22 GSSBuffer::GSSBuffer ( const GSSBuffer &other )
23 {
24   gss_buffer_t otherBuf = other.buf;
25   initEmpty();
26   setValue( (char *)(otherBuf->value), otherBuf->length);
27   this->gssInternal = false;
28 }
29
30 GSSBuffer::GSSBuffer(std::string str, bool gssInternal)
31 {
32   initEmpty();
33   setValue(str);
34   this->gssInternal = gssInternal;
35 }
36
37 GSSBuffer::GSSBuffer(char *str, bool gssInternal)
38 {
39   initEmpty();
40   setValue(str);
41   this->gssInternal = gssInternal;
42 }
43
44 GSSBuffer::GSSBuffer(char *str, int len, bool gssInternal)
45 {
46   initEmpty();
47   setValue(str, len);
48   this->gssInternal = gssInternal;
49 }
50
51 GSSBuffer::GSSBuffer(gss_buffer_t gssbuf, bool gssInternal)
52 {
53   initEmpty();
54   setValue(gssbuf);
55   this->gssInternal = gssInternal;
56 }
57
58 GSSBuffer::~GSSBuffer(void)
59 {
60   OM_uint32 major, minor;
61
62   if (this->gssInternal) 
63   {
64     major = gss_release_buffer(&minor, buf);
65     if (GSS_ERROR(major))
66     {
67       throw GSSException("Error in releasing a buffer allocated by GSS", major, minor);
68     }
69   } else {
70     freeBufValue();
71   }
72 }
73
74
75 void GSSBuffer::initEmpty()
76 {
77   this->gssInternal = false;
78   this->buf = new gss_buffer_desc;
79   this->buf->length = 0;
80   this->buf->value = NULL;
81 }
82
83 void GSSBuffer::freeBufValue()
84 {
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;
89 }
90
91
92 /* setValue pattern:
93  * 1) Throw an exception when attempting to mutate a buffer supplied by 
94  *    GSS itself. 
95  * 2) Free the GSS buffer value, if needed.
96  * 3) Set the buffer value as needed.
97  */
98 void GSSBuffer::setValue(std::string str)
99 {
100   setValue((char *)str.c_str(), str.size());
101 }
102
103 void GSSBuffer::setValue(char *str)
104 {
105   setValue(str, std::strlen(str));
106 }
107
108 void GSSBuffer::setValue(char *str, size_t len)
109 {
110   /* Variables */
111   /* Error checking */
112   if (this->gssInternal)
113     throw std::logic_error("Attempting to modify an immutable GSSBuffer");
114   
115   /* Setup */
116   freeBufValue();
117   
118   /* Main */
119   char *value = new char[len + 1];
120   std::memcpy( (void *)(value), (void *)str, len);
121   value[len] = 0;
122
123   this->buf->value = value;
124   this->buf->length = len;
125   
126   /* Cleanup */
127   /* Return */
128 }
129
130 void GSSBuffer::setValue(gss_buffer_t gssbuf)
131 {
132   if (this->gssInternal)
133     throw std::logic_error("Attempting to modify an immutable GSSBuffer");
134   freeBufValue();
135   delete buf;
136   this->buf = gssbuf;
137 }