File: read_stream.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 2,970 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/utility/safe_browsing/mac/read_stream.h"

#include <string.h>
#include <unistd.h>

#include <algorithm>
#include <array>

#include "base/check.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/posix/eintr_wrapper.h"

namespace safe_browsing {
namespace dmg {

bool ReadStream::ReadExact(base::span<uint8_t> buffer) {
  size_t bytes_read = 0;
  return Read(buffer, &bytes_read) && bytes_read == buffer.size();
}

FileReadStream::FileReadStream(int fd) : fd_(fd) {}

FileReadStream::~FileReadStream() = default;

bool FileReadStream::Read(base::span<uint8_t> buf, size_t* bytes_read) {
  *bytes_read = 0;
  ssize_t signed_bytes_read = HANDLE_EINTR(read(fd_, buf.data(), buf.size()));
  if (signed_bytes_read < 0)
    return false;
  *bytes_read = signed_bytes_read;
  return true;
}

off_t FileReadStream::Seek(off_t offset, int whence) {
  return lseek(fd_, offset, whence);
}

MemoryReadStream::MemoryReadStream(base::span<const uint8_t> byte_buf)
    : byte_buf_(byte_buf), offset_(0) {}

MemoryReadStream::~MemoryReadStream() = default;

bool MemoryReadStream::Read(base::span<uint8_t> buf, size_t* bytes_read) {
  *bytes_read = 0;

  size_t bytes_remaining = byte_buf_.size() - offset_;
  if (bytes_remaining == 0) {
    return true;
  }

  *bytes_read = std::min(buf.size(), bytes_remaining);
  buf.first(*bytes_read)
      .copy_from(byte_buf_.subspan(static_cast<size_t>(offset_), *bytes_read));
  offset_ += *bytes_read;
  return true;
}

off_t MemoryReadStream::Seek(off_t offset, int whence) {
  switch (whence) {
    case SEEK_SET:
      offset_ = offset;
      break;
    case SEEK_CUR:
      offset_ += offset;
      break;
    case SEEK_END:
      offset_ = byte_buf_.size() + offset;
      break;
    default:
      NOTREACHED();
  }
  if (!base::IsValueInRangeForNumericType<size_t>(offset_)) {
    offset_ = -1;
  } else {
    offset_ = std::min(base::checked_cast<size_t>(offset_), byte_buf_.size());
  }
  return offset_;
}

std::optional<std::vector<uint8_t>> ReadEntireStream(ReadStream& stream) {
  std::vector<uint8_t> data;
  std::array<uint8_t, 1024> buffer;
  size_t bytes_read = 0;
  do {
    if (!stream.Read(buffer, &bytes_read)) {
      return std::nullopt;
    }

    data.insert(data.end(), buffer.begin(), buffer.begin() + bytes_read);
  } while (bytes_read != 0);

  return data;
}

bool CopyStreamToFile(ReadStream& source, base::File& dest) {
  dest.Seek(base::File::Whence::FROM_BEGIN, 0);
  std::array<uint8_t, 1024> buffer;
  size_t bytes_read = 0;
  do {
    if (!source.Read(buffer, &bytes_read)) {
      return false;
    }
    if (!dest.WriteAtCurrentPosAndCheck(base::span(buffer).first(bytes_read))) {
      return false;
    }
  } while (bytes_read > 0);
  return true;
}

}  // namespace dmg
}  // namespace safe_browsing