blob: aed2eea189fd366538a57fa47d7c630c2a94d485 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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>
28#include <string.h> // for memcpy
29
30namespace rdr {
31
32 class OutStream {
33
34 protected:
35
36 OutStream() {}
37
38 public:
39
40 virtual ~OutStream() {}
41
42 // check() ensures there is buffer space for at least one item of size
43 // itemSize bytes. Returns the number of items which fit (up to a maximum
44 // of nItems).
45
46 inline int check(int itemSize, int nItems=1)
47 {
48 if (ptr + itemSize * nItems > end) {
49 if (ptr + itemSize > end)
50 return overrun(itemSize, nItems);
51
52 nItems = (end - ptr) / itemSize;
53 }
54 return nItems;
55 }
56
57 // writeU/SN() methods write unsigned and signed N-bit integers.
58
59 inline void writeU8( U8 u) { check(1); *ptr++ = u; }
60 inline void writeU16(U16 u) { check(2); *ptr++ = u >> 8; *ptr++ = (U8)u; }
61 inline void writeU32(U32 u) { check(4); *ptr++ = u >> 24; *ptr++ = u >> 16;
62 *ptr++ = u >> 8; *ptr++ = u; }
63
64 inline void writeS8( S8 s) { writeU8((U8)s); }
65 inline void writeS16(S16 s) { writeU16((U16)s); }
66 inline void writeS32(S32 s) { writeU32((U32)s); }
67
68 // writeCompactLength() writes 1..3 bytes representing length of the data
69 // following. This method is used by the Tight encoder.
70
71 inline void writeCompactLength(unsigned int len) {
72 U8 b = len & 0x7F;
73 if (len <= 0x7F) {
74 writeU8(b);
75 } else {
76 writeU8(b | 0x80);
77 b = len >> 7 & 0x7F;
78 if (len <= 0x3FFF) {
79 writeU8(b);
80 } else {
81 writeU8(b | 0x80);
82 writeU8(len >> 14 & 0xFF);
83 }
84 }
85 }
86
87 // writeString() writes a string - a U32 length followed by the data. The
88 // given string should be null-terminated (but the terminating null is not
89 // written to the stream).
90
91 inline void writeString(const char* str) {
92 U32 len = strlen(str);
93 writeU32(len);
94 writeBytes(str, len);
95 }
96
97 inline void pad(int bytes) {
98 while (bytes-- > 0) writeU8(0);
99 }
100
101 inline void skip(int bytes) {
102 while (bytes > 0) {
103 int n = check(1, bytes);
104 ptr += n;
105 bytes -= n;
106 }
107 }
108
109 // writeBytes() writes an exact number of bytes.
110
111 virtual void writeBytes(const void* data, int length) {
112 const U8* dataPtr = (const U8*)data;
113 const U8* dataEnd = dataPtr + length;
114 while (dataPtr < dataEnd) {
115 int n = check(1, dataEnd - dataPtr);
116 memcpy(ptr, dataPtr, n);
117 ptr += n;
118 dataPtr += n;
119 }
120 }
121
122 // writeOpaqueN() writes a quantity without byte-swapping.
123
124 inline void writeOpaque8( U8 u) { writeU8(u); }
125 inline void writeOpaque16(U16 u) { check(2); *ptr++ = ((U8*)&u)[0];
126 *ptr++ = ((U8*)&u)[1]; }
127 inline void writeOpaque32(U32 u) { check(4); *ptr++ = ((U8*)&u)[0];
128 *ptr++ = ((U8*)&u)[1];
129 *ptr++ = ((U8*)&u)[2];
130 *ptr++ = ((U8*)&u)[3]; }
131 inline void writeOpaque24A(U32 u) { check(3); *ptr++ = ((U8*)&u)[0];
132 *ptr++ = ((U8*)&u)[1];
133 *ptr++ = ((U8*)&u)[2]; }
134 inline void writeOpaque24B(U32 u) { check(3); *ptr++ = ((U8*)&u)[1];
135 *ptr++ = ((U8*)&u)[2];
136 *ptr++ = ((U8*)&u)[3]; }
137
138 // length() returns the length of the stream.
139
140 virtual int length() = 0;
141
142 // flush() requests that the stream be flushed.
143
144 virtual void flush() {}
145
146 // getptr(), getend() and setptr() are "dirty" methods which allow you to
147 // manipulate the buffer directly. This is useful for a stream which is a
148 // wrapper around an underlying stream.
149
150 inline U8* getptr() { return ptr; }
151 inline U8* getend() { return end; }
152 inline void setptr(U8* p) { ptr = p; }
153
154 private:
155
156 // overrun() is implemented by a derived class to cope with buffer overrun.
157 // It ensures there are at least itemSize bytes of buffer space. Returns
158 // the number of items which fit (up to a maximum of nItems). itemSize is
159 // supposed to be "small" (a few bytes).
160
161 virtual int overrun(int itemSize, int nItems) = 0;
162
163 protected:
164
165 U8* ptr;
166 U8* end;
167 };
168
169}
170
171#endif