blocxx
BinarySerialization.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. 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 are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_BinarySerialization_HPP_
40 #define BLOCXX_BinarySerialization_HPP_
41 #include "blocxx/BLOCXX_config.h"
42 #include "blocxx/Types.hpp"
43 #include "blocxx/Bool.hpp"
44 #include "blocxx/String.hpp"
45 #include "blocxx/Array.hpp"
46 #include "blocxx/ByteSwap.hpp"
47 #include "IOException.hpp"
48 
49 #include <iosfwd>
50 
51 // The classes and functions defined in this file are not meant for general
52 // use, they are internal implementation details. They may change at any time.
53 
54 namespace BLOCXX_NAMESPACE
55 {
56 
57 namespace BinarySerialization
58 {
59 
60 // This should generally be kept in sync with the repository version in BLOCXX_HDBCommon.hpp
61 // The general idea is to have it be a concatenation of release numbers with
62 // a revision on the end (to prevent problems during development)
63 // So 3000003 originated from version 3.0.0 rev 4
64 //
65 // 10/25/2003 - 3000005. Changed enumClassNames to send over an enum of strings
66 // instead of object paths.
67 // 7/22/2004 - 3000008. Changed signatures and added versioning. Now all
68 // readObject() calls will be able to read older versions as well as the
69 // current. Introduced MinBinaryProtocolVersion which is the oldest version
70 // we can sucessfully read.
71 // 9/01/2005 - only HDB version to 4000000. Changed key format to use : instead of / to fix a bug.
72 // 10/12/2005 - only HDB version to 4000001. Fixed association and instance key format wrt associations.
73 const UInt32 BinaryProtocolVersion = 4000002;
74 
75 // This is the oldest version the code can handle.
76 const UInt32 MinBinaryProtocolVersion = 3000007;
77 
78 // These values are all used by the binary protocol
79 const UInt8 BIN_OK = 0; // Success returned from server
80 const UInt8 BIN_ERROR = 1; // Error returned from server
81 const UInt8 BIN_EXCEPTION = 2; // CIM Exception returned from server
82 const UInt8 BIN_END = 3; // Final sentinel
83 
84 const UInt8 BIN_LOG_MESSAGE = 45; // log message
85 
86 
87 const UInt8 BINSIG_BOOL = 104;
88 const UInt8 BINSIG_STR = 106;
89 const UInt8 BINSIG_STRARRAY = 107;
90 
92 
93 const UInt8 END_STRINGENUM = 154;
94 
95 
96  BLOCXX_COMMON_API void verifySignature(std::streambuf & istrm, UInt8 validSig);
97 
99  template <typename Handler, typename ReaderFunc>
100  inline void readEnum(
101  std::streambuf & istrm, Handler & result,
102  const ReaderFunc & read, const UInt8 beginsig, const UInt8 endsig)
103  {
104  verifySignature(istrm, beginsig);
105  bool done = false;
106  while (!done)
107  {
108  try
109  {
110  result.handle(read(istrm));
111  }
112  catch (const BadSignatureException& e)
113  {
114  // read threw because we've read all the objects
115  verifySignature(istrm, endsig);
116  done = true;
117  }
118  }
119  }
121  BLOCXX_COMMON_API void write(
122  std::streambuf & ostrm, const void * dataOut, size_t dataOutLen
123  );
124 
125  inline void write(std::streambuf & ostrm, Int32 val)
126  {
127  val = hton32(val);
128  BinarySerialization::write(ostrm, &val, sizeof(val));
129  }
130 
131  inline void write(std::streambuf & ostrm, UInt32 val)
132  {
133  val = hton32(val);
134  BinarySerialization::write(ostrm, &val, sizeof(val));
135  }
136 
137  BLOCXX_COMMON_API void writeLen(std::streambuf & ostrm, UInt32 len);
138 
139  inline void write(std::streambuf & ostrm, UInt8 val)
140  {
141  BinarySerialization::write(ostrm, &val, sizeof(val));
142  }
143 
144  inline void write(std::streambuf & ostrm, UInt16 val)
145  {
146  val = hton16(val);
147  BinarySerialization::write(ostrm, &val, sizeof(val));
148  }
149 
150  inline void write(std::streambuf & ostrm, Int16 val)
151  {
152  val = hton16(val);
153  BinarySerialization::write(ostrm, &val, sizeof(val));
154  }
155 
156  inline void write(std::streambuf & ostrm, UInt64 val)
157  {
158  val = hton64(val);
159  BinarySerialization::write(ostrm, &val, sizeof(val));
160  }
161 
162  inline void write(std::streambuf & ostrm, Int64 val)
163  {
164  val = hton64(val);
165  BinarySerialization::write(ostrm, &val, sizeof(val));
166  }
167 
168  inline void write(std::streambuf & ostrm, const String & str)
169  {
170  str.writeObject(ostrm);
171  }
172 
173  inline void writeBool(std::streambuf & ostrm, Bool arg)
174  {
176  arg.writeObject(ostrm);
177  }
178 
179  inline void writeString(std::streambuf & ostrm, const String & str)
180  {
182  str.writeObject(ostrm);
183  }
184 
185  BLOCXX_COMMON_API void readLen(std::streambuf & istrm, UInt32 & len);
186 
188  template <typename T>
189  inline void
190  readArray(std::streambuf & istr, T & a)
191  {
192  a.clear();
193  UInt32 len;
194  BinarySerialization::readLen(istr, len);
195 
196  a.reserve(len);
197  for (UInt32 i = 0; i < len; i++)
198  {
199  typename T::value_type x;
200  x.readObject(istr);
201  a.push_back(x);
202  }
203  }
204 
206  template <typename T>
207  inline void
208  writeArray(std::streambuf & ostrm, const T & a)
209  {
210  UInt32 len = static_cast<UInt32>(a.size());
211  BinarySerialization::writeLen(ostrm, len);
212  for (UInt32 i = 0; i < len; i++)
213  {
214  a.operator[](i).writeObject(ostrm);
215  }
216  }
217 
218  inline void writeStringArray(
219  std::streambuf & ostrm, const StringArray & stra
220  )
221  {
223  writeArray(ostrm, stra);
224  }
225 
226  BLOCXX_COMMON_API void writeStringArray(
227  std::streambuf & ostrm, const StringArray * propertyList
228  );
229 
230 
231  BLOCXX_COMMON_API void read(
232  std::streambuf & istrm, void * dataIn, size_t dataInLen
233  );
234 
235  inline void read(std::streambuf & istrm, String & arg)
236  {
237  arg.readObject(istrm);
238  }
239 
240  inline void read(std::streambuf & istrm, UInt64 & val)
241  {
242  BinarySerialization::read(istrm, &val, sizeof(val));
243  val = ntoh64(val);
244  }
245 
246  inline void read(std::streambuf & istrm, Int64 & val)
247  {
248  BinarySerialization::read(istrm, &val, sizeof(val));
249  val = ntoh64(val);
250  }
251 
252  inline void read(std::streambuf & istrm, Int32 & val)
253  {
254  BinarySerialization::read(istrm, &val, sizeof(val));
255  val = ntoh32(val);
256  }
257 
258  inline void read(std::streambuf & istrm, UInt32 & val)
259  {
260  BinarySerialization::read(istrm, &val, sizeof(val));
261  val = ntoh32(val);
262  }
263 
264  inline void read(std::streambuf & istrm, UInt16 & val)
265  {
266  BinarySerialization::read(istrm, &val, sizeof(val));
267  val = ntoh16(val);
268  }
269 
270  inline void read(std::streambuf & istrm, Int16 & val)
271  {
272  BinarySerialization::read(istrm, &val, sizeof(val));
273  val = ntoh16(val);
274  }
275 
276  inline void read(std::streambuf & istrm, UInt8 & val)
277  {
278  BinarySerialization::read(istrm, &val, sizeof(val));
279  }
280 
281  inline Bool readBool(std::streambuf & istrm)
282  {
284  Bool b;
285  b.readObject(istrm);
286  return b;
287  }
288 
289  inline String readString(std::streambuf & istrm)
290  {
292  String rv;
293  rv.readObject(istrm);
294  return rv;
295  }
296 
297  inline StringArray readStringArray(std::streambuf & istrm)
298  {
300  StringArray stra;
301  readArray(istrm, stra);
302  return stra;
303  }
304 
305 } // end namespace BinarySerialization
306 
307 } // end namespace BLOCXX_NAMESPACE
308 
309 #endif // BLOCXX_BinarySerialization_HPP_