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

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