1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 __NOGROD_BYTE_INPUT_STREAM_
18 #define __NOGROD_BYTE_INPUT_STREAM_
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 #include <vector>
24 
25 namespace nogrod {
26 
27 class ByteInputStream {
28  public:
29   ByteInputStream(const uint8_t* buffer, size_t size);
30 
31   [[nodiscard]] bool available() const;
32   [[nodiscard]] uint64_t offset() const;
33 
34   [[nodiscard]] uint8_t ReadUint8();
35   [[nodiscard]] uint16_t ReadUint16();
36   [[nodiscard]] uint32_t ReadUint24();
37   [[nodiscard]] uint32_t ReadUint32();
38   [[nodiscard]] uint64_t ReadUint64();
39   [[nodiscard]] uint64_t ReadLeb128();
40   [[nodiscard]] int64_t ReadSleb128();
41   [[nodiscard]] std::string ReadString();
42   [[nodiscard]] std::vector<uint8_t> ReadBytes(uint64_t size);
43 
44  private:
45   const uint8_t* buffer_;
46   uint64_t size_;
47   uint64_t offset_;
48 };
49 
50 };  // namespace nogrod
51 
52 #endif  // __NOGROD_BYTE_INPUT_STREAM_
53