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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <stdint.h>
  25. #include <string.h> // for memcpy
  26. #include <rdr/Exception.h>
  27. #include <rdr/InStream.h>
  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( uint8_t u) { check(1); *ptr++ = u; }
  42. inline void writeU16(uint16_t u) { check(2); *ptr++ = u >> 8;
  43. *ptr++ = (uint8_t)u; }
  44. inline void writeU32(uint32_t u) { check(4); *ptr++ = u >> 24;
  45. *ptr++ = u >> 16;
  46. *ptr++ = u >> 8;
  47. *ptr++ = u; }
  48. inline void writeS8( int8_t s) { writeU8((uint8_t)s); }
  49. inline void writeS16(int16_t s) { writeU16((uint16_t)s); }
  50. inline void writeS32(int32_t s) { writeU32((uint32_t)s); }
  51. inline void pad(size_t bytes) {
  52. while (bytes-- > 0) writeU8(0);
  53. }
  54. // writeBytes() writes an exact number of bytes.
  55. void writeBytes(const uint8_t* data, size_t length) {
  56. while (length > 0) {
  57. check(1);
  58. size_t n = length;
  59. if (length > avail())
  60. n = avail();
  61. memcpy(ptr, data, n);
  62. ptr += n;
  63. data = (uint8_t*)data + n;
  64. length -= n;
  65. }
  66. }
  67. // copyBytes() efficiently transfers data between streams
  68. void copyBytes(InStream* is, size_t length) {
  69. while (length > 0) {
  70. check(1);
  71. size_t n = length;
  72. if (length > avail())
  73. n = avail();
  74. is->readBytes(ptr, n);
  75. ptr += n;
  76. length -= n;
  77. }
  78. }
  79. // writeOpaqueN() writes a quantity without byte-swapping.
  80. inline void writeOpaque8( uint8_t u) { writeU8(u); }
  81. inline void writeOpaque16(uint16_t u) { check(2);
  82. *ptr++ = ((uint8_t*)&u)[0];
  83. *ptr++ = ((uint8_t*)&u)[1]; }
  84. inline void writeOpaque32(uint32_t u) { check(4);
  85. *ptr++ = ((uint8_t*)&u)[0];
  86. *ptr++ = ((uint8_t*)&u)[1];
  87. *ptr++ = ((uint8_t*)&u)[2];
  88. *ptr++ = ((uint8_t*)&u)[3]; }
  89. // length() returns the length of the stream.
  90. virtual size_t length() = 0;
  91. // flush() requests that the stream be flushed.
  92. virtual void flush() {}
  93. // cork() requests that the stream coalesces flushes in an efficient way
  94. virtual void cork(bool enable) { corked = enable; if (!enable) flush(); }
  95. // getptr() and setptr() are "dirty" methods which allow you direct access
  96. // to the buffer. This is useful for a stream which is a wrapper around an
  97. // some other stream API. Note that setptr() should not called with a value
  98. // larger than the bytes actually written as doing so can result in
  99. // security issues. Use pad() in such cases instead.
  100. inline uint8_t* getptr(size_t length) { check(length); return ptr; }
  101. inline void setptr(size_t length) { if (length > avail())
  102. throw Exception("Output stream overflow");
  103. ptr += length; }
  104. private:
  105. inline void check(size_t length)
  106. {
  107. if (length > avail())
  108. overrun(length);
  109. }
  110. // overrun() is implemented by a derived class to cope with buffer overrun.
  111. // It ensures there are at least needed bytes of buffer space.
  112. virtual void overrun(size_t needed) = 0;
  113. protected:
  114. uint8_t* ptr;
  115. uint8_t* end;
  116. bool corked;
  117. };
  118. }
  119. #endif