Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
buffer.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 
19 #include "commonincludes.hpp"
20 #include "buffer.h"
21 
22 
23 
25 _data(NULL),
26 _size(0),
27 _allocatedSize(0)
28 {
29  ;
30 }
31 
33 {
34  _spAllocation.reset();
35  _data = NULL;
36  _size = 0;
37  _allocatedSize = 0;
38 
39 }
40 
41 
42 CBuffer::CBuffer(size_t nSize)
43 {
44  InitWithAllocation(nSize);
45 }
46 
47 
49 {
50 
51  Reset();
52 
53  // deliberately not checking for 0. Ok to allocate a 0 byte array
54  boost::scoped_array<uint8_t> spAlloc(new uint8_t[size+2]); // add two bytes for null termination (makes debugging ascii and unicode strings easier), but these two bytes are invisible to the caller (not included in _allocatedSize)
55 
56  _spAllocation.swap(spAlloc);
57 
58  spAlloc.reset();
59 
60  _data = _spAllocation.get();
61 
62  if (_data)
63  {
64  _data[size] = 0;
65  _data[size+1] = 0;
66  }
67 
68  _size = (_data != NULL) ? size : 0;
70 
71  return (_data != NULL) ? S_OK : E_FAIL;
72 }
73 
74 HRESULT CBuffer::InitNoAlloc(uint8_t* pByteArray, size_t size)
75 {
76  Reset();
77 
78  if (pByteArray == NULL)
79  {
80  size = 0;
81  }
82 
83  _data = pByteArray;
84  _size = size;
85  _allocatedSize = size;
86  return S_OK;
87 }
88 
89 HRESULT CBuffer::InitWithAllocAndCopy(uint8_t* pByteArray, size_t size)
90 {
91  HRESULT hr = S_OK;
92  Reset();
93 
94  if (pByteArray == NULL)
95  {
96  size = 0;
97  }
98 
99  hr = InitWithAllocation(size);
100 
101  if (SUCCEEDED(hr))
102  {
103  memcpy(_data, pByteArray, _size);
104  }
105 
106  return hr;
107 }
108 
109 
110 CBuffer::CBuffer(uint8_t* pByteArray, size_t nByteArraySize, bool fCopy)
111 {
112 
113  if (fCopy == false)
114  {
115  InitNoAlloc(pByteArray, nByteArraySize);
116  }
117  else
118  {
119  InitWithAllocAndCopy(pByteArray, nByteArraySize);
120  }
121 }
122 
123 
124 
125 
127 {
128  HRESULT hr = S_OK;
129 
130  hr = (size <= _allocatedSize);
131 
132  if (SUCCEEDED(hr))
133  {
134  _size = size;
135  }
136 
137  return hr;
138 }
139 
140 
141 
142 
144 {
145  return (_data != NULL);
146 }
147 
148 
149 
#define S_OK
Definition: hresult.h:46
HRESULT InitWithAllocation(size_t size)
Definition: buffer.cpp:48
bool IsValid()
Definition: buffer.cpp:143
CBuffer()
Definition: buffer.cpp:24
uint8_t * _data
Definition: buffer.h:30
HRESULT InitWithAllocAndCopy(uint8_t *pByteArray, size_t nByteArraySize)
Definition: buffer.cpp:89
size_t _size
Definition: buffer.h:31
#define SUCCEEDED(hr)
Definition: hresult.h:28
int32_t HRESULT
Definition: hresult.h:22
HRESULT InitNoAlloc(uint8_t *pByteArray, size_t nByteArraySize)
Definition: buffer.cpp:74
HRESULT SetSize(size_t size)
Definition: buffer.cpp:126
void Reset()
Definition: buffer.cpp:32
#define E_FAIL
Definition: hresult.h:56
size_t _allocatedSize
Definition: buffer.h:32
boost::scoped_array< uint8_t > _spAllocation
Definition: buffer.h:33