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