MemoryBuffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  28. {
  29. MemoryBuffer::MemoryBuffer(void* data, unsigned size) :
  30. Deserializer(size),
  31. buffer_((unsigned char*)data),
  32. readOnly_(false)
  33. {
  34. if (!buffer_)
  35. size_ = 0;
  36. }
  37. MemoryBuffer::MemoryBuffer(const void* data, unsigned size) :
  38. Deserializer(size),
  39. buffer_((unsigned char*)data),
  40. readOnly_(true)
  41. {
  42. if (!buffer_)
  43. size_ = 0;
  44. }
  45. MemoryBuffer::MemoryBuffer(PODVector<unsigned char>& data) :
  46. Deserializer(data.Size()),
  47. buffer_(data.Size() ? &data[0] : 0),
  48. readOnly_(false)
  49. {
  50. }
  51. MemoryBuffer::MemoryBuffer(const PODVector<unsigned char>& data) :
  52. Deserializer(data.Size()),
  53. buffer_(data.Size() ? const_cast<unsigned char*>(&data[0]) : 0),
  54. readOnly_(true)
  55. {
  56. }
  57. unsigned MemoryBuffer::Read(void* dest, unsigned size)
  58. {
  59. if (size + position_ > size_)
  60. size = size_ - position_;
  61. if (!size)
  62. return 0;
  63. unsigned char* srcPtr = &buffer_[position_];
  64. unsigned char* destPtr = (unsigned char*)dest;
  65. position_ += size;
  66. unsigned copySize = size;
  67. while (copySize >= sizeof(unsigned))
  68. {
  69. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  70. srcPtr += sizeof(unsigned);
  71. destPtr += sizeof(unsigned);
  72. copySize -= sizeof(unsigned);
  73. }
  74. if (copySize & sizeof(unsigned short))
  75. {
  76. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  77. srcPtr += sizeof(unsigned short);
  78. destPtr += sizeof(unsigned short);
  79. }
  80. if (copySize & 1)
  81. *destPtr = *srcPtr;
  82. return size;
  83. }
  84. unsigned MemoryBuffer::Seek(unsigned position)
  85. {
  86. if (position > size_)
  87. position = size_;
  88. position_ = position;
  89. return position_;
  90. }
  91. unsigned MemoryBuffer::Write(const void* data, unsigned size)
  92. {
  93. if (size + position_ > size_)
  94. size = size_ - position_;
  95. if (!size)
  96. return 0;
  97. unsigned char* srcPtr = (unsigned char*)data;
  98. unsigned char* destPtr = &buffer_[position_];
  99. position_ += size;
  100. unsigned copySize = size;
  101. while (copySize >= sizeof(unsigned))
  102. {
  103. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  104. srcPtr += sizeof(unsigned);
  105. destPtr += sizeof(unsigned);
  106. copySize -= sizeof(unsigned);
  107. }
  108. if (copySize & sizeof(unsigned short))
  109. {
  110. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  111. srcPtr += sizeof(unsigned short);
  112. destPtr += sizeof(unsigned short);
  113. }
  114. if (copySize & 1)
  115. *destPtr = *srcPtr;
  116. return size;
  117. }
  118. }