• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Android Open Source Project
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 #pragma once
16 
17 #include "aemu/base/msvc.h"
18 
19 #include <string>
20 
21 #include <inttypes.h>
22 #include <sys/types.h>
23 
24 namespace android {
25 namespace base {
26 
27 // Abstract interface to byte streams of all kind.
28 // This is mainly used to implement disk serialization.
29 class Stream {
30 public:
31     // Default constructor.
32     Stream() = default;
33 
34     // Destructor.
35     virtual ~Stream() = default;
36 
37     // Read up to |size| bytes and copy them to |buffer|. Return the number
38     // of bytes that were actually transferred, or -errno value on error.
39     virtual ssize_t read(void* buffer, size_t size) = 0;
40 
41     // Write up to |size| bytes from |buffer| into the stream. Return the
42     // number of bytes that were actually transferred, or -errno value on
43     // error.
44     virtual ssize_t write(const void* buffer, size_t size) = 0;
45 
getProtobuf()46     virtual void* getProtobuf() { return nullptr; }
47 
48     // Write a single byte |value| into the stream. Ignore errors.
49     void putByte(uint8_t value);
50 
51     // Write a 16-bit |value| as big-endian into the stream. Ignore errors.
52     void putBe16(uint16_t value);
53 
54     // Write a 32-bit |value| as big-endian into the stream. Ignore errors.
55     void putBe32(uint32_t value);
56 
57     // Write a 64-bit |value| as big-endian into the stream. Ignore errors.
58     void putBe64(uint64_t value);
59 
60     // Read a single byte from the stream. Return 0 on error.
61     uint8_t getByte();
62 
63     // Read a single big-endian 16-bit value from the stream.
64     // Return 0 on error.
65     uint16_t getBe16();
66 
67     // Read a single big-endian 32-bit value from the stream.
68     // Return 0 on error.
69     uint32_t getBe32();
70 
71     // Read a single big-endian 64-bit value from the stream.
72     // Return 0 on error.
73     uint64_t getBe64();
74 
75     // Write a 32-bit float |value| to the stream.
76     void putFloat(float value);
77 
78     // Read a single 32-bit float value from the stream.
79     float getFloat();
80 
81     // Write a 0-terminated C string |str| into the stream. Ignore error.
82     void putString(const char* str);
83     void putString(const std::string& str);
84 
85     // Write a string |str| of |strlen| bytes into the stream.
86     // Ignore errors.
87     void putString(const char* str, size_t strlen);
88 
89     // Read a string from the stream. Return a new string instance,
90     // which will be empty on error. Note that this can only be used
91     // to read strings that were written with putString().
92     std::string getString();
93 
94     // Put/gen an integer number into the stream, making it use as little space
95     // there as possible.
96     // It uses a simple byte-by-byte encoding scheme, putting 7 bits of the
97     // number with the 8th bit set when there's more data to read, until the
98     // whole number is read.
99     // The compression is efficient if the number range is small, but it starts
100     // wasting space when values approach 14 bits for int16 (16K), 28 bits for
101     // int32 (268M) or 56 bits for int64 (still a lot).
102     void putPackedNum(uint64_t num);
103     uint64_t getPackedNum();
104 
105     // Same thing, but encode negative numbers efficiently as well (single sign
106     // bit + packed unsigned representation)
107     void putPackedSignedNum(int64_t num);
108     int64_t getPackedSignedNum();
109 
110     // Static big-endian conversions
111     static void toByte(uint8_t*);
112     static void toBe16(uint8_t*);
113     static void toBe32(uint8_t*);
114     static void toBe64(uint8_t*);
115     static void fromByte(uint8_t*);
116     static void fromBe16(uint8_t*);
117     static void fromBe32(uint8_t*);
118     static void fromBe64(uint8_t*);
119 };
120 
121 }  // namespace base
122 }  // namespace android
123