You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OutStream.h 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // rdr::OutStream marshalls data into a buffer stored in RDR (RFB Data
  20. // Representation).
  21. //
  22. #ifndef __RDR_OUTSTREAM_H__
  23. #define __RDR_OUTSTREAM_H__
  24. #include <rdr/types.h>
  25. #include <rdr/Exception.h>
  26. #include <rdr/InStream.h>
  27. #include <string.h> // for memcpy
  28. namespace rdr {
  29. class OutStream {
  30. protected:
  31. OutStream() : ptr(NULL), end(NULL), corked(false) {}
  32. public:
  33. virtual ~OutStream() {}
  34. // avail() returns the number of bytes that currently be written to the
  35. // stream without any risk of blocking.
  36. inline size_t avail()
  37. {
  38. return end - ptr;
  39. }
  40. // writeU/SN() methods write unsigned and signed N-bit integers.
  41. inline void writeU8( U8 u) { check(1); *ptr++ = u; }
  42. inline void writeU16(U16 u) { check(2); *ptr++ = u >> 8; *ptr++ = (U8)u; }
  43. inline void writeU32(U32 u) { check(4); *ptr++ = u >> 24; *ptr++ = u >> 16;
  44. *ptr++ = u >> 8; *ptr++ = u; }
  45. inline void writeS8( S8 s) { writeU8((U8)s); }
  46. inline void writeS16(S16 s) { writeU16((U16)s); }
  47. inline void writeS32(S32 s) { writeU32((U32)s); }
  48. inline void pad(size_t bytes) {
  49. while (bytes-- > 0) writeU8(0);
  50. }
  51. // writeBytes() writes an exact number of bytes.
  52. void writeBytes(const void* data, size_t length) {
  53. while (length > 0) {
  54. check(1);
  55. size_t n = length;
  56. if (length > avail())
  57. n = avail();
  58. memcpy(ptr, data, n);
  59. ptr += n;
  60. data = (U8*)data + n;
  61. length -= n;
  62. }
  63. }
  64. // copyBytes() efficiently transfers data between streams
  65. void copyBytes(InStream* is, size_t length) {
  66. while (length > 0) {
  67. check(1);
  68. size_t n = length;
  69. if (length > avail())
  70. n = avail();
  71. is->readBytes(ptr, n);
  72. ptr += n;
  73. length -= n;
  74. }
  75. }
  76. // writeOpaqueN() writes a quantity without byte-swapping.
  77. inline void writeOpaque8( U8 u) { writeU8(u); }
  78. inline void writeOpaque16(U16 u) { check(2); *ptr++ = ((U8*)&u)[0];
  79. *ptr++ = ((U8*)&u)[1]; }
  80. inline void writeOpaque32(U32 u) { check(4); *ptr++ = ((U8*)&u)[0];
  81. *ptr++ = ((U8*)&u)[1];
  82. *ptr++ = ((U8*)&u)[2];
  83. *ptr++ = ((U8*)&u)[3]; }
  84. // length() returns the length of the stream.
  85. virtual size_t length() = 0;
  86. // flush() requests that the stream be flushed.
  87. virtual void flush() {}
  88. // cork() requests that the stream coalesces flushes in an efficient way
  89. virtual void cork(bool enable) { corked = enable; flush(); }
  90. // getptr() and setptr() are "dirty" methods which allow you direct access
  91. // to the buffer. This is useful for a stream which is a wrapper around an
  92. // some other stream API. Note that setptr() should not called with a value
  93. // larger than the bytes actually written as doing so can result in
  94. // security issues. Use pad() in such cases instead.
  95. inline U8* getptr(size_t length) { check(length); return ptr; }
  96. inline void setptr(size_t length) { if (length > avail())
  97. throw Exception("Output stream overflow");
  98. ptr += length; }
  99. private:
  100. inline void check(size_t length)
  101. {
  102. if (length > avail())
  103. overrun(length);
  104. }
  105. // overrun() is implemented by a derived class to cope with buffer overrun.
  106. // It ensures there are at least needed bytes of buffer space.
  107. virtual void overrun(size_t needed) = 0;
  108. protected:
  109. U8* ptr;
  110. U8* end;
  111. bool corked;
  112. };
  113. }
  114. #endif