1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/utility/safe_browsing/mac/read_stream.h"
12 #include "base/logging.h"
13 #include "base/posix/eintr_wrapper.h"
15 namespace safe_browsing
{
18 bool ReadStream::ReadExact(uint8_t* data
, size_t size
) {
19 size_t bytes_read
= 0;
20 return Read(data
, size
, &bytes_read
) && bytes_read
== size
;
23 FileReadStream::FileReadStream(int fd
) : fd_(fd
) {}
25 FileReadStream::~FileReadStream() {}
27 bool FileReadStream::Read(uint8_t* data
, size_t size
, size_t* bytes_read
) {
29 ssize_t signed_bytes_read
= HANDLE_EINTR(read(fd_
, data
, size
));
30 if (signed_bytes_read
< 0)
32 *bytes_read
= signed_bytes_read
;
36 off_t
FileReadStream::Seek(off_t offset
, int whence
) {
37 return lseek(fd_
, offset
, whence
);
40 MemoryReadStream::MemoryReadStream(const uint8_t* data
, size_t size
)
41 : data_(data
), size_(size
), offset_(0) {}
43 MemoryReadStream::~MemoryReadStream() {}
45 bool MemoryReadStream::Read(uint8_t* data
, size_t size
, size_t* bytes_read
) {
51 size_t bytes_remaining
= size_
- offset_
;
52 if (bytes_remaining
== 0)
55 *bytes_read
= std::min(size
, bytes_remaining
);
56 memcpy(data
, data_
+ offset_
, *bytes_read
);
57 offset_
+= *bytes_read
;
61 off_t
MemoryReadStream::Seek(off_t offset
, int whence
) {
70 offset_
= size_
+ offset
;
76 if (static_cast<size_t>(offset_
) >= size_
)
82 } // namespace safe_browsing