MemoryBuffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "MemoryBuffer.h"
  25. #include <cstring>
  26. #include "DebugNew.h"
  27. MemoryBuffer::MemoryBuffer(void* data, unsigned size) :
  28. Deserializer(size),
  29. buffer_((unsigned char*)data),
  30. readOnly_(false)
  31. {
  32. if (!buffer_)
  33. size_ = 0;
  34. }
  35. MemoryBuffer::MemoryBuffer(const void* data, unsigned size) :
  36. Deserializer(size),
  37. buffer_((unsigned char*)data),
  38. readOnly_(true)
  39. {
  40. if (!buffer_)
  41. size_ = 0;
  42. }
  43. MemoryBuffer::MemoryBuffer(Vector<unsigned char>& data) :
  44. Deserializer(data.Size()),
  45. buffer_(data.Size() ? &data[0] : 0),
  46. readOnly_(false)
  47. {
  48. }
  49. MemoryBuffer::MemoryBuffer(const Vector<unsigned char>& data) :
  50. Deserializer(data.Size()),
  51. buffer_(data.Size() ? const_cast<unsigned char*>(&data[0]) : 0),
  52. readOnly_(true)
  53. {
  54. }
  55. unsigned MemoryBuffer::Read(void* dest, unsigned size)
  56. {
  57. if (size + position_ > size_)
  58. size = size_ - position_;
  59. if (!size)
  60. return 0;
  61. unsigned char* srcPtr = &buffer_[position_];
  62. unsigned char* destPtr = (unsigned char*)dest;
  63. position_ += size;
  64. unsigned copySize = size;
  65. while (copySize >= sizeof(unsigned))
  66. {
  67. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  68. srcPtr += sizeof(unsigned);
  69. destPtr += sizeof(unsigned);
  70. copySize -= sizeof(unsigned);
  71. }
  72. if (copySize & sizeof(unsigned short))
  73. {
  74. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  75. srcPtr += sizeof(unsigned short);
  76. destPtr += sizeof(unsigned short);
  77. }
  78. if (copySize & 1)
  79. *destPtr = *srcPtr;
  80. return size;
  81. }
  82. unsigned MemoryBuffer::Seek(unsigned position)
  83. {
  84. if (position > size_)
  85. position = size_;
  86. position_ = position;
  87. return position_;
  88. }
  89. unsigned MemoryBuffer::Write(const void* data, unsigned size)
  90. {
  91. if (size + position_ > size_)
  92. size = size_ - position_;
  93. if (!size)
  94. return 0;
  95. unsigned char* srcPtr = (unsigned char*)data;
  96. unsigned char* destPtr = &buffer_[position_];
  97. position_ += size;
  98. unsigned copySize = size;
  99. while (copySize >= sizeof(unsigned))
  100. {
  101. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  102. srcPtr += sizeof(unsigned);
  103. destPtr += sizeof(unsigned);
  104. size -= sizeof(unsigned);
  105. }
  106. if (copySize & sizeof(unsigned short))
  107. {
  108. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  109. srcPtr += sizeof(unsigned short);
  110. destPtr += sizeof(unsigned short);
  111. }
  112. if (copySize & 1)
  113. *destPtr = *srcPtr;
  114. return size;
  115. }