~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/binlog_encoding.h

  • Committer: Monty Taylor
  • Date: 2010-02-05 08:11:15 UTC
  • mfrom: (1283 build)
  • mto: (1273.13.43 fix_is)
  • mto: This revision was merged to the branch mainline in revision 1300.
  • Revision ID: mordred@inaugust.com-20100205081115-dr82nvrwv4lvw7sd
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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_MESSAGE_BINLOG_ENCODING_H
21
 
#define DRIZZLED_MESSAGE_BINLOG_ENCODING_H
22
 
 
23
 
#include <cstdlib>
24
 
#include <cassert>
25
 
#include <cstring>
26
 
#include <stdint.h>
27
 
 
28
 
#define LENGTH_ENCODE_MAX_BYTES (sizeof(std::size_t) + 1)
29
 
 
30
 
inline unsigned char *
31
 
length_encode(std::size_t length, unsigned char *buf)
32
 
{
33
 
  unsigned char *ptr= buf;
34
 
  assert(length > 1);
35
 
  if (length < 256)
36
 
  {
37
 
    *ptr++= static_cast<unsigned char>((length & 0xFF));
38
 
  }
39
 
  else
40
 
  {
41
 
    int_fast8_t log2m1= -1;        // ceil(log2(ptr - buf)) - 1
42
 
    uint_fast8_t pow2= 1;          // pow2(log2m1 + 1)
43
 
    while (length > 0) 
44
 
    {
45
 
      // Check the invariants
46
 
      assert((static_cast<int_fast8_t>(pow2)) == (1 << (log2m1 + 1)));
47
 
      assert((ptr - buf) <= (1 << (log2m1 + 1)));
48
 
 
49
 
      // Write the least significant byte of the current
50
 
      // length. Prefix increment is used to make space for the first
51
 
      // byte that will hold log2m1.
52
 
      *++ptr= static_cast<unsigned char>(length & 0xFF);
53
 
      length >>= 8;
54
 
 
55
 
      // Ensure the invariant holds by correcting it if it doesn't,
56
 
      // that is, the number of bytes written is greater than the
57
 
      // nearest power of two.
58
 
      if (ptr - buf > pow2) 
59
 
      {
60
 
        ++log2m1;
61
 
        pow2= static_cast<int_fast8_t>(pow2 << 1);
62
 
      }
63
 
    }
64
 
    // Clear the remaining bytes up to the next power of two
65
 
    std::memset(ptr + 1, 0, pow2 - (ptr - buf));
66
 
    *buf= log2m1;
67
 
    ptr= buf + pow2 + 1;
68
 
  }
69
 
  return ptr;
70
 
}
71
 
 
72
 
inline unsigned char *
73
 
length_decode(unsigned char *buf, std::size_t *plen)
74
 
{
75
 
  if (*buf > 1) 
76
 
  {
77
 
    *plen = *buf;
78
 
    return buf + 1;
79
 
  }
80
 
 
81
 
  std::size_t bytes= 1 << (*buf + 1);
82
 
  unsigned char *ptr= buf + 1;
83
 
  std::size_t length= 0;
84
 
  for (unsigned int i = 0 ; i < bytes ; ++i)
85
 
    length |= *ptr++ << (8 * i);
86
 
  *plen= length;
87
 
  return ptr;
88
 
}
89
 
 
90
 
/**
91
 
   Compute how many bytes are use for the length.
92
 
 
93
 
   The number of bytes that make up th length can be computed based on
94
 
   the first byte of the length field. By supplying this byte to the
95
 
   function, the number of bytes that is needed for the length is
96
 
   computed.
97
 
 
98
 
 */
99
 
inline std::size_t
100
 
length_decode_bytes(int peek)
101
 
{
102
 
  return (peek < 2) ? (1 << (peek + 1)) + 1 : 1;
103
 
}
104
 
 
105
 
#endif /* DRIZZLED_MESSAGE_BINLOG_ENCODING_H */