0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Serialization.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
28 #ifndef HYPERTABLE_SERIALIZATION_H
29 #define HYPERTABLE_SERIALIZATION_H
30 
31 #include "InetAddr.h"
32 
33 #include "serialization-c.h"
34 
35 
36 namespace Hypertable { namespace Serialization {
37 
49  inline void encode_i8(uint8_t **bufp, uint8_t val) {
50  HT_ENCODE_I8(*bufp, val);
51  }
52 
60  inline uint8_t decode_i8(const uint8_t **bufp, size_t *remainp) {
61  HT_DECODE_NEED(*remainp, 1);
62  return *(*bufp)++;
63  }
64 
73  inline uint8_t decode_byte(const uint8_t **bufp, size_t *remainp) {
74  return decode_i8(bufp, remainp);
75  }
76 
84  inline void encode_bool(uint8_t **bufp, bool bval) {
85  HT_ENCODE_BOOL(*bufp, bval);
86  }
87 
96  inline bool decode_bool(const uint8_t **bufp, size_t *remainp) {
97  return decode_i8(bufp, remainp) != 0;
98  }
99 
106  inline void encode_i16(uint8_t **bufp , uint16_t val) {
107  HT_ENCODE_I16(*bufp, val);
108  }
109 
117  inline uint16_t decode_i16(const uint8_t **bufp, size_t *remainp) {
118  uint16_t val;
119  HT_DECODE_I16(*bufp, *remainp, val);
120  return val;
121  }
122 
129  inline void encode_i32(uint8_t **bufp, uint32_t val) {
130  HT_ENCODE_I32(*bufp, val);
131  }
132 
140  inline uint32_t decode_i32(const uint8_t **bufp, size_t *remainp) {
141  uint32_t val;
142  HT_DECODE_I32(*bufp, *remainp, val);
143  return val;
144  }
145 
152  inline void encode_i64(uint8_t **bufp, uint64_t val) {
153  HT_ENCODE_I64(*bufp, val);
154  }
155 
163  inline uint64_t decode_i64(const uint8_t **bufp, size_t *remainp) {
164  uint64_t val;
165  HT_DECODE_I64(*bufp, *remainp, val);
166  return val;
167  }
168 
175  inline int encoded_length_vi32(uint32_t val) {
176  return HT_ENCODED_LEN_VI32(val);
177  }
178 
185  inline int encoded_length_vi64(uint64_t val) {
186  return HT_ENCODED_LEN_VI64(val);
187  }
188 
195  inline void encode_vi32(uint8_t **bufp, uint32_t val) {
196  HT_ENCODE_VI32(*bufp, val, return);
197  }
198 
205  inline void encode_vi64(uint8_t **bufp, uint64_t val) {
206  HT_ENCODE_VI64(*bufp, val, return);
207  }
208 
216  inline uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp) {
217  uint32_t n;
218  HT_DECODE_VI32(*bufp, *remainp, n, return n);
219  }
220 
228  inline uint64_t decode_vi64(const uint8_t **bufp, size_t *remainp) {
229  uint64_t n;
230  HT_DECODE_VI64(*bufp, *remainp, n, return n);
231  }
232 
239  inline uint32_t decode_vi32(const uint8_t **bufp) {
240  size_t remain = 6;
241  return decode_vi32(bufp, &remain);
242  }
243 
250  inline uint64_t decode_vi64(const uint8_t **bufp) {
251  size_t remain = 12;
252  return decode_vi64(bufp, &remain);
253  }
254 
261  inline size_t encoded_length_bytes32(int32_t len) {
262  return len + 4;
263  }
264 
274  inline void encode_bytes32(uint8_t **bufp, const void *data, int32_t len) {
275  HT_ENCODE_BYTES32(*bufp, data, len);
276  }
277 
287  inline uint8_t *decode_bytes32(const uint8_t **bufp, size_t *remainp,
288  uint32_t *lenp) {
289  uint8_t *out;
290  HT_DECODE_BYTES32(*bufp, *remainp, out, *lenp);
291  return out;
292  }
293 
300  inline size_t encoded_length_str16(const char *str) {
301  return 2 + ((str == 0) ? 0 : strlen(str)) + 1;
302  }
303 
310  inline size_t encoded_length_str16(const String &str) {
311  return 2 + str.length() + 1;
312  }
313 
325  inline void encode_str16(uint8_t **bufp, const void *str, uint16_t len) {
326  HT_ENCODE_STR16(*bufp, str, len);
327  }
328 
339  inline void encode_str16(uint8_t **bufp, const char *str) {
340  uint16_t len = (str == 0) ? 0 : strlen(str);
341  encode_str16(bufp, str, len);
342  }
343 
354  template <class StringT>
355  inline void encode_str16(uint8_t **bufp, const StringT &str) {
356  encode_str16(bufp, str.c_str(), str.length());
357  }
358 
370  inline const char *decode_str16(const uint8_t **bufp, size_t *remainp) {
371  const char *str;
372  uint16_t len;
373  HT_DECODE_STR16(*bufp, *remainp, str, len);
374  return str;
375  }
376 
389  inline char *decode_str16(const uint8_t **bufp, size_t *remainp,
390  uint16_t *lenp) {
391  char *str;
392  HT_DECODE_STR16(*bufp, *remainp, str, *lenp);
393  return str;
394  }
395 
406  template <class StringT>
407  inline StringT decode_str16(const uint8_t **bufp, size_t *remainp) {
408  char *str;
409  uint16_t len;
410  HT_DECODE_STR16(*bufp, *remainp, str, len);
411  return StringT(str, len); // RVO hopeful
412  }
413 
420  inline size_t encoded_length_vstr(size_t len) {
421  return encoded_length_vi64(len) + len + 1;
422  }
423 
431  inline size_t encoded_length_vstr(const char *s) {
432  return encoded_length_vstr(s ? strlen(s) : 0);
433  }
434 
442  inline size_t encoded_length_vstr(const String &s) {
443  return encoded_length_vstr(s.length());
444  }
445 
453  inline void encode_vstr(uint8_t **bufp, const void *buf, size_t len) {
454  HT_ENCODE_VSTR(*bufp, buf, len);
455  }
456 
463  inline void encode_vstr(uint8_t **bufp, const char *s) {
464  encode_vstr(bufp, s, s ? strlen(s) : 0);
465  }
466 
473  template <class StringT>
474  inline void encode_vstr(uint8_t **bufp, const StringT &s) {
475  encode_vstr(bufp, s.data(), s.length());
476  }
477 
487  inline char *decode_vstr(const uint8_t **bufp, size_t *remainp) {
488  char *buf;
489  size_t len;
490  HT_DECODE_VSTR(*bufp, *remainp, buf, len);
491  (void)len; // avoid warnings because len is assigned but never used
492  return buf;
493  }
494 
502  template <class StringT>
503  inline String decode_vstr(const uint8_t **bufp, size_t *remainp) {
504  char *buf;
505  size_t len;
506  HT_DECODE_VSTR(*bufp, *remainp, buf, len);
507  return StringT(buf, len); // RVO
508  }
509 
518  inline char *decode_vstr(const uint8_t **bufp, size_t *remainp,
519  uint32_t *lenp) {
520  char *buf;
521  HT_DECODE_VSTR(*bufp, *remainp, buf, *lenp);
522  return buf;
523  }
524 
525 
532  inline void encode_inet_addr(uint8_t **bufp, const InetAddr &addr) {
533  HT_ENCODE_I16(*bufp, addr.sin_family);
534  HT_ENCODE_I32(*bufp, addr.sin_addr.s_addr);
535  HT_ENCODE_I16(*bufp, addr.sin_port);
536  }
537 
545  inline InetAddr decode_inet_addr(const uint8_t **bufp, size_t *remainp) {
546  InetAddr addr;
547  HT_DECODE_I16(*bufp, *remainp, addr.sin_family);
548  HT_DECODE_I32(*bufp, *remainp, addr.sin_addr.s_addr);
549  HT_DECODE_I16(*bufp, *remainp, addr.sin_port);
550  return addr;
551  }
552 
553 
562  inline void encode_double(uint8_t **bufp, double val) {
563  int64_t lod = (int64_t)val;
564  int64_t rod = (int64_t)((val - (double)lod) * (double)1000000000000000000.00);
565  HT_ENCODE_I64(*bufp, lod);
566  HT_ENCODE_I64(*bufp, rod);
567  }
568 
577  inline double decode_double(const uint8_t **bufp, size_t *remainp) {
578  int64_t lod, rod;
579  HT_DECODE_I64(*bufp, *remainp, lod);
580  HT_DECODE_I64(*bufp, *remainp, rod);
581  return (double)lod + ((double)rod / (double)1000000000000000000.00);
582  }
583 
589  inline int encoded_length_double() {
590  return 16;
591  }
592 
602  inline bool equal(double a, double b) {
603  int64_t lod_a = (int64_t)a;
604  int64_t rod_a = (int64_t)((a - (double)lod_a) * (double)1000000000000000000.00);
605  double aprime = (double)lod_a + ((double)rod_a / (double)1000000000000000000.00);
606  int64_t lod_b = (int64_t)b;
607  int64_t rod_b = (int64_t)((b - (double)lod_b) * (double)1000000000000000000.00);
608  double bprime = (double)lod_b + ((double)rod_b / (double)1000000000000000000.00);
609  return aprime == bprime;
610  }
611 
614 }} // namespace Hypertable::Serialization
615 
616 #endif // HYPERTABLE_SERIALIZATION_H
#define HT_DECODE_NEED(_r_, _l_)
char * decode_vstr(const uint8_t **bufp, size_t *remainp)
Decode a vstr (vint64, data, null).
#define HT_ENCODE_STR16(_op_, _s_, _len_)
int encoded_length_double()
Length of an encoded double (16 bytes)
#define HT_ENCODE_VSTR(_op_, _s_, _len_)
#define HT_ENCODE_BYTES32(_op_, _ip_, _len_)
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
#define HT_DECODE_VSTR(_ip_, _r_, _out_, _len_)
#define HT_DECODE_I32(_ip_, _r_, _v_)
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
#define HT_ENCODE_I16(_op_, _v_)
#define HT_ENCODE_VI64(_op_, _v_, _done_)
#define HT_ENCODE_VI32(_op_, _v_, _done_)
#define HT_DECODE_BYTES32(_ip_, _r_, _out_, _len_)
size_t encoded_length_vstr(size_t len)
Computes the encoded length of vstr (vint64, data, null)
#define HT_ENCODE_BOOL(_op_, _v_)
void encode_inet_addr(uint8_t **bufp, const InetAddr &addr)
Encode an InetAddr structure.
#define HT_ENCODE_I8(_op_, _v_)
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
#define HT_ENCODED_LEN_VI32(_v_)
uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Decode a 8-bit integer (a byte/character)
Definition: Serialization.h:60
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
int encoded_length_vi32(uint32_t val)
Length of a variable length encoded 32-bit integer (up to 5 bytes)
void encode_vi64(uint8_t **bufp, uint64_t val)
Encode a integer (up to 64-bit) in variable length encoding.
size_t encoded_length_str16(const char *str)
Computes the encoded length of a string16 encoding.
bool decode_bool(const uint8_t **bufp, size_t *remainp)
Decodes a boolean value from the given buffer.
Definition: Serialization.h:96
Encapsulate an internet address.
Definition: InetAddr.h:66
uint16_t decode_i16(const uint8_t **bufp, size_t *remainp)
Decode a 16-bit integer in little-endian order.
#define HT_DECODE_I16(_ip_, _r_, _v_)
#define HT_DECODE_I64(_ip_, _r_, _v_)
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
void encode_i16(uint8_t **bufp, uint16_t val)
Encode a 16-bit integer in little-endian order.
size_t encoded_length_bytes32(int32_t len)
Computes the encoded length of a 32-bit length byte array (i32, bytes)
void encode_i64(uint8_t **bufp, uint64_t val)
Encode a 64-bit integer in little-endian order.
const char * decode_str16(const uint8_t **bufp, size_t *remainp)
Decodes a c-style string from the given buffer.
#define HT_DECODE_STR16(_ip_, _r_, _s_, _len_)
void encode_vstr(uint8_t **bufp, const void *buf, size_t len)
Encode a buffer as variable length string (vint64, data, null)
Hypertable definitions
uint64_t decode_vi64(const uint8_t **bufp, size_t *remainp)
Decode a variable length encoded integer up to 64-bit.
#define HT_ENCODED_LEN_VI64(_v_)
void encode_vi32(uint8_t **bufp, uint32_t val)
Encode a integer (up to 32-bit) in variable length encoding.
void encode_bool(uint8_t **bufp, bool bval)
Encodes a boolean into the given buffer.
Definition: Serialization.h:84
double decode_double(const uint8_t **bufp, size_t *remainp)
Decodes a double as 64-bit left-of-decimal, followed by 64-bit right-of-decimal, both in little-endia...
bool equal(double a, double b)
Compare doubles that may have been serialized and unserialized.
Internet address wrapper classes and utility functions.
#define HT_DECODE_VI64(_ip_, _r_, _v_, _done_)
uint8_t * decode_bytes32(const uint8_t **bufp, size_t *remainp, uint32_t *lenp)
Decodes a variable sized byte array from the given buffer.
#define HT_DECODE_VI32(_ip_, _r_, _v_, _done_)
void encode_str16(uint8_t **bufp, const void *str, uint16_t len)
Encodes a string buffer into the given buffer.
#define HT_ENCODE_I32(_op_, _v_)
InetAddr decode_inet_addr(const uint8_t **bufp, size_t *remainp)
Decode an InetAddr structure.
uint8_t decode_byte(const uint8_t **bufp, size_t *remainp)
Decodes a single byte from the given buffer.
Definition: Serialization.h:73
void encode_bytes32(uint8_t **bufp, const void *data, int32_t len)
Encodes a variable sized byte array into the given buffer.
Macros to serialize/deserialize primitives to/from a memory buffer.
uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp)
Decode a variable length encoded integer up to 32-bit.
void encode_i8(uint8_t **bufp, uint8_t val)
Encodes a byte into the given buffer.
Definition: Serialization.h:49
void encode_double(uint8_t **bufp, double val)
Encodes a double with 18 decimal digits of precision as 64-bit left-of-decimal, followed by 64-bit ri...
#define HT_ENCODE_I64(_op_, _v_)
int encoded_length_vi64(uint64_t val)
Length of a variable length encoded 64-bit integer (up to 9 bytes)