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 #ifndef CHROME_UTILITY_SAFE_BROWSING_MAC_READ_STREAM_H_
6 #define CHROME_UTILITY_SAFE_BROWSING_MAC_READ_STREAM_H_
11 #include "base/macros.h"
13 namespace safe_browsing
{
16 // An interface for reading and seeking over a byte stream.
19 virtual ~ReadStream() {}
21 // Copies up to |size| bytes into |data|. Returns |true| on success with the
22 // actual number of bytes written stored in |size|. Returns |false| on error.
23 // At end-of-stream, returns |true| with |bytes_read| set to 0.
24 virtual bool Read(uint8_t* data
, size_t size
, size_t* bytes_read
) = 0;
26 // Calls |Read| but only returns true if the number of bytes read equals
28 bool ReadExact(uint8_t* data
, size_t size
);
32 return ReadExact(reinterpret_cast<uint8_t*>(t
), sizeof(T
));
35 // Seeks the read stream to |offset| from |whence|. |whence| is a POSIX-style
36 // SEEK_ constant. Returns the resulting offset location, or a negative value
38 virtual off_t
Seek(off_t offset
, int whence
) = 0;
41 // An implementation of ReadStream backed by a file descriptor. This does not
42 // take ownership of the file descriptor.
43 class FileReadStream
: public ReadStream
{
45 explicit FileReadStream(int fd
);
46 ~FileReadStream() override
;
49 bool Read(uint8_t* data
, size_t size
, size_t* bytes_read
) override
;
50 off_t
Seek(off_t offset
, int whence
) override
;
55 DISALLOW_COPY_AND_ASSIGN(FileReadStream
);
58 // An implementation of ReadStream that operates on a byte buffer. This class
59 // does not take ownership of the underlying byte array.
60 class MemoryReadStream
: public ReadStream
{
62 MemoryReadStream(const uint8_t* data
, size_t size
);
63 ~MemoryReadStream() override
;
66 bool Read(uint8_t* data
, size_t size
, size_t* bytes_read
) override
;
67 off_t
Seek(off_t offset
, int whence
) override
;
69 const uint8_t* data() const { return data_
; }
70 size_t size() const { return size_
; }
78 DISALLOW_COPY_AND_ASSIGN(MemoryReadStream
);
82 } // namespace safe_browsing
84 #endif // CHROME_UTILITY_SAFE_BROWSING_MAC_READ_STREAM_H_