MemoryBuffer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "Exception.h"
  25. #include "MemoryBuffer.h"
  26. #include <cstring>
  27. #include "DebugNew.h"
  28. MemoryBuffer::MemoryBuffer(void* data, unsigned size) :
  29. Deserializer(size),
  30. mBuffer((unsigned char*)data),
  31. mReadOnly(false)
  32. {
  33. if ((size) && (!data))
  34. EXCEPTION("Null MemoryBuffer source");
  35. }
  36. MemoryBuffer::MemoryBuffer(const void* data, unsigned size) :
  37. Deserializer(size),
  38. mBuffer((unsigned char*)data),
  39. mReadOnly(true)
  40. {
  41. if ((size) && (!data))
  42. EXCEPTION("Null MemoryBuffer source");
  43. }
  44. void MemoryBuffer::read(void* dest, unsigned size)
  45. {
  46. if (!size)
  47. return;
  48. if (size + mPosition > mSize)
  49. SAFE_EXCEPTION("Attempted to read past buffer end");
  50. unsigned char* srcPtr = &mBuffer[mPosition];
  51. unsigned char* destPtr = (unsigned char*)dest;
  52. mPosition += size;
  53. while (size >= sizeof(unsigned))
  54. {
  55. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  56. srcPtr += sizeof(unsigned);
  57. destPtr += sizeof(unsigned);
  58. size -= sizeof(unsigned);
  59. }
  60. if (size & sizeof(unsigned short))
  61. {
  62. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  63. srcPtr += sizeof(unsigned short);
  64. destPtr += sizeof(unsigned short);
  65. }
  66. if (size & 1)
  67. *destPtr = *srcPtr;
  68. }
  69. unsigned MemoryBuffer::seek(unsigned position)
  70. {
  71. if (position > mSize)
  72. position = mSize;
  73. mPosition = position;
  74. return mPosition;
  75. }
  76. void MemoryBuffer::write(const void* data, unsigned size)
  77. {
  78. if (!size)
  79. return;
  80. if (mReadOnly)
  81. SAFE_EXCEPTION("Buffer is read only");
  82. if (size + mPosition > mSize)
  83. SAFE_EXCEPTION("Attempted to write past buffer end");
  84. unsigned char* srcPtr = (unsigned char*)data;
  85. unsigned char* destPtr = &mBuffer[mPosition];
  86. mPosition += size;
  87. while (size >= sizeof(unsigned))
  88. {
  89. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  90. srcPtr += sizeof(unsigned);
  91. destPtr += sizeof(unsigned);
  92. size -= sizeof(unsigned);
  93. }
  94. if (size & sizeof(unsigned short))
  95. {
  96. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  97. srcPtr += sizeof(unsigned short);
  98. destPtr += sizeof(unsigned short);
  99. }
  100. if (size & 1)
  101. *destPtr = *srcPtr;
  102. }