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