serializable.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2014 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 <keymaster/serializable.h>
  17. #include <assert.h>
  18. #include <keymaster/new>
  19. #include <keymaster/android_keymaster_utils.h>
  20. namespace keymaster {
  21. uint8_t* append_to_buf(uint8_t* buf, const uint8_t* end, const void* data, size_t data_len) {
  22. if (__pval(buf) + data_len < __pval(buf)) // Pointer wrap check
  23. return buf;
  24. if (buf + data_len <= end) {
  25. memcpy(buf, data, data_len);
  26. return buf + data_len;
  27. }
  28. return buf;
  29. }
  30. bool copy_from_buf(const uint8_t** buf_ptr, const uint8_t* end, void* dest, size_t size) {
  31. if (__pval(*buf_ptr) + size < __pval(*buf_ptr)) // Pointer wrap check
  32. return false;
  33. if (end < *buf_ptr + size)
  34. return false;
  35. memcpy(dest, *buf_ptr, size);
  36. *buf_ptr += size;
  37. return true;
  38. }
  39. bool copy_size_and_data_from_buf(const uint8_t** buf_ptr, const uint8_t* end, size_t* size,
  40. UniquePtr<uint8_t[]>* dest) {
  41. if (!copy_uint32_from_buf(buf_ptr, end, size))
  42. return false;
  43. if (__pval(*buf_ptr) + *size < __pval(*buf_ptr)) // Pointer wrap check
  44. return false;
  45. if (*buf_ptr + *size > end)
  46. return false;
  47. if (*size == 0) {
  48. dest->reset();
  49. return true;
  50. }
  51. dest->reset(new (std::nothrow) uint8_t[*size]);
  52. if (!dest->get())
  53. return false;
  54. return copy_from_buf(buf_ptr, end, dest->get(), *size);
  55. }
  56. bool Buffer::reserve(size_t size) {
  57. if (available_write() < size) {
  58. size_t new_size = buffer_size_ + size - available_write();
  59. uint8_t* new_buffer = new (std::nothrow) uint8_t[new_size];
  60. if (!new_buffer)
  61. return false;
  62. memcpy(new_buffer, buffer_.get() + read_position_, available_read());
  63. memset_s(buffer_.get(), 0, buffer_size_);
  64. buffer_.reset(new_buffer);
  65. buffer_size_ = new_size;
  66. write_position_ -= read_position_;
  67. read_position_ = 0;
  68. }
  69. return true;
  70. }
  71. bool Buffer::Reinitialize(size_t size) {
  72. Clear();
  73. buffer_.reset(new (std::nothrow) uint8_t[size]);
  74. if (!buffer_.get())
  75. return false;
  76. buffer_size_ = size;
  77. read_position_ = 0;
  78. write_position_ = 0;
  79. return true;
  80. }
  81. bool Buffer::Reinitialize(const void* data, size_t data_len) {
  82. Clear();
  83. if (__pval(data) + data_len < __pval(data)) // Pointer wrap check
  84. return false;
  85. buffer_.reset(new (std::nothrow) uint8_t[data_len]);
  86. if (!buffer_.get())
  87. return false;
  88. buffer_size_ = data_len;
  89. memcpy(buffer_.get(), data, data_len);
  90. read_position_ = 0;
  91. write_position_ = buffer_size_;
  92. return true;
  93. }
  94. size_t Buffer::available_write() const {
  95. assert(buffer_size_ >= write_position_);
  96. return buffer_size_ - write_position_;
  97. }
  98. size_t Buffer::available_read() const {
  99. assert(buffer_size_ >= write_position_);
  100. assert(write_position_ >= read_position_);
  101. return write_position_ - read_position_;
  102. }
  103. bool Buffer::write(const uint8_t* src, size_t write_length) {
  104. if (available_write() < write_length)
  105. return false;
  106. memcpy(buffer_.get() + write_position_, src, write_length);
  107. write_position_ += write_length;
  108. return true;
  109. }
  110. bool Buffer::read(uint8_t* dest, size_t read_length) {
  111. if (available_read() < read_length)
  112. return false;
  113. memcpy(dest, buffer_.get() + read_position_, read_length);
  114. read_position_ += read_length;
  115. return true;
  116. }
  117. size_t Buffer::SerializedSize() const {
  118. return sizeof(uint32_t) + available_read();
  119. }
  120. uint8_t* Buffer::Serialize(uint8_t* buf, const uint8_t* end) const {
  121. return append_size_and_data_to_buf(buf, end, peek_read(), available_read());
  122. }
  123. bool Buffer::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) {
  124. Clear();
  125. if (!copy_size_and_data_from_buf(buf_ptr, end, &buffer_size_, &buffer_)) {
  126. buffer_.reset();
  127. buffer_size_ = 0;
  128. return false;
  129. }
  130. write_position_ = buffer_size_;
  131. return true;
  132. }
  133. void Buffer::Clear() {
  134. memset_s(buffer_.get(), 0, buffer_size_);
  135. buffer_.reset();
  136. read_position_ = 0;
  137. write_position_ = 0;
  138. buffer_size_ = 0;
  139. }
  140. } // namespace keymaster