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
|
/*
* librdkafka - The Apache Kafka C/C++ library
*
* Copyright (c) 2016 Magnus Edenhill
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _RDVARINT_H
#define _RDVARINT_H
#include "rd.h"
#include "rdbuf.h"
/**
* @name signed varint zig-zag encoder/decoder
* @{
*
*/
/**
* @brief unsigned-varint encodes unsigned integer \p num into buffer
* at \p dst of size \p dstsize.
* @returns the number of bytes written to \p dst, or 0 if not enough space.
*/
static RD_INLINE RD_UNUSED size_t rd_uvarint_enc_u64(char *dst,
size_t dstsize,
uint64_t num) {
size_t of = 0;
do {
if (unlikely(of >= dstsize))
return 0; /* Not enough space */
dst[of++] = (num & 0x7f) | (num > 0x7f ? 0x80 : 0);
num >>= 7;
} while (num);
return of;
}
/**
* @brief encodes a signed integer using zig-zag encoding.
* @sa rd_uvarint_enc_u64
*/
static RD_INLINE RD_UNUSED size_t rd_uvarint_enc_i64(char *dst,
size_t dstsize,
int64_t num) {
return rd_uvarint_enc_u64(dst, dstsize, (num << 1) ^ (num >> 63));
}
static RD_INLINE RD_UNUSED size_t rd_uvarint_enc_i32(char *dst,
size_t dstsize,
int32_t num) {
return rd_uvarint_enc_i64(dst, dstsize, num);
}
/**
* @brief Use on return value from rd_uvarint_dec() to check if
* decoded varint fit the size_t.
*
* @returns 1 on overflow, else 0.
*/
#define RD_UVARINT_OVERFLOW(DEC_RETVAL) (DEC_RETVAL > SIZE_MAX)
/**
* @returns 1 if there were not enough bytes to decode the varint, else 0.
*/
#define RD_UVARINT_UNDERFLOW(DEC_RETVAL) (DEC_RETVAL == 0)
/**
* @param DEC_RETVAL the return value from \c rd_uvarint_dec()
* @returns 1 if varint decoding failed, else 0.
* @warning \p DEC_RETVAL will be evaluated twice.
*/
#define RD_UVARINT_DEC_FAILED(DEC_RETVAL) \
(RD_UVARINT_UNDERFLOW(DEC_RETVAL) || RD_UVARINT_OVERFLOW(DEC_RETVAL))
/**
* @brief Decodes the unsigned-varint in buffer \p src of size \p srcsize
* and stores the decoded unsigned integer in \p nump.
*
* @remark Use RD_UVARINT_OVERFLOW(returnvalue) to check if the varint
* could not fit \p nump, and RD_UVARINT_UNDERFLOW(returnvalue) to
* check if there were not enough bytes available in \p src to
* decode the full varint.
*
* @returns the number of bytes read from \p src.
*/
static RD_INLINE RD_UNUSED size_t rd_uvarint_dec(const char *src,
size_t srcsize,
uint64_t *nump) {
size_t of = 0;
uint64_t num = 0;
int shift = 0;
do {
if (unlikely(srcsize-- == 0))
return 0; /* Underflow */
num |= (uint64_t)(src[(int)of] & 0x7f) << shift;
shift += 7;
} while (src[(int)of++] & 0x80);
*nump = num;
return of;
}
static RD_INLINE RD_UNUSED size_t rd_varint_dec_i64(const char *src,
size_t srcsize,
int64_t *nump) {
uint64_t n;
size_t r;
r = rd_uvarint_dec(src, srcsize, &n);
if (likely(!RD_UVARINT_DEC_FAILED(r)))
*nump = (int64_t)(n >> 1) ^ -(int64_t)(n & 1);
return r;
}
/**
* @returns the maximum encoded size for a type
*/
#define RD_UVARINT_ENC_SIZEOF(TYPE) (sizeof(TYPE) + 1 + (sizeof(TYPE) / 7))
/**
* @returns the encoding size of the value 0
*/
#define RD_UVARINT_ENC_SIZE_0() ((size_t)1)
int unittest_rdvarint(void);
/**@}*/
#endif /* _RDVARINT_H */
|