575.4.7
by Monty Taylor
More header cleanup. |
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
|
|
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
22 |
|
23 |
#include <cstdlib> |
|
24 |
#include <cassert> |
|
25 |
#include <cstring> |
|
575.4.7
by Monty Taylor
More header cleanup. |
26 |
#include <stdint.h> |
27 |
||
28 |
#define LENGTH_ENCODE_MAX_BYTES (sizeof(std::size_t) + 1)
|
|
520.9.1
by mordred
More solaris fixes. |
29 |
|
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
30 |
inline unsigned char * |
520.9.1
by mordred
More solaris fixes. |
31 |
length_encode(std::size_t length, unsigned char *buf) |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
32 |
{
|
33 |
unsigned char *ptr= buf; |
|
34 |
assert(length > 1); |
|
35 |
if (length < 256) |
|
907.3.3
by Monty Taylor
A whole crapload of build fixes. OpenSolaris AMD64 builds with -m64 now by default... still isn't warning clean yet though. Also cleaned up the AM_*FLAGS situation. |
36 |
*ptr++= (unsigned char) (length & 0xFF); |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
37 |
else { |
38 |
int_fast8_t log2m1= -1; // ceil(log2(ptr - buf)) - 1 |
|
39 |
uint_fast8_t pow2= 1; // pow2(log2m1 + 1) |
|
40 |
while (length > 0) { |
|
41 |
// Check the invariants
|
|
42 |
assert(pow2 == (1 << (log2m1 + 1))); |
|
43 |
assert((ptr - buf) <= (1 << (log2m1 + 1))); |
|
44 |
||
45 |
// Write the least significant byte of the current
|
|
46 |
// length. Prefix increment is used to make space for the first
|
|
47 |
// byte that will hold log2m1.
|
|
907.3.3
by Monty Taylor
A whole crapload of build fixes. OpenSolaris AMD64 builds with -m64 now by default... still isn't warning clean yet though. Also cleaned up the AM_*FLAGS situation. |
48 |
*++ptr= (unsigned char)length & 0xFF; |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
49 |
length >>= 8; |
50 |
||
51 |
// Ensure the invariant holds by correcting it if it doesn't,
|
|
52 |
// that is, the number of bytes written is greater than the
|
|
53 |
// nearest power of two.
|
|
54 |
if (ptr - buf > pow2) { |
|
55 |
++log2m1; |
|
56 |
pow2 <<= 1; |
|
57 |
}
|
|
58 |
}
|
|
59 |
// Clear the remaining bytes up to the next power of two
|
|
520.9.1
by mordred
More solaris fixes. |
60 |
std::memset(ptr + 1, 0, pow2 - (ptr - buf)); |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
61 |
*buf= log2m1; |
62 |
ptr= buf + pow2 + 1; |
|
63 |
}
|
|
64 |
return ptr; |
|
65 |
}
|
|
66 |
||
67 |
inline unsigned char * |
|
575.4.7
by Monty Taylor
More header cleanup. |
68 |
length_decode(unsigned char *buf, std::size_t *plen) |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
69 |
{
|
70 |
if (*buf > 1) { |
|
71 |
*plen = *buf; |
|
72 |
return buf + 1; |
|
73 |
}
|
|
74 |
||
575.4.7
by Monty Taylor
More header cleanup. |
75 |
std::size_t bytes= 1 << (*buf + 1); |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
76 |
unsigned char *ptr= buf + 1; |
575.4.7
by Monty Taylor
More header cleanup. |
77 |
std::size_t length= 0; |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
78 |
for (unsigned int i = 0 ; i < bytes ; ++i) |
79 |
length |= *ptr++ << (8 * i); |
|
80 |
*plen= length; |
|
81 |
return ptr; |
|
82 |
}
|
|
83 |
||
84 |
/**
|
|
85 |
Compute how many bytes are use for the length.
|
|
86 |
||
87 |
The number of bytes that make up th length can be computed based on
|
|
88 |
the first byte of the length field. By supplying this byte to the
|
|
89 |
function, the number of bytes that is needed for the length is
|
|
90 |
computed.
|
|
91 |
||
92 |
*/
|
|
575.4.7
by Monty Taylor
More header cleanup. |
93 |
inline std::size_t |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
94 |
length_decode_bytes(int peek) |
95 |
{
|
|
96 |
return (peek < 2) ? (1 << (peek + 1)) + 1 : 1; |
|
97 |
}
|
|
98 |
||
575.4.7
by Monty Taylor
More header cleanup. |
99 |
#endif /* DRIZZLED_SERIALIZE_BINLOG_ENCODING_H */ |