rsStream.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "rsContext.h"
  17. #include "rsStream.h"
  18. namespace android {
  19. namespace renderscript {
  20. IStream::IStream(const uint8_t *buf, bool use64) {
  21. mData = buf;
  22. mPos = 0;
  23. mUse64 = use64;
  24. }
  25. void IStream::loadByteArray(void *dest, size_t numBytes) {
  26. memcpy(dest, mData + mPos, numBytes);
  27. mPos += numBytes;
  28. }
  29. uint64_t IStream::loadOffset() {
  30. uint64_t tmp;
  31. if (mUse64) {
  32. mPos = (mPos + 7) & (~7);
  33. tmp = reinterpret_cast<const uint64_t *>(&mData[mPos])[0];
  34. mPos += sizeof(uint64_t);
  35. return tmp;
  36. }
  37. return loadU32();
  38. }
  39. const char * IStream::loadString() {
  40. uint32_t len = loadU32();
  41. const char *s = rsuCopyString((const char *)&mData[mPos], len);
  42. mPos += len;
  43. return s;
  44. }
  45. // Output stream implementation
  46. OStream::OStream(uint64_t len, bool use64) {
  47. mData = (uint8_t*)malloc(len);
  48. mLength = len;
  49. mPos = 0;
  50. mUse64 = use64;
  51. }
  52. OStream::~OStream() {
  53. free(mData);
  54. }
  55. void OStream::addByteArray(const void *src, size_t numBytes) {
  56. // We need to potentially grow more than once if the number of byes we write is substantial
  57. while (mPos + numBytes >= mLength) {
  58. growSize();
  59. }
  60. memcpy(mData + mPos, src, numBytes);
  61. mPos += numBytes;
  62. }
  63. void OStream::addOffset(uint64_t v) {
  64. if (mUse64) {
  65. mPos = (mPos + 7) & (~7);
  66. if (mPos + sizeof(v) >= mLength) {
  67. growSize();
  68. }
  69. mData[mPos++] = (uint8_t)(v & 0xff);
  70. mData[mPos++] = (uint8_t)((v >> 8) & 0xff);
  71. mData[mPos++] = (uint8_t)((v >> 16) & 0xff);
  72. mData[mPos++] = (uint8_t)((v >> 24) & 0xff);
  73. mData[mPos++] = (uint8_t)((v >> 32) & 0xff);
  74. mData[mPos++] = (uint8_t)((v >> 40) & 0xff);
  75. mData[mPos++] = (uint8_t)((v >> 48) & 0xff);
  76. mData[mPos++] = (uint8_t)((v >> 56) & 0xff);
  77. } else {
  78. addU32(v);
  79. }
  80. }
  81. void OStream::addString(const char *s, size_t len) {
  82. addU32(len);
  83. if (mPos + len*sizeof(char) >= mLength) {
  84. growSize();
  85. }
  86. char *stringData = reinterpret_cast<char *>(&mData[mPos]);
  87. memcpy(stringData, s, len);
  88. mPos += len*sizeof(char);
  89. }
  90. void OStream::addString(const char *s) {
  91. addString(s, strlen(s));
  92. }
  93. void OStream::growSize() {
  94. uint8_t *newData = (uint8_t*)malloc(mLength*2);
  95. memcpy(newData, mData, mLength*sizeof(uint8_t));
  96. mLength = mLength * 2;
  97. free(mData);
  98. mData = newData;
  99. }
  100. } // namespace renderscript
  101. } // namespace android