Mercurial > dovecot > core-2.2
annotate src/lib/numpack.c @ 22664:fea53c2725c0
director: Fix director_max_parallel_moves/kicks type
Should be uint, not time.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 09 Nov 2017 12:24:16 +0200 |
parents | 2e2563132d5f |
children | cb108f786fb4 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
19552
diff
changeset
|
1 /* Copyright (c) 2013-2017 Dovecot authors, see the included COPYING file */ |
14861 | 2 |
3 #include "lib.h" | |
4 #include "buffer.h" | |
5 #include "numpack.h" | |
6 | |
7 void numpack_encode(buffer_t *buf, uint64_t num) | |
8 { | |
9 /* number continues as long as the highest bit is set */ | |
10 while (num >= 0x80) { | |
11 buffer_append_c(buf, (num & 0x7f) | 0x80); | |
12 num >>= 7; | |
13 } | |
14 | |
15 buffer_append_c(buf, num); | |
16 } | |
17 | |
18 int numpack_decode(const uint8_t **p, const uint8_t *end, uint64_t *num_r) | |
19 { | |
20 const uint8_t *c = *p; | |
21 uint64_t value = 0; | |
22 unsigned int bits = 0; | |
23 | |
17450
1c276f2e59a5
lib: fix numpack overflow checking
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
24 while (bits < 64) { |
14861 | 25 if (c == end) |
26 return -1; | |
27 | |
28 value |= (uint64_t)(*c & 0x7f) << bits; | |
29 if (*c < 0x80) | |
30 break; | |
31 | |
32 bits += 7; | |
33 c++; | |
34 } | |
35 | |
17450
1c276f2e59a5
lib: fix numpack overflow checking
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
36 bits += bits_required8(*c); |
1c276f2e59a5
lib: fix numpack overflow checking
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
37 if (bits > 64) /* overflow */ |
14861 | 38 return -1; |
39 | |
40 *p = c + 1; | |
41 *num_r = value; | |
42 return 0; | |
43 } | |
18480 | 44 |
45 int numpack_decode32(const uint8_t **p, const uint8_t *end, uint32_t *num_r) | |
46 { | |
47 uint64_t num; | |
48 | |
49 if (numpack_decode(p, end, &num) < 0) | |
50 return -1; | |
51 if (num > 4294967295U) | |
52 return -1; | |
53 | |
54 *num_r = (uint32_t)num; | |
55 return 0; | |
56 } |