Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // |
| 20 | // rdr::InStream marshalls data from a buffer stored in RDR (RFB Data |
| 21 | // Representation). |
| 22 | // |
| 23 | |
| 24 | #ifndef __RDR_INSTREAM_H__ |
| 25 | #define __RDR_INSTREAM_H__ |
| 26 | |
| 27 | #include <rdr/types.h> |
| 28 | #include <string.h> // for memcpy |
| 29 | |
| 30 | namespace rdr { |
| 31 | |
| 32 | class InStream { |
| 33 | |
| 34 | public: |
| 35 | |
| 36 | virtual ~InStream() {} |
| 37 | |
| 38 | // check() ensures there is buffer data for at least one item of size |
| 39 | // itemSize bytes. Returns the number of items in the buffer (up to a |
| 40 | // maximum of nItems). If wait is false, then instead of blocking to wait |
| 41 | // for the bytes, zero is returned if the bytes are not immediately |
| 42 | // available. |
| 43 | |
| 44 | inline int check(int itemSize, int nItems=1, bool wait=true) |
| 45 | { |
| 46 | if (ptr + itemSize * nItems > end) { |
| 47 | if (ptr + itemSize > end) |
| 48 | return overrun(itemSize, nItems, wait); |
| 49 | |
| 50 | nItems = (end - ptr) / itemSize; |
| 51 | } |
| 52 | return nItems; |
| 53 | } |
| 54 | |
| 55 | // checkNoWait() tries to make sure that the given number of bytes can |
| 56 | // be read without blocking. It returns true if this is the case, false |
| 57 | // otherwise. The length must be "small" (less than the buffer size). |
| 58 | |
| 59 | inline bool checkNoWait(int length) { return check(length, 1, false)!=0; } |
| 60 | |
| 61 | // readU/SN() methods read unsigned and signed N-bit integers. |
| 62 | |
| 63 | inline U8 readU8() { check(1); return *ptr++; } |
| 64 | inline U16 readU16() { check(2); int b0 = *ptr++; int b1 = *ptr++; |
| 65 | return b0 << 8 | b1; } |
| 66 | inline U32 readU32() { check(4); int b0 = *ptr++; int b1 = *ptr++; |
| 67 | int b2 = *ptr++; int b3 = *ptr++; |
| 68 | return b0 << 24 | b1 << 16 | b2 << 8 | b3; } |
| 69 | |
| 70 | inline S8 readS8() { return (S8) readU8(); } |
| 71 | inline S16 readS16() { return (S16)readU16(); } |
| 72 | inline S32 readS32() { return (S32)readU32(); } |
| 73 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 74 | // readString() reads a string - a U32 length followed by the data. |
| 75 | // Returns a null-terminated string - the caller should delete[] it |
| 76 | // afterwards. |
| 77 | |
| 78 | char* readString(); |
| 79 | |
| 80 | // maxStringLength protects against allocating a huge buffer. Set it |
| 81 | // higher if you need longer strings. |
| 82 | |
| 83 | static U32 maxStringLength; |
| 84 | |
| 85 | inline void skip(int bytes) { |
| 86 | while (bytes > 0) { |
| 87 | int n = check(1, bytes); |
| 88 | ptr += n; |
| 89 | bytes -= n; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // readBytes() reads an exact number of bytes. |
| 94 | |
Pierre Ossman | fbad8a9 | 2015-11-10 17:12:56 +0100 | [diff] [blame] | 95 | void readBytes(void* data, int length) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | U8* dataPtr = (U8*)data; |
| 97 | U8* dataEnd = dataPtr + length; |
| 98 | while (dataPtr < dataEnd) { |
| 99 | int n = check(1, dataEnd - dataPtr); |
| 100 | memcpy(dataPtr, ptr, n); |
| 101 | ptr += n; |
| 102 | dataPtr += n; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // readOpaqueN() reads a quantity without byte-swapping. |
| 107 | |
| 108 | inline U8 readOpaque8() { return readU8(); } |
| 109 | inline U16 readOpaque16() { check(2); U16 r; ((U8*)&r)[0] = *ptr++; |
| 110 | ((U8*)&r)[1] = *ptr++; return r; } |
| 111 | inline U32 readOpaque32() { check(4); U32 r; ((U8*)&r)[0] = *ptr++; |
| 112 | ((U8*)&r)[1] = *ptr++; ((U8*)&r)[2] = *ptr++; |
| 113 | ((U8*)&r)[3] = *ptr++; return r; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 114 | |
| 115 | // pos() returns the position in the stream. |
| 116 | |
| 117 | virtual int pos() = 0; |
| 118 | |
| 119 | // getptr(), getend() and setptr() are "dirty" methods which allow you to |
| 120 | // manipulate the buffer directly. This is useful for a stream which is a |
| 121 | // wrapper around an underlying stream. |
| 122 | |
| 123 | inline const U8* getptr() const { return ptr; } |
| 124 | inline const U8* getend() const { return end; } |
| 125 | inline void setptr(const U8* p) { ptr = p; } |
| 126 | |
| 127 | private: |
| 128 | |
| 129 | // overrun() is implemented by a derived class to cope with buffer overrun. |
| 130 | // It ensures there are at least itemSize bytes of buffer data. Returns |
| 131 | // the number of items in the buffer (up to a maximum of nItems). itemSize |
| 132 | // is supposed to be "small" (a few bytes). If wait is false, then |
| 133 | // instead of blocking to wait for the bytes, zero is returned if the bytes |
| 134 | // are not immediately available. |
| 135 | |
| 136 | virtual int overrun(int itemSize, int nItems, bool wait=true) = 0; |
| 137 | |
| 138 | protected: |
| 139 | |
| 140 | InStream() {} |
| 141 | const U8* ptr; |
| 142 | const U8* end; |
| 143 | }; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | #endif |