| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "util/file/string_file_writer.h" | 15 #include "util/file/string_file.h" |
| 16 | 16 |
| 17 #include <string.h> | 17 #include <string.h> |
| 18 | 18 |
| 19 #include <algorithm> |
| 20 |
| 19 #include "base/logging.h" | 21 #include "base/logging.h" |
| 20 #include "base/numerics/safe_math.h" | 22 #include "base/numerics/safe_math.h" |
| 21 #include "util/numeric/safe_assignment.h" | 23 #include "util/numeric/safe_assignment.h" |
| 22 | 24 |
| 23 namespace crashpad { | 25 namespace crashpad { |
| 24 | 26 |
| 25 StringFileWriter::StringFileWriter() : string_(), offset_(0) { | 27 StringFile::StringFile() : string_(), offset_(0) { |
| 26 } | 28 } |
| 27 | 29 |
| 28 StringFileWriter::~StringFileWriter() { | 30 StringFile::~StringFile() { |
| 29 } | 31 } |
| 30 | 32 |
| 31 void StringFileWriter::Reset() { | 33 void StringFile::SetString(const std::string& string) { |
| 34 string_ = string; |
| 35 offset_ = 0; |
| 36 } |
| 37 |
| 38 void StringFile::Reset() { |
| 32 string_.clear(); | 39 string_.clear(); |
| 33 offset_ = 0; | 40 offset_ = 0; |
| 34 } | 41 } |
| 35 | 42 |
| 36 bool StringFileWriter::Write(const void* data, size_t size) { | 43 ssize_t StringFile::Read(void* data, size_t size) { |
| 37 DCHECK(offset_.IsValid()); | 44 DCHECK(offset_.IsValid()); |
| 38 | 45 |
| 39 const size_t offset = offset_.ValueOrDie(); | 46 const size_t offset = offset_.ValueOrDie(); |
| 47 if (offset >= string_.size()) { |
| 48 return 0; |
| 49 } |
| 50 |
| 51 const size_t nread = std::min(size, string_.size() - offset); |
| 52 |
| 53 base::CheckedNumeric<ssize_t> new_offset = offset_; |
| 54 new_offset += nread; |
| 55 if (!new_offset.IsValid()) { |
| 56 LOG(ERROR) << "Read(): file too large"; |
| 57 return -1; |
| 58 } |
| 59 |
| 60 memcpy(data, &string_[offset], nread); |
| 61 offset_ = new_offset; |
| 62 |
| 63 return nread; |
| 64 } |
| 65 |
| 66 bool StringFile::Write(const void* data, size_t size) { |
| 67 DCHECK(offset_.IsValid()); |
| 68 |
| 69 const size_t offset = offset_.ValueOrDie(); |
| 40 if (offset > string_.size()) { | 70 if (offset > string_.size()) { |
| 41 string_.resize(offset); | 71 string_.resize(offset); |
| 42 } | 72 } |
| 43 | 73 |
| 44 base::CheckedNumeric<ssize_t> new_offset = offset_; | 74 base::CheckedNumeric<ssize_t> new_offset = offset_; |
| 45 new_offset += size; | 75 new_offset += size; |
| 46 if (!new_offset.IsValid()) { | 76 if (!new_offset.IsValid()) { |
| 47 LOG(ERROR) << "Write(): file too large"; | 77 LOG(ERROR) << "Write(): file too large"; |
| 48 return false; | 78 return false; |
| 49 } | 79 } |
| 50 | 80 |
| 51 string_.replace(offset, size, reinterpret_cast<const char*>(data), size); | 81 string_.replace(offset, size, reinterpret_cast<const char*>(data), size); |
| 52 offset_ = new_offset; | 82 offset_ = new_offset; |
| 53 | 83 |
| 54 return true; | 84 return true; |
| 55 } | 85 } |
| 56 | 86 |
| 57 bool StringFileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { | 87 bool StringFile::WriteIoVec(std::vector<WritableIoVec>* iovecs) { |
| 58 DCHECK(offset_.IsValid()); | 88 DCHECK(offset_.IsValid()); |
| 59 | 89 |
| 60 if (iovecs->empty()) { | 90 if (iovecs->empty()) { |
| 61 LOG(ERROR) << "WriteIoVec(): no iovecs"; | 91 LOG(ERROR) << "WriteIoVec(): no iovecs"; |
| 62 return false; | 92 return false; |
| 63 } | 93 } |
| 64 | 94 |
| 65 // Avoid writing anything at all if it would cause an overflow. | 95 // Avoid writing anything at all if it would cause an overflow. |
| 66 base::CheckedNumeric<ssize_t> new_offset = offset_; | 96 base::CheckedNumeric<ssize_t> new_offset = offset_; |
| 67 for (const WritableIoVec& iov : *iovecs) { | 97 for (const WritableIoVec& iov : *iovecs) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 80 | 110 |
| 81 #ifndef NDEBUG | 111 #ifndef NDEBUG |
| 82 // The interface says that |iovecs| is not sacred, so scramble it to make sure | 112 // The interface says that |iovecs| is not sacred, so scramble it to make sure |
| 83 // that nobody depends on it. | 113 // that nobody depends on it. |
| 84 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); | 114 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); |
| 85 #endif | 115 #endif |
| 86 | 116 |
| 87 return true; | 117 return true; |
| 88 } | 118 } |
| 89 | 119 |
| 90 FileOffset StringFileWriter::Seek(FileOffset offset, int whence) { | 120 FileOffset StringFile::Seek(FileOffset offset, int whence) { |
| 91 DCHECK(offset_.IsValid()); | 121 DCHECK(offset_.IsValid()); |
| 92 | 122 |
| 93 size_t base_offset; | 123 size_t base_offset; |
| 94 | 124 |
| 95 switch (whence) { | 125 switch (whence) { |
| 96 case SEEK_SET: | 126 case SEEK_SET: |
| 97 base_offset = 0; | 127 base_offset = 0; |
| 98 break; | 128 break; |
| 99 | 129 |
| 100 case SEEK_CUR: | 130 case SEEK_CUR: |
| (...skipping 28 matching lines...) Expand all Loading... |
| 129 << " invalid for size_t"; | 159 << " invalid for size_t"; |
| 130 return -1; | 160 return -1; |
| 131 } | 161 } |
| 132 | 162 |
| 133 offset_ = new_offset_sizet; | 163 offset_ = new_offset_sizet; |
| 134 | 164 |
| 135 return offset_.ValueOrDie(); | 165 return offset_.ValueOrDie(); |
| 136 } | 166 } |
| 137 | 167 |
| 138 } // namespace crashpad | 168 } // namespace crashpad |
| OLD | NEW |