00001 /* 00002 * WOscLib, an object oriented OSC library. 00003 * Copyright (C) 2005 Uli Clemens Franke, Weiss Engineering LTD, Switzerland. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 * 00019 * For details see lgpl.txt 00020 * 00021 * Weiss Engineering LTD. 00022 * Florastrass 42 00023 * 8610 Uster 00024 * Switzerland 00025 * 00026 * uli.franke@weiss.ch 00027 */ 00028 00029 /** WOscBlob source file. 00030 * \file 00031 * 00032 * $Author: cls-nebadje $ ( \ref _UcfWOscLib ) 00033 * $Date: 2006-08-10 15:12:12 $ 00034 * $Revision: 1.4 $ 00035 * 00036 * Copyright (c) Weiss Engineering Ltd 00037 * 00038 */ 00039 #include "WOscBlob.h" 00040 #include "WOscString.h" 00041 #include "WOscException.h" 00042 #include "WOscUtil.h" 00043 #include <string.h> 00044 00045 00046 /** Constructor for WOscBlobs from a binary buffer containing an WOscBlob 00047 * in its binary representation ( [blobsize][blobdata] ). 00048 * 00049 * \param binaryData 00050 * Pointer to the buffer containing the raw data. 00051 * 00052 * \remarks 00053 * Does not handle illegal blob sizes when passing wrong buffers. 00054 * 00055 */ 00056 WOscBlob::WOscBlob(const char* binaryData) 00057 { 00058 // get blob size 00059 m_dataLen = WOscUtil::BufferTo<int>(binaryData); 00060 m_bufferLen = WOscUtil::GetSizeFourByteAligned( m_dataLen ) + BLOB_SIZE_SIZE; 00061 m_buffer = new char[m_bufferLen] ; 00062 00063 // write data to buffer 00064 memcpy(m_buffer, binaryData, m_bufferLen); 00065 } 00066 00067 /** Returns a pointer to the blob data. Get the length of the buffer with 00068 * WOscBlob::GetDataLength() . 00069 * 00070 * \returns 00071 * Pointer to the buffer containing the data. 00072 */ 00073 const char* WOscBlob::GetData() const 00074 { 00075 return m_buffer + BLOB_SIZE_SIZE; 00076 } 00077 00078 00079 /** Constructs a WOscBlob-object from a binary buffer. 00080 * The buffer is internally rounded up to a multiple of 4. 00081 * If querying fo buffer size, the padded size is returned. 00082 * To get the actual data length, use the GetDataLen() function. 00083 * 00084 * \param binaryData 00085 * Buffer with data of length "dataLen". 00086 * 00087 * \param dataLen 00088 * Length of data supplied with "binaryData". 00089 * 00090 * \throws None. 00091 * 00092 * \remarks 00093 * Check general information at WOscBlob . 00094 * 00095 * 00096 * \verbatim 00097 00098 +-------------------+----//---------------------+ 00099 | size of blob data | blob data + padding zeros | 00100 +-------------------+---------------------------+ 00101 00102 |----- 4 bytes -----|-- size rounded up bytes --| 00103 00104 \endverbatim 00105 * 00106 */ 00107 WOscBlob::WOscBlob(const char* binaryData, int dataLen) 00108 { 00109 m_dataLen = dataLen; 00110 00111 /* round up and allocate */ 00112 m_bufferLen = WOscUtil::GetSizeFourByteAligned(dataLen) + BLOB_SIZE_SIZE; 00113 m_buffer = new char[m_bufferLen]; 00114 00115 /* write size to buffer */ 00116 WOscUtil::FillBufferWith(m_buffer, dataLen); 00117 00118 /* write data to buffer */ 00119 memcpy(m_buffer + BLOB_SIZE_SIZE, binaryData, m_dataLen); 00120 00121 /* zeropad buffer */ 00122 for ( int i = m_dataLen + BLOB_SIZE_SIZE; i < m_bufferLen; i++ ) 00123 m_buffer[i] = 0; 00124 } 00125 00126 /** Assignment operator. 00127 * Copies the content of the referenced blob into this one. 00128 * 00129 * \param rhs 00130 * Reference to another blob (right hand side of operator). 00131 * 00132 * \throws None. 00133 * 00134 * \remarks 00135 * None. 00136 */ 00137 WOscBlob& WOscBlob::operator= (WOscBlob &rhs) 00138 { 00139 if ( this != &rhs ) { 00140 delete [] m_buffer; 00141 m_dataLen = rhs.m_dataLen; 00142 m_bufferLen = rhs.m_bufferLen; 00143 m_buffer = new char[m_bufferLen]; 00144 memcpy(m_buffer, rhs.m_buffer, m_bufferLen); 00145 } 00146 return *this; 00147 } 00148 00149 /** Pointer-copy-constructor. 00150 * Copies the content of the referenced blob into this one. 00151 * 00152 * \param b 00153 * Reference to another blob. 00154 * 00155 * \throws None. 00156 * 00157 * \remarks 00158 * None. 00159 */ 00160 WOscBlob::WOscBlob(WOscBlob* b) 00161 { 00162 m_buffer = NULL; 00163 *this = *b; 00164 } 00165 00166 /** Copy-constructor. 00167 * Copies the content of the referenced blob into this one. 00168 * 00169 * \param b 00170 * Reference to another blob. 00171 * 00172 * \throws None. 00173 * 00174 * \remarks 00175 * None. 00176 */ 00177 WOscBlob::WOscBlob(WOscBlob& b) 00178 { 00179 m_buffer = NULL; 00180 *this = b; 00181 } 00182 00183 /** Deletes the WOscBlob-object. 00184 * The internal buffer gets deleted. 00185 * 00186 * \throws None. 00187 * 00188 * \remarks 00189 * None. 00190 */ 00191 WOscBlob::~WOscBlob() 00192 { 00193 delete [] m_buffer; 00194 } 00195 00196 /** Returns the length of the data without zero-padding. 00197 * 00198 * \returns 00199 * Size without zeros. 00200 * 00201 * \see 00202 * GetBufferLen() 00203 */ 00204 int 00205 WOscBlob::GetDataLen() const 00206 { 00207 return m_dataLen; 00208 } 00209 00210 /** Returns the buffer-length of the formatted buffer. 00211 * This length includes zero-padding and size of the blob size. 00212 * 00213 * \returns 00214 * Size of the formatted buffer. 00215 * 00216 * \see 00217 * GetDataLen() 00218 */ 00219 int 00220 WOscBlob::GetBufferLen() const 00221 { 00222 return m_bufferLen; 00223 } 00224 00225 /** Fills the buffer with the raw bytestream of this blob. 00226 * The user must supply the buffer of apropriate size. 00227 * 00228 * \param buffer 00229 * Caller supplied destination buffer. 00230 * 00231 * \param bufferLen 00232 * Length of the supplied buffer. 00233 * 00234 * \throws WOscException 00235 * When caller supplied buffer too small. 00236 * 00237 * \remarks 00238 * This interface makes sure that the buffer remains valid 00239 * when modifying the blob-object afterwards. (contrary to 00240 * WOscBlob::getBuffer() ) 00241 * 00242 * \see 00243 * WOscBlob::getBuffer() 00244 */ 00245 void 00246 WOscBlob::GetBuffer(char* buffer, int bufferLen) const 00247 { 00248 if ( bufferLen < m_bufferLen ) 00249 throw WOscException(ERR_BUFFER_TO_SMALL, __FILE__, __LINE__); 00250 memcpy(buffer, m_buffer, m_bufferLen); 00251 } 00252 00253 /** Returns a pointer to an internal buffer containing 00254 * the current binary representation of this blob. 00255 * Please see remarks for safety issues. 00256 * 00257 * \returns 00258 * Pointer to internal array data. 00259 * 00260 * \throws None. 00261 * 00262 * \remarks 00263 * The pointer remains valid until the modification of this blob-object. 00264 * The next time the blob is destination of an operation (check operators) or 00265 * the blob will be deleted this pointer gets invalid. If you are 00266 * not sure about that issue, please use the other getBuffer()- function. 00267 * 00268 * \see 00269 * WOscBlob::getBuffer(char* buffer, int bufferLen) 00270 */ 00271 const char* WOscBlob::GetBuffer() const 00272 { 00273 return m_buffer; 00274 }