Loading...
Searching...
No Matches
cs_varint.h
1/*
2 * Copyright (c) 2014-2018 Cesanta Software Limited
3 * All rights reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the ""License"");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an ""AS IS"" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef CS_COMMON_CS_VARINT_H_
19#define CS_COMMON_CS_VARINT_H_
20
21#if defined(_WIN32) && _MSC_VER < 1700
22typedef unsigned char uint8_t;
23typedef unsigned __int64 uint64_t;
24#else
25#include <stdbool.h>
26#include <stdint.h>
27#include <stdlib.h>
28#endif
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/* Returns number of bytes required to encode `num`. */
35size_t cs_varint_llen(uint64_t num);
36
37/*
38 * Encodes `num` into `buf`.
39 * Returns number of bytes required to encode `num`.
40 * Note: return value may be greater than `buf_size` but the function will only
41 * write `buf_size` bytes.
42 */
43size_t cs_varint_encode(uint64_t num, uint8_t *buf, size_t buf_size);
44
45/*
46 * Decodes varint stored in `buf`.
47 * Stores the number of bytes consumed into `llen`.
48 * If there aren't enough bytes in `buf` to decode a number, returns false.
49 */
50bool cs_varint_decode(const uint8_t *buf, size_t buf_size, uint64_t *num,
51 size_t *llen);
52
53uint64_t cs_varint_decode_unsafe(const uint8_t *buf, int *llen);
54
55#ifdef __cplusplus
56}
57#endif
58
59#endif /* CS_COMMON_CS_VARINT_H_ */