1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16// Endian-neutral encoding:
17// * Fixed-length numbers are encoded with least-significant byte first
18// * In addition we support variable length "varint" encoding
19// * Strings are encoded prefixed by their length in varint format
20
21#ifndef TENSORFLOW_TSL_PLATFORM_CODING_H_
22#define TENSORFLOW_TSL_PLATFORM_CODING_H_
23
24#include "tensorflow/tsl/platform/stringpiece.h"
25#include "tensorflow/tsl/platform/types.h"
26
27namespace tsl {
28namespace core {
29
30// Maximum number of bytes occupied by a varint32.
31static const int kMaxVarint32Bytes = 5;
32
33// Maximum number of bytes occupied by a varint64.
34static const int kMaxVarint64Bytes = 10;
35
36// Lower-level versions of Put... that write directly into a character buffer
37// REQUIRES: dst has enough space for the value being written
38extern void EncodeFixed16(char* dst, uint16 value);
39extern void EncodeFixed32(char* dst, uint32 value);
40extern void EncodeFixed64(char* dst, uint64 value);
41extern void PutFixed16(string* dst, uint16 value);
42extern void PutFixed32(string* dst, uint32 value);
43extern void PutFixed64(string* dst, uint64 value);
44
45extern void PutVarint32(string* dst, uint32 value);
46extern void PutVarint64(string* dst, uint64 value);
47
48extern void PutVarint32(tstring* dst, uint32 value);
49extern void PutVarint64(tstring* dst, uint64 value);
50
51extern bool GetVarint32(StringPiece* input, uint32* value);
52extern bool GetVarint64(StringPiece* input, uint64* value);
53
54extern const char* GetVarint32Ptr(const char* p, const char* limit, uint32* v);
55extern const char* GetVarint64Ptr(const char* p, const char* limit, uint64* v);
56
57// Internal routine for use by fallback path of GetVarint32Ptr
58extern const char* GetVarint32PtrFallback(const char* p, const char* limit,
59 uint32* value);
60extern const char* GetVarint32Ptr(const char* p, const char* limit,
61 uint32* value);
62extern char* EncodeVarint32(char* dst, uint32 v);
63extern char* EncodeVarint64(char* dst, uint64 v);
64
65// Returns the length of the varint32 or varint64 encoding of "v"
66extern int VarintLength(uint64_t v);
67
68} // namespace core
69} // namespace tsl
70
71#endif // TENSORFLOW_TSL_PLATFORM_CODING_H_
72