blob: 6d22ac6af509f9305f729435bd93274a81458a6f [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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
30namespace 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
74 // readCompactLength() reads 1..3 bytes representing length of the data
75 // following. This method is used by the Tight decoder.
76
77 inline unsigned int readCompactLength() {
78 U8 b = readU8();
79 int result = (int)b & 0x7F;
80 if (b & 0x80) {
81 b = readU8();
82 result |= ((int)b & 0x7F) << 7;
83 if (b & 0x80) {
84 b = readU8();
85 result |= ((int)b & 0xFF) << 14;
86 }
87 }
88 return result;
89 }
90
91 // readString() reads a string - a U32 length followed by the data.
92 // Returns a null-terminated string - the caller should delete[] it
93 // afterwards.
94
95 char* readString();
96
97 // maxStringLength protects against allocating a huge buffer. Set it
98 // higher if you need longer strings.
99
100 static U32 maxStringLength;
101
102 inline void skip(int bytes) {
103 while (bytes > 0) {
104 int n = check(1, bytes);
105 ptr += n;
106 bytes -= n;
107 }
108 }
109
110 // readBytes() reads an exact number of bytes.
111
112 virtual void readBytes(void* data, int length) {
113 U8* dataPtr = (U8*)data;
114 U8* dataEnd = dataPtr + length;
115 while (dataPtr < dataEnd) {
116 int n = check(1, dataEnd - dataPtr);
117 memcpy(dataPtr, ptr, n);
118 ptr += n;
119 dataPtr += n;
120 }
121 }
122
123 // readOpaqueN() reads a quantity without byte-swapping.
124
125 inline U8 readOpaque8() { return readU8(); }
126 inline U16 readOpaque16() { check(2); U16 r; ((U8*)&r)[0] = *ptr++;
127 ((U8*)&r)[1] = *ptr++; return r; }
128 inline U32 readOpaque32() { check(4); U32 r; ((U8*)&r)[0] = *ptr++;
129 ((U8*)&r)[1] = *ptr++; ((U8*)&r)[2] = *ptr++;
130 ((U8*)&r)[3] = *ptr++; return r; }
131 inline U32 readOpaque24A() { check(3); U32 r=0; ((U8*)&r)[0] = *ptr++;
132 ((U8*)&r)[1] = *ptr++; ((U8*)&r)[2] = *ptr++;
133 return r; }
134 inline U32 readOpaque24B() { check(3); U32 r=0; ((U8*)&r)[1] = *ptr++;
135 ((U8*)&r)[2] = *ptr++; ((U8*)&r)[3] = *ptr++;
136 return r; }
137
138 // pos() returns the position in the stream.
139
140 virtual int pos() = 0;
141
142 // getptr(), getend() and setptr() are "dirty" methods which allow you to
143 // manipulate the buffer directly. This is useful for a stream which is a
144 // wrapper around an underlying stream.
145
146 inline const U8* getptr() const { return ptr; }
147 inline const U8* getend() const { return end; }
148 inline void setptr(const U8* p) { ptr = p; }
149
150 private:
151
152 // overrun() is implemented by a derived class to cope with buffer overrun.
153 // It ensures there are at least itemSize bytes of buffer data. Returns
154 // the number of items in the buffer (up to a maximum of nItems). itemSize
155 // is supposed to be "small" (a few bytes). If wait is false, then
156 // instead of blocking to wait for the bytes, zero is returned if the bytes
157 // are not immediately available.
158
159 virtual int overrun(int itemSize, int nItems, bool wait=true) = 0;
160
161 protected:
162
163 InStream() {}
164 const U8* ptr;
165 const U8* end;
166 };
167
168}
169
170#endif