~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/binlog_encoding.h

Further drizzleclient cleanup. Starting to move all NET related functions into Protocol, removed some dead code, removing drizzleclient dependencies spread throughout drizzled.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef BINLOG_ENCODE_H_INCLUDED
2
 
#define BINLOG_ENCODE_H_INCLUDED
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#ifndef DRIZZLED_SERIALIZE_BINLOG_ENCODING_H
 
21
#define DRIZZLED_SERIALIZE_BINLOG_ENCODING_H
3
22
 
4
23
#include <cstdlib>
5
24
#include <cassert>
6
25
#include <cstring>
7
26
#include <stdint.h>
8
27
 
9
 
#define LENGTH_ENCODE_MAX_BYTES (sizeof(size_t) + 1)
 
28
#define LENGTH_ENCODE_MAX_BYTES (sizeof(std::size_t) + 1)
10
29
 
11
30
inline unsigned char *
12
 
length_encode(size_t length, unsigned char *buf)
 
31
length_encode(std::size_t length, unsigned char *buf)
13
32
{
14
33
  unsigned char *ptr= buf;
15
34
  assert(length > 1);
16
35
  if (length < 256)
17
 
    *ptr++= length & 0xFF;
 
36
    *ptr++= (unsigned char) (length & 0xFF);
18
37
  else {
19
38
    int_fast8_t log2m1= -1;        // ceil(log2(ptr - buf)) - 1
20
39
    uint_fast8_t pow2= 1;          // pow2(log2m1 + 1)
26
45
      // Write the least significant byte of the current
27
46
      // length. Prefix increment is used to make space for the first
28
47
      // byte that will hold log2m1.
29
 
      *++ptr= length & 0xFF;
 
48
      *++ptr= (unsigned char)length & 0xFF;
30
49
      length >>= 8;
31
50
 
32
51
      // Ensure the invariant holds by correcting it if it doesn't,
38
57
      }
39
58
    }
40
59
    // Clear the remaining bytes up to the next power of two
41
 
    memset(ptr + 1, 0, pow2 - (ptr - buf));
 
60
    std::memset(ptr + 1, 0, pow2 - (ptr - buf));
42
61
    *buf= log2m1;
43
62
    ptr= buf + pow2 + 1;
44
63
  }
46
65
}
47
66
 
48
67
inline unsigned char *
49
 
length_decode(unsigned char *buf, size_t *plen)
 
68
length_decode(unsigned char *buf, std::size_t *plen)
50
69
{
51
70
  if (*buf > 1) {
52
71
    *plen = *buf;
53
72
    return buf + 1;
54
73
  }
55
74
 
56
 
  size_t bytes= 1 << (*buf + 1);
 
75
  std::size_t bytes= 1 << (*buf + 1);
57
76
  unsigned char *ptr= buf + 1;
58
 
  size_t length= 0;
 
77
  std::size_t length= 0;
59
78
  for (unsigned int i = 0 ; i < bytes ; ++i)
60
79
    length |= *ptr++ << (8 * i);
61
80
  *plen= length;
71
90
   computed.
72
91
 
73
92
 */
74
 
inline size_t
 
93
inline std::size_t
75
94
length_decode_bytes(int peek)
76
95
{
77
96
  return (peek < 2) ? (1 << (peek + 1)) + 1 : 1;
78
97
}
79
98
 
80
 
#endif /* BINLOG_ENCODE_H_INCLUDED */
 
99
#endif /*  DRIZZLED_SERIALIZE_BINLOG_ENCODING_H */