Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2003 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::OutStream marshalls data into a buffer stored in RDR (RFB Data |
| 21 | // Representation). |
| 22 | // |
| 23 | |
| 24 | #ifndef __RDR_OUTSTREAM_H__ |
| 25 | #define __RDR_OUTSTREAM_H__ |
| 26 | |
| 27 | #include <rdr/types.h> |
Pierre Ossman | ded4907 | 2015-11-10 17:13:27 +0100 | [diff] [blame] | 28 | #include <rdr/InStream.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | #include <string.h> // for memcpy |
| 30 | |
| 31 | namespace rdr { |
| 32 | |
| 33 | class OutStream { |
| 34 | |
| 35 | protected: |
| 36 | |
| 37 | OutStream() {} |
| 38 | |
| 39 | public: |
| 40 | |
| 41 | virtual ~OutStream() {} |
| 42 | |
| 43 | // check() ensures there is buffer space for at least one item of size |
| 44 | // itemSize bytes. Returns the number of items which fit (up to a maximum |
| 45 | // of nItems). |
| 46 | |
| 47 | inline int check(int itemSize, int nItems=1) |
| 48 | { |
| 49 | if (ptr + itemSize * nItems > end) { |
| 50 | if (ptr + itemSize > end) |
| 51 | return overrun(itemSize, nItems); |
| 52 | |
| 53 | nItems = (end - ptr) / itemSize; |
| 54 | } |
| 55 | return nItems; |
| 56 | } |
| 57 | |
| 58 | // writeU/SN() methods write unsigned and signed N-bit integers. |
| 59 | |
| 60 | inline void writeU8( U8 u) { check(1); *ptr++ = u; } |
| 61 | inline void writeU16(U16 u) { check(2); *ptr++ = u >> 8; *ptr++ = (U8)u; } |
| 62 | inline void writeU32(U32 u) { check(4); *ptr++ = u >> 24; *ptr++ = u >> 16; |
| 63 | *ptr++ = u >> 8; *ptr++ = u; } |
| 64 | |
| 65 | inline void writeS8( S8 s) { writeU8((U8)s); } |
| 66 | inline void writeS16(S16 s) { writeU16((U16)s); } |
| 67 | inline void writeS32(S32 s) { writeU32((U32)s); } |
| 68 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | // writeString() writes a string - a U32 length followed by the data. The |
| 70 | // given string should be null-terminated (but the terminating null is not |
| 71 | // written to the stream). |
| 72 | |
| 73 | inline void writeString(const char* str) { |
| 74 | U32 len = strlen(str); |
| 75 | writeU32(len); |
| 76 | writeBytes(str, len); |
| 77 | } |
| 78 | |
| 79 | inline void pad(int bytes) { |
| 80 | while (bytes-- > 0) writeU8(0); |
| 81 | } |
| 82 | |
| 83 | inline void skip(int bytes) { |
| 84 | while (bytes > 0) { |
| 85 | int n = check(1, bytes); |
| 86 | ptr += n; |
| 87 | bytes -= n; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // writeBytes() writes an exact number of bytes. |
| 92 | |
Pierre Ossman | fbad8a9 | 2015-11-10 17:12:56 +0100 | [diff] [blame] | 93 | void writeBytes(const void* data, int length) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 94 | const U8* dataPtr = (const U8*)data; |
| 95 | const U8* dataEnd = dataPtr + length; |
| 96 | while (dataPtr < dataEnd) { |
| 97 | int n = check(1, dataEnd - dataPtr); |
| 98 | memcpy(ptr, dataPtr, n); |
| 99 | ptr += n; |
| 100 | dataPtr += n; |
| 101 | } |
| 102 | } |
| 103 | |
Pierre Ossman | ded4907 | 2015-11-10 17:13:27 +0100 | [diff] [blame] | 104 | // copyBytes() efficiently transfers data between streams |
| 105 | |
| 106 | void copyBytes(InStream* is, int length) { |
| 107 | while (length > 0) { |
| 108 | int n = check(1, length); |
| 109 | is->readBytes(ptr, n); |
| 110 | ptr += n; |
| 111 | length -= n; |
| 112 | } |
| 113 | } |
| 114 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 115 | // writeOpaqueN() writes a quantity without byte-swapping. |
| 116 | |
| 117 | inline void writeOpaque8( U8 u) { writeU8(u); } |
| 118 | inline void writeOpaque16(U16 u) { check(2); *ptr++ = ((U8*)&u)[0]; |
| 119 | *ptr++ = ((U8*)&u)[1]; } |
| 120 | inline void writeOpaque32(U32 u) { check(4); *ptr++ = ((U8*)&u)[0]; |
| 121 | *ptr++ = ((U8*)&u)[1]; |
| 122 | *ptr++ = ((U8*)&u)[2]; |
| 123 | *ptr++ = ((U8*)&u)[3]; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 124 | |
| 125 | // length() returns the length of the stream. |
| 126 | |
| 127 | virtual int length() = 0; |
| 128 | |
| 129 | // flush() requests that the stream be flushed. |
| 130 | |
| 131 | virtual void flush() {} |
| 132 | |
| 133 | // getptr(), getend() and setptr() are "dirty" methods which allow you to |
| 134 | // manipulate the buffer directly. This is useful for a stream which is a |
| 135 | // wrapper around an underlying stream. |
| 136 | |
| 137 | inline U8* getptr() { return ptr; } |
| 138 | inline U8* getend() { return end; } |
| 139 | inline void setptr(U8* p) { ptr = p; } |
| 140 | |
| 141 | private: |
| 142 | |
| 143 | // overrun() is implemented by a derived class to cope with buffer overrun. |
| 144 | // It ensures there are at least itemSize bytes of buffer space. Returns |
| 145 | // the number of items which fit (up to a maximum of nItems). itemSize is |
| 146 | // supposed to be "small" (a few bytes). |
| 147 | |
| 148 | virtual int overrun(int itemSize, int nItems) = 0; |
| 149 | |
| 150 | protected: |
| 151 | |
| 152 | U8* ptr; |
| 153 | U8* end; |
| 154 | }; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | #endif |