1 /*
2 * Copyright 2017 WebAssembly Community Group participants
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef WABT_LEB128_H_
18 #define WABT_LEB128_H_
19
20 #include <cstdint>
21
22 #include "src/common.h"
23
24 namespace wabt {
25
26 class Stream;
27
28 // Returns the length of the leb128.
29 Offset U32Leb128Length(uint32_t value);
30
31 void WriteU32Leb128(Stream* stream, uint32_t value, const char* desc);
32 void WriteS32Leb128(Stream* stream, uint32_t value, const char* desc);
33 void WriteS64Leb128(Stream* stream, uint64_t value, const char* desc);
34 void WriteFixedU32Leb128(Stream* stream, uint32_t value, const char* desc);
35
36 Offset WriteU32Leb128At(Stream* stream,
37 Offset offset,
38 uint32_t value,
39 const char* desc);
40
41 Offset WriteFixedU32Leb128At(Stream* stream,
42 Offset offset,
43 uint32_t value,
44 const char* desc);
45
46 Offset WriteU32Leb128Raw(uint8_t* data, uint8_t* end, uint32_t value);
47 Offset WriteFixedU32Leb128Raw(uint8_t* data, uint8_t* end, uint32_t value);
48
49 // Convenience functions for writing enums as LEB128s.
50 template <typename T>
WriteU32Leb128(Stream * stream,T value,const char * desc)51 void WriteU32Leb128(Stream* stream, T value, const char* desc) {
52 WriteU32Leb128(stream, static_cast<uint32_t>(value), desc);
53 }
54
55 template <typename T>
WriteS32Leb128(Stream * stream,T value,const char * desc)56 void WriteS32Leb128(Stream* stream, T value, const char* desc) {
57 WriteS32Leb128(stream, static_cast<uint32_t>(value), desc);
58 }
59
60 // Returns the length of the leb128.
61 size_t ReadU32Leb128(const uint8_t* p, const uint8_t* end, uint32_t* out_value);
62 size_t ReadS32Leb128(const uint8_t* p, const uint8_t* end, uint32_t* out_value);
63 size_t ReadS64Leb128(const uint8_t* p, const uint8_t* end, uint64_t* out_value);
64
65 } // namespace wabt
66
67 #endif // WABT_LEB128_H_
68