blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00039 #ifndef BLOCXX_BinarySerialization_HPP_ 00040 #define BLOCXX_BinarySerialization_HPP_ 00041 #include "blocxx/BLOCXX_config.h" 00042 #include "blocxx/Types.hpp" 00043 #include "blocxx/Bool.hpp" 00044 #include "blocxx/String.hpp" 00045 #include "blocxx/Array.hpp" 00046 #include "blocxx/ByteSwap.hpp" 00047 #include "IOException.hpp" 00048 00049 #include <iosfwd> 00050 00051 // The classes and functions defined in this file are not meant for general 00052 // use, they are internal implementation details. They may change at any time. 00053 00054 namespace BLOCXX_NAMESPACE 00055 { 00056 00057 namespace BinarySerialization 00058 { 00059 00060 // This should generally be kept in sync with the repository version in BLOCXX_HDBCommon.hpp 00061 // The general idea is to have it be a concatenation of release numbers with 00062 // a revision on the end (to prevent problems during development) 00063 // So 3000003 originated from version 3.0.0 rev 4 00064 // 00065 // 10/25/2003 - 3000005. Changed enumClassNames to send over an enum of strings 00066 // instead of object paths. 00067 // 7/22/2004 - 3000008. Changed signatures and added versioning. Now all 00068 // readObject() calls will be able to read older versions as well as the 00069 // current. Introduced MinBinaryProtocolVersion which is the oldest version 00070 // we can sucessfully read. 00071 // 9/01/2005 - only HDB version to 4000000. Changed key format to use : instead of / to fix a bug. 00072 // 10/12/2005 - only HDB version to 4000001. Fixed association and instance key format wrt associations. 00073 const UInt32 BinaryProtocolVersion = 4000002; 00074 00075 // This is the oldest version the code can handle. 00076 const UInt32 MinBinaryProtocolVersion = 3000007; 00077 00078 // These values are all used by the binary protocol 00079 const UInt8 BIN_OK = 0; // Success returned from server 00080 const UInt8 BIN_ERROR = 1; // Error returned from server 00081 const UInt8 BIN_EXCEPTION = 2; // CIM Exception returned from server 00082 const UInt8 BIN_END = 3; // Final sentinel 00083 00084 const UInt8 BIN_LOG_MESSAGE = 45; // log message 00085 00086 00087 const UInt8 BINSIG_BOOL = 104; 00088 const UInt8 BINSIG_STR = 106; 00089 const UInt8 BINSIG_STRARRAY = 107; 00090 00091 const UInt8 BINSIG_STRINGENUM = 115; 00092 00093 const UInt8 END_STRINGENUM = 154; 00094 00095 00096 BLOCXX_COMMON_API void verifySignature(std::streambuf & istrm, UInt8 validSig); 00097 00099 template <typename Handler, typename ReaderFunc> 00100 inline void readEnum( 00101 std::streambuf & istrm, Handler & result, 00102 const ReaderFunc & read, const UInt8 beginsig, const UInt8 endsig) 00103 { 00104 verifySignature(istrm, beginsig); 00105 bool done = false; 00106 while (!done) 00107 { 00108 try 00109 { 00110 result.handle(read(istrm)); 00111 } 00112 catch (const BadSignatureException& e) 00113 { 00114 // read threw because we've read all the objects 00115 verifySignature(istrm, endsig); 00116 done = true; 00117 } 00118 } 00119 } 00121 BLOCXX_COMMON_API void write( 00122 std::streambuf & ostrm, const void * dataOut, size_t dataOutLen 00123 ); 00124 00125 inline void write(std::streambuf & ostrm, Int32 val) 00126 { 00127 val = hton32(val); 00128 BinarySerialization::write(ostrm, &val, sizeof(val)); 00129 } 00130 00131 inline void write(std::streambuf & ostrm, UInt32 val) 00132 { 00133 val = hton32(val); 00134 BinarySerialization::write(ostrm, &val, sizeof(val)); 00135 } 00136 00137 BLOCXX_COMMON_API void writeLen(std::streambuf & ostrm, UInt32 len); 00138 00139 inline void write(std::streambuf & ostrm, UInt8 val) 00140 { 00141 BinarySerialization::write(ostrm, &val, sizeof(val)); 00142 } 00143 00144 inline void write(std::streambuf & ostrm, UInt16 val) 00145 { 00146 val = hton16(val); 00147 BinarySerialization::write(ostrm, &val, sizeof(val)); 00148 } 00149 00150 inline void write(std::streambuf & ostrm, Int16 val) 00151 { 00152 val = hton16(val); 00153 BinarySerialization::write(ostrm, &val, sizeof(val)); 00154 } 00155 00156 inline void write(std::streambuf & ostrm, UInt64 val) 00157 { 00158 val = hton64(val); 00159 BinarySerialization::write(ostrm, &val, sizeof(val)); 00160 } 00161 00162 inline void write(std::streambuf & ostrm, Int64 val) 00163 { 00164 val = hton64(val); 00165 BinarySerialization::write(ostrm, &val, sizeof(val)); 00166 } 00167 00168 inline void write(std::streambuf & ostrm, const String & str) 00169 { 00170 str.writeObject(ostrm); 00171 } 00172 00173 inline void writeBool(std::streambuf & ostrm, Bool arg) 00174 { 00175 BinarySerialization::write(ostrm, BINSIG_BOOL); 00176 arg.writeObject(ostrm); 00177 } 00178 00179 inline void writeString(std::streambuf & ostrm, const String & str) 00180 { 00181 BinarySerialization::write(ostrm, BINSIG_STR); 00182 str.writeObject(ostrm); 00183 } 00184 00185 BLOCXX_COMMON_API void readLen(std::streambuf & istrm, UInt32 & len); 00186 00188 template <typename T> 00189 inline void 00190 readArray(std::streambuf & istr, T & a) 00191 { 00192 a.clear(); 00193 UInt32 len; 00194 BinarySerialization::readLen(istr, len); 00195 00196 a.reserve(len); 00197 for (UInt32 i = 0; i < len; i++) 00198 { 00199 typename T::value_type x; 00200 x.readObject(istr); 00201 a.push_back(x); 00202 } 00203 } 00204 00206 template <typename T> 00207 inline void 00208 writeArray(std::streambuf & ostrm, const T & a) 00209 { 00210 UInt32 len = static_cast<UInt32>(a.size()); 00211 BinarySerialization::writeLen(ostrm, len); 00212 for (UInt32 i = 0; i < len; i++) 00213 { 00214 a.operator[](i).writeObject(ostrm); 00215 } 00216 } 00217 00218 inline void writeStringArray( 00219 std::streambuf & ostrm, const StringArray & stra 00220 ) 00221 { 00222 BinarySerialization::write(ostrm, BINSIG_STRARRAY); 00223 writeArray(ostrm, stra); 00224 } 00225 00226 BLOCXX_COMMON_API void writeStringArray( 00227 std::streambuf & ostrm, const StringArray * propertyList 00228 ); 00229 00230 00231 BLOCXX_COMMON_API void read( 00232 std::streambuf & istrm, void * dataIn, size_t dataInLen 00233 ); 00234 00235 inline void read(std::streambuf & istrm, String & arg) 00236 { 00237 arg.readObject(istrm); 00238 } 00239 00240 inline void read(std::streambuf & istrm, UInt64 & val) 00241 { 00242 BinarySerialization::read(istrm, &val, sizeof(val)); 00243 val = ntoh64(val); 00244 } 00245 00246 inline void read(std::streambuf & istrm, Int64 & val) 00247 { 00248 BinarySerialization::read(istrm, &val, sizeof(val)); 00249 val = ntoh64(val); 00250 } 00251 00252 inline void read(std::streambuf & istrm, Int32 & val) 00253 { 00254 BinarySerialization::read(istrm, &val, sizeof(val)); 00255 val = ntoh32(val); 00256 } 00257 00258 inline void read(std::streambuf & istrm, UInt32 & val) 00259 { 00260 BinarySerialization::read(istrm, &val, sizeof(val)); 00261 val = ntoh32(val); 00262 } 00263 00264 inline void read(std::streambuf & istrm, UInt16 & val) 00265 { 00266 BinarySerialization::read(istrm, &val, sizeof(val)); 00267 val = ntoh16(val); 00268 } 00269 00270 inline void read(std::streambuf & istrm, Int16 & val) 00271 { 00272 BinarySerialization::read(istrm, &val, sizeof(val)); 00273 val = ntoh16(val); 00274 } 00275 00276 inline void read(std::streambuf & istrm, UInt8 & val) 00277 { 00278 BinarySerialization::read(istrm, &val, sizeof(val)); 00279 } 00280 00281 inline Bool readBool(std::streambuf & istrm) 00282 { 00283 BinarySerialization::verifySignature(istrm, BINSIG_BOOL); 00284 Bool b; 00285 b.readObject(istrm); 00286 return b; 00287 } 00288 00289 inline String readString(std::streambuf & istrm) 00290 { 00291 BinarySerialization::verifySignature(istrm, BINSIG_STR); 00292 String rv; 00293 rv.readObject(istrm); 00294 return rv; 00295 } 00296 00297 inline StringArray readStringArray(std::streambuf & istrm) 00298 { 00299 BinarySerialization::verifySignature(istrm, BINSIG_STRARRAY); 00300 StringArray stra; 00301 readArray(istrm, stra); 00302 return stra; 00303 } 00304 00305 } // end namespace BinarySerialization 00306 00307 } // end namespace BLOCXX_NAMESPACE 00308 00309 #endif // BLOCXX_BinarySerialization_HPP_