blob: 94d57dd310189bcc20d98c040ddfc3b4391bcb6b [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068 // writeString() writes a string - a U32 length followed by the data. The
69 // given string should be null-terminated (but the terminating null is not
70 // written to the stream).
71
72 inline void writeString(const char* str) {
73 U32 len = strlen(str);
74 writeU32(len);
75 writeBytes(str, len);
76 }
77
78 inline void pad(int bytes) {
79 while (bytes-- > 0) writeU8(0);
80 }
81
82 inline void skip(int bytes) {
83 while (bytes > 0) {
84 int n = check(1, bytes);
85 ptr += n;
86 bytes -= n;
87 }
88 }
89
90 // writeBytes() writes an exact number of bytes.
91
Pierre Ossmanfbad8a92015-11-10 17:12:56 +010092 void writeBytes(const void* data, int length) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093 const U8* dataPtr = (const U8*)data;
94 const U8* dataEnd = dataPtr + length;
95 while (dataPtr < dataEnd) {
96 int n = check(1, dataEnd - dataPtr);
97 memcpy(ptr, dataPtr, n);
98 ptr += n;
99 dataPtr += n;
100 }
101 }
102
103 // writeOpaqueN() writes a quantity without byte-swapping.
104
105 inline void writeOpaque8( U8 u) { writeU8(u); }
106 inline void writeOpaque16(U16 u) { check(2); *ptr++ = ((U8*)&u)[0];
107 *ptr++ = ((U8*)&u)[1]; }
108 inline void writeOpaque32(U32 u) { check(4); *ptr++ = ((U8*)&u)[0];
109 *ptr++ = ((U8*)&u)[1];
110 *ptr++ = ((U8*)&u)[2];
111 *ptr++ = ((U8*)&u)[3]; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112
113 // length() returns the length of the stream.
114
115 virtual int length() = 0;
116
117 // flush() requests that the stream be flushed.
118
119 virtual void flush() {}
120
121 // getptr(), getend() and setptr() are "dirty" methods which allow you to
122 // manipulate the buffer directly. This is useful for a stream which is a
123 // wrapper around an underlying stream.
124
125 inline U8* getptr() { return ptr; }
126 inline U8* getend() { return end; }
127 inline void setptr(U8* p) { ptr = p; }
128
129 private:
130
131 // overrun() is implemented by a derived class to cope with buffer overrun.
132 // It ensures there are at least itemSize bytes of buffer space. Returns
133 // the number of items which fit (up to a maximum of nItems). itemSize is
134 // supposed to be "small" (a few bytes).
135
136 virtual int overrun(int itemSize, int nItems) = 0;
137
138 protected:
139
140 U8* ptr;
141 U8* end;
142 };
143
144}
145
146#endif