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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/InStream.h>
  26. #include <string.h> // for memcpy
  27. namespace rdr {
  28. class OutStream {
  29. protected:
  30. OutStream() {}
  31. public:
  32. virtual ~OutStream() {}
  33. // check() ensures there is buffer space for at least one item of size
  34. // itemSize bytes. Returns the number of items which fit (up to a maximum
  35. // of nItems).
  36. inline size_t check(size_t itemSize, size_t nItems=1)
  37. {
  38. size_t nAvail;
  39. if (itemSize > (size_t)(end - ptr))
  40. return overrun(itemSize, nItems);
  41. nAvail = (end - ptr) / itemSize;
  42. if (nAvail < nItems)
  43. return nAvail;
  44. return nItems;
  45. }
  46. // writeU/SN() methods write unsigned and signed N-bit integers.
  47. inline void writeU8( U8 u) { check(1); *ptr++ = u; }
  48. inline void writeU16(U16 u) { check(2); *ptr++ = u >> 8; *ptr++ = (U8)u; }
  49. inline void writeU32(U32 u) { check(4); *ptr++ = u >> 24; *ptr++ = u >> 16;
  50. *ptr++ = u >> 8; *ptr++ = u; }
  51. inline void writeS8( S8 s) { writeU8((U8)s); }
  52. inline void writeS16(S16 s) { writeU16((U16)s); }
  53. inline void writeS32(S32 s) { writeU32((U32)s); }
  54. // writeString() writes a string - a U32 length followed by the data. The
  55. // given string should be null-terminated (but the terminating null is not
  56. // written to the stream).
  57. inline void writeString(const char* str) {
  58. U32 len = strlen(str);
  59. writeU32(len);
  60. writeBytes(str, len);
  61. }
  62. inline void pad(size_t bytes) {
  63. while (bytes-- > 0) writeU8(0);
  64. }
  65. inline void skip(size_t bytes) {
  66. while (bytes > 0) {
  67. size_t n = check(1, bytes);
  68. ptr += n;
  69. bytes -= n;
  70. }
  71. }
  72. // writeBytes() writes an exact number of bytes.
  73. void writeBytes(const void* data, size_t length) {
  74. while (length > 0) {
  75. size_t n = check(1, length);
  76. memcpy(ptr, data, n);
  77. ptr += n;
  78. data = (U8*)data + n;
  79. length -= n;
  80. }
  81. }
  82. // copyBytes() efficiently transfers data between streams
  83. void copyBytes(InStream* is, size_t length) {
  84. while (length > 0) {
  85. size_t n = check(1, length);
  86. is->readBytes(ptr, n);
  87. ptr += n;
  88. length -= n;
  89. }
  90. }
  91. // writeOpaqueN() writes a quantity without byte-swapping.
  92. inline void writeOpaque8( U8 u) { writeU8(u); }
  93. inline void writeOpaque16(U16 u) { check(2); *ptr++ = ((U8*)&u)[0];
  94. *ptr++ = ((U8*)&u)[1]; }
  95. inline void writeOpaque32(U32 u) { check(4); *ptr++ = ((U8*)&u)[0];
  96. *ptr++ = ((U8*)&u)[1];
  97. *ptr++ = ((U8*)&u)[2];
  98. *ptr++ = ((U8*)&u)[3]; }
  99. // length() returns the length of the stream.
  100. virtual size_t length() = 0;
  101. // flush() requests that the stream be flushed.
  102. virtual void flush() {}
  103. // getptr(), getend() and setptr() are "dirty" methods which allow you to
  104. // manipulate the buffer directly. This is useful for a stream which is a
  105. // wrapper around an underlying stream.
  106. inline U8* getptr() { return ptr; }
  107. inline U8* getend() { return end; }
  108. inline void setptr(U8* p) { ptr = p; }
  109. private:
  110. // overrun() is implemented by a derived class to cope with buffer overrun.
  111. // It ensures there are at least itemSize bytes of buffer space. Returns
  112. // the number of items which fit (up to a maximum of nItems). itemSize is
  113. // supposed to be "small" (a few bytes).
  114. virtual size_t overrun(size_t itemSize, size_t nItems) = 0;
  115. protected:
  116. U8* ptr;
  117. U8* end;
  118. };
  119. }
  120. #endif