Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / utility / safe_browsing / mac / read_stream.cc
blobac904675a4d596cf8d67397fb7af657a930f3c5f
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"
7 #include <string.h>
8 #include <unistd.h>
10 #include <algorithm>
12 #include "base/logging.h"
13 #include "base/posix/eintr_wrapper.h"
15 namespace safe_browsing {
16 namespace dmg {
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) {
28 *bytes_read = 0;
29 ssize_t signed_bytes_read = HANDLE_EINTR(read(fd_, data, size));
30 if (signed_bytes_read < 0)
31 return false;
32 *bytes_read = signed_bytes_read;
33 return true;
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) {
46 *bytes_read = 0;
48 if (data_ == nullptr)
49 return false;
51 size_t bytes_remaining = size_ - offset_;
52 if (bytes_remaining == 0)
53 return true;
55 *bytes_read = std::min(size, bytes_remaining);
56 memcpy(data, data_ + offset_, *bytes_read);
57 offset_ += *bytes_read;
58 return true;
61 off_t MemoryReadStream::Seek(off_t offset, int whence) {
62 switch (whence) {
63 case SEEK_SET:
64 offset_ = offset;
65 break;
66 case SEEK_CUR:
67 offset_ += offset;
68 break;
69 case SEEK_END:
70 offset_ = size_ + offset;
71 break;
72 default:
73 NOTREACHED();
74 return -1;
76 if (static_cast<size_t>(offset_) >= size_)
77 offset_ = size_;
78 return offset_;
81 } // namespace dmg
82 } // namespace safe_browsing