SkBuffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkBuffer_DEFINED
  8. #define SkBuffer_DEFINED
  9. #include "include/core/SkScalar.h"
  10. #include "include/core/SkTypes.h"
  11. #include "include/private/SkNoncopyable.h"
  12. #include "src/core/SkSafeMath.h"
  13. #include <limits>
  14. /** \class SkRBuffer
  15. Light weight class for reading data from a memory block.
  16. The RBuffer is given the buffer to read from, with either a specified size
  17. or no size (in which case no range checking is performed). It is iillegal
  18. to attempt to read a value from an empty RBuffer (data == null).
  19. */
  20. class SkRBuffer : SkNoncopyable {
  21. public:
  22. SkRBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
  23. /** Initialize RBuffer with a data point and length.
  24. */
  25. SkRBuffer(const void* data, size_t size) {
  26. SkASSERT(data != nullptr || size == 0);
  27. fData = (const char*)data;
  28. fPos = (const char*)data;
  29. fStop = (const char*)data + size;
  30. }
  31. /** Return the number of bytes that have been read from the beginning
  32. of the data pointer.
  33. */
  34. size_t pos() const { return fPos - fData; }
  35. /** Return the total size of the data pointer. Only defined if the length was
  36. specified in the constructor or in a call to reset().
  37. */
  38. size_t size() const { return fStop - fData; }
  39. /** Return true if the buffer has read to the end of the data pointer.
  40. Only defined if the length was specified in the constructor or in a call
  41. to reset(). Always returns true if the length was not specified.
  42. */
  43. bool eof() const { return fPos >= fStop; }
  44. size_t available() const { return fStop - fPos; }
  45. bool isValid() const { return fValid; }
  46. /** Read the specified number of bytes from the data pointer. If buffer is not
  47. null, copy those bytes into buffer.
  48. */
  49. bool read(void* buffer, size_t size);
  50. bool skipToAlign4();
  51. bool readU8(uint8_t* x) { return this->read(x, 1); }
  52. bool readS32(int32_t* x) { return this->read(x, 4); }
  53. bool readU32(uint32_t* x) { return this->read(x, 4); }
  54. // returns nullptr on failure
  55. const void* skip(size_t bytes);
  56. template <typename T> const T* skipCount(size_t count) {
  57. return static_cast<const T*>(this->skip(SkSafeMath::Mul(count, sizeof(T))));
  58. }
  59. private:
  60. const char* fData;
  61. const char* fPos;
  62. const char* fStop;
  63. bool fValid = true;
  64. };
  65. /** \class SkWBuffer
  66. Light weight class for writing data to a memory block.
  67. The WBuffer is given the buffer to write into, with either a specified size
  68. or no size, in which case no range checking is performed. An empty WBuffer
  69. is legal, in which case no data is ever written, but the relative pos()
  70. is updated.
  71. */
  72. class SkWBuffer : SkNoncopyable {
  73. public:
  74. SkWBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
  75. SkWBuffer(void* data) { reset(data); }
  76. SkWBuffer(void* data, size_t size) { reset(data, size); }
  77. void reset(void* data) {
  78. fData = (char*)data;
  79. fPos = (char*)data;
  80. fStop = nullptr; // no bounds checking
  81. }
  82. void reset(void* data, size_t size) {
  83. SkASSERT(data != nullptr || size == 0);
  84. fData = (char*)data;
  85. fPos = (char*)data;
  86. fStop = (char*)data + size;
  87. }
  88. size_t pos() const { return fPos - fData; }
  89. void* skip(size_t size); // return start of skipped data
  90. void write(const void* buffer, size_t size) {
  91. if (size) {
  92. this->writeNoSizeCheck(buffer, size);
  93. }
  94. }
  95. size_t padToAlign4();
  96. void writePtr(const void* x) { this->writeNoSizeCheck(&x, sizeof(x)); }
  97. void writeScalar(SkScalar x) { this->writeNoSizeCheck(&x, 4); }
  98. void write32(int32_t x) { this->writeNoSizeCheck(&x, 4); }
  99. void write16(int16_t x) { this->writeNoSizeCheck(&x, 2); }
  100. void write8(int8_t x) { this->writeNoSizeCheck(&x, 1); }
  101. void writeBool(bool x) { this->write8(x); }
  102. private:
  103. void writeNoSizeCheck(const void* buffer, size_t size);
  104. char* fData;
  105. char* fPos;
  106. char* fStop;
  107. };
  108. #endif