MemoryBuffer.cpp 3.8 KB

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