Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: util/file/string_file.cc

Issue 936153002: Add FileReaderInterface. Move StringFileWriter to StringFile and (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Remove unused #include Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/file/string_file.h ('k') | util/file/string_file_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <limits>
21
19 #include "base/logging.h" 22 #include "base/logging.h"
20 #include "base/numerics/safe_math.h" 23 #include "base/numerics/safe_math.h"
21 #include "util/numeric/safe_assignment.h" 24 #include "util/numeric/safe_assignment.h"
22 25
23 namespace crashpad { 26 namespace crashpad {
24 27
25 StringFileWriter::StringFileWriter() : string_(), offset_(0) { 28 StringFile::StringFile() : string_(), offset_(0) {
26 } 29 }
27 30
28 StringFileWriter::~StringFileWriter() { 31 StringFile::~StringFile() {
29 } 32 }
30 33
31 void StringFileWriter::Reset() { 34 void StringFile::SetString(const std::string& string) {
35 CHECK_LE(string.size(),
36 implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()));
37 string_ = string;
38 offset_ = 0;
39 }
40
41 void StringFile::Reset() {
32 string_.clear(); 42 string_.clear();
33 offset_ = 0; 43 offset_ = 0;
34 } 44 }
35 45
36 bool StringFileWriter::Write(const void* data, size_t size) { 46 ssize_t StringFile::Read(void* data, size_t size) {
37 DCHECK(offset_.IsValid()); 47 DCHECK(offset_.IsValid());
38 48
39 const size_t offset = offset_.ValueOrDie(); 49 const size_t offset = offset_.ValueOrDie();
50 if (offset >= string_.size()) {
51 return 0;
52 }
53
54 const size_t nread = std::min(size, string_.size() - offset);
55
56 base::CheckedNumeric<ssize_t> new_offset = offset_;
57 new_offset += nread;
58 if (!new_offset.IsValid()) {
59 LOG(ERROR) << "Read(): file too large";
60 return -1;
61 }
62
63 memcpy(data, &string_[offset], nread);
64 offset_ = new_offset;
65
66 return nread;
67 }
68
69 bool StringFile::Write(const void* data, size_t size) {
70 DCHECK(offset_.IsValid());
71
72 const size_t offset = offset_.ValueOrDie();
40 if (offset > string_.size()) { 73 if (offset > string_.size()) {
41 string_.resize(offset); 74 string_.resize(offset);
42 } 75 }
43 76
44 base::CheckedNumeric<ssize_t> new_offset = offset_; 77 base::CheckedNumeric<ssize_t> new_offset = offset_;
45 new_offset += size; 78 new_offset += size;
46 if (!new_offset.IsValid()) { 79 if (!new_offset.IsValid()) {
47 LOG(ERROR) << "Write(): file too large"; 80 LOG(ERROR) << "Write(): file too large";
48 return false; 81 return false;
49 } 82 }
50 83
51 string_.replace(offset, size, reinterpret_cast<const char*>(data), size); 84 string_.replace(offset, size, reinterpret_cast<const char*>(data), size);
52 offset_ = new_offset; 85 offset_ = new_offset;
53 86
54 return true; 87 return true;
55 } 88 }
56 89
57 bool StringFileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { 90 bool StringFile::WriteIoVec(std::vector<WritableIoVec>* iovecs) {
58 DCHECK(offset_.IsValid()); 91 DCHECK(offset_.IsValid());
59 92
60 if (iovecs->empty()) { 93 if (iovecs->empty()) {
61 LOG(ERROR) << "WriteIoVec(): no iovecs"; 94 LOG(ERROR) << "WriteIoVec(): no iovecs";
62 return false; 95 return false;
63 } 96 }
64 97
65 // Avoid writing anything at all if it would cause an overflow. 98 // Avoid writing anything at all if it would cause an overflow.
66 base::CheckedNumeric<ssize_t> new_offset = offset_; 99 base::CheckedNumeric<ssize_t> new_offset = offset_;
67 for (const WritableIoVec& iov : *iovecs) { 100 for (const WritableIoVec& iov : *iovecs) {
(...skipping 12 matching lines...) Expand all
80 113
81 #ifndef NDEBUG 114 #ifndef NDEBUG
82 // The interface says that |iovecs| is not sacred, so scramble it to make sure 115 // The interface says that |iovecs| is not sacred, so scramble it to make sure
83 // that nobody depends on it. 116 // that nobody depends on it.
84 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); 117 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size());
85 #endif 118 #endif
86 119
87 return true; 120 return true;
88 } 121 }
89 122
90 FileOffset StringFileWriter::Seek(FileOffset offset, int whence) { 123 FileOffset StringFile::Seek(FileOffset offset, int whence) {
91 DCHECK(offset_.IsValid()); 124 DCHECK(offset_.IsValid());
92 125
93 size_t base_offset; 126 size_t base_offset;
94 127
95 switch (whence) { 128 switch (whence) {
96 case SEEK_SET: 129 case SEEK_SET:
97 base_offset = 0; 130 base_offset = 0;
98 break; 131 break;
99 132
100 case SEEK_CUR: 133 case SEEK_CUR:
(...skipping 28 matching lines...) Expand all
129 << " invalid for size_t"; 162 << " invalid for size_t";
130 return -1; 163 return -1;
131 } 164 }
132 165
133 offset_ = new_offset_sizet; 166 offset_ = new_offset_sizet;
134 167
135 return offset_.ValueOrDie(); 168 return offset_.ValueOrDie();
136 } 169 }
137 170
138 } // namespace crashpad 171 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/file/string_file.h ('k') | util/file/string_file_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698