/usr/include/wvstreams/wvserialize.h is in libwvstreams-dev 4.6.1-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | /* -*- Mode: C++ -*-
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* Code to serialize objects into WvBufs, and more code to read WvBufs and
* construct objects from them.
*/
#ifndef __WVSERIALIZE_H
#define __WVSERIALIZE_H
#include "wvbuf.h"
#include "wvstringlist.h"
#ifndef _WIN32
# if HAVE_INTTYPES_H
# include <inttypes.h>
# else
# if HAVE_STDINT_H
# include <stdint.h>
# endif
# endif
#include <netinet/in.h>
#else
#if _MSC_VER
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
#include <winsock2.h>
#endif
/**
* Encode an object as an array of bytes and put it into a WvBuf. This
* function just calls an overloaded _wv_serialize() function. There was
* really no need for a template here at all, except for symmetry with
* wv_deserialize() which does need one.
*/
template <typename T>
inline void wv_serialize(WvBuf &buf, const T &t)
{
_wv_serialize(buf, t);
}
/**
* This function shouldn't be necessary at all, but using it makes totally
* insane assembler errors go away (gcc 2.95.4, glibc 2.3.1).
*/
inline int32_t _wv_htonl(int32_t i)
{
return htonl(i);
}
inline int16_t _wv_htons(int16_t i)
{
return htons(i);
}
/**
* Helper functions to convert 64 bit ints to and from host byteorder
*/
inline uint64_t ntohll(uint64_t n)
{
#ifdef WORDS_BIGENDIAN
return n;
#else
return (((uint64_t)ntohl(n)) << 32) | ntohl(n >> 32);
#endif
}
inline uint64_t htonll(uint64_t n)
{
#ifdef WORDS_BIGENDIAN
return n;
#else
return (((uint64_t)htonl(n)) << 32) | htonl(n >> 32);
#endif
}
/**
* A helper function that serializes different types of integers. Since
* it's inlined, the "if" is actually executed at compile time, so don't
* worry.
*
* The clever part: it doesn't really matter what size an 'int' or a 'long'
* is, as long as it's one of the sizes supported by this function. If an
* int is 32 bits, we'll use the 32-bit serializer... and so on.
*/
template <typename T>
void wv_serialize_scalar(WvBuf &buf, const T t)
{
if (sizeof(T) == 8)
{
int64_t i = htonll(t);
buf.put(&i, 8);
}
else if (sizeof(T) == 4)
{
int32_t i = _wv_htonl(t);
buf.put(&i, 4);
}
else if (sizeof(T) == 2)
{
int32_t i = _wv_htons(t);
buf.put(&i, 2);
}
else if (sizeof(T) == 1)
buf.put(&t, 1);
else
assert(0);
}
inline void _wv_serialize(WvBuf &buf, long long i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, unsigned long long i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, long i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, unsigned long i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, int i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, unsigned int i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, short i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, unsigned short i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, bool i)
{ wv_serialize_scalar(buf, i); }
/** Note: char != signed char for purposes of function overloading! */
inline void _wv_serialize(WvBuf &buf, char i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, signed char i)
{ wv_serialize_scalar(buf, i); }
inline void _wv_serialize(WvBuf &buf, unsigned char i)
{ wv_serialize_scalar(buf, i); }
/**
* Serialize a WvString. The string serializer is guaranteed to not insert
* any nuls (character 0) into the output stream except for the
* string-terminating one, which is always present. This makes
* deserialization easy.
*/
inline void _wv_serialize(WvBuf &buf, WvStringParm s)
{
if (!s.isnull())
buf.putstr(s);
buf.put("", 1); // terminating nul
}
/** The template wv_serialize doesn't work for const char arrays. */
inline void wv_serialize(WvBuf &buf, const char *t)
{
_wv_serialize(buf, t);
}
/**
* Serialize a WvBuf. This is handier than it sounds, because then
* WvGdbmHash's value can be a WvBuf.
*/
inline void _wv_serialize(WvBuf &buf, const WvBuf &inbuf)
{
wv_serialize(buf, inbuf.used());
buf.put(const_cast<WvBuf *>(&inbuf)->peek(0, inbuf.used()), inbuf.used());
}
/**
* Serialize a list of serializable things.
*
* Oh boy - I think I'm having a bit too much fun.
*/
template <typename T>
void _wv_serialize(WvBuf &buf, const WvList<T> &list)
{
// save the number of elements
_wv_serialize(buf, (size_t)list.count());
// save the elements
typename WvList<T>::Iter i(list);
for (i.rewind(); i.next(); )
_wv_serialize(buf, *i);
}
/** Deserialize an object. See wv_deserialize(). */
template <typename T>
T _wv_deserialize(WvBuf &buf);
/**
* Deserialize a complex templated object. See wv_deserialize().
*
* This class is needed because partial template specialization only works
* on classes, not on functions. So in order to define a generic deserializer
* for, say, WvList<T>, we have to have a class with a member function. Sigh.
*/
template <typename T>
class WvDeserialize
{
public:
static T go(WvBuf &buf)
{ return _wv_deserialize<T>(buf); }
};
/**
* If there's a deserializer for type "T", this will make a default
* deserializer for type "T *"; that is, it'll allocate the new object
* dynamically and you'll have to free it after.
*
* This helps when you want to assume *all* deserializers return pointers
* that you need to delete later.
*
* FIXME: this class takes precedence over *specialized* _wv_deserialize()
* functions for pointers! Pointer-based deserializers need to be classes
* too until this is resolved.
*/
// note: this has to be a class because we use partial template
// specialization, which doesn't work on functions.
template <typename T>
class WvDeserialize<T *>
{
public:
static T *go(WvBuf &buf)
{ return new T(_wv_deserialize<T>(buf)); }
};
/**
* Deserialize an object: read bytes from a buffer, and return an object
* constructed from that.
*
* Note that there is no default deserializer. You have to specialize this
* template for every data type you might want to deserialize. We do define
* some for a few standard C types.
*
* Implementation note:
* If you define a deserializer for your own type, name it _wv_deserialize()
* (with the underscore). If you're unlucky, you may need to define a
* WvDeserialize class instead.
*
* Note that if you have a data structure, you probably want to
* wv_deserialize<MyType *>(buf) instead of wv_deserialize<MyType>(buf) to
* avoid extra copies. You'll have to define _wv_deserialize() appropriately,
* of course. Pointer-based _wv_deserialize() functions allocate memory,
* so you'll have to 'delete' the returned object yourself.
*/
template <typename T>
inline T wv_deserialize(WvBuf &buf)
{
return WvDeserialize<T>::go(buf);
}
/**
* These functions shouldn't be necessary at all, but using it makes totally
* insane assembler errors go away (gcc 2.95.4, glibc 2.3.1).
*/
inline int32_t _wv_ntohl(int32_t i)
{
return ntohl(i);
}
inline int16_t _wv_ntohs(int16_t i)
{
return ntohs(i);
}
/**
* A helper function that deserializes different types of integers. Since
* it's inlined, the "if" is actually executed at compile time, so don't
* worry.
*/
template <typename T>
inline T wv_deserialize_scalar(WvBuf &buf)
{
if (buf.used() < sizeof(T))
return 0;
if (sizeof(T) == 8)
return (T) ntohll(*(int64_t *)buf.get(8));
else if (sizeof(T) == 4)
return (T) _wv_ntohl(*(int32_t *)buf.get(4));
else if (sizeof(T) == 2)
return (T) _wv_ntohs(*(int16_t *)buf.get(2));
else if (sizeof(T) == 1)
return (T) *(int8_t *)buf.get(1);
else
assert(0);
}
template <typename T>
inline T xwv_deserialize_scalar(WvBuf &buf)
{
return 0;
}
template <>
inline long long _wv_deserialize<long long>(WvBuf &buf)
{ return wv_deserialize_scalar<long long>(buf); }
template <>
inline unsigned long long _wv_deserialize<unsigned long long>(WvBuf &buf)
{ return wv_deserialize_scalar<unsigned long long>(buf); }
template <>
inline long _wv_deserialize<long>(WvBuf &buf)
{ return wv_deserialize_scalar<long>(buf); }
template <>
inline unsigned long _wv_deserialize<unsigned long>(WvBuf &buf)
{ return wv_deserialize_scalar<unsigned long>(buf); }
template <>
inline int _wv_deserialize<int>(WvBuf &buf)
{ return wv_deserialize_scalar<int>(buf); }
template <>
inline unsigned int _wv_deserialize<unsigned int>(WvBuf &buf)
{ return wv_deserialize_scalar<unsigned int>(buf); }
template <>
inline short _wv_deserialize<short>(WvBuf &buf)
{ return wv_deserialize_scalar<short>(buf); }
template <>
inline unsigned short _wv_deserialize<unsigned short>(WvBuf &buf)
{ return wv_deserialize_scalar<unsigned short>(buf); }
template <>
inline bool _wv_deserialize<bool>(WvBuf &buf)
{ return wv_deserialize_scalar<bool>(buf); }
template <>
inline char _wv_deserialize<char>(WvBuf &buf)
{ return wv_deserialize_scalar<char>(buf); }
template <>
inline signed char _wv_deserialize<signed char>(WvBuf &buf)
{ return wv_deserialize_scalar<signed char>(buf); }
template <>
inline unsigned char _wv_deserialize<unsigned char>(WvBuf &buf)
{ return wv_deserialize_scalar<unsigned char>(buf); }
/**
* Deserialize a WvString. Stops at (and includes) the terminating nul
* (zero) character. Serialized WvStrings are guaranteed not to contain nul
* except as the last character.
*/
template <>
WvString _wv_deserialize<WvString>(WvBuf &buf);
/** Deserialize a WvBuf. */
// FIXME: it should be possible to do this without using a class!
template <>
class WvDeserialize<WvBuf *>
{
public:
static WvBuf *go(WvBuf &buf)
{
size_t len = wv_deserialize<size_t>(buf);
WvBuf *outbuf = new WvInPlaceBuf(new char[len], 0, len, true);
outbuf->merge(buf, len);
return outbuf;
}
};
/** Deserialize a list of serializable things. */
template <typename T>
class WvDeserialize<WvList<T> *>
{
public:
static WvList<T> *go(WvBuf &buf)
{
WvList<T> *list = new WvList<T>;
size_t nelems = wv_deserialize<size_t>(buf);
for (size_t count = 0; count < nelems; count++)
{
T t = wv_deserialize<T>(buf);
list->append(new T(t), true);
}
return list;
}
};
template <>
class WvDeserialize<WvStringList*>
{
public:
static WvStringList *go(WvBuf &buf)
{
WvStringList *list = new WvStringList();
size_t nelems = wv_deserialize<size_t>(buf);
for (size_t count = 0; count < nelems; count++)
{
WvString str = wv_deserialize<WvString>(buf);
list->append(str);
}
return list;
}
};
#endif // __WVSERIALIZE_H
|