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.

InStream.h 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (C) 2002-2005 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::InStream marshalls data from a buffer stored in RDR (RFB Data
  20. // Representation).
  21. //
  22. #ifndef __RDR_INSTREAM_H__
  23. #define __RDR_INSTREAM_H__
  24. #include <rdr/types.h>
  25. #include <rdr/Exception.h>
  26. #include <string.h> // for memcpy
  27. namespace rdr {
  28. class InStream {
  29. public:
  30. virtual ~InStream() {}
  31. // avail() returns the number of bytes that are currenctly directly
  32. // available from the stream.
  33. inline size_t avail()
  34. {
  35. return end - ptr;
  36. }
  37. // check() ensures there is buffer data for at least needed bytes. Returns
  38. // true once the data is available. If wait is false, then instead of
  39. // blocking to wait for the bytes, false is returned if the bytes are not
  40. // immediately available.
  41. inline size_t check(size_t needed, bool wait=true)
  42. {
  43. if (needed > avail())
  44. return overrun(needed, wait);
  45. return true;
  46. }
  47. // checkNoWait() tries to make sure that the given number of bytes can
  48. // be read without blocking. It returns true if this is the case, false
  49. // otherwise. The length must be "small" (less than the buffer size).
  50. inline bool checkNoWait(size_t length) { return check(length, false); }
  51. // readU/SN() methods read unsigned and signed N-bit integers.
  52. inline U8 readU8() { check(1); return *ptr++; }
  53. inline U16 readU16() { check(2); int b0 = *ptr++; int b1 = *ptr++;
  54. return b0 << 8 | b1; }
  55. inline U32 readU32() { check(4); int b0 = *ptr++; int b1 = *ptr++;
  56. int b2 = *ptr++; int b3 = *ptr++;
  57. return b0 << 24 | b1 << 16 | b2 << 8 | b3; }
  58. inline S8 readS8() { return (S8) readU8(); }
  59. inline S16 readS16() { return (S16)readU16(); }
  60. inline S32 readS32() { return (S32)readU32(); }
  61. inline void skip(size_t bytes) {
  62. while (bytes > 0) {
  63. size_t n = check(1, bytes);
  64. ptr += n;
  65. bytes -= n;
  66. }
  67. }
  68. // readBytes() reads an exact number of bytes.
  69. void readBytes(void* data, size_t length) {
  70. while (length > 0) {
  71. size_t n = check(1, length);
  72. memcpy(data, ptr, n);
  73. ptr += n;
  74. data = (U8*)data + n;
  75. length -= n;
  76. }
  77. }
  78. // readOpaqueN() reads a quantity without byte-swapping.
  79. inline U8 readOpaque8() { return readU8(); }
  80. inline U16 readOpaque16() { check(2); U16 r; ((U8*)&r)[0] = *ptr++;
  81. ((U8*)&r)[1] = *ptr++; return r; }
  82. inline U32 readOpaque32() { check(4); U32 r; ((U8*)&r)[0] = *ptr++;
  83. ((U8*)&r)[1] = *ptr++; ((U8*)&r)[2] = *ptr++;
  84. ((U8*)&r)[3] = *ptr++; return r; }
  85. // pos() returns the position in the stream.
  86. virtual size_t pos() = 0;
  87. // getptr() and setptr() are "dirty" methods which allow you direct access
  88. // to the buffer. This is useful for a stream which is a wrapper around an
  89. // some other stream API.
  90. inline const U8* getptr(size_t length) { check(length); return ptr; }
  91. inline void setptr(size_t length) { if (length > avail())
  92. throw Exception("Input stream overflow");
  93. skip(length); }
  94. private:
  95. // overrun() is implemented by a derived class to cope with buffer overrun.
  96. // It ensures there are at least needed bytes of buffer data. Returns true
  97. // once the data is available. If wait is false, then instead of blocking
  98. // to wait for the bytes, false is returned if the bytes are not
  99. // immediately available.
  100. virtual bool overrun(size_t needed, bool wait=true) = 0;
  101. protected:
  102. InStream() {}
  103. const U8* ptr;
  104. const U8* end;
  105. };
  106. }
  107. #endif